Looping for optimizing a custom series of values

Hi,

I would like to optimize a custom series of values for two variables, as it is shown in the following example code:

SetTradeDelays(0, 0, 0, 0);
SetBacktestMode(backtestRegular);

A1 = 1;
B1 = 1.1; 
A2 = 2;
B2 = 1.1; 
P1[1] = 2; P1[2] = Max(A1*round(P1[1]*B1/A1),P1[1]+A1); P1[3] = Max(A1*round(P1[2]*B1/A1),P1[2]+A1); P1[4] = Max(A1*round(P1[3]*B1/A1),P1[3]+A1); P1[5] = Max(A1*round(P1[4]*B1/A1),P1[4]+A1); P1[6] = Max(A1*round(P1[5]*B1/A1),P1[5]+A1); P1[7] = Max(A1*round(P1[6]*B1/A1),P1[6]+A1); P1[8] = Max(A1*round(P1[7]*B1/A1),P1[7]+A1); P1[9] = Max(A1*round(P1[8]*B1/A1),P1[8]+A1); P1[10] = Max(A1*round(P1[9]*B1/A1),P1[9]+A1); P1[11] = Max(A1*round(P1[10]*B1/A1),P1[10]+A1); P1[12] = Max(A1*round(P1[11]*B1/A1),P1[11]+A1); P1[13] = Max(A1*round(P1[12]*B1/A1),P1[12]+A1); P1[14] = Max(A1*round(P1[13]*B1/A1),P1[13]+A1); P1[15] = Max(A1*round(P1[14]*B1/A1),P1[14]+A1); P1[16] = Max(A1*round(P1[15]*B1/A1),P1[15]+A1); P1[17] = Max(A1*round(P1[16]*B1/A1),P1[16]+A1); P1[18] = Max(A1*round(P1[17]*B1/A1),P1[17]+A1); P1[19] = Max(A1*round(P1[18]*B1/A1),P1[18]+A1); P1[20] = Max(A1*round(P1[19]*B1/A1),P1[19]+A1); P1[21] = Max(A1*round(P1[20]*B1/A1),P1[20]+A1); P1[22] = Max(A1*round(P1[21]*B1/A1),P1[21]+A1); P1[23] = Max(A1*round(P1[22]*B1/A1),P1[22]+A1); P1[24] = Max(A1*round(P1[23]*B1/A1),P1[23]+A1); P1[25] = Max(A1*round(P1[24]*B1/A1),P1[24]+A1);
P2[1] = 2; P2[2] = Max(A2*round(P2[1]*B2/A2),P2[1]+A2); P2[3] = Max(A2*round(P2[2]*B2/A2),P2[2]+A2); P2[4] = Max(A2*round(P2[3]*B2/A2),P2[3]+A2); P2[5] = Max(A2*round(P2[4]*B2/A2),P2[4]+A2); P2[6] = Max(A2*round(P2[5]*B2/A2),P2[5]+A2); P2[7] = Max(A2*round(P2[6]*B2/A2),P2[6]+A2); P2[8] = Max(A2*round(P2[7]*B2/A2),P2[7]+A2); P2[9] = Max(A2*round(P2[8]*B2/A2),P2[8]+A2); P2[10] = Max(A2*round(P2[9]*B2/A2),P2[9]+A2); P2[11] = Max(A2*round(P2[10]*B2/A2),P2[10]+A2); P2[12] = Max(A2*round(P2[11]*B2/A2),P2[11]+A2); P2[13] = Max(A2*round(P2[12]*B2/A2),P2[12]+A2); P2[14] = Max(A2*round(P2[13]*B2/A2),P2[13]+A2); P2[15] = Max(A2*round(P2[14]*B2/A2),P2[14]+A2); P2[16] = Max(A2*round(P2[15]*B2/A2),P2[15]+A2); P2[17] = Max(A2*round(P2[16]*B2/A2),P2[16]+A2); P2[18] = Max(A2*round(P2[17]*B2/A2),P2[17]+A2); P2[19] = Max(A2*round(P2[18]*B2/A2),P2[18]+A2); P2[20] = Max(A2*round(P2[19]*B2/A2),P2[19]+A2); P2[21] = Max(A2*round(P2[20]*B2/A2),P2[20]+A2); P2[22] = Max(A2*round(P2[21]*B2/A2),P2[21]+A2); P2[23] = Max(A2*round(P2[22]*B2/A2),P2[22]+A2); P2[24] = Max(A2*round(P2[23]*B2/A2),P2[23]+A2); P2[25] = Max(A2*round(P2[24]*B2/A2),P2[24]+A2);

V1             = P1[Optimize("P1", 1, 1, 25, 1)];
V2             = P2[Optimize("P2", 1, 1, 25, 1)];

Buy            = Name() == "$SPX" AND Close > MA(Close, V1);
Sell           = Close < MA(Close, V2);
BuyPrice       = Close;
SellPrice      = Close;

So, every value depends on the previous value of each series:

 P1[i] = Max(A1*round(P1[i-1]*B1/A1),P1[i-1]+A1)
 P2[i] = Max(A2*round(P2[i-1]*B2/A2),P2[i-1]+A2)

In the previous code I have write the formula for all the values in the series, which is not very practical. I was wondering how to do that with a loop. I have read various entries about it, including this one https://www.amibroker.com/guide/keyword/for.html, but I have always found examples related with looping through bars. However I would need to do something like this:

i = 2;
for(i = 2; i < 25; i = i+1)
{
    P1[i] = Max(A1*round(P1[i-1]*B1/A1),P1[i-1]+A1);
    P2[i] = Max(A2*round(P2[i-1]*B2/A2),P2[i-1]+A2);
}

where i is not related to bars, but to the position of the value in the series.

Is it possible to do something like that or I'm totally wrong?

Thanks beforehand for your help.

Regards.

Custom arrays being independent from length of AB's standard 1-dim arrays... see Matrix functions.

/// https://forum.amibroker.com/t/looping-for-optimizing-a-custom-series-of-values/22951
SetTradeDelays(0, 0, 0, 0);
SetBacktestMode(backtestRegular);

A1 = 1;
B1 = 1.1; 
A2 = 2;
B2 = 1.1;

periods = Matrix(25, 2, 2);
rownum = MxGetSize(periods, 0);
for(i = 1; i < rownum; i++)
{
	periods[i][0] = Max(A1*round(periods[i-1][0]*B1/A1),periods[i-1][0]+A1);
	periods[i][1] = Max(A2*round(periods[i-1][1]*B2/A2),periods[i-1][1]+A2);
}

MxToString(periods);

idx1 = Optimize("P1", 0, 0, rownum-1, 1);
idx2 = Optimize("P2", 0, 0, rownum-1, 1);

V1 = Max(1,periods[idx1][0]);
V2 = Max(1,periods[idx2][1]);

Buy            = Name() == "$SPX" AND Close > MA(Close, V1);
Sell           = Close < MA(Close, V2);
BuyPrice       = Close;
SellPrice      = Close;

fxshrat,

Thanks for your answer, that's exactly what I needed!

Regards.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.