Hi All,
I want to buy if 1 share of spy if the atr of the previous candle is greater than the atr of 2 candles ago. It was glitching and did not allow me to upload the backtest, but I attached the code in python. Could someone explain what I am doing wrong? It is not ordering any buys even though the atr has increased at some point in the data after the warm up period of 14 days.
If you see what is wrong please attach a fixed code in your response, thank you so much.
Fred Painchaud
Hi Jeremy,
I don't see the attached code. You might want to try to re-attach / copy-and-paste (using the code box from the dropdown menu) it.
Fred
Vladimir
Jeremy123
The best way to get help fast --> attach at least a code snippet.
Jeremy123
Here is the code, sorry about that.
Â
import numpy as np
from datetime import timedelta
class ATRconfirmation(QCAlgorithm):
  def Initialize(self):
    self.SetStartDate(2022, 2, 22)  # Set Start Date
    self.SetCash(10000)  # Set Strategy Cash
    self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
    self.atr_period = 14
    self.SetWarmUp(timedelta(days = 14))
   Â
    self.ATRconfirm = self.ATR(self.spy, self.atr_period, Resolution.Daily)
    self.ATRvalues = RollingWindow[Decimal](3)
   Â
  def OnData(self, data):
    # Make sure indicators and rolling windows are ready
    if self.IsWarmingUp: return
    self.ATRvalues.Add(self.ATRconfirm.Current.Value)
    if self.ATRvalues[1] > self.ATRvalues[2]:
      self.MarketOrder(symbol, 1)
Fred Painchaud
Hi Jeremy,
Here is the modified code:
Fred
Vladimir
Jeremy123
Here is another way to implement your strategyÂ
Trading ATR with Indicator Extensions DelayÂ
If you are satisfied with my answer, please accept it.Â
Jeremy123
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!