Navigation:Home > Content >

HACCA-PRO.mq4

Time: 2016-12-25 | Download file:HACCA-PRO.mq4

//+------------------------------------------------------------------+
//|                                                    HACCA-PRO.mq4 |
//|                                       Copyright 2012, AlFa Corp. |
//|                                      alessio.fabiani @ gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, AlFa"
#property link      "alessio.fabiani @ gmail.com"
//============================================================================================================
#define VER "1.0"
#define DECIMAL_CONVERSION 10
#define WEAK_BUY           0
#define WEAK_SELL          1
#define STRONG_BUY         2
#define MEDIUM_BUY         3
#define STRONG_SELL        4
#define MEDIUM_SELL        5
#define CAUTION            6
#define WEAK_UP            7
#define STRONG_UP          8
#define WEAK_DOWN          9
#define STRONG_DOWN        10
#define CONSOLIDATION      11

//+------------------------------------------------------------------+
//| expert external variables                                        |
//+------------------------------------------------------------------+
extern int Magic                 = 7691;
string IS                        = "-------------- Indicator Settings";
int MaMetod                      = 2;
int MaPeriod                     = 6;
int MaMetod2                     = 3;
int MaPeriod2                    = 2;
extern bool EnableCCIDivCheck    = false;
extern string PP                 = "-------------- Target global pips";
extern double EquityGreaterThan  = 10;
extern int Delta                 = 15;
extern string LM                 =  "----------------- Lot Management";
extern double Lots               = 0.01;
extern string LMm                = "------------- Money Management(%)";
extern bool MM                   = false; //money management
extern double Risk               = 1; //risk in percentage
int StopLoss                     = 8000;
extern int Slippage              = 2;
double xFactor                   = 1.0;
extern int MaxOrders             = 6;
int MaxOpenHours                 = 7;
bool Bounce                      = true;
extern string TMm                = "---------------- Time Management";
extern bool TradeOnFriday        = true;

//+------------------------------------------------------------------+
//| expert internal variables                                        |
//+------------------------------------------------------------------+
static int prevtime = 0;//Do not use when 0-bar is used
double lastBarTime;
bool tradingAllowed = false;
double PointDigits;
double delta_pips;
double MML = 0;
double LA1 = 0, LA2 = 0;
double HA1 = 0, HA2 = 0;
int maxNumOrders = 10, ordersTotal = 0;
bool closeAllNow = false, priceBreakEavenHit = false;
bool BlockCondition = false;

double stopLoss;
double takeProfit;

double latestSignal = 0, lastProfit = 0;
int enteringSignal = 0, openSignal = 0;

string char[256];
int ls_arr00[] = {72, 65, 67, 67, 65, 32, 69, 65, 32, 45, 32, 67, 111, 112, 121, 114, 105, 103, 104, 116, 32, 169, 32, 50, 48, 49, 50, 44, 32, 65, 108, 70, 97, 55, 57, 54, 49};

int marketCondition = -1, latestBalancingOrder = -1;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
//============================================================================================================
int init()
  {
   lastBarTime = TimeCurrent();
//----
   for (int i = 0; i < 256; i++) char[i] = CharToStr(i);
   
   //+------------------------------------------------------------------+
   //| init digits                                                      |
   //+------------------------------------------------------------------+
   if (MarketInfo(Symbol(), MODE_DIGITS) == 3 || MarketInfo(Symbol(), MODE_DIGITS) == 5)
   {
      PointDigits = MarketInfo(Symbol(), MODE_POINT) * DECIMAL_CONVERSION;
      delta_pips = NormalizeDouble(Delta * DECIMAL_CONVERSION * MarketInfo(Symbol(), MODE_POINT), MarketInfo(Symbol(), MODE_DIGITS));
   }
   else
   {
      PointDigits = MarketInfo(Symbol(), MODE_POINT);
      delta_pips = NormalizeDouble(Delta * MarketInfo(Symbol(), MODE_POINT), MarketInfo(Symbol(), MODE_DIGITS));
   }
   
   //+------------------------------------------------------------------+
   //| checking timeframe                                               |
   //+------------------------------------------------------------------+
   if (Period() != PERIOD_H1) 
   {
      Comment("ERROR -- HACCA should be attached to " + Symbol() + " 1 hour chart window");
      Alert("ERROR -- HACCA should be attached to " + Symbol() + " 1 hour chart window");
      
      return(-1);
   }
   Comment(
      /* HACCA EA - Copyright © 2012, AlFa7961 */
      chrToStr(ls_arr00) + 
      "\nExpiration Date: " + 
      char[50] + char[48] + char[49] + char[50] + char[46] + char[49] + char[50] + 
      char[46] + char[51] + char[49] + char[32] + char[50] + char[51] + char[58] + 
      char[53] + char[57] + char[58] + char[48] + char[48]
   );
   
   // Date, until which the expert is allowed to work
   datetime LastAllowedDate = StrToTime(
      /* 2012.12.31 23:59:00 */
      char[50] + char[48] + char[49] + char[50] + char[46] + char[49] + char[50] + 
      char[46] + char[51] + char[49] + char[32] + char[50] + char[51] + char[58] + 
      char[53] + char[57] + char[58] + char[48] + char[48]
   );   

   //+------------------------------------------------------------------+
   //| checking validity                                                |
   //+------------------------------------------------------------------+
   if (!IsDemo())
   {
      Comment("ERROR -- HACCA is not allowed to trade on live account");
      Alert("ERROR -- HACCA is not allowed to trade on live account");

      return(-1);
   }

   if (TimeCurrent() >= LastAllowedDate) 
   {
      Comment("ERROR -- HACCA has expired.");
      Alert("ERROR -- HACCA has expired.");
      
      return(-1);
   }
      
   //+------------------------------------------------------------------+
   //| checking trade contexts                                          |
   //+------------------------------------------------------------------+
   if(IsTradeAllowed()) 
   {
      RefreshRates();
   } 
   else 
   {
      prevtime = Time[1];//Do not use when 0-bar is used
      return(0);
   }


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
//============================================================================================================
int start()
  {

   double timeUnit = ((TimeCurrent() - lastBarTime) / 60);
   /*if (timeUnit < Period()) {
     if(MathAbs(latestSignal) == 100) {}
     else {return(0);}
   }*/
 
   if (timeUnit >= Period()) {
      lastBarTime = TimeCurrent();
      marketCondition = GetMarketCondition(timeUnit);
   }

//----
   tradingAllowed = false;
   
   BlockCondition = CheckBlockCondition();

   if (BlockCondition == false)
   {
      tradingAllowed = true;
   }
   else if (BlockCondition == true)
   {
      tradingAllowed = false;
      DeleteAllPendingOrders();
   }
   
   if (TradeOnFriday == FALSE && DayOfWeek() == 5)
   {
      tradingAllowed = false;
      DeleteAllPendingOrders();
   }

   //+------------------------------------------------------------------+
   //| checking orders                                                  |
   //+------------------------------------------------------------------+
   int cnt = 0;
   for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) {
      OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         if (OrderType() == OP_BUY || OrderType() == OP_SELL)
         {
            cnt++;
         }
      }
   }
   
   if (cnt == 0)
   {
      for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) {
         OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
            if (OrderType() != OP_BUY && OrderType() != OP_SELL) OrderDelete(OrderTicket());
         }
      }
   }

if (tradingAllowed)
{
      if (EnableCCIDivCheck)
      {
         latestSignal = iCustom(Symbol(), 0, "!CCI-Divergence-Master-v13c", 0, 0);
      }

      if (timeUnit >= Period()) {
         /*
          * CrossUp > CrossDown == Bearish
          * CrossUp < CrossDown == Bullish
          */
         double EMACrossUp    = iCustom(Symbol(), 0, "EMA Crossover Signal", 0, 1);
         double EMACrossDown  = iCustom(Symbol(), 0, "EMA Crossover Signal", 1, 1);

         double LWMACrossUp   = iCustom(Symbol(), 0, "LWMA-Crossover_Signal", 0, 1);
         double LWMACrossDown = iCustom(Symbol(), 0, "LWMA-Crossover_Signal", 1, 1);

         if (EMACrossUp == EMPTY_VALUE) EMACrossUp = 0;
         if (EMACrossDown == EMPTY_VALUE) EMACrossDown = 0;
         if (LWMACrossUp == EMPTY_VALUE) LWMACrossUp = 0;
         if (LWMACrossDown == EMPTY_VALUE) LWMACrossDown = 0;
         
         if (EMACrossUp != 0 /*&& LWMACrossUp != 0*/) { latestSignal =  100; }
         if (EMACrossDown != 0 /*&& LWMACrossDown != 0*/) { latestSignal = -100; }
                  
         // Laguerre-ACS1 indicator
         LA1 = iCustom(Symbol(),0,"Laguerre-ACS1",0.6,100,2,MODE_MAIN,1);
         LA2 = iCustom(Symbol(),0,"Laguerre-ACS1",0.6,100,2,MODE_MAIN,2);

         // Heiken Ashi indicator
         HA1 = iCustom(Symbol(), 0, "Heiken Ashi Smoothed", MaMetod, MaPeriod, MaMetod2, MaPeriod2, Symbol(), 2, 1);
         HA2 = iCustom(Symbol(), 0, "Heiken Ashi Smoothed", MaMetod, MaPeriod, MaMetod2, MaPeriod2, Symbol(), 3, 1);

         // Stochastics indicators
         double MACD1 = iMACD(Symbol(),0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
         double MACD2 = iMACD(Symbol(),0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
         bool MACDheadingUp = (MACD1>MACD2);
         bool MACDheadingDw = (MACD1 ST1);

         double RSI1 = iRSI(Symbol(),0,8,PRICE_CLOSE,1);
         double RSI2 = iRSI(Symbol(),0,8,PRICE_CLOSE,2);
         bool RSIheadingUp = (RSI2 < RSI1);
         bool RSIheadingDw = (RSI2 > RSI1);

         // BUY Signal
         /*if( 
             //MACDheadingUp && 
             LA1<=0.15 && 
             ST1<=20 && 
             RSI1<=20 && 
             STheadingUp && 
             RSIheadingUp
            ) openSignal = 2; */
         if (LA1 >= LA2 && HA2 > HA1 && latestSignal == 100 && (marketCondition == STRONG_BUY||marketCondition == STRONG_UP)) { 
            openSignal = 2;
            if (cnt == 0) {enteringSignal = 2;}
         }
         // SELL Signal
         /*if( 
             //MACDheadingDw && 
             LA1>=0.85 && 
             ST1>=80 && 
             RSI1>=80 && 
             STheadingDw && 
             RSIheadingDw
            ) openSignal = 1;*/
         if (LA1 <= LA2 && HA1 > HA2 && latestSignal == -100 && (marketCondition == STRONG_SELL||marketCondition == STRONG_DOWN)) { 
            openSignal = 1;
            if (cnt == 0) {enteringSignal = 1;}
         }
      }

    // expert money management
    if (MM) {
        if (Risk < 0.1 || Risk > 100) {
            Comment("Invalid Risk Value.");
            return (0);
        } else {
            MML = MathFloor( (AccountFreeMargin() * AccountLeverage() * Risk * PointDigits * 100) / (Ask * MarketInfo(Symbol(), MODE_LOTSIZE) * MarketInfo(Symbol(), MODE_MINLOT)) ) * (MarketInfo(Symbol(), MODE_MINLOT));

            if (cnt == 0) MML = MML;
            else MML = normPrice(MML * xFactor);

            double MINLOT = MarketInfo(Symbol(),MODE_MINLOT);

            if (MML>MarketInfo(Symbol(),MODE_MAXLOT)) {MML = MarketInfo(Symbol(),MODE_MAXLOT); }
            if (MML 0)
   {
      int selectedOrderTicket = -1;
      double globalPipDistance = 0;
      double actualPipDistance = 0;
      int pos;
      if (cnt == 1)
      {
         for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) 
         {
            if (OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES))
            {
               if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
               {
                  selectedOrderTicket = OrderTicket();
                  pos = OrderType();
                  if (pos == OP_BUY)
                  {
                     actualPipDistance = (Ask - OrderOpenPrice()) / PointDigits;
                     break;
                  }
                  else if (pos == OP_SELL)
                  {
                     actualPipDistance = (OrderOpenPrice() - Bid) / PointDigits;
                     break;
                  }
               }
            }
         }

         if (Delta > 0 && selectedOrderTicket >= 0 && actualPipDistance <= -Delta)
         {
            if (pos == OP_BUY)
            {
               if (Bounce) OpenBuyOrder();
               else OpenSellOrder();
            }
            else if (pos == OP_SELL)
            {
               if (Bounce) OpenSellOrder();
               else OpenBuyOrder();
            }         
         }
         else if (Delta > 0 && selectedOrderTicket >= 0 && actualPipDistance >= Delta)
         {
            CloseAllOrders();
         }
      }
      else
      {
         double globalProfit = 0;
         for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) 
         {
            if (OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES))
            {
               if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
               {
                  pos = OrderType();
                  if (pos == OP_BUY)
                  {
                     actualPipDistance = (Ask - OrderOpenPrice()) / PointDigits;
                  }
                  else if (pos == OP_SELL)
                  {
                     actualPipDistance = (OrderOpenPrice() - Bid) / PointDigits;
                  }
                  globalProfit += OrderProfit() + OrderSwap() + OrderCommission();
                  globalPipDistance += actualPipDistance;
               }
            }
         }
         
         if ( (Delta > 0 && globalPipDistance >= Delta) || (cnt >= MaxOrders && globalProfit >= 0) )
         {
            CloseAllOrders();
         }
         else if (Delta > 0 && globalPipDistance <= -Delta)
         {
            if (cnt <= MaxOrders)
            {
               selectedOrderTicket = GetLatestPlacedOrder();
               if (selectedOrderTicket >= 0 && OrderSelect(selectedOrderTicket, SELECT_BY_TICKET, MODE_TRADES))
               {
                  pos = OrderType();
                  if (pos == OP_BUY)
                  {
                     actualPipDistance = (Ask - OrderOpenPrice()) / PointDigits;
                  }
                  else if (pos == OP_SELL)
                  {
                     actualPipDistance = (OrderOpenPrice() - Bid) / PointDigits;
                  }
               
                  if (actualPipDistance >= Delta)
                  {
                     if (pos == OP_BUY)
                     {
                        if (Bounce) latestBalancingOrder = OpenSellOrder();
                        else latestBalancingOrder = OpenBuyOrder();
                     }
                     else if (pos == OP_SELL)
                     {
                        if (Bounce) latestBalancingOrder = OpenBuyOrder();
                        else latestBalancingOrder = OpenSellOrder();
                     }
                  }
               
                  if (actualPipDistance <= -Delta)
                  {
                     if (pos == OP_BUY)
                     {
                        if (Bounce) latestBalancingOrder = OpenBuyOrder();
                        else latestBalancingOrder = OpenSellOrder(); 
                     }
                     else if (pos == OP_SELL)
                     {
                        if (Bounce) latestBalancingOrder = OpenSellOrder();
                        else latestBalancingOrder = OpenBuyOrder(); 
                     }
                  }
               }
            }
         }
         
         if (cnt >= MaxOrders)
         {
            for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) 
            {
               if (OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES))
               {
                  if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
                  {
                     int barsLeftFromOpenTime = iBarShift(OrderSymbol(), PERIOD_H1, OrderOpenTime(), TRUE);
               
                     if (barsLeftFromOpenTime >= MaxOpenHours)
                     {
                        pos = OrderType();
                        if (pos == OP_BUY)
                        {
                           actualPipDistance = (Ask - OrderOpenPrice()) / PointDigits;
                        }
                        else if (pos == OP_SELL)
                        {
                           actualPipDistance = (OrderOpenPrice() - Bid) / PointDigits;
                        }
               
                        if (actualPipDistance >= Delta)
                        {
                           if (pos == OP_BUY)
                           {
                              OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);
                              if (Bounce) latestBalancingOrder = OpenSellOrder();
                              else latestBalancingOrder = OpenBuyOrder();
                           }
                           else if (pos == OP_SELL)
                           {
                              OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
                              if (Bounce) latestBalancingOrder = OpenBuyOrder();
                              else latestBalancingOrder = OpenSellOrder();
                           }
                        }

                        /*if (OrderProfit() + OrderSwap() + OrderCommission() >= 0)
                        {
                           if (OrderType() == OP_BUY)
                           {
                              OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);
                           }
                           else if (OrderType() == OP_SELL)
                           {
                              OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
                           }                           
                        }*/
                        
                     }
                  }
               }  
            }
         }         
      }
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
//============================================================================================================
int deinit()
  {
//----
   int Total = OrdersTotal();
   for (int i = Total-1; i >= 0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE) 
      {
         if (OrderMagicNumber() == Magic)
         {
            int Pos = OrderType();
            if ((Pos == OP_BUYSTOP) || (Pos == OP_SELLSTOP) || (Pos == OP_BUYLIMIT) || (Pos == OP_SELLLIMIT)) {bool Result = OrderDelete(OrderTicket(), CLR_NONE);}
            if (Result <= 0) {int Error = GetLastError();}
            else Error = 0;
         }
      }
   }
//----
   return(0);
  }
//============================================================================================================
double normPrice (double price)
{
   if(MarketInfo(Symbol(), MODE_LOTSTEP) == 0.01){return(NormalizeDouble(price,2));}
   else if(MarketInfo(Symbol(), MODE_LOTSTEP) == 0.1){return(NormalizeDouble(price,1));}
   else {
         double otherStep = MathMod(price,MarketInfo(Symbol(), MODE_LOTSTEP));
         price -= otherStep;
   }
}
//============================================================================================================
string chrToStr(int arr[])
{
   string strOut = "";
   for (int cc = 0; cc < ArraySize(arr); cc++)
   {
      strOut = strOut + char[arr[cc]];
   }
   return(strOut);
}
//============================================================================================================
void DeleteAllPendingOrders()
{
      for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) {
         OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
            if (OrderType() != OP_BUY && OrderType() != OP_SELL) OrderDelete(OrderTicket());
         }
      }
}

//============================================================================================================
void CloseAllOrders()
{
   for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) {
      OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         if (OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES))
         {
            if (OrderType() == OP_BUY)
            {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);
            }
            else if (OrderType() == OP_SELL)
            {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
            }
         }
      }
   }
   latestBalancingOrder = -1;
}
//============================================================================================================
int OpenBuyOrder()
{
   int selectedOrderTicket = -1;
   bool selectedOrderModified = FALSE;
   
   stopLoss = Ask - StopLoss * PointDigits;
   if (StopLoss <= 0) stopLoss = 0;
   /*takeProfit = Ask + delta_pips;
   if (Delta == 0)*/ takeProfit = 0;
   if (AccountFreeMarginCheck(Symbol(), OP_BUY, normPrice(MML)) >= 0.0 && CheckBlockCondition() == FALSE) {
         selectedOrderTicket = OrderSend(Symbol(), OP_BUY, normPrice(MML), Ask, Slippage, 0, 0, "HACCA# "+VER+"; Magic# " + Magic, Magic, 0, CLR_NONE);
         if (OrderSelect(selectedOrderTicket, SELECT_BY_TICKET, MODE_TRADES))
         {
            while (!IsTradeAllowed()) Sleep(100);
            RefreshRates();
            selectedOrderModified = OrderModify(selectedOrderTicket, OrderOpenPrice(), stopLoss, takeProfit, 0, CLR_NONE);
            if (selectedOrderModified == FALSE) Print("Error modifying BUY order : ", GetLastError());
         }
   } else {
      if (AccountFreeMarginCheck(Symbol(), OP_BUY, normPrice(MML)) < 0.0) Comment("we recommend you to decrease your start lot or increase deposit");
      else Print("BUY, " + normPrice(MML) + " lots. ");
   }
   
   return(selectedOrderTicket);
}
//============================================================================================================
int OpenSellOrder()
{
   int selectedOrderTicket = -1;
   bool selectedOrderModified = FALSE;
   
   stopLoss = Bid + StopLoss * PointDigits;
   if (StopLoss <= 0) stopLoss = 0;
   /*takeProfit = Bid - delta_pips;
   if (Delta == 0)*/ takeProfit = 0;
   if (AccountFreeMarginCheck(Symbol(), OP_SELL, normPrice(MML)) >= 0.0 && CheckBlockCondition() == FALSE) {
         selectedOrderTicket = OrderSend(Symbol(), OP_SELL, normPrice(MML), Bid, Slippage, 0, 0, "HACCA# "+VER+"; Magic# " + Magic, Magic, 0, CLR_NONE);
         if (OrderSelect(selectedOrderTicket, SELECT_BY_TICKET, MODE_TRADES))
         {
            while (!IsTradeAllowed()) Sleep(100);
            RefreshRates();
            selectedOrderModified = OrderModify(selectedOrderTicket, OrderOpenPrice(), stopLoss, takeProfit, 0, CLR_NONE);
            if (selectedOrderModified == FALSE) Print("Error modifying SELL order : ", GetLastError());
         }
   } else {
      if (AccountFreeMarginCheck(Symbol(), OP_SELL, normPrice(MML)) < 0.0) Comment("we recommend you to decrease your start lot or increase deposit");
      else Print("SELL, " + normPrice(MML) + " lots. ");
   }

   return(selectedOrderTicket);
}
int GetLatestPlacedOrder()
{
   int selectedOrderTicket = -1, latestOrderTicket = -1;
   for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) {
      OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         if (OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES))
         {
            selectedOrderTicket = OrderTicket();
            if (selectedOrderTicket > latestOrderTicket)
            {
               latestOrderTicket = selectedOrderTicket;
            }
         }
      }
   }
   
   return(latestOrderTicket);
}
//============================================================================================================
/*int GetOrderWithMinPositiveDistance()
{
   int selectedOrderTicket = -1;
   
   int actualPipDistance = -1, minPositivePipDistance = 0;
   for (ordersTotal = OrdersTotal(); ordersTotal >= 0; ordersTotal--) {
      actualPipDistance = -1;
      OrderSelect(ordersTotal, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         if (OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_TRADES))
         {
            if (OrderType() == OP_BUY)
            {
               actualPipDistance = (Ask - OrderOpenPrice()) / PointDigits;
            }
            else if (OrderType() == OP_SELL)
            {
               actualPipDistance = (OrderOpenPrice() - Bid) / PointDigits;
            }
         }
      }
      if (actualPipDistance > 0 && (minPositivePipDistance == 0 || minPositivePipDistance > actualPipDistance))
      {
         minPositivePipDistance = actualPipDistance;
         selectedOrderTicket = OrderTicket();
      }
   }
   
   return(selectedOrderTicket);
}*/
//============================================================================================================
bool CheckBlockCondition()
{
   if (AccountBalance() > 0)
   {
      if ( (AccountEquity()/AccountBalance())*100 >= EquityGreaterThan )
      {
         return(FALSE);
      }
   }
   
   if (AccountMargin() > 0)
   {
      if ( (AccountEquity()/AccountMargin())*100 >= EquityGreaterThan )
      {
         return(FALSE);
      }      
   }
   
   return(TRUE);
}
//============================================================================================================
int GetMarketCondition(double timeUnit)
{
   if (timeUnit >= Period())
   {
      double RSIBuf  = iCustom(Symbol(), 0, "TDI", 0, 1);
      double UpZone  = iCustom(Symbol(), 0, "TDI", 1, 1);
      double MdZone  = iCustom(Symbol(), 0, "TDI", 2, 1);
      double DnZone  = iCustom(Symbol(), 0, "TDI", 3, 1);
      double MaBuf   = iCustom(Symbol(), 0, "TDI", 4, 1);
      double MbBuf   = iCustom(Symbol(), 0, "TDI", 5, 1);

      //signals
         if((MaBuf>MbBuf)&&(MbBuf32)&&(MaBuf<68))
           {
            return(WEAK_BUY);
           }
         else if((MaBuf MdZone)&&(MaBuf> MdZone)&&(MaBuf>32)&&(MaBuf<68))
              {
               return(WEAK_SELL);
              }
            else if((MaBuf>MbBuf)&&(MbBuf> MdZone)&&(MaBuf>32)&&(MaBuf<68))
                 {
                  return(STRONG_BUY);
                 }
               else if((MaBuf>MbBuf)&&(MaBuf> MdZone)&&(MbBuf< MdZone)&&(MaBuf>32)&&(MaBuf<68))
                    {
                     return(MEDIUM_BUY);
                    }
                  else if((MaBuf32)&&(MaBuf<68))
                       {
                        return(STRONG_SELL);
                       }
                     else if((MaBuf MdZone)&&(MaBuf>32)&&(MaBuf<68))
                          {
                           return(MEDIUM_SELL);
                          }
         // reversals
                        else if(MaBuf>=68)
                             {
                              return(CAUTION);
                             }
                           else if(MaBuf<=32)
                                {
                                 return(CAUTION);
                                }
         //TDI - Trend Signals     
         if((MbBuf>MdZone)&&(MaBufMdZone)
              {
               return(STRONG_UP);
              }
         if((MbBuf<=MdZone)&&(MaBuf>=MdZone))
           {
            return(WEAK_DOWN);
           }
         else if (MbBuf<=MdZone)
              {
               return(STRONG_DOWN);
              }
         //ranging
         if(UpZone-DnZone<20)
           {
            return(CONSOLIDATION);
           }

   }
   
   return (-1);
}
//============================================================================================================

Recommend