_TRO_SF_TREND
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform. Its primary purpose is to identify and visualize market trends by automatically detecting "fractals"—specific peaks and valleys in price action—and using them to construct a dynamic trading channel.
Core Logic Overview
1. Automatic Trend Detection (Fractal Identification) The script begins by scanning historical price data to find significant turning points. It identifies "fractals," which are the local highs and lows of the market. Depending on the timeframe you are viewing (e.g., 5-minute, 1-hour, or daily charts), the script automatically adjusts its sensitivity to ensure it captures relevant price movements without being distracted by minor "noise."
2. Dynamic Channel Construction Once two major turning points are identified (a peak and a valley), the script calculates the mathematical slope of the trend. It treats these points as the foundation for a linear channel:
- Directional Analysis: It determines if the prevailing trend is upward or downward based on the relationship between these points.
- Step Calculation: It calculates a "step" value, which represents the rate at which the price is moving over time. This allows the script to project where the price "should" be if it continues its current trajectory.
3. Price Projection and Validation The tool doesn't just draw a simple line; it performs a validation check against the most recent price bars. It looks for the closest alignment between the current market price and the calculated trend line to ensure the channel remains relevant to real-time movement. It specifically checks the interaction between the high and low prices of the current and previous candles to determine the most accurate point of contact.
4. Real-Time Data Output Beyond just visual representation, the script calculates three specific metrics that provide deeper insight into the market structure:
- Trend Slope: A numerical value representing the steepness of the price movement.
- Channel Distance: The absolute distance between the current price and the start of the trend (the "anchor" point).
- Angle Calculation: A normalized value reflecting the angle of the channel, which helps traders understand the velocity of the trend.
Summary for the User
In simple terms, this script looks at the history of a currency pair or asset, finds the most important "highs" and "lows," and draws a mathematical trend line through them. It then tells you how steep that trend is, how far the price has traveled since that trend started, and where the price currently sits in relation to that trend. This helps traders visualize the prevailing market direction and identify potential overextended moves.
#property indicator_chart_window
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
//---- input parameters
extern color LineColor = DarkOrange;
extern int AllBars=240;
extern int BarsForFract=0;
int CurrentBar=0;
double Step=0;
int B1=-1,B2=-1;
int UpDown=0;
double P1=0,P2=0,PP=0;
int i=0,AB=300,BFF=0;
int ishift=0;
double iprice=0;
datetime T1,T2;
string TL1, TL2, MIDL, TFS;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//---
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double j;
string temp;
int m,s,k;
m=Time[0]+Period()*60-CurTime();
j=m/60.0;
s=m%60;
m=(m-m%60)/60;
temp = ( m + " minutes " + s + " seconds left to bar end");
int counted_bars=IndicatorCounted();
//----
if ((AllBars==0) || (Bars<AllBars)) AB=Bars; else AB=AllBars; //AB-êîëè÷åñòâî îáñ÷èòûâàåìûõ áàðîâ
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=12; break;
case 240: BFF=15; break;
case 1440: BFF=10; break;
case 10080: BFF=6; break;
default: return(-1); break;
}
}
CurrentBar=2; //ñ÷èòàåì ñ òðåòüåãî áàðà, ÷òîáû ôðàêòàë "çàêðåïèëñÿ
B1=-1; B2=-1; UpDown=0;
while(((B1==-1) || (B2==-1)) && (CurrentBar<AB))
{
if((UpDown<1) && (CurrentBar==Lowest(Symbol(),Period(),MODE_LOW,BFF*2+1,CurrentBar-BFF)))
{
if(UpDown==0) { UpDown=-1; B1=CurrentBar; P1=Low[B1]; }
else { B2=CurrentBar; P2=Low[B2];}
}
if((UpDown>-1) && (CurrentBar==Highest(Symbol(),Period(),MODE_HIGH,BFF*2+1,CurrentBar-BFF)))
{
if(UpDown==0) { UpDown=1; B1=CurrentBar; P1=High[B1]; }
else { B2=CurrentBar; P2=High[B2]; }
}
CurrentBar++;
}
if((B1==-1) || (B2==-1)) { return(-1);} // Çíà÷èò íå íàøëè ôðàêòàëîâ ñðåäè 300 áàðîâ 8-)
Step=(P2-P1)/(B2-B1);//Âû÷èñëèëè øàã, åñëè îí ïîëîæèòåëüíûé, òî êàíàë íèñõîäÿùèé
P1=P1-B1*Step; B1=0;//ïåðåñòàâëÿåì öåíó è ïåðâûé áàð ê íóëþ
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;}
}
//Òåïåðü ïåðåñòàâèì êîíå÷íóþ öåíó è áàð íà ÀÂ, ÷òîáû ëèíèè êàíàëà ðèñîâàëèñü ïîäëèííåå
P2=P1+AB*Step;
T1=Time[B1]; T2=Time[AB];
//Åñëè íå áûëî ïåðåñå÷åíèÿ êàíàëà, òî 0, èíà÷å ñòàâèì ïñèñó.
if(iprice!=0) ExtMapBuffer1[ishift]=iprice;
double mySlope = Step/Point ;
//Comment(temp);
//----
//channel distance of last/current bar
ExtMapBuffer2[0] = NormalizeDouble(MathAbs(PP - P1)/Point,0);
//angle of channel
ExtMapBuffer3[0] = NormalizeDouble(-Step/Point, 2);
//----
return( mySlope );
}
//+------------------------------------------------------------------+
Comments