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 ?
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.
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 … ???
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
Not tested
MFE = summation[barindex-tradeindex](positionperf > 0)
IF longonmarket and barindex-tradeindex > b and (MFE = 0) then
sell at market
endif
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.
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!
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 ?
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.
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.
Thank you so much Peter. Im still new to coding, so this info is very much appreciated 👍
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
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
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
instead of a reversal, maybe try separating it into 2 instructions, first exit the short, then enter long ??