Tracking Maximum Adverse and Favorable Excursions in Trading Strategies

28 Mar 2019
0 comment
0 attachment

This code snippet is designed to calculate and graph the Maximum Adverse Excursion (MAE) and Maximum Favorable Excursion (MFE) during the backtesting of a trading strategy. These metrics are crucial for assessing the potential risk and reward of a strategy while it is in the market.


inside a strategy, it shows mae / mfe
if longonmarket then
    lowestprice=min(tradeprice(1),low)
    mae=lowestprice-tradeprice(1)
    if mae>mae[1] then
        mae=mae[1]
    endif
    highestprice=max(tradeprice(1),high)
    mfe=highestprice-tradeprice(1)
    if mfemae[1] then
        mae=mae[1]
    endif
    lowestprice=min(tradeprice(1),low)
    mfe=tradeprice(1)-lowestprice
    if mfe

Explanation of the Code:

  • MAE and MFE Calculation: The code calculates MAE and MFE for both long and short positions in the market. MAE measures the largest loss from the entry point, while MFE measures the largest profit from the entry point during the trade's lifetime.
  • Conditional Checks: It uses conditional statements to check whether the strategy is in a long or short position and updates the MAE and MFE values based on the highest and lowest prices since entering the trade.
  • Resetting Values: When the strategy is not in the market or switches between long and short positions, it resets the MAE and MFE values to zero.
  • Graphing Results: Finally, the code graphs the rounded values of MFE and MAE, coloring them blue and red respectively, to visually represent the excursions during the strategy's execution.

This snippet is a practical tool for traders and analysts to visually assess the risk and reward characteristics of their trading strategies during backtesting.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/mae-mfe-code-indicator/#post-129348

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.
paul New
Currently debugging life, so my bio is on hold. Check back after the next commit for an update.
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
132
strategy
171

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Logo Logo
Loading...