Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. _m_d_b
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    _
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 7
    • Best 0
    • Controversial 0
    • Groups 0

    _m_d_b

    @_m_d_b

    0
    Reputation
    436
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    _m_d_b Unfollow Follow

    Latest posts made by _m_d_b

    • RE: Is backtrade suitable for my project?

      This might be useful to you:
      https://www.backtrader.com/blog/posts/2016-10-29-strategy-selection/strategy-selection.html#strategy-selection

      posted in General Code/Help
      _
      _m_d_b
    • RE: Feeding Multiple Data Feeds in Indicators

      @Paska-Houso OK I guess I just didn't understand how *self.datas in MySuperIndicator(*self.datas) get mapped to the self.datas object in MySuperIndicator.

      The docs show examples without the * like this

      class MyStrategy(bt.Strategy):
          params = dict(period=20)
      
          def __init__(self):
      
              sma = btind.SimpleMovingAverage(self.data, period=self.params.period)
      
          ...
      

      But I guess I'm to understand that anything passed in the indicator that is not a named parameter (i.e. not like param=x) is mapped to the self.datas object in the indicator?

      posted in General Discussion
      _
      _m_d_b
    • RE: Feeding Multiple Data Feeds in Indicators

      @Paska-Houso Sorry to be annoying here, I don't see that in the documentation. Can you quote the relevant snippet?

      posted in General Discussion
      _
      _m_d_b
    • RE: Non 0th data feed unexpected behavior in indicator (multiple data feeds)

      @Paska-Houso Thanks for the reply, our messages just passed each other in the other thread. I appreciate the help. For those interested, please check out the linked thread for the full discussion.

      posted in General Code/Help
      _
      _m_d_b
    • RE: Feeding Multiple Data Feeds in Indicators

      @Paska-Houso

      Thank you! I think I was just missing the *self.datas notation. I'll give this a try.

      I also pasted a more detailed version of this question here.

      I think your solution works and I was just passing the object incorrectly to the indicator.

      How does *self.datas in MySuperIndicator(*self.datas) get mapped to the self.datas object in MySuperIndicator? I didn't expect that. What's the mechanism there?

      I thought parameters had to be passed in by name (e.g. MySuperIndicator(data=self.data)) and accepted as "params" (params = (('data',{}),).

      posted in General Discussion
      _
      _m_d_b
    • Non 0th data feed unexpected behavior in indicator (multiple data feeds)

      I'm trying to create an indicator that operates on multiple data feeds. The indicator should use all the data feeds, not just the 0th indexed data feed.

      I've been trying to figure out how to pass multiple data feeds into an indicator and the best I've been able to figure out is to have the Strategy pass the self.datas object as a param to the indicator, like this: self.prediction = ExampleIndicator(feeds=self.datas).

      Unfortunately I'm finding that the object that represents those feeds, self.p.feeds, in ExampleIndicator doesn't behave like I would expect it to when accessing its data. Notably when trying to access today's data with [0] it works only for the 0th feed and returns nothing for the non-0th feed. Similarly, trying to access yesterday's data with [-1] work's only for the 0th feed and returns the last item (most recent) of the entire data feed for the non-0th data feed. Here's an example:

      class CustomStrat (bt.Strategy):
      
          def __init__(self):
              self.prediction = ExampleIndicator(feeds=self.datas)
      
          def next(self):
              print("Strategy next")
              for feed in self.datas:
                  print(np.frombuffer(feed.open.get(ago=0, size= 1)))
      
      
      class ExampleIndicator(bt.Indicator):
          lines = ('out',)
      
          params = (('feeds', {}),)
      
          def __init__(self):
              self.i=0
      
          # Get a prediction here
          def next(self):
              print("Indicator next")
              print("total number of days {}".format(len(self.data)))
              for i, feed in enumerate(self.p.feeds):
                  for ago in [0,-1]:
                      print ("Feed {0} [{1}] = {2}, length: {3}".format(
                              feed._name,
                              ago,
                              np.frombuffer(self.p.feeds[i].open.get(ago=ago, size= 3)),
                              len(feed)))
              self.i+=1
              if self.i >5: exit()
      

      Returns

      Indicator next
      total number of days 1
      Feed BCHARTS/BITSTAMPUSD [0] = [], length: 1
      Feed BCHARTS/BITSTAMPUSD [-1] = [], length: 1
      Feed BCHARTS/LOCALBTCUSD [0] = [], length: 0
      Feed BCHARTS/LOCALBTCUSD [-1] = [ 2848.26  4591.82  2476.97], length: 0
      Indicator next
      total number of days 2
      Feed BCHARTS/BITSTAMPUSD [0] = [], length: 2
      Feed BCHARTS/BITSTAMPUSD [-1] = [], length: 2
      Feed BCHARTS/LOCALBTCUSD [0] = [], length: 0
      Feed BCHARTS/LOCALBTCUSD [-1] = [ 2848.26  4591.82  2476.97], length: 0
      Indicator next
      total number of days 3
      Feed BCHARTS/BITSTAMPUSD [0] = [ 1191.16  1228.    1256.32], length: 3
      Feed BCHARTS/BITSTAMPUSD [-1] = [], length: 3
      Feed BCHARTS/LOCALBTCUSD [0] = [], length: 0
      Feed BCHARTS/LOCALBTCUSD [-1] = [ 2848.26  4591.82  2476.97], length: 0
      Indicator next
      total number of days 4
      Feed BCHARTS/BITSTAMPUSD [0] = [ 1228.    1256.32  1287.38], length: 4
      Feed BCHARTS/BITSTAMPUSD [-1] = [ 1191.16  1228.    1256.32], length: 4
      Feed BCHARTS/LOCALBTCUSD [0] = [], length: 0
      Feed BCHARTS/LOCALBTCUSD [-1] = [ 2848.26  4591.82  2476.97], length: 0
      Indicator next
      total number of days 5
      Feed BCHARTS/BITSTAMPUSD [0] = [ 1256.32  1287.38  1260.  ], length: 5
      Feed BCHARTS/BITSTAMPUSD [-1] = [ 1228.    1256.32  1287.38], length: 5
      Feed BCHARTS/LOCALBTCUSD [0] = [], length: 0
      Feed BCHARTS/LOCALBTCUSD [-1] = [ 2848.26  4591.82  2476.97], length: 0
      Indicator next
      total number of days 6
      Feed BCHARTS/BITSTAMPUSD [0] = [ 1287.38  1260.    1269.98], length: 6
      Feed BCHARTS/BITSTAMPUSD [-1] = [ 1256.32  1287.38  1260.  ], length: 6
      Feed BCHARTS/LOCALBTCUSD [0] = [], length: 0
      Feed BCHARTS/LOCALBTCUSD [-1] = [ 2848.26  4591.82  2476.97], length: 0
      

      Notice how the BCHARTS/LOCALBTCUSD [-1] always shows the same thing (which is the latest data on that feed). It's as if it's being treated like a normal python list, not like a BT feeds object.

      Ultimately, I'm just trying to get multiple data feeds into an indicator. Not sure the best way to do it.

      Any ideas/help here?

      Thanks!

      posted in General Code/Help multiple data indicator indicators
      _
      _m_d_b
    • RE: Feeding Multiple Data Feeds in Indicators

      @WillDawn24 @backtrader

      Can you please give an example of how passing multiple data feeds into an indicator is done? For example, if you had two feeds (self.datas[0] and self.datas[1] in the parent strategy):

      1. How do you pass the self.datas array to the indicator?
      2. How would you access each feed in the indicator?
      3. How would you access each feed in the indicator if you didn't know how many data feeds sent in before program execution? I.e. without using the self.data[#] notation and using a loop to loop over available data feeds.
      posted in General Discussion
      _
      _m_d_b