How can I get the price at which buy executed?
Hello. I'm trying to make a exit strategy that clears a position if the current price is lower than the price at which buy executed for the last time, for example, like the below.
if self.price_executed < self.dataclose:
self.order = self.sell()
I wonder how I can get the price of the last buy execution .
Thank you for reading this!
Maxim Korobov last edited by
def notify_order(self, order)
event in strategy.
Read the quickstart: https://www.backtrader.com/docu/quickstart/quickstart.html.
The notified order contains two fields:
The second contains further fields like
price, to indicate the matching price. As pointed out by @Maxim-Korobov , the QuickStart contains code using it:
self.log('BUY EXECUTED, %.2f' % order.executed.price)
Thank you for your answer! I'm following the QuickStart and now I understand that the command shows executed prices, but I actually don't understand how to use it in the function to compare the two prices. Here's the error I get and what I tried.
Traceback (most recent call last): File "D:\PYTHON_SOURCE\temp.py", line 159, in <module> cerebro.run() File "D:\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 794, in run runstrat = self.runstrategies(iterstrat) File "D:\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 919, in runstrategies self._runonce(runstrats) File "D:\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1294, in _runonce strat._oncepost(dt0) File "D:\Anaconda3\lib\site-packages\backtrader\strategy.py", line 267, in _oncepost self.next() File "D:\PYTHON_SOURCE\temp.py", line 108, in next if order.executed.price < self.dataclose: NameError: name 'order' is not defined def next(self): self.log('Close, %.2f' % self.dataclose) if self.order: return if not self.position: if self.stoch > 80: self.log('BUY CREATE, %.2f' % self.dataclose) self.order = self.buy() else: if order.executed.price < self.dataclose: #ERROR OCCURS HERE self.log('SELL CREATE, %.2f' % self.dataclose) self.order = self.sell()
You need to keep a reference to the order notified in
notify_order, to later use it in
I'm sorry. I'm new to Object Oriented Programming and Python. Do you mean that I need to add order to next as a method parameter? I wonder why def notify_order(self, order): can get order.executed.price when it occurs error in def next(self, order):
I would appreciate it if you could give me an example code, if it doesn't take too much of your time.
Since your nickname is also
nullpy, the assumption is that also Python is not your strongest point and not only
A very sincere recommendation is to polish your Python skills before using backtrader or any other algorithmic trading platform. A winning algorithm is not going to be so simple and will have several interactions between not only methods of the same class/instance but with many other.
In any case:
def next(self, order)doesn't exist. The signature of the method is in every code sample
def next(self). No
orderis sent to
Yes, I think I should study more on the language. Thank you for your recommendation. I will try to find ou how the program works!