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:
This example is useful for understanding how to manage and analyze trading performance metrics programmatically.
Check out this related content for more information:
https://www.prorealcode.com/topic/richiesta-creazione-indicatore-runup-drawdown/#post-166936
Visit Link