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/

    Dynamic calculation based on "weights" dictionary

    General Code/Help
    1
    2
    30
    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.
    • J
      josephg last edited by

      Hi, I started discovering BackTrader, and I would appreciate any suggestion how to "structure" my strategy.

      The algorithm needs to read a dict which contains different weights of total position size. Let's say I want to split a complete position in 4 orders, then, any time the datas[0].high fill the target_price position.size += 1 out of total of 4. However, theses target prices are intended to be moved by offset. So, this target_price should be dynamic calculated based on an offset given by a dict.

      Following code is an example of my strategy I want to code, please keep focus on the part I want to create de entries dictionary based on an imported CSV, this file includes offset to then dynamically map params.offset.vaue.

      I want to include this mapping part in the def next() part when if <condition> statement is True when False the offset remains as initially declared.

      lass PoolsJedi(bt.Strategy):
          params = (
              ('offset', 0.0055),
          )
      
          def __init__(self):
              # Start fields
              self.dataopen = self.datas[0].open
              self.dataclose = self.datas[0].close
              self.datahigh = self.datas[0].high
              self.datalow = self.datas[0].low
      
              # Create weights dictionary
              qtr = pd.read_pickle('config/weights.csv')
              entries = {
                  w: {
                      'e{}'.format(i): qtr[e][w]
                      for i,e in enumerate(qtr.columns) 
                  } for w in qtr.index
              }
      
              # Initialize variables
              entries_length = len( entries[list(entries.keys())[0]] )
              cpos = np.array( [0] * entries_length )
      
          """  CODE CONTINUES """
      
      

      I hope this part may give you the whole idea. I'd appreciate any support in advance.

      1 Reply Last reply Reply Quote 0
      • J
        josephg last edited by

        I just figured it out. Thanks anyway for the support!

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }