Strange behaviour MFE Trailing Stop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #85515 quote
    PZonda5
    Participant
    Veteran

    When playing around with a MFE Trailing Stop, I came across this very strange problem.

    On the 9th of may 2018 at 9:21 on the Germany 30 Cash (€1)(3min) a Long trade is entered for a price of 12,920.9. At 11:39 a first temporary top is reached at 12,960.7 (closing price). A difference of 39.8 points.

    In my code the MFE Trailing Stop is set to be activated after 100 points. Despite the fact that the top was not higher than 39.8 points the Long trade will be sold at 14:03.

    When I changed the parameter of the priceexit I noticed the following.

    Formula: priceexit = MAXPRICE-trailingstop*pointsize*0.63

    When the value is between 0.63 and 0.70 the problem will arise and the Long trade will be sold at 14:03.

    But when this value is lower than 0.63 or higher than 0.71, the MFE Trailing Stop will work normal again. In those instances the system will sell the Long trade on the 10th of may, after 10:00, when a top of 110.90 points is reached.

    This seems like a very strange and range specific problem and I don’t know what is causing it. It also only seems to happen at the 9th of may. On the 8th of june, for example, a similar Long trade is activated, but there don’t seem to be any problems with the MFE Trailing Stop.

    // Common Rules
    Defparam Cumulateorders = False
    Defparam Preloadbars = 1000
    
    // On/off
    Extratradecriteria   = 1 // I.e. Long; Only Enters When The Current Bar High Is Lower Then The Lowest Daily High From Today, Yesterday And Day Before.
    
    // Settings
    Positionsize = 1
    SL   = 0.88 // % Stoploss
    PT   = 1.41 // % Profit Target
    
    Nopl=13 //number of points long
    Nops=14 //number of points short
    
    // Day & Time
    Once Entertime = 090000
    Once Lasttime  = 100000
    Once Closetimefriday=173000
    
    Tt1 = Time >= Entertime
    Tt2 = Time <= Lasttime
    Tradetime = Tt1 And Tt2
    
    // [mc] Main Criteria
    If Time = Entertime Then
    Dayopen=open
    Endif
    
    If High > Dayopen+nopl Then
    Mclong=1
    Else
    Mclong=0
    Endif
    
    If Low < Dayopen-nops Then
    Mcshort=1
    Else
    Mcshort=0
    Endif
    
    // [ec] Extra Criteria
    If Extratradecriteria Then
    Min1 = Min(Dhigh(0),dhigh(1))
    Min2 = Min(Dhigh(1),dhigh(2))
    
    Max1 = Max(Dlow(0),dlow(1))
    Max2 = Max(Dlow(1),dlow(2))
    
    Eclong = High < Min(Min1,min2)
    Ecshort = Low > Max(Max1,max2)
    else
    Eclong=1
    Ecshort=1
    Endif
    
    // Long & Short Entry
    If Tradetime Then
    If Mclong and Eclong Then
    Buy Positionsize Contract At Market
    Endif
    If Mcshort and Ecshort Then
    Sellshort Positionsize Contract At Market
    Endif
    Endif
    
    //trailing stop
    trailingstop = 100
     
    //resetting variables when no trades are on market
    if not onmarket then
    MAXPRICE = 0
    priceexit = 0
    endif
     
    //case LONG order
    if longonmarket then
    MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
    if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
    priceexit = MAXPRICE-trailingstop*pointsize*0.63 //set the exit price at the MFE - trailing stop price level
    endif
    endif
     
    //exit on trailing stop price levels
    if onmarket and priceexit>0 then
    SELL AT priceexit STOP
    endif
    
    // Exit At Closetime Friday
    If Onmarket Then
    If (Currentdayofweek=5 And Time>=closetimefriday) Then
    Sell At Market
    Exitshort At Market
    Endif
    Endif
    
    //Profit & Stop Loss
    SET TARGET %PROFIT PT
    Set Stop %loss SL

    If anyone could explain the reason behind this problem (something in the code?), I would be very grateful.

    Paul thanked this post
    MFE-Trailing-Problem.png MFE-Trailing-Problem.png
    #85542 quote
    robertogozzi
    Moderator
    Master

    There are two major logical errors to correct:

    1 – as from my pic, when the trade on May 9th is opened PRICEEXIT still retains the previous value because the second previous price on May 7th was not properly closed, but automatically closed when a new one was entered on the 8th, so the status ONMARKET has always been true and lines 72-73 have NEVER been executed (my suggestion is to add a condition at line 58 to check OnMarket status:

    If Tradetime AND Not OnMarket Then

    2 – you trail stops when LONGONMARKET (lines 77-82) but not when SHORTONMARKET.

    I also suggest adding these invaluable lines (only 5 variables for real accounts, unlimited for demo ones?) to debug your code using your dearest friend GRAPH:

    graph PT
    graph SL
    graph Tradeprice
    graph close
    graph Currentdayofweek
    graph closetimefriday
    graph priceexit
    graph onmarket
    Paul and PZonda5 thanked this post
    x-18.jpg x-18.jpg
    #85549 quote
    robertogozzi
    Moderator
    Master

    The trade opened on the 8th was obviously opposite to the one opened on the 7th.

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

Strange behaviour MFE Trailing Stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
PZonda5 @pzonda5 Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by robertogozzi
7 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/22/2018
Status: Active
Attachments: 2 files
Logo Logo
Loading...