Backtrader Community

    • 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/

    Iterate between __init__ and next ?

    General Code/Help
    3
    5
    539
    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.
    • Akash Saxena
      Akash Saxena last edited by Akash Saxena

      Hi all,
      i'm new to this platform, and am stuck implementing following part of a strategy. i have experimented quite a few things, and read the online help available, but its not working out. any help is welcome !

      Strategy algorithm snippet : (fixed data feed size being used : 1000 periods)

      1. start with an EMA (say, EMA of 200 periods)
      2. generate the EMA line
      3. generate the EMA crossings w.r.t. datas.close
      4. count number of crossings created in 3)
      5. if count is < 10
        5a) decrease EMA period by 1
        5b) goto step 2)
        else
        5c) print out final EMA period and use it for rest of the strategy.

      While implementing above, i create EMA of x period in __init__ of strategy, something like:

      self.current_ema = bt.indicators.ExponentialMovingAverage(self.datas[0].close, period=self.emasize)
      

      i can generate the crossings line in 3) either in __init__ (line processing) or in next (element processing)

      however, the count of crossings can be generated only in next (or so i believe !)

      The problem is, once i generate the count and reach 5b), how do i create the next EMA in 2) ? the control flow wont go back to __init__ for creating a new indicator.

      any ideas/ help welcome !

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        You seem to be NEW with Python, since you ask for an impossibility. Take your time.

        In any case, you cannot modify the period of the created indicators, they are simply not meant to work like that. Your best bet is to create as many EMAs with different periods as you deem necessary ... and then switch amongst them until you find the one you want to stick to.

        1 Reply Last reply Reply Quote 0
        • Akash Saxena
          Akash Saxena last edited by Akash Saxena

          :-) well, thats right - indeed i'm new to Python as well !
          i had thought of creating lots of EMAs upfront ... but wondered there must be a better way to achieve the goal. till then, i'll implement with lots of EMAs.
          thanks a lot !

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

            The other way (don't know if it is better or not) is to calculate your indicator value on each next() with your own method.

            • 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
            1 Reply Last reply Reply Quote 0
            • Akash Saxena
              Akash Saxena last edited by

              thanks for this idea !
              yes it can be done, but it takes more time and effort as i have to test my code for the EMAs and other indicators that i use.

              i finally used a strategy to do the above flowchart for EMA line of a particular period. then used optstrategy to iterate over the EMA periods.
              Not exactly what i started to do, but it gets my job done.

              thanks for reaching out with help !

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