<s> from pyalgotrade import strategy <EOL> from pyalgotrade . technical import ma <EOL> from pyalgotrade . technical import cross <EOL> class SMACrossOver ( strategy . BacktestingStrategy ) : <EOL> def __init__ ( self , feed , instrument , smaPeriod ) : <EOL> super ( SMACrossOver , self ) . __init__ ( feed ) <EOL> self . __instrument = instrument <EOL> self . __position = None <EOL> self . setUseAdjustedValues ( True ) <EOL> self . __prices = feed [ instrument ] . getPriceDataSeries ( ) <EOL> self . __sma = ma . SMA ( self . __prices , smaPeriod ) <EOL> def getSMA ( self ) : <EOL> return self . __sma <EOL> def onEnterCanceled ( self , position ) : <EOL> self . __position = None <EOL> def onExitOk ( self , position ) : <EOL> self . __position = None <EOL> def onExitCanceled ( self , position ) : <EOL> self . __position . exitMarket ( ) <EOL> def onBars ( self , bars ) : <EOL> if self . __position is None : <EOL> if cross . cross_above ( self . __prices , self . __sma ) > <NUM_LIT:0> : <EOL> shares = int ( self . getBroker ( ) . getCash ( ) * <NUM_LIT> / bars [ self . __instrument ] . getPrice ( ) ) <EOL> self . __position = self . enterLong ( self . __instrument , shares , True ) <EOL> elif not self . __position . exitActive ( ) and cross . cross_below ( self . __prices , self . __sma ) > <NUM_LIT:0> : <EOL> self . __position . exitMarket ( ) </s>
