/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2010, neatoLAB"
#property link      "http://www.neatolab.com"

#property indicator_chart_window

#include <WinUser32.mqh>

/*#import "Method311.dll"
   string GetSignal(double& a0[], int a1, double& a2[], double& a3[], double& a4[], double& a5[], double a6, int a7, int a8, double& a9[], double& a10[], double& a11[], int a12, string a13);
   int GetNext(double& a0[], int a1, double& a2[], double& a3[], double a4, double a5, int a6, int a7);
#import*/

int gi_76 = 18;
int gi_80 = 0;
double gd_84 = 2.8;
int gi_92 = 30;
extern string FontName = "Arial";
extern int FontSize = 9;
extern color BuyColour = LimeGreen;
extern color SellColour = Red;
string gs_116 = "Set below to 0 to disable";
double gd_124 = 30.0;
double gd_132 = 70.0;
double gd_140 = 90.0;
double gd_148 = 50.0;
double gd_156 = 35.0;
bool gi_164 = TRUE;
double gd_168 = 2.0;
double gd_176 = 0.0;
bool gi_184 = FALSE;
extern double PercentToRisk = 2.0;
extern bool SendEmail = FALSE;
extern bool AlertPopup = FALSE;
extern bool UpcomingSignalEmail = FALSE;
extern string AuthCode = "NQQYOLBHMZHR";
string gs_arial_216 = "Arial";
int gi_224 = 11;
int gi_228 = Orange;
int gi_232 = LightBlue;
double gda_236[];
double gda_240[];
double gda_244[];
double gda_248[];
double gda_252[];
double gd_256 = 1.0;
string gs_264;
double gda_272[1];
double gda_276[1];
double gda_280[1];
double gd_284;
double gd_292;
double gd_300;
double gda_308[12];
bool gi_312;
int gi_316;
double gd_320;
double gd_328;
int gi_336;
int gi_340;
int gi_344;
bool gi_348;
datetime gt_352;
int gi_356;
datetime gt_360;

int init() {
   gd_256 = CorrectDigit();
   ArrayResize(gda_244, gi_92 + 1);
   ArrayResize(gda_248, gi_92 + 1);
   ArrayResize(gda_252, gi_92 + 1);
   ArrayResize(gda_240, gi_92 + 1);
   ObjectsDeleteAll(0);
   gda_308[0] = gi_76;
   gda_308[1] = gi_80;
   gda_308[2] = gd_84;
   gda_308[3] = gd_124;
   gda_308[4] = gd_132;
   gda_308[5] = gd_140;
   gda_308[6] = gd_148;
   gda_308[7] = gd_156;
   gda_308[8] = 0;
   gda_308[9] = gd_168;
   gda_308[10] = gd_176;
   gda_308[11] = 0;
   if (gi_164) gda_308[8] = 1;
   if (gi_184) gda_308[11] = 1;
   gi_344 = 0;
   gt_352 = 0;
   gi_356 = 0;
   return (0);
}

int deinit() {
   gi_344 = 0;
   ObjectsDeleteAll(0);
   Comment("");
   gt_352 = 0;
   gi_356 = 0;
   return (0);
}

int start() {
   if (StringFind(Symbol(), "EURJPY") >= 0 && Period() == PERIOD_M30) DrawResults();
   else {
      Comment("\nIncorrect Pair/Timeframe\n", 
      "\nPlease make sure you have selected EUR/JPY, with the M30 chart");
   }
   return (0);
}

void DrawResults() {
   int li_0;
   int li_4;
   int li_8;
   double ld_20;
   double ld_28;
   int li_40;
   int li_44;
   string ls_52;
   int li_12 = 0;
   if (gi_344 > Bars) gi_344 = Bars - 2;
   int li_16 = gi_344;
   int li_36 = 0;
   ArrayResize(gda_236, Bars + gi_92 + gi_76);
   if (Bars > gi_92) {
      li_40 = Bars - li_16;
      if (li_16 >= 0) li_40 += gi_92;
      gda_236[0] = iMA(NULL, 0, gi_76, gi_80, MODE_SMA, PRICE_TYPICAL, 0);
      ArrayCopy(gda_252, Close, 0, 0, gi_92);
      ArrayCopy(gda_240, gda_236, 0, 0, gi_92);
      gda_240[0] = iMA(NULL, 0, gi_76, gi_80, MODE_SMA, PRICE_CLOSE, 0);
      //li_44 = GetNext(gda_308, gi_92, gda_240, gda_252, High[0], Low[0], Time[0], Digits);
      if (li_44 > 0) gi_348 = TRUE;
      if (gi_348) gi_336 = Time[0] + 1800 - TimeCurrent();
      DisplayInfo();
      if (gt_360 < Time[0] && gi_336 > 0) {
         gt_360 = Time[0];
         if (SendEmail == TRUE) SendMail("Method 311 Potential Signal Alert!", "There is a potential signal coming up in " + TimeToStr(gi_336, TIME_SECONDS) + "\r\n");
         if (AlertPopup == TRUE) li_4 = MessageBoxA(0, "There is a potential signal coming up in " + TimeToStr(gi_336, TIME_SECONDS), "Method 311 Potential Signal Alert!", 64);
      }
      li_8 = Bars - li_16;
      if (li_8 + gi_92 >= Bars) li_8 = Bars - gi_92;
      li_0 = 1;
      if (gt_352 < Time[1]) {
         Print("Processing Bar...");
         if (Time[1] > gt_352) gt_352 = Time[1];
         for (int li_48 = 1; li_48 < Bars - gi_76; li_48++) gda_236[li_48] = iMA(NULL, 0, gi_76, gi_80, MODE_SMA, PRICE_CLOSE, li_48);
         while (li_0 < li_8) {
            li_0++;
            gi_344++;
            gi_336 = 0;
            gi_340 = 0;
            gi_348 = FALSE;
            if (Time[li_0] > gt_352) gt_352 = Time[li_0];
            gda_272[0] = 0;
            gda_276[0] = 0;
            gda_280[0] = 0;
            ArrayCopy(gda_244, High, 0, li_0, gi_92);
            ArrayCopy(gda_248, Low, 0, li_0, gi_92);
            ArrayCopy(gda_252, Close, 0, li_0, gi_92);
            ArrayCopy(gda_240, gda_236, 0, li_0, gi_92);
            //ls_52 = GetSignal(gda_308, gi_92, gda_240, gda_248, gda_244, gda_252, Open[li_0 - 1], Time[li_0 - 1], Digits, gda_272, gda_276, gda_280, AccountNumber(), AuthCode);
            if (ls_52 != "") {
               Comment(ls_52);
               gi_344 = 0;
               gt_352 = 0;
               return;
            }
            if (gda_272[0] > gda_276[0]) {
               gda_272[0] += MarketInfo(Symbol(), MODE_SPREAD) * Point;
               gda_276[0] += MarketInfo(Symbol(), MODE_SPREAD) * Point;
               gda_280[0] += MarketInfo(Symbol(), MODE_SPREAD) * Point;
               if (li_12 == 0) {
                  gi_356 = Time[li_0 - 1];
                  gi_312 = TRUE;
                  gi_316 = Time[li_0 - 1];
                  gd_284 = gda_272[0];
                  gd_292 = gda_276[0];
                  gd_300 = gda_280[0];
                  if (gd_284 > 0.0 && MarketInfo(Symbol(), MODE_TICKVALUE) > 0.0) {
                     ld_20 = AccountBalance() * (PercentToRisk / 100.0);
                     ld_28 = MathAbs((Open[li_0 - 1] - gd_284) / (Point * gd_256));
                     gd_320 = NormalizeDouble(ld_20 / ld_28 / (MarketInfo(Symbol(), MODE_TICKVALUE) * gd_256), 2);
                     gd_320 /= 2.0;
                  }
                  DisplayInfo();
                  if (SendEmail == TRUE && Time[0] == gi_316) {
                     SendMail("Method 311 Sell Signal Alert!", "Sell signal at: " + TimeToStr(gi_316) 
                        + "\n" 
                        + "\nStopLoss: " + gd_284 
                        + "\nTakeProfit1: " + gd_292 
                        + "\nTakeProfit2: " + gd_300 
                     + "\nSuggested Lot Size: " + gd_320);
                  }
                  if (AlertPopup == TRUE && Time[0] == gi_316) {
                     li_4 = MessageBoxA(0, "Sell signal generated at: " + TimeToStr(gi_316) 
                        + "\nStopLoss: " + gd_284 
                        + "\nTakeProfit1: " + gd_292 
                     + "\nTakeProfit2: " + gd_300, "Method311 Alert!", 48);
                  }
               }
               li_12++;
               ObjectCreate("arrow " + Time[li_0], OBJ_TEXT, 0, Time[li_0], Close[li_0]);
               ObjectSetText("arrow " + Time[li_0], CharToStr(234), 2.5 * FontSize, "Wingdings", SellColour);
               ObjectCreate("SL1 at bar " + Time[li_0], OBJ_TEXT, 0, Time[li_0], gda_272[0]);
               ObjectSetText("SL1 at bar " + Time[li_0], "SL1: " + DoubleToStr(gda_272[0], Digits), FontSize, FontName, SellColour);
               ObjectCreate("TP1 at bar " + Time[li_0], OBJ_TEXT, 0, Time[li_0], gda_276[0]);
               ObjectSetText("TP1 at bar " + Time[li_0], "TP1: " + DoubleToStr(gda_276[0], Digits), FontSize, FontName, SellColour);
               ObjectCreate("TP2 at bar " + Time[li_0], OBJ_TEXT, 0, Time[li_0], gda_280[0]);
               ObjectSetText("TP2 at bar " + Time[li_0], "TP2: " + DoubleToStr(gda_280[0], Digits), FontSize, FontName, SellColour);
            }
            if (gda_272[0] < gda_276[0]) {
               gda_272[0] = gda_272[0] - MarketInfo(Symbol(), MODE_SPREAD) * Point;
               gda_276[0] = gda_276[0] - MarketInfo(Symbol(), MODE_SPREAD) * Point;
               gda_280[0] = gda_280[0] - MarketInfo(Symbol(), MODE_SPREAD) * Point;
               if (li_12 == 0) {
                  gi_356 = Time[li_0 - 1];
                  gi_312 = FALSE;
                  gi_316 = Time[li_0 - 1];
                  gd_284 = gda_272[0];
                  gd_292 = gda_276[0];
                  gd_300 = gda_280[0];
                  if (gd_284 > 0.0 && MarketInfo(Symbol(), MODE_TICKVALUE) > 0.0) {
                     ld_20 = AccountBalance() * (PercentToRisk / 100.0);
                     ld_28 = MathAbs((Open[li_0 - 1] - gd_284) / (Point * gd_256));
                     gd_320 = NormalizeDouble(ld_20 / ld_28 / (MarketInfo(Symbol(), MODE_TICKVALUE) * gd_256), 2);
                     gd_320 /= 2.0;
                  }
                  DisplayInfo();
                  if (SendEmail == TRUE && Time[0] == gi_316) {
                     SendMail("Method 311 Buy Signal Alert!", "Buy signal at: " + TimeToStr(gi_316) 
                        + "\n" 
                        + "\nStopLoss: " + gd_284 
                        + "\nTakeProfit1: " + gd_292 
                        + "\nTakeProfit2: " + gd_300 
                     + "\nSuggested Lot Size: " + gd_320);
                  }
                  if (AlertPopup == TRUE && Time[0] == gi_316) {
                     li_4 = MessageBoxA(0, "Sell signal generated at: " + TimeToStr(gi_316) 
                        + "\nStopLoss: " + gd_284 
                        + "\nTakeProfit1: " + gd_292 
                     + "\nTakeProfit2: " + gd_300, "Method311 Alert!", 48);
                  }
               }
               li_12++;
               ObjectCreate("arrow " + Time[li_0], OBJ_TEXT, 0, Time[li_0], Close[li_0]);
               ObjectSetText("arrow " + Time[li_0], CharToStr(233), 2.5 * FontSize, "Wingdings", BuyColour);
               ObjectCreate("SL1 at bar " + Time[li_0], OBJ_TEXT, 0, Time[li_0], gda_272[0]);
               ObjectSetText("SL1 at bar " + Time[li_0], "SL1: " + DoubleToStr(gda_272[0], Digits), FontSize, FontName, BuyColour);
               ObjectCreate("TP1 at bar " + Time[li_0], OBJ_TEXT, 0, Time[li_0], gda_276[0]);
               ObjectSetText("TP1 at bar " + Time[li_0], "TP1: " + DoubleToStr(gda_276[0], Digits), FontSize, FontName, BuyColour);
               ObjectCreate("TP2 at bar " + Time[li_0], OBJ_TEXT, 0, Time[li_0], gda_280[0]);
               ObjectSetText("TP2 at bar " + Time[li_0], "TP2: " + DoubleToStr(gda_280[0], Digits), FontSize, FontName, BuyColour);
            }
            if (li_12 > 10) {
               gi_344 = Bars - 1;
               li_0 = li_8;
            }
         }
      }
      DisplayInfo();
   }
}

int CorrectDigit() {
   if (Digits == 6) return (100);
   if (Digits == 5) return (10);
   if (Digits == 4) return (1);
   if (Digits == 3) return (10);
   if (Digits == 2) return (1);
   return (0);
}

void DisplayInfo() {
   double ld_20;
   color li_0 = Gray;
   string ls_4 = "No Signal";
   string ls_12 = "";
   Comment("");
   DrawBox();
   DrawStatic();
   if (gi_312 == FALSE) {
      li_0 = LimeGreen;
      ls_4 = "BUY";
      ls_12 = CharToStr(233);
      ld_20 = (gd_300 - gd_292) / (Point * gd_256);
   }
   if (gi_312 == TRUE) {
      li_0 = Red;
      ls_4 = "SELL";
      ls_12 = CharToStr(234);
      ld_20 = (gd_292 - gd_300) / (Point * gd_256);
   }
   if (ld_20 > 0.0) gd_328 = ld_20;
   double ld_28 = PercentToRisk / 100.0;
   int li_36 = 3;
   ObjectCreate("zMethod311-v1-1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v1-1", ls_12, gi_224, "WingDings", li_0);
   ObjectSet("zMethod311-v1-1", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v1-1", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   ObjectCreate("zMethod311-v1-2", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v1-2", ls_4, gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v1-2", OBJPROP_XDISTANCE, 190);
   ObjectSet("zMethod311-v1-2", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   li_36++;
   li_36++;
   ObjectCreate("zMethod311-v2", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v2", TimeToStr(gi_316), gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v2", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-v2", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   if (gd_292 == 0.0) li_0 = Gray;
   li_36++;
   ObjectCreate("zMethod311-v3", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v3", DoubleToStr(gd_292, Digits), gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v3", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v3", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   li_36++;
   ObjectCreate("zMethod311-v4", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v4", DoubleToStr(gd_300, Digits), gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v4", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v4", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   li_36++;
   ObjectCreate("zMethod311-v5", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v5", DoubleToStr(gd_284, Digits), gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v5", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v5", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   if (gi_312 == FALSE) li_0 = LimeGreen;
   if (gi_312 == TRUE) li_0 = Red;
   li_36++;
   ObjectCreate("zMethod311-v6", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v6", DoubleToStr(gd_328, 1) + " pips behind", gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v6", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v6", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   if (gd_292 == 0.0) li_0 = Gray;
   li_36 += 2;
   ObjectCreate("zMethod311-v7", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-v7", DoubleToStr(gd_320, 2), gi_224, gs_arial_216, li_0);
   ObjectSet("zMethod311-v7", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v7", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
   li_36++;
   ObjectCreate("zMethod311-v8", OBJ_LABEL, 0, 0, 0, 0, 0);
   if (gi_336 > 0) ObjectSetText("zMethod311-v8", TimeToStr(gi_336, TIME_SECONDS), gi_224, gs_arial_216, Yellow);
   else ObjectSetText("zMethod311-v8", "No Upcoming Trade", gi_224, gs_arial_216, Gray);
   ObjectSet("zMethod311-v8", OBJPROP_XDISTANCE, 170);
   ObjectSet("zMethod311-v8", OBJPROP_YDISTANCE, li_36 * (gi_224 + 3) + 10);
}

void DrawStatic() {
   int li_0 = 1;
   ObjectCreate("zMethod311-1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-1", "Method 311", gi_224 + 2, gs_arial_216, gi_228);
   ObjectSet("zMethod311-1", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-1", OBJPROP_YDISTANCE, li_0 * gi_224 + 10);
   li_0++;
   ObjectCreate("zMethod311-2", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-2", "----------------------------", gi_224, gs_arial_216, gi_228);
   ObjectSet("zMethod311-2", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-2", OBJPROP_YDISTANCE, li_0 * (gi_224 + 1) + 10);
   li_0++;
   ObjectCreate("zMethod311-3", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-3", "Last Signal was:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-3", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-3", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-4", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-4", "Trade should be entered as close to", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-4", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-4", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-4-1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-4-1", "as possible (market time)", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-4-1", OBJPROP_XDISTANCE, 140);
   ObjectSet("zMethod311-4-1", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-5", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-5", "First Take Profit:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-5", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-5", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-6", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-6", "Second Take Profit:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-6", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-6", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-7", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-7", "Stop Loss:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-7", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-7", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-8", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-8", "Custom Trailing Stop:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-8", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-8", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-9", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-9", "----------------------------", gi_224, gs_arial_216, gi_228);
   ObjectSet("zMethod311-9", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-9", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-10", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-10", "Suggested Lot Size:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-10", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-10", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
   li_0++;
   ObjectCreate("zMethod311-11", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText("zMethod311-11", "Potential signal in:", gi_224, gs_arial_216, gi_232);
   ObjectSet("zMethod311-11", OBJPROP_XDISTANCE, 20);
   ObjectSet("zMethod311-11", OBJPROP_YDISTANCE, li_0 * (gi_224 + 3) + 10);
}

void DrawBox() {
   for (int li_4 = 0; li_4 <= 15; li_4 += 3) {
      for (int li_0 = 0; li_0 <= 24; li_0 += 3) {
         ObjectCreate(StringConcatenate("y", li_0, li_4, "-box"), OBJ_LABEL, 0, 0, 0, 0, 0);
         ObjectSetText(StringConcatenate("y", li_0, li_4, "-box"), CharToStr(110), 4.5 * gi_224, "WingDings", Black);
         ObjectSet(StringConcatenate("y", li_0, li_4, "-box"), OBJPROP_XDISTANCE, gi_224 * li_0);
         ObjectSet(StringConcatenate("y", li_0, li_4, "-box"), OBJPROP_YDISTANCE, gi_224 * li_4);
      }
   }
}