Chiusura parziale posizione

Viewing 15 posts - 1 through 15 (of 44 total)
  • Author
    Posts
  • #174328 quote
    MauroPro
    Participant
    Veteran

    Ciao Roberto, vorrei chiudere in due tranches una strategia, la prima parte (50%) al raggiungimenti di un target di gain e la seconda a breakeven (nel caso il prezzo ritracci, altrimenti secondo le condizioni di uscita del Ts, diciamo ora per semplicità un takeprofit: set target %profit 2).

    Ho trovato negli snippet (n.286) questo codice (ma non mi è chiaro come chiudere la seconda parte della posizione a breakeven). Riusciresti ad inserire il codice in una semplice strategia da utilizzare come modello?

    Grazie

    // partial close
    once partialclose = 0
    If partialclose then
    ONCE PerCent     = 0,5                           //0.1 = 10%  positions to close
    ONCE PerCentGain = 0,005                  //0.005 = 0.5% gain
    ONCE MinLotSize  = 0.5                       //IG minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
     
    IF Not OnMarket THEN
    Flag = 1
    ENDIF
     
    IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain))  AND Flag THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    endif
    
    IF partialclose AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain))  AND Flag THEN
    exitshort CloseQuantity Contracts AT Market
    Flag = 0
    endif
    endif
    #174332 quote
    robertogozzi
    Moderator
    Master

    Le linee 3 e 24 puoi toglierle. La condizione viene già verificata alle linee 15 e 20.

    Basta che aggiungi queste righe alla fine (operano solo DOPO che la prima parte è stata chiusa):

    IF Flag = 0 THEN
       SELL      AT TradePrice STOP
       EXITSHORT AT TradePrice STOP
    ENDIF
    #174334 quote
    MauroPro
    Participant
    Veteran

    Quindi così:

    // partial close
    once partialclose = 0
    ONCE PerCent     = 0.5                           //0.1 = 10%  positions to close
    ONCE PerCentGain = 0.005                         //0.005 = 0.5% gain
    ONCE MinLotSize  = 0.5                           //IG minimum
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
     
    IF Not OnMarket THEN
    Flag = 1
    ENDIF
     
    IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain))  AND Flag THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    endif
    
    IF partialclose AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain))  AND Flag THEN
    exitshort CloseQuantity Contracts AT Market
    Flag = 0
    endif
    
    IF Flag = 0 THEN
    SELL AT TradePrice STOP
    EXITSHORT AT TradePrice STOP
    ENDIF

    Ho provato con due contratti, ma non mi chiude metà posizione (il n di operazioni è lo stesso del TS base).

    Ho un dubbio: lo snippet traduce  la percentuale (ONCE PerCent = 0,5 //0.5 = 50% positions to close) in contratti, oppure bisogna scrivere delle righe per la conversione? (in pratica PerCent 0.5 se si utilizzano 2 contratti converte automaticamente la % in 1 contratto?)

    #174336 quote
    robertogozzi
    Moderator
    Master

    Le righe 6-8 fanno questo lavoro:

    • riga 6: calcola la quantità da chiudere (0.5 equivale al 50%)
    • riga 7: calcola quant’è il restante, assicurandosi che non sia inferiore al minimo richiesto
    • riga 8: determina la quantità da chiudere

    con GRAPH e GRAPHONPRICE puoi verificare, oltre ai dati relativi ai lotti, anche se la percentuale di guadagno è stata raggiunta, o meno:

    GRAPHONPRICE (PositionPrice * (1 + PerCentGain)) AS "Profitto"
    GRAPH CloseQuantity
    GRAPH LeftQty
    GRAPH ExitQuantity
    #174357 quote
    MauroPro
    Participant
    Veteran

    Ho fatto delle prove, ma non riesco. Quando hai tempo prova ad inserire lo snippet sopra riportato in questo TS sul Dax 15 molto semplificato, sicuramente sbaglio qualcosa.

    (per avere più segnali di uscita del primo contratto “Once PerCentGain” può essere anche inferiore a 0.005)

    Grazie

    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //-----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    //--------------------------------------------------------------
    c1Sell=low<low[3] or low<low[4]
    c2Sell=myMacd<myMacdSignal
    c3Sell=myMacd[5]>myMacdSignal[5]
    c4Sell=low < mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    cShort=c1Sell and c2Sell and c3Sell  and c4Sell
    //-------------------------------------------------------------------
    IF  cLong THEN
    BUY 2 CONTRACTS AT MARKET
    ENDIF
    If cShort THEN
    SELLSHORT 2 CONTRACTS AT MARKET
    ENDIF
    //--------------------------------------------------------------
    set stop %loss 0.6
    set target %profit 1.4
    #174361 quote
    robertogozzi
    Moderator
    Master

    Che risultati ti davsnoi GRAPH?

    #174362 quote
    MauroPro
    Participant
    Veteran

    Nessuno, perchè non mi splitta la posizione. ecco perchè vorrei vedere come e dove inserisci lo snippet.

    Compare una cosa così:

    Image-002-1.jpg Image-002-1.jpg
    #174372 quote
    MauroPro
    Participant
    Veteran

    Ciao Roberto, ho riprovato ma quel codice non MI funziona.

    In compenso ho trovato un altro codice simile (presente la sola parte long) che invece funziona (è riportato sotto A).

    Ti vorrei chiedere: mi potresti correggere la parte short che per come ho provato a scriverla non mi funziona (mi splitta più volte la chiusura). La riporto in fondo (B).

    Grazie

    A

    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //-----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    //--------------------------------------------------------------
    c1Sell=low<low[3] or low<low[4]
    c2Sell=myMacd<myMacdSignal
    c3Sell=myMacd[5]>myMacdSignal[5]
    c4Sell=low < mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    cShort=c1Sell and c2Sell and c3Sell  and c4Sell
    //-------------------------------------------------------------------
    ONCE partialclose = 1
    ONCE PerCent      = 0.5             //50%  = positions to close
    ONCE PerCentGain  = 0.005            //0.5% increase
    ONCE MinLotSize   = 0.5              //0.5  lots minimum
    ONCE Increments   = 1
    //
    if partialclose and OnMarket then
    ExitQuantity   = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)
    LeftQty        = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity  = abs(CountOfPosition) - LeftQty
    else
    CloseQuantity  = 0
    Increments     = 1
    endif
    //-----------------------------------------------------------------------
    IF  cLong and not onMarket THEN
    BUY 2 CONTRACTS AT MARKET
    ENDIF
    
    IF close >= (PositionPrice * (1 + (PerCentGain * Increments))) AND LongOnMarket AND CloseQuantity > 0 THEN
    SELL CloseQuantity Contracts AT Market
    Increments = Increments + 1
    ENDIF
    
    //If cShort THEN
    //SELLSHORT 2 CONTRACTS AT MARKET
    //ENDIF
    //--------------------------------------------------------------
    set stop %loss 0.6
    set target %profit 1.4
    
    graph abs(countofposition)
    graph PerCentGain * Increments
    graph ExitQuantity

    B  ( parte short )

    ONCE partialclose = 1
    ONCE PerCent      = 0.5             //25%  = positions to close
    ONCE PerCentGain  = 0.005            //0.5% increase
    ONCE MinLotSize   = 0.5              //0.5  lots minimum
    ONCE Increments   = 1
    //
    if partialclose and OnMarket then
    ExitQuantity   = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)
    LeftQty        = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity  = abs(CountOfPosition) - LeftQty
    else
    CloseQuantity  = 0
    Increments     = 1
    endif
    //---------------------------------------------------------------------------
    If cShort THEN
    SELLSHORT 2 CONTRACTS AT MARKET
    ENDIF
    
    IF close <= (PositionPrice * (1 - (PerCentGain * Increments))) AND shortOnMarket AND CloseQuantity > 0 THEN
    exitShort CloseQuantity Contracts AT Market
    Increments = Increments + 1
    ENDIF
    #174375 quote
    MauroPro
    Participant
    Veteran

    A volte mi splitta più di due volte la chiusura di una posizione short, non sempre.

    Image-001-2.jpg Image-001-2.jpg
    #174405 quote
    robertogozzi
    Moderator
    Master

    Ho potuto verificare che non cghiudeva metà posizione perché prima di arrivare al limite entrava in Stop & Reverse (con GRAPHONPRICE si vede bene che il prezzo non arriva a quel limite). E faceva lo Stop & Reverse perché non avevi usato AND Not OnMarket nelle entrate. Se questo era voluto, allora andava bene come faceva prima.

    L’altro codice ti funziona perché Long e Short sono in due sistemi separati.

    Questo è il tuo primo codice funzionante. Ho fatto delle banali modifiche al codice delle chiusure parziali, ma solo per migliorare la visualizzazione con GRAPH, il funzionamento è lo stesso di prima:

    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //-----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    //--------------------------------------------------------------
    c1Sell=low<low[3] or low<low[4]
    c2Sell=myMacd<myMacdSignal
    c3Sell=myMacd[5]>myMacdSignal[5]
    c4Sell=low < mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    cShort=c1Sell and c2Sell and c3Sell  and c4Sell
    //-------------------------------------------------------------------
    IF  cLong AND Not OnMarket THEN
       BUY 2 CONTRACTS AT MARKET
    ENDIF
    If cShort AND Not OnMarket THEN
       SELLSHORT 2 CONTRACTS AT MARKET
    ENDIF
    //--------------------------------------------------------------
    set stop   %loss   0.6
    set target %profit 1.4
    //
    // partial close
    IF Not OnMarket THEN
       Flag = 1
    ENDIF
    once partialclose = 1
    ONCE PerCent     = 0.5                           //0.1 = 10%  positions to close
    ONCE PerCentGain = 0.005                         //0.005 = 0.5% gain
    ONCE MinLotSize  = 0.5                           //IG minimum
    IF Flag = 1 THEN
       ExitQuantity     = abs(CountOfPosition) * PerCent
       LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
       CloseQuantity    = abs(CountOfPosition) - LeftQty
    ENDIF
    
    IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain))  AND Flag THEN
       SELL CloseQuantity Contracts AT Market
       Flag = 0
    endif
     
    IF partialclose AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain))  AND Flag THEN
       exitshort CloseQuantity Contracts AT Market
       Flag = 0
    endif
     
    IF Flag = 0  THEN
       SELL      AT TradePrice STOP
       EXITSHORT AT TradePrice STOP
    ENDIF
    //
    GRAPHONPRICE (PositionPrice * (1 + PerCentGain)) coloured(0,255,0,255) AS "ProfittoL"
    GRAPHONPRICE (PositionPrice * (1 - PerCentGain)) coloured(255,0,0,255) AS "ProfittoS"
    GRAPH CloseQuantity
    GRAPH LeftQty
    GRAPH ExitQuantity         coloured(0,0,255,255)
    graph Flag                 coloured(255,0,0,255)
    graph abs(CountOfPosition) coloured(0,128,0,150)
    #174410 quote
    MauroPro
    Participant
    Veteran

    Controllo meglio domani, ma nel PRIMO CODICE che hai scritto sopra la chiusura del secondo contratto è diversa dal quella del secondo codice che invece coincide con il sistema di base (quello senza splitPosition).

    VD immagine allegata: (il alto, in marrone, c’è solo la parte long del primo codice, sotto la parte long del secondo codice ed infine la parte long del Ts senza splitPosition). – Mantengo ancora separate le parti per evitare in fase di studio problemi di  stop&reverse.

    Nel SECONDO CODICE sarebbe da controllare la parte short [vd sotto] (ossia se l’ho scritta correttamente) e poi capere perchè  a volte splitta in 3 l’uscita (a volte, ma raramente lo fa anche la parte long). Allego il TS solo short del secondo codice.

    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //-----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    //--------------------------------------------------------------
    c1Sell=low<low[3] or low<low[4]
    c2Sell=myMacd<myMacdSignal
    c3Sell=myMacd[5]>myMacdSignal[5]
    c4Sell=low < mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    cShort=c1Sell and c2Sell and c3Sell  and c4Sell
    //-------------------------------------------------------------------
    ONCE partialclose = 1
    ONCE PerCent      = 0.5              //50%  = positions to close
    ONCE PerCentGain  = 0.005            //0.5% increase
    ONCE MinLotSize   = 0.5              //0.5  lots minimum
    ONCE Increments   = 1
    //
    if partialclose and OnMarket then
    ExitQuantity   = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)
    LeftQty        = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity  = abs(CountOfPosition) - LeftQty
    else
    CloseQuantity  = 0
    Increments     = 1
    endif
    //---------------------------------------------------------------------------
    If cShort THEN
    SELLSHORT 2 CONTRACTS AT MARKET
    ENDIF
    
    IF close <= (PositionPrice * (1 - (PerCentGain * Increments))) AND shortOnMarket AND CloseQuantity > 0 THEN
    exitShort CloseQuantity Contracts AT Market
    Increments = Increments + 1
    ENDIF
    //--------------------------------------------------------------
    set stop %loss 0.6
    set target %profit 1.4
    
    graph abs(countofposition)
    graph PerCentGain * Increments
    graph ExitQuantity
    Image-001-3.jpg Image-001-3.jpg
    #174420 quote
    MauroPro
    Participant
    Veteran

    Ciao Roberto, confermo che nel PRIMO CODICE la chiusura del secondo contratto avviene molto prima della chiusura del secondo contratto del TS senza split  (è quindi sbagliata).

    Confermo anche che nel SECONDO CODICE la chiusura del secondo contratto è temporalmente uguale a quella del TS senza split, MA a volte avviene (inspiegabilmente) in 3 parti e non in 2 come dovrebbe (c’è quindi anche qui qualche errore).

    #174423 quote
    MauroPro
    Participant
    Veteran

    Ho testato solo la parte LONG – cancellando completamente la parte del codice short dei tre sistemi – per evitare ogni tipo di stop&reverse.

    #174425 quote
    MauroPro
    Participant
    Veteran
    TS BASE LONG
    
    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    //-------------------------------------------------------------------
    IF  cLong THEN
    BUY 2 CONTRACTS AT MARKET
    ENDIF
    //--------------------------------------------------------------
    set stop %loss 0.6
    set target %profit 1.4
    
    TS PRIMO CODICE SPLIT-POSITION LONG
    
    
    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    //-------------------------------------------------------------------
    IF  cLong AND Not OnMarket THEN
    BUY 2 CONTRACTS AT MARKET
    ENDIF
    //--------------------------------------------------------------
    set stop   %loss   0.6
    set target %profit 1.4
    //-------------------------------------------------------------------------------------------------------
    // partial close
    IF Not OnMarket THEN
    Flag = 1
    ENDIF
    //-----------------------------------
    once partialclose = 1
    ONCE PerCent     = 0.5                           //0.1 = 10%  positions to close
    ONCE PerCentGain = 0.005                         //0.005 = 0.5% gain
    ONCE MinLotSize  = 0.5                           //IG minimum
    IF Flag = 1 THEN
    ExitQuantity     = abs(CountOfPosition) * PerCent
    LeftQty          = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity    = abs(CountOfPosition) - LeftQty
    ENDIF
    //------------------------------------------------
    IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain))  AND Flag THEN
    SELL CloseQuantity Contracts AT Market
    Flag = 0
    endif
    //----------------------------------------------
    IF Flag = 0  THEN
    SELL      AT TradePrice STOP
    ENDIF
    //---------------------------------------------------
    GRAPHONPRICE (PositionPrice * (1 + PerCentGain)) coloured(0,255,0,255) AS "ProfittoL"
    GRAPHONPRICE (PositionPrice * (1 - PerCentGain)) coloured(255,0,0,255) AS "ProfittoS"
    GRAPH CloseQuantity
    GRAPH LeftQty
    GRAPH ExitQuantity         coloured(0,0,255,255)
    graph Flag                 coloured(255,0,0,255)
    graph abs(CountOfPosition) coloured(0,128,0,150)
    
    
    TS SECONDO CODICE SPLIT-POSITION LONG
    
    
    DEFPARAM CumulateOrders=False
    DEFPARAM Flatbefore = 010000
    DEFPARAM Flatafter = 173000
    //----------------------------------------------------------------------------------------------------------------------
    myMacd = exponentialAverage[12]-exponentialAverage[32]
    myMacdSignal = exponentialAverage[52](myMacd)
    mySuperTrend=superTrend[3,10]
    //-------------------------------------------------------------
    c1Buy=high>high[3] or high>high[4]
    c2Buy=myMacd>myMacdSignal
    c3Buy=myMacd[5]<myMacdSignal[5]
    c4Buy=high > mySuperTrend
    // ------------------------------------------------------------
    cLong=c1Buy and c2Buy and c3Buy  and c4Buy
    //-------------------------------------------------------------------
    ONCE partialclose = 1
    ONCE PerCent      = 0.5             //50%  = positions to close
    ONCE PerCentGain  = 0.005            //0.5% increase
    ONCE MinLotSize   = 0.5              //0.5  lots minimum
    ONCE Increments   = 1
    
    if partialclose and OnMarket then
    ExitQuantity   = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)
    LeftQty        = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)
    CloseQuantity  = abs(CountOfPosition) - LeftQty
    else
    CloseQuantity  = 0
    Increments     = 1
    endif
    //-----------------------------------------------------------------------
    IF  cLong and not onMarket THEN
    BUY 2 CONTRACTS AT MARKET
    ENDIF
    
    IF close >= (PositionPrice * (1 + (PerCentGain * Increments))) AND LongOnMarket AND CloseQuantity > 0 THEN
    SELL CloseQuantity Contracts AT Market
    Increments = Increments + 1
    ENDIF
    
    //--------------------------------------------------------------
    set stop %loss 0.6
    set target %profit 1.4
    
    graph abs(countofposition)
    graph PerCentGain * Increments
    graph ExitQuantity
    #174428 quote
    robertogozzi
    Moderator
    Master

    Funzionano così?

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

Chiusura parziale posizione


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
MauroPro @mauropro Participant
Summary

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

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 07/29/2021
Status: Active
Attachments: 6 files
Logo Logo
Loading...