Series, shifting and timeframes

Posted By: NorbertSz

Series, shifting and timeframes - 09/06/22 18:03

Dear all,

Could you help me understand what am I missing?

Code
function run(){

    BarPeriod = 1;
    LookBack = 240*100;
    StartDate = 20100101;
    EndDate = 20100201;

    set(NOSHIFT);           //shift manually
    asset("EURUSD");
    
    vars priceM1 = series();
    vars priceM5 = series();

    if (is(INITRUN)){
        priceM1 = series(0, 100);
        priceM5 = series(0, 100);
    }
    
    TimeFrame = 1;
    shift(priceM1, priceClose(), 100);
    TimeFrame = 5;
    if (frame(0)) shift(priceM5, priceClose(), 100);

    if (!is(LOOKBACK)){
        int i, j;
        for (i=0; i<10; i++){
            printf("\n--- M5: %.5f", priceM5[i]);
            for (j=0; j<5; j++){
                printf("\n M1: %.5f", priceM1[i*5+j]);
            }
        }
        quit(0);
    }
}


I attached the results.
How is it possible that the M1 middle candle is determining the closing price of the M5 candle? Shouldn't it be the last one?
The expected result are the following:

Code
--- M5 priceE
M1 priceA
M1 priceB
M1 priceC
M1 priceD
M1 priceE
...


Thank you!

Attached picture middle_candle.png
Posted By: NorbertSz

Re: Series, shifting and timeframes - 09/06/22 18:19

I did the custom shifting because the automatic shifting (as suggested in the example codes in TimeFrame help) did exactly the same. And because I did not understand why it does, I tried to "implement", but same results. So the following code, the automatic shifting acts just exactly the previously posted:

Code
function run(){

    BarPeriod = 1;
    LookBack = 240*100;
    StartDate = 20100101;
    EndDate = 20100201;

    asset("EURUSD");

    TimeFrame = 1;
    vars priceM1 = series(priceClose(), 100);
    TimeFrame = 5;
    vars priceM5 = series(priceClose(), 100);

    if (!is(LOOKBACK)){
        int i, j;
        for (i=0; i<10; i++){
            printf("\n--- M5: %.5f", priceM5[i]);
            for (j=0; j<5; j++){
                printf("\n M1: %.5f", priceM1[i*5+j]);
            }
        }
        quit(0);
    }
}
Posted By: AndrewAMD

Re: Series, shifting and timeframes - 09/06/22 20:00

Your if statement with the loops did not check for frame(0).
Posted By: NorbertSz

Re: Series, shifting and timeframes - 09/07/22 05:46

Ahh I guess I understand now.
If I just print the prices as the come:

Code
function run(){
    BarPeriod = 1;
    LookBack = 240*100;
    StartDate = 20100101;
    EndDate = 20100201;

    asset("EURUSD");

    TimeFrame = 1;
    printf("\nM1: %.5f", priceClose());
    TimeFrame = 5;
    if (frame(0)) printf("\n --- M5: %.5f", priceClose());
}


The results are the expected: the last M1 candle closePrice() is the same that M5 candle closePrice(), as seen in the first attachment of this post. Now I understand that if the LookBack finishes not exactly in TimeFrame 5 frame(0) then the whole M1 series will be shifted, because it got new elements but the M5 series did not. Attachment.

One possible solution for printing is shifting the data inside the series:

Code
function run(){

    BarPeriod = 1;
    LookBack = 240*100;
    StartDate = 20100101;
    EndDate = 20100201;

    asset("EURUSD");

    TimeFrame = 1;
    vars priceM1 = series(priceClose(), 100);
    //printf("\nM1: %.5f", priceClose());
    TimeFrame = 5;
    vars priceM5 = series(priceClose(), 100);
    //if (frame(0)) printf("\n --- M5: %.5f", priceClose());

    if (!is(LOOKBACK)){

        //shift some data
        int i, j, shift;
        TimeFrame = 5;
        for (i=0; i<4; i++){
            if (frame(i)) shift = i + 1;
        }

        //print
        for (i=0; i<10; i++){
            printf("\n--- M5: %.5f", priceM5[i]);
            for (j=0; j<5; j++){
                printf("\n M1: %.5f", priceM1[shift + i*5 + j]);
            }
        }
        quit(0);
    }
}


Thank you!

Attached picture end_candle.png
Attached picture shifting.png
© 2024 lite-C Forums