To help others who look to trade breakouts via stop orders whereby they want to trade the initial continuation move when price first breaks the breakout price (in this case previous bar high) I've elaborated on the code in the previous post.
The key is needing to ensure a) capture 1st time price breaks the breakout price, b) ensuring price has not moved significantly above the breakout price and c) time since breakout occurred is not a significant time later.
The steps you need to go through to achieve the above are:
1. Store the datetime of when price first breaks the previous bar high and importantly ensure this can't be updated until a new breakout price has formed - which in this case would be after a new bar has closed and thus a new High[1].
2. Store breakout bar number (so can reset when a new bar forms in line with the above).
3. If price is still within the set range after 1st breaking the previous bar high (in this case <2 pips above the previous bar high) then set stop order price.
4. If time since initial breakout is < allowed time (in this case 2mins since breakout) then place stop order.
5. Cancel stop order if allowed time since breakout has been exceeded.
6. Reset looking for initial breakout when a new bar is completed and thus have a new reference high[1].
Code: Select all
[IntrabarOrderGeneration = true]
variables:
intrabarpersist mybid (0),
intrabarpersist stopentryprice (0),
intrabarpersist TradeSentLong (0),
intrabarpersist TargetPrice (0),
intrabarpersist StopLossPrice (0),
intrabarpersist stoplogging (false),
intrabarpersist BODT (0),
intrabarpersist lookforlong (0),
intrabarpersist BOBarnum (0),
mybidextreme (0);
mybid = close ; //for backtesting only - in live trading mybid=insidebid
stoplogging = false ;
mybidextreme = high[1] + 0.0002 ; //ensures bid price that 1st crosses previous bar high isn't too high
//Store BO datetime and bar number when bid price 1st crosses previous bar high
if mybid > high[1] and lookforlong = 0 then
begin
BODT = datetime + (2/1440); //2mins after bid price 1st crosses previous bar high
BOBarNum = barnumber ; //remember bar counting starts after maxbarsback
lookforlong = 1 ; //ensures only capturing 1st time price breaks high as not reset until a new bar and thus a new breakout price is formed
end;
//Ensures bid price that broke previous bar high is a maximum of 2 pips beyond high and sets stoporder price
if mybid > high[1] and mybid < mybidextreme and TradeSentLong = 0 and lookforlong = 1 then
begin
stopentryprice = mybid + 0.0002;
TradeSentLong = 1;
end;
//Keep stop order in place < BODT time from when bid 1st crossed previous bar high i.e. < 2mins
If TradeSentLong = 1 and datetime < BODT and stoplogging = false then
begin
buy ("long") 100 contracts next bar stopentryprice stop ;
//debugging
Print (Date:7:0, ",",
Time:4:0, ",",
NumToStr ( mybid, 5) , ",",
NumToStr (stopentryprice, 5), ",",
NumToStr (High[1], 5), ",",
datetimetostring(BODT), ",",
NumToStr (currentbar,0), ",",
NumToStr (BOBarnum,0));
end;
//Set TP, SL and reset TSL
if marketposition > 0 then
begin
TradeSentLong = 0 ;
TargetPrice = PosTradeEntryPrice(0,0) + 0.0010 ;
StopLossPrice = PosTradeEntryPrice(0,0) - 0.0010 ;
stoplogging = true ;
sell ("TP") 100 contracts next bar TargetPrice limit ;
sell ("SL") 100 contracts next bar StopLossPrice Stop ;
end;
//Reset TSL if BODT time reached
if TradeSentLong = 1 and datetime >= BODT then
TradeSentLong = 0 ;
//Reset Lookforlong when a new bar completed
if currentbar > BOBarnum then
Lookforlong = 0 ;
General Observations:
1. Mybid refers to tick close in backtest (with Bar Magnifier set to 1 tick precision), in live markets you would use mybid = insidebid (can use GetAppInfo etc within an if statement to switch between live trading and backtesting).
2. In line with the above note that insidebid is NOT updated during a tick and there is no definitive way of obtaining the actual latest bid price. You can use Recalclastbarafter to force an update if ticks are updated less than 0.1 seconds - it's a bit of a hack really though compared to just being able to call the latest bid price at various points within a script. Thus the first reported bid within the script that breaks the previous bar high is the same bid price used within the max entry range, therefore if the 1st bid is higher than this range it won't trade - covers against unusually high slippage/illiquid markets. If using market orders this in my experience means you suffer more slippage on average as you can't do a proper slippage check just prior to entry as insidebid price script refers too is potentially outdated. Recalclastbarafter is not useful in backtesting of course as mybid = tick closing price i.e. it's a static price.
3. I try not to refer to marketposition within conditions and resets if possible. There can be latency between script giving the signal to enter a trade and a trade opening and in meantime a new signal could be created thus updating values before marketposition has changed to 1 or -1. Thus you need to make sure that new values can't be created due this latency.
Also have to consider what happens if an order is rejected, how does your script handle resets if MP doesn't change i.e. don't always assume a signal will actually lead to a change in MP. Hint - you can't just use if MP = 0 because of the potential latency issue mentioned above.