I am new here so apologies if this is very simple. I couldn't find a direct answer in the forum. Thank you in advance
I'm really confused with how to correctly use the rolling windows. I want to use it to get the following:
- most recent high and low
- previous day's high and low
- and day before previous high and low
import numpy as np
class algo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2016, 1, 1) # Set Start Date
self.SetCash(10000) # Set Strategy Cash
self.AddCrypto("BTCUSD", Resolution.Daily, Market.GDAX)
self.SetWarmup(3)
self.window = RollingWindow[TradeBar](2)
self.lowwindow.Add(data["BTCUSD"].Low)
self.highwindow.Add(data["BTCUSD"].High)
def OnData(self, data):
bar = data["BTCUSD"]
high = bar.High
low = bar.Low
prevdayhigh= self.highWindow[1]
prevdaylow = self.lowwindow[1]
prev2dayhigh = self.highWindow[2]
prev2daylow = self.lowwindow[2]
nextdayhigh = self.highWindow[self.highWindow.Count-1]
nextdaylow = self.lowWindow[self.lowWindow.Count-1]
insideday = np.where((( prevdaylow < low ) & (prevdayhigh > high)),1,
np.where(prevdaylow == 0.0, np.nan,np.nan ))
short_termlow=np.where(((low<prevdaylow) & (low<nextdaylow) & (high<nextdayhigh) & (np.isnan(inside_day.shift(1)))),low,
np.where(prevdaylow == 0.0,np.nan,np.nan))
short_termlow_outsideday=np.where(((low<prevdaylow) & (low<nextdaylow) &( low < prevdaylow ) & (high >prevdayhigh) & (high<nextdayhigh) & (np.isnan(inside_day.shift(1)))),1,
np.where(prevdaylow == 0.0,np.nan,np.nan))
Shile Wen
Hi Liz,
To store previous Highs and Lows, you are spot on in that we need RollingWindows. Creating RollingWindows follow this pattern: RollingWindow[type](n data points). Thus, since High and Low values are floats, our constructors should be self.lowWindow = RollingWindow[float](3), and self.highWindow = RollingWindow[float](3). To get the data into our RollingWindows, we need to update them in OnData. RollingWindows have the most recent values at the front, and the oldest at the back, so to get the most recent High value, do recent_high = self.highWindow[0] and to get the oldest value, do oldest = self.highWindow[2] or oldest = self.highWindow[self.highWindow.Count - 1], and the same pattern applies to the Low values. To see this in action, check out the attached backtest.
Best,
Shile Wen
Kenneth Lamers
I want to keep a rolling window that holds the lowest value for each minute of the day.
Then what command gives me the lowest of all the values in my rolling window?
#region imports
from AlgorithmImports import *
#endregion
class algo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 8, 26)
self.SetCash(10000)
self.window = RollingWindow[TradeBar](120)
self.tqqq = self.AddEquity("UBER", Resolution.Minute).Symbol
self.lowWindow = RollingWindow[float](120)
def OnData(self, data):
self.lowWindow.Add(data[self.uber].Low)
Nico Xenox
Hey Kenneth Lamers
I changed a few things from the example provided by Shile Wen and adjusted it to your needs.
Here's the backtest:
Liz
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!