Implementing Run-Up and Draw-Down Calculations in Trading Strategies

07 Mar 2019
0 comment
0 attachment

This code snippet demonstrates how to calculate and track the maximum run-up (MaxRU) and draw-down (MaxDD) in a trading strategy using the ProBuilder language. These metrics are crucial for assessing the performance and risk of trading strategies over a specified number of periods.


// test code (DAX,1 hour,50K units) on PERIODS bars
// ONCE declarations to initialize variables
ONCE Periods = 10
ONCE Capital = 10000
ONCE MinPoint = Capital
ONCE MaxPoint = 0
ONCE MaxRU = 0
ONCE MaxDD = 0
ONCE j = 0

// Reset arrays if it's the first bar
IF BarIndex < 1 THEN
    FOR i = 1 TO Periods
        $Equity[i] = 0
        $TempProfit[i] = 0
        $TempEquity[i] = 0
        $MaxPoint[i] = 0
        $MinPoint[i] = Capital
        $DD[i] = 0
        $RU[i] = 0
    NEXT
ENDIF

// Detect changes in market position
z1 = OnMarket[1] AND Not OnMarket
z2 = LongOnMarket AND ShortOnMarket[1]
z3 = ShortOnMarket AND LongOnMarket[1]
z4 = (Not OnMarket[1] AND Not OnMarket) AND (StrategyProfit <> StrategyProfit[1])
Cambio = z1 OR z2 OR z3 OR z4

// Update equity and profit calculations on change
IF Cambio THEN
    j = min(Periods,j + 1)
    $Equity[j] = Capital + StrategyProfit
    $TempProfit[j] = PositionPerf * PositionPrice / PipSize
    $TempEquity[j] = $Equity[j] + $TempProfit[j]

    // Calculate DrawDown
    $MaxPoint[j] = max($MaxPoint[j],$TempEquity[j])
    $DD[j] = $MaxPoint[j] - $TempEquity[j]

    // Calculate RunUp
    $MinPoint[j] = min($MinPoint[j],$TempEquity[j])
    $RU[j] = $TempEquity[j] - $MinPoint[j]

    // Calculate max DD and RU over the period
    IF j = Periods THEN
        FOR i = 1 TO Periods
            MaxDD = max(MaxDD,$DD[i])
            MaxRU = max(MaxRU,$RU[i])
        NEXT
        DDRUratio = (MaxDD / MaxRU) * 100

        // Shift data in arrays for next calculation
        FOR i = Periods DOWNTO 2
            $Equity[i - 1] = $Equity[i]
            $TempProfit[i - 1] = $TempProfit[i]
            $TempEquity[i - 1] = $TempEquity[i]
            $MaxPoint[i - 1] = $MaxPoint[i]
            $MinPoint[i - 1] = $MinPoint[i]
            $DD[i - 1] = $DD[i]
            $RU[i - 1] = $RU[i]
        NEXT
    ENDIF
ENDIF

// Trading logic based on moving average
avg = Average[100,1](close)
if close crosses over Avg and Not OnMarket then
    buy at Market
elsif close crosses under Avg and Not OnMarket then
    sellshort at Market
endif
set target pprofit 2000
set stop ploss 2000

// Exit strategy based on profit
if positionperf > 0.0005 then
    sell at market
    exitshort at market
endif

// Graphical output of DD/RU ratio and values
graph DDRUratio
graph MaxDD
graph MaxRU

This code snippet includes:

  • Initialization: Setting up initial values and arrays to store trading data.
  • Detection of market position changes: Identifying changes in market positions to update calculations.
  • Equity and profit calculations: Updating equity based on the strategy’s profit and calculating temporary profits.
  • DrawDown and RunUp calculations: Computing the maximum draw-down and run-up values during the trading period.
  • Graphical output: Displaying the draw-down to run-up ratio and their maximum values on a graph for visual analysis.

This example is useful for understanding how to manage and analyze trading performance metrics programmatically.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/richiesta-creazione-indicatore-runup-drawdown/#post-166936

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.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
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...