<s> """<STR_LIT>""" <EOL> from zipline . api import order_target , record , symbol <EOL> def initialize ( context ) : <EOL> context . sym = symbol ( '<STR_LIT>' ) <EOL> context . i = <NUM_LIT:0> <EOL> def handle_data ( context , data ) : <EOL> context . i += <NUM_LIT:1> <EOL> if context . i < <NUM_LIT> : <EOL> return <EOL> short_mavg = data . history ( context . sym , '<STR_LIT>' , <NUM_LIT:100> , '<STR_LIT>' ) . mean ( ) <EOL> long_mavg = data . history ( context . sym , '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' ) . mean ( ) <EOL> if short_mavg > long_mavg : <EOL> order_target ( context . sym , <NUM_LIT:100> ) <EOL> elif short_mavg < long_mavg : <EOL> order_target ( context . sym , <NUM_LIT:0> ) <EOL> record ( AAPL = data . current ( context . sym , "<STR_LIT>" ) , <EOL> short_mavg = short_mavg , <EOL> long_mavg = long_mavg ) <EOL> def analyze ( context = None , results = None ) : <EOL> import matplotlib . pyplot as plt <EOL> import logbook <EOL> logbook . StderrHandler ( ) . push_application ( ) <EOL> log = logbook . Logger ( '<STR_LIT>' ) <EOL> fig = plt . figure ( ) <EOL> ax1 = fig . add_subplot ( <NUM_LIT> ) <EOL> results . portfolio_value . plot ( ax = ax1 ) <EOL> ax1 . set_ylabel ( '<STR_LIT>' ) <EOL> ax2 = fig . add_subplot ( <NUM_LIT> ) <EOL> ax2 . set_ylabel ( '<STR_LIT>' ) <EOL> if ( '<STR_LIT>' in results and '<STR_LIT>' in results and <EOL> '<STR_LIT>' in results ) : <EOL> results [ '<STR_LIT>' ] . plot ( ax = ax2 ) <EOL> results [ [ '<STR_LIT>' , '<STR_LIT>' ] ] . plot ( ax = ax2 ) <EOL> trans = results . ix [ [ t != [ ] for t in results . transactions ] ] <EOL> buys = trans . ix [ [ t [ <NUM_LIT:0> ] [ '<STR_LIT>' ] > <NUM_LIT:0> for t in <EOL> trans . transactions ] ] <EOL> sells = trans . ix [ <EOL> [ t [ <NUM_LIT:0> ] [ '<STR_LIT>' ] < <NUM_LIT:0> for t in trans . transactions ] ] <EOL> ax2 . plot ( buys . index , results . short_mavg . ix [ buys . index ] , <EOL> '<STR_LIT>' , markersize = <NUM_LIT:10> , color = '<STR_LIT:m>' ) <EOL> ax2 . plot ( sells . index , results . short_mavg . ix [ sells . index ] , <EOL> '<STR_LIT:v>' , markersize = <NUM_LIT:10> , color = '<STR_LIT:k>' ) <EOL> plt . legend ( loc = <NUM_LIT:0> ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> ax2 . annotate ( msg , xy = ( <NUM_LIT:0.1> , <NUM_LIT:0.5> ) ) <EOL> log . info ( msg ) <EOL> plt . show ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from datetime import datetime <EOL> import pytz <EOL> from zipline . algorithm import TradingAlgorithm <EOL> from zipline . utils . factory import load_bars_from_yahoo <EOL> start = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , pytz . utc ) <EOL> end = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , pytz . utc ) <EOL> data = load_bars_from_yahoo ( <EOL> stocks = [ '<STR_LIT>' ] , <EOL> indexes = { } , <EOL> start = start , <EOL> end = end , <EOL> ) <EOL> algo = TradingAlgorithm ( initialize = initialize , handle_data = handle_data ) <EOL> results = algo . run ( data ) <EOL> analyze ( results = results ) </s>
