NeutralHedge osc_v1
Download scriptThis tool is designed to analyze the relationship between two different financial instruments (for example, comparing a major currency pair like EURUSD against another asset) to identify price imbalances. It works by measuring how much one asset is "leading" or "trailing" compared to the other based on their historical price movements.
Core Logic and Mechanics
1. Comparative Analysis The script looks at two different symbols simultaneously. It identifies the highest and lowest prices reached by both instruments over a specific period of time (determined by your settings). By comparing these ranges, it calculates a mathematical "ratio" that tells us how much volatility one asset has relative to the other.
2. Mirroring and Correlation A key feature of this logic is the "Mirroring" capability. It can be configured to look at the correlation between the two assets in a standard way or in an inverted (mirrored) way. This allows users to see how one asset reacts when the other moves in opposite directions, helping to identify hidden trends that might not be obvious on a single chart.
3. The Calculation Process The script performs the following steps automatically:
- Range Identification: It finds the high and low extremes for both the main chart asset and the sub-symbol.
- Center Point Mapping: It calculates the "middle ground" (the average of the high and low) for both assets.
- Deviation Tracking: It tracks how far the current price of one asset has moved away from its respective center point, adjusted by the volatility ratio of the other asset.
4. Visual Output (The Histogram) Instead of just giving a number, the script translates these calculations into a visual histogram in a separate window:
- Positive Deviation: If the main asset is trading higher relative to the sub-symbol's movement, it populates one side of the histogram (typically representing a "Sell" sentiment or an overextended move).
- Negative Deviation: If the main asset is trading lower relative to the sub-symbol, it populates the other side (typically representing a "Buy" sentiment).
- Thresholds: The tool draws horizontal lines at specific levels. When the histogram bars cross these thresholds, it indicates that the price difference between the two assets has reached an extreme point, potentially signaling a reversal or a significant trend exhaustion.
Summary for the User
Essentially, this script helps you see if a currency or asset is becoming "stretched" compared to its historical relationship with another asset. When the bars on the histogram reach the magenta threshold lines, it suggests that the price gap between the two instruments has become unusually large, which can be used to identify potential trading opportunities based on price normalization.
//+------------------------------------------------------------------+
//| NeutralHedge osc_v1.mq4 |
//| © 2008.07.02 SwingMan |
//| |
//+------------------------------------------------------------------+
#property copyright "© 2008.07.02 SwingMan"
#property link ""
// source code:
//+------------------------------------------------------------------+
//| OverLay Chart.mq4 Ver.1.10 |
//| Copyright© 2006-2007 S.B.T. |
//| http://sufx.core.t3-ism.net/ |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+
//| This script is free to use/distribute/modify and re-distribute. |
//| (Limited to noncommercial use.) |
//+------------------------------------------------------------------+
// changes:
//+------------------------------------------------------------------+
// - fewhills / 2008.07.02
// I have modified the indicator with 2 extra parameters: starting time and TF period.
// The time parameter is the usual MT4 method: yyyy.mm.dd hh:mm
// The TF period is default at 1 Hr TF = 60
//+------------------------------------------------------------------+
// - SwingMan / 2008.07.02
// draw histogram of ratios
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 MediumSeaGreen
#property indicator_color2 Tomato
#property indicator_level1 0
#property indicator_levelcolor Gray
#property indicator_levelstyle STYLE_SOLID
//---- inputs --------------------------------------------------------
extern string SubSymbol = "EURUSD";
extern string StartingTimeCorr = "2008.07.01 00:00";
extern int TFperiod = 60;
extern double threshold = 70;
extern color threshold_color = Magenta;
extern bool Mirroring = true;
//--------------------------------------------------------------------
string sWinName = "NeutralHedge osc_v1";
//Indicator Buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//-- variables
double dPoint;
int iWindow = 1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
dPoint = MarketInfo(Symbol(), MODE_POINT);
sWinName = sWinName + " (" + Symbol() + "-" + SubSymbol + ") ";
IndicatorShortName(sWinName);
SetIndexBuffer( 0, ExtMapBuffer1 );
SetIndexBuffer( 1, ExtMapBuffer2 );
SetIndexStyle( 0, DRAW_HISTOGRAM, STYLE_SOLID, 2);
SetIndexStyle( 1, DRAW_HISTOGRAM, STYLE_SOLID, 2);
SetIndexEmptyValue( 0, 0.0 );
SetIndexEmptyValue( 1, 0.0 );
SetIndexLabel( 0, "BUY -" +Symbol() );
SetIndexLabel( 1, "SELL-" +Symbol() );
IndicatorDigits(Digits);
Draw_ThresholdLines();
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int _BarsCount;
double _CurRangeHigh, _CurRangeLow, _CurRangeCenter, _CurClose;
double _SubRangeHigh, _SubRangeLow, _SubRangeCenter;
double _SubPoint, _SubDigit;
double _SubOpen, _SubHigh, _SubLow, _SubClose;
double _PipsRatio;
double _GridPips, _GridPrice;
int _i;
int firstBar = iBarShift(NULL,TFperiod,StrToTime(StartingTimeCorr),true);
if (firstBar <0 )
{
Alert("StartingTimeCorr seems to be wrong."); return (0);
}
//-- Initialize Buffers
RefreshRates();
ArrayInitialize( ExtMapBuffer1, 0.0 );
ArrayInitialize( ExtMapBuffer2, 0.0 );
//-- Calculate Visible Bars
_BarsCount = WindowBarsPerChart() + 1;
int _FirstBar = firstBar; // WindowFirstVisibleBar();
int _LastBar = _FirstBar - _BarsCount + 1;
if ( _LastBar < 0 ) {
_LastBar = 0;
_BarsCount = _FirstBar + 1;
}
//-- Calculate Chart Ratio
_CurRangeHigh = High[Highest(Symbol(), 0, MODE_HIGH, _BarsCount, _LastBar)];
_CurRangeLow = Low[Lowest(Symbol(), 0, MODE_LOW, _BarsCount, _LastBar)];
_CurRangeCenter = ( _CurRangeHigh + _CurRangeLow ) * 0.5;
if ( Mirroring ) {
_SubRangeHigh = iLow( SubSymbol, 0, Lowest( SubSymbol, 0, MODE_LOW, _BarsCount, _LastBar ) );
_SubRangeLow = iHigh( SubSymbol, 0, Highest( SubSymbol, 0, MODE_HIGH, _BarsCount, _LastBar ) );
} else {
_SubRangeHigh = iHigh( SubSymbol, 0, Highest( SubSymbol, 0, MODE_HIGH, _BarsCount, _LastBar ) );
_SubRangeLow = iLow( SubSymbol, 0, Lowest( SubSymbol, 0, MODE_LOW, _BarsCount, _LastBar ) );
}
_SubRangeCenter = ( _SubRangeHigh + _SubRangeLow ) * 0.5;
_SubPoint = MarketInfo( SubSymbol, MODE_POINT );
_SubDigit = MarketInfo( SubSymbol, MODE_DIGITS );
_PipsRatio = ( _CurRangeHigh - _CurRangeLow ) / ( _SubRangeHigh - _SubRangeLow );
//-- Draw ratio
for ( _i = _LastBar; _i < _LastBar + _BarsCount; _i ++ ) {
_SubClose = iClose( SubSymbol, 0, _i ) - _SubRangeCenter;
double Close_Sub = _CurRangeCenter + _SubClose * _PipsRatio;
double Close_Main = iClose(Symbol(), 0, _i);
double range = (Close_Main - Close_Sub) / dPoint;
range = NormalizeDouble(range,0);
if (range > 0)
ExtMapBuffer2[_i] = range;
else
ExtMapBuffer1[_i] = range;
}
//----
return(0);
}
//+------------------------------------------------------------------+
void Draw_ThresholdLines()
{
iWindow = WindowFind(sWinName);
Draw_Line("threshLineUp", threshold, threshold_color);
Draw_Line("threshLineDn", -threshold, threshold_color);
return;
}
void Draw_Line(string sName, double value, color iColor)
{
ObjectCreate(sName, OBJ_HLINE, iWindow, 0, value);
ObjectSet(sName, OBJPROP_COLOR, iColor);
ObjectSet(sName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sName, OBJPROP_TIME1, 0);
ObjectSet(sName, OBJPROP_PRICE1, value);
return;
}
Comments