Back

Getting high and low data of current day, previous day, and day before previous day

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))

 

 

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 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

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