Again a question about the trailing stop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #192497 quote
    f1_maik
    Participant
    Junior

    I don’t even bother to ask because so much has already been written here about trailing stops. But I still don’t quite get it. Please can someone help me again.
    I have this trailing stop code and would like to add to it the minimum stop of IG and a points-to-keep in % of the price.
    How can this look like? In gray I have made the minimum stop as I think it would be correct. But the points-to-keep function?

     

    //a = 1
    //b = 0.1
    //c = 0.1
    
    set stop %loss  a
    trailingPercent = b
    stepPercent = c
    
    if onmarket then
    trailingstart = tradeprice(1)*(trailingpercent/100) // - 6*pipsize 
    trailingstep = tradeprice(1)*(stepPercent/100) // - 6*pipsize
    endif
     
    
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    IF LONGONMARKET THEN
    IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
    newSL = tradeprice(1)+trailingstep
    ENDIF
    IF newSL>0 AND close-newSL>trailingstep THEN
    newSL = newSL+trailingstep
    ENDIF
    ENDIF
     
    IF SHORTONMARKET THEN
    IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
    newSL = tradeprice(1)-trailingstep
    ENDIF
    IF newSL>0 AND newSL-close>trailingstep THEN
    newSL = newSL-trailingstep
    ENDIF
    ENDIF
     
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    #192498 quote
    robertogozzi
    Moderator
    Master

    There you go (not tested):

    //a = 1
    //b = 0.1
    //c = 0.1
    MinStop = 10         //10    pips minimum stop loss required
    Points2Keep = 0.01   //0.01% points to keep
     
    set stop ploss  max(MinStop,TradePrice(1) * a / 100 / pipsize)
    trailingPercent = b
    stepPercent = c
    p2k         = TradePrice(1) * Points2Keep / 100
     
    if onmarket then
    trailingstart = tradeprice(1)*(trailingpercent/100) // - 6*pipsize 
    trailingstep = tradeprice(1)*(stepPercent/100) // - 6*pipsize
    endif
     
     
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    IF LONGONMARKET THEN
    IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
    newSL = tradeprice(1)+trailingstep+p2k
    ENDIF
    IF newSL>0 AND close-newSL>trailingstep THEN
    newSL = newSL+trailingstep
    ENDIF
    ENDIF
     
    IF SHORTONMARKET THEN
    IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
    newSL = tradeprice(1)-trailingstep+p2k
    ENDIF
    IF newSL>0 AND newSL-close>trailingstep THEN
    newSL = newSL-trailingstep
    ENDIF
    ENDIF
     
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    f1_maik thanked this post
    #192499 quote
    f1_maik
    Participant
    Junior

    Thank you very much.
    I will test it.

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

Again a question about the trailing stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
f1_maik @f1_maik Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by f1_maik
3 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/01/2022
Status: Active
Attachments: No files
Logo Logo
Loading...