Pregunta para robertogozzi sobre su codigo trailing stop

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #229394 quote
    gustavobp
    Participant
    New

    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:

    1. 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.
    2. 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)
    3. 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!!!

    Timeframe(10 second,UpdateOnClose)
    
    
    
    IF NOT longonmarket then
    HacerTrading = 0
    Alcista = 0
    endif
    
    //// 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] THEN
    Alcista = 1
    ENDIF
    IF close[1] < open[1] THEN
    Alcista = 0
    ENDIF
    NumeroEstrategia = 0
    
    
    //REDUNDANCIA DE TAKEPROFIT Y DE STOP LOSS POR SI HAY ALGUNA COMBINACION QUE NO SE HAGA EN LAS SIGUIENTES
    MiProfit = 0.25
    MiStop = 0.50
    
    IF NOT LONGONMARKET THEN
    IF RangoVelaAnterior1 < 0.50 THEN
    HacerTrading = 0
    ENDIF
    
    IF RangoVelaAnterior1 >= 0.50 AND RangoVelaAnterior1 <= 0.75 AND Alcista = 1 THEN
    HacerTrading = 1
    MiProfit = 0.50
    MiStop = 0.50
    ENDIF
    IF RangoVelaAnterior1 > 0.75 AND RangoVelaAnterior1 <= 1.25 AND Alcista = 1 THEN
    HacerTrading = 1
    MiProfit = 0.50
    MiStop = 0.50
    ENDIF
    IF RangoVelaAnterior1 >= 1.50 AND RangoVelaAnterior2 > 2 AND Alcista = 1 THEN
    HacerTrading = 1
    MiProfit = 0.75
    MiStop = 0.50
    ENDIF
    ENDIF
    
    ////////////////////// FIN DE CONDICIONES DE RANGO Y DEFINICION DE TAKE PROFIT Y STOPS EN BASE A RANGOS DE VELA EN LA ENTRADA   ///////////////////
    
    
    IF HacerTrading = 1 THEN
    
    IF NOT  longonmarket AND NumeroEstrategia = 0 THEN
    C1 = summation[2](close>open) < 3
    C2 = summation[2](close>open) > 1
    C3 = EMA9 > EMA20 AND EMA20 > EMA50
    C4 = 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) < low
    C11 = Average[50](Close) < open
    C12 = Average[20](Close) < low
    C13 = Average[20](Close) < open
    C14 = Average[9](Close) < low
    C15 = Average[9](Close) < open
    
    
    
    
    if 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 then
    BUY 1 CONTRACT at open LIMIT
    SET TARGET pPROFIT MiProfit
    SL = close / 100 * 2      //2% SL
    SET STOP LOSS SL
    NumeroEstrategia = 10
    ENDIF
    ENDIF
    
    ENDIF
    
    
    
    
    
    
    
    
    //
    // 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 opened
    PerCentTP     = 2.0           //2.0%  is TP
    PerCentStart  = 0.2           //0.2%  is the Trailing Stop trigger level
    PerCentStep   = 0.2           //0.2%  is each subsequent step
    PerCentSaved  = 0.1           //0.1%  is how much profit has to be saved when trailing starts and every step
    Multiplier    = 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          = 0
    ProfitPerCent = 0
    ENDIF
    IF OnMarket THEN
    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 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
    //
    //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"
    #229532 quote
    robertogozzi
    Moderator
    Master

    1. Has hecho el cálculo como porcentaje del precio:

    SL = close / 100 * 2      //2% SL
    SET STOP LOSS SL

    Cámbialo a puntos si lo prefieres:

    SL = 50    //50 puntos, por ejemplo
    SET STOP pLOSS SL

    2. 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).

    Timeframe(10 second,UpdateOnClose)
    
    DrawDownLimit = 15000
    // Cálculos de DrawDown
    //
    ONCE MaxPoint = 0
    ONCE MaxDD    = 0
    //------------------------------------------
    //       EQUITY
    Equity        = StrategyProfit
    TempProfit    = PositionPerf * PositionPrice / PipSize * PipValue
    TempEquity    = Equity + TempProfit
    //------------------------------------------
    //       DrawDown
    MaxPoint      = max(MaxPoint,TempEquity)
    DD            = MaxPoint - TempEquity
    MaxDD         = max(MaxDD,DD)
    //
    IF MaxDD >= DrawDownLimit THEN
    Quit
    ENDIF
    //
    //------------------------------------------
    
    IF NOT longonmarket then
    HacerTrading = 0
    Alcista = 0
    endif
    
    //// 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] THEN
    Alcista = 1
    ENDIF
    IF close[1] < open[1] THEN
    Alcista = 0
    ENDIF
    NumeroEstrategia = 0
    
    
    //REDUNDANCIA DE TAKEPROFIT Y DE STOP LOSS POR SI HAY ALGUNA COMBINACION QUE NO SE HAGA EN LAS SIGUIENTES
    MiProfit = 0.25
    MiStop = 0.50
    
    IF NOT LONGONMARKET THEN
    IF RangoVelaAnterior1 < 0.50 THEN
    HacerTrading = 0
    ENDIF
    
    IF RangoVelaAnterior1 >= 0.50 AND RangoVelaAnterior1 <= 0.75 AND Alcista = 1 THEN
    HacerTrading = 1
    MiProfit = 0.50
    MiStop = 0.50
    ENDIF
    IF RangoVelaAnterior1 > 0.75 AND RangoVelaAnterior1 <= 1.25 AND Alcista = 1 THEN
    HacerTrading = 1
    MiProfit = 0.50
    MiStop = 0.50
    ENDIF
    IF RangoVelaAnterior1 >= 1.50 AND RangoVelaAnterior2 > 2 AND Alcista = 1 THEN
    HacerTrading = 1
    MiProfit = 0.75
    MiStop = 0.50
    ENDIF
    ENDIF
    
    ////////////////////// FIN DE CONDICIONES DE RANGO Y DEFINICION DE TAKE PROFIT Y STOPS EN BASE A RANGOS DE VELA EN LA ENTRADA   ///////////////////
    
    IF HacerTrading = 1 THEN
    
    IF NOT  longonmarket AND NumeroEstrategia = 0 THEN
    C1 = summation[2](close>open) < 3
    C2 = summation[2](close>open) > 1
    C3 = EMA9 > EMA20 AND EMA20 > EMA50
    C4 = 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) < low
    C11 = Average[50](Close) < open
    C12 = Average[20](Close) < low
    C13 = Average[20](Close) < open
    C14 = Average[9](Close) < low
    C15 = Average[9](Close) < open
    
    if 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 then
    BUY 1 CONTRACT at open LIMIT
    SET TARGET pPROFIT MiProfit
    SL = close / 100 * 2      //2% SL
    SET STOP LOSS SL
    NumeroEstrategia = 10
    ENDIF
    ENDIF
    
    ENDIF
    
    //
    // 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 opened
    PerCentTP     = 2.0           //2.0%  is TP
    PerCentStart  = 0.2           //0.2%  is the Trailing Stop trigger level
    PerCentStep   = 0.2           //0.2%  is each subsequent step
    PerCentSaved  = 0.1           //0.1%  is how much profit has to be saved when trailing starts and every step
    Multiplier    = 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          = 0
    ProfitPerCent = 0
    ENDIF
    IF OnMarket THEN
    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 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
    //
    //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 SL
    graph MaxDD
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Pregunta para robertogozzi sobre su codigo trailing stop


ProOrder: Trading Automático y Backtesting

New Reply
Author
author-avatar
gustavobp @gustavobp Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by robertogozzi
2 years ago.

Topic Details
Forum: ProOrder: Trading Automático y Backtesting
Language: Spanish
Started: 03/07/2024
Status: Active
Attachments: No files
Logo Logo
Loading...