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/

    Multi-currency portfolio with Futures markets

    General Discussion
    3
    4
    1254
    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.
    • B
      bill last edited by

      My portfolio (a single strategy with multiple data series) contains global markets with multiple different quote currencies (JPY, EUR, USD, CAD, etc). Is hacking/extending Broker's _execute(...) and _get_value(...) methods the proper way to apply FX conversions?

      If you have a plan to implement this or a working example, could you share with us? Thanks!

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

        The broker is, so to say, a single currency broker. A possible approach:

        • Subclass a CommissionInfo. Each instance which operates in a different currency gets assigned a different instance via:

          def addcommissioninfo(self, comminfo, name=None):
          

          which is in the broker

        When the CommissionInfo gets a request like getvalue(position, price), you may want to return a value in the main currency after adjusting internally for the foreign exchange rate of the currency the asset operates in.

        You probably need to override all methods (i.e.: go directly to the source and look at each method)

        The entry point in the documentation: Docs - Commissions: Stocks vs Futures (there are some other chapters following this one)

        1 Reply Last reply Reply Quote 1
        • R
          Rahul Savani last edited by Rahul Savani

          @backtrader I would like to do exactly per your suggestion, making the conversion to pnl in the broker's main currency using a changing FX rate from a field in datas. What in your opinion is the cleanest/best way to make the relevant field of datas`` available to the methods in the new subclass of CommissionInfo```?

          For the functions of the commission scheme called in this loop from the _get_value method of ```BackBroker``:

                  for data in datas or self.positions:
                      comminfo = self.getcommissioninfo(data)
                      position = self.positions[data]
                      # use valuesize:  returns raw value, rather than negative adj val
                      if not self.p.shortcash:
                          dvalue = comminfo.getvalue(position, data.close[0])
                      else:
                          dvalue = comminfo.getvaluesize(position.size, data.close[0])
          
                      dunrealized = comminfo.profitandloss(position.size, position.price,
                                                           data.close[0])
          

          like getvalue and profitandloss, I can just pass in the FX rate here, as I have the data feed to hand. But I wonder if I only need to adapt those methods that are directly called from BackBroker?

          Thanks for your guidance!

          P.S. Looks like I would need to change Trade as well, as it uses methods like profitandloss. If the comminfo object had the data feed automatically available to it, the changes could be isolated to just the corresponding class, with no changes to BackBroker or Trade, but I am not sure if that's a feasible/sensible approach.

          1 Reply Last reply Reply Quote 0
          • R
            Rahul Savani last edited by Rahul Savani

            I settled on:

            • Adding a step to the broker's next method that will update each comminfo (possibly one for each data feed) with the new respective FX rate

            • Update the methods of a new commission class that extends CommissionInfo as per the original suggestion.

            My first post was just confused. With the 2 changes here, the commission info objects have the current rate and apply it to the calculations, and only one minimal change is needed to the broker, and no changes to Trade or other classes are needed.

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