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/

    plotting pivot points or lines

    General Code/Help
    3
    16
    1324
    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.
    • Wayne Filkins
      Wayne Filkins last edited by

      I'm working on a strategy which requires pivot points. So far, I got it to go back 50 candles and basically compare that candle (low[-50]) with the last 100 candles to see if it's the lowest. This gives the low pivot points (they are offset by 50 though, because you can't compare to future candles while running live). Anyway, i'm trying to figure out how to plot it because I don't know how to plot a custom variable. I would like to plot it with an offset of 50 if possible. This is what I have so far for the pivot points:

      Init()

      self.base = bt.LineNum(float('1.0'))
              self.counter = 0
      

      next()

      self.counter = self.counter + 1
      
              self.isLower = True
      
              for i in range(-2 * self.p.pivotSize, -1):
                  if self.counter > self.p.pivotSize:
                      if self.datalow[-1 * self.p.pivotSize] <= self.datalow[i]:
                          self.isLower = True
                      else:
                          self.isLower = False
                          break
      
              if self.isLower == True:
                  self.base[0] = self.datalow[-1 * self.p.pivotSize]
                  print("new base has been found!" + str(self.base[0]))
      

      The counter is just to make sure there are 50 candles to look back to, so on the first 49 candles or w/e you don't get out of range error. I'm pretty sure this works but just can't figure out how to plot. The docs are really oddly worded and they tell you how to plot indicators, so do I have to make this into an indicator or something? It would be nice just to be able to program everything in the strategy.

      Wayne Filkins 1 Reply Last reply Reply Quote 0
      • Wayne Filkins
        Wayne Filkins @Wayne Filkins last edited by

        @Wayne-Filkins It would be nice if they just had a simple line / dot plot function, so I could just be like plot(self.base, line) or plot(self.base, dot) or something and it just plots it. I'm not that great with python or learning frameworks like this so all the extra files like indicator files and "observer" files just confuse me.

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          LinePlotterIndicator,
          Docs - Indicators - Usage

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          Wayne Filkins 3 Replies Last reply Reply Quote 1
          • run-out
            run-out last edited by

            Try this as well

            You could probably execute all of your code in one line and have it available to plot. You need to learn to make the jump to vector thinking and not be constrained by loops.

            RunBacktest.com

            Wayne Filkins 3 Replies Last reply Reply Quote 0
            • Wayne Filkins
              Wayne Filkins @ab_trader last edited by

              @ab_trader That sounds like what i'm looking for, but when I put it in init() or next() it doesn't work. Am I supposed to put it somewhere else?

              I just put

              LinePlotterIndicator(self.base, name='base')
              

              and it says it's not defined.

              1 Reply Last reply Reply Quote 0
              • Wayne Filkins
                Wayne Filkins @ab_trader last edited by

                @ab_trader okay I put bt. before it and now I got "IndexError: array assignment index out of range"

                1 Reply Last reply Reply Quote 0
                • Wayne Filkins
                  Wayne Filkins @run-out last edited by

                  @run-out Could you please explain or link a Youtube video or something where I can learn what you're talking about?

                  1 Reply Last reply Reply Quote 0
                  • Wayne Filkins
                    Wayne Filkins @run-out last edited by

                    @run-out Oh you mean likle using Min() instead of how I did it, yeah I knew as I was doing it that there were probably way better ways. I'm just not that good at this stuff. I have learned Python like 5 times throughout my life and I just forget everything, it's really odd. Now i'm in my 30's and just doing w/e to get by, you know just trying to get it functional. My trading strategy is on the 4h chart so it doesn't have to be super fast or anything. I'll try to write it better, but I still can't get the damn thing to plot :(

                    1 Reply Last reply Reply Quote 0
                    • Wayne Filkins
                      Wayne Filkins @run-out last edited by

                      @run-out actually I think I have to use a loop because i'm not trying to find out if it's the lowest, just the lowest in a range. Not sure how else I can do that.

                      1 Reply Last reply Reply Quote 0
                      • Wayne Filkins
                        Wayne Filkins @ab_trader last edited by

                        @ab_trader Holy shit I think I got it working. I had to declare it as a line instead of using bt.LineNum(float('1.0')) because now it's plotting them. Now I just have to figure out how to offset it by 50 so the dots are in the right place. Idk if it's working right but there are dots on the chart so finally making some progress.

                        1 Reply Last reply Reply Quote 0
                        • A
                          ab_trader last edited by

                          Looks like you are developing fractal indicator, which is already in the bt package, check out Docs - Indicator - References

                          • If my answer helped, hit reputation up arrow at lower right corner of the post.
                          • Python Debugging With Pdb
                          • New to python and bt - check this out
                          Wayne Filkins 2 Replies Last reply Reply Quote 1
                          • Wayne Filkins
                            Wayne Filkins @ab_trader last edited by

                            @ab_trader What i'm doing gets far more complex, that's why I need to really learn how to do all this stuff. I need to get the pivot points first and use them for 2 parts of my strategy. Then I have several other parts. It works but it's not like any strategy which exists it's too complex and customized. I'm using it live and it's working, but I need to spend hours finding the right stocks and the risk management gets complex too so I need to automate it so I don't have to spend all day every day finding stocks, monitoring, and making trades, and I can really get the most profits out of it. Also sometimes I miss opportunities because i'm trying to watch hundreds of stocks all the time so I lose some profits to that.

                            If anyone is really good with backtrader and wants to help with this they can use it too. It's super profitable but really needs to be automated. It cannot be shared with anyone else though. No one knows about this and it took me about 2 years to get it just right.

                            1 Reply Last reply Reply Quote 0
                            • Wayne Filkins
                              Wayne Filkins @ab_trader last edited by

                              @ab_trader I made a fractals indicator before on TradingView. It is kind of like that because that basically finds pivot points and connects them via lines, but I also need to reference the pivot dots for other reasons so if I used that one i'd have to modify it and I have no clue how indicators even work on here yet :(

                              1 Reply Last reply Reply Quote 0
                              • A
                                ab_trader last edited by

                                Go thru the beginning of the docs and Quickstart, it will help you to understand bt internals. Otherwise you would be spending time on the things already made.

                                I am more than sure that fractal indicator returns true or false, that can be used for reference.

                                • If my answer helped, hit reputation up arrow at lower right corner of the post.
                                • Python Debugging With Pdb
                                • New to python and bt - check this out
                                Wayne Filkins 2 Replies Last reply Reply Quote 1
                                • Wayne Filkins
                                  Wayne Filkins @ab_trader last edited by

                                  @ab_trader Yeah i'll try the quickstart. These docs are the hardest docs I have ever tried to read. I don't know if it's the vocabulary the guy uses or what just so much stuff doesn't register in my brain. Every time I look something up it makes no sense to me. Like with plotting, there's info about plotting all over the docs. The one guy showed me LinePlotterIndicator() and that isn't even in the plotting section of the docs. I'm also not that great with python so I guess it'll just take me a while to get the hang of it.

                                  1 Reply Last reply Reply Quote 0
                                  • Wayne Filkins
                                    Wayne Filkins @ab_trader last edited by

                                    @ab_trader If I can use the fractal indicator to reference the true/false for any candle, like fractal[-14] to find out if 14 candles back was a high or low, then it will work, because I have to be able to get the high/low value from the fractal, not just knowing whether or not it is. I'll mess around with it, definitely looks cleaner than my method lol.

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