Confirm on next Timeframe

 

Hi guys,


I'm a newbie on MQL4 so I would like to apolagize in advance for my lack of experience and my laughable code.

So, I have this function:

bool NextTimeFrame()
{
    // Next Timeframe Confirmation
    if(useconfirmation == false)
        return(true);

    int TimeFrames[10] = { 1,5,15,30,60,240,1440,10080,43200,43200 };

    for(int i = 0; i < 9; i++)
    {
        if(TimeFrames[i] == Period()) return(TimeFrames[i+1]);
        if(MarketInfo(Symbol(), MODE_BID) < iBands(NULL, TimeFrames[i], 34, 2, 0, PRICE_CLOSE, 2, 0)
        || MarketInfo(Symbol(), MODE_BID) > iBands(NULL, TimeFrames[i], 34, 2, 0, PRICE_CLOSE, 1, 0))
            return(true);
    }

    return(true);
}

Initially I was using this function to check if the candles were closed above the upper 2.0 BB deviation or below the lower 2.0 BB deviation on the next timeframe. However, it occured to me that it wouldn't be possible as the candles are still being formed on higher timeframes, so instead I decided to go with the bid price, to check the current position within the candle...
Now the issue is that Bollinger Bands are formed after the candle closed, so I'm back in the same spot... Is there anything I can do about this, or should I just forget it?

 
Darkz0r:

Hi guys,


I'm a newbie on MQL4 so I would like to apolagize in advance for my lack of experience and my laughable code.

So, I have this function:

Initially I was using this function to check if the candles were closed above the upper 2.0 BB deviation or below the lower 2.0 BB deviation on the next timeframe. However, it occured to me that it wouldn't be possible as the candles are still being formed on higher timeframes, so instead I decided to go with the bid price, to check the current position within the candle...
Now the issue is that Bollinger Bands are formed after the candle closed, so I'm back in the same spot... Is there anything I can do about this, or should I just forget it?

1) Your function checks all timeframes (not the next timeframe)

2) You have to decide if you want to check closed bar or current bar (Bid)

 
Petr Nosek:

1) Your function checks all timeframes (not the next timeframe)

2) You have to decide if you want to check closed bar or current bar (Bid)

1) Yes, I will have to place the condition outside the function and call it from there. 

2) Ok, let's say the current bar on M1 just closed on BB 3.0 at 08:33.
By logic, this would mean that on M5 this same bar is still being formed until 08:35 so I can not check for its closure just yet, instead I have to rely on bid price and get the current position of the bar.

However, if the bar hasn't closed on M5 yet there will be no Bollinger Bands, correct? 
Which means that comparing if the bid price is above or below BB is impossible. 
 
Darkz0r:

1) Yes, I will have to place the condition outside the function and call it from there. 

2) Ok, let's say the current bar on M1 just closed on BB 3.0 at 08:33.
By logic, this would mean that on M5 this same bar is still being formed until 08:35 so I can not check for its closure just yet, instead I have to rely on bid price and get the current position of the bar.


Yes, you are right.


However, if the bar hasn't closed on M5 yet there will be no Bollinger Bands, correct? 

Which means that comparing if the bid price is above or below BB is impossible. 

No, you are wrong. There are BB but they don't have the final value (just like the bar). But you can compare if the Bid is above or below BB.

 

Hmm... 


But could I still use it like:

if(iClose(NULL, PERIOD_M5, 0) < iBands(NULL, PERIOD_M5, 34, 2, 0, PRICE_CLOSE, 2, 0)



Because both "iClose" and "PRICE_CLOSE" refer to BAR closure... But since it is still moving how does it work exactly? Will it take the last position of the bar and the BB? Or will it simply wait for the bar and BB to close? 

 
2018/01/30 01:14:37 Completed #2857855 aqib786100
 
2018/01/30 01:17:40 Completed #200239 jprizain
 

Ok guys, I see it now, thank you so much!

One more question related to the subject.
Let's say my indicator draws an arrow and sends an alert if the current bar closes at 2.5 BB on M1 and if the current bid price is above BB 2.0 on M5.

Now if I go back through chart history, will I be able to see past arrows drawn on the price chart?
I'm asking because the Arrow is only drawn if the current bid price is above BB 2.0 on M5, but since the bid price is always changing I'm curious to know how this would play out.

Unless the bid price at the time is saved somehow I guess I would only receive the alert, right?
 
Darkz0r:

Ok guys, I see it now, thank you so much!

One more question related to the subject.
Let's say my indicator draws an arrow and sends an alert if the current bar closes at 2.5 BB on M1 and if the current bid price is above BB 2.0 on M5.

Now if I go back through chart history, will I be able to see past arrows drawn on the price chart?
I'm asking because the Arrow is only drawn if the current bid price is above BB 2.0 on M5, but since the bid price is always changing I'm curious to know how this would play out.

Unless the bid price at the time is saved somehow I guess I would only receive the alert, right?
If the candle closes above/below BB, the arrow should remain on the chart. But it depends on your code ;-)
 
Petr Nosek:
If the candle closes above/below BB, the arrow should remain on the chart. But it depends on your code ;-)

Got it... so perhaps it has something to do with my code indeed...

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[])
{
    if (!ValidateParameters())
        return(0);

    int bars = rates_total;
    if(prev_calculated > 0)
    {
        bars = rates_total - prev_calculated;
    }

    CalculateIndicator(bars, rates_total);

    return(rates_total);
}

void CalculateIndicator(int bars, int rates_total)
{
    int outOfRangeBuffer = 5;
    int maxBarBack;

    if (bars < rates_total - outOfRangeBuffer)
        maxBarBack = bars;
    else
        maxBarBack = bars - (outOfRangeBuffer - (rates_total - bars));

    for(int i = maxBarBack; i >= 0; i--)
    {
        CalculateRSI(i);
        CatchBearishDivergence(i + 1);
    }
}



void CalculateRSI(int i)
{
    rsi[i] = iRSI(NULL, 0, period, PRICE_CLOSE, i);             
}

void CatchBearishDivergence(int shift)
{
    if(IsIndicatorPeak(shift) == false)
        return;

    int currentPeak = shift;
    int lastPeak = GetIndicatorLastPeak(shift);

    if(rsi[currentPeak] < rsi[lastPeak]
    && High[currentPeak] > High[lastPeak]
    && MarketInfo(Symbol(), MODE_BID) >= iBands(NULL, NextTimeFrame(), 34, 2, 0, PRICE_CLOSE, 1, 0))
    {
        bearishDivergence[currentPeak] = rsi[currentPeak] + arrowsDisplacement;
        divergencesRSIDiff[currentPeak] = MathAbs(rsi[currentPeak] - rsi[lastPeak]);
        divergencesPriceDiff[currentPeak] = MathAbs(High[currentPeak] - High[lastPeak]);
    }
}

int NextTimeFrame()
{
    int TimeFrames[10] = { 1,5,15,30,60,240,1440,10080,43200,43200 };

    for(int i = 0; i < 9; i++)
        if(TimeFrames[i] == Period()) return(TimeFrames[i+1]);

    return(false);
}

If I change the shift of the iBands() to "currentPeak" the arrows show up, but it seems that is the shift of the current timeframe (M1) and not M5, so that doesn't work.
Also, if I leave it at 0, which is the value that is on my chart, then I get no arrows at all...
 
Darkz0r:

Got it... so perhaps it has something to do with my code indeed...


If I change the shift of the iBands() to "currentPeak" the arrows show up, but it seems that is the shift of the current timeframe (M1) and not M5, so that doesn't work.
Also, if I leave it at 0, which is the value that is on my chart, then I get no arrows at all...

I think your problem is in the highlighted line:

    if(rsi[currentPeak] < rsi[lastPeak]
    && High[currentPeak] > High[lastPeak]
    && MarketInfo(Symbol(), MODE_BID) >= iBands(NULL, NextTimeFrame(), 34, 2, 0, PRICE_CLOSE, 1, 0))

Maybe there should be something like that:

    int shiftNextTimeFrame=iBarShift(_Symbol,NextTimeFrame(),iTime(_Symbol,_Period,shift));

    if(rsi[currentPeak] < rsi[lastPeak]
    && High[currentPeak] > High[lastPeak]
    && iClose(_Symbol,NextTimeFrame(),shiftNextTimeFrame) >= iBands(NULL, NextTimeFrame(), 34, 2, 0, PRICE_CLOSE, 1, shiftNextTimeFrame))

I hope it helps.