Max Drawdowns With Details.. Anyone want to try?

I am trying to create an exploration of a watchlist, where for each symbol the exploration will display the top 3 drawdowns, with the Start Date of the drawdown, the Low Date of the drawdown, the Recovery Date and the BarsDown and Bars Recover......

I am able to list the top 5 drawdowns in my backtest afl, but cannot get the dates nor the barsdown or barsrecover.. using this code

	if(BarCount > 200)
	{
		VarSet("DD2", 100);
		eq = bo.EquityArray();
		drPerc = -100*(eq/Highest(eq) - 1);
		dd = 0;
		bslh = HighestBars(eq);
		bslh[BarCount-1] = 0; // need to catch last DD
		dd = IIf(bslh == 0, IIf(Ref(bslh, -1)==0, 0, HHV(drPerc, Ref(bslh, -1)+1)), 0);
		ddLast = LastValue(Highest(dd));
		VarSet("DD1", ddLast);
		bo.AddCustomMetric("DD1", ddLast);
		for (nDD = 2; nDD <= numberDrawdowns; nDD++)
		{
			ddNew = LastValue(Highest(IIf(dd < ddLast, dd, 0)));
			VarSet("DD" + nDD, ddNew);
			ddLast = ddNew;	
			bo.AddCustomMetric("DD"+nDD, ddLast);
		}
		bo.AddCustomMetric("MDD Cur", LastValue(drPerc));
	}

However this only lists the drawdowns on the report.....

ddr

I want to get this data in exploration (for easier analyses and the ability to export to html, csv)... again, the top 3 to 5 drawdowns with details would be awesome.... below is how I envision the exploration results to look...

Any help, ideas, guidance would be very much appreciated.. thanks

exp

Well... I got it to work.... its a lot of code but it successfully gets the 5 max drawdowns with details... just run it in explore ... have to add the watchlist loop for multiple symbols if you want... if you see anything wrong or can improve on it, please advise

numberDrawdowns=5;

EQ = C;
MaxEQ = Highest( EQ );
DD = 100 * ( Eq - MaxEQ ) / MaxEq;
D2 = -100*(eq/Highest(eq) - 1);
MaxDD = Lowest( DD );
BarsDD = HighestBars( EQ );
BarsDD[BarCount-1] = 0;

DD = IIf(BarsDD == 0, IIf(Ref(BarsDD, -1)==0, 0, HHV(D2, Ref(BarsDD, -1)+1)), 0);
DDLAST = LastValue(Highest(dd));
VarSet("DD1", ddLast);

for (nDD = 2; nDD <= numberDrawdowns; nDD++)
{
	ddNew = LastValue(Highest(IIf(DD < ddLast, dd, 0)));
	VarSet("DD" + nDD, ddNew);
	ddLast = ddNew;
}

BD = ValueWhen(Ref(BarsDD,-1)==0,Ref(DateTime(),-1));
DD=VarGet("DD");

DD1 = VarGet("DD1");
BegD1 = DD==DD1;
BD1 = ValueWhen(BegD1,BD);
LowD1 = D2==DD1;
LD1 = ValueWhen(LowD1,DateTime());
BarsD1 = ValueWhen(LowD1,BarsDD);
ED1 = ValueWhen(DD==DD1,DateTime());
BarsU1 = ValueWhen(DD==DD1,Ref(BarsDD,-1)-BarsD1);

DD2 = VarGet("DD2");
BegD2 = DD==DD2;
BD2 = ValueWhen(BegD2,BD);
LowD2 = D2==DD2;
LD2 = ValueWhen(LowD2,DateTime());
BarsD2 = ValueWhen(LowD2,BarsDD);
ED2 = ValueWhen(DD==DD2,DateTime());
BarsU2 = ValueWhen(DD==DD2,Ref(BarsDD,-1)-BarsD2);

DD3 = VarGet("DD3");
BegD3 = DD==DD3;
BD3 = ValueWhen(BegD3,BD);
LowD3 = D2==DD3;
LD3 = ValueWhen(LowD3,DateTime());
BarsD3 = ValueWhen(LowD3,BarsDD);
ED3 = ValueWhen(DD==DD3,DateTime());
BarsU3 = ValueWhen(DD==DD3,Ref(BarsDD,-1)-BarsD3);

DD4 = VarGet("DD4");
BegD4 = DD==DD4;
BD4 = ValueWhen(BegD4,BD);
LowD4 = D2==DD4;
LD4 = ValueWhen(LowD4,DateTime());
BarsD4 = ValueWhen(LowD4,BarsDD);
ED4 = ValueWhen(DD==DD4,DateTime());
BarsU4 = ValueWhen(DD==DD4,Ref(BarsDD,-1)-BarsD4);;

DD5 = VarGet("DD5");
BegD5 = DD==DD5;
BD5= ValueWhen(BegD5,BD);
LowD5 = D2==DD5;
LD5 = ValueWhen(LowD5,DateTime());
BarsD5 = ValueWhen(LowD5,BarsDD);
ED5 = ValueWhen(DD==DD5,DateTime());
BarsU5 = ValueWhen(DD==DD5,Ref(BarsDD,-1)-BarsD5);
dt=DateTime();


Filter=1;

AddColumn(DD1,"DD1");
AddColumn(BD1,"BD1",formatdatetime);
AddColumn(LD1,"LD1",formatdatetime);
AddColumn(BarsD1,"BarsD1");
AddColumn(ED1,"ED1",formatdatetime);
AddColumn(BarsU1,"BarsU1");

AddTextColumn("||","");

AddColumn(DD2,"DD2");
AddColumn(BD2,"BD2",formatdatetime);
AddColumn(LD2,"LD2",formatdatetime);
AddColumn(BarsD2,"BarsD2");
AddColumn(ED2,"ED2",formatdatetime);
AddColumn(BarsU2,"BarsU2");

AddTextColumn("||","");

AddColumn(DD3,"DD3");
AddColumn(BD3,"BD3",formatdatetime);
AddColumn(LD3,"LD3",formatdatetime);
AddColumn(BarsD3,"BarsD3");
AddColumn(ED3,"ED3",formatdatetime);
AddColumn(BarsU3,"BarsU3");

AddTextColumn("||","");

AddColumn(DD4,"DD4");
AddColumn(BD4,"BD4",formatdatetime);
AddColumn(LD4,"LD4",formatdatetime);
AddColumn(BarsD4,"BarsD4");
AddColumn(ED4,"ED4",formatdatetime);
AddColumn(BarsU4,"BarsU4");

AddTextColumn("||","");

AddColumn(DD5,"DD5");
AddColumn(BD5,"BD5",formatdatetime);
AddColumn(LD5,"LD5",formatdatetime);
AddColumn(BarsD5,"BarsD5");
AddColumn(ED5,"ED5",formatdatetime);
AddColumn(BarsU5,"BarsU5");

@mfu5324 I did not check the logic of your code, but since you are using "dynamic" vars in the first part of the formula I suggest to fully utilize them, also in the last part/exploration:


/// your previous code till the end of the loop

Filter = 1;
dt = DateTime();
BD = ValueWhen( Ref( BarsDD, -1 ) == 0, Ref( dt, -1 ) );
DD = VarGet( "DD" );
for( i = 1; i <= numberDrawdowns; i++ )
{
    DD_ = VarGet( "DD" + i );
    VarSet( "BegD" + i, DD == DD_ );
    BegD_ = VarGet( "BegD" + i );
    VarSet( "BD" + i, ValueWhen( BegD_, BD ) );
    VarSet( "LowD" + i, D2 == DD_ );
    LowD_ = VarGet( "LowD" + i );
    VarSet( "LD" + i, ValueWhen( LowD_, dt ) );
    VarSet( "BarsD" + i, ValueWhen( LowD_, BarsDD ) );
    BarsD_ = VarGet( "BarsD" + i );
    VarSet( "ED" + i, ValueWhen( DD == DD_, dt ) );
    VarSet( "BarsU" + i, ValueWhen( DD == DD_, Ref( BarsDD, -1 ) - BarsD_ ) );

    AddTextColumn("", "|" + i + "|");
    AddColumn( DD_, "DD" + i );
    AddColumn( VarGet( "BD" + i ), "BD" + i, formatdatetime );
    AddColumn( VarGet( "LD" + i ), "LD" + i, formatdatetime );
    AddColumn( BarsD_, "BarsD" + i, 1 );
    AddColumn( VarGet( "ED" + i ) , "ED" + i, formatdatetime );
    AddColumn( VarGet( "BarsU" + i ), "BarsU" + i, 1 );
}

In such a way, if you like it, you can also parametrize the first line:

numberDrawdowns = Param("Number of drawdown", 5, 2, 10, 1);

(I only tested the above code briefly, so please, if you use it, verify it well).

2 Likes