Tracking entry signal for trade
-
Hope I am not over using my question credits here... :slight_smile:
I have a requirement to track the signal that triggered a trade entry. Wondering if there is some place to track that as part of the trade transaction.
If I enter a trade on a particular signal (market in downtrend), I want to exit around a particular set of signals applied to trades made in the downtrend.
I have cases where that trend changes while the position is open which is setting up another set of exit signals applied in the new market condition.
Is there a place to do this in the API?
-
Looks like
addinfo()
object might be a way to do what I want but could use a bit of an example of how to apply this if that is the right direction.https://www.backtrader.com/docu/order.html?highlight=addinfo
-
addinfo
can help. It simply takes**kwargs
and stores then underorder.info
, whereinfo
is just adict
.But since this seems to be more
trade
oriented than order oriented, you would need to track information attrade
level.Activate:
- set
tradehistory=True
in either ´cerebro = Cerebro(**kwargs)or
cerebro.run(**kwargs)`
The trades notified in
strategy.notify_trade
will then:- Record events under
trade.history
(alist
) and these events contain the order which generated then.
In most cases a trade is made up of just 2 orders:
- Opening order
- Closing order
If you add some information to the opening order with
addinfo
, you can always retrieve it from thetrade
withtrade.history[0].event.order
(thedict
has been extended to support dot notation)
Not really elegant, because one has to keep track of the trades and there is really no advantage with regards to keeping the information in own structures.
- set
-
Sorry, walking around in the debugger here and not quite connecting the dots...
I'm working in
Strategy next()
, and don't see where I can get to the trade object. Needing this info where I am applying buy/sell rules. Maybe I am being thick.setting the value for
addinfo
as follows:oinfo = dict(trend=self.trend) self.order = self.sell(data=self.data0, addinfo=oinfo)
I appreciate your help here.
-
Strategies can receive
trade
notifications via thenotify_trade(self, trade)
method, which can be overridden by end users.The
trade
is notified with eachorder
(which is for the samedata
and has the sametradeid
). The idea:class MyStrategy(bt.Strategy): def notify_trade(self, trade): self.mytrade = trade def next(self): # Something like this when operating on a single data source if self.position: # in the market # for sure there is an open trade - get the info that was added to the opening order info = self.trade.history[0].event.order.info
Information which has been stored with
addinfo
. This alleviates having to keep track of which order actually was the one that opened the trade because thetradehistory
keeps that in the index0
. -
Thank you. This is now working as you suggest.