Chiusura posizioni con ordini STOP

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #199230 quote
    Matteo MC
    Participant
    New

    Buongiorno a tutti.

    So che questo argomento è stato trattato più volte ma non sono riuscito a trovare una spiegazione al mio problema.

    Data una posizione long vorrei che questa si chiudesse qualora il prezzo raggiunga il minimo della barra precedente a quella di ingresso. Viceversa per una posizione short vorrei imporre una chiusura se il prezzo tocca il massimo della barra antecedente a quella di ingresso. Ovviamente si tratta di chiusure di “protezione”.

    Per alcuni codici che ho scritto, e che al di là dei risultati, funzionano come dovrebbero, in backtest e in reale, ho usato ordini di uscita come quelli visibili in questo codice:

    //USCITA A
    // Posizioni cumulate disattivate
    DEFPARAM CumulateOrders = False
    
    //CONDIZIONE INGRESSO TORO -----------------------------------------------
    if CLOSE[0]>CLOSE[1] AND CLOSE[1]>CLOSE[2] then
    C1=1
    else
    C1=2
    endif
    //CONDIZIONE INGRESSO ORSO -----------------------------------------------
    if CLOSE[0]<CLOSE[1] AND CLOSE[1]<CLOSE[2] then
    b1=1
    else
    b1=2
    endif
    //Ingresso Toro -------------------------------------------------------------------
    IF C1=1 and shortonmarket=0 THEN
    buy 1 shares at market
    endif
    //Ingresso Orso -------------------------------------------------------------------
    IF b1=1 and longonmarket=0 THEN
    sellshort 1 shares at market
    endif
    //------------------------------------------USCITA----------------------------------------------
    //Uscita Toro
    if longonmarket then
    sell at tradeprice*0.995 stop
    endif
    //Uscita orso
    if shortonmarket then
    exitshort at tradeprice*1.005 Stop
    endif
    
    SET STOP %LOSS 1
    SET TARGET %PROFIT 1

    Ovviamente queste uscite sono le stesse che otterrei con un banale Stop loss, ma le uso per creare degli step di protezione del profitto, moltiplicando il tradeprice per numeri maggiori di uno per posizioni long, o minori di uno per posizioni short.

    Questo codice invece, che è quello descritto all’inizio, non si comporta come dovrebbe, la sola differenza è sulle uscite. Le posizioni long ignorano l’uscita e le short si chiudono praticamente subito.

    //USCITA B
    // Posizioni cumulate disattivate
    DEFPARAM CumulateOrders = False
    //CONDIZIONE INGRESSO TORO -----------------------------------------------
    if CLOSE[0]>CLOSE[1] AND CLOSE[1]>CLOSE[2] then
    C1=1
    else
    C1=2
    endif
    //CONDIZIONE INGRESSO ORSO -----------------------------------------------
    if CLOSE[0]<CLOSE[1] AND CLOSE[1]<CLOSE[2] then
    b1=1
    else
    b1=2
    endif
    //Ingresso Toro -------------------------------------------------------------------
    IF C1=1 and shortonmarket=0 THEN
    buy 1 shares at market
    endif
    //Ingresso Orso -------------------------------------------------------------------
    IF b1=1 and longonmarket=0 THEN
    sellshort 1 shares at market
    endif
    //------------------------------------------USCITA----------------------------------------------
    //Uscita Toro
    if longonmarket then
    sell at low[tradeindex-1] stop
    endif
    //Uscita orso
    if shortonmarket then
    exitshort at high[tradeindex+1] Stop
    endif
    
    SET STOP %LOSS 1
    SET TARGET %PROFIT 1

    Qualcuno puoi indicarmi dove sbaglio? Non capisco perchè indicando il prezzo di uscita nel primo modo non ci siano problemi, mentre nel secondo non funzioni. Ho fatto vari test, anche regolando i valori del primo modo facendo si che per un dato ordine l’uscita corrisponda a quanto previsto dal secondo, e il primo continua a funzionare.

    L’ho testato sul grafico orario di “France 40 Cash 1E”

    Grazie mille a tutti.

    #199231 quote
    robertogozzi
    Moderator
    Master

    TradeIndex è il valore di BarIndex all’ingresso, quindi può essere un numero che può essere 187 o 2045, per cui utilizzandolo come indice di un prezzo va a prendere il prezzo di 2000 barre prima!

    Va usata l’espressione (barindex – tradeindex)+ 1:

    //Uscita Toro
    if longonmarket then
       sell at low[(barindex - tradeindex)+ 1] stop
    endif
    //Uscita orso
    if shortonmarket then
       exitshort at high[(barindex - tradeindex)+ 1] Stop
    endif
    #199232 quote
    Matteo MC
    Participant
    New

    Lo sapevo…non ci devo lavorare di notte a queste cose…

    Che figuraccia…

    Grazie mille Roberto!

    #199249 quote
    robertogozzi
    Moderator
    Master

    Non ci sono figuracce, capita a tutti di sbagliare, nonostante l’esperienza che si può avere!

    GraHal ha un bel motto nel suo profilo “Ask a question, you may be a fool for a day. Don’t ask a question, you may be a fool for life!?“.

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

Chiusura posizioni con ordini STOP


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
Matteo MC @mrwayne Participant
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: 08/19/2022
Status: Active
Attachments: No files
Logo Logo
Loading...