Wednesday, 13 November 2019

python quantopian 2 buy & sell

simulate trade performance based on python algorithm

def initialize(context):
    context.aapl = sid(24)
    
def handle_data(context, data):
    hist = data.history(context.aapl, 'price', 50, '1d')
    log.info(hist)
    #last 50 day average
    sma_50 = hist.mean()
    #last 20 day average
    sma_20 = hist[-20:].mean()
    
    if sma_20 > sma_50:
       #buy apple with all money
       order_target_percent(context.aapl, 1.0)
           
    elif sma_50 > sma_20:
       #sell all apple share
       order_target_percent(context.aapl, -1.0)

reference:

No comments:

Post a Comment