Stoploss and breakeven not working.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #106387 quote
    Manuel Aboy
    Participant
    Average

    Hello guys, I would appreciate some help as I have a strategy that works fine when I do a backtest but it fails on live market. For some reason out of my knowledge the stoploss order at breakeven doesnt work. In the attached files, picture 1 shows how the order closed once it touched the stoploss at breakeven. But, in picture 2, the long trade is still ongoing in real market. Thank you very much for your help. Regards Manuel

    DEFPARAM CumulateOrders = false // Acumulación de posiciones desactivada
    
    media=30             //average to use
    units=5              //units to buy or sell
    takeprofit = 100     //takeprofit in points
    stoploss = 100       //stoploss in points
    BreakevenAt = 25     //percent achieved of target to move stop to entry (breakeven)
    PointsToKeep = 1     //how much points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    
    // Condiciones para entrada de posiciones largas
    indicator1 = ExponentialAverage[media](close)
    indicator2 = ExponentialAverage[media*2](close)
    c1 = (indicator1 > indicator2)
    indicator3 = ExponentialAverage[media*2](close)
    c2 = (close < indicator3)
    
    IF c1 AND c2 THEN
    BUY units CONTRACT AT MARKET
    SET STOP pTRAILING stoploss
    ENDIF
    
    // Condiciones de entrada de posiciones cortas
    indicator6 = ExponentialAverage[media](close)
    indicator7 = ExponentialAverage[media*2](close)
    c4 = (indicator6 < indicator7)
    indicator8 = ExponentialAverage[media*2](close)
    c5 = (close > indicator8)
    
    IF c4 AND c5 THEN
    SELLSHORT units CONTRACT AT MARKET
    SET STOP pTRAILING stoploss
    ENDIF
    
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevel=0
    ENDIF
    
    startBreakeven =  takeprofit*(BreakevenAt/100)//how much points in gain to activate the breakeven function
    
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
    ENDIF
    
    //place the new stop orders on market at breakevenLevel
    IF LONGONMARKET AND breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    ENDIF
    
    // --- SELL SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
    ENDIF
    
    //place the new stop orders on market at breakevenLevel
    IF SHORTONMARKET AND breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    ENDIF
    
    graphonprice breakevenlevel as "Breakeven"
    2-Alemania-30-Cash-1€.itf
    #106390 quote
    Manuel Aboy
    Participant
    Average

    Sorry, forgot to attach the pictures.

    Capture-1.jpg Capture-1.jpg Capture-2.jpg Capture-2.jpg
    #106396 quote
    Vonasi
    Moderator
    Master

    What you are doing wrong is posting in the wrong forum and not giving your topic a meaningful title! 🙂

    I will move it to the ProOrder forum as it is a strategy question. I will also give it a more meaningful title than ‘What am I doing wrong?’

    #106402 quote
    Manuel Aboy
    Participant
    Average

    You are right. Sorry about that.
    The topic reflects my frustration but it doesn’t help other users to identify what the question is about.
    Thanks

    #106454 quote
    robertogozzi
    Moderator
    Master

    Unfortunately your pics don’t show the date and time when the trade entered, so I cannot find them to replicate your situation.

    #106476 quote
    Manuel Aboy
    Participant
    Average

    Entry on DAX, yesterday 03/09/19 at 10 am. See attached picture for more details.

    The black point show where the trade should have closed.

    Regards

    Manuel

    #106491 quote
    Manuel Aboy
    Participant
    Average

    Hi,

    I have been working on a coded version for a trailing stop without using SET STOP pTRAILING. Unfortunately, results are not the same.

    I would appreciate if anyone can help to code a trailing stop which that the same that my original code.

    Option 1: original code using SET STOP pTRAILING

    DEFPARAM CumulateOrders = false // Acumulación de posiciones desactivada
    
    media=30             //average to use
    units=5              //units to buy or sell
    takeprofit = 100     //takeprofit in points
    stoploss = 100       //stoploss in points
    BreakevenAt = 25     //percent achieved of target to move stop to entry (breakeven)
    PointsToKeep = 1     //how much points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    
    // Condiciones para entrada de posiciones largas
    indicator1 = ExponentialAverage[media](close)
    indicator2 = ExponentialAverage[media*2](close)
    c1 = (indicator1 > indicator2)
    indicator3 = ExponentialAverage[media*2](close)
    c2 = (close < indicator3)
    
    IF c1 AND c2 THEN
    BUY units CONTRACT AT MARKET
    SET STOP pTRAILING stoploss
    ENDIF
    
    // Condiciones de entrada de posiciones cortas
    indicator6 = ExponentialAverage[media](close)
    indicator7 = ExponentialAverage[media*2](close)
    c4 = (indicator6 < indicator7)
    indicator8 = ExponentialAverage[media*2](close)
    c5 = (close > indicator8)
    
    IF c4 AND c5 THEN
    SELLSHORT units CONTRACT AT MARKET
    SET STOP pTRAILING stoploss
    ENDIF
    
    //reset the breakevenLevel when no trade are on market
    IF NOT ONMARKET THEN
    breakevenLevel=0
    ENDIF
    
    startBreakeven =  takeprofit*(BreakevenAt/100)//how much points in gain to activate the breakeven function
    
    // --- BUY SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
    ENDIF
    
    //place the new stop orders on market at breakevenLevel
    IF LONGONMARKET AND breakevenLevel>0 THEN
    SELL AT breakevenLevel STOP
    ENDIF
    
    // --- SELL SIDE ---
    //test if the price have moved favourably of "startBreakeven" points already
    IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
    //calculate the breakevenLevel
    breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
    ENDIF
    
    //place the new stop orders on market at breakevenLevel
    IF SHORTONMARKET AND breakevenLevel>0 THEN
    EXITSHORT AT breakevenLevel STOP
    ENDIF
    
    graphonprice breakevenlevel as "Breakeven"

    Option 2: new code without using SET STOP pTRAILING

    DEFPARAM CumulateOrders = false // Acumulación de posiciones desactivada
    
    media= 30              //average to use
    units= 5               //units to buy or sell
    stoploss = 100         //initial stoploss in points
    trailingstart = 25    //trailing will start @trailinstart points profit
    trailingstep  = 100    //trailing step to move the stoploss
    pointstokeep = 5       //how much points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)
    
    // Condiciones para entrada de posiciones largas
    indicator1 = ExponentialAverage[media](close)
    indicator2 = ExponentialAverage[media*2](close)
    c1 = (indicator1 > indicator2)
    indicator3 = ExponentialAverage[media*2](close)
    c2 = (close < indicator3)
    
    IF c1 AND c2 THEN
    BUY units CONTRACT AT MARKET
    ENDIF
    
    // Condiciones de entrada de posiciones cortas
    indicator6 = ExponentialAverage[media](close)
    indicator7 = ExponentialAverage[media*2](close)
    c4 = (indicator6 < indicator7)
    indicator8 = ExponentialAverage[media*2](close)
    c5 = (close > indicator8)
    
    IF c4 AND c5 THEN
    SELLSHORT units CONTRACT AT MARKET
    ENDIF
    
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    InitialSL=0
    ENDIF
    
    //manage long positions
    IF LONGONMARKET THEN
    IF InitialSL = 0 THEN
    InitialSL = tradeprice(1) - stoploss*pipsize
    Endif
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+pointstokeep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND high-newSL>=trailingstep*pipsize THEN
    newSL = high - trailingstep*pipsize
    ENDIF
    ENDIF
    
    //manage short positions
    IF SHORTONMARKET THEN
    IF InitialSL = 0 THEN
    InitialSL = tradeprice(1) + stoploss*pipsize
    Endif
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-pointstokeep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-low>=trailingstep*pipsize THEN
    newSL = low + trailingstep*pipsize
    ENDIF
    ENDIF
    
    //stop order to exit the positions
    IF onmarket and newSL=0 then
    SELL AT InitialSL STOP
    EXITSHORT AT InitialSL STOP
    ENDIF
    
    IF onmarket and newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    
    
    Graphonprice InitialSL coloured (220,100,100)as "Initial SL"
    Graphonprice newSL as "Trailing SL"
    
    General-Test-2.itf
    #106495 quote
    robertogozzi
    Moderator
    Master

    The trade you mentioned closed as due at the breakeven level your code calculated.

    y-4.jpg y-4.jpg
    #106505 quote
    Manuel Aboy
    Participant
    Average

    In a backtest YES, but in real market it doesnt work.

    Picture 1 shows the same results than you. Trade closed at breakeven.

    Picture 2 shows how the trade is still ongoing and the stop loss at breakeven was not respected.

    Capture-1-1.jpg Capture-1-1.jpg Capture-2-1.jpg Capture-2-1.jpg
    #106547 quote
    robertogozzi
    Moderator
    Master

    I cannot replicate the live trade, but trying to figure out what the reason could have bean, the only issue could have been having a breakevenlevel price too close to the closing price when the pending stop order is placed. But it isn’t, the difference was about 14 pips and I don’t think there could have been such a high requirement at that time, unless there were some scheduled incoming news that might have increased volatility and the broker might have raised the distance requirements to a much higher value.

    I can’t think of anything else.

    I suggest that you hit Ctrl+M from the platform and send a request for assistance with the above details.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

Stoploss and breakeven not working.


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by robertogozzi
6 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/03/2019
Status: Active
Attachments: 7 files
Logo Logo
Loading...