<s> from pyalgotrade import strategy <EOL> from pyalgotrade import plotter <EOL> from pyalgotrade . tools import yahoofinance <EOL> from pyalgotrade . technical import vwap <EOL> from pyalgotrade . stratanalyzer import sharpe <EOL> class VWAPMomentum ( strategy . BacktestingStrategy ) : <EOL> def __init__ ( self , feed , instrument , vwapWindowSize , threshold ) : <EOL> super ( VWAPMomentum , self ) . __init__ ( feed ) <EOL> self . __instrument = instrument <EOL> self . __vwap = vwap . VWAP ( feed [ instrument ] , vwapWindowSize ) <EOL> self . __threshold = threshold <EOL> def getVWAP ( self ) : <EOL> return self . __vwap <EOL> def onBars ( self , bars ) : <EOL> vwap = self . __vwap [ - <NUM_LIT:1> ] <EOL> if vwap is None : <EOL> return <EOL> shares = self . getBroker ( ) . getShares ( self . __instrument ) <EOL> price = bars [ self . __instrument ] . getClose ( ) <EOL> notional = shares * price <EOL> if price > vwap * ( <NUM_LIT:1> + self . __threshold ) and notional < <NUM_LIT> : <EOL> self . marketOrder ( self . __instrument , <NUM_LIT:100> ) <EOL> elif price < vwap * ( <NUM_LIT:1> - self . __threshold ) and notional > <NUM_LIT:0> : <EOL> self . marketOrder ( self . __instrument , - <NUM_LIT:100> ) <EOL> def main ( plot ) : <EOL> instrument = "<STR_LIT>" <EOL> vwapWindowSize = <NUM_LIT:5> <EOL> threshold = <NUM_LIT> <EOL> feed = yahoofinance . build_feed ( [ instrument ] , <NUM_LIT> , <NUM_LIT> , "<STR_LIT:.>" ) <EOL> strat = VWAPMomentum ( feed , instrument , vwapWindowSize , threshold ) <EOL> sharpeRatioAnalyzer = sharpe . SharpeRatio ( ) <EOL> strat . attachAnalyzer ( sharpeRatioAnalyzer ) <EOL> if plot : <EOL> plt = plotter . StrategyPlotter ( strat , True , False , True ) <EOL> plt . getInstrumentSubplot ( instrument ) . addDataSeries ( "<STR_LIT>" , strat . getVWAP ( ) ) <EOL> strat . run ( ) <EOL> print "<STR_LIT>" % sharpeRatioAnalyzer . getSharpeRatio ( <NUM_LIT> ) <EOL> if plot : <EOL> plt . plot ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( True ) </s>
