Here is something I tried and it looks decent.

Clone Algorithm

257

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 numpy as np import statsmodels.api as smapi def initialize(context): set_symbol_lookup_date('2015-05-01') context.XLE = sid(19655) context.stocks = symbols('XOM','CVX','SLB','KMI','EOG','COP','PXD','APC','OXY','HAL','WMB','PSX','VLO','BHI','TSO','SE') schedule_function(myfunc,date_rule=date_rules.every_day(),time_rule=time_rules.market_close(minutes=30)) def handle_data(context, data): record(l=context.account.leverage) pass def myfunc(context, data): prices = history(90, "1d", "price") prices = prices.drop([context.XLE], axis=1) ret = prices.pct_change(5) ret.fillna(0, inplace=True) ret = np.log1p(ret).values cumret = ret #np.cumsum(ret, axis=0) xle = np.mean(cumret, axis=1) i = 0 score = [] for sid in prices: diff = np.diff(cumret[:,i]) X = smapi.add_constant(diff, prepend=True) Y = np.diff(cumret[:,i] - xle) res = smapi.OLS(Y, X).fit() score.append(res.params[1] / res.ssr) i += 1 netscore = np.sum(np.abs(score)) i = 0 wsum = 0 for sid in prices: try: val = 140000 * score[i] / netscore order_target_value(sid, val) wsum += val except: log.info("exception") i += 1 continue i += 1 order_target_value(context.XLE, -wsum)