Back to Community
simple moving average strategy

When the average closing price of previous 5 days is higher than that of 60 days, the stock price has a rise tendency, buy. Vice versa.
I try to write the code myself according to api instructions, but there's always mistake. So finally I cloned from the instruction, which works pretty fine.
Here's the algorithm unable to run backtest:

def initialize(context):
context.security=sid(24)
schedule_function(rebalance,date_rule=date_rules.every_day())
def rebalance(context,data):
hist1 = data.history(context.security,field='price',bar_count=5,frequency='1d')
hist2 = data.history(context.security,field='price',bar_count=60,frequency='1d')
MA5 = hist1.mean()
MA60 = hist2.mean()

if data.can_trade(context.security):  

if MA5 > MA60:  
       order_target_percent(context.security,1.0)  
    elif MA5 < MA60:  
        order_target_percent(context.security,0)  
Clone Algorithm
13
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a2078c64ff4293fa1a1a858
There was a runtime error.
1 response

@ Zhilan,

Here's the algorithm unable to run backtest:

Should be

fields='price'  

not
field='price'