duct
Download scriptThis script functions as a dynamic trend analysis tool designed to identify market structure and provide visual guidance on potential entry points. Instead of relying on standard lagging indicators, it focuses on identifying structural highs and lows to project linear trends directly onto your chart.
Core Logic and Behavior
1. Structural Identification The script scans historical price data to find significant "pivot points"—the highest highs and lowest lows within a specific window of time (defined by the clamping parameters). By identifying these peaks and valleys, it establishes the primary direction of the market movement.
2. Trend Line Projection Once the primary structural points are identified, the script calculates the rate of change (the "Step") between them. It then projects two primary trend lines:
- Trend Line 1: Represents a projected path based on previous price action.
- Trend Line 2: Represents an extended projection that accounts for recent market volatility and momentum.
- Center Line: A dotted line is rendered to represent the midpoint between these two projections, offering a visual "fair value" or equilibrium point.
3. Volatility Awareness (Flat Market Detection) The logic includes a built-in check to determine if the market is moving significantly or if it is "flat." It compares the current trend slope against the Average True Range (ATR). If the movement is too small relative to the typical volatility, the script flags the market as flat, which changes how it labels potential signals.
4. Dynamic Labeling One of the most user-friendly features of this script is its automated labeling. Based on the interaction between the trend direction, the "flat" market status, and the current price position relative to the projected lines, it automatically applies text labels such as "Buy," "Sell," or "Take."
These labels provide a real-time summary of the script's logic:
- When the trend is strong and clear, it identifies primary Buy and Sell zones.
- When the market is in a specific state of equilibrium or transition, it may switch to "Take" signals, suggesting areas where price may consolidate or reach a target.
Summary for the User
Essentially, this tool automates the process of drawing trend lines and identifying market structure. It looks at where the market was (the highs and lows) to calculate where it is going, then draws those paths on your screen while automatically telling you whether the current geometry suggests a buying opportunity, a selling opportunity, or a neutral "take" zone.
//+------------------------------------------------------------------+
//| DUCT.mq4 |
//| Yuriy Tokman (YTG) |
//| http://ytg.com.ua/ |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman (YTG)"
#property link "http://ytg.com.ua/"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
input ENUM_TIMEFRAMES TimeFrame = 60;
input int Needed_Bars = 0;
input int Clamping_Bars = 0;
input double Koeff_Flat = 0.25;
input color collir = Lime;
double ExtMapBuffer1[];
int Current_Bar=0,shift_b=0,trend,flat,i=0,Count_Bars=300,clamping_bars=0,Bars_1=-1,Bars_2=-1,Up_vs_Down=0, Time_Frame;
double Price_1=0,Price_2=0,Price_3=0,Steps=0,price_b=0;
datetime Time_1,Time_2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,164);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexEmptyValue(0,0.0);
//---
return(INIT_SUCCEEDED);
}
//----
void OnDeinit(const int reason){ DelObject();}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
int limit,y=0;
int counted_bars=prev_calculated;
limit=Bars-counted_bars;
if((Needed_Bars==0) || (Bars<Needed_Bars)) Count_Bars=Bars; else Count_Bars=Needed_Bars; //Count_Bars-êîëè÷åñòâî îáñ÷èòûâàåìûõ áàðîâ
Time_Frame = (int)TimeFrame;
if(Period()<Time_Frame)Time_Frame=Period();
if(Clamping_Bars>0) clamping_bars=Clamping_Bars;
else
{
switch(Time_Frame)
{
case 1: clamping_bars=12; break;
case 5: clamping_bars=48; break;
case 15: clamping_bars=24; break;
case 30: clamping_bars=24; break;
case 60: clamping_bars=12; break;
case 240: clamping_bars=15; break;
case 1440: clamping_bars=10; break;
case 10080: clamping_bars=6; break;
default: DelObject(); return(-1); break;
}
}
Current_Bar=2;
Bars_1=y-1; Bars_2=y-1; Up_vs_Down=0;
while(((Bars_1==-1) || (Bars_2==-1)) && (Current_Bar<Count_Bars))
{
if((Up_vs_Down<1) && (Current_Bar==iLowest(Symbol(),Time_Frame,MODE_LOW,clamping_bars*2+1,Current_Bar-clamping_bars)))
{
if(Up_vs_Down==0) {Up_vs_Down=-1; Bars_1=Current_Bar; Price_1=iLow(Symbol(),Time_Frame,Bars_1);}
else Bars_2=Current_Bar; Price_2=iLow(Symbol(),Time_Frame,Bars_2);
}
if((Up_vs_Down>-1) && (Current_Bar==iHighest(Symbol(),Time_Frame,MODE_HIGH,clamping_bars*2+1,Current_Bar-clamping_bars)))
{
if(Up_vs_Down==0) {Up_vs_Down=1; Bars_1=Current_Bar; Price_1=iHigh(Symbol(),Time_Frame,Bars_1);}
else Bars_2=Current_Bar; Price_2=iHigh(Symbol(),Time_Frame,Bars_2);
}
Current_Bar++;
}
if((Bars_1==-1) || (Bars_2==-1)) { DelObject(); return(-1); }
Steps=(Price_2-Price_1)/(Bars_2-Bars_1);
Price_1=Price_1-Bars_1*Steps; Bars_1=0;
shift_b=0; price_b=0;
if(Up_vs_Down==1)
{
Price_3=iLow(Symbol(),Time_Frame,2)-2*Steps;
for(i=3;i<=Bars_2;i++)
{
if(iLow(Symbol(),Time_Frame,i)<Price_3+Steps*i) Price_3=iLow(Symbol(),Time_Frame,i)-i*Steps;
}
if(iLow(Symbol(),Time_Frame,0)<Price_3)
{
shift_b=0;
price_b=Price_3;
}
if(iLow(Symbol(),Time_Frame,1)<Price_3+Steps)
{
shift_b=1;
price_b=Price_3+Steps;
}
if(iHigh(Symbol(),Time_Frame,0)>Price_1)
{
shift_b=0;
price_b=Price_1;
}
if(iHigh(Symbol(),Time_Frame,1)>Price_1+Steps)
{
shift_b=1;
price_b=Price_1+Steps;
}
}
else
{
Price_3=iHigh(Symbol(),Time_Frame,2)-2*Steps;
for(i=3;i<=Bars_2;i++)
{
if(iHigh(Symbol(),Time_Frame,i)>Price_3+Steps*i) Price_3=iHigh(Symbol(),Time_Frame,i)-i*Steps;
}
if(iLow(Symbol(),Time_Frame,0)<Price_1)
{
shift_b=0;
price_b=Price_1;
}
if(iLow(Symbol(),Time_Frame,1)<Price_1+Steps)
{
shift_b=1;
price_b=Price_1+Steps;
}
if(iHigh(Symbol(),Time_Frame,0)>Price_3)
{
shift_b=0;
price_b=Price_3;
}
if(iHigh(Symbol(),Time_Frame,1)>Price_3+Steps)
{
shift_b=1;
price_b=Price_3+Steps;
}
}
Price_2=Price_1+Count_Bars*Steps;
Time_1=Time[iBarShift(Symbol(),0,iTime(Symbol(),Time_Frame,Bars_1))];
Time_2=Time[iBarShift(Symbol(),0,iTime(Symbol(),Time_Frame,Count_Bars))];
if(price_b!=0) ExtMapBuffer1[shift_b]=price_b;
if(Steps>0) trend=-1; else trend=1;
if(MathAbs(Steps*20)>=2*iATR(Symbol(),Time_Frame,40,0)*Koeff_Flat) flat=0; else flat=1;
if(ObjectFind("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0))==-1)
{
ObjectCreate("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJ_TREND,0,Time_2,Price_3+Steps*Count_Bars,Time_1,Price_3);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_COLOR,collir);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_WIDTH,2);
}
else
{
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_TIME1,Time_2);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_PRICE1,Price_3+Steps*Count_Bars);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_TIME2,Time_1);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_PRICE2,Price_3);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_COLOR,collir);
ObjectSet("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_WIDTH,2);
}
if(ObjectFind("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0))==-1)
{
ObjectCreate("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJ_TREND,0,Time_2,Price_2,Time_1,Price_1);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_COLOR,collir);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_WIDTH,2);
}
else
{
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_TIME1,Time_2);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_PRICE1,Price_2);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_TIME2,Time_1);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_PRICE2,Price_1);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_COLOR,collir);
ObjectSet("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_WIDTH,2);
}
if(ObjectFind("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0))==-1)
{
ObjectCreate("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJ_TREND,0,Time_2,(Price_2+Price_3+Steps*Count_Bars)/2,Time_1,(Price_1+Price_3)/2);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_COLOR,collir);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_WIDTH,1);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_STYLE,STYLE_DOT);
}
else
{
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_TIME1,Time_2);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_PRICE1,(Price_2+Price_3+Steps*Count_Bars)/2);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_TIME2,Time_1);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_PRICE2,(Price_1+Price_3)/2);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_WIDTH,1);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_STYLE,STYLE_DOT);
ObjectSet("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),OBJPROP_COLOR,collir);
}
if(flat==1)
{
if(Price_3+Steps*Count_Bars>Price_2)
{
ObjectSetText("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Sell");
ObjectSetText("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Buy");
}
else
{
ObjectSetText("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Sell");
ObjectSetText("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Buy");
}
}
else
{
if((Price_3+Steps*Count_Bars>Price_2) && (trend==-1))
{
ObjectSetText("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Sell");
ObjectSetText("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Take");
}
if((Price_3+Steps*Count_Bars>Price_2) && (trend==1))
{
ObjectSetText("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Take");
ObjectSetText("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Buy");
}
if((Price_3+Steps*Count_Bars<Price_2) && (trend==-1))
{
ObjectSetText("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Sell");
ObjectSetText("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Take");
}
if((Price_3+Steps*Count_Bars<Price_2) && (trend==1))
{
ObjectSetText("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Take");
ObjectSetText("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0),"Buy");
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
void DelObject()
{
ObjectDelete("TrendLine1_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0));
ObjectDelete("TrendLine2_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0));
ObjectDelete("CenterLine_"+DoubleToStr(Time_Frame,0)+DoubleToStr(Clamping_Bars,0));
}
//+------------------------------------------------------------------+
Comments