ProRealCode - Trading & Coding with ProRealTime™
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
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
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?)
Le righe 6-8 fanno questo lavoro:
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
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
Che risultati ti davsnoi GRAPH?
Nessuno, perchè non mi splitta la posizione. ecco perchè vorrei vedere come e dove inserisci lo snippet.
Compare una cosa così:
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
A volte mi splitta più di due volte la chiusura di una posizione short, non sempre.
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)
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
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).
Ho testato solo la parte LONG – cancellando completamente la parte del codice short dei tre sistemi – per evitare ogni tipo di stop&reverse.
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
Funzionano così?
Chiusura parziale posizione
This topic contains 43 replies,
has 2 voices, and was last updated by
robertogozzi
4 years, 5 months ago.
| Forum: | ProOrder: Trading Automatico & Backtesting |
| Language: | Italian |
| Started: | 07/29/2021 |
| Status: | Active |
| Attachments: | 6 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.