salve a tutti,
da qualche tempo sto studiando dei sistemi di stop and reverce, che alcune volte funzionano ed altre no…
questo è il caso del sistema in questione
ho provato diverse possibilità di scrittura ma non ne sono venuto a capo.
il sistema è multitimeframe:
TIMEFRAME (4H,UPDATEONCLOSE)
RSI14LONG= RSI[14](close)>=50
CCILONG= CCI[20](typicalPrice)>=0
MEDIA20EXPO= CLOSE CROSSES OVER ExponentialAverage[20](close)
IF RSI14LONG AND CCILONG AND MEDIA20EXPO AND REVERCE=0 THEN
BUY 1 CONTRACT AT MARKET
STOPPE=(LOWEST[2](LOW))
ENDIF
TIMEFRAME (15MN,DEFAULT)
IF LONGONMARKET AND REVERCE=0 THEN
SET TARGET %PROFIT 2
SELL AT STOPPE STOP
reverce=0
ENDIF
//trailingstop
// NewTrade is true whenever a new trade is opened, be it:
//
// - a trade when there was none previously
// - a trade that has changed direction due to a Stop & Reverse (it's Long and previously was Short and viceversa)
//
// This will be useful to reset variables to their initial values after a trade has been opened.
//
NewTrade = (OnMarket AND Not OnMarket[1]) OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)
//
// Reset variables to their initial value when a new trade shows
//
IF Not OnMarket and reverce=0 OR NewTrade and reverce=0 THEN //reset to default values when a new trade has been opened (or none is OnMarket)
PerCentTP = 1.0 //2.0% is TP
PerCentStart = x //0.2% is the Trailing Stop trigger level
PerCentStep = y //0.2% is each subsequent step
PerCentSaved = z //0.1% is how much profit has to be saved when trailing starts and every step
Multiplier = M //1.5 is how much PerCentSaved is incremented each trailing step
// 1.5 is a 50% increment, so that:
// 0.1% becomes 0.15%, then
// 0.15% becomes 0.225%, then
// 0.225% becomes 0.3375%, etc...
Distance = 6 //6 pip distance from current price (if required by the broker)
MySL = 0
ProfitPerCent = 0
ENDIF
//
// The trailing stop can operate only when OnMarket
//
IF OnMarket AND REVERCE=0 THEN
//
// before the trailing stop is triggered some calculations need to be done, accordin to settings
//
IF MySL = 0 THEN
PCent = PositionPrice * PerCentTP / 100
PStart = PositionPrice * PerCentStart / 100
PStep = PositionPrice * PerCentStep / 100
PSaved = PositionPrice * PerCentSaved / 100
ENDIF
//
// check if Trailing Stop has to be triggered
//
IF MySL = 0 THEN
IF LongOnMarket THEN
IF (close - PositionPrice) >= PStart THEN
MySL = min(close,PositionPrice + PSaved)
PSaved = PSaved * Multiplier
ENDIF
ELSIF ShortOnMarket THEN
IF (PositionPrice - close) >= PStart THEN
MySL = max(close,PositionPrice - PSaved)
PSaved = PSaved * Multiplier
ENDIF
ENDIF
ELSE
//
// check if another Step has been triggered
//
IF LongOnMarket THEN
IF (close - MySL) >= PStep THEN
MySL = min(close,MySL + PSaved)
PSaved = PSaved * Multiplier
ENDIF
ELSIF ShortOnMarket THEN
IF (MySL - close) >= PStep THEN
MySL = max(close,MySL - PSaved)
PSaved = PSaved * Multiplier
ENDIF
ENDIF
ENDIF
//
// place Pending STOP orders
//
IF MySL > 0 and reverce=0 THEN
IF (MySL = close) OR (abs(MySL - close) < Distance) THEN //exit immediately in case MySL has reached
// the current price or there's not enough DISTANCE
SELL AT MARKET
EXITSHORT AT MARKET
ELSE
SELL AT MySL STOP
EXITSHORT AT MySL STOP
ENDIF
ENDIF
ENDIF
//reverceside
IF NOT ONMARKET THEN
reverce=0
ENDIF
IF LONGONMARKET and POSITIONPERF<0 and CLOSE CROSSES UNDER DonchianChannelDown[10] THEN
SELL AT MARKET
REVERCE= 1
ENDIF
IF longonmarket[1] AND REVERCE=1 THEN
SELLSHORT 1 CONTRACT AT MARKET
STOPPEREVERCE= HIGHEST[1] (HIGH)
TARGETREVERCE= CLOSE-((HIGHEST[1](HIGH)- CLOSE))
reverce=1
ENDIF
IF SHORTONMARKET and reverce=1 THEN
EXITSHORT AT STOPPEREVERCE STOP
EXITSHORT AT TARGETREVERCE stop
ENDIF
if shortonmarket[1] or not onmarket then
reverce=0
endif
GRAPHONPRICE TARGETREVERCE
GRAPHONPRICE STOPPEREVERCE
GRAPH REVERCE
il primo ingresso è solo long, la condizione principale è sulle 4h, l’entry e il trailing stop a 15 minuti
il reverce entra in funzione nella condizione in cui il close cross under il canale di donchian a 10 periodi.
vi allego il sistema è uno screeshot del problema.
Le righe 123-128 toglile e scrivi queste al posto delle righe 119-120:
SELLSHORT 1 CONTRACT AT MARKET
STOPPEREVERCE= HIGHEST[1] (HIGH)
TARGETREVERCE= CLOSE-((HIGHEST[1](HIGH)- CLOSE))
Però c’è un altro errore logico, TARGETREVERCE e STOPPEREVERCE non possono essere piazzati entrambi come ordini pendenti dello stesso tipo, se uno è STOP l’altro dovrebbe essere LIMIT.
ciao roberto,
intanto grazie,
alcune cose che non ho capito:
1: io per indicare il trade principale e il reverce utilizzo una variabile (reverce=0/1) , serve o non serve in questo sistema in particolare o in generale?
2: quindi posso entrare in una posizione short senza chiudere una posizione long in essere? in quel caso viene eseguita subito o alla prossima barra?
3 : le righe 130-133 io ho provato tutte le combinazioni sia con ordini pendenti stop/limit; limit/stop ecc eppure mi da backtest mi salta lo stop o il target o entrambi.
non ne cavo un ragno dal buco…
1. Non serve, ma se ti serve per SL e TP degli Short, lascialo:
2. quando entri Long prima chiude eventuali operazioni Short aperte, poi apre la nuova operazione Long. L’opposto per lo Short;
3. STOP si usa quando il prezzo di uscita è peggiorativo rispetto all’attuale (ad esempio SL), mentre LIMIT si usa quando è migliorativo (ad esempio TP).
Esempio di entrata SENZA Stop & Reverse:
If Not OnMarket then
If CondizioniLong then
Buy at Market
ElsIf CondizioniShort then
SellShort at Market
Endif
Endif
Esempio di entrata CON Stop & Reverse:
If Not LongOnMarket and CondizioniLong then
Buy at Market
ElsIf Not ShortOnMarket and CondizioniShort then
SellShort at Market
Endif