How to execute order during gap open on next bar ?
-
May be I have misconception on using the backtrader. Suppose I have a strategy to calculate a target price to buy (or sell). In the next():, I issued an order with StopLimit. If the target price is within next day's high low range, it triggers exactly at the target price. That matches my expectation.
But if there is a gap, it never trigger.
What I want is, whenever the market break away from the target price, I will enter the market, at that price if possible, otherwise at the market price. I tried different order types, still cannot get this to work.
-
@saikee-wong said in How to execute order during gap open on next bar ?:
May be I have misconception on using the backtrader
We don't know, the provided information is not enough to understand what your actual problem is.
@saikee-wong said in How to execute order during gap open on next bar ?:
I issued an order with StopLimit
@saikee-wong said in How to execute order during gap open on next bar ?:
But if there is a gap, it never trigger.
Do you buy? Do you sell? In which direction does the gap go?
@saikee-wong said in How to execute order during gap open on next bar ?:
What I want is, whenever the market break away from the target price, I will enter the market, at that price if possible, otherwise at the market price
If you issue a
StopLimit
you can only enter at theLimit
or at a better price. You CANNOT enter at a worse price (which seems the implication from "breakaway, i.e.: the price runs away from your order) and you CANNOT enter atMarket
price.The best you can do is explain your scenario with exact orders, prices and market movement.
-
Thank you for the reply.
For example, the figure below:
https://www.dropbox.com/s/amb7swq9mhwyxqp/190111z.png?dl=0
I calculated the top yellow line and projected a target price, if it penetrates, enter the market with buy order. So, with StopLimit and if the next day open below the target price and penetrate up, it works perfectly. My strategy is to buy even if gap opening above the target price happened, then just buy at open price or market price, how can I simulate that ?
-
You need orders like
MIT
(akaMarket If Touched
), but such an order type is not implemented in backtrader. You would couple it with yourStopLimit
and make them OCO orders.You would also need to make sure your broker supports bundling both orders together as OCO.
Your real best option is to use intraday data.
-
You could also simulate the gap entering with a simple
Stop
and link both theStopLimit
andStop
to make them OCO orders, which guarantees that only one of them gets executed. See