salve a tutti,
grazie all’aiuto di Roberto Gozzi ho scritto una versione di trailing stop che sfrutta i canali di Donchian per seguire il prezzo.
il funzionamento è il seguente: per essere attivato il trailing stop su un trade long è necessario che sia il close che il canale di donchian inferiore sia sopra il prezzo di entrata;
successivamente lo stop segue il canale di donchian inferiore.
per la versione short il meccanismo è l’opposto.
Siccome alcune volte i canali di Donchian si avvicinano troppo al prezzo sforando al distanza minima di stop, è stata aggiunta una distanza minima per lo stop stesso, onde evitare che una strategia possa fallire.
allego il codice, sia per chi volesse usarlo o prenderne spunto
saluti a tutti
//resetting variables when no trades are on market
DONKYDOWN=DonchianChannelDown[5]
DONKYUP=DonchianChannelUp[5]
distance=6
if not onmarket then
NEWSL = 0
endif
//case SHORT order
if shortonmarket AND NEWSL=0 then
IF CLOSE<TRADEPRICE(1) AND DONKYDOWN<TRADEPRICE(1) THEN
NEWSL= DONKYUP*PIPSIZE
ENDIF
ENDIF
IF NEWSL>0 AND CLOSE<NEWSL AND DONKYUP<DONKYUP[1] THEN
NEWSL= DONKYUP*PIPSIZE
ENDIF
//case LONG order
if LONGONMARKET AND NEWSL=0 then
IF CLOSE>TRADEPRICE(1) AND DONKYDOWN>TRADEPRICE(1) THEN
NEWSL= DONKYDOWN*PIPSIZE
ENDIF
ENDIF
IF NEWSL>0 AND CLOSE>NEWSL AND DONKYDOWN>DONKYDOWN[1] THEN
NEWSL=DONKYDOWN*PIPSIZE
ENDIF
//exit on trailing stop price levels
if newSL>0 then
if longonmarket then
if (close-distance)>newSL then
sell at newsl stop
elsif(close+distance)<newsl then
sell at newsl limit
else
sell at market
endif
elsif shortonmarket then
if (close+distance)< newsl then
exitshort at newsl stop
elsif (close-distance)>newsl then
exitshort at newsl limit
else
exitshort at market
endif
endif
endif
, sia per chi volesse contribuire a migliorarlo.
Ho tolto PIPSIZE dove lo avevi messo e l’ho aggiunto alla distanza; ho anche aggiunto la verifica che il nuovo stop loss venga aggiornato solo se maggiore (se Long) o minore (se Short) del precedente:
//resetting variables when no trades are on market
DONKYDOWN = DonchianChannelDown[5]
DONKYUP = DonchianChannelUp[5]
distance = 6 * PipSize
if not onmarket then
NEWSL = 0
endif
//case SHORT order
if shortonmarket AND NEWSL=0 then
IF CLOSE<TRADEPRICE(1) AND DONKYDOWN<TRADEPRICE(1) THEN
NEWSL= DONKYUP
ENDIF
ENDIF
IF NEWSL>0 AND CLOSE<NEWSL AND DONKYUP<DONKYUP[1] THEN
NEWSL = max(NEWSL,DONKYUP)
ENDIF
//case LONG order
if LONGONMARKET AND NEWSL=0 then
IF CLOSE>TRADEPRICE(1) AND DONKYDOWN>TRADEPRICE(1) THEN
NEWSL= DONKYDOWN
ENDIF
ENDIF
IF NEWSL>0 AND CLOSE>NEWSL AND DONKYDOWN>DONKYDOWN[1] THEN
NEWSL = min(NEWSL,DONKYDOWN)
ENDIF
//exit on trailing stop price levels
if newSL>0 then
if longonmarket then
if (close-distance)>newSL then
sell at newsl stop
elsif (close+distance)<newsl then
sell at newsl limit
else
sell at market
endif
elsif shortonmarket then
if (close+distance)< newsl then
exitshort at newsl stop
elsif (close-distance)>newsl then
exitshort at newsl limit
else
exitshort at market
endif
endif
endif
Ti riepilogo l’uso di PIPZISE:
a) serve per fare in modo che certi calcoli tra pips (punti) e prezzi valgano per ogni strumento, che abbia un rapporto prezzo/pip di 1:1 (come il DAX ed altri indici), oppure 1:100 (come Aud/Jpy) o 1:10000 (come Eur/Usd), in quanto fa si che sia la piattaforma ad eseguire le dovute conversioni. Sommare 1 al prezzo del DAX, ad esempio 14000, da come risultato 14001 (rapporto 1:1), mentre lo stesso 1 aggiunto ad Eur/Usd, diciamo 1.0980, darebbe come risultato 2.0980, se lo sottrai darebbe 0.0980, prezzi inarrivabili!!!
b) differenza di prezzo / pipsize = differenza in punti/pips (es. Eur/Usd, 0.0135 diventano 135 punti) si usa per convertire un prezzo, o una differenza espressa comunque sotto forma di prezzo, in PIP/PUNTI
c) differenza di punti * pipsize = differenza in prezzo (es. Eur/Usd, 18 punti diventano 0.0018 di prezzo) si usa per convertire PIPS/PUNTI in prezzo o differenza di prezzo (ad esempio se il RANGE è di 0.0035, RANGE*PipSize lo converte in 35 Pips/Punti).