#property  copyright "Copyright © 2007, Bogie Enterprises"

extern   bool     UseEmail = true;
extern   bool     UseMM = true;
extern   bool     MicroAcct = false;
extern   double   Risk = 15;
double   var_96 = 0;
extern   double   Lots = 0;
double   var_112 = 0;
int      var_120 = 0;
extern   int      StartTime = 20;
extern   int      TakeProfit = 100;
extern   int      StopLoss = 100;
int      var_136 = 15;
extern   int      MaxOrders = 3;
extern   double   avg = 1.9;
extern   int      HiLi = 21;
extern   int      LoLi = 17;
bool     var_160 = false;
int      NeedModify = 1;
int      var_168 = 2;
int      var_172;
double   ModifySL;
double   var_184;
double   var_192;
string   var_200;
int      var_208;
int      Magic = 7447;
int      var_216 = 0;
int      var_220 = 0;
double   OpenSL = 0;
double   OpenTP = 0;
string   var_240 = "";
int      var_248 = 0;
double   var_252 = 1;
int      var_260 = 0;
int      OrdTicket = 0;
int      I = 0;
extern   int      x2 = 11;
extern   int      x3 = 28;
extern   double   MinMarginLevel = 100;
extern   int      Server_Local_Difference = 4;

//+------------------------------------------------------------------+

int init()
{
if (var_160 == true)
   {
   if ((Symbol() == "GBPJPY") || (Symbol() == "GBPJPYm"))
      {
      TakeProfit = 0;
      StopLoss = 175;
      var_136 = 0;
      var_112 = 0;
      var_120 = 20;
      StartTime = 23;
      NeedModify = 1;
      }
   if ((Symbol() == "USDJPY") || (Symbol() == "USDJPYm"))
      {
      TakeProfit = 120;
      StopLoss = 175;
      var_136 = 0;
      var_112 = 0;
      var_120 = 10;
      StartTime = 13;
      NeedModify = 1;
      }
   if ((Symbol() == "GBPUSD") || (Symbol() == "GBPUSDm"))
      {
      TakeProfit = 0;
      StopLoss = 75;
      var_136 = 15;
      avg = 3.0;
      HiLi = 19;
      LoLi = 17;
      x2 = 18;
      x3 = 26;
      StartTime = 23;
      NeedModify = 1;
      }
   if ((Symbol() == "EURUSD") || (Symbol() == "EURUSDm"))
      {
      TakeProfit = 100;
      StopLoss = 100;
      var_136 = 15;
      avg = 1.9;
      HiLi = 21;
      LoLi = 17;
      x2 = 11;
      x3 = 28;
      StartTime = 20;
      NeedModify = 1;
      }
   if ((Symbol() == "USDCHF") || (Symbol() == "USDCHFm"))
      {
      TakeProfit = 120;
      StopLoss = 175;
      var_136 = 0;
      var_112 = 0;
      var_120 = 16;
      StartTime = 4;
      NeedModify = 1;
      }
   if ((Symbol() == "EURJPY") || (Symbol() == "EURJPYm"))
      {
      TakeProfit = 0;
      StopLoss = 250;
      var_136 = 0;
      var_112 = 0;
      var_120 = 20;
      StartTime = 23;
      NeedModify = 1;
      }
   }
}

//+------------------------------------------------------------------+

int start()
{
string var_start_0;
int    var_start_8;
int    need.open.buy;
int    need.open.sell;
double var_start_20;
int    var_start_28;
int    var_start_32;
int    var_start_36;
int    var_start_40;
int    local.start.time;
int    need.close.buy;
int    need.close.sell;
int    ticket;
int    shift;
int    i;
int    count;
int    ordtotal;
int    j;
int SleepOk = 2000;
var_start_0 = "2035.02.07";
var_start_8 = StrToTime(var_start_0);


var_172 = StartTime - var_168;
StartTime = var_172 + var_168;

need.open.buy = 0;
need.open.sell = 0;
need.open.buy = 0;
need.open.sell = 0;

if (UseEmail == true) email();
if (var_248 != TimeDay(TimeCurrent())) var_248 = 0;

var_start_28 = Time[0] + Period() * 60 - TimeCurrent();
var_start_20 = var_start_28 / 60.0;
var_start_32 = var_start_28 % 60;
var_start_28 = (var_start_28 - var_start_28 % 60) / 60;
if (var_start_28 >= 60) var_start_40 = var_start_28 / 60; else var_start_40 = 0;
var_start_36 = var_start_28 - var_start_40 * 60;

var_112 = (iHigh(Symbol(),PERIOD_D1,0) - iLow(Symbol(),PERIOD_D1,0)) / avg;

if ((var_248 == 0) && (Hour() == StartTime) && (iClose(Symbol(),PERIOD_D1,0) > iHigh(Symbol(),PERIOD_D1,0) - var_112))
   {
   need.open.buy = 1;
   }
if ((var_248 == 0) && (Hour() == StartTime) && (iClose(Symbol(),PERIOD_D1,0) < iLow(Symbol(),PERIOD_D1,0) + var_112))
   {
   need.open.sell = 1;
   }

local.start.time = StartTime - Server_Local_Difference;
if (local.start.time < 0) local.start.time = local.start.time + 24;

if (MyOrdersTotal() == 0)
   {
   var_220 = 0;
   var_208 = 0;
   }

if (MyOrdersTotal() == 0)
   {
   var_260 = var_112 / Point;
   var_240 = "EntryGap";
   }

if (MyOrdersTotal() != 0)
   {
   var_260 = var_120 * var_252;
   var_240 = "ExitGap";
   }

Comment(var_240," = ",var_260,"  --  Profit Pips = ",BuyGap(),"\nVolume = ",Volume[0],"   ---   Server Time = ",TimeToStr(TimeCurrent(),TIME_MINUTES),"\nServer Trade Time = ",StartTime,":00","\nLocal Trade Time = ",local.start.time,":00","\nExpiration Date = ",var_start_0);

need.close.buy = 0;
need.close.sell = 0;

if (BuyGap() > var_208) var_208 = BuyGap();

var_120 = MathRound((iHigh(Symbol(),PERIOD_D1,0) - iLow(Symbol(),PERIOD_D1,0)) / avg / Point);
if (var_120 < LoLi) var_120 = x2;
if (var_120 > HiLi) var_120 = x3;

if ((MyOrdersTotal() == 0) && (AccountEquity() / (AccountMargin() + 0.0001) > MinMarginLevel / 100.0))
   {
   OpenSL = 0;
   OpenTP = 0;
   if (need.open.buy == 1)
      {
      var_216 = DayOfWeek();
      var_220 = 1;
      ModifySL = 0;
      var_252 = 1;
      var_248 = TimeDay(TimeCurrent());
      if (StopLoss > 0) OpenSL = iClose(Symbol(),PERIOD_D1,0) - StopLoss * Point;
      if (TakeProfit > 0) OpenTP = iClose(Symbol(),PERIOD_D1,0) + TakeProfit * Point;
      for (I = MaxOrders - 1; I >= 0; I--)
         {
         RefreshRates();
         ticket = OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,OpenSL,OpenTP,"BUYSignal",Magic,0,Blue);
         }
      if (UseEmail) SendMail("Signal Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(Ask,Digits) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS) + "\n\nOpen Buy  ===  sl = " + DoubleToStr(OpenSL,Digits) + "  tp = " + DoubleToStr(OpenTP,Digits) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2));
      if (ticket > 0)
         {
         if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
         }
            else
         {
         Print("Error opening BUY order : ",GetLastError());
         }
      return(0);
      }
   if (need.open.sell == 1)
      {
      var_216 = DayOfWeek();
      var_220 = 2;
      ModifySL = 0;
      var_252 = 1;
      var_248 = TimeDay(TimeCurrent());
      if (StopLoss > 0) OpenSL = iClose(Symbol(),PERIOD_D1,0) + StopLoss * Point;
      if (TakeProfit > 0) OpenTP = iClose(Symbol(),PERIOD_D1,0) - TakeProfit * Point;
      for (I = MaxOrders - 1; I >= 0; I--)
         {
         RefreshRates();
         ticket = OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,OpenSL,OpenTP,"SELLSignal",Magic,0,Red);
         }
      if (UseEmail) SendMail("Signal Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(Bid,Digits) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS) + "\n\nOpen Sell  === sl = " + DoubleToStr(OpenSL,Digits) + "  tp = " + DoubleToStr(OpenTP,Digits) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2));
      if (ticket > 0)
         {
         if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
         }
            else
         {
         Print("Error opening SELL order : ",GetLastError());
         }
      return(0);
      }
   }

shift = 0;
count = 0;

for (i = 0; i <= 6; i++)
   {
   if ((High[shift + 1] < High[shift + 2]) && (Low[shift + 1] > Low[shift + 2])) i--;
   shift++;
   count++;
   if (i == 3) break;
   }

var_184 = NormalizeDouble(High[iHighest(Symbol(),0,MODE_HIGH,count,1)],Digits);
var_192 = NormalizeDouble(Low[iLowest(Symbol(),0,MODE_LOW,count,1)],Digits);

ordtotal = OrdersTotal();
for (j = 0; j < ordtotal; j++)
   {
   OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
   if (OrderSymbol() == Symbol())
      {
      OrdTicket = OrderTicket();
      if (OrderType() == OP_BUY)
         {
         if (NeedModify == 1)
            {
            if (OrderClosePrice() - OrderOpenPrice() > var_120 * var_252 * Point)
               {
               ModifySL = NormalizeDouble(OrderOpenPrice() + var_120 * var_252 * Point - 4 * Point,Digits);
               var_252 = var_252 + 0.5;
               }
            if ((ModifySL > OrderStopLoss() + Point) && (ModifySL > 0) && (ModifySL != OrderStopLoss()))
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),ModifySL,OrderTakeProfit(),0,GreenYellow);
               }
            }
         if (need.close.buy == 1)
            {
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
            if (UseEmail) SendMail("Close Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(Bid,Digits) + "\n\nClose Buy  === Profit = " + DoubleToStr(OrderProfit(),2) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
            }
         }
      if (OrderType() == OP_SELL)
         {
         if (NeedModify == 1)
            {
            if (OrderOpenPrice() - OrderClosePrice() > var_120 * var_252 * Point)
               {
               ModifySL = NormalizeDouble(OrderOpenPrice() - var_120 * var_252 * Point + 4 * Point,Digits);
               var_252 = var_252 + 0.5;
               }
            if ((ModifySL < OrderStopLoss() - Point) && (ModifySL > 0) && (ModifySL != OrderStopLoss()))
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),ModifySL,OrderTakeProfit(),0,Red);
               }
            }
         if (need.close.sell == 1)
            {
            OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
            if (UseEmail) SendMail("Close Alert - Bogie-ATC2007-1-DEMO","[" + Symbol() + "]  " + DoubleToStr(Bid,Digits) + "\n\nClose Sell === Profit = " + DoubleToStr(OrderProfit(),2) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

int MyOrdersTotal()
{
int cnt = 0;

for (int i = 0; i < OrdersTotal(); i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (OrderSymbol() == Symbol()) cnt++;
      }
   }
return(cnt);
}

//+------------------------------------------------------------------+

int BuyGap()
{
double pips = 0;

for (int i = 0; i < OrdersTotal(); i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (OrderSymbol() == Symbol())
         {
         if (OrderType() == OP_BUY) pips = (OrderClosePrice() - OrderOpenPrice()) / Point;
         if (OrderType() == OP_SELL) pips = (OrderOpenPrice() - OrderClosePrice()) / Point;
         }
      }
   }
return(pips);
}

//+------------------------------------------------------------------+

void email()
{
if (OrdTicket > 0)
   {
   OrderSelect(OrdTicket,SELECT_BY_TICKET,MODE_HISTORY);
   if (OrderSymbol() == Symbol())
      {
      if ((OrderType() == OP_BUY) && (OrderClosePrice() == OrderStopLoss()))
         {
         SendMail("Close Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(OrderClosePrice(),Digits) + "\n\n  Close Buy @ sl  ===  Order Profit = " + DoubleToStr(OrderProfit(),2) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
         }
      if ((OrderType() == OP_SELL) && (OrderClosePrice() == OrderStopLoss()))
         {
         SendMail("Close Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(OrderClosePrice(),Digits) + "\n\n  Close Sell @ sl  ===  Order Profit = " + DoubleToStr(OrderProfit(),2) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
         }
      if ((OrderType() == OP_BUY) && (OrderClosePrice() == OrderTakeProfit()))
         {
         SendMail("Close Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(OrderClosePrice(),Digits) + "\n\n  Close Buy @ tp  ===  Order Profit = " + DoubleToStr(OrderProfit(),2) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
         }
      if ((OrderType() == OP_SELL) && (OrderClosePrice() == OrderTakeProfit()))
         {
         SendMail("Close Alert - Bogie-ATC2007-1","[" + Symbol() + "]  " + DoubleToStr(OrderClosePrice(),Digits) + "\n\n  Close Sell @ tp  ===  Order Profit = " + DoubleToStr(OrderProfit(),2) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(),2) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
         }
      OrdTicket = 0;
      }
   }
}

//+------------------------------------------------------------------+

double LotsOptimized()
{
if (UseMM == false) return(Lots);

double lots = Lots;
int    ordtotal = OrdersHistoryTotal();
int    losscnt = 0;
double var_LotsOptimized_16 = 0;
int    digits = 1;

if (MarketInfo(Symbol(),MODE_LOTSTEP) == 1.0) digits = 1;
if (MicroAcct == true) digits = 2;

lots = NormalizeDouble(AccountFreeMargin() * Risk / 100.0 / 1000.0,digits);

if (var_96 > 0.0)
   {
   for (int i = ordtotal - 1; i >= 0; i--)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) == 0)
         {
         Print("Error in history!");
         break;
         }
      if ((OrderSymbol() != Symbol()) || (OrderType() > OP_SELL)) continue;
      if (OrderProfit() > 0.0) break;
      if (OrderProfit() < 0.0) losscnt++;
      }
   if (losscnt > 1) lots = NormalizeDouble(lots - lots * losscnt / var_96,1);
   }

if ((lots < 0.1) && (MicroAcct == false)) lots = 0.1;
if ((lots < 0.01) && (MicroAcct == true)) lots = 0.01;
if (lots > 50.0) lots = 50;
return(lots);
}