I wrote a simple algorithm to buy and sell breakout of a 20 day rolling period of highs and lows during those 20 days. Currently I have two issues; first the prices for natural gas are incorrect. The first fill price is around 78$ when in fact the contract traded near 2$ during the start of the backtest. Second, the calculation and fills seem to be off as well. Using a twenty day period there is only 5 trades during the 5 years and I know that the price broke the levels I've set quite a bit more than that. I have attached my code hear and I appreciate all the support. Thank you kindly,
class FocusedRedGoshawk(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2010, 1, 1)
self.SetEndDate(2015,1,1)
self.SetCash(100000)
self.naturalgas = self.AddFuture(Futures.Energies.NaturalGas, Resolution.Daily).Symbol
self.high = self.MAX(self.naturalgas, 20, Resolution.Daily, Field.High)
self.low = self.MIN(self.naturalgas, 20, Resolution.Daily, Field.Low)
self.SetWarmUp(timedelta(days=20))
self.SetBenchmark(self.naturalgas)
def OnData(self, data: Slice):
if not self.high.IsReady:
return
self.Debug(f"Symbol: {self.naturalgas} 20 Day High: {self.high.Current.Value} , Low: {self.low.Current.Value}")
if not self.Portfolio.Invested:
if self.Securities[self.naturalgas].Close > self.high.Current.Value:
self.MarketOrder(self.naturalgas, 1)
if self.Securities[self.naturalgas].Close < self.low.Current.Value:
self.MarketOrder(self.naturalgas,-1)
if self.Portfolio[self.naturalgas].IsLong:
if self.Securities[self.naturalgas].Close < self.low.Current.Value:
self.MarketOrder(self.naturalgas,-2)
if self.Portfolio[self.naturalgas].IsShort:
if self.Securities[self.naturalgas].Close > self.high.Current.Value:
self.MarketOrder(self.naturalgas, 2)
Vladimir
AegeanFutures,
"N Day Breakout UNG" might help.
If you are satisfied with my answer, please accept it.
Vladimir
AegeanFutures,
Here is slightly modified version
Fred Painchaud
Hi Aegean,
It looks like you're not trading futures natural gas contracts in the chain but the continuous contract. Is that what you want?
This can help to deal with the chain:
and this thread with continuous:
Especially this post in the thread:
This example also:
Re the data which seems incorrect, it may really be that the data is incorrect. If after looking at all of this you still see wrong data, I would create an issue here:
Fred
AegeanFutures
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!