/*
   Generated by EX4-TO-MQ4 decompiler V4.0.220.2c []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2008, Halcyon Forex, LTD. All rights reserved."
#property link      "http://www.halcyonfx.com"

extern double TakeProfit = 40.0;
extern double StopLoss = 200.0;
extern double CSL = 250.0;
extern double Lots = 0.1;
extern double step = 0.4;
extern double top = 4.0;
extern double bottom = 0.1;
extern double Trailingstop = 145.0;
extern double ttstep = 25.0;
extern double breakout1 = 10.0;
extern double breakout2 = 20.0;
extern int Magic = 44444444;
int g_ticket_168;
int g_ticket_172;
int g_pos_176;
double g_ihigh_180;
double g_ilow_188;
bool gi_196;
bool gi_204;
int gi_208;
int g_datetime_212;
double g_ihigh_216;
double g_ilow_224;
double g_ihigh_232;
double g_ilow_240;
int g_count_252;
int g_count_256;
double gd_268;
int gi_284;
double gd_292;
double gd_300;
double gd_308;
double gd_316;

int init() {
   if (Digits < 4) gd_316 = 0.01;
   else gd_316 = 0.0001;
   datetime lt_unused_0 = Time[0];
   if (GlobalVariableCheck("Sidewinder4_Arrears")) gd_300 = GlobalVariableGet("Sidewinder4_Arrears");
   if (GlobalVariableCheck("Sidewinder4_Lots")) Lots = GlobalVariableGet("Sidewinder4_Lots");
   double ld_4 = AccountBalance();
   return (0);
}

int deinit() {
   if (IsOptimization()) {
      GlobalVariableDel("Sidewinder4_Arrears");
      GlobalVariableDel("Sidewinder4_Lots");
   }
   return (0);
}

int start() {
   bool li_0;
   double l_price_4;
   double l_price_12;
   /*
   if (TimeCurrent() > StrToTime("2009.06.15 23:59")) {
      Alert("Temporary license expired. Please email support@halcyonfx.com to request a permanent licence.");
      return;
   }
   if (!IsDemo()) {
      Alert("This account :" + AccountNumber() + " is not licensed to trade this EA. Contact sales@halcyonfx.com for a live trading license.");
      return (0);
   }
   if (IsTesting()) return (0);
   */
   if (OrdersTotal() > 0) {
      for (g_pos_176 = 0; g_pos_176 < OrdersTotal(); g_pos_176++) {
         OrderSelect(g_pos_176, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderCloseTime() == 0) li_0 = TRUE;
      }
   }
   if (OrdersHistoryTotal() > 0 && gi_284 != 4) {
      OrderSelect(g_ticket_168, SELECT_BY_TICKET, MODE_HISTORY);
      if (OrderMagicNumber() == Magic && li_0 != TRUE) {
         gi_284 = 4;
         Print("Last arrears = ", gd_300);
         Print("Last order profit = ", NormalizeDouble(OrderProfit(), 2));
         gd_308 = NormalizeDouble(OrderProfit(), 2);
         gd_292 = gd_300;
         gd_300 = gd_292 + gd_308;
         Print("New Arrears = ", gd_300);
         if (gd_300 > 0.0) {
            Print("Arrears greater than Zero, resetting");
            Lots = bottom;
            g_count_252 = 0;
            g_count_256++;
            gd_268 = AccountBalance();
            gd_300 = 0;
            GlobalVariableDel("Sidewinder4_Arrears");
            GlobalVariableDel("Sidewinder4_Lots");
         }
         if (gd_300 < 0.0) {
            Print("Arrears in NEGATIVE TERRITORY!");
            g_count_252++;
            g_count_256 = 0;
            Lots = OrderLots() + step;
            GlobalVariableSet("Sidewinder4_Arrears", gd_300);
         }
      }
   }
   if (Lots > top) Lots = bottom;
   if (g_count_252 > 3) {
      gi_204 = TRUE;
      Comment("LEVEL 2 ALERT ON!");
   }
   if (g_count_252 <= 3) {
      gi_204 = FALSE;
      Comment("Status = Normal");
   }
   if (gi_204 != TRUE) {
      if (gi_208 != Time[0]) {
         gi_208 = Time[0];
         gi_196 = TRUE;
         g_datetime_212 = TimeCurrent();
      }
      if (gi_196 == TRUE) {
         g_ihigh_216 = iHigh(NULL, PERIOD_H4, 2);
         g_ihigh_232 = iHigh(NULL, PERIOD_H4, 1);
         g_ilow_224 = iLow(NULL, PERIOD_H4, 2);
         g_ilow_240 = iLow(NULL, PERIOD_H4, 1);
         if (g_ihigh_216 > g_ihigh_232) g_ihigh_180 = g_ihigh_216;
         if (g_ihigh_216 < g_ihigh_232) g_ihigh_180 = g_ihigh_232;
         if (g_ilow_224 < g_ilow_240) g_ilow_188 = g_ilow_224;
         if (g_ilow_224 > g_ilow_240) g_ilow_188 = g_ilow_240;
      }
      if (gi_196 == TRUE && TimeCurrent() > g_datetime_212 + 480 && li_0 != TRUE && GetBid() > g_ihigh_180 + breakout1 * gd_316) {
         g_ticket_168 = OrderSend(Symbol(), OP_BUY, Lots, GetAsk(), 3, 0, 0, "Buy", Magic, 0, Blue);
         if (g_ticket_168 > 0) {
            if (OrderSelect(g_ticket_168, SELECT_BY_TICKET, MODE_TRADES)) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - StopLoss * gd_316, OrderOpenPrice() + TakeProfit * gd_316, 0, Blue);
            Print("LONG order opened : ", OrderOpenPrice());
            g_ticket_172 = OrderTicket();
            gi_284 = 9;
            gi_196 = FALSE;
            GlobalVariableSet("Sidewinder4_Lots", OrderLots());
            return (0);
         }
      }
      if (gi_196 == TRUE && TimeCurrent() > g_datetime_212 + 480 && li_0 != TRUE && GetAsk() < g_ilow_188 - breakout1 * gd_316) {
         g_ticket_168 = OrderSend(Symbol(), OP_SELL, Lots, GetBid(), 3, 0, 0, "Sell", Magic, 0, Red);
         if (g_ticket_168 > 0) {
            if (OrderSelect(g_ticket_168, SELECT_BY_TICKET, MODE_TRADES)) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + StopLoss * gd_316, OrderOpenPrice() - TakeProfit * gd_316, 0, Red);
            Print("SHORT order opened : ", OrderOpenPrice());
            g_ticket_172 = OrderTicket();
            gi_284 = 9;
            gi_196 = FALSE;
            GlobalVariableSet("Sidewinder4_Lots", OrderLots());
            return (0);
         }
      }
   }
   if (gi_204 == TRUE) {
      l_price_4 = GetAsk() + Trailingstop * gd_316;
      l_price_12 = GetBid() - Trailingstop * gd_316;
      if (OrdersTotal() > 0) {
         for (int l_pos_20 = 0; l_pos_20 < OrdersTotal(); l_pos_20++) {
            OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES);
            if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
               if (GetAsk() < OrderOpenPrice() - Trailingstop * gd_316 && l_price_4 < OrderStopLoss()) {
                  OrderModify(OrderTicket(), OrderOpenPrice(), l_price_4, OrderTakeProfit(), 0, Green);
                  return (0);
               }
            }
            if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
               if (GetBid() > OrderOpenPrice() + Trailingstop * gd_316 && l_price_12 > OrderStopLoss()) {
                  OrderModify(OrderTicket(), OrderOpenPrice(), l_price_12, OrderTakeProfit(), 0, Green);
                  return (0);
               }
            }
         }
      }
      if (gi_208 != Time[0]) {
         gi_208 = Time[0];
         gi_196 = TRUE;
         g_datetime_212 = TimeCurrent();
      }
      if (gi_196 == TRUE) {
         g_ihigh_216 = iHigh(NULL, PERIOD_H4, 2);
         g_ihigh_232 = iHigh(NULL, PERIOD_H4, 1);
         g_ilow_224 = iLow(NULL, PERIOD_H4, 2);
         g_ilow_240 = iLow(NULL, PERIOD_H4, 1);
         if (g_ihigh_216 > g_ihigh_232) g_ihigh_180 = g_ihigh_216;
         if (g_ihigh_216 < g_ihigh_232) g_ihigh_180 = g_ihigh_232;
         if (g_ilow_224 < g_ilow_240) g_ilow_188 = g_ilow_224;
         if (g_ilow_224 > g_ilow_240) g_ilow_188 = g_ilow_240;
      }
      if (gi_196 == TRUE && TimeCurrent() > g_datetime_212 + 480 && li_0 != TRUE && GetBid() > g_ihigh_180 + breakout2 * gd_316) {
         g_ticket_168 = OrderSend(Symbol(), OP_BUY, Lots, GetAsk(), 5, GetAsk() - CSL * gd_316, 0, "Buy", Magic, 0, Green);
         if (g_ticket_168 > 0) {
            if (OrderSelect(g_ticket_168, SELECT_BY_TICKET, MODE_TRADES)) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - CSL * gd_316, 0, 0, Blue);
            Print("LONG order opened : ", OrderOpenPrice());
            GlobalVariableSet("Sidewinder4_Lots", OrderLots());
            gi_196 = FALSE;
            gi_284 = 9;
            return (0);
         }
      } else {
         if (gi_196 == TRUE && TimeCurrent() > g_datetime_212 + 480 && li_0 != TRUE && GetAsk() < g_ilow_188 - breakout2 * gd_316) {
            g_ticket_168 = OrderSend(Symbol(), OP_SELL, Lots, GetBid(), 5, GetBid() + CSL * gd_316, 0, "Sell", Magic, 0, Red);
            if (g_ticket_168 > 0) {
               if (OrderSelect(g_ticket_168, SELECT_BY_TICKET, MODE_TRADES)) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + CSL * gd_316, 0, 0, Red);
               Print("SHORT order opened : ", OrderOpenPrice());
               GlobalVariableSet("Sidewinder4_Lots", OrderLots());
               gi_196 = FALSE;
               gi_284 = 9;
               return (0);
            }
         }
      }
   }
   return (0);
}

double GetBid() {
   return (NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits));
}

double GetAsk() {
   return (NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits));
}