Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Printing days since equity high in backtest report
- This topic is empty.
-
AuthorPosts
-
August 14, 2017 at 8:19 am #101680TrentRothallParticipant
I’ve been playing around with some custom backtest code to try and print “days since equity high” in the backtest report.
Printing bars since equity high is relatively straight forward and while handy doesn’t give you a figure in calandar days. Below prints bars since high.
Code:StaticVarSet(“Low” + Name(), Low); // In regular codeeq = Foreign(“~~~EQUITY”, “C”);
cash = Foreign(“~~~EQUITY”, “L”);
dr = eq – Highest(eq);
bslh = HighestBars(eq);
//———————————————————————————//————————————————————————————-
SetCustomBacktestProc( “” );
if ( Status( “action” ) == actionPortfolio )
{
bo = GetBacktesterObject();
// run default backtest procedure without generating the trade list
bo.Backtest( True );// iterate through closed trades
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
LowArray = StaticVarGet(“Low” + trade.Symbol);
LowAtEntry = Lookup(LowArray, trade.EntryDateTime);
trade.AddCustomMetric(“Low At Entry”, LowAtEntry);
bs = Lookup(bslh,trade.ExitDateTime);
trade.AddCustomMetric(“Bslh”,bs);}
// generate trade list
bo.ListTrades( );}
I think to prints days since high. I need to use DaysSince1900(). But i’m unsure how to set the bar when equity made a previous high. i would then subtract todays value od DaysSince1900() from the previous highs value. That probably doesn’t make sense… I know in my head how to do it i think i just can’t write it.The following only prints DaysSince1900
Any ideas, i think it will be handy.
Code:StaticVarSet(“Low” + Name(), Low); // In regular codeeq = Foreign(“~~~EQUITY”, “C”);
cash = Foreign(“~~~EQUITY”, “L”);
dr = eq – Highest(eq);
bslh = HighestBars(eq);
//———————————————————————————
lh = StaticVarSet(“High”, bslh == DaysSince1900());
// trying to set lh to the day when equity was highest//————————————————————————————-
SetCustomBacktestProc( “” );
if ( Status( “action” ) == actionPortfolio )
{
bo = GetBacktesterObject();
// run default backtest procedure without generating the trade list
bo.Backtest( True );// iterate through closed trades
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
LowArray = StaticVarGet(“Low” + trade.Symbol);
LowAtEntry = Lookup(LowArray, trade.EntryDateTime);
trade.AddCustomMetric(“Low At Entry”, LowAtEntry);
bs = Lookup(bslh,trade.ExitDateTime);
trade.AddCustomMetric(“Bslh”,bs);
dayshigh = Lookup(DaysSince1900()-lh,trade.ExitDateTime); // trying to calculate days since eq high
trade.AddCustomMetric(“Days since eq High”,dayshigh);
}// generate trade list
bo.ListTrades( );}
August 14, 2017 at 4:47 pm #107350SaidBitarMembersimplest way if you have number of bars (trading days) you can get the approximate calendar days by multiplying by 365 and divide by 252
i think if you want to get the exact days you need to use a loop to get the date when it was the highest value of equity, then you use DateTimeDiff(date when you had the high, current date) the answer will be in seconds / 3600 / 24 you get it in days
-
AuthorPosts
- You must be logged in to reply to this topic.