Navigation´╝ÜHome > Content >

Testing1.4.mq4

Time: 2014-01-22 | Download file:Testing1.4.mq4

#property copyright ""
#property link      ""
#include 
#include 

//===================================================================================================================================================
extern string EAName             = "Testing";
//===================================================================================================================================================
extern int MagicNumberBuy        = 12345;   
extern int MagicNumberSell       = 54321;   
extern bool   OnScreenInfo       = true;   // Show info on chart
extern bool   ShowClock          = false;  // Show time on chart
extern bool   DrawAveragePrice   = false;  // Draw average price line
extern bool   DualTrade          = true;  // Allow buy and sell
extern bool   NewTradeBuy        = true;   // Allow buys
extern bool   NewTradeSell       = true;   // Allow sells 
extern bool   Reverse_Orders     = false;  // If True it will open buy order instead of sell 

extern string Setting            = ">>> Pips Settings"; 
extern bool   CheckNewBar        = true;   // on new bar
extern int    MaxTrades          = 100;     // Max number of open orders
extern double PipStep            = 25.0;   // Step between pips
extern double PipStepExponent    = 1.0;    // Exponent
extern int    StaticTakeProfit   = 500;     // Fixed Take Profit
extern double breakevengain      = 0;      // gain in pips required to enable the break even
extern double breakeven          = 0;      // break even

extern string ProfitSetting      = ">>> Close order settings";
extern double Profit_            = 0;     // Profit in $ to close all open orders
extern double Loss_              = 0;   // Loss in $ to close all open orders
extern double ProfitPersent      = 30;     // Percent to close(10...50) // percent to close
extern double SecondProfitPersent = 50;    // Percent to close previous last order
extern bool   CloseInMA          = true;

extern string LotSetting         = ">>> Calculating lot size";
extern double StartLot           = 0.1;    // Initial lot size
extern double LotIncrement       = 0.01;    // Increment lot to add  
extern double MaxLot             = 100;     // Maximum lot size
extern bool   UseAutoLot         = false;  // Use Auto-lot ... % deposit
extern double AutoLot            = 0.5;    // Percent - calculate lot size from equity
extern double AutoLotIncrement   = 0.3;    // Percent of lot to add

extern string SignalSetting      = ">>> Entry Logic"; 
extern int    Fastsmaperiod      = 5;
extern int    Hightsmaperiod     = 50;
extern int    Lowsmaperiod       = 50;
extern int    SMA_OpenDistance   = 0;
extern int    Rsi                = 14;
extern int    Rsi_level          = 50;
extern int    Macd_fast          = 12;
extern int    Macd_slow          = 26;
extern int    Macd_signal        = 9;
extern bool   MA_close           =true;
extern int    Up_fastma          =5;
extern int    Up_slowma          =50;
extern int    Dn_fastma          =5;
extern int    Dn_slowma          =50;

//===================================================================================================================================================
int i = 0;
int TimePrev = 0;
int vDigits;
int OrderSended = 0;
int TotalBuyOrders = 0, TotalSellOrders = 0;
int Lpos, Lpos1, Cpos;

double Spread;
double mPipStep;
double vPoint;
double PriceTarget, AveragePrice, LastBuyPrice, LastSellPrice;
double BuySummLot,SellSummLot,TotalProfitBuy,TotalProfitSell;
double BLot, SLot;
double Cprofit, Lprofit, Lprofit1, PrcCL;

string LastOrderComment = "";
string BComment, SComment;

//===================================================================================================================================================
//===================================================================================================================================================
int init()
{
  vPoint  = Point;
  vDigits = Digits;
  TimePrev = Time[0];
  Spread = NormalizeDouble(MarketInfo(Symbol(), MODE_SPREAD),vDigits)*vPoint;
  
  // 5 digit broker
  int DcD = 1;
  if((vDigits == 5)||(vDigits == 3)) DcD = 10;   
  PipStep           *= DcD;
  StaticTakeProfit  *= DcD;
  SMA_OpenDistance  *= DcD;
  
  if(OnScreenInfo) DrawInfo();
  
  return(0);
}

//===================================================================================================================================================
//===================================================================================================================================================
int deinit()
{
  ObjectDelete("CurrTime");
  ObjectDelete("BuyZeroLevel");
  ObjectDelete("BuyAveragePrice");
  ObjectDelete("SellZeroLevel");
  ObjectDelete("SellAveragePrice");

  return(0);
}

//===================================================================================================================================================
//===================================================================================================================================================
int start()
{
 
 if (OrdersTotal()>0 && Profit_>0 && NetProfit()>=Profit_){
    CloseAll(0);
    Sleep(1000);
    if (OrdersTotal()>0) CloseAll(0);
    }
    
 if (OrdersTotal()>0 && Loss_>0 && NetProfit()<=-Loss_){
    CloseAll(0);
    Sleep(1000);
    if (OrdersTotal()>0) CloseAll(0);
    } 
  // breakeven  
  if(breakevengain>0)movebreakeven(breakevengain,breakeven);    
  
  // clock
  if(ShowClock) ShowCurrentTime();

  // info
  if(OnScreenInfo) DrawInfo();

  // checking new bar
  if(TimePrev == Time[0] && CheckNewBar == true) return(0);   

  CheckOverlapping();  

  //-------------------------------------------------------------------------------------------------------------------------------------------------
  // New buy orders
  TotalBuyOrders = CountOfOrders(MagicNumberBuy);
  if(TotalBuyOrders > 0 && TotalBuyOrders < MaxTrades)
  {
    OrderSended = -1;
    LastSellPrice = FindLastOrderParameter(MagicNumberBuy, "price");

    if(LastSellPrice - Ask >= GetPipstepForStep(TotalBuyOrders + 1) * vPoint)
    {
      BLot   = GetLotForStep(MagicNumberBuy, TotalBuyOrders);
      BComment = StringSubstr(LastOrderComment, 0, StringFind(LastOrderComment, "|", 0)) + "|";
      OrderSended = SendMarketOrder(OP_BUY, BLot, 0, 0, MagicNumberBuy, BComment);
    }
  }
  
  
  //-------------------------------------------------------------------------------------------------------------------------------------------------
  // New sell orders
  TotalSellOrders = CountOfOrders(MagicNumberSell);
  if(TotalSellOrders > 0 && TotalSellOrders < MaxTrades)
  {
    OrderSended = -1;
    LastBuyPrice = FindLastOrderParameter(MagicNumberSell, "price");

    if (Bid - LastBuyPrice >= GetPipstepForStep(TotalSellOrders + 1) * vPoint) 
    {
      SLot   = GetLotForStep(MagicNumberSell, TotalSellOrders);
      SComment = StringSubstr(LastOrderComment, 0, StringFind(LastOrderComment, "|", 0)) + "|";
      OrderSended = SendMarketOrder(OP_SELL, SLot, 0, 0, MagicNumberSell, SComment);
    }
  }

  //-------------------------------------------------------------------------------------------------------------------------------------------------
  //Move Take Profit
  CheckTakeProfit();

  //-------------------------------------------------------------------------------------------------------------------------------------------------
  //Check new bar...first orders of the series only according to new bars

  if (TimePrev == Time[0]) return(0);   
  TimePrev = Time[0];

  int TradeSignal = GetSignal();
  if (Reverse_Orders) TradeSignal = -TradeSignal;  
  
  // New buy series ...
  if (TotalBuyOrders == 0 && NewTradeBuy && TradeSignal > 0 && (DualTrade == true || TotalSellOrders == 0))
    SendMarketOrder(OP_BUY, GetStartLot(), StaticTakeProfit, 0, MagicNumberBuy, TimeCurrent() + "|");     
  
  // New sell series ...
  if (TotalSellOrders == 0 && NewTradeSell && TradeSignal < 0 && (DualTrade == true || TotalBuyOrders == 0))
    SendMarketOrder(OP_SELL, GetStartLot(), StaticTakeProfit, 0, MagicNumberSell, TimeCurrent() + "|");
    //--close in signals
    if(CloseInMA==true)
    {
    if(GetSignalClose()==1) CloseAll(1);
    if(GetSignalClose()==-1) CloseAll(-1);
    }
  return(0);
}

//===================================================================================================================================================
//===================================================================================================================================================
void CheckTakeProfit()
{
  //BUY
  TotalBuyOrders = CountOfOrders(MagicNumberBuy);
  
  PriceTarget = 0;
  AveragePrice = 0;
  if(TotalBuyOrders > 0) 
  {
    PriceTarget = FindFirstOrderParameter(MagicNumberBuy, "price") + StaticTakeProfit*vPoint;
    AveragePrice = CalculateAveragePrice(MagicNumberBuy);
  }

  for (i = 0; i < OrdersTotal(); i++) 
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberBuy)
        if (NormalizeDouble(OrderTakeProfit(),vDigits) != NormalizeDouble(PriceTarget,vDigits)) ModifyTakeProfit(PriceTarget);

  if(DrawAveragePrice == true)
  {
    if(AveragePrice == 0)
    {
      if(ObjectFind("BuyZeroLevel") != -1) ObjectDelete("BuyZeroLevel");
      if(ObjectFind("BuyAveragePrice") != -1) ObjectDelete("BuyAveragePrice");
    }
    else
    {
      if(ObjectFind("BuyZeroLevel") == -1) 
      {
        ObjectCreate("BuyZeroLevel",OBJ_HLINE, 0, 0, AveragePrice);
        ObjectSet("BuyZeroLevel", OBJPROP_COLOR, Blue);
        ObjectSet("BuyZeroLevel", OBJPROP_STYLE, DRAW_SECTION);
      }
      else ObjectSet("BuyZeroLevel", OBJPROP_PRICE1, AveragePrice);
      
      if(ObjectFind("BuyAveragePrice") == -1) 
      {
        ObjectCreate("BuyAveragePrice",OBJ_HLINE, 0, 0, AveragePrice + StaticTakeProfit*vPoint);
        ObjectSet("BuyAveragePrice", OBJPROP_COLOR, Blue);
        ObjectSet("BuyAveragePrice", OBJPROP_STYLE, DRAW_LINE);
      }
      else ObjectSet("BuyAveragePrice", OBJPROP_PRICE1, AveragePrice + StaticTakeProfit*vPoint);
    }    
  }
  //<_BUY
  
  //SELL
  PriceTarget = 0;
  AveragePrice = 0;
  TotalSellOrders = CountOfOrders(MagicNumberSell);
  if(TotalSellOrders > 0) 
  {
    PriceTarget = FindFirstOrderParameter(MagicNumberSell, "price") - StaticTakeProfit*vPoint;
    AveragePrice = CalculateAveragePrice(MagicNumberSell);
  }

  for (i = 0; i < OrdersTotal(); i++) 
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberSell)
        if (NormalizeDouble(OrderTakeProfit(),vDigits) != NormalizeDouble(PriceTarget,vDigits)) ModifyTakeProfit(PriceTarget);
  
  if(DrawAveragePrice == true)
  {
    if(AveragePrice == 0)
    {
      if(ObjectFind("SellZeroLevel") != -1) ObjectDelete("SellZeroLevel");
      if(ObjectFind("SellAveragePrice") != -1) ObjectDelete("SellAveragePrice");
    }
    else
    {
      if(ObjectFind("SellZeroLevel") == -1) 
      {
        ObjectCreate("SellZeroLevel",OBJ_HLINE, 0, 0, AveragePrice);
        ObjectSet("SellZeroLevel", OBJPROP_COLOR, Red);
        ObjectSet("SellZeroLevel", OBJPROP_STYLE, DRAW_SECTION);
      }
      else ObjectSet("SellZeroLevel", OBJPROP_PRICE1, AveragePrice);

      if(ObjectFind("SellAveragePrice") == -1) 
      {
        ObjectCreate("SellAveragePrice",OBJ_HLINE, 0, 0, AveragePrice - StaticTakeProfit*vPoint);
        ObjectSet("SellAveragePrice", OBJPROP_COLOR, Red);
        ObjectSet("SellAveragePrice", OBJPROP_STYLE, DRAW_LINE);
      }
      else ObjectSet("SellAveragePrice", OBJPROP_PRICE1, AveragePrice - StaticTakeProfit*vPoint);
    }    
  }
  
  //<_SELL

}

//===================================================================================================================================================
//===================================================================================================================================================
double CalculateAveragePrice(int mNumber)
{
  double AveragePrice1 = 0;
  double Count = 0;
  for (int i1 = 0; i1 < OrdersTotal(); i1++)
    if(OrderSelect(i1, SELECT_BY_POS, MODE_TRADES))
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == mNumber)
        if (OrderType() == OP_BUY  || OrderType() == OP_SELL) 
        {
           AveragePrice1 += OrderOpenPrice() * OrderLots();
           Count += OrderLots();
        }
  
  if(AveragePrice1 > 0 && Count > 0)
    return( NormalizeDouble(AveragePrice1 / Count, vDigits));
  else
    return(0);
}

//===================================================================================================================================================
//===================================================================================================================================================
int GetSignal()
{
  int Signal = 0;

  double MA_Signal = iMA(Symbol(),0,Fastsmaperiod,0,MODE_SMA,PRICE_CLOSE,0);
  double MA_Signala = iMA(Symbol(),0,Hightsmaperiod,0,MODE_SMA,PRICE_HIGH,0);
  double MA_Signalb = iMA(Symbol(),0,Lowsmaperiod,0,MODE_SMA,PRICE_LOW,0);
  double MACD = iMACD(Symbol(),0,Macd_fast,Macd_slow,Macd_signal,PRICE_CLOSE,MODE_MAIN,0);
  double RSI = iRSI(Symbol(),0,Rsi,PRICE_CLOSE,0);
  double Ma_Bid_Diff = MathAbs(MA_Signal - Bid)/vPoint;
      
      if (MA_Signal>MA_Signala) if (MACD>0) if (RSI>Rsi_level) if (Ma_Bid_Diff > SMA_OpenDistance && Bid > MA_Signala)  Signal = 1; 
      if (MA_Signal SMA_OpenDistance && Bid < MA_Signalb)  Signal = -1;

  return(Signal);
}

//===================================================================================================================================================
//===================================================================================================================================================
int GetSignalClose()
{

  int MA_SignalClose=0;
         
  double iUp_fastma=iMA(Symbol(),0,Up_fastma,0,MODE_SMA,PRICE_CLOSE,0);
  double iDn_fastma=iMA(Symbol(),0,Dn_fastma,0,MODE_SMA,PRICE_CLOSE,0); 
  double iUp_slowma=iMA(Symbol(),0,Up_slowma,0,MODE_SMA,PRICE_HIGH,0);
  double iDn_slowma=iMA(Symbol(),0,Dn_slowma,0,MODE_SMA,PRICE_LOW,0);   
  
      if(iUp_fastmaiDn_slowma)if(Reverse_Orders)MA_SignalClose=-1; else MA_SignalClose=1;
      if(iDn_fastma>iUp_slowma && iDn_fastma MaxLot) CurrLot = MaxLot;
  if(CurrLot < StartLot) CurrLot = StartLot;
  
  if(CurrLot == 0) CurrLot = StartLot;
  
  return(CurrLot);
}

//===================================================================================================================================================
//===================================================================================================================================================
double GetStartLot()
{
  double FirstLot = 0;
  
  if(UseAutoLot == true)
  {
    FirstLot = NormalizeDouble(AccountEquity() * AutoLot/10000, 2);
  }
  else
  {
    FirstLot = StartLot;
  }
  
  if(FirstLot > MaxLot) FirstLot = MaxLot;
  if(FirstLot < StartLot) FirstLot = StartLot;
  
  return(FirstLot);
}

//===================================================================================================================================================
//===================================================================================================================================================
double GetPipstepForStep(int CurrStep)
{
  double CurrPipstep = NormalizeDouble(PipStep * MathPow(PipStepExponent,CurrStep), 0);
   
  return(CurrPipstep);
}

//===================================================================================================================================================
//===================================================================================================================================================
double FindFirstOrderParameter(int mNumber, string ParamName) 
{
  int mOrderTicket = 0;
  double mOrderPrice = 0;
  double mOrderLot = 0;
  double mOrderProfit = 0;
  int PrevTicket = 0;
  int CurrTicket = 0;
  for (i = OrdersTotal() - 1; i >= 0; i--) 
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == mNumber)
      {
        CurrTicket = OrderTicket();
        if (CurrTicket < PrevTicket || PrevTicket == 0)
        { 
          PrevTicket = CurrTicket;
          
          mOrderPrice = OrderOpenPrice();
          mOrderTicket = OrderTicket();
          mOrderLot = OrderLots();
          mOrderProfit = OrderProfit() + OrderSwap() + OrderCommission();
        }
      }
   
  if(ParamName == "price") return(mOrderPrice);
  else if(ParamName == "ticket") return(mOrderTicket);
  else if(ParamName == "lot") return(mOrderLot);
  else if(ParamName == "profit") return(mOrderProfit);
  return(0);
}

//===================================================================================================================================================
//===================================================================================================================================================
double FindLastOrderParameter(int mNumber, string ParamName) 
{
  int mOrderTicket = 0;
  double mOrderPrice = 0;
  double mOrderLot = 0;
  double mOrderProfit = 0;
  int PrevTicket = 0;
  int CurrTicket = 0;

  for (i = OrdersTotal() - 1; i >= 0; i--) 
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == mNumber) 
      {
        CurrTicket = OrderTicket();
        if (CurrTicket > PrevTicket) 
        {
          PrevTicket = CurrTicket;
          
          mOrderPrice = OrderOpenPrice();
          mOrderTicket = OrderTicket();
          mOrderLot = OrderLots();
          mOrderProfit = OrderProfit() + OrderSwap() + OrderCommission();
          LastOrderComment = OrderComment();
        }
      }
   
  if(ParamName == "price") return(mOrderPrice);
  else if(ParamName == "ticket") return(mOrderTicket);
  else if(ParamName == "lot") return(mOrderLot);
  else if(ParamName == "profit") return(mOrderProfit);
  return(0);
}

//===================================================================================================================================================
//===================================================================================================================================================
double GetClosedProfit(int mNumber)
{
  double ClosedProfit = 0;
  
  for (i = OrdersHistoryTotal(); i > 0; i--) 
    if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == mNumber)
        if(StringSubstr(LastOrderComment, 0, StringFind(LastOrderComment, "|", 0)) == StringSubstr(OrderComment(), 0, StringFind(OrderComment(), "|", 0)))
          ClosedProfit = ClosedProfit + OrderProfit();

  return(ClosedProfit);
}

//===================================================================================================================================================
//===================================================================================================================================================
bool ModifyTakeProfit(double takeprofit)
{
  while(!IsStopped())
  {
    if(IsTradeContextBusy())
    {
      Sleep(3000);
      continue;
    }
    if(!IsTradeAllowed())
    {
      return(False);
    }
    if(!OrderModify(OrderTicket(), OrderOpenPrice(), 0, NormalizeDouble(takeprofit,vDigits), 0, Yellow))
    {
      int Err = GetLastError();
      Print("!!! Error(",Err,"): ",ErrorDescription(Err));
      return(False);
    }
    else
    {
      break;
    }
  }
  
  return(True);
}

//===================================================================================================================================================
//===================================================================================================================================================
int SendMarketOrder(int Type, double Lots, int TP, int SL, int Magic, string Cmnt, double OpenPrice = 0, string mSymbol = "")
{
  double Price, Take, Stop;
  int Ticket, Color, Err; 
  int ErrorCount = 0;
  while(!IsStopped())
  {
    if(ErrorCount > 5) return(0);
    if(!IsConnected())
    {
      ErrorCount = ErrorCount + 1;
      Print("No connection with server!");
      Sleep(1000);
    }
    if(IsTradeContextBusy())
    {
      Sleep(3000);
      continue;
    }
    switch(Type)
    {
      case OP_BUY:
        if(mSymbol == "")
          Price = NormalizeDouble(Ask, vDigits);
        else
          Price = NormalizeDouble(MarketInfo(mSymbol, MODE_ASK), vDigits);
        Take = IIFd(TP == 0, 0, NormalizeDouble( Price + TP * vPoint, vDigits));
        Stop = IIFd(SL == 0, 0, NormalizeDouble( Price - SL * vPoint, vDigits));
        Color = Blue;
        break;
      case OP_SELL:
        if(mSymbol == "")
          Price = NormalizeDouble( Bid, vDigits);
        else
          Price = NormalizeDouble(MarketInfo(mSymbol, MODE_BID), vDigits);
        Price = NormalizeDouble( Bid, Digits);
        Take = IIFd(TP == 0, 0, NormalizeDouble( Price - TP * vPoint, vDigits));
        Stop = IIFd(SL == 0, 0, NormalizeDouble( Price + SL * vPoint, vDigits));
        Color = Red;
        break;
      default:
        return(-1);
    }
    if(IsTradeAllowed())
    {
      if(mSymbol == "")
        Ticket = OrderSend(Symbol(), Type, Lots, Price, 2*Spread, 0, 0, Cmnt, Magic, 0, Color); // amended code   
//        Ticket = OrderSend(Symbol(), Type, Lots, Price, 2*Spread, Stop, Take, Cmnt, Magic, 0, Color); // original code for this line
      else
        Ticket = OrderSend(mSymbol, Type, Lots, Price, 2*Spread, Stop, Take, Cmnt, Magic, 0, Color);
      
      if(Ticket < 0)
      {
        Err = GetLastError();
        if (Err == 4   || /* SERVER_BUSY */
            Err == 129 || /* INVALID_PRICE */ 
            Err == 135 || /* PRICE_CHANGED */ 
            Err == 137 || /* BROKER_BUSY */ 
            Err == 138 || /* REQUOTE */ 
            Err == 146 || /* TRADE_CONTEXT_BUSY */
            Err == 136 )  /* OFF_QUOTES */
        {
          Sleep(3000);
          continue;
        }
        else
        {
          break;
        }
      }
      break;
    }
    else
    {
      break;
    }
  }

  return(Ticket);
}

//===================================================================================================================================================
//===================================================================================================================================================
double IIFd(bool condition, double ifTrue, double ifFalse) 
{
  if (condition) return(ifTrue); else return(ifFalse);
}

//===================================================================================================================================================
//===================================================================================================================================================
void DrawInfo()
{
  BuySummLot = 0; TotalProfitBuy = 0;
  for(i=OrdersTotal();i>=0;i--)
  {
    if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && (OrderMagicNumber()==MagicNumberBuy)) 
    {
      BuySummLot += OrderLots(); 
      TotalProfitBuy += OrderProfit() + OrderCommission() + OrderSwap();
    }
  }
  double ClosedBuyProfit = GetClosedProfit(MagicNumberBuy);
  
  SellSummLot = 0; TotalProfitSell = 0;
  for(i=OrdersTotal();i>=0;i--)
  {
    if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && (OrderMagicNumber()==MagicNumberSell)) 
    {
      SellSummLot += OrderLots(); 
      TotalProfitSell += OrderProfit() + OrderCommission() + OrderSwap();
    }
  }
  double ClosedSellProfit = GetClosedProfit(MagicNumberSell);
  
  Comment(
  "\n",
  ">>> BUY  Orders: ",TotalBuyOrders," lots: ",BuySummLot," Profit: ",TotalProfitBuy,
  "\n",">>> Profit Taken:",ClosedBuyProfit,"\n",
  "\n",
  ">>> SELL Orders: ",TotalSellOrders," lots: ",SellSummLot," Profit: ",TotalProfitSell,
  "\n",">>> Profit taken :", ClosedSellProfit
  );

}

//===================================================================================================================================================
//===================================================================================================================================================
void ShowCurrentTime()
{
  int min,sec;
  min = Time[0] + Period()*60 - CurTime();
  sec = min%60;
  min = (min - min%60)/60;
	
  if(ObjectFind("CurrTime") != 0)
    ObjectCreate("CurrTime", OBJ_TEXT, 0, Time[0], Close[0]);
  else
    ObjectMove("CurrTime", 0, Time[0], Close[0]);
 
  ObjectSetText("CurrTime", "                <" + min + ":" + sec, 14, "Verdana", Black);
}

//===================================================================================================================================================
//===================================================================================================================================================
void CheckOverlapping()
{
  //BUY--->
  TotalBuyOrders = CountOfOrders(MagicNumberBuy);
  if (TotalBuyOrders >= 2) 
  {
    Lpos = 0; Cpos = 0; Lprofit = 0; Cprofit = 0;
    Lpos = LidingProfitOrder(MagicNumberBuy);
    Cpos = CloseProfitOrder(MagicNumberBuy);
    
    if(Lprofit > 0 && Lprofit1 <= 0)
    {
      if(Lprofit + Cprofit > 0 && (Lprofit + Cprofit)*100/Lprofit > ProfitPersent) 
      {
        Lpos1 = 0;
        CloseSelectOrder(MagicNumberBuy); 
      }
    }
    else if(Lprofit > 0 && Lprofit1 > 0)
    {
      if(Lprofit + Lprofit1 + Cprofit > 0 && (Lprofit + Lprofit1 + Cprofit)*100/(Lprofit + Lprofit1) > SecondProfitPersent) CloseSelectOrder(MagicNumberBuy); 
    }
  } 
  //<---BUY

  //SELL--->
  TotalSellOrders = CountOfOrders(MagicNumberSell);
  if (TotalSellOrders >= 2) 
  {
    Lpos = 0; Cpos = 0; Lprofit = 0; Cprofit = 0;
    Lpos = LidingProfitOrder(MagicNumberSell);
    Cpos = CloseProfitOrder(MagicNumberSell);
       
    if(Lprofit > 0 && Lprofit1 <= 0)
    {
      if(Lprofit + Cprofit > 0 && (Lprofit + Cprofit)*100/Lprofit > ProfitPersent) 
      {
        Lpos1 = 0;
        CloseSelectOrder(MagicNumberSell); 
      }
    }  
    if(Lprofit > 0 && Lprofit1 > 0)
    {
      if(Lprofit + Lprofit1 + Cprofit > 0 && (Lprofit + Lprofit1 + Cprofit)*100/(Lprofit + Lprofit1) > SecondProfitPersent) CloseSelectOrder(MagicNumberSell); 
    }
  } 
  //<---SELL
}


//======================================== Most profitable order =======================================
int LidingProfitOrder(int mNumber) 
{
   Lprofit1 = 0;
   Lpos1 = 0;
   int TotalOrders = CountOfOrders(mNumber);
   double profit  = 0;
   int    Pos     = 0;
   for (i = 0; i < OrdersTotal(); i++) 
   {
       if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))  
       {
          if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mNumber))
          {
             if (OrderType() == OP_SELL || OrderType() == OP_BUY) 
             { 
                profit = OrderProfit();
                Pos    = OrderTicket();
                if (profit > 0 && profit > Lprofit) {
                   // Previous value
                   Lprofit1 = Lprofit;
                   Lpos1    = Lpos;

                   // Maximum value
                   Lprofit = profit;
                   Lpos    = Pos;
                }
             }
          }
       }   
   }    
   return (Lpos);
} 
//========================================  Least Profitable Order =======================================
int CloseProfitOrder(int mNumber) 
{
   double profit  = 0;
   int    Pos     = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
       if (OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))  {
          if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mNumber)){
             if (OrderType() == OP_SELL || OrderType() == OP_BUY) { 
                profit = OrderProfit();
                Pos    = OrderTicket();
                if (profit < 0 && profit < Cprofit) {
                   Cprofit = profit;
                   Cpos    = Pos;
                }
             }
          }
       }   
   }    
   return (Cpos);
}
//==========================================  Closing Orders ===============================================
int CloseSelectOrder(int mNumber)
{
  int error =  0;
  int error1 = 0;
  int error2 = 0;
  int Result = 0;
//                       ----------------------  Last Order -----------------------                            
       
  while (error1 == 0) 
  {
          RefreshRates();
            i = OrderSelect(Lpos, SELECT_BY_TICKET, MODE_TRADES);
            if  (i != 1 ) {
                Print ("Error! Not possible to select most profitable order . Operation cancelled.");
                return (0);
            }  
            if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mNumber)) {
               if (OrderType() == OP_BUY) {
                  error1 =  (OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), Spread, Blue));
                  if (error1 == 1) {
                     Print ("Leading Order closed successfully"); 
                     Sleep (500);   
                  } else {
                     Print ("Error closing leading order, Repeat Operation. ");                     
                  }      
               } 
//                        -----------------------------------------------------               
               if (OrderType() == OP_SELL) {
                  error1 = (OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), Spread, Red));
                  if (error1 == 1) {
                     Print ("Leading Order closed successfully"); 
                     Sleep (500);   
                  } else {
                     Print ("Error closing leading order, Repeat Operation. ");                     
                  }
               }
            } 
      }

//                       ---------------------- Previous Last  -----------------------                            		
      if(Lpos1 != 0)
      {
      while (error2 == 0) {
            RefreshRates();
            i = OrderSelect(Lpos1, SELECT_BY_TICKET, MODE_TRADES);
            if  (i != 1 ) {
                Print ("Error! Not possible to select previous most profitable order . Operation cancelled.");
                return (0);
            }  
            if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mNumber)) {
               if (OrderType() == OP_BUY) {
                  error2 =  (OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), Spread, Blue));
                  if (error2 == 1) {
                     Print ("Previous leading order closed successfully");
                     Sleep (500);   
                  } else {
                     Print ("Error closing previous leading order, Repeat Operation. ");                     
                  }      
               } 
//                        -----------------------------------------------------               
               if (OrderType() == OP_SELL) {
                  error2 = (OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), Spread, Red));
                  if (error2 == 1) {
                     Print ("Previous leading order closed successfully"); 
                     Sleep (500);   
                  } else {
                     Print ("Error closing previous leading order, Repeat Operation. ");                     
                  }
               }
            } 
      }
      }
//								----------- Selected (Least profitable order ) -----------
      while (error == 0) {
            RefreshRates();
            int i3 = OrderSelect(Cpos, SELECT_BY_TICKET, MODE_TRADES);
            if  (i3 != 1 ) {
                Print ("Error! Not possible to select least profitable order. Operation cancelled");
                return (0);
            }    
            if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == mNumber)) {
               if (OrderType() == OP_BUY) {
                  error = (OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), Spread, Blue)); 
                  if (error == 1 ) {
                     Print ("Order closed successfully."); 
                     Sleep (500);   
                  } else {
                     Print ("Error during Order Close. Repeat operation.  ");                    
                  } 
               }        
//                             --------------------------------------------------                
               if (OrderType() == OP_SELL) {
                  error = (OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), Spread, Red));
                  if (error == 1) {
						Print ("Order closed successfully."); 
                     Sleep (500);   
                  } else {
						  Print ("Error during Order Close. Repeat operation.  ");                     
                  }
               }
            }
      }     
       
  Result = 1;
  return (Result);    
} 

//================================================= Calculate Net Profit ===============================================//

double NetProfit() {
   double Profit = 0;
   for (int i4 = OrdersTotal() - 1; i4 >= 0; i4--) 
   {
      if(OrderSelect(i4, SELECT_BY_POS, MODE_TRADES))
      {
      if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MagicNumberBuy || OrderMagicNumber() == MagicNumberSell)) 
      {
      Print("Profit_ = ",Profit);
      if (OrderType() <= OP_SELL) Profit += OrderProfit();
      }
   }
   }
   return (Profit);
} 


//================================================== Close All Orders ===================================================//

int CloseAll(int OrdrType) 
{ 
bool ClTicket=false;
   for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--) 
   { 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
      { 
      if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MagicNumberBuy || OrderMagicNumber() == MagicNumberSell)  && OrderCloseTime()==0) 
      { 
            if((OrderType()==OP_BUY)&&((OrdrType==0)||(OrdrType==1)))  ClTicket=OrderClose(OrderTicket(),OrderLots(),Bid,2*Spread,Blue); 
            if((OrderType()==OP_SELL)&&((OrdrType==0)||(OrdrType==-1))) ClTicket=OrderClose(OrderTicket(),OrderLots(),Ask,2*Spread,Red); 
      } 
   }
   }
   return(0); 
}

//================================================== breakeven  ===================================================//

void movebreakeven(double breakevengain1,double breakeven1)
{
bool ModOrders=false;
   RefreshRates();
   if(OrdersTotal()>0)
   {
      for(i=OrdersTotal();i>=0;i--)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
         if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && (OrderMagicNumber() == MagicNumberBuy || OrderMagicNumber() == MagicNumberSell))
         {
            if(OrderType()==OP_BUY)
            {
               if(NormalizeDouble((Bid-OrderOpenPrice()),vDigits)>=NormalizeDouble(breakevengain1*vPoint,vDigits))
               {
                  if((NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),vDigits)=NormalizeDouble(breakevengain1*vPoint,vDigits))
               {
                  if((NormalizeDouble((OrderOpenPrice()-OrderStopLoss()),vDigits)        

Recommend