Barishpolets Channels v1.0.P_v2
Download scriptThis script is designed to automatically identify and project trend channels on a trading chart by analyzing historical price movements. Rather than simply drawing lines based on random points, it uses a systematic logic to find significant structural "anchors" in the market.
Core Logic Overview
1. Identifying Market Structures (Fractals) The tool begins by scanning the historical data to find "fractals." In trading terms, these are local peaks (highs) and valleys (lows). The script automatically adjusts how far back it looks for these points depending on the timeframe you are viewing (for example, it uses a wider window for daily charts and a narrower one for very fast, short-term charts).
2. Determining the Primary Trend The script searches for two distinct fractal points to establish a primary trendline:
- If it finds a peak followed by another peak, it establishes a descending channel.
- If it finds a valley followed by another valley, it establishes an ascending channel. It calculates the "Step" (the slope) between these two points to understand the speed and direction of the current price movement.
3. Projecting the Channel Once the primary trend is established, the script performs three main actions:
- Primary Line: It draws a line representing the main path of the price trend.
- Parallel Line: It projects a second, parallel line to create a "channel." This helps traders see the boundaries of the current market movement.
- Median Line: It automatically calculates and draws a middle line (the equilibrium point) between the top and bottom of the channel.
4. Detecting Breakouts One of the more advanced features of this logic is its ability to detect when the price "breaks" the channel. The script checks if recent price action has moved outside the boundaries established by the trend lines. If a break occurs, it identifies the specific point where the price crossed that boundary, providing a visual cue that the current trend may be ending or shifting.
Summary of Visual Output
When applied to a chart, the tool provides a clear visual framework consisting of:
- The Upper/Lower Boundary: Defining the "walls" of the current market corridor.
- The Midline: Providing a target for potential pullbacks or reversals.
- Trend Projection: Extending these lines into the future to help anticipate where the price might head next based on current momentum.
//+------------------------------------------------------------------+
//| 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