how to code MFE exit

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #156597 quote
    nonetheless
    Participant
    Master

    I’ve been working on a long-only scalping algo and noticed that in all cases where  MFE = 0 then that trade went all the way to the stoploss.

    So I’m thinking there may be some optimal number of bars (b) after which, if the trade has never been in profit, it may be better to close – something like this:

    IF longonmarket and barindex-tradeindex > b and (MFE = 0) then
    sell at market
    endif

    but how do I code the MFE part ?

    #156626 quote
    Monobrow
    Participant
    Senior
    if longonmarket then
    graph entryprice
    ppc=((close-entryprice)/100) //custom
    if ppc<-0.2 then
    sell at market
    endif
    endif
    if shortonmarket then
    graph entryprice
    ppc=((entryprice-close)/100) //custom
    if ppc<-0.2 then
    exitshort at market
    endif
    endif

    I found this in another thread from “Paul” – might be what you are looking for? Or may give you an idea.

    nonetheless thanked this post
    #156635 quote
    nonetheless
    Participant
    Master

    I found this in another thread from “Paul”

    thanks for that, but it doesn’t look like what I’m after. I’m thinking something more like

    if not onmarket then
     MAXPRICE = 0
     endif
    
    IF longonmarket then
     MAXPRICE = MAX(MAXPRICE,close)
     if barindex-tradeindex > b and MAXPRICE = 0 then
    sell at market
    endif
    endif

    but I don’t think that’s right either … ???

    #156657 quote
    Roger
    Participant
    Veteran

    MFE = max favourable excursion right ?

    In this case you could try that if you want to exit at 50% of the MFE :

    (not tested)

     

    level=0.5
    
    IF longonmarket then
    
    MAXPRICE = MAX(MAXPRICE,close)
    mfe=MAX-positionprice
    trigger=positionprice+mfe*level
    
    if barindex-tradeindex > b and mfe > 0 and close<trigger then
    sell at market
    endif
    
    endif
    Florian, nonetheless and Midlanddave thanked this post
    #156660 quote
    Dow Jones
    Participant
    Veteran

    Not tested

    MFE = summation[barindex-tradeindex](positionperf > 0)
    IF longonmarket and barindex-tradeindex > b and (MFE = 0) then
    sell at market
    endif
    nonetheless, Monobrow and Midlanddave thanked this post
    #156668 quote
    nonetheless
    Participant
    Master

    Thanks @roger, that is a useful piece of code but obviously will only work with positions that are in profit. What I am trying to address are long positions that open but fall straight away, never go into profit so that at close the MFE = 0

    @Dow Jones, that is exactly what I was looking for, thanks very much. The code works as expected and gives a small advantage, so it seems the original idea was a good one 😁😁😁

    Must try it on other algos.

    #157469 quote
    Monobrow
    Participant
    Senior

    Hi All

     

    I added this bit of code to an otherwise working Algo last night (which has been running for some time) and it quit the system around 3 bars after the MFE failed to go positive when opening a trade. I didnt keep the error (bah, I will next time) but it was along the lines of “quit due to a negative number”.

     

    MFE = summation[barindex-tradeindex](positionperf > 0)
    IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) then
    Buy 5 perpoint at market
    endif

     

    any ideas? thanks!

    #157478 quote
    nonetheless
    Participant
    Master

    I’m not clear on what you want to achieve here. You want to exit a short position if after 24 bars the MFE is between 0 and 6 ?

    #157479 quote
    Monobrow
    Participant
    Senior

    Not exactly… the algo will have recently opened a short position and if, within 24 bars the MFE is not over 6, i want to reverse that position and go long,

     

    it worked very well in backtest, honest guv.

    #157480 quote
    PeterSt
    Participant
    Master
    IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) then

    I will bet your spread that it is not allowed (or unpredictable) to have both conditions on one line. Usually, a compiler/interpreter deals with the rightmost part first, working itself towards the left. Thus make it this :

    IF shortonmarket then 
      IF barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) then
        ...
      ENDIF
    ENDIF

    and it will always be fine regarding this aspect.

    So notice that with all on one line, TradeIndex will be undefined (could be -1) when (Short)OnMarket is not true.

    Monobrow, nonetheless and Midlanddave thanked this post
    #157486 quote
    Monobrow
    Participant
    Senior

    Thank you so much Peter. Im still new to coding, so this info is very much appreciated 👍

    #157596 quote
    Vonasi
    Moderator
    Master
    IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) then

    The above is perfectly fine and the code between THEN and ENDIF will be carried out if all four conditions separated by AND are true. It is exactly the same as:

    if shortonmarket then
    if barindex-tradeindex > 24 then
    if MFE => 0 then
    if MFE <= 6 then
    .
    .
    .
    endif
    endif
    endif
    endif
    #157600 quote
    Monobrow
    Participant
    Senior

    The above is perfectly fine

    Thanks for the info Vonasi. And yes, the code works perfectly in backtest. But it did give me a “negative error” in live today.

     

    anyway… based on what you just said I took a closer look at the trade and at the exit time, the MFE was Zero and the position perf negative, so maybe it didnt like this.

    so… ive added a MAX to the code which doesnt appear to impact performance and will hopefully get rid of the error.

    you guys will make a coder of me yet :0

     

    MFE = MAX(1,summation[barindex-tradeindex](positionperf > 0))
    IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) then
    Buy 5 perpoint at market
    endif
    
    #157619 quote
    Monobrow
    Participant
    Senior

    different day, same error 🙁

    works perfectly in backtest, I managed to capture the message today. this code is the only addition to the one that works on live and the one that doesnt.. please help…

     

    MFE = MAX(1,summation[barindex-tradeindex](positionperf > 0))
    IF shortonmarket and barindex-tradeindex > 24 and (MFE <= 6) then
    Buy 5 perpoint at market
    endif
    negative-code.jpg negative-code.jpg
    #157621 quote
    nonetheless
    Participant
    Master

    instead of a reversal, maybe try separating it into 2 instructions, first exit the short, then enter long ??

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

how to code MFE exit


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 18 replies,
has 6 voices, and was last updated by Monobrow
5 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/06/2021
Status: Active
Attachments: 2 files
Logo Logo
Loading...