The Cauchy Difference indicator is a specialized technical analysis tool designed to visualize market behavior by calculating the mathematical gap between two different types of averages. Instead of looking at standard price movements, this script focuses on the "core" value of a candle and compares its arithmetic mean against its geometric growth.
How it works
To understand how this indicator produces its visual line, it is helpful to break down its logic into three distinct layers:
1. The Price Foundation (PC) The script begins by identifying a specific representative price for every candle. Rather than relying on just the closing price, it calculates the average of the four main data points: High, Low, Open, and Close. This provides a balanced "center point" for each period.
2. The Two Mathematical Models (MA & MG) Once those center points are established, the script calculates two different types of averages over a user-defined period (the default is 2):
- Arithmetic Average (MA): This is the standard way of averaging numbers—it adds all the values together and divides by the total count. It represents a simple, linear progression.
- Geometric Average (MG): This is a more complex calculation used to find the average rate of change. It multiplies the values together and then takes the $n$-th root. This type of math is often used to account for compounding growth or proportional changes.
3. The Difference (The Final Result) The final line you see on your chart is the result of subtracting the Geometric Average from the Arithmetic Average.
What this means for the trader
By plotting the "difference" between these two types of averages, the indicator highlights discrepancies in market momentum. When the arithmetic average and geometric average are perfectly aligned, the difference is zero. As they diverge, the line moves away from the center.
Essentially, this tool helps filter out standard price noise to show you how much a price's linear movement deviates from its proportional growth, providing a unique perspective on market volatility and trend stability.
//+------------------------------------------------------------------+
//| Cauchy difference.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_separate_window
#property indicator_buffers 1
#property indicator_color1 clrDarkViolet
#property indicator_width1 2
input int period=2;
//---- buffers
double ExtMapBuffer1[];
string name="";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
name="Cauchy difference";
IndicatorShortName(name);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexLabel(0,name);
SetIndexDrawBegin(0,period);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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=rates_total-prev_calculated;
if(prev_calculated==0)limit--;
else limit++;
if(limit==rates_total-1)limit=rates_total-period;
for(int i=0; i<limit && !IsStopped(); i++)
ExtMapBuffer1[i]=MA(i)-MG(i);
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
double PC(int _i=0)
{
double pc=0;
pc=((High[_i]+Open[_i]+Close[_i]+Low[_i])/4)/Point;
return(pc);
}
//----
double MA(int _i=0)
{
double ar= 0;
for(int i=_i; i<_i+period; i++)
{
ar+=PC(i);
}
return(ar/period);
}
//----
double MG(int _i=0)
{
double ge= 1;
for(int i=_i; i<_i+period; i++)
{
ge*=PC(i);
}
return(MathPow(ge,1.0/period));
}
//+------------------------------------------------------------------+
Comments