Get year from trade.EntryPrice

Hi all,

I'm trying to get year from trade.EntryPrice (in the loop below), in the format of 2014 (YYYY). Is there an easy way? Been looking around but now found anything that works in the GetBacktesterObject context.

SetCustomBacktestProc(""); 
if( Status("action") == actionPortfolio ) 
{ 
   bo = GetBacktesterObject(); 
   bo.Backtest(1); // run default backtest procedure 
   
   // iterate through closed trades first 
   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
   {
	   symbolRisk = StaticVarGet( trade.Symbol + "Risk" );
       entryRisk = Lookup( symbolRisk, trade.EntryDateTime );
       
       RMultiple = (trade.ExitPrice() - trade.EntryPrice()) / entryRisk;
       SumProfitPerRisk = SumProfitPerRisk + RMultiple;        
       NumTrades++; 
       
       trade.AddCustomMetric("R-multiple", RMultiple); 
       
       //TODO: Get year from trade.EntryPrice()   
       // Wished result: 2014... :(
   } 
  
   bo.ListTrades(); 
} 

Something like this untested snippet should work:

// Put this outside your for loop
yr = Year();
bi = BarIndex();

// ... and this where you have TODO:
entryBar = Lookup(bi, trade.entrydatetime,0);
entryYear = yr[entryBar];
2 Likes

Worked great, thanks! :slightly_smiling_face: