# Calculate the profit/Loss since the new stock launch

I would like to add a column at backtest that calculate the 5yr P&L of the stock
some new stock show "0"

For example, if the stock is new launched 2years before, how to get the 1st close?

I try to write as the following code, but the result is still "0"
Many Thanks!

``````TimeFrameSet( inMonthly ); // switch to monthly frame
startbar=IIf(Ref(C,-48)>0,Ref(C,-48),FirstVisibleValue( C ));
``````

What you do is not correct.

Read here and see example number 3 there.
https://www.amibroker.com/guide/a_custommetrics.html

Here is modification to use Datetimediff

``````// Calculate 5 years cum profit and add custom metric
// Original code at https://www.amibroker.com/guide/a_custommetrics.html
// Modified at https://forum.amibroker.com/t/calculate-the-profit-loss-since-the-new-stock-launch/37363/2
SetCustomBacktestProc("");

/* Now custom-backtest procedure follows */
if( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest( 1 ); // run default backtest procedure
SumProfit = 0;

// iterate through closed trades first
{
diff = DateTimeDiff( StaticVarGet( "LastDt_" + trade.Symbol ), trade.EntryDateTime ) / inYearly;

if( diff < 5 )
{
// here we sum up \$ profit
}

}

// iterate through eventually still open positions
{
diff = DateTimeDiff( StaticVarGet( "LastDt_" + trade.Symbol ), trade.EntryDateTime ) / inYearly;

if( diff < 5 )
{
}

}