Exploration/Scan/Backtest based on visible bar on active chart

Hi all,
I have the following AFL to plot equity lines and running Exploration/Scan/Backtest based on visible bars.
I can clearly see there are buy & sell signal on the chart with changing equity value. on the chart.

Broker_2018-02-23_16-23-59

However, Scan and Backtest won't produce any results.
Is it possible to do Scan Backtest based on visible bars? Appreciate any advice on this.

Thanks.

BuyPrice = Close;
SellPrice = Close;

function ParamOptimize( pname, defaultval, minv, maxv, step )
{
    return Optimize( pname,
                     Param( pname, defaultval, minv, maxv, step ),
                     minv, maxv, step );
}


_SECTION_BEGIN( "FastEMA" );
FastEMA = ParamOptimize( "FastEMA", 1, 1, 200, 5 );
Plot( ema( C, FastEMA ), _DEFAULT_NAME(), colorGreen );
_SECTION_END();


_SECTION_BEGIN( "SlowEMA" );
SlowEMA = ParamOptimize( "SlowEMA", 21, 1, 200, 10 );
Plot( ema( C, SlowEMA ), _DEFAULT_NAME(), colorBlue );
_SECTION_END();

Buy = Cross( ema( C, FastEMA ), ema( C, SlowEMA ) );

//Sell rule: EMA

Sell = Cross( ema( C, SlowEMA ), ema( C, FastEMA ) ) ;

Buy = ExRem( Buy, Sell );

Sell = ExRem( Sell, Buy );

SetChartOptions( 0, chartShowArrows | chartShowDates );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

Plot( Close, "Close", colorDefault, styleCandle );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, Offset = -45 );

PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );


//Compute Equity Curve
_SECTION_BEGIN( "Equity Curve" );
	
	bi = Barindex();
	fvb = FirstVisiblevalue( bi );
	lvb = LastVisiblevalue( bi );
	
	fromIndex = Nz(fvb);
	toIndex = Nz(lvb);

	dateNumArray = DateNum();
	fromDate = dateNumArray[fromIndex];
	toDate = dateNumArray[toIndex];
	
    e = Equity( 1, 3, fromDate, toDate ); 
	Plot( e, "Equity", colorGrey40, styleLeftAxisScale | ParamStyle( "Equity Curve", defaultstyle = styleDashed | styleThick ) );

_SECTION_END();

Functions FirstVisiblevalue and LastVisiblevalue are chart functions. See "...Visible...".

Analysis does not have visible chart area, does it. So those functions would return 0 for those barindexes. Just trace.

bi = BarIndex();

_TRACEF( "%g, %g", FirstVisibleValue(bi), LastVisibleValue(bi) );

So either store indexes in chart or use bars of selected analysis range if code is applied there in analysis.

Thanks fxshrat. I traced the code and got 0 as you mentioned. Just realised that _TRACEF is so superb. It also traces and updates the value as I change the chart view.
It's fun to play with Ambroker flexibility