Trade Basedon On Time Of Day
Hey All, I'm trying to test a strategy which takes a long position once it breaks the HOD, but only after a specific time. That said, not sure how to go about keeping track of the HOD as well as how to indicate when to take a long, based on that aforementioned time. I'd greatly appreciate any tips or pointers you guys might have, since I wasn't really able to find anything similar anywhere.
please post what you have tried so far. Its much easier that way.
At every next() check the new high and store it in a variable and a simple date time if condition before buy would do
Thanks for the response.
Tbh, while I do not have an issue with manipulating data frames outside of backtrader, I'm having issues with even printing closing prices of the candles which I'm feeding to backtrader. I'm trying to feed it a standard OHLCV data from Binance which is saved as a csv.
As an example, I can print the time and date, but not the volume or close and I'm getting this in the output when i try to do so:
<backtrader.lineiterator.SingleCoupler object at 0x000002E2D3D79A30>
def next(self): dt = self.data.datetime.datetime() date = self.data.datetime.date() volume = self.data.volume() print(volume)
If you would be so kind please and provide an example of looping through the data and simply comparing the current close to previous close, that'd be great.
This is how I'm feeding the data at the moment btw.
fromdate = datetime.datetime.strptime('2021-08-01', '%Y-%m-%d') todate = datetime.datetime.strptime('2021-08-19', '%Y-%m-%d') data = bt.feeds.GenericCSVData(dataname='data\\SOL_30M.csv', dtformat=2, compression=30, timeframe=bt.TimeFrame.Minutes, fromdate=fromdate, todate=todate) cerebro.adddata(data)
run-out last edited by
volume = self.data.volume()
In next, the current bar value is defined by
and the previous bar by
So current bar volume is (using long notation for datas):
and if you wish to calculate the current close minus previous close:
self.datas.close - self.datas.close[-1]
dtformattells backtrader what format you are using for dates. Use:
dtformat = "%Y-%m-%d"
@run-out Much appreciated, this is very useful. Can easily compare previous close, vol, time, etc. That said, for some reason I'm getting an error when trying to compare the date of each candle. Would you happen to know what I'm doing wrong here? Please see below example.
if self.datas.close > self.datas.close[-1]: print(self.datas.close) print("Yep") else: print(self.datas.close) print("Nope")
This works fine.
date = self.datas.datetime.date() print(date)
This too. But-
if self.datas.datetime.datetime == self.datas.datetime.datetime[-1]: print("Yep") else: print("Nope")
Here I'm unable to compare the date of one candle to another, unlike with the close which I can do. It's due to not being able to put it in an array ? I'm getting the below when I try to do so:
TypeError: 'method' object is not subscriptable
Thanks in advance.
@frenzyy Please disregard - found a solution here: https://community.backtrader.com/topic/3797/opening-range-breakout-strategy-for-multiple-stocks