Barishpolets Channels v1.0.P_v1
Download scriptThe Barishpolets Channels script is a technical analysis tool designed to automatically identify and project market trends by locating significant price pivots. Instead of simply drawing lines based on recent price action, it uses a specific logic to find structural "fractals"—peaks and valleys in the market history—to establish a channel of movement.
Here is a breakdown of how the logic functions:
1. Identification of Key Price Points (Fractals)
The tool begins by scanning historical price data to find major turning points, known as fractals.
- Dynamic Scanning: Depending on the timeframe you are viewing (e.g., 5-minute charts vs. daily charts), the script automatically adjusts its "look-back" range to ensure it finds relevant structural points rather than minor price fluctuations.
- Peak and Valley Detection: It searches for a specific sequence of highs and lows. It identifies whether the primary move is currently oriented upward or downward by finding the first significant high (peak) and then seeking out the subsequent low (valley), or vice versa.
2. Channel Calculation and Slope
Once two significant fractals are identified, the script calculates the mathematical relationship between them:
- The Step: It determines the "Step," which is the rate of change (the slope) between the two points. This tells the tool how much the price moves per candle.
- Trend Direction: If the step value is positive, it recognizes a descending movement; if negative, it recognizes an ascending one.
3. Projecting the Trend Lines
Using the calculated "Step," the script creates a visual channel on your chart:
- Primary Line: This draws the main trend path extending into the future based on the current slope.
- Opposite Trendline: The logic also seeks out an opposing price point to create a secondary parallel line. This helps visualize the "width" of the market's current range or corridor.
- Median Line: It automatically calculates a middle path between these two trendlines, providing a "fair value" area for the price to trade within.
4. Smart Breakout Detection
A key feature of this logic is its ability to monitor if the current price is breaking out of the established channel. The script checks the most recent price candles against the projected boundaries. If the price moves outside the calculated "Step" range, the tool can identify this as a potential break in the trend, marking where the old market structure has failed and a new movement may be beginning.
Summary for the User
Essentially, this tool removes the guesswork from drawing manual trendlines. It looks back at the history to find the most significant "structural" turns in price, calculates the exact speed of that move, and projects a three-line channel into the future to help you see where the price is likely to stay—or where it might break out.
//+------------------------------------------------------------------+
//| Barishpolets Channels.mq4 |
//| |
//| Conversion from MT3 by Mike & ... |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- Inputs
extern int CalcBars=240; //number of bars counted. 0-all bars counted
extern int BarsForFract=0; //bars of fractal's "shoulder"
//---- Variables
int B1=-1,B2=-1,UpDown=0,CurrentBar=0,limit,k,i,i2,NumBars;
int BFF=0,ishift=0,LowBar,HighBar,BMax,BMin,start;
double P1=0,P2=0,PP=0,Step=0,iprice=0,PMax,PMin,price;
//---- Buffers
double Buffer1[];
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(1);
SetIndexBuffer(0,Buffer1);
SetIndexStyle(0,DRAW_ARROW,EMPTY);
SetIndexArrow(0,164);
//---- selections
if (BarsForFract>0) {BFF=BarsForFract;}
else
{
switch (Period())
{
case 1: BFF=12; break;
case 5: BFF=48; break;
case 15: BFF=24; break;
case 30: BFF=24; break;
case 60: BFF=24; break;
case 240: BFF=24; break;
case 1440: BFF=12; break;
case 10080: BFF=12; break;
default: return(-1);
}
}
//+----
return(0);
}
//+------------------------------------------------------------------+
int deinit()
{
for(k=0; k<limit; k++)
{
Buffer1[k]=0.0;
}
ObjectDelete("TL1");
ObjectDelete("TL2");
ObjectDelete("MIDL");
//----
return(0);
}
//+------------------------------------------------------------------+
int start()
{
//---- check inputs
if (Bars<=CalcBars || CalcBars<1)
{
Print("ERROR: Bars("+Bars+")<=CalcBars("+CalcBars+") or CalcBars("+CalcBars+")<1");
return(-1);
}
//----
int counted_bars=IndicatorCounted();
if(counted_bars<0) {Print("Error at counted_bars..."); return (-1);}
//---- Calc. area
if (CalcBars==0 || CalcBars>Bars) NumBars=Bars; else NumBars=CalcBars;
limit=NumBars-1;
//----
if(counted_bars<1)
{
for(k=limit; k>=0; k--)
{
Buffer1[k]=0.0;
}
}
if(counted_bars>0) counted_bars--;
//----
CurrentBar=2; //start counting from the third bar as the fractal is "pinned down"
B1=-1; B2=-1; UpDown=0;
while ((B1==-1 || B2==-1) && CurrentBar<NumBars)
{
//UpDown=1 means the 1st fractal has been found on top,
//UpDown=-1 means the 1st fractal has been found at the bottom,
//UpDown=0 means a fractal has not yet been found
//Â1 è Â2 - base line fractal bars
//Ð1 è Ð2 - prices for this line
//
// Low section
//
if (UpDown<1 && CurrentBar==Lowest(NULL,0,MODE_LOW,BFF*2+1,CurrentBar))
{
if (UpDown==0) { UpDown=-1; B1=CurrentBar; P1=Low[B1];}
else { B2=CurrentBar; P2=Low[B2];}
}
//
// High section
//
if (UpDown>-1 && CurrentBar==Highest(NULL,0,MODE_HIGH,BFF*2+1,CurrentBar))
{
if (UpDown==0) { UpDown=1; B1=CurrentBar; P1=High[B1];}
else { B2=CurrentBar; P2=High[B2];}
}
CurrentBar++;
} // end while
//----
if (B1==-1 || B2==-1) return(-1); // No fractals have been found in 300 bar range 8-)
Step=(P2-P1)/(B2-B1); //step calculated, if positive then the channel is descending
P1=P1-B1*Step; B1=0; //price and the 1st bar are being restored to zero
//
//Reference point for opposite trendline
//
ishift=0; iprice=0;
if (UpDown==1)
{
PP=Low[2]-2*Step;
for (i=3; i<=B2; i++)
{
if (Low[i]<PP+Step*i) {PP=Low[i]-i*Step;}
if (Low[0]<PP) {ishift=0; iprice=PP;}
if (Low[1]<PP+Step) {ishift=1; iprice=PP+Step;}
if (High[0]>P1) {ishift=0; iprice=P1;}
if (High[1]>P1+Step) {ishift=1; iprice=P1+Step;}
}
}
else
{
PP=High[2]-2*Step;
for (i=3; i<=B2; i++)
{
if (High[i]>PP+Step*i) {PP=High[i]-i*Step;}
if (Low[0]<P1) {ishift=0; iprice=P1;}
if (Low[1]<P1+Step) {ishift=1; iprice=P1+Step;}
if (High[0]>PP) {ishift=0; iprice=PP;}
if (High[1]>PP+Step) {ishift=1; iprice=PP+Step;}
}
}
//----
//Last price and bar to be set on ÀÂ to draw the line to the upmost right
P2=P1+NumBars*Step;
B1=Time[B1]; B2=Time[NumBars];
// 0 (zero) means the channel line remains intact, dot means the channel is broken
Buffer1[ishift]=iprice;
//----
ObjectCreate("TL1",OBJ_TREND,0,B2,PP+Step*NumBars,B1,PP);
ObjectCreate("TL2",OBJ_TREND,0,B2,P2,B1,P1);
ObjectCreate("MIDL",OBJ_TREND,0,B2,(P2+PP+Step*NumBars)/2,B1,(P1+PP)/2);
//----
return(0);
}
Comments