I'm coming over from Quantopian, and I am trying to get an algo that was shared there to work on QuantConnect.
I've attached the backtest of the code, in which I've done a few changes to try to get it to work on QuantConnect. It would be great if someone can help make the remaining adjustments. I'm currently still learning Python, and with the switch from Quantopian, it's been a bit overwhelming.
The following is the original code from Quantopian:
STD=21
LEV=1
MAXH=252
def initialize(context):
# date_rules.week_start(days_offset=0)
# date_rules.every_day()
set_commission(commission.PerShare(cost=0.00, min_trade_cost=1))
schedule_function(trade, date_rules.week_start(days_offset=0), time_rules.market_open(minutes = 33))
def trade(context, data):
qqq_symbol = symbol('TQQQ')
tmf_symbol = symbol('TMF')
edz_symbol = symbol('EDZ')
prices_qqq = data.history(qqq_symbol,'close', STD,'1d')
prices_tmf = data.history(tmf_symbol,'close', STD,'1d')
prices_edz = data.history(edz_symbol,'close', STD,'1d')
W=14
R_qqq = prices_qqq.pct_change()[-W:-1]
R_tmf = prices_tmf.pct_change()[-W:-1]
R_edz = prices_edz.pct_change()[-W:-1]
current_qqq = data.current(qqq_symbol, 'price')
max_qqq = data.history(qqq_symbol, 'price', MAXH, '1d').max()
raw_wt_qqq = 0.50*((max_qqq/current_qqq)**2)/(R_qqq.std()**2)
raw_wt_tmf = 0.20/(R_tmf.std()**2)
raw_wt_edz = 0.30/(R_edz.std()**2)
wt = abs(raw_wt_qqq) + abs(raw_wt_tmf) + abs(raw_wt_edz)
#print("raw vola %.5f, %.5f, %.5f"%(R_qqq.std()**2, R_tmf.std()**2, R_edz.std()**2))
#print("raw wt %.5f, %.5f, %.5f"%(raw_wt_qqq,raw_wt_tmf, raw_wt_edz))
wt_qqq = 1*raw_wt_qqq/wt
wt_tmf = 1*raw_wt_tmf/wt
wt_edz = 1*raw_wt_edz/wt
record(qqq_symbol= wt_qqq)
record(tmf_symbol= wt_tmf)
record(edz_symbol= wt_edz)
if wt_edz > 0.25:
wt_edz=2*wt_edz/0.25*wt_edz
wt = abs(wt_qqq) + abs(wt_tmf) + abs(wt_edz)
wt_qqq = 1*wt_qqq/wt
wt_tmf = 1*wt_tmf/wt
wt_edz = 1*wt_edz/wt
order_target_percent(qqq_symbol, wt_qqq*LEV)
order_target_percent(tmf_symbol, wt_tmf*LEV)
order_target_percent(edz_symbol, wt_edz*LEV)
record(lever=context.account.leverage)
Shile Wen
Hi Martin,
To implement this, I would suggest learning our API. A good way to start learning how to use our API is through our BootCamp. If you would like more individualized help, we have 1-on-1 tutoring service for a small fee. Furthermore, another good way to start is through Alex’s guide on how to migrate an algorithm from Quantopian to QuantConnect.
Best,
Shile Wen
Martin Grunin
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!