Here is a small variation from one of Henry Carstens "rough" 101 Trading Ideas systems from http://henrycarstens.com/101-trading-ideas-silver-and-gold-gold-silver-strategy-101tradingideas/

This version uses ETFs instead of futures.

This is long-only variation and because "17 days" of holding felt too overoptimized I changed it to round 20.

I usually don't like short/long versions of the same algo (long/short volatility is usually not symmetrical FX being the main exception to the rule)

It is also said on the end of the post that "this also works with crude" so I combined both crude (USO in this case) and gold (GLD) systems. Both have leverage level of 0.5.

Clone Algorithm

106

Loading...

There was an error loading this backtest.
Retry

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 |

# http://henrycarstens.com/101-trading-ideas-silver-and-gold-gold-silver-strategy-101tradingideas/ import numpy as np from scipy import stats from scipy.optimize import minimize import pandas as pd import talib import time import math import random as rand def initialize(context): context.leverage = 1. context.hold_days = 20 schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes=10)) schedule_function(update_chart, date_rules.every_day(), time_rules.market_close(minutes=5)) context.weights = pd.Series() context.eqs = [sid(26807), # gld sid(28320), # uso ] context.silver = sid(28368) context.close_trade = {} def update_chart(context,data): record(leverage = context.account.leverage) # record(opt_dd=context.opt_dd) # log.debug("Leverage: %f" % (context.account.leverage)) longs = shorts = 0 long_lever = 0 short_lever = 0 for position in context.portfolio.positions.itervalues(): if position.amount > 0: longs += 1 long_lever += position.amount*data.current(position.sid, 'price') if position.amount < 0: shorts += 1 short_lever += -position.amount*data.current(position.sid, 'price') #record(long_count=longs, short_count=shorts) long_lever /= context.portfolio.portfolio_value short_lever /= context.portfolio.portfolio_value record(long_lever=long_lever, short_lever=short_lever) def rebalance(context, data): hist_eqs = context.eqs + [context.silver] c = data.history(hist_eqs, 'close', 100 ,'1d').iloc[:-1] weights = pd.Series(0., index=context.eqs) for eq in context.eqs: if eq in context.close_trade: context.close_trade[eq] -= 1 if context.close_trade[eq] > 0: weights[eq] = context.weights[eq] #* 0.9 #log.debug("%s in trade %d days, weight %f" % (eq, context.close_trade[eq], weights[eq])) else: weights[eq] = 0 del context.close_trade[eq] if weights[eq] == 0: # Not in trade p = data.current(eq, 'price') if (c[eq].iloc[-20:].mean() > c[eq].iloc[-40:-20].mean() and p < c[eq].iloc[-20:].mean() and c[context.silver].iloc[-40:].mean() < c[context.silver].iloc[-60:-20].mean()): # Open trade weights[eq] = 1. / len(context.eqs) context.close_trade[eq] = context.hold_days #if (c[eq].iloc[-20:].mean() < c[eq].iloc[-40:-20].mean() and # p > c[eq].iloc[-20:].mean() and # c[context.silver].iloc[-40:].mean() > c[context.silver].iloc[-60:-20].mean()): # # Open trade # weights[eq] = -1. # context.close_trade[eq] = context.hold_days if weights.abs().sum() > 1.: weights /= weights.abs().sum() weights *= context.leverage log.debug(weights) for eq in weights.index: order_target_percent(eq, weights[eq]) context.weights = weights