# Compute Yearly gain with a twist

I have a code that computes the profit loss/change over time. This is working, however i need to tweak it for a particular scenario. Lets say i’m trying to get the Year to Date change of a particular stock that only started trading in the middle of 2017. If lets say my From-To Dates is 12/29/2017 and I choose the Year to Date param, this stock won’t have a bar or a value from the beginning of the year. How can i tweak my code below to consider the scenario I mentioned above? Currently for stocks that dont have a candle at the beginning of the year, the exploration yields a value of 0.00 .

``````_SECTION_BEGIN("Gainers/Losers % Change over Time");
// PARAMETERS
TtD_Param = ParamList("Period","Day to Date|Week to Date|Month to Date|Year to Date",0);

// VARIABLES
TtD_Period	= IIf(TtD_Param	==	"Day to Date",1,
IIf(TtD_Param	==	"Week to Date",DayOfWeek(),
IIf(TtD_Param	==	"Month to Date",FDayMonth,
IIf(TtD_Param	==	"Year to Date",FDayYear,0))));
// FORMULA
TtD_Change = 100 * (Close - Ref(Close, -TtD_Period) ) / Ref(Close, -TtD_Period);
_SECTION_END();

_SECTION_BEGIN("Explorer");
// FILTER
Filter = 1;
// DISPLAY COLUMNS
SetSortColumns(-3);
_SECTION_END();
``````

Hi @sebastian04
As a guide, I’m using the following AFL to compute the annual and monthly performance. It will automatically run from the start of available data for the year(s) set in the evaluation range.

``````/*
MonthlyTable_01.afl

Exploration code to produce a month-by-month tabular presentation of percentage changes
in a value.

In this code the value is designated as "eq" (for equity), but is simply set to the
price Close for demonstration purposes.

Original code provided by "Mike", here:

http://finance.groups.yahoo.com/group/amibroker/message/125846

Extra comments, _TRACE, and some changes by Progster

*/

_TRACE("!CLEAR!"); // this clears the internal log window.
_TRACE("First line after clear");

//   Calculation of the value for which the tabular display of %Chg is desired.
//   Change this to whatever calculation you wish.
eq = Close;

//   Make the year and month available (in arrays) for every bar processed
yr = Year();      //   e.g. 2008
mo = Month();      //   i.e. 1 - 12

//   Create arrays marking new years and new months
YearChange = yr != Ref( yr, -1 );         //   TRUE on first bar of new year
MonChange = mo != Ref( mo, -1 );         //   TRUE on first bar of new month

FirstYr = 0;
LastYr = 0;

startbar = 0;
endbar = 0;

FirstBar = Status("firstbarinrange");
LastBar = Status("lastbarinrange");

////////////////////////////
////////////////////////////
for ( i = 0; i < BarCount; i++ )
{
if ( FirstBar[ i ] )
{
startbar = i;         //   save the bar index of the first bar in the analysis range
}

if ( LastBar[ i ] )
{
endbar = i;            //   save the bar index of the last bar in the analysis range
break;
}
}

////////////////////////////
// collect yearly / monthly changes in symbol
// into dynamic variables
////////////////////////////

//   Initialize tracking variables
LastYrValue = eq[ startbar ];         //   note: initial equity was set to Close, above
LastMoValue = eq[ startbar ];

MaxYrProfit = MinYrProfit = 0;
MaxMoProfit = MinMoProfit = 0;

//   Loop the analysis range (only)
for ( i = startbar + 1; i <= endbar; i++ )
{

//   Calculate yearly statistics on year change (and at at end of analysis range)
if ( YearChange[ i ] || i == endbar )
{
Chg = 100 * ( -1 + eq[ i ] / LastYrValue );      //   percentage change calc
//Chg = 100 * ( -1 + eq[ i - 1 ] / LastYrValue );      //   percentage change calc

VarSet( "ChgYear" + yr[ i ], Chg );
//VarSet( "ChgYear" + yr[ i - 1 ], Chg );         //   save in dynamic variable for each year

// Track max and min yearly profit across years seen
MaxYrProfit = Max( MaxYrProfit, Chg );
MinYrProfit = Min( MinYrProfit, Chg );

if ( FirstYr == 0 )
FirstYr = yr[ i - 1 ];

// LastYr = yr[ i ];
LastYr = yr[ i - 1 ];

// LastYrValue = eq[ i ];
LastYrValue = eq[ i - 1 ];
}

//   Calculate monthly statistics on month change (and at at end of analysis range)
if ( MonChange [ i ] || i == endbar )
{

thisYr = yr[ i - 1];
mon = mo[ i - 1 ];

//Chg = 100 * ( -1 + eq[ i ] / LastMoValue );      //   percentage change calc
Chg = 100 * ( -1 + eq[ i - 1 ] / LastMoValue );      //   percentage change calc

_TRACE( "Calculations for " ) ;
_TRACE( "Year: " +  NumToStr(thisYr, 1.0) ) ;
_TRACE( "Month: " +  NumToStr(mon, 1.0) ) ;
_TRACE( "LastMoValue: " +  NumToStr(LastMoValue, 1.2) ) ;
// _TRACE( "eq[" + NumToStr(i - 1, 1.0) + "]: " +  NumToStr(eq[ i - 1], 1.2) ) ;
_TRACE( "ThisMoValue: " +  NumToStr(eq[ i ], 1.2) ) ;
//_TRACE( "ThisMoValue: " +  NumToStr(eq[ i - 1], 1.2) ) ;
_TRACE( "Chg: " +  NumToStr(Chg, 1.2) ) ;
_TRACE( "---------------------------" ) ;

VarSet( "ChgMon" + yr[ i ] + "-" + mon, Chg );
//VarSet( "ChgMon" + yr[ i - 1 ] + "-" + mon, Chg );   //   save in dynamic variable for each month

VarSet( "SumChgMon" + mon, Chg + Nz( VarGet( "SumChgMon" + mon ) ) );
VarSet( "SumMon" + mon, 1 + Nz( VarGet( "SumMon" + mon ) ) );

// Track max and min monthly profit across months seen
MaxMoProfit = Max( MaxMoProfit, Chg );
MinMoProfit = Min( MinMoProfit, Chg );

LastMoValue = eq[ i ];
//LastMoValue = eq[ i - 1 ];
}
}

////////////////////////////
// Transfer dynamic variable values into arrays and add to exploration.
////////////////////////////

Years = 0;
Jan = Feb = Mar = Apr = May = Jun = Jul = Aug = Sep = Oct = Nov = Dec = 0;
Annual = 0;
index = startbar;

for ( y = FirstYr; y <= LastYr; y++ )
{
Years[ index ] = y;
Jan[ index ] = VarGet( "ChgMon" + y + "-" + 1 );
Feb[ index ] = VarGet( "ChgMon" + y + "-" + 2 );
Mar[ index ] = VarGet( "ChgMon" + y + "-" + 3 );
Apr[ index ] = VarGet( "ChgMon" + y + "-" + 4 );
May[ index ] = VarGet( "ChgMon" + y + "-" + 5 );
Jun[ index ] = VarGet( "ChgMon" + y + "-" + 6 );
Jul[ index ] = VarGet( "ChgMon" + y + "-" + 7 );
Aug[ index ] = VarGet( "ChgMon" + y + "-" + 8 );
Sep[ index ] = VarGet( "ChgMon" + y + "-" + 9 );
Oct[ index ] = VarGet( "ChgMon" + y + "-" + 10 );
Nov[ index ] = VarGet( "ChgMon" + y + "-" + 11 );
Dec[ index ] = VarGet( "ChgMon" + y + "-" + 12 );
Annual[ index ] = VarGet( "ChgYear" + y );

index++;
}

Filter = Years;

SetOption("NoDefaultColumns", False);