Hi All
One key strategy I use manually is confirmation of direction with market structure or break of market structure. This uses significant higher highs and significant lower lows.
I am wondering if it is possible to code this? For example if I enter into a Long trade with defined target (say TP=100) and stop loss (say SL=20), then when the market structure continues in my favour I want to move my SL to BE & then continue to move my SL via trailing stop as market continues in my favour
I currently do this by (on a Long Trade) looking for a past significant high to be broken up after entering ( current higher high) and a higher low been established after a previous significant Low.
I’ve attached a Line & Point as well as a candlestick chart to illustrate the strategy, so basically I would be looking for a sequence as below as price cycles up:
- H2 > H1
- L2 > L1
- Then move SL to break even
- IF H3 > H2 & L3 > L2 Then
- Move SL to L1 etc etc
- Exit would be if price breaks this market structure say if
- H3 < H2 & L3 < L2
The strategy obviously would need to be scanning previous reference bars from entering into the trade (or last reference bar if first BE condition has already been met) to establish these significant Highs & Lows rather than just the previous bar.
Does this sound feasible?
There you go (I did not test it, though):
ONCE SigHI1 = 0
ONCE SigHI2 = 0
ONCE SigLO1 = 0
ONCE SigLO2 = 0
ONCE LookBack = 10
IF Not OnMarket Then
MySL = 0
ENDIF
MaxHI = max(open,close)
MinLO = min(open,close)
Bullish = close > open
Bearish = close < open
SwingHI = highest[LookBack](MaxHI)
SwingLO = lowest[LookBack](MinLO)
SigHI = SwingHI AND Bearish AND Bullish[1]
SigLO = SwingLO AND Bullish AND Bearish[1]
IF LongOnMarket AND SigHI > 0 AND SigHI > SigHI1 THEN
SigHI2 = SigHI1
SigHI1 = SigHI
IF SigHI1 > SigLO1 THEN
MySL = max(TradePrice,max(MySL,SigLO1))
ENDIF
ELSIF ShortOnMarket AND SigLO > 0 AND SigLO < SigLO1 THEN
SigLO2 = SigLO1
SigLO1 = SigLO
IF SigLO1 < SigHI1 THEN
MySL = min(TradePrice,min(MySL,SigLO1))
ENDIF
ENDIF
IF OnMarket AND MySL <> 0 THEN
SELL AT MySL Stop
EXITSHORT AT MySL Stop
ENDIF
a significant Swing point is often recognized as such by the space to its left, so I used a LOOKBACK value of 10 bars, which you can adjust to suit your needs.
Of course this snippet is to be added to your existing code.
Above added as Log 228 to here …
Snippet Link Library
Thanks Roberto, I’ll give this a go and see how she flies!
I appreciate it
I changed line 6 to “ Not OnMarket” (there was a missing space). Sorry.
Cool, thanks
Initial tests were getting an error message of:
Variable not used Sighi2 & Siglo2
Indeed they are not used, so you can delete lines 2, 4, 18 and 24.
//TAKE PROFITS AT BREAK OF MARKET STRUCTURE
//-------------------------------------------------------------------------
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
if onmarket or strategyprofit<>strategyprofit[1] then
flag = 1
endif
if dayofweek <> dayofweek[1] then
flag = 0
endif
entry=13495
SET STOP pLOSS sl
SET TARGET pPROFIT tp
sl=20
tp=100
// Conditions to enter long position
notradetime1=213000
notradetime2=233000
notradetime=time>notradetime1 and time<notradetime2
timeframe (20 seconds, updateonclose)
c1=low<=entry
timeframe(default, updateonclose)
IF not daysForbiddenEntry and not flag and not onmarket and not notradetime and c1 Then
Buy 1 perpoint AT MARKET
ENDIF
// from PRC
timeframe (60 minutes, updateonclose)
ONCE SigHI1 = 0
//ONCE SigHI2 = 0
ONCE SigLO1 = 0
//ONCE SigLO2 = 0
ONCE LookBack = 5
IF Not OnMarket Then
MySL = 0
ENDIF
MaxHI = max(open,close)
MinLO = min(open,close)
Bullish = close > open
Bearish = close < open
SwingHI = highest[LookBack](MaxHI)
SwingLO = lowest[LookBack](MinLO)
SigHI = SwingHI AND Bearish AND Bullish[1]
SigLO = SwingLO AND Bullish AND Bearish[1]
IF LongOnMarket AND SigHI > 0 AND SigHI > SigHI1 THEN
//SigHI2 = SigHI1
SigHI1 = SigHI
IF SigHI1 > SigLO1 THEN
MySL = max(TradePrice,max(MySL,SigLO1))
ENDIF
ELSIF ShortOnMarket AND SigLO > 0 AND SigLO < SigLO1 THEN
//SigLO2 = SigLO1
SigLO1 = SigLO
IF SigLO1 < SigHI1 THEN
MySL = min(TradePrice,min(MySL,SigLO1))
ENDIF
ENDIF
IF OnMarket AND MySL <> 0 THEN
SELL AT MySL Stop
EXITSHORT AT MySL Stop
ENDIF
Hi Roberto
Please find attached the code in which I’ve inserted your modified suggestion. I’ve also attached the results.
I cannot see why it doesn’t take profits at the first break of Market structure (I’ve marked up the chart)?
The code I’ve used gets me in at a set point on the 20 sec chart & I’me using your code on the 1 hr to monitor when there is a sign of it reversing.
The plan is if I can get this recognising a change in market structure (either a break, hence reversal or a continuation I can then ask it to either move my stop to BE and follow it up on subsequent significant higher highs etc. Or take profits if a definite
break of structure.
Any suggestions
Steve
I’ll take a look at your code tomorrow morning.
Please do not add text to your code, unless each line is commented with leading double slashes “//“. Thank you 🙂
I moved it below your code.
Hi
Line 21 or 27 something wrong in code?
I tested it on Usd/Cad, 1h + 20s, but no trade was enterd on June 23rd at 13:00 (my first trade was entered on the 24th.
Can you post a more recent trade (20 seconds only have about 5 days of data history) ?
Hi Roberto – I’ve used USD/JPY with a set point at 10691 just to get an entry
Regards
Steve
Sorry, buy I don’t jave thet instrument and can’t replicate your trades.
I have Spot UsdJpy, or IG’s CFD.