morning chaps,
I am attempting to put together a very simple breakout strategy of a range formed over a certain period of time. I have pieced together something that is fairly amateur and I am not capable of really adding the ‘complexity’ (it’s very simple) I need. For some reason, although I have only stipulated that it not trades on a Sunday (something I will apply to more days when it actually works), it doesn’t seem to place trades every day.
Is there anyone that could cast an eye and help with the below?
The time of the range is 11:00am – 12:00pm
The range is from high to the low of the last hour from 12:00pm and the trade is made on the breakout
On a long, the range low is the stop loss and vice versa for a short
Target is 1.5 x the range
Once it hits 1:1 RRR the stoploss moves to breakeven
This is a very simple but fairly robust strategy if anyone could support it, I think it would take you about two minutes to solve
defparam cumulateorders = FALSE
DEFPARAM FLATAFTER = 153000
daysForbiddenEntry = OpenDayOfWeek = 0
noEntryBeforeTime = 115500
timeEnterBefore = time >= noEntryBeforeTime
noEntryAfterTime = 120500
timeEnterAfter = time < noEntryAfterTime
// --- parameters
Size = 1 //position size
StopLossLong = lowest[LookbackPeriod](low)
StopLossShort = highest[LookbackPeriod](high)
LookbackPeriod = 12 //lookback period to find highest high and lowest low (price range)
PointsDistance = 5 //distance in points to add/substract from highest high or lowest low to put the pending stop orders
// ------------
if not onmarket AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry then
BUY Size CONTRACTS AT highest[LookbackPeriod](high)+PointsDistance*pointsize STOP
STOPLOSSLONG = lowest[LookbackPeriod](low[1])
TARGETLONG = highest[LookbackPeriod](high[1])+(1.5*(highest[LookbackPeriod](high[1])-lowest[LookbackPeriod](low[1])))
endif
if onmarket AND LONGONMARKET then
sell at STOPLOSSLONG stop
sell at TARGETLONG limit
endif
if not onmarket AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry then
SELLSHORT Size CONTRACTS AT lowest[LookbackPeriod](low)-PointsDistance*pointsize STOP
STOPLOSSSHORT = highest[LookbackPeriod](high[1.5])
TARGETSHORT = lowest[LookbackPeriod](low[1])-1*(highest[LookbackPeriod](high[1]-lowest[LookbackPeriod](low[1])))
endif
if onmarket AND SHORTONMARKET then
exitshort at STOPLOSSSHORT stop
exitshort at TARGETSHORT limit
endif
That seems fine, but to make the SL and TP active from the entry candle you should also copy lines 28 and 29 between lines 24 and 25 for LONG trades and copy lines 39-40 between lines 35 and 36 for SHORT trades.
It will trade every time conditions are met, not everyday.
Pending orders only last 1 bar, then they are cancelled automatically. So they are placed only within the specified time range.
Hi Roberto,
Thanks for coming back so quickly! and it’s good to hear I haven’t completely messed this up… I will amend the lines you have referenced.
Although there are conditions, they are only to trade within a certain time and not on a Sunday – shouldn’t it, therefore, be able to trade each day? I apologise if I am being short-sighted…
In addition, is it possible to outline how to move stop loss to breakeven on reaching a rrr of 1:1? Or perhaps there is a similar/previous post that I can reference so not to take too much time.
best, Tom
Sorry Roberto – it’s just occurred to me what you mean… that price may not break out of the range within those time periods and therefore a trade won’t be made.
Hi, i have made the amendments but unfortunately, the stop loss and profit aren’t activating (at least how I would like them to) – for instance on a short trade that instantly whipsaws long, it doesn’t exit the trade for quite a while, which points to the stop loss not being in place at the high of the range of the last 12 candles. Is the algo looking back 12 periods on each new candle? if so, how do I fix the stoploss in place on the execution of the trade?
Please post the current code you are using.
Just to confirm this is to be traded on USDJPY 5min
defparam cumulateorders = FALSE
DEFPARAM FLATAFTER = 173000
daysForbiddenEntry = OpenDayOfWeek = 0 OR OpenDayOfWeek = 5 OR OpenDayOfWeek = 6
noEntryBeforeTime = 115500
timeEnterBefore = time >= noEntryBeforeTime
noEntryAfterTime = 140000
timeEnterAfter = time < noEntryAfterTime
// --- parameters
Size = 3 //position size
StopLossLong = lowest[LookbackPeriod](low)
StopLossShort = highest[LookbackPeriod](high)
LookbackPeriod = 12 //lookback period to find highest high and lowest low (price range)
PointsDistance = 0 //distance in points to add/substract from highest high or lowest low to put the pending stop orders
// ------------
if not onmarket AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry then
BUY Size CONTRACTS AT highest[LookbackPeriod](high)+PointsDistance*pointsize STOP
STOPLOSSLONG = lowest[LookbackPeriod](low)
TARGETLONG = highest[LookbackPeriod](high[1])+(1.5*(highest[LookbackPeriod](high[1])-lowest[LookbackPeriod](low[1])))
sell at STOPLOSSLONG stop
sell at TARGETLONG limit
endif
if not onmarket AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry then
SELLSHORT Size CONTRACTS AT lowest[LookbackPeriod](low)-PointsDistance*pointsize STOP
STOPLOSSSHORT = highest[LookbackPeriod](high)
TARGETSHORT = lowest[LookbackPeriod](low[1])-(1.5*(highest[LookbackPeriod](high[1]-lowest[LookbackPeriod](low[1]))))
exitshort at STOPLOSSSHORT stop
exitshort at TARGETSHORT limit
endif
To make it Stop & Reverse, just replace Not OnMarket in line 21 with Not LongOnMarket and Not OnMarket in line 29 with Not ShortOnMarket.
You shouldn’t have removed the original lines 27-30 and 38-41. Restore them into their place.