PA IO detection v2

Download script
Author: mason
PA IO detection v2

This script is a specialized charting tool designed to automatically identify and highlight specific types of price action patterns—specifically Inside Bars and Outside Bars. By detecting these formations, the tool helps traders visualize moments of market consolidation (contraction) or sudden expansions in momentum (breakouts).

Core Functionality

The logic works by scanning historical price data and comparing the current price bar's range (the distance between its high and low points) against the preceding bar. It then categorizes these movements into four distinct visual types:

1. Inside Bar Detection

An Inside Bar occurs when the current candle is completely contained within the boundaries of the previous candle. This often signals a period of indecision or a "pause" in the market. The script distinguishes between two types:

  • Standard Inside Bars: Simple identification of a candle whose highs and lows are within the previous candle's range.
  • Nested/Sequential Inside Bars (INB): A more advanced detection that looks back at a set number of previous bars to ensure the current inside bar is the smallest in that sequence, highlighting a significant tightening of price action.

2. Outside Bar Detection

An Outside Bar occurs when the current candle "engulfs" the previous one, meaning its high is higher and its low is lower than the previous candle. This indicates a surge in volatility or a shift in momentum. The script identifies:

  • Standard Outside Bars (OB): Identified by the price moving significantly toward one end of the range relative to the opening price.
  • Strong Outside Bars (SOB): These are high-impact signals where the candle doesn't just engulf the previous one, but closes very near its own extreme high or low. This suggests a powerful, decisive move in a specific direction.

Visual Representation

Rather than cluttering the chart with complex lines or figures, the script uses directional arrows to signal these events:

  • Upward Signals: Placed above the price action to indicate bullish inside bars or bullish breakouts.
  • Downward Signals: Placed below the price action to indicate bearish inside bars or bearish breakouts.

Customization and Sensitivity

The logic includes several customizable filters that allow users to refine what they see on their charts:

  • Toggle Visibility: You can choose to show only specific types of signals (e.g., showing "Strong Outside Bars" while hiding standard ones).
  • Proximity Thresholds: The script uses percentage-based logic to determine how "strong" a move is, ensuring that only high-quality price action—where the price closes near the extremes—is highlighted as a "Strong" signal.
  • Dynamic Positioning: The arrows are automatically offset from the candles based on recent market volatility (using ATR calculations), ensuring they remain clearly visible and do not overlap with the actual price bars.
Indicators Used
Indicator of the average true rangeMoving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
PA IO detection v2
//------------------------------------------------------------------/
// PA IO detection v2.mq4
// 
//------------------------------------------------------------------/
#property copyright "mason" 

#property indicator_chart_window 
#property indicator_buffers 8
#property indicator_color1 Chocolate
#property indicator_color2 Chocolate
#property indicator_color3 OrangeRed
#property indicator_color4 OrangeRed
#property indicator_color5 Chocolate
#property indicator_color6 Chocolate
#property indicator_color7 OrangeRed
#property indicator_color8 OrangeRed

extern bool   ShowIB=false;
extern bool   ShowINB=true;
extern bool   ShowOB=true;
extern bool   ShowStrongOB=true;
extern double PreviousInsideBars=4;
extern double PercentOBClosefromStart=50;
extern double PercentOBClosetoEnd=20;
extern int    backperiod=10;
extern double IBPercentLabel=40;
extern double INBPercentLabel=80;
extern double OBPercentLabel=50;
extern double SOBPercentLabel=100;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];

int init() 
  {
   SetIndexStyle(0,DRAW_ARROW,0,1);  //Inside Bar Up
   SetIndexArrow(0,241);
   SetIndexBuffer(0,ExtMapBuffer1); 
   SetIndexEmptyValue(0,0.0);
   SetIndexLabel(0,"IB Up");

   SetIndexStyle(1,DRAW_ARROW,0,1);  //Inside Bar Down
   SetIndexArrow(1,242); 
   SetIndexBuffer(1,ExtMapBuffer2); 
   SetIndexEmptyValue(1,0.0);
   SetIndexLabel(1,"IB Down");

   SetIndexStyle(2,DRAW_ARROW,0,2); //I4B Up
   SetIndexArrow(2,241); 
   SetIndexBuffer(2,ExtMapBuffer3); 
   SetIndexEmptyValue(2,0.0);
   SetIndexLabel(2,"INB Up");

   SetIndexStyle(3,DRAW_ARROW,0,2); //I4B Down
   SetIndexArrow(3,242); 
   SetIndexBuffer(3,ExtMapBuffer4); 
   SetIndexEmptyValue(3,0.0);
   SetIndexLabel(3,"INB Down");

   SetIndexStyle(4,DRAW_ARROW,0,1); //Outside Bar Up
   SetIndexArrow(4,233); 
   SetIndexBuffer(4,ExtMapBuffer5); 
   SetIndexEmptyValue(4,0.0);
   SetIndexLabel(4,"BUOB Up");

   SetIndexStyle(5,DRAW_ARROW,0,1); //Outside Bar Down
   SetIndexArrow(5,234); 
   SetIndexBuffer(5,ExtMapBuffer6); 
   SetIndexEmptyValue(5,0.0);
   SetIndexLabel(5,"BEOB Down");

   SetIndexStyle(6,DRAW_ARROW,0,2); //Strong Outside Bar Up
   SetIndexArrow(6,233); 
   SetIndexBuffer(6,ExtMapBuffer7); 
   SetIndexEmptyValue(6,0.0);   
   SetIndexLabel(6,"Strong BUOB Up");

   SetIndexStyle(7,DRAW_ARROW,0,2); //Strong Outside Bar Down
   SetIndexArrow(7,234); 
   SetIndexBuffer(7,ExtMapBuffer8); 
   SetIndexEmptyValue(7,0.0);  
   SetIndexLabel(7,"Strong BEOB Down");

   return(0);
  } 

int deinit() {
   }

  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double range,rangecurrent;
   int limit,count,count2,baratr;
   int    counted_bars=IndicatorCounted();
   bool nb,ib,ob,tbh,tbl;
   double buff1[],buff2[];
   
  //---- check for possible errors
   if(counted_bars<0) return(-1);
  //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   if (backperiod==0) limit=Bars-counted_bars; else limit=backperiod;
   
   if (Bars<400) baratr=Bars/2; else baratr=200;

   ArrayResize(buff1,limit+baratr);
   ArrayResize(buff2,baratr);
   
   for(int i2=limit+baratr; i2>=0; i2--) buff1[i2]=iATR(NULL,0,1,i2);
   
  //---- main loop
     for(int i=limit; i>=0; i--)
       {
         range=iMAOnArray(buff1,Bars,baratr,0,0,i);  // for arrow position
         rangecurrent=High[i]-Low[i];
         
         ExtMapBuffer1[i]=0;
         ExtMapBuffer2[i]=0;
         ExtMapBuffer3[i]=0;
         ExtMapBuffer4[i]=0;
         ExtMapBuffer5[i]=0;
         ExtMapBuffer6[i]=0;
         ExtMapBuffer7[i]=0;
         ExtMapBuffer8[i]=0;
         
//       Inside Bar
         if ((ShowIB)||(ShowINB)&&(i!=0))
         if ((High[i]<High[i+1])&&(Low[i]>Low[i+1])) {
            if (ShowIB) {
                ExtMapBuffer1[i] = High[i]+(IBPercentLabel/100*range);
                ExtMapBuffer2[i] = Low[i]-(IBPercentLabel/100*range);
            }

            if (ShowINB) {
                bool rangesmaller=true;
                for (int i3=2;i3<PreviousInsideBars;i3++) {
                    if ((High[i+i3]-Low[i+i3])<(High[i]-Low[i])) {
                        rangesmaller=false;
                        break;
                    }
                }
                if (rangesmaller) {
                   ExtMapBuffer3[i] = High[i]+(INBPercentLabel/100*range);
                   ExtMapBuffer4[i] = Low[i]-(INBPercentLabel/100*range);
                }
            }
         }
         

//       Outside Bar
         if ((ShowOB)||(ShowStrongOB))
         if ((High[i]>High[i+1])&&(Low[i]<Low[i+1])) {
            if (ShowOB) {
               if ((Open[i]<Close[i])&&(Close[i]>PercentOBClosefromStart/100*rangecurrent+Low[i]))  ExtMapBuffer5[i] = High[i]+(OBPercentLabel/100*range);
               if ((Open[i]>Close[i])&&(Close[i]<High[i]-PercentOBClosefromStart/100*rangecurrent)) ExtMapBuffer6[i] = Low[i]-(OBPercentLabel/100*range);
             }
             if (ShowStrongOB) {
                if ((Open[i]<Close[i])&&(Close[i]>High[i+1])&&((High[i]-Close[i])<(PercentOBClosetoEnd/100*rangecurrent))) ExtMapBuffer7[i] = High[i]+(SOBPercentLabel/100*range);
                if ((Open[i]>Close[i])&&(Close[i]<Low[i+1])&&((Close[i]-Low[i])<(PercentOBClosetoEnd/100*rangecurrent)))   ExtMapBuffer8[i] = Low[i]-(SOBPercentLabel/100*range);
             }
         }


       } //for
   return(0);
  } //start()
//+------------------------------------------------------------------+

Comments