Per trade custom metric

Hello !

I have used KB for adding custom metrics but faced strange behaviour. I'm adding VIX and today's change in percent.

VixClose = Foreign( "VIX", "C" ); 
TimeFrameSet( inDaily  );
VixClose = Ref(VixClose,-1);
TimeFrameRestore();
PrevClose=TimeFrameGetPrice( "C", inDaily, -1 );
daychng=100*(C-PrevClose)/PrevClose;
StaticVarSet( "Sdaychng", daychng);
StaticVarSet( "VIXC", VixClose);

SetCustomBacktestProc(""); 

if( Status("action") == actionPortfolio ) 
 { 
     bo = GetBacktesterObject(); 
    bo.Backtest(True);
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        SdaychngVar = StaticVarGet( "Sdaychng" );
        trade.AddCustomMetric( "Day Change", Lookup( SdaychngVar, trade.EntryDateTime ) );
        symbolVIX = StaticVarGet("VIXC");
        trade.AddCustomMetric( "Vix Close", Lookup( symbolVIX, trade.EntryDateTime ) );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        symbolVIX = StaticVarGet("VIXC");
        trade.AddCustomMetric( "Vix Close", Lookup( symbolVIX, trade.EntryDateTime ) );
        SdaychngVar = StaticVarGet( "Sdaychng" );
        trade.AddCustomMetric( "Day Change", Lookup( SdaychngVar, trade.EntryDateTime ) );
    }
    bo.ListTrades( );
}

VIX metric works as it should but in "Day Change" I always see "-1.#J" . I have googled "Amibroker -1.#J" but there is nothing about that.
When I plot daychng series , itt works fine so series are ok.

Any ideas ? Thank you all for support !

You are not doing it right.

You should use SetForeign.
Next you should expand after TimeFrameRestore ( Please read the manual slowly and carefully).
Next you should add symbol name to static vars.

tmfrm = InDaily;

SetForeign("VIX"); 
	TimeFrameSet( tmfrm );
		VixClose = Ref(C,-1);
	TimeFrameRestore();
VixClose = TimeFrameExpand(VixClose, tmfrm);

PrevClose=TimeFrameGetPrice( "C", inDaily, -1 );
daychng=100*(C-PrevClose)/PrevClose;

StaticVarSet( "Sdaychng_"+Name(), daychng);
StaticVarSet( "VIXC_"+Name(), VixClose);

SetCustomBacktestProc(""); 
if( Status("action") == actionPortfolio ) 
 { 
    bo = GetBacktesterObject(); 
    bo.Backtest(True);
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        SdaychngVar = StaticVarGet( "Sdaychng_"+trade.Symbol );
        trade.AddCustomMetric( "Day Change", Lookup( SdaychngVar, trade.EntryDateTime ) );
        symbolVIX = StaticVarGet("VIXC_"+trade.Symbol);
        trade.AddCustomMetric( "Vix Close", Lookup( symbolVIX, trade.EntryDateTime ) );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        symbolVIX = StaticVarGet("VIXC_"+trade.Symbol);
        trade.AddCustomMetric( "Vix Close", Lookup( symbolVIX, trade.EntryDateTime ) );
        SdaychngVar = StaticVarGet( "Sdaychng_"+trade.Symbol );
        trade.AddCustomMetric( "Day Change", Lookup( SdaychngVar, trade.EntryDateTime ) );
    }
    bo.ListTrades( );
}
2 Likes

Thank you so much ! Now it works.