CONCATENATE function

I am trying to do the following but could not proceed further , Solution is requested

// Calculate EMA 5 and EMA 20
ema5 = EMA(Close, 5);
ema20 = EMA(Close, 20);

// Concatenate EMA 5 and EMA 20 into a single array
X = Concat(1, ema5, ema20); // Add a constant term to X

let me explain

START
Step 1 → Take three array variables A, E, and O
Step 2 → Store even values in array E
Step 3 → Store odd values in array O
Step 4 → Start loop from 0 to sizeof(E)
Step 5 → Copy E[n] to A[index]
Step 6 → Start loop from 0 to sizeof(O)
Step 7 → Copy E[n] to A[index]
Step 8 → Display A
STOP

The output should look like this −
Even -> 0 2 4 6 8
Odd -> 1 3 5 7 9
Concat -> 0 2 4 6 8 1 3 5 7 9

// Declare the arrays
A = ValueWhen(1, 0);
E = ValueWhen(1, 0);
Oc = ValueWhen(1, 0);

// Fill the arrays with even and odd values
for (i = 0; i < 10; i++)
{
    if (i % 2 == 0)
    {
        E[i / 2] = i;
    }
    else
    {
        Oc[i / 2] = i;
    }
}

// Concatenate the arrays
index = 0;
for (i = 0; i < 5; i++)
{
    A[index++] = E[i];
}
for (i = 0; i < 5; i++)
{
    A[index++] = Oc[i];
}

// Calculate EMA 5 and EMA 20 for the concatenated array
ema5 = EMA(A, 5);
ema20 = EMA(A, 20);

// Concatenate EMA 5 and EMA 20 into a single array
X = concat( 1,ema5, ema20); // Add a constant term to X

// Define the class labels (1 for up trend, 0 for down trend)
Y = IIf(A > Ref(A, -1), 1, 0);

// Fit a linear model to the data using the LinReg function
model = LinearReg(Y, X);

// Extract the coefficients from the model
coefficients = model[0];

// Use the coefficients to predict the trend for the current period
trend = coefficients[0] + coefficients[1] * ema5 + coefficients[2] * ema20;

// Print the arrays
//Print("Even -> ", E);
//Print("Odd -> ", Oc);
//Print("Concat -> ", A);

// Plot the trend on the chart
Plot(trend, "Trend", colorRed, styleLine);

what is the solution?

All arrays in AmiBroker have BarCount bars (items). What you are asking for would require having TWICE as many items in the array.

You did not explained the GOAL you want to achieve (WHY do you need such array)
and is makes it impossible to give you meaningful solution.

Please follow this advice: How to ask a good question

The only way to create 1 - dimensional vector that has size different than BarCount is to use 1-dimensional matrix and use MxSetBlock to fill first part of matrix with EMA5 and second with EMA20

https://www.amibroker.com/guide/afl/matrix.html
https://www.amibroker.com/guide/afl/mxsetblock.html

mx = Matrix( BarCount * 2, 1 );

mx = MxSetBlock( mx, 0, BarCount - 1, 0, 0, MA( C, 5 ) );
mx = MxSetBlock( mx, BarCount, 2 * BarCount - 1, 0, 0, MA( C, 20 ) );

Thank You TJ,

Why i am getting this .
Con Error 1 20 dec22

Sorry, the column index should be 0, not 1.

mx = Matrix( BarCount * 2, 1 );

mx = MxSetBlock( mx, 0, BarCount - 1, 0, 0, MA( C, 5 ) );
mx = MxSetBlock( mx, BarCount, 2 * BarCount - 1, 0, 0, MA( C, 20 ) );
1 Like