#property  copyright " Copyright© 2008, www.bankingfx.com"
#property  link "http://www.bankingfx.com"









//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
extern string  ParametersMM = "----------Параметры усреднения----------";
extern double  MaximalRisk = 5000;
extern double  MM_K = 2;
extern int     Orders_Total = 6;

extern bool    SAR = false;
extern double  step = 0.02;
extern double  maximum  = 0.2;

extern string  rrr1 = "--------------------------------------------------------------------";
extern int     DistanceOpeningPips = 10;
extern int     StepOpeningPips = 3;
extern string  rrr2 = "----------";
extern int     OpeningPendingPips_2 = 30;
extern int     OpeningPendingPips_3 = 30;
extern int     OpeningPendingPips_4 = 30;
extern int     OpeningPendingPips_5 = 30;
extern int     OpeningPendingPips_6 = 30;
extern string  rrr3 = "--------------------------------------------------------------------";
extern int     DistanceStopLossPips = 10;
extern int     StepStopLossPips = 3;
extern string  rrr4 = "----------";
extern int     StartTrailingPercent_2 = 50;
extern int     StartTrailingPercent_3 = 50;
extern int     StartTrailingPercent_4 = 50;
extern int     StartTrailingPercent_5 = 40;
extern int     StartTrailingPercent_6 = 35;
extern bool    Sounds = true;
extern bool    Comments = true;

extern bool    Is5Digits = true;
extern int     DIGITS = 0;
extern int     Magic = 9815756;



string Period_str;
int TimerComment;
int Error_4_count,Error_6_count,Error_129_count,Error_135_count,Error_136_count,Error_137_count,Error_138_count,Error_146_count;

int Total_Buy,Total_Sell,Total_Pending_Buy,Total_Pending_Sell,Digits_r;
double Orders_Profit,Min_Price_Buy,Max_Price_Buy,Min_Price_Sell,Max_Price_Sell,Lots_Max_Buy,Lots_Max_Sell,SL_Price_Buy,TP_Price_Buy,SL_Price_Sell,TP_Price_Sell;
double SAR_0,SAR_1,SAR_2;
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM









extern   string   BankingFX_Diamond_Parameters = "---------- EA BankingFX Diamond EUR-GBP 15M ----------";
//extern   int      Magic = 9815756;
extern   int      Hour_Start_Trade = 22;
extern   int      Hour_Stop_Trade = 2;
int      LastMinute = 50;
extern   string   Lots_Parameters = "---------- Lots - Parameters ----------";
extern   double   Lots = 0.1;
extern   bool     LotsOptimized = true;
extern   int      Risk = 10;
extern   int      Maximum_Lots = 100;
extern   string   Order_Parameters = "---------- Order - Parameters ----------";
extern   double   StopLoss = 32;//?????????????????
extern   double   TakeProfit = 5;//?????????????????
extern   bool     Virtual_TP = false;
extern   int      slippage = 4;//?????????????????
extern   int      slippage_close = 4;//????????????
extern   double   MaxSpread = 2.1;//??????????????????
extern   bool     Time_Opened_Protection = true;
extern   int      Begin1 = 60;
extern   int      Pips1 = 3;//?????????????????????
extern   int      Begin2 = 140;
extern   int      Pips2 = 2;//????????????????????
extern   int      Begin3 = 170;
extern   int      Pips3 = 0;//????????????????????
extern   int      Begin4 = 200;
extern   int      Pips4 = 1;//????????????????????
extern   int      Begin5 = 240;
extern   int      Pips5 = 4;//????????????????????
extern   string   Volatility_Parameters = "---------- Volatility - Parameters ----------";
extern   bool     Volatility_Filter = false;
extern   double   sec = 0.001;
extern   double   se2c = 0.001;
extern   double   se3c = 0.0012;
int      SMA_period = 8;
double   RSI_period = 6;
double   TF = PERIOD_M15;
double   MinRSI1 = 30;
double   MaxRSI1 = 70;
double   MinRSI2 = 40;
double   MaxRSI2 = 60;
bool     NeedSendBuy = true;
bool     NeedSendSell = true;
double   MinLot = 0;
double   MaxLot = 0;
int      LotSize = 0;
int      Leverage = 0;
double   LT;

//+------------------------------------------------------------------+

int init()
{
Digits_r=DIGITS;
if(DIGITS==0) Digits_r=Digits;

if(Is5Digits)
   {
   StopLoss *= 10;
   TakeProfit *= 10;
   slippage *= 10;
   slippage_close *= 10;
   MaxSpread *= 10;
   Pips1 *= 10;
   Pips2 *= 10;
   Pips3 *= 10;
   Pips4 *= 10;
   Pips5 *= 10;
   
   DistanceOpeningPips *= 10;
   StepOpeningPips *= 10;
   OpeningPendingPips_2 *= 10;
   OpeningPendingPips_3 *= 10;
   OpeningPendingPips_4 *= 10;
   OpeningPendingPips_5 *= 10;
   OpeningPendingPips_6 *= 10;
   DistanceStopLossPips *= 10;
   StepStopLossPips *= 10;
   }

if(Comments)
   {
   ObjectCreate("Comment",OBJ_LABEL,0,0,0);
   ObjectSet("Comment",OBJPROP_CORNER,2);
   ObjectSet("Comment",OBJPROP_XDISTANCE,10);
   ObjectSet("Comment",OBJPROP_YDISTANCE,10);
   ObjectSetText("Comment"," ",10,"Arial",Lime);
   }

return(0);
}

//+------------------------------------------------------------------+

int deinit()
{

ObjectDelete("Comment");

return(0);
}

//+------------------------------------------------------------------+

int start()
{
Comment("  ---   Server Time = ",TimeToStr(TimeCurrent(),TIME_MINUTES));

if ((Symbol() != "EURGBP") && (Symbol() != "EURGBPm")) return;





//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

Total_Buy=0;
Total_Sell=0;
Total_Pending_Buy=0;
Total_Pending_Sell=0;
Orders_Profit=0;
Min_Price_Buy=0;
Max_Price_Buy=0;
Min_Price_Sell=0;
Max_Price_Sell=0;
Lots_Max_Buy=0;
Lots_Max_Sell=0;
SL_Price_Buy=0;
TP_Price_Buy=0;
SL_Price_Sell=0;
TP_Price_Sell=0;

bool New_Buy,New_Sell;

if(TimerComment<TimeLocal()-10)
   {
   ObjectSetText("Comment"," ",10,"Arial",Lime);
   WindowRedraw();
   }

int Total_Buy_Sell;

Total_Buy_Sell=Orders_Total_Buy_Sell();

if(SAR)
   {
   SAR_0=iSAR(NULL,0,step,maximum,0);
   SAR_1=iSAR(NULL,0,step,maximum,1);
   SAR_2=iSAR(NULL,0,step,maximum,2);
   
   if(SAR_0<Low[0] && SAR_1<Low[1] && SAR_2>Low[2]) New_Buy=true;
   if(SAR_0>High[0] && SAR_1>High[1] && SAR_2<High[2]) New_Sell=true;
   }

if(Total_Buy==1 && Total_Pending_Buy==0 && Orders_Total>1)
   {
   if(Ask<=Min_Price_Buy-OpeningPendingPips_2*Point)
      {
      if(!SAR) Open_Order(OP_BUYSTOP, Lots_Max_Buy*MM_K, Ask+DistanceOpeningPips*Point, SL_Price_Buy, TP_Price_Buy);
      else if(New_Buy) Open_Order(OP_BUY, Lots_Max_Buy*MM_K, Ask, SL_Price_Buy, TP_Price_Buy, slippage);
      }
   }

if(Total_Sell==1 && Total_Pending_Sell==0 && Orders_Total>1)
   {
   if(Bid>=Max_Price_Sell+OpeningPendingPips_2*Point)
      {
      if(!SAR) Open_Order(OP_SELLSTOP, Lots_Max_Sell*MM_K, Bid-DistanceOpeningPips*Point, SL_Price_Sell, TP_Price_Sell);
      else if(New_Sell) Open_Order(OP_SELL, Lots_Max_Sell*MM_K, Bid, SL_Price_Sell, TP_Price_Sell, slippage);
      }
   }

if(Total_Buy>1 && Total_Pending_Buy==0)
   {
   if(Total_Buy==2 && Orders_Total>2)
      {
      if(Ask<=Min_Price_Buy-OpeningPendingPips_3*Point)
         {
         if(!SAR) Open_Order(OP_BUYSTOP, Lots_Max_Buy*MM_K, Ask+DistanceOpeningPips*Point, SL_Price_Buy, TP_Price_Buy);
         else if(New_Buy) Open_Order(OP_BUY, Lots_Max_Buy*MM_K, Ask, SL_Price_Buy, TP_Price_Buy, slippage);
         }
      }
   if(Total_Buy==3 && Orders_Total>3)
      {
      if(Ask<=Min_Price_Buy-OpeningPendingPips_4*Point)
         {
         if(!SAR) Open_Order(OP_BUYSTOP, Lots_Max_Buy*MM_K, Ask+DistanceOpeningPips*Point, SL_Price_Buy, TP_Price_Buy);
         else if(New_Buy) Open_Order(OP_BUY, Lots_Max_Buy*MM_K, Ask, SL_Price_Buy, TP_Price_Buy, slippage);
         }
      }
   if(Total_Buy==4 && Orders_Total>4)
      {
      if(Ask<=Min_Price_Buy-OpeningPendingPips_5*Point)
         {
         if(!SAR) Open_Order(OP_BUYSTOP, Lots_Max_Buy*MM_K, Ask+DistanceOpeningPips*Point, SL_Price_Buy, TP_Price_Buy);
         else if(New_Buy) Open_Order(OP_BUY, Lots_Max_Buy*MM_K, Ask, SL_Price_Buy, TP_Price_Buy, slippage);
         }
      }
   if(Total_Buy==5 && Orders_Total>5)
      {
      if(Ask<=Min_Price_Buy-OpeningPendingPips_6*Point)
         {
         if(!SAR) Open_Order(OP_BUYSTOP, Lots_Max_Buy*MM_K, Ask+DistanceOpeningPips*Point, SL_Price_Buy, TP_Price_Buy);
         else if(New_Buy) Open_Order(OP_BUY, Lots_Max_Buy*MM_K, Ask, SL_Price_Buy, TP_Price_Buy, slippage);
         }
      }
   }

if(Total_Sell>1 && Total_Pending_Sell==0)
   {
   if(Total_Sell==2 && Orders_Total>2)
      {
      if(Bid>=Max_Price_Sell+OpeningPendingPips_3*Point)
         {
         if(!SAR) Open_Order(OP_SELLSTOP, Lots_Max_Sell*MM_K, Bid-DistanceOpeningPips*Point, SL_Price_Sell, TP_Price_Sell);
         else if(New_Sell) Open_Order(OP_SELL, Lots_Max_Sell*MM_K, Bid, SL_Price_Sell, TP_Price_Sell, slippage);
         }
      }
   if(Total_Sell==3 && Orders_Total>3)
      {
      if(Bid>=Max_Price_Sell+OpeningPendingPips_4*Point)
         {
         if(!SAR) Open_Order(OP_SELLSTOP, Lots_Max_Sell*MM_K, Bid-DistanceOpeningPips*Point, SL_Price_Sell, TP_Price_Sell);
         else if(New_Sell) Open_Order(OP_SELL, Lots_Max_Sell*MM_K, Bid, SL_Price_Sell, TP_Price_Sell, slippage);
         }
      }
   if(Total_Sell==4 && Orders_Total>4)
      {
      if(Bid>=Max_Price_Sell+OpeningPendingPips_5*Point)
         {
         if(!SAR) Open_Order(OP_SELLSTOP, Lots_Max_Sell*MM_K, Bid-DistanceOpeningPips*Point, SL_Price_Sell, TP_Price_Sell);
         else if(New_Sell) Open_Order(OP_SELL, Lots_Max_Sell*MM_K, Bid, SL_Price_Sell, TP_Price_Sell, slippage);
         }
      }
   if(Total_Sell==5 && Orders_Total>5)
      {
      if(Bid>=Max_Price_Sell+OpeningPendingPips_6*Point)
         {
         if(!SAR) Open_Order(OP_SELLSTOP, Lots_Max_Sell*MM_K, Bid-DistanceOpeningPips*Point, SL_Price_Sell, TP_Price_Sell);
         else if(New_Sell) Open_Order(OP_SELL, Lots_Max_Sell*MM_K, Bid, SL_Price_Sell, TP_Price_Sell, slippage);
         }
      }
   }

if((Total_Pending_Buy>0 && Total_Buy>1) || (Total_Pending_Sell>0 && Total_Sell>1)) TrailingOrder();

if((Total_Pending_Buy==0 && Total_Buy>1) || (Total_Pending_Sell==0 && Total_Sell>1)) TrailingStop();

if(Total_Buy_Sell>0 && Orders_Profit<=(0-MathAbs(MaximalRisk)))
   {
   Close_Order();
   Delete_Order();
   }

if(Total_Buy>1 || Total_Sell>1) return (0);

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM





/*if ((AccountNumber() != 1106347) && (AccountNumber() != 1107403) && (AccountNumber() != 19762) && (AccountNumber() != 2029289))
   {
   Comment("=== EA BankingFx Diamond work ONLY in account 1107403, 1106347 ===");
   Print("=== EA BankingFx Diamond work ONLY in account 1107403, 1106347 ===");
   return;
   }

int day = 4;
int month = 11;
int year = 2008;

if (((TimeDay(TimeCurrent()) > day) && (TimeMonth(TimeCurrent()) >= month) && (TimeYear(TimeCurrent()) >= year)) || ((TimeMonth(TimeCurrent()) > month) && (TimeYear(TimeCurrent()) >= year)) || (TimeYear(TimeCurrent()) > year))
   {
   Comment(" EA BankingFx Diamond EUR-GBP - EXPIRED ");
   Print(" EA BankingFx Diamond EUR-GBP - EXPIRED ");
   return;
   }*/

double tp = TakeProfit;

if (Virtual_TP == true)
   {
   for (int i = 0; i < OrdersTotal(); i++)
      {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if ((OrderType() == OP_BUY) && (OrderMagicNumber() == Magic))
         {
         if (Bid >= OrderOpenPrice() + TakeProfit * Point)
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage_close,Orange);
            Print("Virtual TP - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         }
      if ((OrderType() == OP_SELL) && (OrderMagicNumber() == Magic))
         {
         if (Ask <= OrderOpenPrice() - TakeProfit * Point)
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage_close,Orange);
            Print("Prot. Level 1 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         }
      }
   }

if (Time_Opened_Protection == true)
   {
   for (int j = 0; j < OrdersTotal(); j++)
      {
      OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if ((OrderType() == OP_BUY) && (OrderMagicNumber() == Magic))
         {
         if ((TimeCurrent() - OrderOpenTime() > Begin1 * 60) && (TimeCurrent() - OrderOpenTime() < Begin2 * 60) && (Bid >= OrderOpenPrice() + Pips1 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage_close,Orange);
            Print("Prot. Level 1 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin2 * 60) && (TimeCurrent() - OrderOpenTime() < Begin3 * 60) && (Bid >= OrderOpenPrice() + Pips2 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage_close,Orange);
            Print("Prot. Level 2 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin3 * 60) && (TimeCurrent() - OrderOpenTime() < Begin4 * 60) && (Bid >= OrderOpenPrice() - Pips3 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage_close,Orange);
            Print("Prot. Level 3 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin4 * 60) && (TimeCurrent() - OrderOpenTime() < Begin5 * 60) && (Bid >= OrderOpenPrice() - Pips4 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage_close,Orange);
            Print("Prot. Level 4 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin5 * 60) && (Bid >= OrderOpenPrice() - Pips5 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage_close,Orange);
            Print("Prot. Level 5 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         }
      if ((OrderType() == OP_SELL) && (OrderMagicNumber() == Magic))
         {
         if ((TimeCurrent() - OrderOpenTime() > Begin1 * 60) && (TimeCurrent() - OrderOpenTime() < Begin2 * 60) && (Ask <= OrderOpenPrice() - Pips1 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage_close,Orange);
            Print("Prot. Level 1 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin2 * 60) && (TimeCurrent() - OrderOpenTime() < Begin3 * 60) && (Ask <= OrderOpenPrice() - Pips2 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage_close,Orange);
            Print("Prot. Level 2 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin3 * 60) && (TimeCurrent() - OrderOpenTime() < Begin4 * 60) && (Ask <= OrderOpenPrice() + Pips3 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage_close,Orange);
            Print("Prot. Level 3 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin4 * 60) && (TimeCurrent() - OrderOpenTime() < Begin5 * 60) && (Ask <= OrderOpenPrice() + Pips4 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage_close,Orange);
            Print("Prot. Level 4 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         if ((TimeCurrent() - OrderOpenTime() > Begin5 * 60) && (Ask <= OrderOpenPrice() + Pips5 * Point))
            {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage_close,Orange);
            Print("Prot. Level 5 - Close Price :" + DoubleToStr(OrderClosePrice(),4) + " Lots : " + DoubleToStr(OrderLots(),2) + " Order Number : " + DoubleToStr(OrderTicket(),0) + " Profit : $ " + DoubleToStr(OrderProfit(),2));
            }
         }
      }
   }

if (Volatility_Filter == true)
   {
   if (iOpen(Symbol(),PERIOD_M5,0) >= Ask + sec) return;
   if (iOpen(Symbol(),PERIOD_M5,0) <= Bid - sec) return;
   if (iOpen(Symbol(),PERIOD_M5,1) >= Ask + se2c) return;
   if (iOpen(Symbol(),PERIOD_M5,1) <= Bid - se2c) return;
   if (iOpen(Symbol(),PERIOD_M5,2) >= Ask + se3c) return;
   if (iOpen(Symbol(),PERIOD_M5,2) <= Bid - se3c) return;
   }

if (Ask - Bid > MaxSpread * 0.0001) return(0);

MinLot = MarketInfo(Symbol(),MODE_MINLOT);
MaxLot = MarketInfo(Symbol(),MODE_MAXLOT);
Leverage = AccountLeverage();
LotSize = MarketInfo(Symbol(),MODE_LOTSIZE);
LT = MathMin(MaxLot,MathMax(MinLot,Lots));

if (LotsOptimized && (Risk > 0.0))
   {
   LT = MathMax(MinLot,MathMin(MaxLot,NormalizeDouble(Risk / 100.0 * AccountFreeMargin() / MinLot / (LotSize / 100),0) * MinLot));
   }

LT = MathMax(MinLot,MathMin(MaxLot,NormalizeDouble(LT / MinLot,0) * MinLot));
LT = NormalizeDouble(LT,1);

if (AccountFreeMargin() < Ask * LT * LotSize / Leverage)
   {
   Print("Low Account Balance. Lots = ",LT," , Free Margin = ",AccountFreeMargin());
   Comment("Low Account Balance. Lots = ",LT," , Free Margin = ",AccountFreeMargin());
   return;
   }

if (LT > Maximum_Lots) LT = Maximum_Lots;

double rsi1 = iRSI(NULL,TF,RSI_period,PRICE_CLOSE,0);
double rsi2 = iRSI(NULL,PERIOD_M1,20,PRICE_CLOSE,0);
double sma1 = iMA(NULL,0,SMA_period,0,MODE_SMA,PRICE_MEDIAN,1);
double sma2 = iMA(NULL,0,SMA_period,0,MODE_SMA,PRICE_MEDIAN,1);

if (IsTradeTime())
   {
   if ((DayOfWeek() == 0) || (DayOfWeek() == 5)) return(0);
   if ((DayOfWeek() == 1) && (Hour() < Hour_Start_Trade)) return(0);
   if (!ExistPosition() && ((rsi1 < MinRSI1) || (rsi2 < 36)) && (sma1 >= Ask + 0.0002))
      {
      if (NeedSendBuy)
         {
         tp = 0;
         if (!Virtual_TP) tp = Ask + TakeProfit * Point;
         OrderSend(Symbol(),OP_BUY,LT,NormalizeDouble(Ask,Digits_r),slippage,NormalizeDouble(Ask - StopLoss * Point,Digits_r),NormalizeDouble(tp,Digits_r),"BankingFX-Diamond",Magic,0,Blue);
         NeedSendBuy = false;
         NeedSendSell = true;
         }
      }
   if (!ExistPosition() && ((rsi1 > MaxRSI1) || (rsi2 > 64)) && (sma2 <= Bid - 0.0002))
      {
      if (NeedSendSell)
         {
         tp = 0;
         if (!Virtual_TP) tp = Bid - TakeProfit * Point;
         OrderSend(Symbol(),OP_SELL,LT,NormalizeDouble(Bid,Digits_r),slippage,NormalizeDouble(Bid + StopLoss * Point,Digits_r),NormalizeDouble(tp,Digits_r),"BankingFX-Diamond",Magic,0,Red);
         NeedSendSell = false;
         NeedSendBuy = true;
         }
      }
   if ((rsi1 < MaxRSI2) && (rsi1 > MinRSI2))
      {
      NeedSendSell = true;
      NeedSendBuy = true;
      }
   }
}

//+------------------------------------------------------------------+

bool IsTradeTime()
{
if ((Hour_Start_Trade < Hour_Stop_Trade) && ((TimeHour(TimeCurrent()) < Hour_Start_Trade) || (TimeHour(TimeCurrent()) >= Hour_Stop_Trade))) return(false);
if ((Hour_Start_Trade > Hour_Stop_Trade) && ((TimeHour(TimeCurrent()) < Hour_Start_Trade) && (TimeHour(TimeCurrent()) >= Hour_Stop_Trade))) return(false);
if (Hour_Stop_Trade == 0) Hour_Stop_Trade = 24;
if ((Hour() == Hour_Stop_Trade - 1) && (Minute() >= LastMinute)) return(false);
return(true);
}

//+------------------------------------------------------------------+

bool ExistPosition()
{
bool result = false;

for (int i = 0; i < OrdersTotal(); i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == Magic))
         {
         if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
            {
            result = true;
            break;
            }
         }
      }
   }
return(result);
}





//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

//--------------------------------------------------------------------
int Orders_Total_Buy_Sell()
{
int i,count;

Total_Buy=0;
Total_Sell=0;
Total_Pending_Buy=0;
Total_Pending_Sell=0;
Orders_Profit=0;
Min_Price_Buy=-1;
Max_Price_Buy=0;
Min_Price_Sell=-1;
Max_Price_Sell=0;
Lots_Max_Buy=0;
Lots_Max_Sell=0;
SL_Price_Buy=0;
TP_Price_Buy=0;
SL_Price_Sell=0;
TP_Price_Sell=0;

for(i=0;i<OrdersTotal();i++)
   {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      {
      if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
      count++;
      
      if(OrderType()==0)
         {
         if(Min_Price_Buy<0 || OrderOpenPrice()<Min_Price_Buy) Min_Price_Buy=OrderOpenPrice();
         if(OrderOpenPrice()>Max_Price_Buy) Max_Price_Buy=OrderOpenPrice();
         if(OrderLots()>Lots_Max_Buy) Lots_Max_Buy=OrderLots();
         SL_Price_Buy=OrderStopLoss();
         TP_Price_Buy=OrderTakeProfit();
         }
      
      if(OrderType()==1)
         {
         if(Min_Price_Sell<0 || OrderOpenPrice()<Min_Price_Sell) Min_Price_Sell=OrderOpenPrice();
         if(OrderOpenPrice()>Max_Price_Sell) Max_Price_Sell=OrderOpenPrice();
         if(OrderLots()>Lots_Max_Sell) Lots_Max_Sell=OrderLots();
         SL_Price_Sell=OrderStopLoss();
         TP_Price_Sell=OrderTakeProfit();
         }
      
      if(OrderType()<2) Orders_Profit += OrderProfit();
      
      if(OrderType()==2 || OrderType()==4) Total_Pending_Buy++;
      if(OrderType()==3 || OrderType()==5) Total_Pending_Sell++;
      
      if(OrderType()==0 || OrderType()==2 || OrderType()==4) Total_Buy++;
      if(OrderType()==1 || OrderType()==3 || OrderType()==5) Total_Sell++;
      }
   }
return(count);
}
//--------------------------------------------------------------------



//--------------------------------------------------------------------
int Open_Order(int TYPE, double LOTS, double PRICE, double SL_Prise, double TP_Prise, int SLIPPAGE = 0)
{
int ticket,Slippage_r,StopLevel;
double lots_r,sl_r,tp_r;
color color_r;
string Type_str;

// сбрасываем счетчики ошибок
Error_4_count=0;
Error_6_count=0;
Error_129_count=0;
Error_135_count=0;
Error_136_count=0;
Error_137_count=0;
Error_138_count=0;
Error_146_count=0;
//----------------

PRICE=NormalizeDouble(PRICE,Digits);

if(TYPE==0) { Type_str="open Buy - "; color_r=Blue; }
if(TYPE==1) { Type_str="open Sell - "; color_r=Red; }
if(TYPE==2) { Type_str="open Buy_Limit - "; color_r=DodgerBlue; }
if(TYPE==4) { Type_str="open Buy_Stop - "; color_r=DodgerBlue; }
if(TYPE==3) { Type_str="open Sell_Limit - "; color_r=Salmon; }
if(TYPE==5) { Type_str="open Sell_Stop - "; color_r=Salmon; }

StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL);

if(TYPE==0)
   {
   if(Bid-SL_Prise<StopLevel*Point) return(0);
   if(TP_Prise-Bid<StopLevel*Point) return(0);
   }
if(TYPE==1)
   {
   if(SL_Prise-Ask<StopLevel*Point) return(0);
   if(Ask-TP_Prise<StopLevel*Point) return(0);
   }

sl_r=NormalizeDouble(SL_Prise,Digits);
tp_r=NormalizeDouble(TP_Prise,Digits);

lots_r=NormalizeDouble(LOTS,2);

Slippage_r=SLIPPAGE;
if(TYPE>1) Slippage_r=0;

if(Comments) ObjectSetText("Comment","Отправляем приказ..",10,"Arial",Lime);
if(Comments) WindowRedraw();
TimerComment=TimeLocal();

while(true)
   {
   ticket=OrderSend(Symbol(),TYPE,lots_r,NormalizeDouble(PRICE,Digits_r),Slippage_r,NormalizeDouble(sl_r,Digits_r),NormalizeDouble(tp_r,Digits_r),0,Magic,0,color_r);
   if(ticket>0)
      {
      if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
      if(Comments) WindowRedraw();
      if(Sounds) PlaySound("ok.wav");
      TimerComment=TimeLocal();
      break;
      }
   if(Fun_Error(GetLastError())==1) continue;
   break;
   }
return(ticket);
}
//--------------------------------------------------------------------



//--------------------------------------------------------------------
void Close_Order()
{
bool cl;
int i;
bool Close_Buy,Close_Sell;

//---------------------------------------
Close_Buy=true;
Close_Sell=true;
//---------------------------------------

for(i=0;i<OrdersTotal();i++)
   {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      {
      if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
      
      RefreshRates();
      
      if(OrderType()==OP_BUY)
         {
         if(Close_Buy)
            {
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            TimerComment=TimeLocal();
            
            // сбрасываем счетчики ошибок
            Error_4_count=0;
            Error_6_count=0;
            Error_129_count=0;
            Error_135_count=0;
            Error_136_count=0;
            Error_137_count=0;
            Error_138_count=0;
            Error_146_count=0;
            //----------------
            
            while(true)
               {
               cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits_r),slippage*2,Goldenrod);
               if(cl)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  i--;
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            }
         }
      if(OrderType()==OP_SELL)
         {
         if(Close_Sell)
            {
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            TimerComment=TimeLocal();
            
            // сбрасываем счетчики ошибок
            Error_4_count=0;
            Error_6_count=0;
            Error_129_count=0;
            Error_135_count=0;
            Error_136_count=0;
            Error_137_count=0;
            Error_138_count=0;
            Error_146_count=0;
            //----------------
            
            while(true)
               {
               cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits_r),slippage*2,Goldenrod);
               if(cl)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  i--;
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            }
         }
      }
   }
}
//--------------------------------------------------------------------



//--------------------------------------------------------------------
void Delete_Order()
{
bool dl;
int i;
bool Delete_Buy,Delete_Sell;

//---------------------------------------
Delete_Buy=true;
Delete_Sell=true;
//---------------------------------------

for(i=0;i<OrdersTotal();i++)
   {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      {
      if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
      
      if(OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)
         {
         if(Delete_Buy)
            {
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            TimerComment=TimeLocal();
            
            // сбрасываем счетчики ошибок
            Error_4_count=0;
            Error_6_count=0;
            Error_129_count=0;
            Error_135_count=0;
            Error_136_count=0;
            Error_137_count=0;
            Error_138_count=0;
            Error_146_count=0;
            //----------------
            
            while(true)
               {
               dl=OrderDelete(OrderTicket(),Black);
               if(dl)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  i--;
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            }
         }
      if(OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
         {
         if(Delete_Sell)
            {
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            TimerComment=TimeLocal();
            
            // сбрасываем счетчики ошибок
            Error_4_count=0;
            Error_6_count=0;
            Error_129_count=0;
            Error_135_count=0;
            Error_136_count=0;
            Error_137_count=0;
            Error_138_count=0;
            Error_146_count=0;
            //----------------
            
            while(true)
               {
               dl=OrderDelete(OrderTicket(),Black);
               if(dl)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  i--;
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            }
         }
      }
   }
}
//--------------------------------------------------------------------



//-------------------------------------------------------------------------------------------------------------------
void TrailingOrder()
{
//----
int    cmd,i,StopLevel;
double OpenPricePending;
bool Mod;
//----

for(i=0;i<OrdersTotal();i++)
   {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      {
      if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
      
      OpenPricePending=OrderOpenPrice();
      cmd=OrderType();
      
      RefreshRates();
      
      StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
      
      // сбрасываем счетчики ошибок
      Error_4_count=0;
      Error_6_count=0;
      Error_129_count=0;
      Error_135_count=0;
      Error_136_count=0;
      Error_137_count=0;
      Error_138_count=0;
      Error_146_count=0;
      //----------------
      
      if(cmd==OP_BUYSTOP)
         {
         if(OpenPricePending-Ask>=(DistanceOpeningPips+StepOpeningPips)*Point)
            {
            OpenPricePending=Ask+DistanceOpeningPips*Point;
            OpenPricePending=NormalizeDouble(OpenPricePending,Digits);
            if(OpenPricePending-Ask<=StopLevel*Point) continue;
            
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            while(true)
               {
               Mod=OrderModify(OrderTicket(),NormalizeDouble(OpenPricePending,Digits_r),OrderStopLoss(),OrderTakeProfit(),0,DodgerBlue);
               if(Mod == true)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            continue;
            }
         }
      if(cmd==OP_SELLSTOP)
         {
         if(Bid-OpenPricePending>=(DistanceOpeningPips+StepOpeningPips)*Point)
            {
            OpenPricePending=Bid-DistanceOpeningPips*Point;
            OpenPricePending=NormalizeDouble(OpenPricePending,Digits);
            if(Bid-OpenPricePending<=StopLevel*Point) continue;
            
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            while(true)
               {
               Mod=OrderModify(OrderTicket(),NormalizeDouble(OpenPricePending,Digits_r),OrderStopLoss(),OrderTakeProfit(),0,Salmon);
               if(Mod == true)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            }
         }
      }
   }
}
//-------------------------------------------------------------------------------------------------------------------



//-------------------------------------------------------------------------------------------------------------------
void TrailingStop()
{
//----
int    cmd,i,StopLevel,StartTrailingPercent_r;
double SLossPriceM;
bool Mod;
//----

for(i=0;i<OrdersTotal();i++)
   {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      {
      if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
      
      SLossPriceM=OrderStopLoss();
      cmd=OrderType();
      
      if(cmd==0)
         {
         if(Total_Buy<2 || Total_Pending_Buy>0) continue;
         if(Total_Buy==2) StartTrailingPercent_r=StartTrailingPercent_2;
         if(Total_Buy==3) StartTrailingPercent_r=StartTrailingPercent_3;
         if(Total_Buy==4) StartTrailingPercent_r=StartTrailingPercent_4;
         if(Total_Buy==5) StartTrailingPercent_r=StartTrailingPercent_5;
         if(Total_Buy==6) StartTrailingPercent_r=StartTrailingPercent_6;
         }
      if(cmd==1)
         {
         if(Total_Sell<2 || Total_Pending_Sell>0) continue;
         if(Total_Sell==2) StartTrailingPercent_r=StartTrailingPercent_2;
         if(Total_Sell==3) StartTrailingPercent_r=StartTrailingPercent_3;
         if(Total_Sell==4) StartTrailingPercent_r=StartTrailingPercent_4;
         if(Total_Sell==5) StartTrailingPercent_r=StartTrailingPercent_5;
         if(Total_Sell==6) StartTrailingPercent_r=StartTrailingPercent_6;
         }
      
      StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
      
      // сбрасываем счетчики ошибок
      Error_4_count=0;
      Error_6_count=0;
      Error_129_count=0;
      Error_135_count=0;
      Error_136_count=0;
      Error_137_count=0;
      Error_138_count=0;
      Error_146_count=0;
      //----------------
      
      if(cmd==OP_BUY)
         {
         if((SLossPriceM<Min_Price_Buy && Bid>=Min_Price_Buy+(Max_Price_Buy-Min_Price_Buy)*StartTrailingPercent_r*0.01) || (SLossPriceM>=Min_Price_Buy && Bid-SLossPriceM>=(DistanceStopLossPips+StepStopLossPips)*Point))
            {
            SLossPriceM=Bid-DistanceStopLossPips*Point;
            SLossPriceM=NormalizeDouble(SLossPriceM,Digits);
            if(Bid-SLossPriceM<=StopLevel*Point) continue;
            
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            while(true)
               {
               Mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLossPriceM,Digits_r),OrderTakeProfit(),0);
               if(Mod == true)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            continue;
            }
         }
      if(cmd==OP_SELL)
         {
         if((SLossPriceM>Max_Price_Sell && Ask<=Max_Price_Sell-(Max_Price_Sell-Min_Price_Sell)*StartTrailingPercent_r*0.01) || (SLossPriceM<=Max_Price_Sell && SLossPriceM-Ask>=(DistanceStopLossPips+StepStopLossPips)*Point))
            {
            SLossPriceM=Ask+DistanceStopLossPips*Point;
            SLossPriceM=NormalizeDouble(SLossPriceM,Digits);
            if(SLossPriceM-Ask<=StopLevel*Point) continue;
            
            if(Comments) ObjectSetText("Comment","Отправляем приказ...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            while(true)
               {
               Mod=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLossPriceM,Digits_r),OrderTakeProfit(),0);
               if(Mod == true)
                  {
                  if(Comments) ObjectSetText("Comment","Приказ успешно исполнен.",10,"Arial",Lime);
                  if(Comments) WindowRedraw();
                  if(Sounds) PlaySound("ok.wav");
                  TimerComment=TimeLocal();
                  break;
                  }
               if (Fun_Error(GetLastError())==1) continue;
               break;
               }
            }
         }
      }
   }
}
//-------------------------------------------------------------------------------------------------------------------



//--------------------------------------------------------------------
int Fun_Error(int Error)
  {
int c1;

TimerComment=TimeLocal();
   
   switch(Error)
     {
                             // Преодолимые ошибки
                             
      case  4:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Торговый сервер занят. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_4_count>=3) return(0);
         Error_4_count++;
         Sleep(5000);
         RefreshRates();
         return(1);
      case  6:
         if(!IsConnected())
            {
            if(Comments) ObjectSetText("Comment","ВНИМАНИЕ! Связь с сервером отсутствует или прервана.",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            Alert("ВНИМАНИЕ! Связь с сервером отсутствует или прервана.");
            return(0);
            }
         else
            {
            if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Неустойчивая связь с торговым сервером. Пробуем ещё раз...",10,"Arial",Lime);
            if(Comments) WindowRedraw();
            if(Error_6_count>=3) return(0);
            Error_6_count++;
            Sleep(5000);
            RefreshRates();
            return(1);
            }
      case 129:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Неправильная цена bid или ask. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_129_count>=1) return(0);
         Error_129_count++;
         Sleep(5000);
         RefreshRates();
         return(1);
      case 135:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Цена изменилась. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_135_count>=10) return(0);
         Error_135_count++;
         RefreshRates();
         return(1);
      case 136:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Нет цен. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_136_count>=10) return(0);
         Error_136_count++;
         while(RefreshRates()==false)
            {
            Sleep(100);
            c1++;
            if(c1>60) break;
            }
         return(1);
      case 137:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Брокер занят. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_137_count>=3) return(0);
         Error_137_count++;
         Sleep(3000);
         RefreshRates();
         return(1);
      case 138:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Запрошенная цена устарела. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_138_count>=3) return(0);
         Error_138_count++;
         RefreshRates();
         return(1);
      case 146:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Подсистема торговли занята. Пробуем ещё раз...",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         if(Error_146_count>=120) return(0);
         Error_146_count++;
         Sleep(1000);
         RefreshRates();
         return(1);
         
                             // Критические ошибки
      
      case  2:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Общая ошибка.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      case  5:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Старая версия терминала.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      case 64:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Счет заблокирован.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      case 128:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Истек срок ожидания совершения сделки.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         Sleep(5000);
         RefreshRates();
         return(0);
      case 130:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Неправильные стопы.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      case 133:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Торговля запрещена.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      case 134:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Недостаточно денег для совершения операции.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      case 147:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error+" - Использование даты истечения ордера запрещено брокером.",10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
      default:
         if(Comments) ObjectSetText("Comment","Ошибка "+Error,10,"Arial",Lime);
         if(Comments) WindowRedraw();
         return(0);
     }
  }
//--------------------------------------------------------------------

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

