Calculating MAE and MFE for Trading Positions

20 Aug 2019
0 comment
0 attachment

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:

  • Initialization: The variable n is set to 12, which represents the number of bars to look back for recent MAE and MFE calculations.
  • Conditional Execution: The code inside the if block executes if the current bar index is not zero, ensuring calculations are only performed after the first bar.
  • Accumulation: MAE and MFE are accumulated over all bars using the differences between open and low prices for MAE, and high and open prices for MFE.
  • Maximum Values: The code tracks the largest single-bar MAE and MFE encountered using the max function.
  • Recent Values: MAE and MFE since the bar n bars ago are calculated directly from historical data functions lowest and highest.
  • Average Recent Values: A loop calculates the total MAE and MFE for the last n bars, which are then averaged.
  • Output: The indicator outputs several values, each colored for visual distinction, representing average and maximum MAE and MFE values over all time and for recent bars.

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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/mae-and-mfe-calculator/#post-77327

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...