Move to BE on Confirmation of Market Structure

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #137284 quote
    Steveaw
    Participant
    Average

    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:

    1. H2 > H1
    2. L2 > L1
    3. Then move SL to break even
    4. IF H3 > H2 & L3 > L2 Then
    5. Move SL to L1 etc etc
    6. Exit would be if price breaks this market structure say if
    7. 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?

    Screenshot-2020-06-27-at-12.14.55.png Screenshot-2020-06-27-at-12.14.55.png Screenshot-2020-06-27-at-12.15.56.png Screenshot-2020-06-27-at-12.15.56.png
    #137288 quote
    robertogozzi
    Moderator
    Master

    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.

    GraHal and Steveaw thanked this post
    #137290 quote
    GraHal
    Participant
    Master

    Above added as Log 228 to here …

    Snippet Link Library

    robertogozzi thanked this post
    #137292 quote
    Steveaw
    Participant
    Average

    Thanks Roberto, I’ll give this a go and see how she flies!

    I appreciate it

    #137293 quote
    robertogozzi
    Moderator
    Master

    I changed line 6 to “ Not OnMarket” (there was a missing space). Sorry.

    #137297 quote
    Steveaw
    Participant
    Average

    Cool, thanks

    Initial tests were getting an error message of:

    Variable not used Sighi2 & Siglo2

    #137314 quote
    robertogozzi
    Moderator
    Master

    Indeed they are not used, so you can delete lines 2, 4, 18 and 24.

    Steveaw thanked this post
    #137413 quote
    Steveaw
    Participant
    Average
    //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

    Screenshot-2020-06-28-at-17.05.42.png Screenshot-2020-06-28-at-17.05.42.png
    #137416 quote
    robertogozzi
    Moderator
    Master

    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.

    #137427 quote
    auvergnat
    Participant
    Veteran

    Hi

    Line 21 or 27 something wrong in code?

    #137428 quote
    Steveaw
    Participant
    Average

    Great thanks

    #137477 quote
    robertogozzi
    Moderator
    Master

    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) ?

    #137511 quote
    Steveaw
    Participant
    Average

    Will do

    #137529 quote
    Steveaw
    Participant
    Average

    Hi Roberto – I’ve used USD/JPY with a set point at 10691 just to get an entry

    Regards

    Steve

    Screenshot-2020-06-29-at-16.53.29.png Screenshot-2020-06-29-at-16.53.29.png
    #137538 quote
    robertogozzi
    Moderator
    Master

    Sorry, buy I don’t jave thet instrument and can’t replicate your trades.

    I have Spot UsdJpy, or IG’s CFD.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.

Move to BE on Confirmation of Market Structure


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Steveaw @steveaw Participant
Summary

This topic contains 19 replies,
has 4 voices, and was last updated by Steveaw
5 years, 7 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/27/2020
Status: Active
Attachments: 6 files
Logo Logo
Loading...