getrendosc_v1
Download scriptThis script is a technical analysis tool designed for the MetaTrader platform. It creates a visual oscillator that helps traders identify trends and momentum by smoothing out market price fluctuations.
Here is a professional breakdown of how the logic functions:
Core Logic and Data Processing
The indicator works by analyzing three distinct layers of market data to provide a clear picture of price movement:
1. Relative Price Positioning (The Basis) Instead of looking at just the current price, the script examines a specific "window" of recent history. It identifies the highest high and the lowest low within that window. It then calculates where the current price sits relative to the starting price of that period. This tells the tool whether the price is moving aggressively toward the top or bottom of its recent range.
2. Momentum Normalization (The Oscillator) The script takes the raw price data and converts it into a normalized percentage. By calculating the ratio of the move against the total range of the "window," it eliminates the "noise" of different asset prices (e.g., making a $1,000 stock look similar to a $10 currency pair).
3. Weighted Smoothing (The Trend) To prevent the indicator from jumping erratically with every small price tick, the script applies two different smoothing techniques:
- Summation Scaling: It aggregates values over a set period to balance positive and negative movements, centering the data around a neutral midline.
- Hodrick-Prescott Filter (HP Filter): This is a sophisticated statistical method often used in economics. In this script, it acts as a high-quality "noise filter." It separates the underlying long-term trend from short-term fluctuations, resulting in a much smoother line that highlights the primary direction of the market.
Visual Output
The indicator produces two distinct visual elements on the chart:
- The Main Oscillator (OscTrend): A yellow-green line that represents the processed momentum. It oscillates around a central level, showing the "heartbeat" of the price trend.
- The Filtered Signal (HPFilter): A tomato-colored line that provides a refined version of the trend. By comparing the position of the main oscillator against this filtered signal, traders can visually identify when momentum is accelerating or slowing down.
Summary for the User
In practical terms, this script transforms raw price action into a smoothed "trend map." It identifies where the price is currently sitting within its recent range, removes the "jitter" of standard market volatility using advanced mathematical filtering, and presents it as an oscillator. This allows you to see the prevailing direction of the market more clearly than by looking at a standard price chart alone.
//+------------------------------------------------------------------+
//| geTrendOsc.mq5 |
//| Copyright © 2007, Forte928 |
//| |
//+------------------------------------------------------------------+
//--- àâòîðñòâî èíäèêàòîðà
#property copyright "Copyright © 2007, Forte928"
//--- àâòîðñòâî èíäèêàòîðà
#property link ""
//--- íîìåð âåðñèè èíäèêàòîðà
#property version "1.10"
//--- îòðèñîâêà èíäèêàòîðà â îòäåëüíîì îêíå
#property indicator_separate_window
//--- äëÿ ðàñ÷¸òà è îòðèñîâêè èíäèêàòîðà èñïîëüçîâàíî äâà áóôåðà
#property indicator_buffers 2
//--- èñïîëüçîâàíî äâà ãðàôè÷åñêèõ ïîñòðîåíèÿ
#property indicator_plots 2
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà CG |
//+----------------------------------------------+
//--- îòðèñîâêà èíäèêàòîðà 1 â âèäå ëèíèè
#property indicator_type1 DRAW_LINE
//--- â êà÷åñòâå öâåòà áû÷åé ëèíèè èíäèêàòîðà èñïîëüçîâàí YellowGreen öâåò
#property indicator_color1 clrYellowGreen
//--- ëèíèÿ èíäèêàòîðà 1 - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style1 STYLE_SOLID
//--- òîëùèíà ëèíèè èíäèêàòîðà 1 ðàâíà 1
#property indicator_width1 1
//--- îòîáðàæåíèå áû÷åé ìåòêè èíäèêàòîðà
#property indicator_label1 "OscTrend"
//+----------------------------------------------+
//| Ïàðàìåòðû îòðèñîâêè èíäèêàòîðà Trigger |
//+----------------------------------------------+
//--- îòðèñîâêà èíäèêàòîðà 2 â âèäå ëèíèè
#property indicator_type2 DRAW_LINE
//--- â êà÷åñòâå öâåòà ìåäâåæüåé ëèíèè èíäèêàòîðà èñïîëüçîâàí Tomato öâåò
#property indicator_color2 clrTomato
//--- ëèíèÿ èíäèêàòîðà 2 - íåïðåðûâíàÿ êðèâàÿ
#property indicator_style2 STYLE_SOLID
//--- òîëùèíà ëèíèè èíäèêàòîðà 2 ðàâíà 1
#property indicator_width2 1
//--- îòîáðàæåíèå ìåäâåæüåé ìåòêè èíäèêàòîðà
#property indicator_label2 "HPFilter"
//+----------------------------------------------+
//| Ïàðàìåòðû îòîáðàæåíèÿ ãîðèçîíòàëüíûõ óðîâíåé |
//+----------------------------------------------+
#property indicator_level1 80
#property indicator_level2 60
#property indicator_level3 50
#property indicator_level4 40
#property indicator_level5 20
#property indicator_level6 0
#property indicator_levelcolor clrGray
#property indicator_levelstyle STYLE_DASHDOTDOT
//+-----------------------------------+
//| îáúÿâëåíèå êîíñòàíò |
//+-----------------------------------+
#define RESET 0 // êîíñòàíòà äëÿ âîçâðàòà òåðìèíàëó êîìàíäû íà ïåðåñ÷¸ò èíäèêàòîðà
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+----------------------------------------------+
input uint Window=1;
input uint OscPeriod=14;
input uint HPPeriod=100;
input uint CBars=500;
input uint Shift=0; // ñäâèã èíäèêàòîðà ïî ãîðèçîíòàëè â áàðàõ
//+----------------------------------------------+
//--- îáúÿâëåíèå äèíàìè÷åñêèõ ìàññèâîâ, êîòîðûå â äàëüíåéøåì
//--- áóäóò èñïîëüçîâàíû â êà÷åñòâå èíäèêàòîðíûõ áóôåðîâ
double IndBuffer[];
double TriggerBuffer[];
//--- Îáúÿâëåíèå öåëûõ ïåðåìåííûõ íà÷àëà îòñ÷¸òà äàííûõ
int min_rates_total;
//--- îáúÿâëåíèå ãëîáàëüíûõ ïåðåìåííûõ
int Count[];
double Series[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- èíèöèàëèçàöèÿ ïåðåìåííûõ íà÷àëà îòñ÷¸òà äàííûõ
min_rates_total=int(Window+OscPeriod+HPPeriod);
//--- ðàñïðåäåëåíèå ïàìÿòè ïîä ìàññèâû ïåðåìåííûõ
ArrayResize(Count,OscPeriod);
ArrayResize(Series,OscPeriod);
//--- èíèöèàëèçàöèÿ ïàìÿòè
ArrayInitialize(Count,0);
ArrayInitialize(Series,0.0);
//--- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
SetIndexBuffer(0,IndBuffer,INDICATOR_DATA);
//--- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 1 ïî ãîðèçîíòàëè íà Shift
PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//--- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷¸òà îòðèñîâêè èíäèêàòîðà
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//--- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(IndBuffer,true);
//--- ïðåâðàùåíèå äèíàìè÷åñêîãî ìàññèâà â èíäèêàòîðíûé áóôåð
SetIndexBuffer(1,TriggerBuffer,INDICATOR_DATA);
//--- îñóùåñòâëåíèå ñäâèãà èíäèêàòîðà 2 ïî ãîðèçîíòàëè íà Shift
PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//--- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷¸òà îòðèñîâêè èíäèêàòîðà
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//--- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//--- èíäåêñàöèÿ ýëåìåíòîâ â áóôåðå êàê â òàéìñåðèè
ArraySetAsSeries(TriggerBuffer,true);
//--- èíèöèàëèçàöèè ïåðåìåííîé äëÿ êîðîòêîãî èìåíè èíäèêàòîðà
string shortname="geTrendOsc";
//--- ñîçäàíèå èìåíè äëÿ îòîáðàæåíèÿ â îòäåëüíîì ïîäîêíå è âî âñïëûâàþùåé ïîäñêàçêå
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- îïðåäåëåíèå òî÷íîñòè îòîáðàæåíèÿ çíà÷åíèé èíäèêàòîðà
IndicatorSetInteger(INDICATOR_DIGITS,0);
//---
}
//+------------------------------------------------------------------+
//| 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[])
{
//--- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷¸òà
if(rates_total<min_rates_total) return(RESET);
//--- îáúÿâëåíèÿ ëîêàëüíûõ ïåðåìåííûõ
int limit,bar;
//--- ðàñ÷åò ñòàðòîâîãî íîìåðà limit äëÿ öèêëà ïåðåñ÷åòà áàðîâ
limit=int(rates_total-Window-1);
limit=int(MathMin(limit,CBars));
//--- îñóùåñòâëåíèå ñäâèãà íà÷àëà îòñ÷¸òà îòðèñîâêè èíäèêàòîðà
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rates_total-limit);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,rates_total-limit);
//--- èíäåêñàöèÿ ýëåìåíòîâ â ìàññèâàõ êàê â òàéìñåðèÿõ
ArraySetAsSeries(Open,true);
ArraySetAsSeries(Low,true);
ArraySetAsSeries(High,true);
ArraySetAsSeries(Close,true);
//--- îñíîâíîé öèêë ðàñ÷¸òà èíäèêàòîðà
for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
double IxHigh,IxLow,IxOpen,IxClose;
//---
IxHigh=High[ArrayMaximum(High,bar,Window)];
IxLow=Low[ArrayMinimum(Low,bar,Window)];
if(IxHigh-IxLow>0)
{
IxOpen=Open[bar+Window];
IxClose=Close[bar];
Series[Count[0]]=(IxClose-IxOpen)/(IxHigh-IxLow);
}
else Series[Count[0]]=0;
//---
double Sumer=0;
double AbsSumer=0;
//---
for(int Px=int(OscPeriod-1); Px>=0; Px--)
{
Sumer+=Series[Px];
AbsSumer+=MathAbs(Series[Px]);
}
if(AbsSumer) IndBuffer[bar]=Sumer/AbsSumer*100/2+50;
else IndBuffer[bar]=0.0;
//---
HPFilter(IndBuffer,TriggerBuffer,HPPeriod,limit+1);
//---
if(bar>0) Recount_ArrayZeroPos(Count,OscPeriod);
}
//---
return(rates_total);
}
//+------------------------------------------------------------------+
//| Ïåðåñ÷åò ïîçèöèè ñàìîãî íîâîãî ýëåìåíòà â ìàññèâå |
//+------------------------------------------------------------------+
void Recount_ArrayZeroPos(int &CoArr[],// Âîçâðàò ïî ññûëêå íîìåðà òåêóùåãî çíà÷åíèÿ öåíîâîãî ðÿäà
int Size)
{
//---
int numb,Max1,Max2;
static int count=1;
//---
Max2=Size;
Max1=Max2-1;
//---
count--;
if(count<0) count=Max1;
//---
for(int iii=0; iii<Max2; iii++)
{
numb=iii+count;
if(numb>Max1) numb-=Max2;
CoArr[iii]=numb;
}
//---
}
//+------------------------------------------------------------------+
//| HPFilter - Ôèëüòð Õîäðèêà-Ïðåñêîòòà |
//+------------------------------------------------------------------+
void HPFilter(double &aySource[],double &ayResult[],int Lambda,int iCount)
{
//---
double Ak[],Bk[],Ck[],H1=0.0,H2=0.0,H3=0.0,H4=0.0,H5=0.0,HH1=0.0,HH2=0.0,HH3=0.0,HH5=0.0,HB,HC,Z;
ArrayResize(Ak,iCount);
ArrayResize(Bk,iCount);
ArrayResize(Ck,iCount);
//---
Ak[0]=1.0+Lambda;
Bk[0]=-2.0*Lambda;
Ck[0]=Lambda;
//---
for(int Hx=1; Hx<iCount-2; Hx++)
{
Ak[Hx]=6.0*Lambda+1.0;
Bk[Hx]=-4.0*Lambda;
Ck[Hx]=Lambda;
}
//---
Ak[1]=5.0*Lambda+1;
Ak[iCount-1]=1.0+Lambda;
Ak[iCount-2]=5.0*Lambda+1.0;
Bk[iCount-2]=-2.0*Lambda;
Bk[iCount-1]=0.0;
Ck[iCount-2]=0.0;
Ck[iCount-1]=0.0;
//--- forward
for(int Hx=0; Hx<iCount; Hx++)
{
Z=Ak[Hx]-H4*H1-HH5*HH2;
HB=Bk[Hx];
HH1=H1;
H1=(HB-H4*H2)/Z;
Bk[Hx]=H1;
HC=Ck[Hx];
HH2=H2;
H2=HC/Z;
Ck[Hx]=H2;
Ak[Hx]=(aySource[Hx]-HH3*HH5-H3*H4)/Z;
HH3=H3;
H3=Ak[Hx];
H4=HB-H5*HH1;
HH5=H5;
H5=HC;
}
//--- backward
H2=0;
H1=Ak[iCount-1];
ayResult[iCount-1]=H1;
for(int Hx=iCount-2; Hx>=0; Hx--)
{
ayResult[Hx]=Ak[Hx]-Bk[Hx]*H1-Ck[Hx]*H2;
H2=H1;
H1=ayResult[Hx];
}
//---
}
//+------------------------------------------------------------------+
Comments