I keep strugling with my Pairs Trading Strategy.

This time I a suffering a problem with what I think it is a problem with histories.

I download my histories from an Oanda Demo account, which runs much better and faster than MT4.

This is a snippet of the code I am running:

Code:
function run()
{
	BarPeriod =1;
	StartDate = 20160615; 
	EndDate = 20170201; 
	LookBack = 1000;
	UnstablePeriod = 50;
	vars pairRatio, priceClose1, priceClose2;
	//---------1 ----------//
	asset(asset1);
	Spread =	Slippage = RollShort = RollLong = 0; 
	priceClose1 = series(log(priceClose()) / log(10));
	//---------2 ----------//
	asset(asset2);
	Spread =	Slippage = RollShort = RollLong = 0; 
	priceClose2 = series(1/log(priceClose()) / log(10));
	pairRatio = series((priceClose1[0]) * (priceClose2[0]));
	
	if(is(FIRSTINITRUN)) { 
		while(loop(asset1,asset2)) assetHistory(Loop1,1);
	}
	
	int halfLife = LookBack;
	vars zScore = series((pairRatio[0] - SMA(pairRatio,halfLife)) / StdDev(pairRatio, halfLife));
	if (Bar > LookBack) printf("nzScore: %f",zScore[0]);


With this code snippet, it runs fine in "Trade" mode for LookBack smaller than 1000 (or so, I don't know exactly where is the frontier). If I try with LookBack = 2000 and bigger, then I get

Code:
zScore: -1.#IND00
zScore: -1.#IND00
zScore: -1.#IND00
zScore: -1.#IND00
zScore: -1.#IND00
zScore: -1.#IND00
zScore: -1.#IND00
zScore: -1.#IND00

[2093: Wed 08.02.17 14:55]  0.99387
zScore: -1.#IND00
Logout.. ok



I guess there must be a problem with histories: either I don't have enough histories, or there's a gap and at some point one of the asset's price is zero, or something like that.

I would like to know what could I do to
1) First, to be sure that this issue is due to a histories problem
2) Second, if it is actually a history problem, to solve it somehow... like using latest known price if there's a gap, or another intermediate solution.

If I run this same script in Trading mode with Oanda, it runs wonderfully.

I think that what is happening is that MT4 history sucks horribly, and when I'm in trading mode with MT4 accounts, it loads MT4 history, it does not get correct histories, and then it gets this error...

How do I solve this nightmare?