Back

Error when testing futures EMA strategy

Hi there, I am pretty new to the API, jstu finished all the bootcamps, and am now trying to familiarise myself with the API by writing a simple backtest using futures, I got a runtime error though and have been scratching my head for a while as I am not sure how to debug this, googling does not find anything and the message seems a bit too generic, I think I have all the components for a backtest but somehow the code fails. this is my code (mostly drag and drop from the menu really, which is why it kinda surprise me that it fails):

from Alphas.EmaCrossAlphaModel import EmaCrossAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from FuturesUniverseSelectionModel import FuturesUniverseSelectionModel

class MultidimensionalTachyonCircuit(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2018, 12, 20) # Set Start Date
self.SetEndDate(2019, 3, 31)
self.SetCash(100000) # Set Strategy Cash

self.SetUniverseSelection(FuturesUniverseSelectionModel(self.SelectFuturesSymbols))
self.AddAlpha(EmaCrossAlphaModel(50, 200, Resolution.Daily))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(TrailingStopRiskManagementModel(0.05))




# def OnData(self, data):
# '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
# Arguments:
# data: Slice object keyed by symbol containing the stock data
# '''

# # if not self.Portfolio.Invested:
# # self.SetHoldings("SPY", 1)

def SelectFuturesSymbols(self, utcTime):
ticker = Futures.Indices.SP500EMini
return [ Symbol.Create(ticker, SecurityType.Future, Market.USA) ]

Could anyone help point my mistake please ? thank you

Update Backtest







0

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.


Hi Felix,

The issue here is that the Algorithm Wizard specifies `Market.USA` in the SelectFuturesSymbols method when it should be `Market.CME`. Changing line 32 in the code above to reflect this will allow the backtest to complete.

We've fixed this bug now. Next time this universe selection model is selected with the Algorithm Wizard, this small change will not be necessary.

Best,
Derek Melchin

0

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.


Hi Derek thank you for your answer, unfortunately I still got error with this code, after changing the USA to CME

from Alphas.EmaCrossAlphaModel import EmaCrossAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from FuturesUniverseSelectionModel import FuturesUniverseSelectionModel

class MultidimensionalTachyonCircuit(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2015, 12, 20) # Set Start Date
self.SetEndDate(2019, 3, 31)
#self.SetWarmUp(TimeSpan.FromDays(200))
self.SetCash(100000) # Set Strategy Cash

self.SetUniverseSelection(FuturesUniverseSelectionModel(self.SelectFuturesSymbols))
self.AddAlpha(EmaCrossAlphaModel(50, 200, Resolution.Daily))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(TrailingStopRiskManagementModel(0.05))




# def OnData(self, data):
# '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
# Arguments:
# data: Slice object keyed by symbol containing the stock data
# '''
# # if not self.Portfolio.Invested:
# # self.SetHoldings("SPY", 1)

def SelectFuturesSymbols(self):
ticker = Futures.Indices.SP500EMini
return [ Symbol.Create(ticker, SecurityType.Future, Market.CME) ]

 

Error is as follow:

undefined

105482_1592923562.jpgtried playing around with the codes but still could not make it work, anything I am missing here ? thank you in advance

0

Hi Felix,

Ensure the project was compiled with the changes we applied before backtesting by clicking the "Build" button in the Algorithm Lab. I created the same algorithm in the Algorithm Wizard and did not experience the issue shown in the image above. See the attached backtest for reference.

If building the project doesn't resolve this, I recommend just recreating the project now that the Algorithm Wizard has been updated.

Best,
Derek Melchin

0

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.


Hi Derek thanks for that, there is still a weird behaviour, I recreated the project and if the EMACrossAlphaModel is in Minute Resolution it works, but I want it in daily resolution, when I use Resolution.Daily then the algorithm does not seem to return anything, there is no trade, nothing. Any ideas ? thanks

0

Hi Felix,

The lowest resolution of our futures data is at the minute level. View our documentation for reference.

We can set up some consolidators to reduce the resolution of our data to daily. Check out our Bootcamp lesson and documentation for more information and examples.

Best,
Derek Melchin

0

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.


Hi Derek thank you for your answer, actually looked at the examples and documentations and while each part makes sense on its own, I still have little ideas how all of them should fit together, and I think this is mainly because I am not too sure what is really going on behind the background, for example the simple algorithm we discussed previously, I am not sure how the backtest took all this and create that, like as a beginner, it seems to me that there are too much abstractions here, I am not comprehending what is really going on in the background and hence struggle to add and modify stuff

This would be a more general question than jsut the consolidator problem, but what would you suggest to get a holistic understanding of how the whole things work if I am willing to put in the hours?
I have read the documentation and actually finished the bootcamp but am really struggling to create any backtest.
Now thinking to read the whole source codes of LEAN, but not too sure if this is the best practical way. Please let me know your thoughts. Thanks.

0

Hi Felix,

I don't think reading the LEAN source code would be the most practical approach to getting familiar with our API. After reading the documentation and completing the Bootcamp lessons, I think it's paramount to start with developing simple algorithms to get first-hand experience. There are tons of examples in our documentation, the community threads, the Strategy Library, and our GitHub repo. I'd recommend using these examples as inspiration but coding entire algorithms from scratch. Start small and eventually the harder algorithms will become easier.

Best,
Derek Melchin

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed