
#property copyright "2008 - Bogie Enterprises"
#property link      "www.bogie-enterprises.com"


extern bool UseEmail = TRUE;
extern bool UseMM = TRUE;
extern bool MiniAcct = FALSE;
extern double Risk = 5.0;
extern double Lots = 0.0;
extern double TakeProfit = 0.0;
extern double StopLoss = 200.0;
extern double TrailingStop = 170.0;
extern double BuyTrigger = 54.0;
extern double SellTrigger = 74.0;
extern int MagicNumber = 12345;
extern int NoTradeDay_1 = 0;
extern int NoTradeDay_2 = 0;


double gd_unused_156;
double gd_unused_164 = 1.0;
double g_price_172;
double g_price_180;
int gi_unused_188 = 0;
int g_bars_192;
int g_slippage_196 = 5;
int gi_unused_200 = 0;
int g_ticket_204;
int g_ticket_208 = 0;
string g_comment_212 = "Bogie-NN-v8";



int gi_224 = 888;
string g_time2str_232;

bool gi_244 = FALSE;
bool gi_unused_248 = FALSE;
int g_datetime_252 = 0;
bool gi_256 = FALSE;
bool gi_260 = FALSE;

int init() {
   int li_8;
   
   Comment("Bogie-NN-v8 INITIALIZING");
   
   g_bars_192 = Bars;
   
   int l_digits_4 = MarketInfo(Symbol(), MODE_DIGITS);
   if (l_digits_4 == 2) li_8 = 1;
   if (l_digits_4 == 3) li_8 = 10;
   if (l_digits_4 == 2) li_8 = 1;
   if (l_digits_4 == 3) li_8 = 10;
   if (l_digits_4 == 4) li_8 = 1;
   if (l_digits_4 == 5) li_8 = 10;
   
   if (StopLoss > 0.0) StopLoss = NormalizeDouble(StopLoss * li_8 * Point, Digits);
   else StopLoss = 0;
   if (TakeProfit > 0.0) TakeProfit = NormalizeDouble(TakeProfit * li_8 * Point, Digits);
   else TakeProfit = 0;
   if (TrailingStop > 0.0) TrailingStop = NormalizeDouble(TrailingStop * li_8 * Point, Digits);
   else TrailingStop = 0;
   return (0);
}

int deinit() {
   return (0);
}

int start() {

   string ls_16;
   int l_ord_total_40;

   if (Bars < 200) return (0);
   
   email();
   
   ModifyOrders();
   
   double l_icustom_0 = iCustom(NULL, PERIOD_H1, "Bogie-NN-IND-v8", 1, 1);
   double l_icustom_8 = iCustom(NULL, PERIOD_H1, "Bogie-NN-IND-v8", 1, 2);
   
   if (l_icustom_8 <= BuyTrigger && l_icustom_0 >= BuyTrigger) ls_16 = "BUY";
   else ls_16 = "NONE";
   
   if (l_icustom_8 >= SellTrigger && l_icustom_0 <= SellTrigger) ls_16 = "SELL";
   else ls_16 = "NONE";
   
   string ls_24 = AccountCompany();
   Comment(ls_24, " - Server Time = ", TimeToStr(TimeCurrent(), TIME_MINUTES), 
      "\nTrade Signal = ", ls_16);
   
   if (!IsBarEnd()) return (0);
      
   for (int l_pos_32 = OrdersTotal() - 1; l_pos_32 >= 0; l_pos_32--) {
      OrderSelect(l_pos_32, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY) {
            
            if (!(l_icustom_8 >= SellTrigger && l_icustom_0 <= SellTrigger || !Trade())) continue;
            
            if (IsTradeContextBusy()) {
               Print("Trade context is busy! The expert cannot close position!");
               return (-1);
            }
            
            RefreshRates();
            OrderClose(OrderTicket(), OrderLots(), Bid, g_slippage_196, Aqua);
            if (!(UseEmail)) break;
            SendMail("Close Alert - Bogie-NN-v8", "[" + Symbol() + "]  " + DoubleToStr(OrderClosePrice(), Digits) + "\n\n  Close Buy   ===  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));
            break;
         }
         if (OrderType() == OP_SELL) {
            if (l_icustom_8 <= BuyTrigger && l_icustom_0 >= BuyTrigger) {
               if (IsTradeContextBusy()) {
                  Print("Trade context is busy! The expert cannot close position!");
                  return (-1);
               }
               RefreshRates();
               OrderClose(OrderTicket(), OrderLots(), Ask, g_slippage_196, OrangeRed);
               if (!(UseEmail)) break;
               SendMail("Close Alert - Bogie-NN-v8", "[" + Symbol() + "]  " + DoubleToStr(OrderClosePrice(), Digits) + "\n\n  Close Sell   ===  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));
               break;
            }
         }
      }
   }
   int l_count_36 = 0;
   for (l_pos_32 = OrdersTotal() - 1; l_pos_32 >= 0; l_pos_32--) {
      OrderSelect(l_pos_32, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) l_count_36++;
   }
   if (l_count_36 == 0 && Trade()) {
      l_ord_total_40 = OrdersTotal();
      gd_unused_156 = 0;
      gd_unused_164 = 1;
      if (l_icustom_8 <= BuyTrigger && l_icustom_0 >= BuyTrigger) {
         if (TakeProfit > 0.0) g_price_172 = Ask + TakeProfit;
         else g_price_172 = 0;
         if (StopLoss > 0.0) g_price_180 = Ask - StopLoss;
         else g_price_180 = 0;
         g_ticket_204 = 0;
         if (IsTradeContextBusy()) {
            Print("Trade context is busy! The expert cannot open position!");
            return (-1);
         }
         RefreshRates();
         g_ticket_204 = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, g_slippage_196, 0, 0, g_comment_212, MagicNumber, 0, Aqua);
         if (g_ticket_204 > 0) {
            if (OrderSelect(g_ticket_204, SELECT_BY_TICKET, MODE_TRADES)) Print("BUY order opened : ", OrderOpenPrice());
         } else Print("Error opening BUY order : ", GetLastError());
         l_ord_total_40 = OrdersTotal();
         for (int l_pos_44 = 0; l_pos_44 < l_ord_total_40; l_pos_44++) {
            OrderSelect(l_pos_44, SELECT_BY_POS, MODE_TRADES);
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
               if (IsTradeContextBusy()) {
                  Print("Trade context is busy! The expert cannot open position!");
                  return (-1);
               }
               OrderModify(OrderTicket(), OrderOpenPrice(), g_price_180, g_price_172, 0, GreenYellow);
            }
         }
         if (UseEmail) {
            SendMail("Signal Alert - Bogie-NN-v8", "[" + Symbol() + "]  " + DoubleToStr(Ask, Digits) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),
               TIME_DATE|TIME_SECONDS) + "\n\nOpen Buy  ===  sl = " + DoubleToStr(StopLoss, Digits) + "  tp = " + DoubleToStr(TakeProfit, Digits) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),
               2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(), 2));
         }
      } else {
         if (l_icustom_8 >= SellTrigger && l_icustom_0 <= SellTrigger) {
            if (TakeProfit > 0.0) g_price_172 = Bid - TakeProfit;
            else g_price_172 = 0;
            if (StopLoss > 0.0) g_price_180 = Bid + StopLoss;
            else g_price_180 = 0;
            g_ticket_204 = 0;
            if (IsTradeContextBusy()) {
               Print("Trade context is busy! The expert cannot open position!");
               return (-1);
            }
            RefreshRates();
            g_ticket_204 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, g_slippage_196, 0, 0, g_comment_212, MagicNumber, 0, OrangeRed);
            if (g_ticket_204 > 0) {
               if (OrderSelect(g_ticket_204, SELECT_BY_TICKET, MODE_TRADES)) Print("BUY order opened : ", OrderOpenPrice());
            } else Print("Error opening BUY order : ", GetLastError());
            l_ord_total_40 = OrdersTotal();
            for (l_pos_44 = 0; l_pos_44 < l_ord_total_40; l_pos_44++) {
               OrderSelect(l_pos_44, SELECT_BY_POS, MODE_TRADES);
               if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
                  if (IsTradeContextBusy()) {
                     Print("Trade context is busy! The expert cannot open position!");
                     return (-1);
                  }
                  OrderModify(OrderTicket(), OrderOpenPrice(), g_price_180, g_price_172, 0, GreenYellow);
               }
            }
            if (UseEmail) {
               SendMail("Signal Alert - Bogie-NN-v8", "[" + Symbol() + "]  " + DoubleToStr(Ask, Digits) + "\n\nServer Trade Time = " + TimeToStr(TimeCurrent(),
                  TIME_DATE|TIME_SECONDS) + "\n\nOpen Sell  ===  sl = " + DoubleToStr(StopLoss, Digits) + "  tp = " + DoubleToStr(TakeProfit, Digits) + "\nAccount Balance = " + DoubleToStr(AccountBalance(),
                  2) + "   ===  Account Equity = " + DoubleToStr(AccountEquity(), 2));
            }
         }
      }
   }
   return (0);
}

void ModifyOrders() {
   for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         g_ticket_208 = OrderTicket();
         if (OrderType() == OP_BUY) {
            if (OrderStopLoss() < Bid - TrailingStop - 5.0 * Point) {
               if (IsTradeContextBusy()) {
                  Print("Trade context is busy! The expert cannot modify position!");
                  return;
               }
               RefreshRates();
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop, OrderTakeProfit(), 0, Aqua);
               return;
            }
         }
         if (OrderType() == OP_SELL) {
            if (OrderStopLoss() > Ask + TrailingStop + 5.0 * Point) {
               if (IsTradeContextBusy()) {
                  Print("Trade context is busy! The expert cannot modify position!");
                  return;
               }
               RefreshRates();
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop, OrderTakeProfit(), 0, OrangeRed);
               return;
            }
         }
      }
   }
}



bool Trade() {
   if (DayOfWeek() == NoTradeDay_1) {
      Alert("No trading today");
      return (FALSE);
   }
   if (DayOfWeek() == NoTradeDay_2) {
      Alert("No trading today");
      return (FALSE);
   }
   return (TRUE);
}



bool IsBarEnd() {
   bool li_ret_0 = FALSE;
   if (g_bars_192 != Bars) {
      li_ret_0 = TRUE;
      g_bars_192 = Bars;
   }
   return (li_ret_0);
}


double LotsOptimized() {
   if (UseMM == FALSE) return (Lots);
   double l_lots_0 = Lots;
   int li_20 = 1;
   if (MarketInfo(Symbol(), MODE_LOTSTEP) == 1.0) li_20 = 1;
   if (MiniAcct == TRUE) li_20 = 2;
   l_lots_0 = NormalizeDouble(AccountFreeMargin() * Risk / 100.0 / 1000.0, li_20);
   if (l_lots_0 < 0.1 && MiniAcct == FALSE) l_lots_0 = 0.1;
   if (l_lots_0 < 0.01 && MiniAcct == TRUE) l_lots_0 = 0.01;
   if (l_lots_0 > 50.0) l_lots_0 = 50;
   return (l_lots_0);
}



void email() {
   if (g_ticket_208 > 0) {
      OrderSelect(g_ticket_208, SELECT_BY_TICKET, MODE_HISTORY);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY && OrderClosePrice() == OrderStopLoss()) {
            SendMail("Close Alert - Bogie-NN-v8", "[" + 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-NN-v8", "[" + 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-NN-v8", "[" + 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-NN-v8", "[" + 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));
         }
         g_ticket_208 = 0;
      }
   }
}


