NeutralHedge osc_v2

Download script
Author: � 2008.07.02 SwingMan
NeutralHedge osc_v2

This script is designed to function as a specialized trading tool that analyzes the relationship between two different financial instruments—specifically, your primary trading pair and a secondary "sub-symbol" (such as EURUSD). The core purpose of this logic is to visualize the correlation or "ratio" between these two markets to help identify potential opportunities for balanced or hedged trading.

Core Analytical Logic

The tool operates by comparing the price movement of two symbols over a specific period of time. Here is how it processes that information:

  • Price Range Mapping: The script identifies the highest and lowest price points reached by both your main chart and the secondary symbol within a defined timeframe. It calculates a "center point" for both to establish a baseline for their movement.
  • Ratio Calculation: By comparing the price ranges of the two symbols, the script determines a mathematical ratio. This allows it to project how much the main symbol should be moving relative to the secondary symbol based on their historical behavior.
  • Difference Tracking (The Histogram): The primary visual output is a histogram. This represents the "gap" or difference between where the main symbol's price actually is and where it "should" be, based on the movements of the secondary symbol.
    • Positive/Negative Divergence: If the price difference favors one direction, it appears on one side of the histogram; if it favors the opposite, it appears on the other.
  • Threshold Monitoring: The script includes a specific "threshold" setting. This draws horizontal lines on your chart to serve as visual boundaries. When the histogram bars cross these levels, it indicates that the price divergence between the two instruments has reached a significant magnitude.

Trade Performance Tracking

Beyond just visualizing data, the script includes a manual reporting feature designed to track specific trade outcomes:

  • Entry Point Recording: You can input a specific time and type of order (Buy or Sell) into the settings.
  • Real-time Profit/Loss Calculation: Based on your manual entry, the script automatically calculates the current "Win/Loss" status. It does this by measuring the price change of your main position against the price change of the secondary symbol simultaneously.
  • Dynamic On-Screen Display: The script then prints a clear summary directly onto your chart window. This display shows:
    • The specific time you entered the trade.
    • The prices at the moment of entry for both symbols.
    • The current live prices for both symbols.
    • The net profit or loss calculated from those two movements combined.

Summary for the User

Essentially, this tool acts as a "comparison engine." Instead of looking at one currency pair in isolation, it allows you to see how that pair is behaving relative to another. It highlights when the two markets are moving out of sync (the histogram) and provides a live calculation of your profits or losses if you are trading them in a correlated way.

Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest prices of each barSeries array that contains close prices for each barSeries array that contains close prices for each bar
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
12 Views
0 Downloads
0 Favorites
NeutralHedge osc_v2
//+------------------------------------------------------------------+
//|                                          NeutralHedge osc_v2.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
// v1 - draw histogram of ratios
// v2 - write trading results
//+------------------------------------------------------------------+

#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 string EntryTime = "";
extern string _____Order____ = "BUY=0,  SELL=1";
extern int Order_Main = -1;
extern color threshold_color = Magenta;
extern bool Mirroring = true;
//--------------------------------------------------------------------

string sWinName = "NeutralHedge osc_v2";

//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();
   DeleteOwnObjects();
   
   return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{  
   DeleteOwnObjects();
   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;
   } 
   
   //-- write win/loss
   Write_WinLossTrading();
//----
   return(0);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//    Draw Threshold Lines
//+------------------------------------------------------------------+
string sObject = "NH_";
int fontSize_Text = 8;
int iLabelCorner = 1;
int xx1 = 80;
int xx2 = 2;
int xx3 = 60;
int xx4 = 30;
int yy0 = 3, yy;
int yStep = 10;


void Write_WinLossTrading()
{
   double WinLoss;
   string text1, text2, sOrder, sEmpty;
      
   //EntryTime = "2008.07.02 20:00";   
   if (EntryTime == "") return;
   yy = yy0;
   
   int entryBar = iBarShift(Symbol(),TFperiod,StrToTime(EntryTime),true);
   double Entry_Main = iClose(Symbol(), 0, entryBar);
   double Entry_Sub  = iClose(SubSymbol, 0, entryBar);
   double Price_Main = iClose(Symbol(), 0, 0);
   double Price_Sub  = iClose(SubSymbol, 0, 0);
   
   WinLoss = 0;   
   if (Order_Main == OP_BUY) {
      WinLoss = (Price_Main - Entry_Main) + (Entry_Sub - Price_Sub);
      sOrder = Symbol() + "-BUY:";
   } else 
   if (Order_Main == OP_SELL) {
      WinLoss = -(Price_Main - Entry_Main) - (Entry_Sub - Price_Sub);
      sOrder = Symbol() + "-SELL:";
   } else {
      Entry_Main = 0;
      Entry_Sub  = 0;
      sOrder = "Order_Main:";
      sEmpty = "EMPTY";
   }
   WinLoss = WinLoss / dPoint;

   //......................................................
   text1 = "Entry time:";
   text2 = EntryTime;
   Write_Label(text1, text2, xx1+10, xx2, yy); yy = yy + yStep;

   //......................................................
   text1 = sOrder;
   if (sOrder == "Order_Main:") {
      text2 = sEmpty;
      int xx5 = xx4;
   } else {
      text2 = DoubleToStr(Entry_Main,Digits) + " / " + DoubleToStr(Entry_Sub,Digits);
      xx5 = xx2;
   }   
   Write_Label(text1, text2, xx1, xx5, yy); yy = yy + yStep;
   
   //......................................................
   text1 = "Current price:";
   text2 = DoubleToStr(Price_Main,Digits) + " / " + DoubleToStr(Price_Sub,Digits);
   Write_Label(text1, text2, xx1, xx2, yy); yy = yy + yStep;
   
   //......................................................
   text1 = "WinLoss:";
   text2 = DoubleToStr(WinLoss,0);
   Write_Label(text1, text2, xx1, xx3, yy); yy = yy + yStep;   
}

void Write_Label(string text1, string text2, int xx1, int xx2, int yy)
{
   color dColor = Orange;
   string name1 = sObject + "1" + yy;
   string name2 = sObject + "2" + yy;
   SetLabelObject(name1, text1, dColor, xx1, yy);
   SetLabelObject(name2, text2, dColor, xx2, yy);
   return;
}

void SetLabelObject(string sName, string sText, color dColor, int xx, int yy)
{
   ObjectCreate(sName, OBJ_LABEL, iWindow, 0, 0);
        ObjectSetText(sName,sText,fontSize_Text, "Arial Bold", dColor);
        ObjectSet(sName, OBJPROP_CORNER, iLabelCorner);
        ObjectSet(sName, OBJPROP_XDISTANCE, xx);
        ObjectSet(sName, OBJPROP_YDISTANCE, yy);
   return;     
}

void DeleteOwnObjects()
{
   int i=0;
   while (i <= ObjectsTotal()) {
      if (StringFind(ObjectName(i), sObject) >= 0) ObjectDelete(ObjectName(i));
      else
      i++;
   }
   return;
}


//+------------------------------------------------------------------+
//    Draw Threshold Lines
//+------------------------------------------------------------------+
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