This ProBuilder code snippet is designed to calculate Maximum Adverse Equity (MAE) and Maximum Favourable Equity (MFE) for trading positions, specifically focusing on long positions. These metrics are crucial for understanding the potential risk and reward associated with open trading positions. The code calculates all-time and recent MAE and MFE values, both average and maximum, which can be useful for setting stop loss and take profit levels dynamically.
n = 12
if barindex <> 0 then
mae = mae + (open - low)
total = total + 1
mfe = mfe + (high - open)
biggestmae = max((open - low),biggestmae)
biggestmfe = max((high - open),biggestmfe)
recentmae = (open[n] - lowest[n](low))
recentmfe = (highest[n](high) - open[n])
maerec = 0
mferec = 0
for i = 1 to n
maerec = maerec + (open[i] - low[i])
mferec = mferec + (high[i] - open[i])
next
endif
averecentmae = maerec / n
averecentmfe = mferec / n
avemae = mae / total
avemfe = mfe / total
return -avemae coloured(128,0,0) as "All Time Average MAE", avemfe coloured(0,128,0) as "All Time Average MFE", -biggestmae coloured(128,0,0) as "Biggest Ever 1 Bar MAE", biggestmfe coloured(0,128,0) as "Biggest Ever 1 Bar MFE", -recentmae coloured(128,0,0) as "MAE Since n Bar Open", recentmfe coloured(0,128,0) as "MFE Since n Bar Open", -averecentmae coloured(128,0,0) as "Average Recent MAE", averecentmfe coloured(0,128,0) as "Average Recent MFE"
Explanation of the Code:
This code provides a comprehensive view of how far prices have moved in favor of or against a position, which is essential for risk management in trading strategies.
Check out this related content for more information:
https://www.prorealcode.com/topic/mae-and-mfe-calculator/#post-77327
Visit Link