at_Itp4_Env_v1
Download scriptThis tool is designed as a visual technical indicator for trading charts. Its primary purpose is to smooth out price fluctuations and create a "channel" or "envelope" around the market price, helping traders identify the core trend and potential overextended price levels.
How the Logic Works
The script processes market data through three distinct logical phases to generate the final visual output:
1. Dynamic Data Point Selection The indicator first calculates how to sample price data based on your chosen timeframe. Instead of just looking at the very last price, it uses a specific "step" (based on the hours and period settings) to pick out significant historical price points. This ensures that the calculations remain consistent regardless of whether you are viewing a small chart or a large one.
2. The Smoothing & Interpolation Engine This is the core "brain" of the script. It takes those selected price points and performs a multi-layered smoothing process:
- Averaging: It blends historical prices to create a more stable baseline.
- Curvature Calculation: It uses a mathematical interpolation method (similar to how a curve is drawn between three points). By calculating the relationship between three different points in time, it creates a fluid, curved line rather than jagged, blocky steps. This helps filter out "noise"—those tiny, insignificant price flickers that can often mislead traders.
3. The Envelope Generation Once the script has established this smooth, central trend line (the "at_Itp(t)" line), it performs a final calculation to create the outer boundaries:
- Upper Bound: It calculates a percentage shift upward from the center line.
- Lower Bound: It calculates a corresponding percentage shift downward.
Visual Result
The end result on your chart is a three-line system:
- A Center Line: Represents the smoothed "true" trend of the asset.
- An Upper and Lower Boundary: These create a visual corridor. When the price stays within these boundaries, it is considered to be following the established trend. When the price pushes outside of these lines, it may indicate that the market is becoming overbought or oversold relative to its recent movement.
//+------------------------------------------------------------------+
//| at_Itp(t)Env.mq4 |
//| at_Itp(t): Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//| Envelope: Copyright © 2005, Nikolay Kositsin |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Blue
#property indicator_color3 Red
extern int hours=4;
extern int s=10;
extern int cb=1000;
extern double Envelope_Shift=0.20;
extern int Shift=0;
double ci[];
double UperBuffer [];
double DownBuffer [];
//================================================
int init()
{
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
//---- IndexShift
SetIndexShift(0,Shift);
SetIndexShift(1,Shift);
SetIndexShift(2,Shift);
//----
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//----
SetIndexLabel (0, "Envelope+");
SetIndexLabel (1, "at_Itp(t)");
SetIndexLabel (2, "Envelope-");
//---- indicator buffers
IndicatorBuffers(3);
//---- indicator buffers mapping
SetIndexBuffer (0, UperBuffer);
SetIndexBuffer (1, ci);
SetIndexBuffer (2, DownBuffer);
return(0);
}
//================================================
int start()
{
// int counted_bars=IndicatorCounted();
int i,x,m,pt,aa,bb,cc;
//----------------------------
pt=hours*60/Period();
//-----------------------
for (m=0; m<=s; m++)
{
i=0;
while (i<=cb)
{
if (m==0) { ci[cb+pt]=Close[cb+pt]; ci[i]=Close[i]; ci[cb+2*pt]=Close[cb+2*pt];}
if (m!=0 & m!=s) { ci[i+pt]=0.5*(ci[i]+ci[i+2*pt]); ci[0]=0.5*(ci[0]+ci[pt]); ci[cb+2*pt]=0.5*(ci[cb+2*pt]+ci[cb+pt]); }
if (m==s) { aa=i; bb=i+pt; cc=i+2*pt;
for (x=i; x<=i+pt-1; x++) {ci[x]=ci[aa]*((x-bb)*(x-cc))/((aa-bb)*(aa-cc))+ci[bb]*((x-aa)*(x-cc))/((bb-aa)*(bb-cc))+ci[cc]*((x-aa)*(x-bb))/((cc-aa)*(cc-bb));}
}
i=i+pt;
}
}
//+------------------------------------------------------------------+
//|+++++++++++++++++++++++++++++++++++++++++Envelope code+++++++++++++
// Input indicator buffer: ci. |
//| Output indicator buffers: UperBuffer, DownBuffer. |
//+------------------------------------------------------------------+ |
int ExtCountedBars=IndicatorCounted();
int r; r=Bars-ExtCountedBars-1;
while(r>=0)
{
UperBuffer[r] = ci[r]*(1+Envelope_Shift/100);
DownBuffer[r] = ci[r]/(1+Envelope_Shift/100);
r--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments