Wednesday, 13 November 2019

python quantopian 3 schedule, benchmark

algorithm trade lose holding stock (not trading) by 28%

def initialize(context):
    #performance for holding stock without trade
    set_benchmark(sid(24))
    context.aapl = sid(24)
    #schedule function fires everyday after market open 1h instead of default every minute.
    #up side: faster analysis, fewer trades, downside poor return.    
    schedule_function(ma_crossover_handling, date_rules.every_day(), time_rules.market_open(hours=1))
 
def ma_crossover_handling(context, data):
    hist = data.history(context.aapl, 'price', 50, '1d')
    log.info(hist)
    sma_50 = hist.mean()
    sma_20 = hist[-20:].mean()
 
    if sma_20 > sma_50:
       order_target_percent(context.aapl, 1.0)
         
    elif sma_50 > sma_20:
       order_target_percent(context.aapl, -1.0)

No comments:

Post a Comment