iCompass_v1.2
Download scriptThe iCompass script is a sophisticated technical analysis tool designed to identify market trends and provide timely entry signals based on price action, volatility, and momentum. Instead of relying on a single price point, it analyzes the relationship between high, low, and median prices over a specific period to determine the underlying "flow" of the market.
Core Analytical Logic
The script functions through a multi-layered processing system:
1. Relative Position Analysis The tool first examines the range between the highest price and the lowest price over a set number of bars. It calculates where the current median price sits within that range. By comparing this position to historical data, it determines whether the price is pushing toward the top (bullish) or bottom (bearish) of its recent range.
2. Momentum Calculation & Smoothing Once the relative position is established, the script converts this into a momentum value. To prevent "market noise" from causing erratic movements, the logic applies a weighted smoothing technique. This ensures that more recent price actions carry more weight than older data, allowing the indicator to react quickly to new trends while remaining stable enough to ignore minor fluctuations.
3. Trend Identification (The Compass) The results of the smoothing process are used to generate trend signals. The script looks for specific crossovers in these smoothed values:
- UpTrend: When the momentum shifts significantly into positive territory, it identifies an upward trend.
- DownTrend: When the momentum shifts into negative territory, it identifies a downward trend.
The visual representation on your chart utilizes a Moving Average as a baseline, displaying specific "Trend Lines" only when the underlying momentum confirms a sustained direction. This helps filter out sideways (choppy) markets where no clear trend exists.
Automated Alert System
Beyond visual cues, the script includes an intelligent notification system. It monitors for "crosses"—the exact moment the momentum flips from negative to positive (Buy Signal) or positive to negative (Sell Signal).
When these shifts occur, the tool generates a real-time alert including:
- The current price of the asset.
- The specific currency pair or symbol.
- The current time and timeframe of the chart.
This allows traders to stay informed of trend changes instantly without needing to constantly monitor the screen for visual crossovers.
//===================================================================================================================================================//
#property copyright "Copyright 2014-2019, Nikolaos Pantzos"
#property link "https://www.mql5.com/en/users/pannik"
#property version "1.2"
#property description "iCompass"
#property strict
//===================================================================================================================================================//
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrMidnightBlue
#property indicator_color2 clrDarkViolet
#property indicator_width1 2
#property indicator_width2 2
//===================================================================================================================================================//
input int BarsCount = 10000;
input int MAperiods = 30;
input int ShiftBars = 0;
input bool AlertON = TRUE;
//===================================================================================================================================================//
double ExtBuffer0[];
double ExtBuffer1[];
double ExtBuffer2[];
double ExtBuffer3[];
double ExtBufferh1[];
double ExtBufferh2[];
double MovingAverage[];
bool AlertBuy=TRUE;
bool AlertSell=TRUE;
int AlertSignal=0;
//===================================================================================================================================================//
int OnInit(void)
{
IndicatorShortName(WindowExpertName());
IndicatorDigits(Digits);
IndicatorBuffers(7);
//---
SetIndexBuffer(0,ExtBufferh1);
SetIndexStyle(0,DRAW_LINE);
SetIndexLabel(0,"UpTrend");
//---
SetIndexBuffer(1,ExtBufferh2);
SetIndexStyle(1,DRAW_LINE);
SetIndexLabel(1,"DownTrend");
//---
SetIndexBuffer(2,ExtBuffer3);
SetIndexBuffer(3,ExtBuffer0);
SetIndexBuffer(4,ExtBuffer1);
SetIndexBuffer(5,ExtBuffer2);
SetIndexBuffer(6,MovingAverage);
return(INIT_SUCCEEDED);
}
//===================================================================================================================================================//
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 i;
int k;
int Limit=0;
double Value=0;
double LastValue=0;
double GetBuffer=0;
double MedianPrice;
double MinLow=0;
double MaxHigh=0;
double sum=0;
double sumw=0;
double weight;
double SmoothBars=NormalizeDouble(MAperiods/3,0);
int CountedBars=BarsCount;//IndicatorCounted();
if(CountedBars>Bars-1-SmoothBars)
CountedBars=Bars-1-(MAperiods/3);
if(CountedBars<0)
return(-1);
if(CountedBars>0)
CountedBars--;
Limit=CountedBars;
//-----------------------------------------------------------------------------------
for(i=0; i<Limit; i++)
{
MovingAverage[i]=iMA(NULL,0,MAperiods,0,0,0,i+ShiftBars);
MaxHigh=high[Highest(NULL,0,MODE_HIGH,MAperiods,i+ShiftBars)];
MinLow=low[Lowest(NULL,0,MODE_LOW,MAperiods,i+ShiftBars)];
MedianPrice=(high[i+ShiftBars]+low[i+ShiftBars])/2;
Value=0.33*2*((MedianPrice-MinLow)/(MaxHigh-MinLow)-0.5)+0.67*LastValue;
ExtBuffer0[i]=0.5*MathLog((1+Value)/(1-Value))+0.5*GetBuffer;
LastValue=Value;
GetBuffer=ExtBuffer0[i];
if(ExtBuffer0[i]>0)
ExtBuffer1[i]=10;
else
ExtBuffer1[i]=-10;
}
//-----------------------------------------------------------------------------------
for(i=Limit; i>=0; i--)
{
sum=0;
sumw=0;
for(k=0; k<SmoothBars && (i+k)<BarsCount; k++)
{
weight=SmoothBars-k;
sumw+=weight;
sum+=weight*ExtBuffer1[i+k];
}
if(sumw!=0)
ExtBuffer2[i]=sum/sumw;
else
ExtBuffer2[i]=0;
}
//-----------------------------------------------------------------------------------
for(i=0; i<=Limit; i++)
{
sum=0;
sumw=0;
for(k=0; k<SmoothBars && (i-k)>=0; k++)
{
weight=SmoothBars-k;
sumw+=weight;
sum+=weight*ExtBuffer2[i-k];
}
if(sumw!=0)
ExtBuffer3[i]=sum/sumw;
else
ExtBuffer3[i]=0;
}
//-----------------------------------------------------------------------------------
for(i=Limit-1; i>=0; i--)
{
ExtBufferh1[i]=EMPTY_VALUE;
ExtBufferh2[i]=EMPTY_VALUE;
//---
if(ExtBuffer3[i]>0)
{
ExtBufferh1[i]=MovingAverage[i];
if(ExtBuffer3[i+1]<0)
ExtBufferh1[i+1]=MovingAverage[i+1];
}
//---
if(ExtBuffer3[i]<0)
{
ExtBufferh2[i]=MovingAverage[i];
if(ExtBuffer3[i+1]>0)
ExtBufferh2[i+1]=MovingAverage[i+1];
}
//---
}
//-----------------------------------------------------------------------------------
//Pop up alerts
if(AlertON==TRUE)
{
if((AlertBuy==true) && ((ExtBuffer3[1]>0)) && ((ExtBuffer3[2]<0)))
{
AlertSignal=1;
AlertBuy=FALSE;
AlertSell=TRUE;
}
//---
if((AlertSell==true) && ((ExtBuffer3[1]<0)) && ((ExtBuffer3[2]>0)))
{
AlertSignal=-1;
AlertBuy=TRUE;
AlertSell=FALSE;
}
//-----------------------------------------------------------------------------------
if(AlertSignal>0)
{
Alert(WindowExpertName()+" => Buy signal @ "+DoubleToStr(close[0],Digits)+" "+Symbol()+", Time: "+TimeToString(TimeCurrent())+", "+DoubleToStr(Period(),0)+" Minutes Chart");
AlertSignal=0;
}
if(AlertSignal<0)
{
Alert(WindowExpertName()+" => Sell signal @ "+DoubleToStr(close[0],Digits)+" "+Symbol()+", Time: "+TimeToString(TimeCurrent())+", "+DoubleToStr(Period(),0)+" Minutes Chart");
AlertSignal=0;
}
}
//-----------------------------------------------------------------------------------
return(rates_total);
}
//===================================================================================================================================================//
Comments