Hi guys I was trying a simple strategy for getting better at using the qc platform but just when I tried to backtest it the results are not displayed on the chart. It acts like if I didn't wrote any code, do you know why?
import numpy as np
from clr import AddReference
AddReference("System")
AddReference("NodaTime")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
from datetime import datetime, timedelta
from System import *
from NodaTime import DateTimeZone
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Brokerages import *
from QuantConnect.Data.Market import *
from datetime import timedelta
class DataConsolidationAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetCash(30000)
self.StopRisk = 1
if self.Portfolio.TotalUnrealizedProfit > 599.99: #take profit
self.Liquidate()
self.SetStartDate(2018,1,1) #Set Start Date
self.SetEndDate(2021,1,1) #Set End Date
self.AddEquity("symbol", Resolution.Minute)
self.ema = self.EMA("symbol", 30, MovingAverageType.Simple, Resolution.Minute)
self.vwap = self.VWAP("symbol", 30, Resolution.Minute)
fiveMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=5))
self.SubscriptionManager.AddConsolidator("symbol", fiveMinuteConsolidator)
def fiveMinuteBarHandler(self, sender, bar):
self.Debug(str(self.Time) + " " + str(bar))
def OnData(self, data):
if not self.ema.IsReady:
return
if not self.vawap.IsReady:
return
if self.ema > self.vwap:
self.MarketOrder("symbol",1)
if self.ema < self.vwap:
self.MarketOrder("symbol",-1)
The backtest is also saying on the research guide that there are 7 parameters detected and for so is likely overfitting...seems strange.
Thankyou for your time!
Derek Melchin
Hi Pietro,
The main issue is that the indentation of `fiveMinuteBarHandler` and `OnData` needs fixing. In addition, when comparing indicators of different type (EMA and VWAP), we should compare their values. For example,
ema = self.ema.Current.Value vwap = self.vwap.Current.Value if ema > vwap and not self.Portfolio['BOXL'].IsLong: self.MarketOrder("BOXL",1) if ema < vwap and not self.Portfolio['BOXL'].IsShort: self.MarketOrder("BOXL",-1)
See the attached backtest for reference.
In regards to the parameters, these are just the hard-coded values in the algorithm. For instance:
Best,
Derek Melchin
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.
Pietro ciarmatori
Hi Derek, thankyou for your help, but I didn't understand what do you mean by: "these are just the hard-coded values in the algorithm".
So what do I need to do for making them work?
I tried to look for some help in the docs but I didn't manage to find anything.
Best regards.
Derek Melchin
Hi Pietro,
Hard-coded values are literal values that we directly specify in the source code. For instance, 1 is a hard-coded value in the line
self.MarketOrder("BOXL",1)
To make the algorithm work, click the "Clone Algorithm" button in the backtest embedded above.
Best,
Derek Melchin
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.
Pietro ciarmatori
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!