Pregunta para robertogozzi sobre su codigo trailing stop
Forums › ProRealTime foro Español › Soporte ProOrder › Pregunta para robertogozzi sobre su codigo trailing stop
- This topic has 1 reply, 2 voices, and was last updated 9 months ago by robertogozzi.
-
-
03/07/2024 at 2:40 PM #229394
Buenos dias @robertogozzi y compañeros del foro,
Vereis, he unido mi estrategia Long (en modo backtesting) y el codigo de gestion de trailing stop de Roberto Gozzi, funciona bastante bien pero tengo algunas dudas para optimizar el codigo y comprender algunas variables del mismo para poder optimizarlo por ejemplo para Mini SP500 y para Mini Nasdaq100, sabemos que ambios se mueven de manera muy diferente y cada punto no vale lo mismo.
Ayer hice un backtestinh del codigo de robertogozzi con mi estrategia de entrada y me surgen las siguientes dudas:
Instrumento utilizado: Mini sp500
Marco temporal: 10 segundos
Rango de tiempo probado dia 6-03-204 dsde las 0:41 hasta las 21:45h
Problemas que me he encontrado:
- Al abrir una operacion, el sitema automatico me abre un STOP a un valor igual al precio y no en valor de puntos cmo a mi me gustaria.
- En el backtesting de ayer hay un Drawdown de – 1.163,40 dolares, algo imposible de soportar por mi cartera ni por mi psicologia, me gustaria poder configurarle un drawDown maximo de por ejemplo 150 dolares (no se en que variable se configura esto)
- Ayer me ha abierto una operacion en la cual iba ganando 75 dolares, y el trailing stop no se ha movido, me gustaria poder definirle que cada por ejemplo 37 dolares (o 75pt) el trailing stop me vaya protegiendo la posicion por si se da vuelta o se gira el mercado al menos me voy con algo (que variable controla esto?)
Aqui os dejo el codigo, y muchas gracias nuevamente a @robertogozzi por compartir este genial codigo, espero pueda ayudarme en esta optimizacion que necesito para adaptarlo al SP500 con esta explicacion del DrwDown y el trailing stop.
Aqui va el codigo, muchas gracias!!!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180Timeframe(10 second,UpdateOnClose)IF NOT longonmarket thenHacerTrading = 0Alcista = 0endif//// VARIABLES DE SISTEMA //////EMA9 = Average[9](Close)EMA20 = Average[20](Close)EMA50 = Average[50](Close)VolumenAnterior = Volume[1]RangoVelaAnterior1 = Range[1]RangoVelaAnterior2 = Range[2]IF close[1] > open[1] THENAlcista = 1ENDIFIF close[1] < open[1] THENAlcista = 0ENDIFNumeroEstrategia = 0//REDUNDANCIA DE TAKEPROFIT Y DE STOP LOSS POR SI HAY ALGUNA COMBINACION QUE NO SE HAGA EN LAS SIGUIENTESMiProfit = 0.25MiStop = 0.50IF NOT LONGONMARKET THENIF RangoVelaAnterior1 < 0.50 THENHacerTrading = 0ENDIFIF RangoVelaAnterior1 >= 0.50 AND RangoVelaAnterior1 <= 0.75 AND Alcista = 1 THENHacerTrading = 1MiProfit = 0.50MiStop = 0.50ENDIFIF RangoVelaAnterior1 > 0.75 AND RangoVelaAnterior1 <= 1.25 AND Alcista = 1 THENHacerTrading = 1MiProfit = 0.50MiStop = 0.50ENDIFIF RangoVelaAnterior1 >= 1.50 AND RangoVelaAnterior2 > 2 AND Alcista = 1 THENHacerTrading = 1MiProfit = 0.75MiStop = 0.50ENDIFENDIF////////////////////// FIN DE CONDICIONES DE RANGO Y DEFINICION DE TAKE PROFIT Y STOPS EN BASE A RANGOS DE VELA EN LA ENTRADA ///////////////////IF HacerTrading = 1 THENIF NOT longonmarket AND NumeroEstrategia = 0 THENC1 = summation[2](close>open) < 3C2 = summation[2](close>open) > 1C3 = EMA9 > EMA20 AND EMA20 > EMA50C4 = Average[50](Close) < low[1]C5 = Average[50](Close) < open[1]C6 = Average[20](Close) < low[1]C7 = Average[20](Close) < open[1]C8 = Average[9](Close) < low[1]C9 = Average[9](Close) < open[1]C10 = Average[50](Close) < lowC11 = Average[50](Close) < openC12 = Average[20](Close) < lowC13 = Average[20](Close) < openC14 = Average[9](Close) < lowC15 = Average[9](Close) < openif C1 AND C2 AND C3 AND C4 AND C5 AND C6 AND C7 AND C8 AND C9 AND C10 AND C11 AND C12 AND C13 AND C14 AND C15 thenBUY 1 CONTRACT at open LIMITSET TARGET pPROFIT MiProfitSL = close / 100 * 2 //2% SLSET STOP LOSS SLNumeroEstrategia = 10ENDIFENDIFENDIF//// incremental Trailing Stop (based on Percentages)//NewTrade = (OnMarket AND Not OnMarket[1]) OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)//IF Not OnMarket OR NewTrade THEN //reset to default values when a new trade has been openedPerCentTP = 2.0 //2.0% is TPPerCentStart = 0.2 //0.2% is the Trailing Stop trigger levelPerCentStep = 0.2 //0.2% is each subsequent stepPerCentSaved = 0.1 //0.1% is how much profit has to be saved when trailing starts and every stepMultiplier = 1.5 //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 = 0ProfitPerCent = 0ENDIFIF OnMarket THENIF MySL = 0 THENPCent = PositionPrice * PerCentTP / 100PStart = PositionPrice * PerCentStart / 100PStep = PositionPrice * PerCentStep / 100PSaved = PositionPrice * PerCentSaved / 100ENDIF//// check if Trailing Stop has to be triggered//IF MySL = 0 THENIF LongOnMarket THENIF (close - PositionPrice) >= PStart THENMySL = min(close,PositionPrice + PSaved)PSaved = PSaved * MultiplierENDIFELSIF ShortOnMarket THENIF (PositionPrice - close) >= PStart THENMySL = max(close,PositionPrice - PSaved)PSaved = PSaved * MultiplierENDIFENDIFELSE//// check if another Step has been triggered//IF LongOnMarket THENIF (close - MySL) >= PStep THENMySL = min(close,MySL + PSaved)PSaved = PSaved * MultiplierENDIFELSIF ShortOnMarket THENIF (MySL - close) >= PStep THENMySL = max(close,MySL - PSaved)PSaved = PSaved * MultiplierENDIFENDIFENDIF//// place Pending STOP orders//IF MySL > 0 THENIF (MySL = close) OR (abs(MySL - close) < Distance) THEN //exit immediately in case MySL has reached// the current price or there's not enough DISTANCESELL AT MARKETEXITSHORT AT MARKETELSESELL AT MySL STOPEXITSHORT AT MySL STOPENDIFENDIFENDIF////graphonprice PositionPrice coloured(0,0,0,255) AS "Entry"//graphonprice MySL coloured(0,128,0,155) AS "Trailing Stop"//IF LongOnMarket THEN//graphonprice PositionPrice - SL coloured(255,0,0,255) AS "Stop Loss"//ELSE//graphonprice PositionPrice + SL coloured(255,0,0,255) AS "Stop Loss"//ENDIF//graph PCent coloured(0,0,0,255)//graph PStart coloured(0,0,255,255)//graph PStep coloured(0,128,0,155)//graph PSaved coloured(255,0,0,255)//graph positionperf * positionprice / pipsize AS "Gain"03/09/2024 at 4:05 PM #2295321. Has hecho el cálculo como porcentaje del precio:
12SL = close / 100 * 2 //2% SLSET STOP LOSS SLCámbialo a puntos si lo prefieres:
12SL = 50 //50 puntos, por ejemploSET STOP pLOSS SL2. Agregué el cálculo DrawDown. Utilice la variable MaxDD para decidir qué hacer cuando supere el límite que desea. Pongo SALIR, para que la estrategia se detenga.
Utilice GRAPH MaxDD para ver el valor que tiene.3. Puedes indicar un porcentaje diferente, en lugar del 0,2%. O necesita transformar los porcentajes y el código de trailing stop en puntos (pips).
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191Timeframe(10 second,UpdateOnClose)DrawDownLimit = 15000// Cálculos de DrawDown//ONCE MaxPoint = 0ONCE MaxDD = 0//------------------------------------------// EQUITYEquity = StrategyProfitTempProfit = PositionPerf * PositionPrice / PipSize * PipValueTempEquity = Equity + TempProfit//------------------------------------------// DrawDownMaxPoint = max(MaxPoint,TempEquity)DD = MaxPoint - TempEquityMaxDD = max(MaxDD,DD)//IF MaxDD >= DrawDownLimit THENQuitENDIF////------------------------------------------IF NOT longonmarket thenHacerTrading = 0Alcista = 0endif//// VARIABLES DE SISTEMA //////EMA9 = Average[9](Close)EMA20 = Average[20](Close)EMA50 = Average[50](Close)VolumenAnterior = Volume[1]RangoVelaAnterior1 = Range[1]RangoVelaAnterior2 = Range[2]IF close[1] > open[1] THENAlcista = 1ENDIFIF close[1] < open[1] THENAlcista = 0ENDIFNumeroEstrategia = 0//REDUNDANCIA DE TAKEPROFIT Y DE STOP LOSS POR SI HAY ALGUNA COMBINACION QUE NO SE HAGA EN LAS SIGUIENTESMiProfit = 0.25MiStop = 0.50IF NOT LONGONMARKET THENIF RangoVelaAnterior1 < 0.50 THENHacerTrading = 0ENDIFIF RangoVelaAnterior1 >= 0.50 AND RangoVelaAnterior1 <= 0.75 AND Alcista = 1 THENHacerTrading = 1MiProfit = 0.50MiStop = 0.50ENDIFIF RangoVelaAnterior1 > 0.75 AND RangoVelaAnterior1 <= 1.25 AND Alcista = 1 THENHacerTrading = 1MiProfit = 0.50MiStop = 0.50ENDIFIF RangoVelaAnterior1 >= 1.50 AND RangoVelaAnterior2 > 2 AND Alcista = 1 THENHacerTrading = 1MiProfit = 0.75MiStop = 0.50ENDIFENDIF////////////////////// FIN DE CONDICIONES DE RANGO Y DEFINICION DE TAKE PROFIT Y STOPS EN BASE A RANGOS DE VELA EN LA ENTRADA ///////////////////IF HacerTrading = 1 THENIF NOT longonmarket AND NumeroEstrategia = 0 THENC1 = summation[2](close>open) < 3C2 = summation[2](close>open) > 1C3 = EMA9 > EMA20 AND EMA20 > EMA50C4 = Average[50](Close) < low[1]C5 = Average[50](Close) < open[1]C6 = Average[20](Close) < low[1]C7 = Average[20](Close) < open[1]C8 = Average[9](Close) < low[1]C9 = Average[9](Close) < open[1]C10 = Average[50](Close) < lowC11 = Average[50](Close) < openC12 = Average[20](Close) < lowC13 = Average[20](Close) < openC14 = Average[9](Close) < lowC15 = Average[9](Close) < openif C1 AND C2 AND C3 AND C4 AND C5 AND C6 AND C7 AND C8 AND C9 AND C10 AND C11 AND C12 AND C13 AND C14 AND C15 thenBUY 1 CONTRACT at open LIMITSET TARGET pPROFIT MiProfitSL = close / 100 * 2 //2% SLSET STOP LOSS SLNumeroEstrategia = 10ENDIFENDIFENDIF//// incremental Trailing Stop (based on Percentages)//NewTrade = (OnMarket AND Not OnMarket[1]) OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)//IF Not OnMarket OR NewTrade THEN //reset to default values when a new trade has been openedPerCentTP = 2.0 //2.0% is TPPerCentStart = 0.2 //0.2% is the Trailing Stop trigger levelPerCentStep = 0.2 //0.2% is each subsequent stepPerCentSaved = 0.1 //0.1% is how much profit has to be saved when trailing starts and every stepMultiplier = 1.5 //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 = 0ProfitPerCent = 0ENDIFIF OnMarket THENIF MySL = 0 THENPCent = PositionPrice * PerCentTP / 100PStart = PositionPrice * PerCentStart / 100PStep = PositionPrice * PerCentStep / 100PSaved = PositionPrice * PerCentSaved / 100ENDIF//// check if Trailing Stop has to be triggered//IF MySL = 0 THENIF LongOnMarket THENIF (close - PositionPrice) >= PStart THENMySL = min(close,PositionPrice + PSaved)PSaved = PSaved * MultiplierENDIFELSIF ShortOnMarket THENIF (PositionPrice - close) >= PStart THENMySL = max(close,PositionPrice - PSaved)PSaved = PSaved * MultiplierENDIFENDIFELSE//// check if another Step has been triggered//IF LongOnMarket THENIF (close - MySL) >= PStep THENMySL = min(close,MySL + PSaved)PSaved = PSaved * MultiplierENDIFELSIF ShortOnMarket THENIF (MySL - close) >= PStep THENMySL = max(close,MySL - PSaved)PSaved = PSaved * MultiplierENDIFENDIFENDIF//// place Pending STOP orders//IF MySL > 0 THENIF (MySL = close) OR (abs(MySL - close) < Distance) THEN //exit immediately in case MySL has reached// the current price or there's not enough DISTANCESELL AT MARKETEXITSHORT AT MARKETELSESELL AT MySL STOPEXITSHORT AT MySL STOPENDIFENDIFENDIF////graphonprice PositionPrice coloured(0,0,0,255) AS "Entry"//graphonprice MySL coloured(0,128,0,155) AS "Trailing Stop"//IF LongOnMarket THEN//graphonprice PositionPrice - SL coloured(255,0,0,255) AS "Stop Loss"//ELSE//graphonprice PositionPrice + SL coloured(255,0,0,255) AS "Stop Loss"//ENDIF//graph PCent coloured(0,0,0,255)//graph PStart coloured(0,0,255,255)//graph PStep coloured(0,128,0,155)//graph PSaved coloured(255,0,0,255)//graph positionperf * positionprice / pipsize AS "Gain"//graph SLgraph MaxDD -
AuthorPosts
Find exclusive trading pro-tools on