stop loss & profit

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #196823 quote
    Damien00121
    Participant
    New

    Salve,

    vorrei sapere, se possibile, come tradurre in codice un’istruzione di questo tipo :

    SET STOP %LOSS 3 %TRAILING 6

    per poter controllarne l’andamento e l’uscita anche come indicatore.

    Grazie

    Damien

    #196824 quote
    robertogozzi
    Moderator
    Master

    Innanzitutto occorre dire che

    SET STOP %LOSS 3 %TRAILING 6

    funziona solo in backtest, non live, in qusnto non è possibile indicare due stop diversi sulla stessa linea.

    Mettendoli su linee separate, la seconda prevarrà, in quanto l’esecuzione sequenziale del codice fa si che la seconda linea sovrascriva la prima.

    Quindi devi mettere solo la prima  cioè SET STOP %LOSS 3, poi usare un codice per il trailing stop (sul forum ce ne sono molti tipi).

    Damien00121 thanked this post
    #196877 quote
    Damien00121
    Participant
    New

    Roberto,

    perdonami se continuo nella richiesta.

    Sto cercando nel forum di prorealcode ed effettivamente trovo tanti codici di trailing stop.

    Io vorrei conoscere il codice che replica questo :

    SET STOP %TRAILING x

    laddove x può essere quel 3-4-5%

    Grazie

    Saluti

    Damien

    #196946 quote
    robertogozzi
    Moderator
    Master

    Questo è il codice di Nicolas, modificato per l’uso con le percentuali:

    //*********************************************************************************
    // https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
    // (lines 17- 56)
    // using Percentages
    //
    //trailing stop function
    //
    StartPerCent  = 0.5    //Trailing Stop will activate after 0.5% gain
    StepPerCent   = 0.1    //Trailing Steps will occur at any further 0.1% gain
    //
    trailingstart = PositionPrice * StartPerCent / 100 / PipSize
    trailingstep  = PositionPrice * StepPerCent  / 100 / PipSize
    //
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-PositionPrice>=trailingstart*pipsize THEN
    newSL = PositionPrice+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = max(newSL,newSL+trailingstep*pipsize)
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND PositionPrice-close>=trailingstart*pipsize THEN
    newSL = PositionPrice-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = min(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
    //*********************************************************************************
    //graphonprice PositionPrice coloured(0,0,255,255)
    //graphonprice newSL         coloured(255,0,0,255)
    //graph TrailingStart
    //graph TrailingStep

    Devi assegnare alle due variabili StartPerCent e StepPerCent il valore che preferisci. Poi devi aggiungere TUTTE le righe alla FINE del tuo codice, senza cambiare altro (puoi togliere tutto ciò che è commentato, ovviamente).

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

stop loss & profit


ProOrder: Trading Automatico & Backtesting

New Reply
Author
Summary

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

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 07/05/2022
Status: Active
Attachments: No files
Logo Logo
Loading...