Hi guys,
I'm trying to migrate the Percentile Channels from Quantopian to QuantConnect. I'm getting some runtime error. Any pointers / guides?
import numpy as np
import pandas as pd
class BasicTemplateAlgorithm(QCAlgorithm):
# Assets from CSSA blog (VTI, ICF, LQD, DBC, cash = SHY)
active = [
"VTI", # VTI - Vanguard Total Stock Market ETF (Inception: 24 May 01)
"VNQ", # VNQ - Vanguard REIT ETF (U.S. Real Estate) (Inception: 23 Sep 04)
"LQD", # LQD - iShares iBoxx $ Investment Grade Corporate Bond ETF (Inception: 22 Jul 02)
"TLT", # TLT - iShares 20+ Year Treasury Bond ETF
"DBC", # DBC - PowerShares DB Commodity Index Tracking Fund (Inception: 3 Feb 06)
]
cash = "SHY"
channels = [60, 120, 180, 252]
entry = 0.75
exit = 0.25
signals = pd.DataFrame(0., index=channels, columns=active)
LEVERAGE = 1.0
def Initialize(self):
# Set the cash we'd like to use for our backtest
# This is ignored in live trading
self.SetCash(100000)
# Start and end dates for the backtest.
# These are ignored in live trading.
self.SetStartDate(2010,1,1)
self.SetEndDate(2017,1,1)
# Add assets to be used
for symbol in self.active:
self.AddEquity(symbol, Resolution.Minute)
self.AddEquity(self.cash, Resolution.Minute)
# Schedule functions
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("SHY", 5), Action(self.CheckChannels))
self.Schedule.On(self.DateRules.MonthStart("SHY"), self.TimeRules.BeforeMarketClose("SHY", 30), Action(self.Rebalance))
def OnData(self, slice):
# Simple buy and hold template
#if not self.Portfolio.Invested:
# self.SetHoldings(self.spy, 1)
# self.Debug("numpy test >>> print numpy.pi: " + str(np.pi))
pass
def CheckChannels(self):
#h = self.History(self.active, 252, Resolution.Day)[self.active]
h = self.History(self.symbols, 252, Resolution.Daily)
for c in self.channels:
channel_prices = h[-c:]
entry = channel_prices.quantile(self.entry)
exit = channel_prices.quantile(self.exit)
for s in self.active:
if h[s][-1] >= entry[s]:
self.signals[s][c] = 1
if h[s][-1] <= exit[s]:
self.signals[s][c] = -1
def Rebalance(self):
#h = self.History(self.active, 21, Resolution.Daily)[self.active]
h = self.History(self.active, 21, Resolution.Daily)
# Allocation
#inv_vol = 1. / np.log(h)/diff().std()
inv_vol = np.log(h)
inv_vol = np.diff(inv_vol)
inv_vol = np.std(inv_vol)
inv_vol = 1. / inv_vol
channel_weight = self.signals.sum() / len(self.channels)
active_weight = channel_weight * inv_vol
active_weight = active_weight.abs() / active_weight.abs().sum()
active_weight[channel_weight < 0.] = 0.
active_weight = active_weight.fillna(0)
# Ordering
for s in self.active:
self.SetHoldings(s, active_weight[s] * self.LEVERAGE)
cash_weight = 1. - active_weight.sum()
self.SetHoldings(self.cash, cash_weight)
I have commented out the two lines self.Schedule.On() so that the failed backtest can be attached.
Alexandre Catarino
The pandas.DataFrame from our History is different from Quantopian. Here is a thread where we talked about it:
Please checkout the attached backtest, where we made the necessary changes.
Benjamin Ng
Thanks Alexandre Catarino ! Will check out the other thread!
Benjamin Ng
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!