Strategia trailing stop

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #82462 quote
    peru2404
    Participant
    Average

    Salve a tutti volevo sapere come poter programmare una strategia che venda sotto (e compri sopra) la media a 63 periodi impostando lo stop a 10  e impostando un trailing stop che parta da 30 e si sposti di 10.

    Grazie

    #82571 quote
    robertogozzi
    Moderator
    Master

    Prova questa (io l’ho testata solo sommariamente):

    DEFPARAM CumulateOrders = FALSE
    Media = average[63,0](close)
    Sopra = close > Media
    Sotto = close < Media
    IF Sopra AND Not OnMarket THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    IF Sotto AND Not OnMarket THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    //************************************************************************
    //     trailing stop function
    TrailingStart   = 30 * pipsize   //20   trailing will start @trailinstart points profit
    TrailingStep    = 10 * pipsize   //10   trailing step to move the "stoploss"
    MainSL          = 10 * pipsize   //10   initial SL
    //
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL        = 0
    InitialSL    = 0
    ENDIF
    //
    IF LongOnMarket THEN
    IF NewSL = 0 THEN
    MyProfit = close - TradePrice
    IF MyProfit >= TrailingStart THEN
    SLunits = max(1,round(((MyProfit - TrailingStart) / TrailingStep) - 0.5) + 1)
    NewSL   = TradePrice + (SLunits * TrailingStep) - MainSL
    ENDIF
    ELSE
    MyProfit = close - NewSL
    IF MyProfit >= TrailingStart THEN
    SLunits = round(((MyProfit - TrailingStart) / TrailingStep) - 0.5)
    NewSL   = NewSL + (SLunits * TrailingStep)
    ENDIF
    ENDIF
    ELSIF ShortOnMarket THEN
    IF NewSL = 0 THEN
    MyProfit = TradePrice - close
    IF MyProfit >= TrailingStart THEN
    SLunits = max(1,round(((MyProfit - TrailingStart) / TrailingStep) - 0.5) + 1)
    NewSL   = (SLunits * TrailingStep) + MainSL - TradePrice
    ENDIF
    ELSE
    MyProfit = NewSL - close
    IF MyProfit >= TrailingStart THEN
    SLunits = round(((MyProfit - TrailingStart) / TrailingStep) - 0.5)
    NewSL   = NewSL - (SLunits * TrailingStep)
    ENDIF
    ENDIF
    ENDIF
    //
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL      AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    #82637 quote
    peru2404
    Participant
    Average

    Grazie Roberto quando hai tempo riesci a postarmi anche quell altra di qualche giorno fa? grazie

    #82640 quote
    peru2404
    Participant
    Average

    e comunque questa non funziona mi dice che non è stata utilizzata la variabile initalSL

    ed eliminandola non fa comunque quello che chiedevo

    #82668 quote
    robertogozzi
    Moderator
    Master

    Devi eliminarla, si.

    Su quale strumento, TF e candela l’hai provata e non funziona?

    Se il profitto raggiunge 30+ viene spostato lo stop di 10 pips, quindi la prima volta va in pareggio. Se il profitto raggiunge 40+ (sempre la prima volta), vuoi che lo strop si sposti di soli 10 o di 20 (in modo da tenere sempre una distanza di 30 pips di differenza)?

    Ricordi qual’era? Forse quella di Nicolas https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/?

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

Strategia trailing stop


Supporto ProOrder

New Reply
Author
author-avatar
peru2404 @peru2404 Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by robertogozzi
7 years, 3 months ago.

Topic Details
Forum: Supporto ProOrder
Language: Italian
Started: 10/10/2018
Status: Active
Attachments: No files
Logo Logo
Loading...