How to add an indicator showing whether previous trade made money or lost money
-
Using the moving average cross over system. But I want to add another criteria that I only buy when the previous trade made money.
I.e I buy if MA(20) > MA(40) AND last trade made money.
How do I implement that indicator showing whether previous trade made money or not? -
You could store the last cost in your strategy. You would watch for finished trades and then inspect the attributes of the closed trade:
commission (float): current accumulated commission
pnl (float): current profit and loss of the trade (gross pnl)
pnlcomm (float): current profit and loss of the trade minus commission (net pnl)The notification can be accessed in notify_trade
You could also create an indicator the same way.
-
@chat123 said in How to add an indicator showing whether previous trade made money or lost money:
How do I implement that indicator showing whether previous trade made money or not?
The concept
Indicator
is meant to operate on the data feeds (or information already derived thereof) and not on the money flows.What you are looking for, if you really want to package it, is an
Analyzer
which has, as theStrategy
anotify_trade
method and you can keep count of the positive/negative trades without mixing it up with your logic.Or else follow the approach mentioned by @dasch
-
@backtrader i did not check if indicators get notified of trades. Learned something today :)
Here is some code i use as an analyzer, i stripped some code, only left trade check in:
from __future__ import (absolute_import, division, print_function, unicode_literals) import os import backtrader as bt class InMoovTradeAnalyzer(bt.Analyzer): params = dict( path='', # path being observed range_positive=40, # range for positive positions in pips range_negative=20, # range for negative positions in pips stoploss=15, # stoploss distance in pips pips_notif=5, # notify every n pips ) def __init__(self): self._path = os.path.abspath(self.p.path) # path to store files self._live = False # notify inmoov only if live trading self._lastState = None # Store last order state (positive/negative) self._lastPipDiff = None # Last pip diff seen (pips, always positive) def notify_trade(self, trade): #if trade.justopened: # self.createAction("text", "Attention, a new trade was just opened.") # self.createAction("gesture", "trade") # self.createAction("notif", "negative_1") if trade.isclosed: # Reset informations after trade is closed self._lastState = None self._lastPipDiff #if trade.pnl > 0: # self.createAction("text", "Trade was successfully closed. Profit is %.2f Euro." % trade.pnl) # self.createAction("gesture", "profit") #else: # self.createAction("text", "Trade was closed. Negative trade with %.2f Euro." % trade.pnl) # self.createAction("gesture", "loss")
-
Seems the right way to go, imho.
-
Thank you all! Will I be able to use analyzer's value to determinate the next trade? I.e I buy if MA(20) > MA(40) AND last trade made money. How to code this with a analyzer showing if last trade made money?
-
i.e to code the logic "buy if MA(20) > MA(40) AND last trade made money." in "def next(self):", can I use analyzer? Or best way is dasch's way above?
-