Chiusura parziale posizione
Forums › ProRealTime forum Italiano › Supporto ProOrder › Chiusura parziale posizione
- This topic has 43 replies, 2 voices, and was last updated 3 years ago by robertogozzi.
-
-
07/29/2021 at 10:07 AM #174328
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
123456789101112131415161718192021222324// partial closeonce partialclose = 0If partialclose thenONCE PerCent = 0,5 //0.1 = 10% positions to closeONCE PerCentGain = 0,005 //0.005 = 0.5% gainONCE MinLotSize = 0.5 //IG minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag = 1ENDIFIF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF partialclose AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain)) AND Flag THENexitshort CloseQuantity Contracts AT MarketFlag = 0endifendif07/29/2021 at 10:27 AM #174332Le 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):
1234IF Flag = 0 THENSELL AT TradePrice STOPEXITSHORT AT TradePrice STOPENDIF07/29/2021 at 10:48 AM #174334Quindi così:
123456789101112131415161718192021222324252627// partial closeonce partialclose = 0ONCE PerCent = 0.5 //0.1 = 10% positions to closeONCE PerCentGain = 0.005 //0.005 = 0.5% gainONCE MinLotSize = 0.5 //IG minimumExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyIF Not OnMarket THENFlag = 1ENDIFIF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF partialclose AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain)) AND Flag THENexitshort CloseQuantity Contracts AT MarketFlag = 0endifIF Flag = 0 THENSELL AT TradePrice STOPEXITSHORT AT TradePrice STOPENDIFHo 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?)
07/29/2021 at 11:07 AM #174336Le 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:
1234GRAPHONPRICE (PositionPrice * (1 + PerCentGain)) AS "Profitto"GRAPH CloseQuantityGRAPH LeftQtyGRAPH ExitQuantity07/29/2021 at 9:11 PM #174357Ho 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
123456789101112131415161718192021222324252627282930DEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend//--------------------------------------------------------------c1Sell=low<low[3] or low<low[4]c2Sell=myMacd<myMacdSignalc3Sell=myMacd[5]>myMacdSignal[5]c4Sell=low < mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4BuycShort=c1Sell and c2Sell and c3Sell and c4Sell//-------------------------------------------------------------------IF cLong THENBUY 2 CONTRACTS AT MARKETENDIFIf cShort THENSELLSHORT 2 CONTRACTS AT MARKETENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.407/29/2021 at 10:03 PM #174361Che risultati ti davsnoi GRAPH?
07/29/2021 at 10:14 PM #17436207/30/2021 at 9:38 AM #174372Ciao 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455DEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend//--------------------------------------------------------------c1Sell=low<low[3] or low<low[4]c2Sell=myMacd<myMacdSignalc3Sell=myMacd[5]>myMacdSignal[5]c4Sell=low < mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4BuycShort=c1Sell and c2Sell and c3Sell and c4Sell//-------------------------------------------------------------------ONCE partialclose = 1ONCE PerCent = 0.5 //50% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumONCE Increments = 1//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0Increments = 1endif//-----------------------------------------------------------------------IF cLong and not onMarket THENBUY 2 CONTRACTS AT MARKETENDIFIF close >= (PositionPrice * (1 + (PerCentGain * Increments))) AND LongOnMarket AND CloseQuantity > 0 THENSELL CloseQuantity Contracts AT MarketIncrements = Increments + 1ENDIF//If cShort THEN//SELLSHORT 2 CONTRACTS AT MARKET//ENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.4graph abs(countofposition)graph PerCentGain * Incrementsgraph ExitQuantityB ( parte short )
1234567891011121314151617181920212223ONCE partialclose = 1ONCE PerCent = 0.5 //25% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumONCE Increments = 1//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0Increments = 1endif//---------------------------------------------------------------------------If cShort THENSELLSHORT 2 CONTRACTS AT MARKETENDIFIF close <= (PositionPrice * (1 - (PerCentGain * Increments))) AND shortOnMarket AND CloseQuantity > 0 THENexitShort CloseQuantity Contracts AT MarketIncrements = Increments + 1ENDIF07/30/2021 at 9:58 AM #17437507/30/2021 at 6:59 PM #174405Ho 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:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667DEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend//--------------------------------------------------------------c1Sell=low<low[3] or low<low[4]c2Sell=myMacd<myMacdSignalc3Sell=myMacd[5]>myMacdSignal[5]c4Sell=low < mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4BuycShort=c1Sell and c2Sell and c3Sell and c4Sell//-------------------------------------------------------------------IF cLong AND Not OnMarket THENBUY 2 CONTRACTS AT MARKETENDIFIf cShort AND Not OnMarket THENSELLSHORT 2 CONTRACTS AT MARKETENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.4//// partial closeIF Not OnMarket THENFlag = 1ENDIFonce partialclose = 1ONCE PerCent = 0.5 //0.1 = 10% positions to closeONCE PerCentGain = 0.005 //0.005 = 0.5% gainONCE MinLotSize = 0.5 //IG minimumIF Flag = 1 THENExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyENDIFIF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endifIF partialclose AND ShortOnMarket and close <= (PositionPrice * (1 - PerCentGain)) AND Flag THENexitshort CloseQuantity Contracts AT MarketFlag = 0endifIF Flag = 0 THENSELL AT TradePrice STOPEXITSHORT AT TradePrice STOPENDIF//GRAPHONPRICE (PositionPrice * (1 + PerCentGain)) coloured(0,255,0,255) AS "ProfittoL"GRAPHONPRICE (PositionPrice * (1 - PerCentGain)) coloured(255,0,0,255) AS "ProfittoS"GRAPH CloseQuantityGRAPH LeftQtyGRAPH ExitQuantity coloured(0,0,255,255)graph Flag coloured(255,0,0,255)graph abs(CountOfPosition) coloured(0,128,0,150)07/30/2021 at 9:46 PM #174410Controllo 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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051DEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend//--------------------------------------------------------------c1Sell=low<low[3] or low<low[4]c2Sell=myMacd<myMacdSignalc3Sell=myMacd[5]>myMacdSignal[5]c4Sell=low < mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4BuycShort=c1Sell and c2Sell and c3Sell and c4Sell//-------------------------------------------------------------------ONCE partialclose = 1ONCE PerCent = 0.5 //50% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumONCE Increments = 1//if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0Increments = 1endif//---------------------------------------------------------------------------If cShort THENSELLSHORT 2 CONTRACTS AT MARKETENDIFIF close <= (PositionPrice * (1 - (PerCentGain * Increments))) AND shortOnMarket AND CloseQuantity > 0 THENexitShort CloseQuantity Contracts AT MarketIncrements = Increments + 1ENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.4graph abs(countofposition)graph PerCentGain * Incrementsgraph ExitQuantity07/31/2021 at 9:10 AM #174420Ciao 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).
07/31/2021 at 9:14 AM #17442307/31/2021 at 9:28 AM #174425123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131TS BASE LONGDEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4Buy//-------------------------------------------------------------------IF cLong THENBUY 2 CONTRACTS AT MARKETENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.4TS PRIMO CODICE SPLIT-POSITION LONGDEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4Buy//-------------------------------------------------------------------IF cLong AND Not OnMarket THENBUY 2 CONTRACTS AT MARKETENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.4//-------------------------------------------------------------------------------------------------------// partial closeIF Not OnMarket THENFlag = 1ENDIF//-----------------------------------once partialclose = 1ONCE PerCent = 0.5 //0.1 = 10% positions to closeONCE PerCentGain = 0.005 //0.005 = 0.5% gainONCE MinLotSize = 0.5 //IG minimumIF Flag = 1 THENExitQuantity = abs(CountOfPosition) * PerCentLeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyENDIF//------------------------------------------------IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THENSELL CloseQuantity Contracts AT MarketFlag = 0endif//----------------------------------------------IF Flag = 0 THENSELL AT TradePrice STOPENDIF//---------------------------------------------------GRAPHONPRICE (PositionPrice * (1 + PerCentGain)) coloured(0,255,0,255) AS "ProfittoL"GRAPHONPRICE (PositionPrice * (1 - PerCentGain)) coloured(255,0,0,255) AS "ProfittoS"GRAPH CloseQuantityGRAPH LeftQtyGRAPH 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 LONGDEFPARAM CumulateOrders=FalseDEFPARAM Flatbefore = 010000DEFPARAM 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>myMacdSignalc3Buy=myMacd[5]<myMacdSignal[5]c4Buy=high > mySuperTrend// ------------------------------------------------------------cLong=c1Buy and c2Buy and c3Buy and c4Buy//-------------------------------------------------------------------ONCE partialclose = 1ONCE PerCent = 0.5 //50% = positions to closeONCE PerCentGain = 0.005 //0.5% increaseONCE MinLotSize = 0.5 //0.5 lots minimumONCE Increments = 1if partialclose and OnMarket thenExitQuantity = max(ExitQuantity[1],abs(CountOfPosition) * PerCent)LeftQty = max(MinLotSize,abs(CountOfPosition) - ExitQuantity)CloseQuantity = abs(CountOfPosition) - LeftQtyelseCloseQuantity = 0Increments = 1endif//-----------------------------------------------------------------------IF cLong and not onMarket THENBUY 2 CONTRACTS AT MARKETENDIFIF close >= (PositionPrice * (1 + (PerCentGain * Increments))) AND LongOnMarket AND CloseQuantity > 0 THENSELL CloseQuantity Contracts AT MarketIncrements = Increments + 1ENDIF//--------------------------------------------------------------set stop %loss 0.6set target %profit 1.4graph abs(countofposition)graph PerCentGain * Incrementsgraph ExitQuantity07/31/2021 at 9:36 AM #174428Funzionano così?
-
AuthorPosts
Find exclusive trading pro-tools on