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/

    psar

    Indicators/Strategies/Analyzers
    5
    6
    391
    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.
    • N
      nima021 last edited by

      hello
      I am working on strategy and I have problem in converting Parabolic SAR indicator from pineScript to python and using in backtrader.
      in pine the refrence code of calculation is:

      psar = ta.sar(start, increment, maximum)
      start (simple int/float) Start.
      inc (simple int/float) Increment.
      max (simple int/float) Maximum

      but in talib the refrence code are:
      real = SAR(high, low, acceleration=0, maximum=0)

      and in backtrader refrence code Params are:
      bcktrader.ind.PSAR(period, af, afmax)
      period (2)
      af (0.02)
      afmax (0.2)

      i found :
      increment val= af and maximum=afmax but I couldnt find any refrence in what happened for start? my start is 0.11 and its float and can not put start = peroid because period can not be float
      how can I add start value to backtrader strategy?

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

        @nima021 I had a reference script for psar, hopefully this helps.

        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 thank you very much dear
          as I read 'start' in pineScript == 'period' in backtrader?
          I had set it on both pine script and backtrader and it worked.
          But my problem is that set value of start pram = 0.11 in my strategy pineScript and I get good result
          but when I wanted to convert it to python:
          set value of period pram must be = 0.11 but when I give the value to period I have eroor:
          TypeError: 'float' object cannot be interpreted as an integer
          but when I give period=2 that works!
          but I have to give period=0.11
          can you help me pls?

          1 Reply Last reply Reply Quote 0
          • Ahmed Malik
            Ahmed Malik last edited by

            I am working on strategy How To Fix A Blown Head Gasket Without Replacing It? and I have problem in converting Parabolic SAR indicator from pineScript to python and using in backtrader.

            1 Reply Last reply Reply Quote 0
            • D
              DeclanForan last edited by

              Stump removal is a form of landscaping and it's one of the most popular forms of lawn care. You need to follow this Stump Grinding Rotorua and get more new skills for construction. You might think that stump removal is only for extremely large or dense trees, or those in commercial areas where space is limited. But you'd be wrong. A stump removal is an important part of removing old trees from your property. Stumps must be cleared away from your house, driveway, or any other place that you don't want them to stay.

              1 Reply Last reply Reply Quote 0
              • M
                mirand852 last edited by

                interesting information

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