Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Trade Basedon On Time Of Day

    General Code/Help
    3
    7
    158
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      Frenzyy last edited by

      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.

      Many thanks!

      R 1 Reply Last reply Reply Quote 0
      • R
        rajanprabu @Frenzyy last edited by

        @frenzyy

        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

        F 1 Reply Last reply Reply Quote 1
        • F
          Frenzyy @rajanprabu last edited by

          @rajanprabu
          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.

          Many thanks!

          F 1 Reply Last reply Reply Quote 0
          • F
            Frenzyy @Frenzyy last edited by

            @frenzyy
            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 1 Reply Last reply Reply Quote 0
            • run-out
              run-out @Frenzyy last edited by

              @frenzyy

              @frenzyy said in Trade Basedon On Time Of Day:

              volume = self.data.volume()

              In next, the current bar value is defined by [0] and the previous bar by [-1] etc.

              So current bar volume is (using long notation for datas):

              self.datas[0].volume[0]
              

              and if you wish to calculate the current close minus previous close:

              self.datas[0].close[0] - self.datas[0].close[-1]
              

              When loading, dtformat tells backtrader what format you are using for dates. Use:

              dtformat = "%Y-%m-%d"
              

              See docs

              RunBacktest.com

              F 1 Reply Last reply Reply Quote 2
              • F
                Frenzyy @run-out last edited by

                @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[0].close[0] > self.datas[0].close[-1]:
                            print(self.datas[0].close[0])
                            print("Yep")
                        else:
                            print(self.datas[0].close[0])
                            print("Nope")
                

                This works fine.

                date = self.datas[0].datetime.date()
                print(date)
                

                This too. But-

                        if self.datas[0].datetime.datetime[0] == self.datas[0].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 [0]? I'm getting the below when I try to do so:
                TypeError: 'method' object is not subscriptable

                Thanks in advance.

                F 1 Reply Last reply Reply Quote 0
                • F
                  Frenzyy @Frenzyy last edited by

                  @frenzyy Please disregard - found a solution here: https://community.backtrader.com/topic/3797/opening-range-breakout-strategy-for-multiple-stocks

                  1 Reply Last reply Reply Quote 1
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors