interesting indicator problem

 

Hi Guys,

I'm trying to learn MT4 programming, and this very moment I'm developing an PinBar detection indicator, which tells me to buy or sell. I developed this indicator especially for the 4 hour chart, because of the special rules for buy or sell. The rule for detection is as follows:

Body of candle <= 35% of candlelength.
At least 1 wick of candle >= 50 % of candlelength.
If this bith is true, check the four candles on the 1 hour chart which created the pinbar on the 4 hour chart and check the closes. If last 1 hour closes higher as first bar close, we buy, else we sell.

So far I have no problems detecting those bars with the indicator.
However, the next rule tells me to buy or sell.
I zoom in to the 1hour chart and look at the 4 bars that created the pinbar on the 4 hour chart.
I used the iClose function to pickup the value of the close of the first and the last bar of that formation on the 1 hour bar.
However..as programmed now, I always look at the first and last candle on the 1hour chart itself. And that is not ok. It has to be the first and last bar of the formation and not of the chart.

Here is the code what I now have with the wrong iClose functions in it.

if ( (((100.0/CandleLength)*BodyLength)<=35) && ( (((100.0/CandleLength)*UpperWick)>=50.0) || (((100.0/CandleLength)*LowerWick)>=50.0) ) [B][I]&& iClose(Symbol(),60,1) > iClose(Symbol(),60,4)[/I][/B])
      {
         Buy[i] = Close[i+1];
         SetLevel(true,i+1,Close[i+1]);
      }

I haven't been able to pin down the right 1 hour candles in the background on that particular 4hour candle.
Is someone out there that can give me the answer to my problem?

tx very much!

Jacob

Files:
pinbar_v1.mq4  4 kb
 

You need to identify the correct bar number for the first H1 bar . . . something like this . . .

datetime H4BarTime;
int H1BarNumber;

H4BarTime = Time[i+1];

H1BarNumber = iBarShift(NULL, PERIOD_H1, H4BarTime);   //  the bar number of the first H1 bar that makes up the H4 bar that is at (i+1)
 
Jonkie76: If last 1 hour closes higher as first bar close, we buy, else we sell.

.. pickup the value of the close of the first and the last bar of that formation on the 1 hour bar.

You will not need the other previous then last on 1 hour chart bars as the strategy you mentioned, but you need to check the correctness of the formation by the 4 hours chart.

datetime _oldtime=iTime(NULL,240,0);
for (int _shift=0; _shift<4; _shift++ ) 
 {
  if ( _oldtime > iTime(NULL,60,_shift) ) break;
// ... calculations ...

But if you need only the last bar of the formation on 1 hour chart when it occurred

you need to reverse it and replace the break

  if ( _oldtime > iTime(NULL,60,_shift) ) 
   {
    for (int _countS=(_shift+1); _countS<(_shift+1+4); _countS++ )
     {
      //... calculations... OHLC

But, if you go from beginning of formation of bars to the last ( [0] ) you will not need the above code, just

for (int _index=_maximum; _index>-1; _index-- )
 {
  datetime _H4barShiftTime=iTime(NULL,240,_someIndex),
   _H4barNewTime=iTime(NULL,240,_someIndex + 1 ),
   _H1barTime = iTime(NULL,60,_index);
  if ( _H1barTime >= _H4barShiftTime && _H1barTime < _H4barNewTime )
   {
    // OHLC calculation ...
 

tx for the info guys, I will look into that, and will let ya know ..

 

OK, Raptor, thank you for the solution!

It works just fine :

datetime H4BarTime;
int H1BarNumber;

H4BarTime = Time[i+1];

H1BarNumber = iBarShift(NULL, PERIOD_H1, H4BarTime); // the bar number of the first H1 bar that makes up the H4 bar that is at (i+1)