class ContinuousFuture(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetCash(1000000)
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2023, 6, 1)
self.continuous_contract = self.add_future(Futures.Energies.CrudeOilWTI,
data_normalization_mode = DataNormalizationMode.BackwardsRatio,
data_mapping_mode = DataMappingMode.OpenInterest,
contract_depth_offset = 0,
resolution=Resolution.DAILY)
self.slow_sma = self.sma(self.continuous_contract.Symbol, 3, Resolution.DAILY)
self.fast_sma = self.sma(self.continuous_contract.Symbol, 5, Resolution.DAILY)
def on_data(self, slice):
self.current_contract = self.securities[self.continuous_contract.mapped]
self.log(f"{self.current_contract.close} {self.slow_sma.current.value} {self.fast_sma.current.value}")
if self.current_contract is not None and self.current_contract.Symbol != self.continuous_contract.mapped:
self.log(f"{time} - rolling position from {self.current_contract.symbol} to {self.continuous_contract.mapped}")
current_position_size = self.current_contract.holdings.quantity
self.liquidate(self.current_contract.Symbol)
self.set_holdings(self.continuous_contract.mapped, current_position_size)
self.current_contract = self.securities[self.continuous_contract.mapped]
Hi guys, I am new here. I am trying to compute the simple moving average for continuous futures. The code snippet above returns no errors when executed. But the values of the sma are not correct. Here is a snippet of the output logs from the code above
2022-03-24 20:00:00 : 111.245 97.26056115751287 95.7416011719743
2022-03-25 20:00:00 : 112.635 98.38043782245417 97.36818566816957
2022-03-28 20:00:00 : 103.47 95.2215129961522 96.0697104909493
2022-03-29 20:00:00 : 105.19 93.44425418503756 95.44316131269383
2022-03-30 20:00:00 : 107.46 91.94914611807695 94.24067556389711
2022-03-31 20:00:00 : 101.15 91.28303766022876 92.48319639257885
2022-04-01 20:00:00 : 99.485 89.61340335977081 90.18294063508381
2022-04-04 20:00:00 : 103.685 88.50516177706265 90.21086483244339
2022-04-05 20:00:00 : 101.31 88.55751964711186 89.55115566982342
2022-04-06 20:00:00 : 96.995 87.84487086033103 87.72037548043625
2022-04-07 20:00:00 : 97.145 85.94835245632652 87.01005371010206
2022-04-08 20:00:00 : 97.93 84.93610030204192 86.74477383518608
2022-04-11 00:00:00 : 0.0 84.93610030204192 86.74477383518608
2022-04-11 20:00:00 : 94.83 84.44099889812388 85.28187775307299
2022-04-12 20:00:00 : 100.515 85.55332380368861 85.20753620404811
2022-04-13 20:00:00 : 103.67 87.36801158187936 86.45865826811509
2022-04-14 20:00:00 : 105.975 90.64672283329647 88.10089997925495
2022-04-18 20:00:00 : 106.99 92.55492109281106 89.77882867850958
2022-04-19 20:00:00 : 102.405 92.18672357259844 91.10609307279982
2022-04-20 20:00:00 : 102.44 91.13473065770525 91.44974409166494
First column is the close price, second column is the slow sma (3), third column is the fast sma (5). If I compute the sma myself using the closing price as logged, I am unable to get the same sma values. Am I understanding the code wrongly? How do I solve this issue?
Louis Szeto
Hi YQ
We have crossed checked our indicators with third party sources. You might find common reasons on why the indicator values have discrepencies with other sources in this doc.
In Futures, the continuous contracts are not smoothly transit from one contract to another. There will be slight jumps in between, so trader would use some normalization skills to smoothen this gap. This would also affect the indicator results as well. You can find details of data normalization supported in LEAN in here.
Best
Louis
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.
Tushar Verma
Hi Louis Szeto
Can you help me with this
How do I add Algoseeks free cloud data of US Equities as my universe here for backtesting ? I want to select all US Equities with market cap of more than 100 million dollars as my universe
I am new to the platform and I am unable to start properly as I am unable to select my universe . The only method I got to know is by adding individual stocks which I dont think will fulfill my purpose.
Can you guys please suggest some resource from where I can start as I am unable to understand it from the documentation
YQ Lim
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!