Buongiorno a tutti, ho postato da poco un sistema sul CAC40 (https://www.prorealcode.com/prorealtime-trading-strategies/box-7-oclock-cac40-intraday/) e mi è stato evidenziato da un membro della community che il trailing stop non funziona.
Chiedo cortesemente se potete aiutarmi nel correggere il problema:
in pratica la posizione, se in profitto, dovrebbe aggiornarsi ogni 10 pips per poi incrementare ogni 6 pips, ma questo non avviene mai.
Allego codice versione Long:
DEFPARAM CumulateOrders = False
DEFPARAM FLATAFTER = 220000
Timeframe (1 Hour, Updateonclose)
//Once $MAXI[0] = High
//Once $mini[0] = Low
Once COMPRA = High
//Timeframe (1 Hour, Updateonclose)
Timeframe (1 Hour, Updateonclose)
IF Time = 080000 AND not OnMarket THEN
COMPRA = Highest[7](high[1]) + 5*pipsize
BUY 1 CONTRACT AT COMPRA STOP
ENDIF
//ENDIF
Timeframe (30 minutes, Updateonclose)
SET STOP pLOSS 70
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trailing Stop
//------------------------------------------------------------------------------------
IF Not OnMarket THEN
TrailStart = 10 //10 Start trailing profits from this point
BasePerCent = 0.100 //10.0% Profit to keep
StepSize = 6 //6 Pips chunks to increase Percentage
PerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunk
RoundTO = -0.5 //-0.5 rounds to Lower integer, +0.4 rounds to Higher integer
PriceDistance = 7 * pipsize//8.9 minimun distance from current price
y1 = 0
y2 = 0
ProfitPerCent = BasePerCent
ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN //LONG
x1 = (close - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN //go ahead only if N+ pips
Diff1 = abs(TrailStart - x1)
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO))
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit
ENDIF
ELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN//SHORT
x2 = (tradeprice - close) / pipsize //convert price to pips
IF x2 >= TrailStart THEN //go ahead only if N+ pips
Diff2 = abs(TrailStart - x2)
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO))
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc))
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))
y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit
ENDIF
ENDIF
IF y1 THEN //Place pending STOP order when y>0
SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price
IF abs(close - SellPrice) > PriceDistance THEN
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
SELL AT Market
ENDIF
ENDIF
IF y2 THEN //Place pending STOP order when y>0
ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price
IF abs(close - ExitPrice) > PriceDistance THEN
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
SET STOP pLOSS 70
Grazie e buona giornata.
Da prove in demo il codice non aggiorna mai la posizione anche quando il profitto supera abbondantemente i 10 pips (target minimo di “azionamento” del trailing stop) e non è una questione di timeframe (in questo esempio 30 minuti).
Il codice di trailing l’ho copiato dal forum ma probabilmente l’ho modificato erroneamente: forse tu riesci a vedere subito un eventuale errore.
Le righe dalla nr. 23 alla nr. 31 probabilmente sono quelle da valutare.
Grazie
E’ solo un problema di parametri, sono troppo stretti per funzionare, per le righe 24-26 usa questi:
TrailStart = 30 //10 Start trailing profits from this point
BasePerCent = 0.100 //10.0% Profit to keep
StepSize = 10 //6 Pips chunks to increase Percentage
Lo stop loss indicalo solo una volta, alla riga 19, con questo valore:
SET STOP pLOSS 270
infine aggiungi queste quattro righe alla fine del codice, in modo da vedere i prezzi ed il profitto/perdita in corso, candela per candela:
graphonprice TradePrice AS "Entrata"
graphonprice SellPrice coloured("Green") AS "Trailing Stop LONG"
graphonprice ExitPrice coloured("Red") AS "Trailing Stop SHORT"
graph PositionPerf * TradePrice / PipSize AS "Pips di guadagno/perdita"
In realtà funziona anche con i tuoi parametri originali, ma esce quasi sempre subito, perché appena arriva a 10, ne mette in salvo 6 e basta che ritracci 4 pip che esce!