Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

PIN Bar detector – Amibroker AFL code

4 min read

Pin Bar Detector – (Amibroker Indicator) is pin bar pattern detection system inspired for Earnforex – Pin Bar Detecor mql4 code. Pin Bar is a bar with a long upper or lower “tail”, “wick” or “shadow” and a much smaller “body” or “real body”, you can find pin bars on any plain candlestick chart. The Pinbar (also known as “Pin-bar” or “Pin bar”) pattern was first introduced by Martin Pring in his Pring on Price Patterns.

pinbar

 
It is a pure price action based indicator and in this indicator marks the bullish pin bar and bearish pin bar on the charts and also one scan use the exploration to find out the pin bar patterns from the list of stocks.

Recommended Timeframe
Higher timeframes like Hourly, 4 hourly, Daily, Weekly timeframes

pin-bar-trading-strategy

Bullish Reversal Pin Bar Formation

In a bullish pin bar reversal setup, the pin bar’s tail points down because it shows rejection of lower prices or a level of support. This setup very often leads to a rise in price.

Bearish Reversal Pin Bar Formation

In a bearish pin bar reversal setup, the pin bar’s tail points up because it shows rejection of higher prices or a level of resistance. This setup very often leads to a drop in price.

Pin Bar Patterns in Gold Daily Charts
Pin Bar Detector

Input Parameters for Pin Bar

MaxNoseBodySize (default = 0.33) — maximum allowed body/length ratio for the Nose bar.

NoseBodyPosition (default = 0.4) — Nose body should be position in top (bottom for bearish pattern) part of the Nose bar.

LeftEyeOppositeDirection (default = true) — tells the indicator that the Left Eye bar should be bearish for bullish Pinbar, and bullish for bearish Pinbar.

NoseSameDirection (default = true) — tells the indicator that the Nose bar should be of the same direction as the pattern itself.

NoseBodyInsideLeftEyeBody (default = false) — tells the indicator that the Nose body should be inside the Left Eye body.

LeftEyeMinBodySize (default = 0.1) — minimum size of the Left Eye body relative to the bar length.

NoseProtruding (default = 0.5) — minimum protrusion of the Nose bar relative to the bar length.

NoseBodyToLeftEyeBody (default = 1) — the maximum size of the Nose body relative to the Left eye body.

NoseLengthToLeftEyeLength (default = 0) — minimum Nose length relative to the Left Eye length.

LeftEyeDepth (default = 0.2) — minimum depth of the Left Eye relative to its length. Depth is the length of the part of the bar behind the Nose.

Note: This strategy doesn’t detects a perfect pin bar however some manual intervention is necessary to spot pin bar patterns accurately

/* Done      by    Rajandran R */
/* Author of www.marketcalls.in  */
/* Code Completion Date : 14th July 2014 */


_SECTION_BEGIN("Pin Bar AFL");

SetBarsRequired(100000,0);

GraphXSpace = 15;

SetChartOptions(0,chartShowArrows|chartShowDates);

SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));

GfxSetBkMode(0); 

GfxSetOverlayMode(1);

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));

Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

dec = (Param("Decimals",2,0,7,1)/10)+1;
Title = EncodeColor(55)+  Title = Name() + "     " + EncodeColor(32) + Date() +
"      " + EncodeColor(5) + "{{INTERVAL}}  " +
	EncodeColor(55)+ "     Open = "+ EncodeColor(52)+ WriteVal(O,dec) + 
	EncodeColor(55)+ "     High = "+ EncodeColor(5) + WriteVal(H,dec) +
	EncodeColor(55)+ "      Low = "+ EncodeColor(32)+ WriteVal(L,dec) + 
	EncodeColor(55)+ "    Close = "+ EncodeColor(52)+ WriteVal(C,dec)+
	EncodeColor(55)+ "    Volume = "+ EncodeColor(52)+ WriteVal(V,1);


MaxNoseBodySize = 0.33; // Max. Body / Candle length ratio of the Nose Bar
NoseBodyPosition = 0.4; // Body position in Nose Bar (e.g. top/bottom 40%)
LeftEyeOppositeDirection = True; // true = Direction of Left Eye Bar should be opposite to pattern (bearish bar for bullish Pinbar pattern and vice versa)
NoseSameDirection = False; // true = Direction of Nose Bar should be the same as of pattern (bullish bar for bullish Pinbar pattern and vice versa)
NoseBodyInsideLeftEyeBody = False; // true = Nose Body should be contained inside Left Eye Body
LeftEyeMinBodySize = 0.1; // Min. Body / Candle length ratio of the Left Eye Bar
NoseProtruding = 0.5; // Minmum protrusion of Nose Bar compared to Nose Bar length
NoseBodyToLeftEyeBody = 1; // Maximum relative size of the Nose Bar Body to Left Eye Bar Body
NoseLengthToLeftEyeLength = 0; // Minimum relative size of the Nose Bar Length to Left Eye Bar Length
LeftEyeDepth = 0.2; // Minimum relative depth of the Left Eye to its length; depth is difference with Nose's back

up=down =0;
point = 0.1;
blpin=brpin=False;

for(i=1;i<BarCount;i++)
{

      NoseLength = High[i] - Low[i];
      if (NoseLength == 0) NoseLength = Point;
      LeftEyeLength = High[i - 1] - Low[i - 1];
      if (LeftEyeLength == 0) LeftEyeLength = Point;
      NoseBody = abs(Open[i] - Close[i]);
      if (NoseBody == 0) NoseBody = point;
      LeftEyeBody = abs(Open[i - 1] - Close[i - 1]);
      if (LeftEyeBody == 0) LeftEyeBody = point;

      // Bearish Pinbar
      if (High[i] - High[i - 1] >= NoseLength * NoseProtruding) // Nose protrusion
      {
         if (NoseBody / NoseLength <= MaxNoseBodySize) // Nose body to candle length ratio
         {
            if (1 - (High[i] - Max(Open[i], Close[i])) / NoseLength < NoseBodyPosition) // Nose body position in bottom part of the bar
            {
               if ((!LeftEyeOppositeDirection) || (Close[i - 1] > Open[i - 1])) // Left Eye bullish if required
               {
                  if ((!NoseSameDirection) || (Close[i] < Open[i])) // Nose bearish if required
                  {
                     if (LeftEyeBody / LeftEyeLength  >= LeftEyeMinBodySize) // Left eye body to candle length ratio
                     {
                        if ((Max(Open[i], Close[i]) <= High[i - 1]) && (Min(Open[i], Close[i]) >= Low[i - 1])) // Nose body inside Left Eye bar
                        {
                           if (NoseBody / LeftEyeBody <= NoseBodyToLeftEyeBody) // Nose body to Left Eye body ratio
                           {
                              if (NoseLength / LeftEyeLength >= NoseLengthToLeftEyeLength) // Nose length to Left Eye length ratio
                              {
                                 if (Low[i] - Low[i - 1] >= LeftEyeLength * LeftEyeDepth)  // Left Eye low is low enough
                                 {
                                    if ((!NoseBodyInsideLeftEyeBody) || ((Max(Open[i], Close[i]) <= Max(Open[i - 1], Close[i - 1])) && (Min(Open[i], Close[i]) >= Min(Open[i - 1], Close[i - 1])))) // Nose body inside Left Eye body if required
                                    {
                                       Down[i] = High[i] + 5 * Point + NoseLength / 5;
                                       brpin[i]=True;
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
      
      // Bullish Pinbar
      if (Low[i - 1] - Low[i] >= NoseLength * NoseProtruding) // Nose protrusion
      {
         if (NoseBody / NoseLength <= MaxNoseBodySize) // Nose body to candle length ratio
         {
            if (1 - (Min(Open[i], Close[i]) - Low[i]) / NoseLength < NoseBodyPosition) // Nose body position in top part of the bar
            {
               if ((!LeftEyeOppositeDirection) || (Close[i - 1] < Open[i - 1])) // Left Eye bearish if required
               {
                  if ((!NoseSameDirection) || (Close[i] > Open[i])) // Nose bullish if required
                  {
                     if (LeftEyeBody / LeftEyeLength >= LeftEyeMinBodySize) // Left eye body to candle length ratio
                     {
                        if ((Max(Open[i], Close[i]) <= High[i - 1]) && (Min(Open[i], Close[i]) >= Low[i - 1])) // Nose body inside Left Eye bar
                        {
                           if (NoseBody / LeftEyeBody <= NoseBodyToLeftEyeBody) // Nose body to Left Eye body ratio
                           {
                              if (NoseLength / LeftEyeLength >= NoseLengthToLeftEyeLength) // Nose length to Left Eye length ratio
                              {
                                 if (High[i - 1] - High[i] >= LeftEyeLength * LeftEyeDepth) // Left Eye high is high enough
                                 {
                                    if ((!NoseBodyInsideLeftEyeBody) || ((Max(Open[i], Close[i]) <= Max(Open[i - 1], Close[i - 1])) && (Min(Open[i], Close[i]) >= Min(Open[i - 1], Close[i - 1])))) // Nose body inside Left Eye body if required
                                    {
                                       Up[i] = Low[i] - 5 * Point - NoseLength / 5;
                                       blpin[i]=True;
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }

for( i = 1; i < BarCount; i++ )
{
if(blpin[i] ) PlotText( "bull.pin " , i, L[ i ]-2.5, colorLime );
if(brpin[i] ) PlotText( " bear pin" , i, H[ i ]+2.5, colorOrange );
}
PlotShapes(shapeHollowSmallSquare*brpin,4,0,H ,5);
PlotShapes(shapeHollowSmallSquare*blpin,5,0,L ,-5);

Filter=brpin OR blpin;
AddColumn(brpin,"Bearish Pin",1.2);
AddColumn(blpin,"Bullish Pin",1.2);

_SECTION_END();

Steps to Install Pin Bar Detector Afl Code

1)Copy PIN Bar Detector.afl file to \program files\amibroker\formula\basic folder
2)Open Amibroker and Open a New Blank Chart
3)Goto Charts->Basic Charts and apply/drag-and-drop the Pin Bar detector code into the blank chart
4)Bingo you are done. Now you will be able to see the Pin Bar Detector indicator with Buy and Sell signals.

References
Pin Bar Trading System – Earnforex
Characteristic of Pin Bar formation – Learn to Trade the Market

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

18 Replies to “PIN Bar detector – Amibroker AFL code”

  1. thanks for the pin bar afl .

    i have selected 20 stocks in my favourite list.

    how to scan these 20 stocks in real time with pin bar afl in multi time frames. or in single time frame. and a popup notification shud be there even while i m writing mails .surfing etc .

    please also note that i keep only nifty chart open with 5 min tf open

    is it possible in amibroker. ?

    1. 1)Make a Watchlist of 20 Symbols
      2)Enable Realtime Scan and set the scan frequency
      3)Now Run Multiple Scans/Exploration for different timeframes.

  2. நாளை மார்க்கெட் நேரத்தில் பயன்படுத்தும் போதுதான் தெரியும் எப்படி இருக்கிறது என்று. நன்றாக இருக்கும் என்று நினைக்கிறேன். நன்றி ராஜேந்திரன் சார்.

    1. Krishnaswamy,

      Its only for higher timeframes and mostly involved human subjective decision to identify the correct pin bar even though the system provides the information.

  3. Respected Rajandran R
    i am vikram patel from ahmedabad i am senior citizzen i have talked with you several times ago regarding swing high and swing low
    i have tried to contact lot of expert but they werw not able to prepare afl as per my calculation
    you are on top of experts of ami broker as you talked you do not have time and donot write afl for personal people
    i have one request to suggest name of expert who can prepare afl on swing high and swing low.
    as you mention that swing high and swing low of spider software is repainting ,even though it is useful to me please help me in this matter do not dissappointme
    thanks
    vikram patel
    mobile no–9137741905

  4. Respected Rajandran R,

    I am a Commodity Trader last 5 years, now 2 years I am very disappointed about Commodity Market because can’t earn of this market. Now some days ago I think leave this market.

    Can you suggest me, what should I do and what is right market EQUITY & COMMODITY? Your PINBAR afl which market is suitable like stocks or commodity.

    I hope once I try with your PINBAR afl (position trading) but which market EQ & COM.

    Thanks

    Naveen Kumar Murthy

  5. When I verified syntax in formula editor of Amibroker, I got syntax error at Ln:22, Col: 149; Error 16. Too many arguments.

    Please help.

    1. Make sure you have enough candlesticks. It worked for many other people. Also make sure you are using the recent version of Amibroker.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More