Multi-currency portfolio with Futures markets
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!
The broker is, so to say, a single currency broker. A possible approach:
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
CommissionInfogets 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)
@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 ofCommissionInfo```?
For the functions of the commission scheme called in this loop from the
_get_valuemethod 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) else: dvalue = comminfo.getvaluesize(position.size, data.close) dunrealized = comminfo.profitandloss(position.size, position.price, data.close)
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
Thanks for your guidance!
P.S. Looks like I would need to change
Tradeas well, as it uses methods like
profitandloss. If the
comminfoobject had the data feed automatically available to it, the changes could be isolated to just the corresponding class, with no changes to
Trade, but I am not sure if that's a feasible/sensible approach.
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
CommissionInfoas 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
Tradeor other classes are needed.