I encountered the same issue.
you can either downgrade to matplotlib 3.2.2 or refer this pull request to fix it yourself:
In next you have assigend your entry order object to the variable self.order. When in notify_order, you access the current order object using the variable order. These are both order objects.
In an order object you can access the status of that object which is stores as in next self.order.status or in notify_order as order.status.
For completed orders this status variable will equal 4.
In your code, in next, you can use:
if self.order.status != self.order.Completed etc.
if self.order.status != 4 etc.
The cancel is normal. It is cancelling the other side of the bracket, so if you fill on the take profit, the stop gets cancelled and vice versa. If you want a bit more information in your log you can show the order reference numbers.
Also as another side note, I notice you are assigning the size using order.size. Size is acutally tracked in order.created.size.
@kav said in How to cancel only parent order in bracket.:
I meant how to cancel only child order in bracket (can't find the edit button on this forum somehow).
no, I really meant the parent order. Anyone knows where the edit button is on this forum?
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.
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.