Print Trade (simple question)
-
Hi Guys,
this might be a simple one but I cant seem to figure it out. After running a strategy successfully how do you
- print off each trade, date, value
- and if it was a positive or negative as shown on the standard chart?
I have implemented the first part like this:
def next(self): if not self.position: if self.rsi < self.params.rsi_low: self.buy(size=10) print('{},BUY,{}'.format(self.datetime.datetime(), self.data.close[0])) else: if self.rsi > self.params.rsi_high: self.sell(size=10) print('{},SELL,{}'.format(self.datetime.datetime(), self.data.close[0]))
but I am sure there must be a way to dump this information after Cerebro has completed. I think the clue is somewhere in implementing an observer but I have not been able to figure it out yet - help would be much appreciated!
-
Hi,
read up on Analyzers (https://www.backtrader.com/docu/analyzers/analyzers.html), especially https://www.backtrader.com/docu/analyzers-reference.html#tradeanalyzer and https://www.backtrader.com/docu/analyzers-reference.html#transactions.
Basically you attach the analyzer(s) to the Cerebro instance, run the test and get the analyzers after the test is finished.
-
Hi fintrading,
many thanks for the pointers - now I am able to get my trades. The only bit I can't yet figure out is where to see if the trade was positive or negative - like you see on the standard plot. Is this somewhere under observers?
Thanks again!
-
I guess i am trying to show trades and not transactions so that i can see the pnl. I can see that thid can be done through trades class but just need an example to understand how it works. Thanks!
-
In the Docs - Quickstart it is shown how to print trade pnl:
def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm))
-
You may also want co check
history
of the trades. I think it can be extracted fromstrategy
object returned aftercerebro
run. But I can be wrong, never used it.Reference Docs - Trade
-
Great!
so the information that I am looking for is contained in:
which is in the main strategy.
How can I access this info please?
-
I probably missed something but:
Trade
is documented including profit and loss: Docs - Trade
There is a screenshot from a debugging session showing you exactly where trades are stored and an example from @ab_trader with
notify_trade
, which you can use (and it would be my recommendation) store the trades.- What is exactly your problem?
- Which is the information you cannot access?
-
Yes sorry. I think i need to be more specific. I am building a strategy which builds patterns based on successful trades. The best way for me to process this would be by dumping all trades into a pandas dataframe with headers shown in the trades sections of docs i.e. ref, status, tradeid, etc. In your opinion what would be the easiest way of achieving this? Thanks again!
-
Collect trades during
notify_trade
and add them to your dataframe. -
Haha - it was hiding in plain sight! or I was just being blind! Thanks a lot guys!