Sell when price is higher than 1.75 standard deviations above the 20-day Moving Average after closing any existing long position.

Buy when price is lower than 1.75 standard deviations below the 20-day Moving Average after closing any existing short position.

I expect there to be one or more errors.

Regards,

Peter

Clone Algorithm

1761

Loading...

There was an error loading this backtest.

Backtest from
to
with
initial capital

Cumulative performance:

Algorithm
Benchmark

Custom data:

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 |

import pandas def process_df(df): df = df.rename(columns={'Close': 'price'}) df = df.fillna(method='ffill') df['MA20']=pandas.stats.moments.rolling_mean(df['price'], 20) df['ABS']=abs(df['price']-df['MA20']) df['STDDEV']=pandas.stats.moments.rolling_std(df['ABS'], 20) df['UPPER_BB']=df['MA20']+2*df['STDDEV'] df['LOWER_BB']=df['MA20']-2*df['STDDEV'] return df def initialize(context): fetch_csv('https://raw.githubusercontent.com/pcawthron/StockData/master/CMG%202011%20Daily%20Close.csv', date_column='Date', symbol='CMG', usecols=['Close'], post_func = process_df, date_format='%d/%m/%Y' ) context.stock = sid(28016) context.qty = 400 context.stddev_limit = 1.75 def handle_data(context, data): if str(data['CMG'].datetime.year) == "2011": record(CMG=data['CMG'].price) record(Upper=data['CMG']['UPPER_BB']) record(MA20=data['CMG']['MA20']) record(Lower=data['CMG']['LOWER_BB']) order_handling(context, data) def order_handling(context, data): # At top of bands? if data['CMG'].price > data['CMG']['MA20'] + data['CMG']['STDDEV'] * context.stddev_limit : # Are we long or neutral? if context.portfolio.positions[sid(28016)].amount >= 0: # Close our long position if we have one close_position(context, data) order(sid(28016), -context.qty) # At bottom of bands? if data['CMG'].price < data['CMG']['MA20'] - data['CMG']['STDDEV'] * context.stddev_limit : # Are we short or neutral? if context.portfolio.positions[sid(28016)].amount <= 0: # Close our short position if we have one close_position(context, data) order(sid(28016), context.qty) return def close_position(context, data): # Open position? if context.portfolio.positions[sid(28016)].amount > 0: order(sid(28016), -context.qty) elif context.portfolio.positions[sid(28016)].amount < 0: order(sid(28016), context.qty) return

We have migrated this algorithm to work with a new version of the Quantopian API. The code is different than the original version, but the investment rationale of the algorithm has not changed. We've put everything you need to know here on one page.

This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.