Coding Help: MTF Problem

 

Hi guys,

First of all, thank you to those who help beginners to solve their problems and I appreciate if you help me with this problem.


This code is part of my personal indicator and my problem is that the arrow #1 appear after candle closed and after reload of the indicator, arrow #2 appear too and the arrow #1 still stays.

I don't know what is incorrect on my code
I want the arrow #1 does not appear, and arrow #2 will appear after the current candle closes.

chart period is M1 and indicator calculation is M5.

thank in advance 


    int limit;
    int counted_bars = IndicatorCounted();
    datetime tfM5[];
    int j = 1;
    if (counted_bars < 0)
        return;
    if (counted_bars > 0)
        counted_bars--;
    limit = MathMax(Bars - counted_bars - 1, 0);
    
    ArrayCopySeries(tfM5, MODE_TIME, Symbol(), PERIOD_M5);
    
    for (int i = 1; i < limit; i++)
    {
        if (j < ArraySize(tfM5) && Time[i] < tfM5[j])
            j++;
            
        value1 = iRSI(NULL, PERIOD_M5, 14, 0, j);
        value2 = iRSI(NULL, PERIOD_M5, 14, 0, j + 1);
        
        if (value1 > 30 && value2 < 30 && value1 > value2)
        {
            int y = iBarShift(NULL, PERIOD_M5, Time[i]);
            int x = iBarShift(NULL, PERIOD_M5, Time[i + 1]);
            if (x != y)
            {
                CrossUp[i] = Low[i] - iATR(NULL, 0, 15, i) * ArrowDistance;
            }
            else
            {
                CrossUp[i] = EMPTY_VALUE;
            }
        }
        
        if (value1 < 70 && value2 > 70 && value1 < value2)
        {
            int y = iBarShift(NULL, PERIOD_M5, Time[i]);
            int x = iBarShift(NULL, PERIOD_M5, Time[i + 1]);
            if (x != y)
            {
                CrossDown[i] = High[i] + iATR(NULL, 0, 15, i) * ArrowDistance;
            }
            else
            {
                CrossDown[i] = EMPTY_VALUE;
            }
        }
    }


MTF Problem

 
Bahar Hajizadeh:

This code is part of my personal indicator and my problem is that the arrow #1 appear after candle closed and after reload of the indicator, arrow #2 appear too and the arrow #1 still stays.

I don't know what is incorrect on my code
I want the arrow #1 does not appear, and arrow #2 will appear after the current candle closes.

chart period is M1 and indicator calculation is M5.

I made some assumptions on how you declared all your buffers, indexes and variables, and content of OnInit, and I was able to run without the problem mentioned by you.

As such, may I suggest that you post your complete code?

 
Bahar Hajizadeh: my problem is that the arrow #1 appear after candle closed and after reload of the indicator, arrow #2 appear too and the arrow #1 still stays.

I don't know what is incorrect on my code

    if (counted_bars > 0)
        counted_bars--;
    limit = MathMax(Bars - counted_bars - 1, 0);
    
    ArrayCopySeries(tfM5, MODE_TIME, Symbol(), PERIOD_M5);
    
    for (int i = 1; i < limit; i++)
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. You don't process bar zero of the current time frame, or reprocess all bars of the bar zero of the source time frame. See How to do your lookbacks correctly № 14.

  3. No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You should stop using the old event handlers and IndicatorCounted and start using the new ones.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  5. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - Forex Calendar - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3 № 26

 
William Roeder:
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. You don't process bar zero of the current time frame, or reprocess all bars of the bar zero of the source time frame. See How to do your lookbacks correctly № 14.

  3. No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. You should stop using the old event handlers and IndicatorCounted and start using the new ones.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  5. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - Forex Calendar - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3 № 26

Dear William Roeder,

sorry for create post in wrong section, next time will be correct place.

I fixed that as you told me above,

another question : is that possible to call rates_total and prev_calculated and etc like OnCalculate parameters in my custom function?


int OnCalculate(const int rates_total,       // the size of the input timeseries
                const int prev_calculated,   // bars processed at the previous call
                const datetime& time[],      // Time
                const double& open[],        // Open
                const double& high[],        // High
                const double& low[],         // Low
                const double& close[],       // Close
                const long& tick_volume[],   // Tick Volume
                const long& volume[],        // Real Volume
                const int& spread[]          // Spread
                )
{
    if (!download_history(TimeFrame))
        return prev_calculated;
    const int lookback = TimeFrame / _Period;
    int       iBar     = rates_total - MathMax(lookback, prev_calculated);
    int       iLast    = iBar;
    while (iBar > 1)
    {
        --iBar;
        int iTF = iBarShift(NULL, TimeFrame, Time[iBar]);
        if (iTF != 0)
            iLast = iBar;
        value1 = iRSI(NULL, TimeFrame, period, 0, iTF);
        value2 = iRSI(NULL, TimeFrame, period, 0, iTF + 1);
        if (value1 > Oversell && value2 < Oversell && value1 > value2)
        {
            int y = iBarShift(NULL, TimeFrame, Time[iBar]);
            int x = iBarShift(NULL, TimeFrame, Time[iBar + 1]);
            if (x != y)
            {
                CrossUp[iBar] = Low[iBar] - iATR(NULL, 0, 15, iBar) * ArrowDistance;
            }
            else
            {
                CrossUp[iBar] = EMPTY_VALUE;
            }
        }
        if (value1 < Overbought && value2 > Overbought && value1 < value2)
        {
            int y = iBarShift(NULL, TimeFrame, Time[iBar]);
            int x = iBarShift(NULL, TimeFrame, Time[iBar + 1]);
            if (x != y)
            {
                CrossDown[iBar] = High[iBar] + iATR(NULL, 0, 15, iBar) * ArrowDistance;
            }
            else
            {
                CrossDown[iBar] = EMPTY_VALUE;
            }
        }
    }
   #define  REDRAW_BAR_LAST    false
    return rates_total - REDRAW_BAR_LAST - iLast;
}


// Is That Possible? I mean something like this

void MyFunction(const int rates_total,       // the size of the input timeseries
                const int prev_calculated,   // bars processed at the previous call
                const datetime& time[],      // Time
                const double& open[],        // Open
                const double& high[],        // High
                const double& low[],         // Low
                const double& close[],       // Close
                const long& tick_volume[],   // Tick Volume
                const long& volume[],        // Real Volume
                const int& spread[]          // Spread
                )
{
    if (!download_history(TimeFrame))
        return prev_calculated;
    const int lookback = TimeFrame / _Period;
    int       iBar     = rates_total - MathMax(lookback, prev_calculated);
    int       iLast    = iBar;
    while (iBar > 1)
    {
        --iBar;
        int iTF = iBarShift(NULL, TimeFrame, Time[iBar]);
        if (iTF != 0)
            iLast = iBar;
        value1 = iRSI(NULL, TimeFrame, period, 0, iTF);
        value2 = iRSI(NULL, TimeFrame, period, 0, iTF + 1);
        if (value1 > Oversell && value2 < Oversell && value1 > value2)
        {
            int y = iBarShift(NULL, TimeFrame, Time[iBar]);
            int x = iBarShift(NULL, TimeFrame, Time[iBar + 1]);
            if (x != y)
            {
                CrossUp[iBar] = Low[iBar] - iATR(NULL, 0, 15, iBar) * ArrowDistance;
            }
            else
            {
                CrossUp[iBar] = EMPTY_VALUE;
            }
        }
        if (value1 < Overbought && value2 > Overbought && value1 < value2)
        {
            int y = iBarShift(NULL, TimeFrame, Time[iBar]);
            int x = iBarShift(NULL, TimeFrame, Time[iBar + 1]);
            if (x != y)
            {
                CrossDown[iBar] = High[iBar] + iATR(NULL, 0, 15, iBar) * ArrowDistance;
            }
            else
            {
                CrossDown[iBar] = EMPTY_VALUE;
            }
        }
    }
   #define  REDRAW_BAR_LAST    false
    return rates_total - REDRAW_BAR_LAST - iLast;
}
 
Bahar Hajizadeh: another question : is that possible to call rates_total and prev_calculated and etc like OnCalculate parameters in my custom function?

They only exist in OnCalculate. Pass them

int OnCalculate(const int rates_total,       // the size of the input timeseries
                const int prev_calculated,   // bars processed at the previous call
                const datetime& time[],      // Time
                const double& open[],        // Open
                const double& high[],        // High
                const double& low[],         // Low
                const double& close[],       // Close
                const long& tick_volume[],   // Tick Volume
                const long& volume[],        // Real Volume
                const int& spread[]          // Spread
                )
{
   return MyFunction(rates_total, prev_calculated);
}
Reason: