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/

    Using ParabolicSAR inside strategy

    Indicators/Strategies/Analyzers
    3
    8
    324
    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.
    • switchfire
      switchfire last edited by

      Good morning,
      I'm having difficulty implementing the ParabolicSAR in my strategy. I believe I've done the params and initialization correctly (not for sure, though), but I'm not sure how to add the conditions in. Am I supposed to build the calculations and use a variable for the condition? Or is that done inside backtrader? I want to include a psar condition inside the buy/sell conditions.

      Params:

      ('period', 2), # psar period ('start' in pine)
      ('af', 0.005), #psar af ('increment' in pine)
      ('afmax', 1), #psar afmax ('maximum' in pine)
      

      init:

      self.psar = bt.indicators.ParabolicSAR(
                  period = self.p.period,# Period
                  plot = False,
                  af = self.p.af,
                  afmax = self.p.afmax,
                  )
      

      I am new, thank you for your patience.

      N 1 Reply Last reply Reply Quote 0
      • switchfire
        switchfire last edited by

        I have searched google and backtrader references with no luck. I may just need a point in the right direction.

        run-out 1 Reply Last reply Reply Quote 0
        • N
          nima021 @switchfire last edited by

          @switchfire start in pine can be float but prriod in python can not be float.

          N 1 Reply Last reply Reply Quote -1
          • N
            nima021 @nima021 last edited by

            @nima021 how can put float as integer?

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

              @switchfire I had a similar script on file so I will simply share for your reference.

              import backtrader as bt
              
              class Strategy(bt.Strategy):
              
                  params = (
                      ("period", 2),  # psar period ('start' in pine)
                      ("af", 0.005),  # psar af ('increment' in pine)
                      ("afmax", 1),  # psar
                  )
              
                  def log(self, txt, dt=None):
                      """ Logging function fot this strategy"""
                      dt = dt or self.data.datetime[0]
                      if isinstance(dt, float):
                          dt = bt.num2date(dt)
                      print("%s, %s" % (dt.date(), txt))
              
                  def print_signal(self):
                      self.log(
                          f"o {self.datas[0].open[0]:7.2f} "
                          f"h {self.datas[0].high[0]:7.2f} "
                          f"l {self.datas[0].low[0]:7.2f} "
                          f"c {self.datas[0].close[0]:7.2f} "
                          f"v {self.datas[0].volume[0]:7.0f} "
                          f"psar {self.psar[0]:5.0f}"
                      )
              
                  def __init__(self):
                      self.psar = bt.ind.PSAR(period=self.p.period, af=self.p.af, afmax=self.p.afmax)
              
                  def next(self):
                      self.log(f"period: {self.p.period}, af: {self.p.af}, afmax: {self.p.afmax}")
                      self.print_signal()
              
              if __name__ == "__main__":
                  import time
              
                  cerebro = bt.Cerebro()
                  start = time.time()
                  data = bt.feeds.GenericCSVData(
                      dataname="data/2006-day-001.txt",
                      dtformat=("%Y-%m-%d"),
                      timeframe=bt.TimeFrame.Days,
                      compression=1,
                  )
                  print(f"time {(time.time() - start)}")
                  cerebro.adddata(data)
              
                  kwargs = dict(
                      period=3,
                      af=.004,
                      afmax=.9
                  )
                  cerebro.addstrategy(Strategy, **kwargs)
              
              
                  # Execute
                  cerebro.run()
              

              RunBacktest.com

              N 1 Reply Last reply Reply Quote 0
              • N
                nima021 @run-out last edited by

                @run-out thanx, I had red this before but I want to put 0.11 for period but it errored period can not be float.
                but in pine script it can be
                start=0.11
                please read my question completely
                best regards

                run-out N 2 Replies Last reply Reply Quote 0
                • run-out
                  run-out @nima021 last edited by

                  @nima021 I can't comment on what pinescript is doing.

                  In backtrader period is basically the number of bars you wish to calculate over. There are no partial bars in this scenario, hence the requirement for an int

                  RunBacktest.com

                  1 Reply Last reply Reply Quote 0
                  • N
                    nima021 @nima021 last edited by

                    @run-out said in Using ParabolicSAR inside strategy:

                    int

                    Yes I searched and found QuantConnect. this is exactly I said. for live trading you should have orginal PSAR that is float. in pineScript and QuantConnct this is float but in backtrader because of it's own definition. and when its float you can reach more different takeprofit.

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