Codice rientro in posizione

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #187433 quote
    MauroPro
    Participant
    Veteran

    Ciao Roberto, seguendo tue indicazioni di tempo fa ho costruito un codice per rientrare a mercato dopo che un TS è uscito (lo posto con le note nel caso possano servire ad altri utenti).

    Ti volevo chiedere due cose, intanto la prima: se puoi controllare se formalmente è corretto, mi sembra di si ma è sempre meglio un tuo controllo  (testato su un TS di prova sul cfd del S&P 500 (1 euro a punto).

    //PROVA CODICE RIENTRO  [ S&P 500 - H1] 
    
    defparam cumulateorders = false
    positionSize =1 
    avg100 = average[100](close)
    //------------------------------------------------------------------------------ //ENTRATA1:  CROSS INIZIALE
    if close crosses over avg100 then                    
    buy positionSize contracts at market
    set stop %loss 0.7
    set target %profit 2
    endif
    //------------------------------------------------------------------------------ //ENTRATA 2 : RIENTRO IN POSIZIONE
    if strategyProfit <> strategyProfit[1] then        //NOTA 1                                                  
    closeIndex = barIndex[1]
    endif
    nBars = 4
    if not onMarket and (barIndex - closeIndex) > nBars and close > tradePrice(2)  and close > avg100 then  //NOTA 2
    buy positionSize contract at market
    set stop %loss 0.7
    set target %profit 1.6
    endif
    
    
    //**************************************************
    //NOTA 1: Non c’è una costante specifica come TradeIndex, però puoi fartela facilmente.
    //Nell’esempio la chiamerò CloseIndex e prenderà il valore dell’ultima barra di chiusura (che un trade è chiuso si vede da quando il risultato della strategia viene aggiornato, è la barra precedente):
    //if strategyProfit <> strategyProfit[1] then
    //closeIndex=barIndex[1]
    //*******************************************
    //NOTA 2: Tieni presente che TRADEPRICE non è il prezzo di entrata, ma semplicemente l’ultimo prezzo tradato, quindi quando non sei a mercato TRADEPRICE o TRADEPRICE(1) è il prezzo di uscita dell’ultima posizione e quello d’entrata è TRADEPRICE(2)
    
    //****************************************************
    #187442 quote
    robertogozzi
    Moderator
    Master

    Si, io ho aggiunto alla fine queste righe:

    graph closeIndex
    graph (barIndex - closeIndex)
    graphonprice tradeprice(2)

    e mi sembra andasse tutto bene (ho controllato solo 3-4 operazioni, in modo sparso).

    #187443 quote
    MauroPro
    Participant
    Veteran

    Grazie Roberto, la seconda cosa: non riesco ad inserire correttamente lo split della posizione (codice verificato) nel TS  in modo tale che valga SOLTANTO per l’entrata1 (cross iniziale).

    [In pratica riesco a distinguere per le due entrate SL e TP, ma non lo split]

    Puoi inserire nel codice sopra riportato il codice dello split (riportato sotto)?

    once partialcloseGain = 1
    If partialcloseGain then
    ONCE PerCent = 0.5
    ONCE PerCentGain = 0.02 //0.02= 2%
    ONCE MinLotSize = 0.5
    ExitQuantity = abs(CountOfPosition) * PerCent
    LeftQty = max(MinLotSize,abs(CountOfPosition) – ExitQuantity)
    CloseQuantity = abs(CountOfPosition) – LeftQty
    IF Not OnMarket THEN
    Flag = 1
    ENDIF
    IF partialcloseGain AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    endif
    endif

    #187455 quote
    robertogozzi
    Moderator
    Master

    Ho aggiunto unaverifica che CloseQuantity non sia mai < 0.
    Ho anaggiunto la variabile Codice per fare in modo che riconosca quando è la prima entrata, all’incrocio:

    //PROVA CODICE RIENTRO  [ S&P 500 - H1]
     
    defparam cumulateorders = false
    once Codice = 0
    positionSize = 1
    avg100 = average[100](close)
    //------------------------------------------------------------------------------ //ENTRATA1:  CROSS INIZIALE
    if close crosses over avg100 then
    Codice = 0
    buy positionSize contracts at market
    set stop %loss 0.7
    set target %profit 2
    endif
    //------------------------------------------------------------------------------ //ENTRATA 2 : RIENTRO IN POSIZIONE
    if strategyProfit <> strategyProfit[1] then        //NOTA 1
    closeIndex = barIndex[1]
    endif
    nBars = 4
    if not onMarket and (barIndex - closeIndex) > nBars and close > tradePrice(2)  and close > avg100 AND Codice = 0 then  //NOTA 2
    Codice = 1
    buy positionSize contract at market
    set stop %loss 0.7
    set target %profit 1.6
    endif
    //
    once partialcloseGain = 1
    If partialcloseGain then
    ONCE PerCent = 0.5
    ONCE PerCentGain = 0.002 //0.02= 2%
    ONCE MinLotSize = 0.5
    ExitQuantity = abs(CountOfPosition) * PerCent
    LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity = max(0,abs(CountOfPosition) - LeftQty)
    IF Not OnMarket THEN
    Flag = 1
    ENDIF
    IF partialcloseGain AND LongOnMarket and close >= (PositionPrice *  (1 + PerCentGain)) AND Flag=1 AND Codice = 0 and CloseQuantity > 0 THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    endif
    endif
    //**************************************************
    //NOTA 1: Non c’è una costante specifica come TradeIndex, però puoi fartela facilmente.
    //Nell’esempio la chiamerò CloseIndex e prenderà il valore dell’ultima barra di chiusura (che un trade è chiuso si vede da quando il risultato della strategia viene aggiornato, è la barra precedente):
    //if strategyProfit <> strategyProfit[1] then
    //closeIndex=barIndex[1]
    //*******************************************
    //NOTA 2: Tieni presente che TRADEPRICE non è il prezzo di entrata, ma semplicemente l’ultimo prezzo tradato, quindi quando non sei a mercato TRADEPRICE o TRADEPRICE(1) è il prezzo di uscita dell’ultima posizione e quello d’entrata è TRADEPRICE(2)
     
    //****************************************************
    MauroPro thanked this post
    #187459 quote
    MauroPro
    Participant
    Veteran

    Grazie Roberto, faccio un pò di prove e ti faccio sapere.

    Una cosa al volo: se volessi utilizzare lo split per entrambe le entrate (come fa lo split iniziale che avevo riportato senza il tuo flag “Codice”) devo scrivere alla riga 37:

    AND (Flag=1 or Flag=0) ?

    #187468 quote
    robertogozzi
    Moderator
    Master

    No, devi solo togliere CODICE, oppure se vuoi mantenere quella variabile, assegnargli 0 alla riga 20.

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

Codice rientro in posizione


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
MauroPro @mauropro Participant
Summary

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

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