Dynamic max drawdown

I want access to the value of max drawdown at every bar.
I receive an error i think on Highest(PortEquity[bar]).
I can't find a way to get the highest value of the equity line until the actual bar.

 PortEquity = Null; 
	for (bar=0; bar < BarCount; ++bar)
	{
		
			
			PortEquity[ bar ] = bo.Equity; 
			DD[bar] = 100* (bo.Equity/Highest(PortEquity[bar])-1);
			_TRACE(NumToStr(dt[bar],formatDateTime)+": Previous DD="+DD) ;
			if (DD[bar] < -5 )
			{
			//do something ;
			}
	}

Highest() function is an array function!
It is incorrect to use such array functions within barcount loop.
https://www.amibroker.com/guide/afl/highest.html
https://www.amibroker.com/guide/h_understandafl.html
https://www.amibroker.com/guide/a_mistakes.html

m = MA( Close, 20);
Buy = Cross( Close, m );
Sell = Cross( m, Close ); 
Short = Cover = 0;

SetCustomBacktestProc(""); 
if ( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 

	bo.PreProcess();
	
	dt = DateTime();
	max_eq = 0;
	/// @link https://forum.amibroker.com/t/dynamic-max-drawdown/12337/2
	for ( bar = 0; bar < BarCount; bar++ )
	{
		bo.ProcessTradeSignals(bar);
		
		eq = bo.Equity;
		max_eq = Max(max_eq, eq);
		DD = 100 * ( eq / max_eq - 1 );
		_TRACE( NumToStr( dt[bar], formatDateTime ) + ": Previous DD=" + DD ) ;

		if( DD < -5 )
		{
			_TRACE( "DD below -5" );
		}
	}
	
	bo.PostProcess();	
}
4 Likes