How can I use Dynamic stop loss with my Code?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #179523 quote
    efahmy
    Participant
    Average
    // Definition of code parameters
    DEFPARAM CumulateOrders = FALSE // Cumulating positions deactivated
    timeframe(DEFAULT)
    positionsize = 1
    // Conditions to enter long positions
    myPRCDynamicRSI, myPRCDynamicRSI2, myPRCDynamicRSI3, myPRCDynamicRSI4 = CALL "PRC_DynamicRSI"[0.1, 0.1, 14, 60]
    c1 = (myPRCDynamicRSI > myPRCDynamicRSI2)
    indicator1, indicator2, ignored = CALL "Terminus indicator"[9, 6, 3, 1, 0]
    c2 = (indicator1 CROSSES OVER indicator2)
    ignored, indicator3, indicator4 = CALL "Terminus indicator"[9, 6, 3, 1, 0]
    c3 = (indicator3 CROSSES OVER indicator4)
    IF c1 AND c2 AND C3 THEN
    BUY positionsize CONTRACT AT MARKET
    SET STOP pTRAILING 4.0
    SET TARGET pPROFIT 3.0
    ENDIF
    C4 = (indicator1 > indicator2)
    C5 = (indicator3 > indicator4)
    C6 = (myPRCDynamicRSI CROSSES OVER myPRCDynamicRSI2)
    IF C4 AND C5 AND C6 THEN
    BUY positionsize CONTRACT AT MARKET
    SET STOP pTRAILING 4.0
    SET TARGET pPROFIT 3.0
    ENDIF
    // EXIT LONG CONDITIONS
    IF LONGONMARKET AND (myPRCDynamicRSI CROSSES UNDER myPRCDynamicRSI2)THEN
    SELL AT MARKET
    ENDIF
    
    //******************************************************************************************************
    //
    //     trailing stop function
    trailingstep      = 5     //5    trailing step to move the "stoploss" after BreakEven
    startBreakeven    = 5     //5    pips in gain to activate the breakeven function
    PointsToKeep      = 3     //3    pips to keep in profit above/below entry price when the breakeven is activated
    //
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL          = 0
    breakevenLevel = 0
    ENDIF
    //******************************************************************************************************
    // ----- BREAKEVEN code
    //
    //reset the breakevenLevel when no trade are on market
    // --- LONG  side
    IF LONGONMARKET AND (close - tradeprice(1)) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THEN
    breakevenLevel = tradeprice(1) + (PointsToKeep * pipsize)                         //calculate the breakevenLevel
    ENDIF
    // --- SHORT side
    IF SHORTONMARKET AND (tradeprice(1) - close) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THEN
    breakevenLevel = tradeprice(1) + (PointsToKeep * pipsize)                         //calculate the breakevenLevel
    ENDIF
    //Set new Stop Loss
    IF breakevenLevel > 0 THEN
    newSL = BreakEvenLevel
    ENDIF
    //******************************************************************************************************
    // ----- TRAILING STOP code
    //
    //manage long positions
    IF LONGONMARKET AND BreakEvenLevel THEN
    //next moves after BreakEven
    IF newSL > 0 AND ((close - newSL) >= (trailingstep * pipsize)) THEN
    newSL = newSL + (trailingstep * pipsize)
    ENDIF
    ENDIF
    //manage short positions
    IF SHORTONMARKET AND BreakEvenLevel THEN
    //next moves after BreakEven
    IF newSL > 0 AND ((newSL - close) >= (trailingstep * pipsize)) THEN
    newSL = newSL - (trailingstep * pipsize)
    ENDIF
    ENDIF
    //stop order to exit the positions
    IF newSL > 0 THEN
    SELL      AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    //******************************************************************************************************
    #179539 quote
    Nicolas
    Keymaster
    Master

    Even if I understand what you want ask to us with your title, please describe it in your post content next time.

    You have to remove all SET STOP TRAILING of your initial code and copy/paste Roberto’s code at the end. It should operate correctly this way.

    efahmy thanked this post
    #179558 quote
    GraHal
    Participant
    Master

    Is the Terminus Indicator available anywhere please?

    I’ve searched in the Library on here and on MarketPlace, no success.

    #179560 quote
    efahmy
    Participant
    Average

    [attachment file=”Terminus-indicator.itf”]

    #179561 quote
    efahmy
    Participant
    Average

    I have done as you said but it opened the position without setting any stop loss as you can see, how I can fix that please.

    STOP-LOSS.jpg STOP-LOSS.jpg
    #179569 quote
    robertogozzi
    Moderator
    Master

    @efahmy

    Do not embed files of any kind in your post, as this slows down loading the pages.

    Use the attach button only.

    Thank you 🙂

    #179570 quote
    robertogozzi
    Moderator
    Master

    When any code is not really huge (hundreds of lines), it’s advisable to copy and paste the code (along with the ITF file, if available). Thank you 🙂

    This your indicator:

    // Author: Tom's - Leofi
    // Name: Terminus indicator
    // Description: Trend average indicator
    
    // Var indicator
    emaSlow = Average[slow, type](close)
    emaNormal = Average[normal, type](close)
    emaFast = Average[fast, type](close)
    
    // Logic
    if (emaFast > emaNormal and emaNormal > emaSlow) then
    backgroundcolor(0,250,0)
    elsif (emaFast < emaNormal and emaNormal < emaSlow) then
    backgroundcolor(250,0,0)
    else
    backgroundcolor(250,165,0)
    endif
    
    // Boolean draw indicator
    if (drawIndicator) then
    indicatorAlpha = 250
    else
    indicatorAlpha = 0
    endif
    
    return emaFast coloured(200, 200, 200, indicatorAlpha) STYLE(line, 2) as "Fast Average 0", emaNormal coloured(80, 80, 80, indicatorAlpha) STYLE(line, 2) as "Normal Average 1", emaSlow coloured(0, 0, 0, indicatorAlpha) STYLE(line, 2) as "Slow Average 2"
    GraHal thanked this post
    #179571 quote
    efahmy
    Participant
    Average

    Hi Roberto,

    I’m not after the Terminus indicator, I’m after your dynamic stop loss, it doesn’t work with my code as shown in the figure attached, could you please advice.

    #179573 quote
    robertogozzi
    Moderator
    Master

    Yes, but to test it we needed the code.
    It may work while backtesting, not in autotrading, as the trailing stop seems too tight.
    Post details about any issues you have experienced with the above code.

    #179585 quote
    efahmy
    Participant
    Average

    Hi Roberto,

    It seems that this dynamic stop loss works only when the price is above breakeven level, but it doesn’t provide protection initial stop loss and and then trail, is there a chance to add protection stop loss and then work as dynamic??

    #179594 quote
    robertogozzi
    Moderator
    Master

    This is not a STOP LOSS, your strategy needs to take care of that.

    It’s a trailing stop, so it starts working AFTER having gained at least trailingstep pips (at the closing of the bar).

    Keep SET TARGET pPROFIT and replace SET STOP pTRAILING with SET STOP pLOSS (usually TP is at least 1.5+ times the SL).

    efahmy thanked this post
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

How can I use Dynamic stop loss with my Code?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
efahmy @efahmy Participant
Summary

This topic contains 10 replies,
has 4 voices, and was last updated by robertogozzi
4 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/13/2021
Status: Active
Attachments: 1 files
Logo Logo
Loading...