//+------------------------------------------------------------------+
//|                                                      BasicOp.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property library
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int Slippage = 50;
           
void CloseBuy()
{
   if(CountBuy() > 0)
   {
      for(int i=OrdersTotal()-1; i >=0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == true)
         {
            if(OrderSymbol() == Symbol())
            {
               if (OrderType() == OP_BUY) int resp = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Black);
            }
         }
      }
   }
}

void CloseSell()
{
   if(CountSell() > 0)
   {
      for(int i=OrdersTotal()-1; i >=0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == true)
         {
            if(OrderSymbol() == Symbol())
            {
               if (OrderType() == OP_SELL) int resp = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Black);
            }
         }
      }
   }
}

void CloseBuySymbol(string sym)
{
   if(CountBuySymbol(sym) > 0)
   {
      for(int i=OrdersTotal()-1; i >=0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == true)
         {
            if(OrderSymbol() == sym)
            {
               if (OrderType() == OP_BUY) int resp = OrderClose(OrderTicket(), OrderLots(), MarketInfo(sym,MODE_BID), Slippage, Black);
            }
         }
      }
   }
}

void CloseSellSymbol(string sym)
{
   if(CountSellSymbol(sym) > 0)
   {
      for(int i=OrdersTotal()-1; i >=0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == true)
         {
            if(OrderSymbol() == sym)
            {
               if (OrderType() == OP_SELL) int resp = OrderClose(OrderTicket(), OrderLots(), MarketInfo(sym,MODE_ASK), Slippage, Black);
            }
         }
      }
   }
}


int CountBuy()
{
   int count = 0;
   for(int trade = OrdersTotal()-1; trade >= 0; trade--)
   { 
      int resp = OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol())
      {
         if (OrderType() == OP_BUY) count++;
      }
   }
   return(count);
}

int CountSell()
{
   int count = 0;
   for(int trade = OrdersTotal()-1; trade >= 0; trade--)
   { 
      int resp = OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol())
      {
         if (OrderType() == OP_SELL) count++;
      }
   }
   return(count);
}

int CountBuySymbol(string sym)
{
   int count = 0;
   for(int trade = OrdersTotal()-1; trade >= 0; trade--)
   { 
      int resp = OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == sym)
      {
         if (OrderType() == OP_BUY) count++;
      }
   }
   return(count);
}

int CountSellSymbol(string sym)
{
   int count = 0;
   for(int trade = OrdersTotal()-1; trade >= 0; trade--)
   { 
      int resp = OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == sym)
      {
         if (OrderType() == OP_SELL) count++;
      }
   }
   return(count);
}

int CountSymbol(string sym)
{
   int count = 0;
   for(int trade = OrdersTotal()-1; trade >= 0; trade--)
   { 
      int resp = OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == sym)
      {
         count++;
      }
   }
   return(count);
}

void OpenBuy(double lot, double tp, double sl)
{
   double TPo = 0;
   double SLo = 0;
   if (tp != 0) TPo = NormalizeDouble(Bid + tp * Point, Digits);
   if (sl != 0) SLo = NormalizeDouble(Bid - sl * Point, Digits);
   int resp = OrderSend(Symbol(), OP_BUY, lot, Ask, Slippage, SLo, TPo, NULL, 0, 0, Blue);
}

void OpenSell(double lot, double tp, double sl)
{
   double TPo = 0;
   double SLo = 0;
   if (tp != 0) TPo = NormalizeDouble(Ask - tp * Point, Digits);
   if (sl != 0) SLo = NormalizeDouble(Ask + sl * Point, Digits);
   int resp = OrderSend(Symbol(), OP_SELL, lot, Bid, Slippage, SLo, TPo, NULL, 0, 0, Red);
}

void OpenBuyPrice(double lot, double tp, double sl)
{
   double TPo = 0;
   double SLo = 0;
   if (tp != 0) TPo = NormalizeDouble(tp, Digits);
   if (sl != 0) SLo = NormalizeDouble(sl, Digits);
   int resp = OrderSend(Symbol(), OP_BUY, lot, Ask, Slippage, SLo, TPo, NULL, 0, 0, Blue);
}

void OpenSellPrice(double lot, double tp, double sl)
{
   double TPo = 0;
   double SLo = 0;
   if (tp != 0) TPo = NormalizeDouble(tp, Digits);
   if (sl != 0) SLo = NormalizeDouble(sl, Digits);
   int resp = OrderSend(Symbol(), OP_SELL, lot, Bid, Slippage, SLo, TPo, NULL, 0, 0, Red);
}

void OpenBuySymbol(string sym, double lot, double tp, double sl)
{
   double TPo = 0;
   double SLo = 0;
   if (tp != 0) TPo = NormalizeDouble(Bid + tp * Point, Digits);
   if (sl != 0) SLo = NormalizeDouble(Bid - sl * Point, Digits);
   int resp = OrderSend(sym, OP_BUY, lot, Ask, Slippage, SLo, TPo, NULL, 0, 0, Blue);
}

void OpenSellSymbol(string sym, double lot, double tp, double sl)
{
   double TPo = 0;
   double SLo = 0;
   if (tp != 0) TPo = NormalizeDouble(Ask - tp * Point, Digits);
   if (sl != 0) SLo = NormalizeDouble(Ask + sl * Point, Digits);
   int resp = OrderSend(sym, OP_SELL, lot, Bid, Slippage, SLo, TPo, NULL, 0, 0, Red);
}

void CommentService(string str1)
{
      Comment(
      "\n--------------------------------------------"      
      + str1 +
      "\n--------------------------------------------");

}

bool isNewBar(datetime ct)
{
   if (ct != Time[0])
   {
      return true;
   }
   else return false;
}

int trend(string sym, int tf)
{
   int ADX_Period=14;
   int ADX_Price=PRICE_CLOSE;
   double Step_Psar=0.02;
   double Max_Psar=0.2; 
   double PADX=iADX(sym,tf,ADX_Period,ADX_Price,1,0);
   double NADX=iADX(sym,tf,ADX_Period,ADX_Price,2,0);
   double Psar=iSAR(sym,tf,Step_Psar,Max_Psar,0) ;
   if (Psar < iClose(sym,tf,0) && PADX > NADX) return 1;
   else if (Psar > iClose(sym,tf,0) && NADX > PADX) return -1;
   else return 0;
}

int trendMA(string sym, int tf)
{
   int MA_Period=100;
   int MA_Price=PRICE_CLOSE;
   double MA=iMA(sym,tf,MA_Period,0,MODE_LWMA,MA_Price,0);
   if(MA < iClose(sym,tf,0)) return 1;
   else if (MA > iClose(sym,tf,0)) return -1;
   else return 0;
}

double LotCount(string sym, int risk)
{
   double Free =AccountFreeMargin();
   double One_Lot =MarketInfo(sym,MODE_MARGINREQUIRED);
   double Step =MarketInfo(sym,MODE_LOTSTEP);
   double Min_Lot =MarketInfo(sym,MODE_MINLOT);
   double Max_Lot =MarketInfo(sym,MODE_MAXLOT);
   double Lot =MathFloor(Free*risk/100/One_Lot/Step)*Step;
   if(Lot<Min_Lot) Lot=Min_Lot;
   if(Lot>Max_Lot) Lot=Max_Lot;
   return Lot;
}

bool tradehours(int starth, int end)
{
   if(end == 0) end = 24;
   if(Hour() >= starth && Hour() < end) return true;
   else return false;
}

double ATRsl(string sym, int tf)
{
   double up = iCustom(sym,tf,"NRTR_ATR_STOP",0,0);
   double dn = iCustom(sym,tf,"NRTR_ATR_STOP",1,0);
   if(up > dn) return up;
   else return (0-dn);
}

int trflat(string sym)
{
   double ema1,ema2,sto;
   ema1=iMA(sym,60,100,0,MODE_SMMA,PRICE_CLOSE,0);
   ema2=iMA(sym,60,50,0,MODE_SMMA,PRICE_CLOSE,0);
   sto=iStochastic(sym,PERIOD_H4,30,3,3,MODE_SMA,0,MODE_MAIN,0);      
  
   if(ema2>ema1 && sto>50) 
   {
      return 1;  
   } 
   if(ema2<ema1 && sto<50)
   {
      return -1;
   }
   if((ema2>ema1 && sto<50) || (ema2<ema1 && sto>50))
   {
      return 0;
   }
   else return 0;
}
