Code Library / Candlestick Patterns

MQL5 Pin Bar Detector

Detect bullish and bearish pin bars.

Language: MQL5 · Category: Candlestick Patterns · Tags: pin bar, MQL5, candlestick, reversal, wick

Source code

enum PIN {NONE,BULL,BEAR};
PIN DetectPin(int s) {
   double o[],h[],l[],c[];
   CopyOpen(_Symbol,PERIOD_CURRENT,s,1,o); CopyHigh(_Symbol,PERIOD_CURRENT,s,1,h);
   CopyLow(_Symbol,PERIOD_CURRENT,s,1,l); CopyClose(_Symbol,PERIOD_CURRENT,s,1,c);
   double body=MathAbs(c[0]-o[0]),range=h[0]-l[0];
   double uw=h[0]-MathMax(o[0],c[0]),lw=MathMin(o[0],c[0])-l[0];
   if(range==0||body==0) return NONE;
   if(lw>=body*2&&MathMin(o[0],c[0])>l[0]+range*0.6) return BULL;
   if(uw>=body*2&&MathMax(o[0],c[0])<h[0]-range*0.6) return BEAR;
   return NONE;
}

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own MQL5 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.

Related snippets