ok, just figured it out. I can reuse the DataTrades observer. No change needed for the MetaDataTrades class. It will generate multiple lines for the assets. The author sure constructed backtrader to be versatile!
Here is my code to show pnl in individual asset. Just accumulate daily settled pnl for each asset. Haven't checked the exact numbers yet. Eyeballing it looks alright comparing to the total account pnl.
class Aseet_monitor(bt.utils.py3.with_metaclass(MetaDataTrades, bt.observer.Observer)):
_stclock = True
params = (('usenames', True),)
plotinfo = dict(plot=True, subplot=True, plothlines=[0.0],
plotlines = dict()
strat = self._owner
for inst in strat.datas:
pos = strat.broker.positions[inst]
cur_line = getattr(self.lines, inst._name)
comminfo = strat.broker.getcommissioninfo(inst)
if len(self) == 1:
cur_line = 0
if pos.size != 0:
cur_pnl = comminfo.profitandloss(pos.size, inst.close[-1],
cur_pnl = 0
cur_line = cur_line[-1] + cur_pnl
Here is the output. The whole strategy is carried by HG_copper, while CL and ZN are underwater almost the whole time.
Comparing to the Value observer.
I ended up fixing this by changing my def log function as follows:
def log(self, txt, dt=None, doprint=False):
''' Logging function for this strategy'''
if self.params.printlog or doprint:
dt = dt or self.datas.datetime.date(0)
time = self.data.datetime.time(0)
print('%s %s, %s' % (dt.isoformat(), time, txt))
And adding this parameter to GenericCsvData:
Thanks alot for this important information!
Just discussed here with friends and it came up topic with "data-quality", which is said to be expensive!
Free-Data has only a quality of 70% !
Is that true? What does that mean?
This kind of statement definatelly needs exact qualification of spezific quality-aspects!
Untrusting IB-Data feels like untrusting the Broker itself!
Restrictions are there, but not in quality,
Anybody experienced backtesting-trouble caused by bad dataquality of OFFICIALLY(!) downloaded historical-data?
Sound crazy and unreal.
you corrupted your data yourself :)
ok, maybe some gaps or irritations after marketclosing, when nobody should trade anyway.)
maybe differences in data at DealingDesk- or MarketMaker-Broker, which make their own market, and nobody should trade with them anyway.
As this is topic far away from coding, and i can hardly find information and seems very important for Back-Trading.
Just4Info: Attached some datasources, that passes my way.
@Sajil-Thamban I'm guessing when the crossover happens closer to the end of a bar, it will be treated as happening in the next bar. Because I observed similar behavior for my testing. I have the exact same strategy tested in backtrader and Tradingview. For some trades, the enter and exit (crossovers) occur at the same bar but for some trades, the crossovers will occur one bar earlier or later, even visually I saw the crossover as happening at the same bar on both platforms. The situation will become even worse when both MA lines overlapping each other during a tight range. Just my 2 cents worth of opinion.
PositionsValue is an indicator that returns the market value at each bar for each datas line, as well as cash.
Can you please provide the code you are using and your unexpected output so we can analyze why you are getting the results you are? Thanks.
Hi, did you start or even finished your FXCM implementation? I just started to look into this topic a little, but i am lacking a bit of information on what functions need to be provided by the different classes and what are the expected return values for backtrader to work.
Probably I misinterpreted your original post. There are many indicators putting the predicted/calculated values to the future bars ( like Ichimoku indicator for example) and this is perfectly legal. BTW, there were multiple discussions about it in this forum:
My apologies for misunderstanding.
@soulmachine Hello, tks for your explanation. I'm testing cryptocurrency too, on 30 mins data. If I'm testing more than a year of data, I do not need to change the default parameters right? maybe only change the factor to 365?