Need help with MFE stop loss

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #127673 quote
    jebus89
    Participant
    Master

    Hi all, i have previously had a word document with a bunch of algostuff written down, my computer with that document however has crashed and for some reason, the only thing i didnt get out from it, was the following thing:

    I have previously asked for help with this: https://www.prorealcode.com/blog/learning/trailing-stop-max-favorable-excursion-mfe/

    Im not sure if it has been requested before but i could not find it, using the search bar, altough i know i have asked about this before (im pretty sure at least). Sorry for asking about it again now maybe 1-2 years later 🙂 Would appreciate the help as i dont think its too hard to do, i just want it done right.

    I have earlier recieved help to modify the MFE trailing stop in the link so that it would Start after XX pips in profit. This is the code:

    
    //trailing stop
    trailingstop = 80 // 65
    StartTrailingStopValue = 30 // 20
    
    //resetting variables when no trades are on market
    if not onmarket then
    MAXPRICE = 0
    MINPRICE = close
    priceexit = 0
    endif
    
    //case SHORT order
    if shortonmarket then
    MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade
    if tradeprice(1)-close > StartTrailingStopValue*pointsize then
    if tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
    priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level
    endif
    endif
    endif
    
    //case LONG order
    if longonmarket then
    MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
    if close-tradeprice(1) > StartTrailingStopValue*pointsize then
    if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
    priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
    endif
    endif
    endif
    
    //exit on trailing stop price levels
    if onmarket and priceexit>0 then
    EXITSHORT AT priceexit STOP
    SELL AT priceexit STOP
    endif
    

    However what i need help with is to evolve this MFE trailing stop further, i hope my example is easy to understand.

    Lets say the Target = 100 pips.

    If the trade is currently 50 pips in profit: Start a trailing stop loss of 25 pips.

    But if the trade moves up and is now 85 pips in profit: Start a trailing stop loss of 10 pips.

     

    So in other words: If the trade moves very close to the target, i want to change the stop loss so it gets tighter. I had one code with 3 levels in total, but i think only 2 is needed, at least for my use 🙂

    Any help is very appreciated.

    #127696 quote
    Vonasi
    Moderator
    Master

    Nothing to do with your question but at a glance I think the MFE is calculated wrong. When long the MFE is not the close but the high and when short it is not the close but the low. So it should be:

    if shortonmarket then
    MINPRICE = MIN(MINPRICE,low) //saving the MFE of the current trade
    
    if longonmarket then
    MAXPRICE = MAX(MAXPRICE,high) //saving the MFE of the current trade
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Need help with MFE stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
jebus89 @jebus89 Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by Vonasi
5 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/23/2020
Status: Active
Attachments: No files
Logo Logo
Loading...