Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Example (simple): SMA proportioned positions

Here's a simple toy strategy for beginners to play with. Add additional securities, or alter the MA periods to see if you can smooth the curve out.

(We still can't see the user recorded indicator lines legend...!!!)

def initialize(context):  
    context.MetricSymbol   = symbol('SPY')  
    context.RiskOnSymbols  = symbols('SPY','QQQ')  
    context.RiskOffSymbols = symbols('TLT','AGG')

def handle_data(context, data):  
    if get_open_orders():  
        return

    metricClose = data[context.MetricSymbol].close_price  
    metricSMA   = data[context.MetricSymbol].mavg(21)  
    metricSMA2  = data[context.MetricSymbol].mavg(84)

    riskOnPct  = (metricSMA/metricClose) * ((metricSMA *2 / metricSMA2) *.25) /len(context.RiskOnSymbols)  
    riskOffPct = (metricClose/metricSMA) * ((metricSMA2 *2 / metricSMA) *.25) /len(context.RiskOffSymbols)

    for securityID in context.RiskOnSymbols:  
        order_target_percent(securityID, riskOnPct)  
    for securityID in context.RiskOffSymbols:  
        order_target_percent(securityID, riskOffPct)

    record(RiskOnPortion  = riskOnPct)  
    record(RiskOffPortion = riskOffPct)  
    record(Leverage = context.account.leverage)