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
33
indicator
132
strategy
171

Recent Snippets

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 [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...