After my stop is hit, I want to check a condition and then place an order (which would be executed on the open on the next candle, after the stop is hit). However my understanding is that the stop order is executed after next is called, so it wouldn't execute the order on the candle immediately after the stop is hit, rather it would get executed on the 2nd candle after the stop is hit rather than the first.
Is there a way to solve this problem?
Best posts made by student
-
Execute trade immediatly after a stop is hit
Latest posts made by student
-
RE: Net Profit/Loss Observer Misconfigured
Okay fixed that issue as well using
cerebro.addobserver(bt.observers.BuySell, barplot=True)
. -
RE: Net Profit/Loss Observer Misconfigured
Okay I managed to fix that by adding
stdstats=False
and manually adding the observers. However I noticed one of the sell triangles is behind the candle
You can see that a trade is closed in the top observer and if you look closely you can see a red triangle behind the candle. Not sure if this is a bug or something I've done wrong. -
Net Profit/Loss Observer Misconfigured
I added a second timeframe and the panel shown above used to show "Trades - Net Profit/Loss" panel with red and blue dots. Now it doesn't show the colour of the dots so I don't know if the trade is a profit or a loss. Is there some sort of way to configure the observer so it goes back to how it was by default? Why does it change so the image as shown when I add a second timeframe? -
RE: Execute trade immediatly after a stop is hit
Thanks for bearing with, I added
cerebro.broker.setcommission(leverage=100)
now everything works how I want it. Thanks.
-
RE: Execute trade immediatly after a stop is hit
I thought you could use leverage/margin in backtrader? I can use it for shorts it seems but not longs?
-
RE: Execute trade immediatly after a stop is hit
Okay I will post the code here as well
import backtrader as bt import pandas as pd import datetime # get data data = pd.read_csv('https://pastebin.com/raw/8gah3XhH') # convert timestamp to datetime object data['timestamp'] = pd.to_datetime(data['timestamp'], utc=True) data.set_index('timestamp', inplace=True) class TestStrat(bt.Strategy): params = dict( printout=True ) # Stuff for logging def log(self, txt, dt=None): if self.p.printout: dt = dt or self.data.datetime[0] dt = bt.num2date(dt) print('%s, %s' % (dt.isoformat(), txt)) … self.log('Order Rejected') # Sentinel to None: new orders allowed self.order = None def next(self): if self.data.datetime.time() == datetime.time(8,40): self.buy(size=3.63) # Create a cerebro entity cerebro = bt.Cerebro() # Add strategy cerebro.addstrategy(TestStrat) # Pass it to the backtrader datafeed and add it to the cerebro data = bt.feeds.PandasData(dataname=data) # Resample it to 5 min bars cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5) cerebro.broker.setcash(10000) cerebro.run()
What prints to the console:
2019-10-24T08:40:00, ORDER ACCEPTED/SUBMITTED 2019-10-24T08:45:00, Order Margin 2019-10-25T08:40:00, ORDER ACCEPTED/SUBMITTED 2019-10-25T08:45:00, Order Margin 2019-10-26T08:40:00, ORDER ACCEPTED/SUBMITTED 2019-10-26T08:45:00, Order Margin
A snippet of the data
2019-10-24 08:38:00+00:00,7440.5,7440.5,7434.0,7436.0 2019-10-24 08:39:00+00:00,7436.0,7441.5,7435.5,7441.0 2019-10-24 08:40:00+00:00,7441.0,7443.0,7441.0,7443.0 2019-10-24 08:41:00+00:00,7443.0,7446.5,7442.5,7444.0 2019-10-24 08:42:00+00:00,7444.0,7444.5,7444.0,7444.5 2019-10-24 08:43:00+00:00,7444.5,7444.5,7444.0,7444.5 2019-10-24 08:44:00+00:00,7444.5,7445.0,7444.0,7444.5 2019-10-24 08:45:00+00:00,7444.5,7444.5,7441.5,7442.0 2019-10-24 08:46:00+00:00,7442.0,7442.0,7441.5,7442.0 2019-10-24 08:47:00+00:00,7442.0,7446.5,7441.5,7446.0 2019-10-24 08:48:00+00:00,7446.0,7446.5,7443.5,7444.0 2019-10-24 08:49:00+00:00,7444.0,7444.0,7443.0,7443.0 2019-10-24 08:50:00+00:00,7443.0,7443.5,7443.0,7443.5
-
RE: Execute trade immediatly after a stop is hit
I've stripped the code and data down and stuck it in a google colab here https://colab.research.google.com/drive/14Jz3DYBjIejVIcir4BInlw1kUF2hEOJr . You can run it in your browser my pressing the play button next to the code. As you can see, same problem still occurs.
-
RE: Execute trade immediatly after a stop is hit
Seems to work when I do
self.buy(size=3.63*0.3)
I can't understand why I'm getting margin called? I have no other positions open, no other orders?
-
RE: Execute trade immediatly after a stop is hit
Okay, so I stepped through my code. The stop gets hit and the buy order does get submitted in the right place. The problem is in fact that I'm running into order.margin, which prevents the buy order being taken. I'm logging everything and I can see there is no additional orders are before this. My self.broker.get_cash() value is 9801.0 and I'm trying to submit this order
self.buy(size=3.63)
The close of the candle when this is submitted is 7443.0 and the open of the candle after is also 7443.0. Yes I am using leverage but surely this is still fine?
"Order.Margin: the order execution would imply a margin call and the previously accepted order has been taken off the system"
Why would I be getting margin called with these values?
-
RE: Execute trade immediatly after a stop is hit
To clarify further, my issue isn't with the execution of the stop as @ab_trader suggested, that works fine. The issue is with placing an order after the stop has been executed and having it execute in the following bar.