Implementing Dynamic Take Profit Strategy Using MFE in ProBuilder

29 May 2017
0 comment
0 attachment

This code snippet demonstrates how to calculate and utilize the Maximum Favorable Excursion (MFE) to dynamically set take profit levels in a trading strategy using the ProBuilder language. The MFE measures the maximum potential profit for a trade from the entry point to the closing point.

defparam cumulateorders=false //dummy strategy
if rsi[14] crosses over 50 then
  buy at market
endif

set stop ploss 50

//increase the array index
if not longonmarket and longonmarket[1] then
  index=index+1
endif

//compute the MFE
if longonmarket then
  mfe = max(mfe,high-tradeprice)
  $mfeArray[index] = mae
endif

//at least 10 orders to allow the average calculation
if isset($mfeArray[2]) then
  sum=0
  for i = 0 to index do
    sum=sum+$mfeArray[i]
  next
  avg = sum/index
endif

graph avg

if avg>0 then
  set target profit avg
else
  set target pprofit 200
endif

Explanation of the Code:

  • Line 1: Disables order accumulation to treat each order independently.
  • Lines 2-4: A buy order is placed if the 14-period RSI crosses over 50.
  • Line 6: Sets a stop loss of 50 points.
  • Lines 8-10: Manages an index for tracking the number of trades.
  • Lines 12-15: Calculates the MFE for an open position and stores it in an array. MFE is the maximum profit observed from the entry price to the current high.
  • Lines 17-23: Checks if there are at least 10 entries in the MFE array to start calculating the average MFE. It then calculates the average MFE from the array values.
  • Line 25: Plots the average MFE on the chart for visualization.
  • Lines 27-30: Sets the dynamic take profit level based on the average MFE if it’s greater than zero; otherwise, it defaults to a take profit of 200 points.

This snippet is useful for traders looking to implement a dynamic take profit strategy based on historical performance of their trades, adjusting their exit points to optimize potential gains.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/mae-maximisation/#post-143693

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
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...