For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Broker.get_value() on multiple instruments



  • I have 4 instruments in my bt. I set the commissions for each as follows:

    cerebro.broker.setcommission(commission=0.01, name="intr_0")

    cerebro.broker.setcommission(commission=0.02, name="intr_1")

    etc.

    Now I want to access the pnl for an individual instrument inside Strategy:

    self.broker.get_value(datas=self.datas[1])

    and I get the following error:

    File "C:\Users\micro\Miniconda3\envs\abc\lib\site-packages\backtrader\broker.py", line 80, in getcommissioninfo
    if data._name in self.comminfo:
    AttributeError: 'float' object has no attribute '_name'

    stepping through the code at:

    def getcommissioninfo(self, data)

    data is actually a price!

    Any thoughts?

    Kind regards


  • administrators

    You are passing a data feed instead of an array of data feeds, hence the name datas in plural, (even if wrong in English) to make it clear what you have to pass.



  • Thank you BT. So I should write:

    self.broker.get_value(datas=self.datas) ?

    this returns the total portfolio. I was hoping to get a list or dict of individual instrument values

    Cheers



  • @backtrader - sorry to bother you about this again, but is how the function is supposed to work?


  • administrators

    @deedy said in Broker.get_value() on multiple instruments:

    self.broker.get_value(datas=self.datas)
    

    If you pass the complete list of data feeds, you will get the value of the complete portfolio.

    If you pass an iterable with less data feeds, you will get only the value for the data feeds included in that iterable.

    If you pass an iterable with one data feed, you will get only the value for the included data feed.


 

});