Download MetaTrader 5

(CopyBuffer) Get refreshed information from ZigZag Indicator?

To add comments, please log in or register
You sell neither signals nor apps? No problem, everyone can make profit!
Arturo Lopez Perez
161445
Arturo Lopez Perez 2013.08.21 19:32 

Good afternoon,

I come from MQL4 and I am trying to obtain refreshed information from the Zig Zag Indicator and the following code works when the indicator is just loaded to the chart and iterates past bars.

    // Iteration limit
    int start = 1;    
    int limit;

    // If no data
    if(rates_total < 30) return(0);
   
    // Calculate limit and reset buffers
    if(prev_calculated > rates_total || prev_calculated <= 0)
    {
        limit=rates_total-30;
        ArrayInitialize(ExtMapBuffer1, EMPTY_VALUE);
        ArrayInitialize(ExtMapBuffer2, EMPTY_VALUE);
    } else {
        limit=rates_total-prev_calculated;
    }

    //--
    //-- ZigZag Values
    //-- When just loaded to the chart, refreshed zig zag information arrives
    //-- When testing, limit is always 1 and I don't get refreshed information
    if(CopyBuffer(ZigZagHandle, 0, 0, limit+1, ZigZagBuffer) != limit+1) return(0);

However, in the tester (when Limit and Iterator both are 1) it does no extract updated information and therefore my indicator does not do what it should.

Could you be kind enough to let me know what am I missing or send me to an example? I have tried several things without success.

(https://www.mql5.com/en/docs/series/copybuffer)

Thanks!

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
Timeseries and Indicators Access / CopyBuffer - Documentation on MQL5
Andrey Barinov
39238
Andrey Barinov 2013.08.21 20:25  
PzTrading:

Good afternoon,

I come from MQL4 and I am trying to obtain refreshed information from the Zig Zag Indicator and the following code works when the indicator is just loaded to the chart and iterates past bars.

However, in the tester (when Limit and Iterator both are 1) it does no extract updated information and therefore my indicator does not do what it should.

Could you be kind enough to let me know what am I missing or send me to an example? I have tried several things without success.

(https://www.mql5.com/en/docs/series/copybuffer)

Thanks!

Looks like in your case limit is ==0, not 1. To make it 1 it should be limit=rates_total-prev_calculated+1;
Alain Verleyen
Moderator
30707
Alain Verleyen 2013.08.21 20:32  
PzTrading:

Good afternoon,

I come from MQL4 and I am trying to obtain refreshed information from the Zig Zag Indicator and the following code works when the indicator is just loaded to the chart and iterates past bars.

However, in the tester (when Limit and Iterator both are 1) it does no extract updated information and therefore my indicator does not do what it should.

Could you be kind enough to let me know what am I missing or send me to an example? I have tried several things without success.

(https://www.mql5.com/en/docs/series/copybuffer)

Thanks!

What is "Iterator" ? What result are you expecting ?

We can't help with information you provide. It's probable useful to post full code and/or to explain what your indicator is supposed to do.

Arturo Lopez Perez
161445
Arturo Lopez Perez 2013.08.21 20:45  
Wahoo:
Looks like in your case limit is ==0, not 1. To make it 1 it should be limit=rates_total-prev_calculated+1;

Hi Wahoo,

Thanks! But I still have the same problem. If I dump the latest X values from the ZigZagBuffer, they are not a zigzag! :(

I am missing the past "recalculated" values the Zig Zag Indicator assigns. In other words, I need the "repainted" data, not the bar by bar data.

By using the overload of CopyBuffer from date1 to date2 forcing date1 to be zero works, but it is super slow.

Andrey Barinov
39238
Andrey Barinov 2013.08.21 20:47  
PzTrading:

Hi Wahoo,

Thanks! But I still have the same problem. If I dump the latest X values from the ZigZagBuffer, they are not a zigzag! :(

well, in this case we really need to see your code.

How do you get ZigZag handle? What you mean saying that it is not a zigzag?

Arturo Lopez Perez
161445
Arturo Lopez Perez 2013.08.21 20:53  
Wahoo:

well, in this case we really need to see your code.

How do you get ZigZag handle? What you mean saying that it is not a zigzag?

OK. I paste the important parts.

//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 4

//---- iCustom Buffers and Handles
double ZigZagBuffer[];
int    ZigZagHandle;

int OnInit()
{
    // ... Stuff here 

    // Zig Zag Buffer (it is the third)
    SetIndexBuffer(3, ZigZagBuffer, INDICATOR_CALCULATIONS);
    ArraySetAsSeries(ZigZagBuffer, true);

    // Zig Zag Handle
    ZigZagHandle = iCustom(_Symbol, 0, "Examples\\ZigZag", Amplitude, ZZBack, ZZDev);
    if(ZigZagHandle == INVALID_HANDLE) return(INIT_FAILED);

    // ... More Stuff here

    // Ok!
    return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    ArraySetAsSeries(close,true);
    ArraySetAsSeries(high,true);
    ArraySetAsSeries(low,true);
    ArraySetAsSeries(time,true);

    // Iteration limit
    int start = 1;    
    int limit;

    // If no data
    if(rates_total < 30) return(0);
   
    // Calculate limit and reset buffers
    if(prev_calculated > rates_total || prev_calculated <= 0)
    {
        limit=rates_total-30;
        ArrayInitialize(ExtMapBuffer1, EMPTY_VALUE);
        ArrayInitialize(ExtMapBuffer2, EMPTY_VALUE);
    } else {
        limit=rates_total-prev_calculated+1;
    }
    
    // Read Zig Zag Buffer
    if(CopyBuffer(ZigZagHandle, 0, 0, limit+1, ZigZagBuffer) == 0) return(0);

    //-- Iteration
    for(int i = limit; i >= start; i--)
    {
        // The Zig Zag Buffer is not holding updated data in tester or real trading. No Zig Zag! :-D  
    }
}

I want to clarify that ZigZagBuffer holds Zig-Zag data when iterating past bars. But not in the tester nor in live usage.

I would have to increase the amount of records copied from the Zig Zag Indicator, but it is a very slow solution, it almost freezes my VM.

Andrey Barinov
39238
Andrey Barinov 2013.08.21 20:58  

Well, why do you expect it to have renewed data if you are copying only one single bar's value, which is the latest one?

To have it renewed you need to copy more bars... Otherwise they won't be updated.

Arturo Lopez Perez
161445
Arturo Lopez Perez 2013.08.21 21:02  
Wahoo:

Well, why do you expect it to have renewed data if you are copying only one single bar's value, which is the latest one?

To have it renewed you need to copy more bars... Otherwise they won't be updated.

I know Wahoo. I tried by timestamps and it is reaaaally slow. I only access that data eventually, perhaps once every 20 or 30 bars. Is there a more elegant solution than copying a lot of data from one array to another on each execution? On MT4, for example, you can call iCustom() only when you need it, without having to copy the buffer partially every single iteration. Rather than copying the whole buffer every iteration, I would like to access one of the zig-zag values every once in a while.
Alain Verleyen
Moderator
30707
Alain Verleyen 2013.08.21 21:04  
PzTrading:

OK. I paste the important parts.

I want to clarify that ZigZagBuffer holds Zig-Zag data when iterating past bars. But not in the tester nor in live usage.

I would have to increase the amount of records copied from the Zig Zag Indicator, but it is a very slow solution, it almost freezes my VM.

Obviously it's very slow if you are using CopyBuffer with a high count value ON EACH TICK.

Buffer for ZigZag is mainly fill out with EMPTY value, you have to search the values different from EMPTY to get ZigZag high and low.

It's still not clear for me what you are trying to achieve.

Arturo Lopez Perez
161445
Arturo Lopez Perez 2013.08.21 21:40  

Thanks to both of you for your replies,

My problem was that during the testing or live trading, getting Zig Zag values one at a time neglected to capture the re-arranged past values. I have finally found out a nice way to achieve what I wanted. What I did was calling CopyBuffer once every bar from the current time to the time of the third zig zag point found the last time. This makes takes care of the issue grabbing all the refreshed values of the Zig Zag Indicator.

Best regards.

To add comments, please log in or register