xedginves@yahoo.com__Neutral_Hedge_osc_v4.5

Author: � 2008.11.25_ICQ497827403_Man
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 bar
Indicators Used
Moving average indicatorStandard Deviation indicator
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
0 Views
0 Downloads
0 Favorites
xedginves@yahoo.com__Neutral_Hedge_osc_v4.5

#property copyright "© 2008.11.25_ICQ497827403_Man"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 MediumSeaGreen
#property indicator_color2 DeepPink
#property indicator_color3 Lime
#property indicator_color4 Aqua

#include <stdlib.mqh>

extern string SubSymbol = "";
extern int Period_Ratio = 72;
extern int nDrawBars = 144;
extern string ____Correlation____ = "1= correlated, 0=autom, -1= not correl";
extern int Pair_Correlation = 0;
extern string Trade_EntryTime = "";
extern string _____Order____ = "BUY=0,  SELL=1";
extern int Order_MainPair = -1;
extern double Factor_Threshold = 0.0;
string gs_132 = "xedginves@yahoo.com__NeutralHedge osc_v4.5";
string gs_140;
string gs_148 = "NH43_";
int gi_unused_156 = 8;
int gi_160 = 1;
int gi_164 = 80;
int gi_168 = 2;
int gi_unused_172 = 60;
int gi_unused_176 = 30;
int gi_180 = 3;
int gi_184;
int gi_188 = 10;
double g_ibuf_192[];
double g_ibuf_196[];
double g_ibuf_200[];
double g_ibuf_204[];
double g_ibuf_208[];
double g_ibuf_212[];
double g_point_216;
int gi_224;
double gd_228;
double gd_236;
int gi_244;
bool gi_248;
int gi_252 = 0;
bool gi_256 = TRUE;

void Set_DefaultPairCorrelation(string as_0, int ai_8, string &as_12, int &ai_20) {
   if (ai_8 == -1) ai_20 = 0;
   else
      if (ai_8 == 1) ai_20 = 1;
   if (as_12 == "") {
      if (as_0 == "GBPUSD") {
         as_12 = "EURUSD";
         if (ai_8 == 0) ai_20 = 0;
      } else {
         if (as_0 == "GBPJPY") {
            as_12 = "EURJPY";
            if (ai_8 == 0) ai_20 = 0;
         } else {
            if (as_0 == "EURUSD") {
               as_12 = "USDCHF";
               if (ai_8 == 0) ai_20 = 1;
            } else {
               if (as_0 == "USDJPY") {
                  as_12 = "AUDUSD";
                  if (ai_8 == 0) ai_20 = 1;
               }
            }
         }
      }
   }
}

double Get_ThresholdValue(double ad_0, double ada_8[], double ada_12[], int ai_16, int ai_20, int ai_24, int ai_28) {
   double lda_32[1000];
   ArraySetAsSeries(lda_32, TRUE);
   int l_period_36 = ai_20 - ai_16 + 1;
   int li_40 = -1;
   for (int li_44 = ai_16; li_44 < ai_20; li_44++) {
      li_40++;
      if (ada_8[li_44] != 0.0 && ada_8[li_44] != EMPTY_VALUE) lda_32[li_40] = MathAbs(ada_8[li_44]);
      else
         if (ada_12[li_44] != 0.0 && ada_12[li_44] != EMPTY_VALUE) lda_32[li_40] = MathAbs(ada_12[li_44]);
   }
   double l_ima_on_arr_48 = iMAOnArray(lda_32, 0, l_period_36, ai_24, MODE_SMA, ai_28);
   double l_istddev_on_arr_56 = iStdDevOnArray(lda_32, 0, l_period_36, ai_24, MODE_SMA, ai_28);
   double ld_ret_64 = l_ima_on_arr_48 + ad_0 * l_istddev_on_arr_56;
   return (ld_ret_64);
}

void Write_Label(int ai_0, int ai_4, string as_8, string as_16, string as_24, int ai_32, int ai_36, int ai_40) {
   int li_44 = 8;
   int li_48 = 42495;
   string ls_52 = as_8 + "1" + ai_40;
   string ls_60 = as_8 + "2" + ai_40;
   SetLabelObject(ai_0, ai_4, ls_52, as_16, li_44, li_48, ai_32, ai_40);
   SetLabelObject(ai_0, ai_4, ls_60, as_24, li_44, li_48, ai_36, ai_40);
}

void Write_LabelComment(int ai_0, int ai_4, string as_8, string as_16, string as_24, int ai_32, int ai_36, int ai_40) {
   int li_44 = 8;
   int li_48 = 42495;
   string ls_52 = as_8 + "C1" + ai_40;
   string ls_60 = as_8 + "C2" + ai_40;
   SetLabelObject(ai_0, ai_4, ls_52, as_16, li_44, li_48, ai_32, ai_40);
   SetLabelObject(ai_0, ai_4, ls_60, as_24, li_44, li_48, ai_36, ai_40);
}

void SetLabelObject(int a_window_0, int a_corner_4, string a_name_8, string a_text_16, int a_fontsize_24, color a_color_28, int a_x_32, int a_y_36) {
   ObjectCreate(a_name_8, OBJ_LABEL, a_window_0, 0, 0);
   ObjectSetText(a_name_8, a_text_16, a_fontsize_24, "Arial Bold", a_color_28);
   ObjectSet(a_name_8, OBJPROP_CORNER, a_corner_4);
   ObjectSet(a_name_8, OBJPROP_XDISTANCE, a_x_32);
   ObjectSet(a_name_8, OBJPROP_YDISTANCE, a_y_36);
}

void DeleteOwnObjects(string as_0) {
   int li_8 = 0;
   while (li_8 <= ObjectsTotal()) {
      if (StringFind(ObjectName(li_8), as_0) >= 0) ObjectDelete(ObjectName(li_8));
      else li_8++;
   }
}

int init() {
   int l_str2time_0;
   string ls_4;
   Set_DefaultPairCorrelation(Symbol(), Pair_Correlation, SubSymbol, gi_256);
   if (SubSymbol == "") {
      Alert("SubSymbol is missing!");
      return;
   }
   if (nDrawBars == 0) nDrawBars = Period_Ratio;
   if (Trade_EntryTime != "") {
      l_str2time_0 = StrToTime(Trade_EntryTime);
      if (l_str2time_0 > iTime(Symbol(), 0, 0)) {
         gi_248 = FALSE;
         ls_4 = Symbol() + " input ERROR: false Trade_EntryTime (" + Trade_EntryTime + ")";
         Alert(ls_4);
      } else gi_248 = TRUE;
   }
   if (gi_248 == TRUE) {
      if (Order_MainPair == -1) {
         ls_4 = Symbol() + "  " + "Order_MainPair is missing! Enter BUY=0 or SELL=1";
         Alert(ls_4);
         gi_248 = FALSE;
      }
   }
   g_point_216 = MarketInfo(Symbol(), MODE_POINT);
   gs_140 = gs_132 + " (" + Symbol() + "-" + SubSymbol + ", " + Period_Ratio + ") ";
   IndicatorShortName(gs_140);
   IndicatorBuffers(6);
   SetIndexBuffer(0, g_ibuf_196);
   SetIndexBuffer(1, g_ibuf_192);
   SetIndexBuffer(2, g_ibuf_208);
   SetIndexBuffer(3, g_ibuf_212);
   SetIndexBuffer(4, g_ibuf_200);
   SetIndexBuffer(5, g_ibuf_204);
   SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 2);
   SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 2);
   SetIndexStyle(2, DRAW_ARROW, EMPTY, 1);
   SetIndexArrow(2, 159);
   SetIndexStyle(3, DRAW_ARROW, EMPTY, 1);
   SetIndexArrow(3, 159);
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
   SetIndexEmptyValue(2, 0.0);
   SetIndexEmptyValue(3, 0.0);
   SetIndexEmptyValue(4, 0.0);
   SetIndexEmptyValue(5, 0.0);
   SetIndexLabel(0, "BUY -" + Symbol());
   SetIndexLabel(1, "SELL-" + Symbol());
   SetIndexLabel(2, "Thresh UP");
   SetIndexLabel(3, "Thresh DN");
   IndicatorDigits(Digits);
   ArrayInitialize(g_ibuf_192, 0.0);
   ArrayInitialize(g_ibuf_196, 0.0);
   ArrayInitialize(g_ibuf_200, 0.0);
   ArrayInitialize(g_ibuf_204, 0.0);
   ArrayInitialize(g_ibuf_208, 0.0);
   ArrayInitialize(g_ibuf_212, 0.0);
   DeleteOwnObjects(gs_148);
   gi_224 = WindowFind(gs_140);
   return (0);
}

int deinit() {
   DeleteOwnObjects(gs_148);
   return (0);
}

int start() {
   double l_high_0;
   double l_low_8;
   double ld_16;
   double ld_32;
   double ld_40;
   double ld_48;
   double ld_56;
   double ld_64;
   double ld_80;
   double l_iclose_88;
   double ld_96;
   if (SubSymbol == "") return (-1);
   RefreshRates();
   ArrayInitialize(g_ibuf_200, 0.0);
   ArrayInitialize(g_ibuf_204, 0.0);
   for (int li_76 = 0; li_76 < nDrawBars; li_76++) {
      l_high_0 = High[iHighest(Symbol(), 0, MODE_HIGH, Period_Ratio, li_76)];
      l_low_8 = Low[iLowest(Symbol(), 0, MODE_LOW, Period_Ratio, li_76)];
      ld_16 = (l_high_0 + l_low_8) / 2.0;
      if (gi_256 == FALSE) {
         ld_32 = iLow(SubSymbol, 0, iLowest(SubSymbol, 0, MODE_LOW, Period_Ratio, li_76));
         ld_40 = iHigh(SubSymbol, 0, iHighest(SubSymbol, 0, MODE_HIGH, Period_Ratio, li_76));
      } else {
         ld_32 = iHigh(SubSymbol, 0, iHighest(SubSymbol, 0, MODE_HIGH, Period_Ratio, li_76));
         ld_40 = iLow(SubSymbol, 0, iLowest(SubSymbol, 0, MODE_LOW, Period_Ratio, li_76));
      }
      ld_48 = (ld_32 + ld_40) / 2.0;
      ld_64 = (l_high_0 - l_low_8) / (ld_32 - ld_40);
      ld_56 = iClose(SubSymbol, 0, li_76) - ld_48;
      ld_80 = ld_16 + ld_56 * ld_64;
      l_iclose_88 = iClose(Symbol(), 0, li_76);
      ld_96 = (l_iclose_88 - ld_80) / g_point_216;
      if (ld_96 > 0.0) g_ibuf_192[li_76] = ld_96;
      else g_ibuf_196[li_76] = ld_96;
      if (li_76 == 0) gd_228 = ld_96;
      for (int li_72 = li_76; li_72 < li_76 + Period_Ratio; li_72++) {
         ld_56 = iClose(SubSymbol, 0, li_72) - ld_48;
         ld_80 = ld_16 + ld_56 * ld_64;
         l_iclose_88 = iClose(Symbol(), 0, li_72);
         ld_96 = (l_iclose_88 - ld_80) / g_point_216;
         if (ld_96 > 0.0) g_ibuf_200[li_72] = ld_96;
         else g_ibuf_204[li_72] = ld_96;
      }
      gi_244 = Get_ThresholdValue(Factor_Threshold, g_ibuf_200, g_ibuf_204, li_76, li_76 + Period_Ratio, 1, 0);
      g_ibuf_208[li_76] = gi_244;
      g_ibuf_212[li_76] = -gi_244;
      if (li_76 == 0) gd_236 = gi_244;
   }
   if (gi_248) Write_WinLossTrading();
   Write_CommentTrading();
   return (0);
}

void Write_WinLossTrading() {
   double ld_0;
   string ls_8;
   string ls_16;
   string ls_unused_24;
   int l_digits_32;
   double l_point_36;
   int l_shift_44;
   double l_iclose_48;
   double l_iclose_56;
   double l_iclose_64;
   double l_iclose_72;
   double ld_80;
   double ld_88;
   string ls_96;
   string ls_104;
   double ld_112;
   double l_iclose_120;
   double l_iclose_128;
   double ld_136;
   double ld_144;
   double ld_152;
   if (Trade_EntryTime != "") {
      ls_unused_24 = "EMPTY";
      gi_184 = gi_180;
      gi_224 = WindowFind(gs_140);
      gi_160 = TRUE;
      l_digits_32 = MarketInfo(SubSymbol, MODE_DIGITS);
      l_point_36 = MarketInfo(SubSymbol, MODE_POINT);
      l_shift_44 = iBarShift(Symbol(), 0, StrToTime(Trade_EntryTime), TRUE);
      l_iclose_48 = iClose(Symbol(), 0, l_shift_44);
      l_iclose_56 = iClose(SubSymbol, 0, l_shift_44);
      l_iclose_64 = iClose(Symbol(), 0, 0);
      l_iclose_72 = iClose(SubSymbol, 0, 0);
      ld_0 = 0;
      if (Order_MainPair == 0) {
         ld_80 = (l_iclose_64 - l_iclose_48) / g_point_216;
         ld_88 = (l_iclose_56 - l_iclose_72) / l_point_36;
         ld_0 = ld_80 + ld_88;
         ls_96 = "buy " + Symbol() + ": " + DoubleToStr(l_iclose_48, Digits) + " / " + DoubleToStr(l_iclose_64, Digits) + " = " + DoubleToStr(ld_80, 0);
         ls_104 = "sell " + SubSymbol + ": " + DoubleToStr(l_iclose_56, l_digits_32) + " / " + DoubleToStr(l_iclose_72, l_digits_32) + " = " + DoubleToStr(ld_88, 0);
      } else {
         if (Order_MainPair == 1) {
            ld_80 = (-(l_iclose_64 - l_iclose_48)) / g_point_216;
            ld_88 = (-(l_iclose_56 - l_iclose_72)) / l_point_36;
            ld_0 = ld_80 + ld_88;
            ls_96 = "sell " + Symbol() + ": " + DoubleToStr(l_iclose_48, Digits) + " / " + DoubleToStr(l_iclose_64, Digits) + " = " + DoubleToStr(ld_80, 0);
            ls_104 = "buy " + SubSymbol + ": " + DoubleToStr(l_iclose_56, l_digits_32) + " / " + DoubleToStr(l_iclose_72, l_digits_32) + " = " + DoubleToStr(ld_88, 0);
         } else {
            ls_96 = "trade " + Symbol() + ": " + "NONE" + " / " + DoubleToStr(l_iclose_64, Digits);
            ls_104 = "trade " + SubSymbol + ": " + "NONE" + " / " + DoubleToStr(l_iclose_72, l_digits_32);
         }
      }
      ld_112 = -2147483647;
      if (Order_MainPair == 0) {
         for (int li_160 = 0; li_160 < l_shift_44; li_160++) {
            l_iclose_120 = iClose(Symbol(), 0, li_160);
            l_iclose_128 = iClose(SubSymbol, 0, li_160);
            ld_136 = (l_iclose_120 - l_iclose_48) / g_point_216;
            ld_144 = (l_iclose_56 - l_iclose_128) / l_point_36;
            ld_152 = ld_136 + ld_144;
            if (ld_152 > ld_112) ld_112 = ld_152;
         }
      } else {
         if (Order_MainPair == 1) {
            for (li_160 = 0; li_160 < l_shift_44; li_160++) {
               l_iclose_120 = iClose(Symbol(), 0, li_160);
               l_iclose_128 = iClose(SubSymbol, 0, li_160);
               ld_136 = (-(l_iclose_120 - l_iclose_48)) / g_point_216;
               ld_144 = (-(l_iclose_56 - l_iclose_128)) / l_point_36;
               ld_152 = ld_136 + ld_144;
               if (ld_152 > ld_112) ld_112 = ld_152;
            }
         }
      }
      ls_8 = "Entry time:";
      ls_16 = Trade_EntryTime;
      Write_Label(gi_224, gi_160, gs_148, ls_8, ls_16, gi_164 + 10, gi_168, gi_184);
      gi_184 += gi_188;
      Write_Label(gi_224, gi_160, gs_148, ls_96, "", gi_168, gi_168, gi_184);
      gi_184 += gi_188;
      Write_Label(gi_224, gi_160, gs_148, ls_104, "", gi_168, gi_168, gi_184);
      gi_184 += gi_188;
      ls_8 = "WinLoss: " + DoubleToStr(ld_0, 0) + " (max= " + DoubleToStr(ld_112, 0) + ")";
      Write_Label(gi_224, gi_160, gs_148, ls_8, "", gi_168, gi_168, gi_184);
      gi_184 += gi_188;
   }
}

void Write_CommentTrading() {
   string ls_0;
   string ls_16;
   string ls_44;
   string ls_52;
   int li_8 = 3;
   int li_12 = 12;
   gi_160 = FALSE;
   if (gi_256 == FALSE) ls_16 = "Correlation= false";
   else ls_16 = "Correlation= true";
   Write_LabelComment(gi_224, gi_160, gs_148, ls_16, "", li_8, li_8, li_12);
   li_12 += gi_188;
   string ls_24 = "HedgeRange= " + DoubleToStr(gd_228, 1);
   Write_LabelComment(gi_224, gi_160, gs_148, ls_24, "", li_8, li_8, li_12);
   li_12 += gi_188;
   if (gd_228 < 0.0) ls_0 = "Threshold= " + DoubleToStr(-gd_236, 1);
   else ls_0 = "Threshold= " + DoubleToStr(gd_236, 1);
   Write_LabelComment(gi_224, gi_160, gs_148, ls_0, "", li_8, li_8, li_12);
   li_12 += gi_188;
   int l_datetime_32 = iTime(Symbol(), 0, gi_252);
   string ls_36 = "(lastBar= " + TimeToStr(l_datetime_32) + ")";
   Write_LabelComment(gi_224, gi_160, gs_148, ls_36, "", li_8, li_8, li_12);
   if (gd_228 < 0.0) {
      ls_44 = "buy " + Symbol() + ", sell " + SubSymbol;
      if (gd_228 < -gd_236) {
         ls_52 = "Trading: OK";
         GlobalVariableSet("OK", 1);
      } else {
         ls_52 = "Trading: NONE";
         GlobalVariableSet("OK", 0);
      }
   } else {
      ls_44 = "sell " + Symbol() + ", buy " + SubSymbol;
      if (gd_228 > gd_236) {
         ls_52 = "Trading: OK";
         GlobalVariableSet("OK", 1);
      } else {
         ls_52 = "Trading: NONE";
         GlobalVariableSet("OK", 0);
      }
   }
   li_12 += gi_188;
   Write_LabelComment(gi_224, gi_160, gs_148, ls_52, "", li_8, li_8, li_12);
   li_12 += gi_188;
   Write_LabelComment(gi_224, gi_160, gs_148, ls_44, "", li_8, li_8, li_12);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---