I think I know now why the problem is happening.
I was assuming that with this line
vars priceClose1 = series(priceClose());
I had in priceClose1 the same content, in the same moment, as in priceClose(N), being N the candle number I want to get.
But I must be missing something, because when running this code, you'll see that I don't have the same price with priceClose(3) and priceClose1[3] .
What am I missing here?
While I find a better answer, I will use a for loop in the 2nd run (as I can see that in the first run priceClose() doesn't have the full history) to fill my array of prices. I have run into memory problems with this approach, but I will give it a try again.
Here is the proof of concept if you want to test it yourself:
string asset1 = "EUR/USD";
string asset2 = "USD/CHF";
function run()
{
StartDate = 20120701;
EndDate = 20170125;
BarPeriod = 5;
LookBack = 4000;
vars pairRatio, priceClose1, priceClose2;
asset(asset1);
priceClose1 = series(priceClose(),10);
Spread = Slippage = RollShort = RollLong = 0;
asset(asset2);
priceClose2 = series(priceClose(),10);
Spread = Slippage = RollShort = RollLong = 0;
static int secondRun = 0;
if(is(FIRSTINITRUN)) { // read the parameters only in the first run
printf("nINICIO");
while(loop(asset1,asset2)) assetHistory(Loop1,1);
int i=0;
for (i=1; i<=10;i++){
printf("nFIRST RUN %i %f",i, priceClose(i));
}
}
else {
if (secondRun == 2) secondRun = 3;
if (secondRun == 1) secondRun = 2;
if (secondRun == 0) secondRun = 1;
}
if (secondRun == 1) {
printf("nsecond run");
int i=0;
for (i=1; i<=10;i++){
printf("nSECOND RUN %i %f %f",i, priceClose1[i],priceClose2[i]);
printf("n2nd run p2: %f",priceClose(i));
}
}
if (secondRun == 2) {
printf("nthird run");
int i=0;
for (i=1; i<=10;i++){
printf("nTHIRD RUN %i %f %f",i, priceClose1[i],priceClose2[i]);
printf("n3rd run p2: %f",priceClose(i));
}
}
}