3
votes

I want to find the pinbars circled in the attached image below. For the one on the right, the body is about 1/5th of the candle size and for the one on the left, let's say the body is 1/7th the size candle size and it also has a wick of 1/7th the candle size at the bottom. How can I use the iOpen(), iLow() and other similar functions to find these two types of pinbars?

enter image description here

2
Try using ratios of the tails relative to the range of the candle, as well as the ratio of the body of the candle to the tails & the range of the candle.Dean
@Dean thanks though I already solved this using an idea very similar to your recommendation. But I'm still trying to get stackoverflow.com/questions/58139482/… working. I would be really grateful if you could take a look at it for meSuperHueman

2 Answers

1
votes

Q : How can I use the iOpen(), iLow() and other similar functions to find these two types of pinbars?

May use a working template like this WasThisTheUserDefinedBearishPATTERN() function and having defined all your user-specific conditions and constants, just call
if ( WasThisTheUserDefinedBearishPATTERN( aShift, 4.5, 0.1, 2000 ) ) { do_something(); } when using it for trading:

bool WasThisTheUserDefinedBearishPATTERN( const int    aBarNUMBER,
                                          const double aBody2uWICK_MUL,
                                          const double min_FRACTION,
                                          const double MAX_FRACTION
                                          ) {
// BAR-UPPER-wick:
   double aBarUpWK = ( iHigh(  _Symbol, PERIOD_CURRENT, aBarNUMBER )
                     - iClose( _Symbol, PERIOD_CURRENT, aBarNUMBER )
                       );
// BAR-body:
   double aBarBODY = ( iClose( _Symbol, PERIOD_CURRENT, aBarNUMBER )
                     - iOpen(  _Symbol, PERIOD_CURRENT, aBarNUMBER )
                       );
// BAR-LOWER-wick:
   double aBarLoWK = ( iLow(   _Symbol, PERIOD_CURRENT, aBarNUMBER )
                     - iClose( _Symbol, PERIOD_CURRENT, aBarNUMBER )
                       );
// FLAG:
   bool  aBearishFLAG = FALSE;
// -----------------------------------------------------------------
// USER-DEFINE-ABLE SET OF SHAPE-CONDITIONS ..... = add all that apply
// BEARISH BODY:
//              has body
//              has body BEARISH
//              has body not more than about a lo-wick size * < min_FRACTION, MAX_FRACTION >
//              has body not less than about a hi-wick size * _MUL 
// -------------
   if (  aBarBODY >= 0                                      // not BEARISH
      || aBarUpWK <  aBarBODY * aBody2uWICK_MUL             // not long enough UpperWICK
         ){
         aBearishFLAG = FALSE;                              // (NO)
   }
   else { if (        aBarBODY <  0                         // has BEARISH CANDLE
             && (     aBarLoWK == 0                         // and may LO-WICK ZERO
                || (  aBarLoWk / aBarBODY > min_FRACTION    //     .GT. min-WICK-2-BODY-FRACTION
                   && aBarLoWk / aBarBODY < MAX_FRACTION    //     .LT. MAX-WICK-2-BODY-FRACTION
                      )
                  )
                ){  aBearishFLAG = TRUE;  }                 // (YES)
          else {    aBearishFLAG = FALSE; }                 // ( NO) OTHERWISE
   }
   return( aBearishFLAG );
}

Ex-post :

I did WasThisTheUserDefinedBearishPATTERN( aShift, 1, 1/6, 2/6 )
and was expecting it to result in something...

Besides the comment below, kindly also respect The Rules of the World of MQL4 language:

Print( "DIV( 1  / 7  ) == ", 1  / 7  );   // will show you The Rules
Print( "DIV( 1. / 7. ) == ", 1. / 7. );   // will show you The Rules
0
votes

So the way I would approach this is as follows:

1) What are the characteristics of each candle I am trying to define?

a) Down or up (I prefer 1 or -1)

b) Large rejection tail relative to the body

c) Large rejection tail relative to the non-rejection tail

d) Small body relative to the rejection tail

2) How do we code this in MQL4 using predefined functions?

Let's first make a few assumptions...

First, you are wanting to define these candles in real-time so you can execute when they form.

Second, let's go off the M30 for any given Symbol().

The below function (not tested) will return a 1 when there is a buy signal, and a -1 for a sell signal, and 0 if no signal. You will need to adjust the extern variables accordingly as this is more of a science than a "one-size-fits-all". Also, you might need to use IsNewBar.

//the values below should sum to 100
extern int RTail_To_Range_Ratio = 80;    //rejection tail ratio to range of candle
extern int Body_To_Range_Ratio = 10;     //body size ratio to range of candle
extern int NRTail_To_Range_Ratio = 10;   //non-rejection tail ratio to range of candle


int Signal()
{
    //candle information
    double dOpen  = iOpen(Symbol(),PERIOD_M30,0);
    double dHigh  = iHigh(Symbol(),PERIOD_M30,0);
    double dLow   = iLow(Symbol(),PERIOD_M30,0);
    double dClose = iClose(Symbol(),PERIOD_M30,0);
    double dBody  = MathAbs(dOpen - dClose);
    double dRange = dHigh - dLow;
    double dRTail = 0, dTail = 0;


    //green candle
    if(dClose > dOPen){
        dRTail = dOPen - dLow;
        dTail = dHigh - dClose;
        //now check ratios 
        if((dTail / dRange) <= (NRTail_To_Range_Ratio / 100) && (dBody / dRange) <= (Body_To_Range_Ratio / 100) && (dRTail / dRange) >= (RTail_To_Range_Ratio / 100)){
            return 1;
        }
    }
    //red candle
    else{
        dRTail = dHigh - dOpen;
        dTail = dClose - dLow;
        //now check ratios
        if((dTail / dRange) <= (NRTail_To_Range_Ratio / 100) && (dBody / dRange) <= (Body_To_Range_Ratio / 100) && (dRTail / dRange) >= (RTail_To_Range_Ratio / 100)){
            return -1;
        }
    }

    return 0;
}

Let me know if this solves your problem @SuperHueman.