how to code MFE exit
Forums › ProRealTime English forum › ProOrder support › how to code MFE exit
- This topic has 18 replies, 6 voices, and was last updated 3 years ago by Monobrow.
-
-
01/06/2021 at 2:56 PM #156597
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:
123IF longonmarket and barindex-tradeindex > b and (MFE = 0) thensell at marketendifbut how do I code the MFE part ?
01/06/2021 at 4:33 PM #1566261234567891011121314if longonmarket thengraph entrypriceppc=((close-entryprice)/100) //customif ppc<-0.2 thensell at marketendifendifif shortonmarket thengraph entrypriceppc=((entryprice-close)/100) //customif ppc<-0.2 thenexitshort at marketendifendifI found this in another thread from “Paul” – might be what you are looking for? Or may give you an idea.
1 user thanked author for this post.
01/06/2021 at 5:09 PM #156635I 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
12345678910if not onmarket thenMAXPRICE = 0endifIF longonmarket thenMAXPRICE = MAX(MAXPRICE,close)if barindex-tradeindex > b and MAXPRICE = 0 thensell at marketendifendifbut I don’t think that’s right either … ???
01/06/2021 at 7:06 PM #156657MFE = max favourable excursion right ?
In this case you could try that if you want to exit at 50% of the MFE :
(not tested)
12345678910111213level=0.5IF longonmarket thenMAXPRICE = MAX(MAXPRICE,close)mfe=MAX-positionpricetrigger=positionprice+mfe*levelif barindex-tradeindex > b and mfe > 0 and close<trigger thensell at marketendifendif3 users thanked author for this post.
01/06/2021 at 7:21 PM #156660Not tested
1234MFE = summation[barindex-tradeindex](positionperf > 0)IF longonmarket and barindex-tradeindex > b and (MFE = 0) thensell at marketendif3 users thanked author for this post.
01/06/2021 at 8:40 PM #156668Thanks @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.
01/12/2021 at 9:42 AM #157469Hi 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”.
1234MFE = summation[barindex-tradeindex](positionperf > 0)IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) thenBuy 5 perpoint at marketendifany ideas? thanks!
01/12/2021 at 10:26 AM #157478I’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 ?
01/12/2021 at 10:33 AM #15747901/12/2021 at 10:36 AM #1574801IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) thenI 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 :
12345IF shortonmarket thenIF barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) then...ENDIFENDIFand 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.
3 users thanked author for this post.
01/12/2021 at 11:06 AM #15748601/12/2021 at 6:59 PM #1575961IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) thenThe 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:
1234567891011if shortonmarket thenif barindex-tradeindex > 24 thenif MFE => 0 thenif MFE <= 6 then...endifendifendifendif01/12/2021 at 8:39 PM #157600The 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
1234MFE = MAX(1,summation[barindex-tradeindex](positionperf > 0))IF shortonmarket and barindex-tradeindex > 24 and (MFE => 0 and MFE <= 6) thenBuy 5 perpoint at marketendif01/13/2021 at 10:33 AM #157619different 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…
1234MFE = MAX(1,summation[barindex-tradeindex](positionperf > 0))IF shortonmarket and barindex-tradeindex > 24 and (MFE <= 6) thenBuy 5 perpoint at marketendif01/13/2021 at 10:49 AM #157621instead of a reversal, maybe try separating it into 2 instructions, first exit the short, then enter long ??
-
AuthorPosts
Find exclusive trading pro-tools on