/*
   Generated by EX4-TO-MQ4 decompiler ONE FILE V4.0.218.3
   EX4 ID : 04E20A41724C2645BAEB3FDF008C44EC
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Forexeasystems © 2008"
#property link      "http://www.forexeasystems.com"

#include <EA_SIGMA1.0_ULTIMATE_Trade.mqh>

#import "EA_SIGMA1.0_ULTIMATE_Proxy.dll"
   int Init(string a0, string a1, int a2, double a3, double a4, int a5, double a6, double a7, int a8, int a9, string a10, int a11, double a12, int a13, string a14);
   double GetBid(string a0, double a1);
   double MoneyManagement(double a0, double a1, double a2);
#import "EA_SIGMA1.0_ULTIMATE_Version.dll"
   string Version(string a0);
#import

extern string SYSTEM_CONTROL_PANEL = "";
extern int MagicNumber = 753860;
extern bool MoneyManagement = TRUE;
extern double Lots = 0.1;
extern int StopLoss = 40;
extern double TakeProfit = 8.0;
extern int MaximumSlippage = 3;
extern double MaxTradeSize = 150.0;
extern int MaximumRisk = 3;
extern bool FridayMode = FALSE;
extern bool FractionalLots = FALSE;
extern bool ST_TP_Pending_Mode = FALSE;
extern int StartTime = 22;
extern int EndTime = 4;
extern double ATR_Period = 97.0;
extern double UrATR = 0.0013;
int gi_164 = 13;
int gi_168 = 999;
int gi_172 = 15;
int gi_176 = 1;
double gd_180;
string g_symbol_188;
int gi_196 = 1;
int gi_200;
int gi_204;
int gi_208 = 1;
bool gi_212 = TRUE;
int gi_216 = 0;
string gs_220 = "ExpertAdvisor_I";
string gs_228 = "EA SIGMA ULTIMATE 1.0 Build 01";

int init() {
   double l_lotstep_0;
   HideTestIndicators(TRUE);
   if (IsLibrariesAllowed() == FALSE) {
      Print("EA SIGMA >> PLEASE ALLOW EX4 IMPORT");
      Alert("EA SIGMA >> PLEASE ALLOW EX4 IMPORT");
      return (0);
   }
   if (IsDllsAllowed() == FALSE) {
      Print("EA SIGMA >> PLEASE ALLOW DLL IMPORT");
      Alert("EA SIGMA >> PLEASE ALLOW DLL IMPORT");
      return (0);
   }
   if (FractionalLots == TRUE) {
      l_lotstep_0 = MarketInfo(Symbol(), MODE_LOTSTEP);
      if (l_lotstep_0 != 0.01) Alert("EA SIGMA 1.0 ULTIMATE >> NOT SUPPORTED FRACTIONAL LOT");
   }
   if (IsTradeContextBusy() == TRUE) Alert("EA SIGMA 1.0 ULTIMATE  >> TRADE CONTEX BUSY");
   if (MaximumSlippage > 10) Alert("EA SIGMA 1.0 ULTIMATE  >> MAX SLIPPAGE TO HIGH");
   if (MaximumSlippage < 2) Alert("EA SIGMA 1.0 ULTIMATE  >> MAX SLIPPAGE TO LITTLE");
   if (IsStopped() == TRUE) Alert("EA SIGMA 1.0 ULTIMATE  >> AUTO TRADING STOPPED BY BROKERSERVER");
   if (Period() != PERIOD_M30) Alert("EA SIGMA 1.0 ULTIMATE  >> PLEASE PLACE THE EA ON THE 30 MIN EURGBP CHART");
   if (Bars < 1000) Alert("EA SIGMA 1.0 ULTIMATE  >> LESS THEN 1000 BARS IN THE CHART - EA CANNOT WORK");
   if (StringSubstr(Symbol(), 0, 6) != "EURGBP" && StringSubstr(Symbol(), 0, 7) != "EURGBPm") Alert("EA SIGMA 1.0 ULTIMATE >> PLEASE PLACE THE EA ON THE 30 MIN EURGBP CHART");
   if (MaximumRisk == 0) {
      Alert("EA SIGMA 1.0 ULTIMATE  >> MAXIMUM RISK IS EQUAL NULL");
      MaximumRisk = 1;
   }
   if (MaximumRisk > 100) {
      Alert("EA SIGMA 1.0 ULTIMATE  >> MAXIMUM RISK OVER 100");
      MaximumRisk = 100;
   }
   CheckVersion();
   if (FractionalLots == TRUE && MarketInfo(Symbol(), MODE_LOTSTEP) == 0.01) gi_176 = 2;
   if (Digits == 5) gd_180 = 10.0 * Point;
   else gd_180 = Point;
   string ls_unused_8 = StringSubstr(Symbol(), 0, 3);
   string ls_16 = StringSubstr(Symbol(), 3, 3);
   string ls_24 = "";
   if (StringLen(Symbol()) > 6) ls_24 = StringSubstr(Symbol(), 6, StringLen(Symbol()) - 6);
   g_symbol_188 = AccountCurrency() + ls_16 + ls_24;
   double ld_32 = GetBid(g_symbol_188, MarketInfo(g_symbol_188, MODE_BID));
   if (ld_32 == 0.0) {
      g_symbol_188 = ls_16 + AccountCurrency() + ls_24;
      ld_32 = GetBid(g_symbol_188, MarketInfo(g_symbol_188, MODE_BID));
      if (ld_32 == 0.0) g_symbol_188 = "";
   }
   Registered();
   Comments();
   return (0);
}

int deinit() {
   return (0);
}

void CheckVersion() {
   string ls_0;
   string ls_8;
   if (gi_212 != FALSE) {
      if (TimeCurrent() > gi_216) {
         ls_0 = "..........................................................";
         ls_0 = Version(gs_220);
         if (ls_0 != gs_228) {
            Print("Current System Version: " + gs_228);
            Print("Actual System Version: " + ls_0);
            ls_8 = "Please close this application and run EA_SIGMA1.0_ULTIMATE_UPDATE.exe for updating the build version";
            Print(ls_8);
            Alert(ls_8);
            gi_212 = FALSE;
         }
         gi_216 = TimeCurrent();
         gi_216 += 3600 * gi_208;
      }
   }
}

void Registered() {
   Print("registering with Advisor: " + gs_220 + " Account: " + AccountName() + "...");
   int l_bool_0 = IsTesting();
   int li_4 = IsOptimization();
   gi_200 = Init(gs_220, AccountName(), MoneyManagement, Lots, MaxTradeSize, MaximumRisk, MarketInfo(Symbol(), MODE_MINLOT), MarketInfo(Symbol(), MODE_MAXLOT), l_bool_0, li_4, AccountCurrency(), AccountLeverage(), MarketInfo(Symbol(), MODE_LOTSIZE), StopLoss, Symbol());
   if (gi_200 == 0) {
      Print("You have registered");
      return;
   }
   if (gi_200 == 1) {
      Print("No license for this account");
      Alert("EA SIGMA 1.0 ULTIMATE >> No license for this account");
      return;
   }
   if (gi_200 == -1) {
      Print("Error connect to DB for registration");
      Alert("EA SIGMA 1.0 ULTIMATE >> Error connect to DB for registration");
      gi_204 = TimeCurrent();
      gi_204 += 3600 * gi_196;
      return;
   }
   Print("Unknown error");
}

void Comments() {
   string ls_0 = "" 
      + "\n" 
      + "SIGMA 1.0 Ultimate © Build 02" 
      + "\n" 
      + "------------------------------------------------" 
      + "\n" 
      + "BROKER INFORMATIONS:" 
      + "\n" 
      + "Broker Company:        " + AccountCompany() 
      + "\n" 
      + "------------------------------------------------" 
      + "\n" 
      + "ACCOUNT INFORMATIONS:" 
      + "\n" 
      + "Account Name:          " + AccountName() 
      + "\n" 
      + "Account Number:        " + AccountNumber() 
      + "\n" 
      + "Account Leverage:      " + DoubleToStr(AccountLeverage(), 0) 
      + "\n" 
      + "Account Balance:       " + DoubleToStr(AccountBalance(), 2) 
      + "\n" 
      + "Account Currency:      " + AccountCurrency() 
      + "\n" 
      + "Account Equity:        " + DoubleToStr(AccountEquity(), 2) 
      + "\n" 
      + "------------------------------------------------" 
      + "\n" 
      + "MARGIN INFORMATIONS:" 
      + "\n" 
      + "Free Margin:           " + DoubleToStr(AccountFreeMargin(), 2) 
      + "\n" 
      + "Used Margin:           " + DoubleToStr(AccountMargin(), 2) 
      + "\n" 
      + "------------------------------------------------" 
      + "\n" 
   + "Actual Server Time     " + TimeToStr(TimeCurrent(), TIME_SECONDS);
   Comment(ls_0);
}

double Moneymanagement() {
   double ld_0 = MoneyManagement(AccountFreeMargin(), Bid, MarketInfo(g_symbol_188, MODE_BID));
   return (NormalizeDouble(ld_0, gi_176));
}

int start() {
   int l_hour_0;
   double l_iatr_4;
   double ld_12;
   if (gi_200 >= 1) return (0);
   if (gi_200 == -1) {
      if (TimeCurrent() > gi_204) {
         Registered();
         if (gi_200 != 0) gi_200 = 1;
      }
   }
   if (IsTesting() == FALSE && IsOptimization() == 0 && IsVisualMode() == FALSE) CheckVersion();
   if (StringSubstr(Symbol(), 0, 6) == "EURGBP") {
      Comments();
      l_hour_0 = TimeHour(TimeCurrent());
      if ((StartTime < EndTime && StartTime <= l_hour_0 && l_hour_0 <= EndTime) || (EndTime < StartTime && StartTime <= l_hour_0 || l_hour_0 <= EndTime)) {
         l_iatr_4 = iATR(NULL, PERIOD_M15, ATR_Period, 0);
         if (l_iatr_4 < UrATR) {
            if (ST_TP_Pending_Mode == TRUE) SL_TP_Corrected(StopLoss, TakeProfit, MagicNumber, gd_180);
            if (ExistPosition(MagicNumber) == 0) {
               ld_12 = Moneymanagement();
               if (AccountFreeMargin() < ld_12 * MarketInfo(Symbol(), MODE_LOTSTEP) / AccountLeverage()) {
                  Alert("EA SIGMA 1.0 ULTIMATE >> NOT ENOUGH MARGIN!", AccountFreeMargin());
                  return (0);
               }
               OpenSellOrder(ld_12, StopLoss, TakeProfit, MaximumSlippage, MagicNumber, gd_180, ST_TP_Pending_Mode, FridayMode, gi_164, gi_172);
               OpenBuyOrder(ld_12, StopLoss, TakeProfit, MaximumSlippage, MagicNumber, gd_180, ST_TP_Pending_Mode, FridayMode, gi_164, gi_172);
            }
            CloseOrders(MaximumSlippage, gi_164, gi_172, MagicNumber);
         }
      }
      TrailingPositions(gi_168, MagicNumber);
   }
   return (0);
}
