Author: Copyright 2019, SEM
Price Data Components
Series array that contains close prices for each barSeries array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains open prices of each barSeries array that contains open time of each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reached
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
S-v1
ÿþ//+------------------------------------------------------------------+

//|    ProjectName |

//|   Copyright 2018, CompanyName |

//|    http://www.companyname.net |

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



#property copyright "Copyright 2019, SEM"

#property link "https://www.mql5.com"

#property version "1.00"



//#include <Trade\OrderInfo.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>



CPositionInfo m_position; // trade position object

//COrderInfo m_order;

CTrade m_trade; // trading object

CSymbolInfo    m_symbol;



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

//|                                                                  |

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

double _SYMBOL_POINT_ = SymbolInfoDouble(Symbol(),SYMBOL_POINT);

int _SYMBOL_DIGITS_ = (int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);



int EXPERT_MAGIC=10101; // magic number

ENUM_TIMEFRAMES _period=PERIOD_CURRENT;//period





input  int TrailingStop_STOPS_LEVELBreakeven=10;



input  int TrailingStop_STOPS_LEVEL=35; //Trailing Stop LEVEL

input  int TakeProfit=100;//Profit

//int limit_total=190;

input  int limit_total_symbol=190; //8<8B ?>78F89

input double MaximumRisk   = 0.01;    // Maximum Risk in percentage

input double DecreaseFactor     = 3;  // Descrease factor



string _name[]= {"label"

                 ,"BUY_Lot","BUY_Close_Profit","BUY_Close_Loss","BUY_Close_All","BUY_BezUbutok","BUY_TakeProfit","BUY_Trailing"

                 ,"SELL_Lot","SELL_Close_Profit","SELL_Close_Loss","SELL_Close_All","SELL_BezUbutok","SELL_TakeProfit","SELL_Trailing"

                 ,"BuyStop","BuyLimit","SellStop","SellLimit","BuySellStop","BuySellLimit","StopLimit_Close"

                 ,"Trailing"

                };

string _nameObect = Symbol()+string(EXPERT_MAGIC);











double _CloseTik=0.00;

double _Close_1=0.00;

double _Close_2=0.00;



double _OpenTik=0.00;

double _Open_1=0.00;

double _Open_2=0.00;

double ___F_profit___B=0.0;

double ___F_profit___S=0.0;

//double m_adjusted_point;    // point value adjusted for 3 or 5 points







input ushort                     InpGep        = 5;             //BABC? >B B5:CI59 F5=K 4;O ?5@2>3> >B;>65==>3> >@45@0;

input ushort                     InpStep       = 1;             //(03 <564C >B;>65==K<8 >@45@0<8;

input uchar                      InpQuantity   = 5;              //>;8G5AB2> 2KAB02;O5<KE >@45@>2;



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

//|                                                                  |

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

input ushort                     InpStopLoss       = 0;             // Stop Loss (in pips)

input ushort                     InpTakeProfit     = 0;             // Take Profit (in pips)

ulong                            m_slippage=30;                      // slippage

//double                           m_adjusted_pointStop;                   // point value adjusted for 3 or 5 points

double                           ExtGep=0.0;

double                           ExtStep=0.0;

double                           ExtStopLoss=0.0;

double                           ExtTakeProfit=0.0;



enum ENUM_PENDING_ORDERS

  {

   buy_limit         =0,   // Buy Limit

   buy_stop          =1,   // Buy Stop

   sell_limit        =2,    // Sell Limit

   sell_stop         =3    // Sell Stop

  };



input uint RTOTAL=4;        // '8A;> ?>2B>@>2 ?@8 =5C40G=KE A45;:0E

//input uint SLEEPTIME=1;     // @5<O ?0C7K <564C ?>2B>@0<8 2 A5:C=40E

input uint  Deviation_=10;  // B:;>=5=85 F5=K

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

//| Expert initialization function |

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

int OnInit()

  {



   EventSetTimer(1); // :>;8G5AB2> A5:C=4

   ButtonCreateFor();







   _CloseTik=iClose(NULL,_period,0);

   _Close_1=iClose(NULL,_period, 1);

   _Close_2=iClose(NULL,_period, 2);



   _OpenTik=iOpen(NULL,_period,0);

   _Open_1=iOpen(NULL,_period, 1);

   _Open_2=iOpen(NULL,_period, 2);



   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function |

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

void OnDeinit(const int reason)

  {

   for(int i=0; i<ArraySize(_name); i++)

     {

      ObjectDelete(0,_nameObect+_name[i]);

     };

   EventKillTimer();

  }







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

//| |

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

void OnTick()

  {



//ObjectGetInteger(0,"BUY",OBJPROP_STATE,0);



   _CloseTik=iClose(NULL,_period,0);

   _Close_1=iClose(NULL,_period, 1);

   _Close_2=iClose(NULL,_period, 2);



   _OpenTik=iOpen(NULL,_period,0);

   _Open_1=iOpen(NULL,_period, 1);

   _Open_2=iOpen(NULL,_period, 2);





   _BUY();

   _SELL();











//double PROFIT=0.1;



//   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//   if(total>0)

//     {

//      ulong position_ticket=PositionGetTicket(total-1); // B8:5B ?>78F88

//      //PROFIT=PositionGetDouble(POSITION_PROFIT); // >1J5< ?>78F88

//

//     };



//   if(ObjectGetInteger(0,_nameObect+"Trailing_BUY",OBJPROP_STATE,0)==true)

//     {_Trailing_BUY();};

//   if(ObjectGetInteger(0,_nameObect+"Trailing_SELL",OBJPROP_STATE,0)==true)

//     {_Trailing_SELL();};

//   if(ObjectGetInteger(0,_nameObect+"Trailing",OBJPROP_STATE,0)==true)

//     {_Trailing();};

//

//

//   if(ObjectGetInteger(0,_nameObect+"BezUbutok_BUY",OBJPROP_STATE,0)==true)

//     {_BTrailing_BUY();};

//   if(ObjectGetInteger(0,_nameObect+"BezUbutok_SELL",OBJPROP_STATE,0)==true)

//     {_BTrailing_SELL();};

//

//

//   if(ObjectGetInteger(0,_nameObect+"TakeProfit_BUY",OBJPROP_STATE,0)==true)

//     {_TakeProfit_BUY();};

//   if(ObjectGetInteger(0,_nameObect+"TakeProfit_SELL",OBJPROP_STATE,0)==true)

//     {_TakeProfit_SELL();};





//   if(total<limit_total_symbol)// :>;8G5AB2> >B:@KBKE ?>78F89

//     {

//      //if(PROFIT>0)

//        {

//         //if(_Open_2<_Close_2)

//           {

//            //if(_Open_1<_Close_1)

//              {

//               if(_OpenTik<_CloseTik)

//                 {

//                  if(F_profit(POSITION_TYPE_BUY)>0.00)

//                    {

//                     if(ObjectGetInteger(0,_nameObect+"BUY",OBJPROP_STATE,0)==true)

//                       {

//

//                        OpenBuy(TradeSizeOptimized());

//                       };

//

//                     if((F_profit(POSITION_TYPE_BUY)-___F_profit___B)>0)

//                       {

//                        //_Trailing_BUY();

//

//                        ___F_profit___B=F_profit(POSITION_TYPE_BUY);

//                       };

//

//                    };

//                 };

//              };

//           };

//         //if(_Open_2>_Close_2)

//           {

//            //if(_Open_1>_Close_1)

//              {

//               if(_OpenTik>_CloseTik)

//                 {

//                  if(F_profit(POSITION_TYPE_SELL)>0.00)

//                    {

//                     if(ObjectGetInteger(0,_nameObect+"SELL",OBJPROP_STATE,0)==true)

//                       {

//

//                        OpenSell(TradeSizeOptimized());

//                       };

//

//                     if((F_profit(POSITION_TYPE_SELL)-___F_profit___S)>0)

//                       {

//                        //_Trailing_SELL();

//

//                        ___F_profit___S=F_profit(POSITION_TYPE_SELL);

//                       };

//                    };

//                 };

//              };

//           };

//        };

//

//      //_Trailing();

//      //_profit_();

//     };

   return;

  }



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

//|                                                                  |

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

void _BUY()

  {

//if(_Open_2<_Close_2)

     {

      //if(_Open_1<_Close_1)

        {

         if(_OpenTik<_CloseTik)

           {

            if(F_profit(POSITION_TYPE_BUY)>0.00)

              {

               if(ObjectGetInteger(0,_nameObect+"BUY_Lot",OBJPROP_STATE,0)==true)

                 {

                  OpenBuy(TradeSizeOptimized());

                  if((F_profit(POSITION_TYPE_BUY)-___F_profit___B)>0)

                    {

                     ___F_profit___B=F_profit(POSITION_TYPE_BUY);

                    };

                 };

              };

           };

        };

     };

  }



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

//|                                                                  |

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

void _SELL()

  {

//if(_Open_2>_Close_2)

     {

      //if(_Open_1>_Close_1)

        {

         if(_OpenTik>_CloseTik)

           {

            if(F_profit(POSITION_TYPE_SELL)>0.00)

              {

               if(ObjectGetInteger(0,_nameObect+"SELL_Lot",OBJPROP_STATE,0)==true)

                 {

                  OpenSell(TradeSizeOptimized());

                 };

               if((F_profit(POSITION_TYPE_SELL)-___F_profit___S)>0)

                 {

                  ___F_profit___S=F_profit(POSITION_TYPE_SELL);

                 };

              };

           };

        };

     };

   return;

  }

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

//|                                                                  |

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

bool _BUTTON()

  {

//5>1E>48<K5 ?5@5<5==K5

   double BUY_Lot=0.00;

   double BUY_Close_Profit=0.00;

   double BUY_Close_Loss=0.00;

   double BUY_Close_All=0.00;

   int BUY_BezUbutok=0;

   int BUY_TakeProfit=0;

   int BUY_Trailing=0;

   double SELL_Lot=0.00;

   double SELL_Close_Profit=0.00;

   double SELL_Close_Loss=0.00;

   double SELL_Close_All=0.00;

   int SELL_BezUbutok=0;

   int SELL_TakeProfit=0;

   int SELL_Trailing=0;

   double BuyStop=0.00;

   double BuyLimit=0.00;

   double SellStop=0.00;

   double SellLimit=0.00;

   int BuySellStop=0;

   int BuySellLimit=0;

   int StopLimit_Close=0;



   int Trailing =0;



//5>1E>48<K5 ?5@5<5==K5

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

//|                                                                  |

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

// 0AG5B 40==KE 4;O >B>1@065=8O

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

     {

      if(PositionGetSymbol(i)==Symbol())

        {

         Trailing+=1;

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            if(m_position.StopLoss() > m_position.PriceOpen())

              {

               BUY_BezUbutok+=1;

              };

            if(m_position.TakeProfit() > m_position.PriceOpen() && m_position.TakeProfit() > 0)

              {

               BUY_TakeProfit+=1;

              };

            BUY_Lot+=PositionGetDouble(POSITION_VOLUME);

            BUY_Trailing+=1;

            if(PositionGetDouble(POSITION_PROFIT)>0)

              {

               BUY_Close_Profit+=PositionGetDouble(POSITION_PROFIT);

              }

            else

              {

               BUY_Close_Loss+=PositionGetDouble(POSITION_PROFIT);

              };

            BUY_Close_All+=PositionGetDouble(POSITION_PROFIT);

           }

         else

           {

            if(m_position.StopLoss() < m_position.PriceOpen() && m_position.StopLoss() > 0)

              {

               SELL_BezUbutok+=1;

              };

            if(m_position.TakeProfit() < m_position.PriceOpen() && m_position.TakeProfit() > 0)

              {

               SELL_TakeProfit+=1;

              };

            SELL_Lot+=PositionGetDouble(POSITION_VOLUME);

            SELL_Trailing+=1;

            if(PositionGetDouble(POSITION_PROFIT)>0)

              {

               SELL_Close_Profit+=PositionGetDouble(POSITION_PROFIT);

              }

            else

              {

               SELL_Close_Loss+=PositionGetDouble(POSITION_PROFIT);

              };

            SELL_Close_All+=PositionGetDouble(POSITION_PROFIT);

           };

        };

     };

//ORDER

   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of open positions

     {

      StopLimit_Close+=1;

      if(OrderGetTicket(i)>0)

        {

         if(OrderGetString(ORDER_SYMBOL)==Symbol())

           {

            int _ORDER_TYPE_=(int)OrderGetInteger(ORDER_TYPE);

            if(_ORDER_TYPE_==ORDER_TYPE_BUY_STOP)

              {

               BuyStop+=OrderGetDouble(ORDER_VOLUME_INITIAL);

               BuySellStop+=1;

              }

            else

               if(_ORDER_TYPE_==ORDER_TYPE_SELL_STOP)

                 {

                  SellStop+=OrderGetDouble(ORDER_VOLUME_INITIAL);

                  BuySellStop+=1;

                 }

               else

                  if(_ORDER_TYPE_==ORDER_TYPE_BUY_LIMIT)

                    {

                     BuyLimit+=OrderGetDouble(ORDER_VOLUME_INITIAL);

                     BuySellLimit+=1;

                    }

                  else

                     if(_ORDER_TYPE_==ORDER_TYPE_SELL_LIMIT)

                       {

                        SellLimit+=OrderGetDouble(ORDER_VOLUME_INITIAL);

                        BuySellLimit+=1;

                       };

           };

        };

     };



// 0AG5B 40==KE 4;O >B>1@065=8O

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

//|                                                                  |

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

////0720=8O :=>?>:

   ObjectSetString(0,_nameObect+"BUY_Lot",OBJPROP_TEXT,"BUY Lot ("+DoubleToString(BUY_Lot,2)+")");

   ObjectSetString(0,_nameObect+"BUY_Close_Profit",OBJPROP_TEXT,"Close Profit ("+DoubleToString(BUY_Close_Profit,2)+")");

   ObjectSetString(0,_nameObect+"BUY_Close_Loss",OBJPROP_TEXT,"Close Loss ("+DoubleToString(BUY_Close_Loss,2)+")");

   ObjectSetString(0,_nameObect+"BUY_Close_All",OBJPROP_TEXT,"Close All ("+DoubleToString(BUY_Close_All,2)+")");

   ObjectSetString(0,_nameObect+"BUY_BezUbutok",OBJPROP_TEXT,"Breakeven ("+IntegerToString(BUY_BezUbutok,0)+")");

   ObjectSetString(0,_nameObect+"BUY_TakeProfit",OBJPROP_TEXT,"TakeProfit ("+IntegerToString(BUY_TakeProfit,0)+")");

   ObjectSetString(0,_nameObect+"BUY_Trailing",OBJPROP_TEXT,"Trailing ("+IntegerToString(BUY_Trailing,0)+")");

   ObjectSetString(0,_nameObect+"SELL_Lot",OBJPROP_TEXT,"SELL Lot ("+DoubleToString(SELL_Lot,2)+")");

   ObjectSetString(0,_nameObect+"SELL_Close_Profit",OBJPROP_TEXT,"Close Profit ("+DoubleToString(SELL_Close_Profit,2)+")");

   ObjectSetString(0,_nameObect+"SELL_Close_Loss",OBJPROP_TEXT,"Close Loss ("+DoubleToString(SELL_Close_Loss,2)+")");

   ObjectSetString(0,_nameObect+"SELL_Close_All",OBJPROP_TEXT,"Close All ("+DoubleToString(SELL_Close_All,2)+")");

   ObjectSetString(0,_nameObect+"SELL_BezUbutok",OBJPROP_TEXT,"Breakeven ("+IntegerToString(SELL_BezUbutok,0)+")");

   ObjectSetString(0,_nameObect+"SELL_TakeProfit",OBJPROP_TEXT,"TakeProfit ("+IntegerToString(SELL_TakeProfit,0)+")");

   ObjectSetString(0,_nameObect+"SELL_Trailing",OBJPROP_TEXT,"Trailing ("+IntegerToString(SELL_Trailing,0)+")");

   ObjectSetString(0,_nameObect+"BuyStop",OBJPROP_TEXT,"BuyStop ("+DoubleToString(BuyStop,2)+")");

   ObjectSetString(0,_nameObect+"BuyLimit",OBJPROP_TEXT,"BuyLimit ("+DoubleToString(BuyLimit,2)+")");

   ObjectSetString(0,_nameObect+"SellStop",OBJPROP_TEXT,"SellStop ("+DoubleToString(SellStop,2)+")");

   ObjectSetString(0,_nameObect+"SellLimit",OBJPROP_TEXT,"SellLimit ("+DoubleToString(SellLimit,2)+")");

   ObjectSetString(0,_nameObect+"BuySellStop",OBJPROP_TEXT,"BuySellStop ("+IntegerToString(BuySellStop,0)+")");

   ObjectSetString(0,_nameObect+"BuySellLimit",OBJPROP_TEXT,"BuySellLimit ("+IntegerToString(BuySellLimit,0)+")");

   ObjectSetString(0,_nameObect+"StopLimit_Close",OBJPROP_TEXT,"StopLimit Close ("+IntegerToString(StopLimit_Close,0)+")");

   ObjectSetString(0,_nameObect+"Trailing",OBJPROP_TEXT,"Trailing = "+IntegerToString(Trailing,0)+", All = "+IntegerToString(PositionsTotal(),0)+".");

   ObjectSetString(0,_nameObect+"label",OBJPROP_TEXT,Symbol() + " - "+TimeToString(iTime(NULL,0,0)+Period()*60-TimeCurrent(),TIME_SECONDS));

////0720=8O :=>?>:

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

//|                                                                  |

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

////&25B :=>?>:

   if(ObjectGetInteger(0,_nameObect+"BUY_Lot",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BUY_Lot",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BUY_Lot",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(BUY_Close_Profit>0)

     {

      ObjectSetInteger(0,_nameObect+"BUY_Close_Profit",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(BUY_Close_Profit==0)

        {

         ObjectSetInteger(0,_nameObect+"BUY_Close_Profit",OBJPROP_BGCOLOR,clrGoldenrod);

        }

      else

        {

         ObjectSetInteger(0,_nameObect+"BUY_Close_Profit",OBJPROP_BGCOLOR,clrCrimson);

        };

//---

   if(BUY_Close_Loss>0)

     {

      ObjectSetInteger(0,_nameObect+"BUY_Close_Loss",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(BUY_Close_Loss==0)

        {

         ObjectSetInteger(0,_nameObect+"BUY_Close_Loss",OBJPROP_BGCOLOR,clrGoldenrod);

        }

      else

        {

         ObjectSetInteger(0,_nameObect+"BUY_Close_Loss",OBJPROP_BGCOLOR,clrCrimson);

        };

//---

   if(BUY_Close_All>0)

     {

      ObjectSetInteger(0,_nameObect+"BUY_Close_All",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(BUY_Close_All==0)

        {

         ObjectSetInteger(0,_nameObect+"BUY_Close_All",OBJPROP_BGCOLOR,clrGoldenrod);

        }

      else

        {

         ObjectSetInteger(0,_nameObect+"BUY_Close_All",OBJPROP_BGCOLOR,clrCrimson);

        };

//---

   if(ObjectGetInteger(0,_nameObect+"BUY_BezUbutok",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BUY_BezUbutok",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BUY_BezUbutok",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"BUY_TakeProfit",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BUY_TakeProfit",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BUY_TakeProfit",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"BUY_Trailing",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BUY_Trailing",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BUY_Trailing",OBJPROP_BGCOLOR,clrGoldenrod);

     };

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

//|                                                                  |

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

   if(ObjectGetInteger(0,_nameObect+"SELL_Lot",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"SELL_Lot",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"SELL_Lot",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(SELL_Close_Profit>0)

     {

      ObjectSetInteger(0,_nameObect+"SELL_Close_Profit",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(SELL_Close_Profit==0)

        {

         ObjectSetInteger(0,_nameObect+"SELL_Close_Profit",OBJPROP_BGCOLOR,clrGoldenrod);

        }

      else

        {

         ObjectSetInteger(0,_nameObect+"SELL_Close_Profit",OBJPROP_BGCOLOR,clrCrimson);

        };

//---

   if(SELL_Close_Loss>0)

     {

      ObjectSetInteger(0,_nameObect+"SELL_Close_Loss",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(SELL_Close_Loss==0)

        {

         ObjectSetInteger(0,_nameObect+"SELL_Close_Loss",OBJPROP_BGCOLOR,clrGoldenrod);

        }

      else

        {

         ObjectSetInteger(0,_nameObect+"SELL_Close_Loss",OBJPROP_BGCOLOR,clrCrimson);

        };

//---

   if(SELL_Close_All>0)

     {

      ObjectSetInteger(0,_nameObect+"SELL_Close_All",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(SELL_Close_All==0)

        {

         ObjectSetInteger(0,_nameObect+"SELL_Close_All",OBJPROP_BGCOLOR,clrGoldenrod);

        }

      else

        {

         ObjectSetInteger(0,_nameObect+"SELL_Close_All",OBJPROP_BGCOLOR,clrCrimson);

        };

//---

   if(ObjectGetInteger(0,_nameObect+"SELL_BezUbutok",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"SELL_BezUbutok",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"SELL_BezUbutok",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"SELL_TakeProfit",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"SELL_TakeProfit",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"SELL_TakeProfit",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"SELL_Trailing",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"SELL_Trailing",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"SELL_Trailing",OBJPROP_BGCOLOR,clrGoldenrod);

     };

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

//|                                                                  |

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

   if(ObjectGetInteger(0,_nameObect+"BuyStop",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BuyStop",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BuyStop",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"BuyLimit",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BuyLimit",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BuyLimit",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"SellStop",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"SellStop",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"SellStop",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"SellLimit",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"SellLimit",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"SellLimit",OBJPROP_BGCOLOR,clrGoldenrod);

     };

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

//|                                                                  |

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

   if(ObjectGetInteger(0,_nameObect+"BuySellStop",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BuySellStop",OBJPROP_BGCOLOR,clrCadetBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BuySellStop",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"BuySellLimit",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"BuySellLimit",OBJPROP_BGCOLOR,clrCadetBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"BuySellLimit",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"StopLimit_Close",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"StopLimit_Close",OBJPROP_BGCOLOR,clrDarkViolet);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"StopLimit_Close",OBJPROP_BGCOLOR,clrGoldenrod);

     };

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

//|                                                                  |

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

   if(ObjectGetInteger(0,_nameObect+"Trailing",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"Trailing",OBJPROP_BGCOLOR,clrCadetBlue);

     }

   else

     {

      ObjectSetInteger(0,_nameObect+"Trailing",OBJPROP_BGCOLOR,clrGoldenrod);

     };

//---

   if(ObjectGetInteger(0,_nameObect+"label",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,_nameObect+"label",OBJPROP_BGCOLOR,clrCadetBlue);

      ChartRedraw(0);

      ObjectSetInteger(0,_nameObect+"label",OBJPROP_STATE,false);

      _Skrinshot();

      ObjectSetInteger(0,_nameObect+"label",OBJPROP_BGCOLOR,clrGoldenrod);

     };

////&25B :=>?>:



   if(ObjectGetInteger(0,_nameObect+"BUY_Close_Profit",OBJPROP_STATE,0)==true)

     {

      CheckForCloseBuyProfit();

      ObjectSetInteger(0,_nameObect+"BUY_Close_Profit",OBJPROP_STATE,false);

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SELL_Close_Profit",OBJPROP_STATE,0)==true)

     {

      CheckForCloseSellProfit();

      ObjectSetInteger(0,_nameObect+"SELL_Close_Profit",OBJPROP_STATE,false);

     };

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

//|                                                                  |

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





   if(ObjectGetInteger(0,_nameObect+"BUY_Close_Loss",OBJPROP_STATE,0)==true)

     {

      CheckForCloseBuyLoss();

      ObjectSetInteger(0,_nameObect+"BUY_Close_Loss",OBJPROP_STATE,false);

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SELL_Close_Loss",OBJPROP_STATE,0)==true)

     {

      CheckForCloseSellLoss();

      ObjectSetInteger(0,_nameObect+"SELL_Close_Loss",OBJPROP_STATE,false);

     };

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

//|                                                                  |

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

   if(ObjectGetInteger(0,_nameObect+"BUY_Close_All",OBJPROP_STATE,0)==true)

     {

      CheckForCloseBuy();

      ObjectSetInteger(0,_nameObect+"BUY_Close_All",OBJPROP_STATE,false);

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SELL_Close_All",OBJPROP_STATE,0)==true)

     {

      CheckForCloseSell();

      ObjectSetInteger(0,_nameObect+"SELL_Close_All",OBJPROP_STATE,false);

     };



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

//|  _BTrailing_BUY                                                                |

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

   if(ObjectGetInteger(0,_nameObect+"BUY_BezUbutok",OBJPROP_STATE,0)==true)

     {

      _BTrailing_BUY();

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SELL_BezUbutok",OBJPROP_STATE,0)==true)

     {

      _BTrailing_SELL();

     };

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

//|    _TakeProfit_BUY                                                              |

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

   if(ObjectGetInteger(0,_nameObect+"BUY_TakeProfit",OBJPROP_STATE,0)==true)

     {

      _TakeProfit_BUY();

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SELL_TakeProfit",OBJPROP_STATE,0)==true)

     {

      _TakeProfit_SELL();

     };

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

//|       BUY_Trailing                                                           |

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

   if(ObjectGetInteger(0,_nameObect+"BUY_Trailing",OBJPROP_STATE,0)==true)

     {

      _Trailing_BUY();

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SELL_Trailing",OBJPROP_STATE,0)==true)

     {

      _Trailing_SELL();

     };

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

//|Stop                                                            |

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

//buy_limit         =0,   // Buy Limit

//    buy_stop          =1,   // Buy Stop

//  sell_limit        =2    // Sell Limit

//  sell_stop         =3    // Sell Stop



   if(ObjectGetInteger(0,_nameObect+"BuyStop",OBJPROP_STATE,0)==true)

     {

      BuyStopSellLimit(buy_stop);

      ObjectSetInteger(0,_nameObect+"BuyStop",OBJPROP_STATE,false);

     };

//---



   if(ObjectGetInteger(0,_nameObect+"BuyLimit",OBJPROP_STATE,0)==true)

     {

      BuyLimitSellStop(buy_limit);

      ObjectSetInteger(0,_nameObect+"BuyLimit",OBJPROP_STATE,false);

     };

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

//|                                                                  |

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



   if(ObjectGetInteger(0,_nameObect+"SellStop",OBJPROP_STATE,0)==true)

     {

      BuyLimitSellStop(sell_stop);

      ObjectSetInteger(0,_nameObect+"SellStop",OBJPROP_STATE,false);

     };

//---



   if(ObjectGetInteger(0,_nameObect+"SellLimit",OBJPROP_STATE,0)==true)

     {

      BuyStopSellLimit(sell_limit);

      ObjectSetInteger(0,_nameObect+"SellLimit",OBJPROP_STATE,false);

     };

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

//|                                                                  |

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



   if(ObjectGetInteger(0,_nameObect+"BuySellStop",OBJPROP_STATE,0)==true)

     {

      BuyStopSellLimit(buy_stop);

      BuyLimitSellStop(sell_stop);

      ObjectSetInteger(0,_nameObect+"BuySellStop",OBJPROP_STATE,false);

     };

//---



   if(ObjectGetInteger(0,_nameObect+"BuySellLimit",OBJPROP_STATE,0)==true)

     {

      BuyLimitSellStop(buy_limit);

      BuyStopSellLimit(sell_limit);

      ObjectSetInteger(0,_nameObect+"BuySellLimit",OBJPROP_STATE,false);

     };

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

//|                                                                  |

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



   if(ObjectGetInteger(0,_nameObect+"StopLimit_Close",OBJPROP_STATE,0)==true)

     {

      StopLimit_Close_F();

      ObjectSetInteger(0,_nameObect+"StopLimit_Close",OBJPROP_STATE,false);

     };

//---

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

//|                                                                  |

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



   if(ObjectGetInteger(0,_nameObect+"Trailing",OBJPROP_STATE,0)==true)

     {

      _Trailing();

     };

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

//|                                                                  |

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



   return true;

  }

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

//|                                                                  |

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

void OnTimer()

  {

   _BUTTON();

   ChartRedraw(0);

  }

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

//|                                                                  |

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

void StopLimit_Close_F()

  {

//----

   for(uint count=0; count<=RTOTAL && !IsStopped(); count++)

     {

      //---- 70:@K205< 2A5 2KAB02;5==K5 >@45@0 ?> B5:CI5<C A8<2>;C

      int total=OrdersTotal();

      if(!total)

         return  ; // 2A5 >@45@0 70:@KBK

      for(int pos=total-1; pos>=0; pos--)

        {

         ulong ticket=ulong(OrderGetTicket(pos));

         if(!OrderSelect(ticket))

            continue;

         string symbol=OrderGetString(ORDER_SYMBOL);

         if(symbol!=Symbol())

            continue;

         bool _Close=true;

         OrderCloseByTicket(_Close,symbol,ticket,Deviation_);

        }

      if(!OrdersTotal())

         break;

      Sleep(100);

     }

   return  ;

  }

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

//|                                                                  |

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



bool OrderCloseByTicket

(

   bool &Signal,         // D;03 @07@5H5=8O =0 A45;:C

   const string symbol,  // B>@3>20O ?0@0 A45;:8

   ulong ticket,         // B8:5B

   uint deviation        // A;8??06

)

  {

//----

   if(!Signal)

      return(true);



//---- 1JO2;5=85 AB@C:BC@ B>@3>2>3> 70?@>A0 8 @57C;LB0B0 B>@3>2>3> 70?@>A0

   MqlTradeRequest request;

   MqlTradeResult result;

//---- 1JO2;5=85 AB@C:BC@K @57C;LB0B0 ?@>25@:8 B>@3>2>3> 70?@>A0

   MqlTradeCheckResult check;



//---- >1=C;5=85 AB@C:BC@

   ZeroMemory(request);

   ZeroMemory(result);

   ZeroMemory(check);



//---- =8F80;870F8O AB@C:BC@K B>@3>2>3> 70?@>A0 MqlTradeRequest 4;O C40;5=8O >B;>65==>3> >@45@0

   request.action=TRADE_ACTION_REMOVE;

   request.deviation=deviation;

   request.order=ticket;

//----

   string comment="";

   string ordertype=EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE)));

   StringConcatenate(comment,"<<< ============ ",__FUNCTION__,"(): #40;O5< >B;>65==K9 ",ordertype," >@45@ ?> ",symbol," A B8:5B>< ",string(ticket)," ============ >>>");

   Print(comment);



//---- B?@02:0 ?@8:070 =0 70:@K20=85 ?>78F88 =0 B>@3>2K9 A5@25@

   if(!OrderSend(request,result) || result.retcode!=TRADE_RETCODE_DONE)

     {

      Print(__FUNCTION__,"(): 52>7<>6=> C40;8BL ",ordertype," >@45@ A B8:5B>< ",string(ticket),"!");

      Print(__FUNCTION__,"(): OrderSend(): ",ResultRetcodeDescription(result.retcode));

      return(false);

     }

   else

      if(result.retcode==TRADE_RETCODE_DONE)

        {

         Signal=false;

         comment="";

         StringConcatenate(comment,"<<< ============ ",__FUNCTION__,"(): B;>65==K9 ",ordertype," >@45@ ?> ",symbol," A B8:5B>< ",string(ticket)," C40;Q= ============ >>>");

         Print(comment);

         PlaySound("ok.wav");

        }

      else

        {

         Print(__FUNCTION__,"(): 52>7<>6=> C40;8BL ",ordertype," >@45@ A B8:5B>< ",string(ticket),"!");

         Print(__FUNCTION__,"(): OrderSend(): ",ResultRetcodeDescription(result.retcode));

         return(false);

        }

//----

   return(true);

  }

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

//| 2>72@0B AB@8=3>2>3> @57C;LB0B0 B>@3>2>9 >?5@0F88 ?> 53> :>4C     |

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

string ResultRetcodeDescription(int retcode)

  {

   string str;

//----

   switch(retcode)

     {

      case TRADE_RETCODE_REQUOTE:

         str=" 5:2>B0";

         break;

      case TRADE_RETCODE_REJECT:

         str="0?@>A >B25@3=CB";

         break;

      case TRADE_RETCODE_CANCEL:

         str="0?@>A >B<5=5= B@5945@><";

         break;

      case TRADE_RETCODE_PLACED:

         str="@45@ @07<5I5=";

         break;

      case TRADE_RETCODE_DONE:

         str="0O2:0 2K?>;=5=0";

         break;

      case TRADE_RETCODE_DONE_PARTIAL:

         str="0O2:0 2K?>;=5=0 G0AB8G=>";

         break;

      case TRADE_RETCODE_ERROR:

         str="H81:0 >1@01>B:8 70?@>A0";

         break;

      case TRADE_RETCODE_TIMEOUT:

         str="0?@>A >B<5=5= ?> 8AB5G5=8N 2@5<5=8";

         break;

      case TRADE_RETCODE_INVALID:

         str="5?@028;L=K9 70?@>A";

         break;

      case TRADE_RETCODE_INVALID_VOLUME:

         str="5?@028;L=K9 >1J5< 2 70?@>A5";

         break;

      case TRADE_RETCODE_INVALID_PRICE:

         str="5?@028;L=0O F5=0 2 70?@>A5";

         break;

      case TRADE_RETCODE_INVALID_STOPS:

         str="5?@028;L=K5 AB>?K 2 70?@>A5";

         break;

      case TRADE_RETCODE_TRADE_DISABLED:

         str="">@3>2;O 70?@5I5=0";

         break;

      case TRADE_RETCODE_MARKET_CLOSED:

         str=" K=>: 70:@KB";

         break;

      case TRADE_RETCODE_NO_MONEY:

         str="5B 4>AB0B>G=KE 45=56=KE A@54AB2 4;O 2K?>;=5=8O 70?@>A0";

         break;

      case TRADE_RETCODE_PRICE_CHANGED:

         str="&5=K 87<5=8;8AL";

         break;

      case TRADE_RETCODE_PRICE_OFF:

         str="BACBAB2CNB :>B8@>2:8 4;O >1@01>B:8 70?@>A0";

         break;

      case TRADE_RETCODE_INVALID_EXPIRATION:

         str="525@=0O 40B0 8AB5G5=8O >@45@0 2 70?@>A5";

         break;

      case TRADE_RETCODE_ORDER_CHANGED:

         str="!>AB>O=85 >@45@0 87<5=8;>AL";

         break;

      case TRADE_RETCODE_TOO_MANY_REQUESTS:

         str="!;8H:>< G0ABK5 70?@>AK";

         break;

      case TRADE_RETCODE_NO_CHANGES:

         str=" 70?@>A5 =5B 87<5=5=89";

         break;

      case TRADE_RETCODE_SERVER_DISABLES_AT:

         str="2B>B@5948=3 70?@5I5= A5@25@><";

         break;

      case TRADE_RETCODE_CLIENT_DISABLES_AT:

         str="2B>B@5948=3 70?@5I5= :;85=BA:8< B5@<8=0;><";

         break;

      case TRADE_RETCODE_LOCKED:

         str="0?@>A 701;>:8@>20= 4;O >1@01>B:8";

         break;

      case TRADE_RETCODE_FROZEN:

         str="@45@ 8;8 ?>78F8O 70<>@>65=K";

         break;

      case TRADE_RETCODE_INVALID_FILL:

         str="#:070= =5?>445@68205<K9 B8? 8A?>;=5=8O >@45@0 ?> >AB0B:C";

         break;

      case TRADE_RETCODE_CONNECTION:

         str="5B A>548=5=8O A B>@3>2K< A5@25@><";

         break;

      case TRADE_RETCODE_ONLY_REAL:

         str="?5@0F8O @07@5H5=0 B>;L:> 4;O @50;L=KE AG5B>2";

         break;

      case TRADE_RETCODE_LIMIT_ORDERS:

         str=">AB83=CB ;8<8B =0 :>;8G5AB2> >B;>65==KE >@45@>2";

         break;

      case TRADE_RETCODE_LIMIT_VOLUME:

         str=">AB83=CB ;8<8B =0 >1J5< >@45@>2 8 ?>78F89 4;O 40==>3> A8<2>;0";

         break;

      case TRADE_RETCODE_INVALID_ORDER:

         str="525@=K9 8;8 70?@5IQ==K9 B8? >@45@0";

         break;

      case TRADE_RETCODE_POSITION_CLOSED:

         str=">78F8O A C:070==K< POSITION_IDENTIFIER C65 70:@KB0";

         break;

      case TRADE_RETCODE_INVALID_CLOSE_VOLUME:

         str="0:@K205<K9 >1J5< ?@52KH05B B5:CI89 >1J5< ?>78F88";

         break;

      case TRADE_RETCODE_CLOSE_ORDER_EXIST:

         str=";O C:070==>9 ?>78F88 C65 5ABL >@45@ =0 70:@KB85";

         break;

      case TRADE_RETCODE_LIMIT_POSITIONS:

         str=">;8G5AB2> >B:@KBKE ?>78F89, :>B>@>5 <>6=> >4=>2@5<5==> 8<5BL =0 AG5B5, <>65B 1KBL >3@0=8G5=> =0AB@>9:0<8 A5@25@0";

         break;

      //case : str=""; break;

      //case : str=""; break;

      //case : str=""; break;

      //case : str=""; break;

      default:

         str="58725AB=K9 @57C;LB0B";

     }

//----

   return(str);

  }

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

//|                                                                  |

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

void BuyStopSellLimit(ENUM_PENDING_ORDERS   InpOrders)

  {

//---

//if(InpLots<=0.0)

//  {

//   Print("The \"Lots\" can't be smaller or equal to zero");

//   return;

//  }

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

      return;

   if(!RefreshRates())

      return;



   string err_text="";

//if(!CheckVolumeValue(InpLots,err_text))

//  {

//   Print(err_text);

//   return;

//  }

//---

   if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))

      m_trade.SetTypeFilling(ORDER_FILLING_FOK);

   else

      if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))

         m_trade.SetTypeFilling(ORDER_FILLING_IOC);

      else

         m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

//---

   m_trade.SetDeviationInPoints(m_slippage);

   m_trade.SetAsyncMode(true);

//--- tuning for 3 or 5 digits

    

   //m_adjusted_pointStop=m_symbol.Point()*_SYMBOL_DIGITS_;



   int _SYMBOL_TRADE_STOPS_LEVEL=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);

//Print(_SYMBOL_TRADE_STOPS_LEVEL);

   if(InpGep>_SYMBOL_TRADE_STOPS_LEVEL)

     {_SYMBOL_TRADE_STOPS_LEVEL=InpGep;};



   int _SYMBOL_SPREAD=(int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);

//Print(_SYMBOL_SPREAD);

   if(InpStep>_SYMBOL_SPREAD)

     {_SYMBOL_SPREAD=InpGep;};





   ExtGep       = _SYMBOL_POINT_ * _SYMBOL_TRADE_STOPS_LEVEL;

   ExtStep      = _SYMBOL_POINT_ * _SYMBOL_SPREAD;

   ExtStopLoss    = _SYMBOL_POINT_ * InpStopLoss;

   ExtTakeProfit  = _SYMBOL_POINT_ * InpTakeProfit;

//--- start work





   double start_price_ask=m_symbol.Ask()+ExtGep;

   double start_price_bid=m_symbol.Bid()+ExtGep;







//--- pending orders UP

   for(int i=0; i<InpQuantity; i++)

     {

      double price_ask     = start_price_ask+i*ExtStep;

      double price_bid     = start_price_bid+i*ExtStep;

      if(InpOrders==buy_stop)

        {

         double sl         = (ExtStopLoss==0.0)   ? 0.0 : price_ask - ExtStopLoss;

         double tp         = (ExtTakeProfit==0.0) ? 0.0 : price_ask + ExtTakeProfit;

         m_trade.BuyStop(

            //m_symbol.LotsMin()

            TradeSizeOptimized(),m_symbol.NormalizePrice(price_ask),m_symbol.Name(),

            m_symbol.NormalizePrice(sl),

            m_symbol.NormalizePrice(tp),

            1);

        }

      else

        {

         double sl         = (ExtStopLoss==0.0)   ? 0.0 : price_ask + ExtStopLoss;

         double tp         = (ExtTakeProfit==0.0) ? 0.0 : price_bid - ExtTakeProfit;

         m_trade.SellLimit(TradeSizeOptimized(),m_symbol.NormalizePrice(price_bid),m_symbol.Name(),

                           m_symbol.NormalizePrice(sl),

                           m_symbol.NormalizePrice(tp),

                           1)

         ;

        }

     }

  }

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

//|                                                                  |

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

void BuyLimitSellStop(ENUM_PENDING_ORDERS   InpOrders)

  {

//---

//if(InpLots<=0.0)

//  {

//   Print("The \"Lots\" can't be smaller or equal to zero");

//   return;

//  }

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

      return;

   if(!RefreshRates())

      return;



//string err_text="";

//if(!CheckVolumeValue(InpLots,err_text))

//  {

//   Print(err_text);

//   return;

//  }

//---

   if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))

      m_trade.SetTypeFilling(ORDER_FILLING_FOK);

   else

      if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))

         m_trade.SetTypeFilling(ORDER_FILLING_IOC);

      else

         m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

//---

   m_trade.SetDeviationInPoints(m_slippage);

   m_trade.SetAsyncMode(true);

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;

   //m_adjusted_pointStop=m_symbol.Point()*digits_adjust;



   int _SYMBOL_TRADE_STOPS_LEVEL=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);

//Print(_SYMBOL_TRADE_STOPS_LEVEL);

   if(InpGep>_SYMBOL_TRADE_STOPS_LEVEL)

     {_SYMBOL_TRADE_STOPS_LEVEL=InpGep;};



   int _SYMBOL_SPREAD=(int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);

//Print(_SYMBOL_SPREAD);

   if(InpStep>_SYMBOL_SPREAD)

     {_SYMBOL_SPREAD=InpGep;};



   //ExtGep     = m_adjusted_pointStop * InpGep;

   //ExtStep    = m_adjusted_pointStop * InpStep;

   

    ExtGep       = _SYMBOL_POINT_ * _SYMBOL_TRADE_STOPS_LEVEL;

   ExtStep      = _SYMBOL_POINT_ * _SYMBOL_SPREAD;

   

   ExtStopLoss    = _SYMBOL_POINT_ * InpStopLoss;

   ExtTakeProfit  = _SYMBOL_POINT_ * InpTakeProfit;

//--- start work

   double start_price_ask=m_symbol.Ask()-ExtGep;

   double start_price_bid=m_symbol.Bid()-ExtGep;

//--- pending orders DOWN

   for(int i=0; i<InpQuantity; i++)

     {

      double price_ask     = start_price_ask-i*ExtStep;

      double price_bid     = start_price_bid-i*ExtStep;

      if(InpOrders==buy_limit)

        {

         double sl         = (ExtStopLoss==0.0)   ? 0.0 : price_ask - ExtStopLoss;

         double tp         = (ExtTakeProfit==0.0) ? 0.0 : price_ask + ExtTakeProfit;

         m_trade.BuyLimit(TradeSizeOptimized(),m_symbol.NormalizePrice(price_ask),m_symbol.Name(),                    //m_symbol.LotsMin()

                          m_symbol.NormalizePrice(sl),

                          m_symbol.NormalizePrice(tp),

                          1);

        }

      else

        {

         double sl         = (ExtStopLoss==0.0)   ? 0.0 : price_bid + ExtStopLoss;

         double tp         = (ExtTakeProfit==0.0) ? 0.0 : price_bid - ExtTakeProfit;

         m_trade.SellStop(TradeSizeOptimized(),m_symbol.NormalizePrice(price_bid),m_symbol.Name(),

                          m_symbol.NormalizePrice(sl),

                          m_symbol.NormalizePrice(tp),

                          1);

        }

     }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return(false);

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return(false);

//---

   return(true);

  }

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

//| Check the correctness of the order volume                        |

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

bool CheckVolumeValue(double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

   double min_volume=m_symbol.LotsMin();

   if(volume<min_volume)

     {

      error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }



//--- maximal allowed volume of trade operations

   double max_volume=m_symbol.LotsMax();

   if(volume>max_volume)

     {

      error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }



//--- get minimal step of volume changing

   double volume_step=m_symbol.LotsStep();



   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",

                                     volume_step,ratio*volume_step);

      return(false);

     }

   error_description="Correct volume value";

   return(true);

  }

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

//| Checks if the specified filling mode is allowed                  |

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

bool IsFillingTypeAllowed(int fill_type)

  {

//--- Obtain the value of the property that describes allowed filling modes

   int filling=m_symbol.TradeFillFlags();

//--- Return true, if mode fill_type is allowed

   return((filling & fill_type)==fill_type);

  }

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

//| |

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

void _Trailing_BUY()

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

      //if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

     {

      string   position_GetSymbol=PositionGetSymbol(i); // GetSymbol ?>78F88

      if(position_GetSymbol==Symbol())

        {

         //long digits=SymbolInfoInteger(m_position.Symbol(),SYMBOL_DIGITS);

         long _SYMBOL_TRADE_STOPS_LEVEL=SymbolInfoInteger(m_position.Symbol(),SYMBOL_TRADE_STOPS_LEVEL);



         double _PriceCurrent_Stop=0.0;

         double _PriceOpen_Stop_bezubutok=0.0;

         double _PositionModify_Stop=0.0;

         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         if(TrailingStop_STOPS_LEVEL>_SYMBOL_TRADE_STOPS_LEVEL)

           {_SYMBOL_TRADE_STOPS_LEVEL=TrailingStop_STOPS_LEVEL;};



         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            _PriceCurrent_Stop=m_position.PriceCurrent()-(_SYMBOL_TRADE_STOPS_LEVEL+1)*_SYMBOL_POINT_;



            if(m_position.PriceCurrent()>m_position.PriceOpen())

              {

               //_PriceOpen_Stop_bezubutok=m_position.PriceOpen()+(TrailingStopProfit*m_adjusted_point);  //157 C1KB>:



               //if(_PriceCurrent_Stop>=_PriceOpen_Stop_bezubutok && TrailingStopProfit>0)

               //  {_PositionModify_Stop=_PriceOpen_Stop_bezubutok;}  //157 C1KB>:

               //else if(TrailingStopProfit==0 || _PriceCurrent_Stop>_PriceOpen_Stop_bezubutok || m_position.StopLoss()==0)

               //  {_PositionModify_Stop=_PriceCurrent_Stop;};



               //if(_PositionModify_Stop>m_position.StopLoss()

               if(_PriceCurrent_Stop>m_position.StopLoss()

                  || m_position.StopLoss()==0.0    //AB>?>2 =5B

                 )

                 {

                  //--- save Magic Number for the position

                  if(_PriceCurrent_Stop>0)

                    {

                     m_trade.SetExpertMagicNumber(m_position.Magic());

                     m_trade.PositionModify(m_position.Ticket(),

                                            _PriceCurrent_Stop,

                                            m_position.TakeProfit());

                    };



                 };

              };

           };

        };



     };



  }

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

void _Trailing_SELL()

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

      //if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

     {

      string   position_GetSymbol=PositionGetSymbol(i); // GetSymbol ?>78F88

      if(position_GetSymbol==Symbol())

        {



         long _SYMBOL_TRADE_STOPS_LEVEL=SymbolInfoInteger(m_position.Symbol(),SYMBOL_TRADE_STOPS_LEVEL);



         double _PriceCurrent_Stop=0.0;

         double _PriceOpen_Stop_bezubutok=0.0;

         double _PositionModify_Stop=0.0;

         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         if(TrailingStop_STOPS_LEVEL>_SYMBOL_TRADE_STOPS_LEVEL)

           {_SYMBOL_TRADE_STOPS_LEVEL=TrailingStop_STOPS_LEVEL;};

         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         if(m_position.PositionType()==POSITION_TYPE_SELL)

           {



            _PriceCurrent_Stop=m_position.PriceCurrent()+_SYMBOL_TRADE_STOPS_LEVEL*_SYMBOL_POINT_;



            if(m_position.PriceCurrent()<m_position.PriceOpen())

              {

               //_PriceOpen_Stop_bezubutok=m_position.PriceOpen()-(TrailingStopProfit*m_adjusted_point);  //157 C1KB>:

               //if(_PriceCurrent_Stop<=_PriceOpen_Stop_bezubutok && TrailingStopProfit>0)

               //  {_PositionModify_Stop=_PriceOpen_Stop_bezubutok;}  //157 C1KB>:

               //else if(TrailingStopProfit==0 || _PriceCurrent_Stop<_PriceOpen_Stop_bezubutok || m_position.StopLoss()==0)

               //  {_PositionModify_Stop=_PriceCurrent_Stop;};

               //if(_PositionModify_Stop<m_position.StopLoss()

               if(_PriceCurrent_Stop<m_position.StopLoss()



                  || m_position.StopLoss()==0.0   //AB>?>2 =5B

                 )

                 {

                  //Print(_PriceOpen_Stop_bezubutok);

                  if(_PriceCurrent_Stop>0)

                    {

                     //--- save Magic Number for the position

                     m_trade.SetExpertMagicNumber(m_position.Magic());

                     m_trade.PositionModify(m_position.Ticket(),

                                            _PriceCurrent_Stop,

                                            m_position.TakeProfit());

                    };



                 }



              };

           };

        };



     };



  }

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

//| |

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

void _Trailing()

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

      //if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

     {

      string   position_GetSymbol=PositionGetSymbol(i); // GetSymbol ?>78F88

      if(position_GetSymbol==Symbol())

        {

         long digits=SymbolInfoInteger(m_position.Symbol(),SYMBOL_DIGITS);

         long _SYMBOL_TRADE_STOPS_LEVEL=SymbolInfoInteger(m_position.Symbol(),SYMBOL_TRADE_STOPS_LEVEL);

         double m_adjusted_point=SymbolInfoDouble(m_position.Symbol(),SYMBOL_POINT);

         double _PriceCurrent_Stop=0.0;

         double _PriceOpen_Stop_bezubutok=0.0;

         double _PositionModify_Stop=0.0;

         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         if(TrailingStop_STOPS_LEVEL>_SYMBOL_TRADE_STOPS_LEVEL)

           {_SYMBOL_TRADE_STOPS_LEVEL=TrailingStop_STOPS_LEVEL;};



         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            _PriceCurrent_Stop=m_position.PriceCurrent()-(_SYMBOL_TRADE_STOPS_LEVEL+1)*m_adjusted_point;



            if(m_position.PriceCurrent()>m_position.PriceOpen())

              {

               //_PriceOpen_Stop_bezubutok=m_position.PriceOpen()+(TrailingStopProfit*m_adjusted_point);  //157 C1KB>:



               //if(_PriceCurrent_Stop>=_PriceOpen_Stop_bezubutok && TrailingStopProfit>0)

               //  {_PositionModify_Stop=_PriceOpen_Stop_bezubutok;}  //157 C1KB>:

               //else if(TrailingStopProfit==0 || _PriceCurrent_Stop>_PriceOpen_Stop_bezubutok || m_position.StopLoss()==0)

               //  {_PositionModify_Stop=_PriceCurrent_Stop;};



               //if(_PositionModify_Stop>m_position.StopLoss()

               if(_PriceCurrent_Stop>m_position.StopLoss()

                  || m_position.StopLoss()==0.0    //AB>?>2 =5B

                 )

                 {

                  //--- save Magic Number for the position

                  if(_PriceCurrent_Stop>0)

                    {

                     m_trade.SetExpertMagicNumber(m_position.Magic());

                     m_trade.PositionModify(m_position.Ticket(),

                                            _PriceCurrent_Stop,

                                            m_position.TakeProfit());

                    };



                 };

              };

           };



         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         if(m_position.PositionType()==POSITION_TYPE_SELL)

           {



            _PriceCurrent_Stop=m_position.PriceCurrent()+_SYMBOL_TRADE_STOPS_LEVEL*m_adjusted_point;



            if(m_position.PriceCurrent()<m_position.PriceOpen())

              {

               //_PriceOpen_Stop_bezubutok=m_position.PriceOpen()-(TrailingStopProfit*m_adjusted_point);  //157 C1KB>:

               //if(_PriceCurrent_Stop<=_PriceOpen_Stop_bezubutok && TrailingStopProfit>0)

               //  {_PositionModify_Stop=_PriceOpen_Stop_bezubutok;}  //157 C1KB>:

               //else if(TrailingStopProfit==0 || _PriceCurrent_Stop<_PriceOpen_Stop_bezubutok || m_position.StopLoss()==0)

               //  {_PositionModify_Stop=_PriceCurrent_Stop;};

               //if(_PositionModify_Stop<m_position.StopLoss()

               if(_PriceCurrent_Stop<m_position.StopLoss()



                  || m_position.StopLoss()==0.0   //AB>?>2 =5B

                 )

                 {

                  //Print(_PriceOpen_Stop_bezubutok);

                  if(_PriceCurrent_Stop>0)

                    {

                     //--- save Magic Number for the position

                     m_trade.SetExpertMagicNumber(m_position.Magic());

                     m_trade.PositionModify(m_position.Ticket(),

                                            _PriceCurrent_Stop,

                                            m_position.TakeProfit());

                    };



                 }



              };

           };

        };



     };



  }









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

//| |

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



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

string TFMigrate(int tf)

  {

   switch(tf)

     {

      case 0:

         return("CURRENT");

      case 1:

         return("M01");

      case 5:

         return("M05");

      case 15:

         return("M15");

      case 30:

         return("M30");

      case 60:

         return("H1");

      case 240:

         return("H4");

      case 1440:

         return("D1");

      case 10080:

         return("W1");

      case 43200:

         return("MN1");



      case 2:

         return("M2");

      case 3:

         return("M3");

      case 4:

         return("M4");

      case 6:

         return("M6");

      case 10:

         return("M10");

      case 12:

         return("M12");

      case 20:

         return("M20");

      case 16385:

         return("H1");

      case 16386:

         return("H2");

      case 16387:

         return("H3");

      case 16388:

         return("H4");

      case 16390:

         return("H6");

      case 16392:

         return("H8");

      case 16396:

         return("H12");

      case 16408:

         return("D1");

      case 32769:

         return("W1");

      case 49153:

         return("MN1");

      default:

         return("CURRENT");

     }

  }

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

string nomerPeriod(int tf)

  {

   switch(tf)

     {

      case 0:

         return("0");

      case 1:

         return("01");

      case 2:

         return("02");

      case 3:

         return("03");

      case 4:

         return("04");

      case 5:

         return("05");

      case 6:

         return("06");

      case 10:

         return("07");

      case 12:

         return("08");

      case 15:

         return("09");

      case 20:

         return("10");

      case 30:

         return("11");

      case 60:

         return("12");

      case 240:

         return("13");

      case 1440:

         return("14");

      case 10080:

         return("15");

      case 43200:

         return("16");

      case 16385:

         return("17");

      case 16386:

         return("18");

      case 16387:

         return("19");

      case 16388:

         return("20");

      case 16390:

         return("21");

      case 16392:

         return("22");

      case 16396:

         return("23");

      case 16408:

         return("24");

      case 32769:

         return("25");

      case 49153:

         return("26");

      default:

         return("0");

     }

  }

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

//| ScrollToTime   |

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

bool ScrollToTime(datetime aTime,long currChart)

  {

   datetime tm[1];

   aTime/=PeriodSeconds(ChartPeriod(currChart));

   aTime*=PeriodSeconds(ChartPeriod(currChart));

   int rv=CopyTime(ChartSymbol(currChart),ChartPeriod(currChart),0,1,tm);

   int NewPos=Bars(ChartSymbol(currChart),ChartPeriod(currChart),aTime,tm[0])-1;

   NewPos=MathMin(NewPos,Bars(ChartSymbol(currChart),ChartPeriod(currChart))-1);

//ChartNavigate(0,CHART_END,-NewPos);

   ChartNavigate(currChart,CHART_END,-NewPos);

   return true;

  }

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

//| |

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

//string _Sumbol_N(string ft)

//  {

//   if(ft ==  "EURUSD" ) return("01");

//   if(ft ==  "GBPUSD" ) return("02");

//   if(ft ==  "NZDUSD" ) return("03");

//   if(ft ==  "AUDUSD" ) return("04");

//   if(ft ==  "USDCAD" ) return("05");

//   if(ft ==  "USDCHF" ) return("06");

//   if(ft ==  "USDJPY" ) return("07");

//   if(ft ==  "XAUUSD" ) return("08");

//   if(ft ==  "HO" ) return("09");

//   if(ft ==  "CL" ) return("10");

//   if(ft ==  "WT" ) return("11");

//   if(ft ==  "BRN" ) return("12");

//   if(ft ==  "USDX" ) return("13");

//   if(ft ==  "USDXXX" ) return("14");

//

//   return "";

//  }

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

//string TFMigrate(int tf)

//  {

//   switch(tf)

//     {

// case 0: return("CURRENT");

// case 1: return("M1");

// case 5: return("M5");

// case 15: return("M15");

// case 30: return("M30");

// case 60: return("H1");

// case 240: return("H4");

// case 1440: return("D1");

// case 10080: return("W1");

// case 43200: return("MN1");

//

// case 2: return("M2");

// case 3: return("M3");

// case 4: return("M4");

// case 6: return("M6");

// case 10: return("M10");

// case 12: return("M12");

//   case 20: return("M20");

// case 16385: return("H1");

// case 16386: return("H2");

// case 16387: return("H3");

// case 16388: return("H4");

// case 16390: return("H6");

// case 16392: return("H8");

// case 16396: return("H12");

// case 16408: return("D1");

// case 32769: return("W1");

// case 49153: return("MN1");

// default: return("CURRENT");

//     }

//  }

//|  ?8A0=85 : >102;O5B : AB@>:5 S AB>;L:> ;848@CNI8E =C;59 "0",   |

//| : GB>1K 4;8=0 AB@>:8 S AB0;0 @02=0 K.   |

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

//|  0@0<5B@K:    |

//|    s - AB@>:0  |

//|    k - 4;8=0 AB@>:8 S    |

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

//string AddLeadingZero(string s,int k)

//  {

//   if(StringLen(s)==k)

//     { return(s);}

//   else

//     {while(StringConcatenate(s,"0",s)<k);};

//   return(s);

//  }

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

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

//| Script program start function |

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

//void OnStart()

//  {

//   Comment("");

//   ChartSetInteger(0,CHART_SHIFT,false);

//   ChartSetInteger(0,CHART_AUTOSCROLL,false);

//   int _bars=Bars(Symbol(),0);

////_bars=1000;

//   int _count=0;

//   for(int i=0; i<_bars; i++)

//     {

// MqlDateTime time_now;

// datetime candle_time=iTime(NULL,_Period,i);

// TimeToStruct(candle_time,time_now);  // change time to struct

//

// if(time_now.hour==13 && time_now.min==00)

//   {

//    _count+=1;

//    string _date=string(time_now.year)+"."+AddLeadingZero(string(time_now.mon),2)+"."+AddLeadingZero(string(time_now.day),2);

//    Comment(_date);

//    bool _true=ScrollToTime(candle_time);

//    Sleep(1000);

//    //--- ?>;CG8< =><5@ A0<>3> ?5@2>3> 2848<>3> =0 3@0D8:5 10@0 (=C<5@0F8O :0: 2 B09<A5@88)

//    long first_bar=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);

//    Print(string(first_bar)+" = "+string(i));

//

//    string name=_Sumbol_N(string(Symbol()))

// +" "+AddLeadingZero(string(_count),3)

// +" "+string(time_now.year)+"."

// +AddLeadingZero(string(time_now.mon),2)+"."

// +AddLeadingZero(string(time_now.day),2)

// //+" "+AddLeadingZero(string(time_now.hour),2)+"."

// //+AddLeadingZero(string(time_now.min),2)

// +" "+_nameObect+"_"+TFMigrate(Period())

// //+" 2560_1600.PNG"

// ;

//

//    ChartScreenShot(0,"//!:@8=H>BK//"+_nameObect+"//"+name+".PNG",2560,1600,ALIGN_CENTER);

//    //i=5;

//   };

//     };

//

////Print(iTime(NULL,_Period,0));

//

//  }







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

//| Calculate optimal lot size    |

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

double TradeSizeOptimized(void)

  {

   double price=0.0;

   double margin=0.0;

//--- select lot size

   if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))

      return(0.0);

   if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))

      return(0.0);

   if(margin<=0.0)

      return(0.0);



   double lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*MaximumRisk/margin,2);

//--- calculate number of losses orders without a break

   if(DecreaseFactor>0)

     {

      //--- select history for access

      HistorySelect(0,TimeCurrent());

      //---

      int    orders=HistoryDealsTotal();  // total history deals

      int    losses=0;     // number of losses orders without a break



      for(int i=orders-1; i>=0; i--)

        {

         ulong ticket=HistoryDealGetTicket(i);

         if(ticket==0)

           {

            Print("HistoryDealGetTicket failed, no trade history");

            break;

           }

         //--- check symbol

         if(HistoryDealGetString(ticket,DEAL_SYMBOL)!=_Symbol)

            continue;

         //--- check Expert Magic number

         //if(HistoryDealGetInteger(ticket,DEAL_MAGIC)!=MA_MAGIC)

         //   continue;

         //--- check profit

         double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);

         if(profit>0.0)

            break;

         if(profit<0.0)

            losses++;

        }

      //---

      if(losses>1)

         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

     }

//--- normalize and check limits

   double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

   lot=stepvol*NormalizeDouble(lot/stepvol,0);



   double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);

   if(lot<minvol)

      lot=minvol;



   double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);

   if(lot>maxvol)

      lot=maxvol;

//--- return trading volume

   return(lot);

  }

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

void ButtonCreate(string name,int Ydist,int Ysize,int FONTSIZE=12)

  {

   int Xdist=10;

   int Xsize=150;

   if(ObjectFind(0,_nameObect+name)<0)

     {

      ObjectCreate(0,_nameObect+name,OBJ_BUTTON,0,100,100);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_COLOR,clrBlack);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_BGCOLOR,clrGoldenrod);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_BORDER_COLOR,clrLime);

      //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

      ObjectSetInteger(0,_nameObect+name,OBJPROP_BACK,false);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_XDISTANCE,Xdist+Xsize);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_YDISTANCE,Ydist);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_XSIZE,Xsize);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_YSIZE,Ysize);

      ObjectSetString(0,_nameObect+name,OBJPROP_FONT,"Sans Serif");

      ObjectSetString(0,_nameObect+name,OBJPROP_TEXT,name);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_FONTSIZE,FONTSIZE);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

      ObjectSetInteger(0,_nameObect+name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);

     };

  }

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

//| |

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

void ButtonCreateFor()

  {

   int Ydist=5;

   int Ysize=18;

   for(int i=0; i<ArraySize(_name); i++)

     {

      if(_name[i]=="label")

        {

         ButtonCreate(_name[i],Ydist,Ysize-2,10);

         Ydist=Ydist+Ysize;

         ObjectSetInteger(0,_nameObect+"label",OBJPROP_STATE,false);

         double price=0.0;

         double margin=0.0;

         if(SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))

           {

            if(OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))

              {ObjectSetString(0,_nameObect+"label",OBJPROP_TEXT,Symbol()+ " " + DoubleToString(MathCeil(margin),0));}

            else

              {

               ObjectSetString(0,_nameObect+"label",OBJPROP_TEXT,Symbol() + " "+DoubleToString(MathCeil(0),0));

              };

           };

        }

      else

        {

         if(i==8 || i==15 || i==1 || i==(ArraySize(_name)-1))

           {

            Ydist=Ydist+3;

           };

         ButtonCreate(_name[i],Ydist,Ysize,10);

         Ydist=Ydist+Ysize+3;

        };

     };

  }

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

//| |

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

void _TakeProfit_BUY()

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i))

        {

         if(m_position.Symbol()==Symbol())

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               double _PriceCurrent_TakeProfit=NormalizeDouble(m_position.PriceOpen()+((TakeProfit)*_SYMBOL_POINT_),_SYMBOL_DIGITS_) ;

               double _PriceCurrent=NormalizeDouble(m_position.PriceCurrent()+((TakeProfit)*_SYMBOL_POINT_), _SYMBOL_DIGITS_);

                 {

                  if(_PriceCurrent_TakeProfit>m_position.TakeProfit() || m_position.TakeProfit()==0.0)

                    {

                     if(_PriceCurrent_TakeProfit>0)

                       {

                        m_trade.PositionModify(m_position.Ticket(),

                                               m_position.StopLoss(),

                                               _PriceCurrent_TakeProfit);

                       };



                    };

                 };

              };

           };

        };

  }

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

//| |

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

void _TakeProfit_SELL()

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions



      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

        {

         if(PositionGetSymbol(i)==Symbol())

           {

            if(m_position.PositionType()==POSITION_TYPE_SELL)

              {

               double _PriceCurrent_TakeProfit =NormalizeDouble(m_position.PriceOpen()-(TakeProfit*_SYMBOL_POINT_),_SYMBOL_DIGITS_) ;

               double _PriceCurrent=NormalizeDouble(m_position.PriceCurrent()-(TakeProfit*_SYMBOL_POINT_),_SYMBOL_DIGITS_);

               //Print( _PriceCurrent - _PriceCurrent_TakeProfit );

                 {

                  if(_PriceCurrent_TakeProfit<m_position.TakeProfit() || m_position.TakeProfit()==0.0)

                    {

                     if(_PriceCurrent_TakeProfit>0)

                       {

                        m_trade.PositionModify(m_position.Ticket(),

                                               m_position.StopLoss(),

                                               _PriceCurrent_TakeProfit);

                       }

                     ;



                    }



                 };

              };

           };



        };



  }

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

//| |

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

void _BTrailing_BUY()

  {

//double _B_BUY=0.0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i))

        {

         if(m_position.Symbol()==Symbol())

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               long _SYMBOL_TRADE_STOPS_LEVEL=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);

               //if(TrailingStop_STOPS_LEVELBreakeven>_SYMBOL_TRADE_STOPS_LEVEL)

               //  {_SYMBOL_TRADE_STOPS_LEVEL=TrailingStop_STOPS_LEVELBreakeven;};

               double _PriceCurrent_Stop=NormalizeDouble(m_position.PriceOpen()+((TrailingStop_STOPS_LEVELBreakeven)*_SYMBOL_POINT_),_SYMBOL_DIGITS_) ;

               double _PriceCurrent_Stop_LEVEL=NormalizeDouble(m_position.PriceOpen()+((_SYMBOL_TRADE_STOPS_LEVEL)*_SYMBOL_POINT_),_SYMBOL_DIGITS_) ;

               double _PriceCurrent=NormalizeDouble(m_position.PriceCurrent()-((_SYMBOL_TRADE_STOPS_LEVEL)*_SYMBOL_POINT_),_SYMBOL_DIGITS_);

               if(_PriceCurrent >  _PriceCurrent_Stop && _PriceCurrent>_PriceCurrent_Stop_LEVEL)

                 {

                  if(_PriceCurrent_Stop>m_position.StopLoss() || m_position.StopLoss()==0.0)

                    {

                     if(_PriceCurrent_Stop>0)

                       {

                        m_trade.PositionModify(m_position.Ticket(),

                                               _PriceCurrent_Stop,

                                               m_position.TakeProfit());

                       };



                    };

                 };

              };

           };

        };

  }

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

//| |

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

void _BTrailing_SELL()

  {

//double _B_SELL=0.0;

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

        {

         if(PositionGetSymbol(i)==Symbol())

           {

            long _SYMBOL_TRADE_STOPS_LEVEL=SymbolInfoInteger(m_position.Symbol(),SYMBOL_TRADE_STOPS_LEVEL);

            //if(TrailingStop_STOPS_LEVELBreakeven>_SYMBOL_TRADE_STOPS_LEVEL)

            //  {_SYMBOL_TRADE_STOPS_LEVEL=TrailingStop_STOPS_LEVELBreakeven;};

            if(m_position.PositionType()==POSITION_TYPE_SELL)

              {

               double _PriceCurrent_Stop =NormalizeDouble(m_position.PriceOpen()-((TrailingStop_STOPS_LEVELBreakeven)*_SYMBOL_POINT_),_SYMBOL_DIGITS_) ;

               double _PriceCurrent_Stop_LEVEL =NormalizeDouble(m_position.PriceOpen()-((_SYMBOL_TRADE_STOPS_LEVEL)*_SYMBOL_POINT_),_SYMBOL_DIGITS_) ;

               double _PriceCurrent=NormalizeDouble(m_position.PriceCurrent()+((_SYMBOL_TRADE_STOPS_LEVEL)*_SYMBOL_POINT_),_SYMBOL_DIGITS_);

               if(_PriceCurrent<_PriceCurrent_Stop && _PriceCurrent<_PriceCurrent_Stop_LEVEL)

                 {

                  if(_PriceCurrent_Stop<m_position.StopLoss()   || m_position.StopLoss()==0.0)

                    {

                     if(_PriceCurrent_Stop>0)

                       {

                        m_trade.PositionModify(m_position.Ticket(),

                                               _PriceCurrent_Stop,

                                               m_position.TakeProfit());

                       };

                    }

                 };

              };

           };



        };

  }





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

//| |

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

string _Sumbol_N(string ft)

  {

   if(ft ==  "EURUSD")

      return("01");

   if(ft ==  "GBPUSD")

      return("02");

   if(ft ==  "NZDUSD")

      return("03");

   if(ft ==  "AUDUSD")

      return("04");

   if(ft ==  "USDCAD")

      return("05");

   if(ft ==  "USDCHF")

      return("06");

   if(ft ==  "USDJPY")

      return("07");

   if(ft ==  "XAUUSD")

      return("08");

   if(ft ==  "HO")

      return("09");

   if(ft ==  "CL")

      return("10");

   if(ft ==  "WT")

      return("11");

   if(ft ==  "BRN")

      return("12");

   if(ft ==  "USDX")

      return("13");

   if(ft ==  "USDXXX")

      return("14");



   return "";

  }

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

//| |

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

string AddLeadingZero(string s,int k)

  {

   if(StringLen(s)==k)

     { return(s);}

   else

     {while(StringConcatenate(s,"0",s)<k);};

   return(s);

  }

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

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

//| |

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

double F_profit(ENUM_POSITION_TYPE __POSITION_TYPE)

  {

   double PROFIT=0.01;

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

     {

      if(PositionGetSymbol(i)==Symbol())

        {

         if(m_position.PositionType()==__POSITION_TYPE)

           {

            double _POSITION_PROFIT=PositionGetDouble(POSITION_PROFIT);

            PROFIT=PROFIT+_POSITION_PROFIT;

            if(_POSITION_PROFIT<0.0)

              {

               return -1;

              };

           };

        };

     };

   return PROFIT;

  }

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

bool _Skrinshot()

  {

   MqlDateTime time_now;

   TimeToStruct(TimeLocal(),time_now);

   string name="SEND\\"+ _Sumbol_N(Symbol())+" "+(Symbol())+" "+nomerPeriod(Period())+"_"+TFMigrate(Period())+" "+string(time_now.year)+"."+AddLeadingZero(string(time_now.mon),2)

               +"."+AddLeadingZero(string(time_now.day),2)+" "+AddLeadingZero(string(time_now.hour),2)+"."+AddLeadingZero(string(time_now.min),2);

   Sleep(100);

   ChartScreenShot(0,name+" 1920_1200.PNG",1920,1200,ALIGN_LEFT);

   return (true);

  }

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



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



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

bool OpenBuy(double LotsX)

  {





//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   if(PositionsTotal()<limit_total_symbol)

     {

      //--- >1JO2;5=85 8 8=8F80;870F8O 70?@>A0 8 @57C;LB0B0

      MqlTradeRequest request= {0};

      MqlTradeResult result= {0};

      //--- ?0@0<5B@K 70?@>A0

      request.action=TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

      request.symbol=Symbol(); // A8<2>;

      request.volume=LotsX; // >1J5< 2 0.1 ;>B *((int)MathAbs(main_buffer))

      request.type=ORDER_TYPE_BUY; // B8? >@45@0

      request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK); // F5=0 4;O >B:@KB8O

      request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

      request.magic=EXPERT_MAGIC; // MagicNumber >@45@0

      //request.sl=SymbolInfoDouble(Symbol(),SYMBOL_BID)-TrailingStop_STOPS_LEVEL*SymbolInfoDouble(Symbol(),SYMBOL_POINT); // #@>25=L Stop Loss >@45@0

      //request.tp=SymbolInfoDouble(Symbol(),SYMBOL_BID)+(TrailingStop_STOPS_LEVEL*2)*SymbolInfoDouble(Symbol(),SYMBOL_POINT); // #@>25=L Take Profit >@45@0

      //request.tp=0.72102; // #@>25=L Take Profit >@45@0

      //--- >B?@02:0 70?@>A0

      if(!OrderSend(request,result))

         PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

      //--- 8=D>@<0F8O >1 >?5@0F88

      PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

     };



   return (true);

  }

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

//| |

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

bool OpenSell(double LotsX)

  {

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   if(PositionsTotal()<limit_total_symbol)

     {

      //--- >1JO2;5=85 8 8=8F80;870F8O 70?@>A0 8 @57C;LB0B0

      MqlTradeRequest request= {0};

      MqlTradeResult result= {0};

      //--- ?0@0<5B@K 70?@>A0

      request.action=TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

      request.symbol=Symbol(); // A8<2>;

      request.volume=LotsX; // >1J5< 2 0.1 ;>B *((int)MathAbs(main_buffer))

      request.type=ORDER_TYPE_SELL; // B8? >@45@0

      request.price=SymbolInfoDouble(Symbol(),SYMBOL_BID); // F5=0 4;O >B:@KB8O

      request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

      request.magic=EXPERT_MAGIC; // MagicNumber >@45@0

      //request.sl=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+TrailingStop_STOPS_LEVEL*SymbolInfoDouble(Symbol(),SYMBOL_POINT); // #@>25=L Stop Loss >@45@0

      //request.tp=SymbolInfoDouble(Symbol(),SYMBOL_ASK)-(TrailingStop_STOPS_LEVEL*2)*SymbolInfoDouble(Symbol(),SYMBOL_POINT); // #@>25=L Take Profit >@45@0

      //request.tp=0.67409; // #@>25=L Take Profit >@45@0

      //--- >B?@02:0 70?@>A0

      if(!OrderSend(request,result))

         PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

      //--- 8=D>@<0F8O >1 >?5@0F88

      PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

     };



   return (true);

  }

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

//| 0:@KB85 2A5E ?>78F89 Sell |

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

bool CheckForCloseSell()

  {

//--- >1JO2;5=85 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request;

   MqlTradeResult result;

   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   for(int i=total-1; i>=0; i--)

     {

      //--- ?0@0<5B@K >@45@0

      ulong position_ticket=PositionGetTicket(i); // B8:5B ?>78F88

      string position_symbol=PositionGetString(POSITION_SYMBOL); // A8<2>;

      int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9

      ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber ?>78F88

      double volume=PositionGetDouble(POSITION_VOLUME); // >1J5< ?>78F88

      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // B8? ?>78F88

      //--- 2K2>4 8=D>@<0F88 > ?>78F88

      PrintFormat("#%I64u %s %s %.2f %s [%I64d]",

                  position_ticket,

                  position_symbol,

                  EnumToString(type),

                  volume,

                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),

                  magic);

      //--- 5A;8 MagicNumber A>2?0405B

      if(type==POSITION_TYPE_SELL && position_symbol==Symbol())

        {

         //--- >1=C;5=85 7=0G5=89 70?@>A0 8 @57C;LB0B0

         ZeroMemory(request);

         ZeroMemory(result);

         //--- CAB0=>2:0 ?0@0<5B@>2 >?5@0F88

         request.action =TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

         request.position =position_ticket; // B8:5B ?>78F88

         request.symbol=position_symbol; // A8<2>;

         request.volume=volume; // >1J5< ?>78F88

         request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

         request.magic=EXPERT_MAGIC; // MagicNumber ?>78F88

         //--- CAB0=>2:0 F5=K 8 B8?0 >@45@0 2 7028A8<>AB8 >B B8?0 ?>78F88

         request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);

         request.type=ORDER_TYPE_BUY;

         //--- 2K2>4 8=D>@<0F88 > 70:@KB88

         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- >B?@02:0 70?@>A0

         if(!OrderSend(request,result))

            PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

         //--- 8=D>@<0F8O >1 >?5@0F88

         PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

         //---

        }

     }

   return (true);

  }

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

//| 0:@KB85 2A5E ?>78F89 Buy |

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

bool CheckForCloseBuy()

  {

//--- >1JO2;5=85 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request;

   MqlTradeResult result;

   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   for(int i=total-1; i>=0; i--)

     {

      //--- ?0@0<5B@K >@45@0

      ulong position_ticket=PositionGetTicket(i); // B8:5B ?>78F88

      string position_symbol=PositionGetString(POSITION_SYMBOL); // A8<2>;

      int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9

      ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber ?>78F88

      double volume=PositionGetDouble(POSITION_VOLUME); // >1J5< ?>78F88

      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // B8? ?>78F88

      //--- 2K2>4 8=D>@<0F88 > ?>78F88

      PrintFormat("#%I64u %s %s %.2f %s [%I64d]",

                  position_ticket,

                  position_symbol,

                  EnumToString(type),

                  volume,

                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),

                  magic);

      //--- 5A;8 MagicNumber A>2?0405B

      if(type==POSITION_TYPE_BUY && position_symbol==Symbol())

        {

         //--- >1=C;5=85 7=0G5=89 70?@>A0 8 @57C;LB0B0

         ZeroMemory(request);

         ZeroMemory(result);

         //--- CAB0=>2:0 ?0@0<5B@>2 >?5@0F88

         request.action =TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

         request.position =position_ticket; // B8:5B ?>78F88

         request.symbol=position_symbol; // A8<2>;

         request.volume=volume; // >1J5< ?>78F88

         request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

         request.magic=EXPERT_MAGIC; // MagicNumber ?>78F88

         //--- CAB0=>2:0 F5=K 8 B8?0 >@45@0 2 7028A8<>AB8 >B B8?0 ?>78F88

         request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);

         request.type=ORDER_TYPE_SELL;

         //--- 2K2>4 8=D>@<0F88 > 70:@KB88

         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- >B?@02:0 70?@>A0

         if(!OrderSend(request,result))

            PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

         //--- 8=D>@<0F8O >1 >?5@0F88

         PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

         //---

        }

     }

   return (true);

  }





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

//|                                                                  |

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

bool CheckForCloseBuyProfit()

  {

//--- >1JO2;5=85 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request;

   MqlTradeResult result;

   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   for(int i=total-1; i>=0; i--)

     {

      //--- ?0@0<5B@K >@45@0

      ulong position_ticket=PositionGetTicket(i); // B8:5B ?>78F88

      string position_symbol=PositionGetString(POSITION_SYMBOL); // A8<2>;

      int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9

      ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber ?>78F88

      double volume=PositionGetDouble(POSITION_VOLUME); // >1J5< ?>78F88

      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // B8? ?>78F88

      //--- 2K2>4 8=D>@<0F88 > ?>78F88

      PrintFormat("#%I64u %s %s %.2f %s [%I64d]",

                  position_ticket,

                  position_symbol,

                  EnumToString(type),

                  volume,

                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),

                  magic);

      //--- 5A;8 MagicNumber A>2?0405B

      if(type==POSITION_TYPE_BUY && position_symbol==Symbol() && PositionGetDouble(POSITION_PROFIT)>0.0)

        {

         //--- >1=C;5=85 7=0G5=89 70?@>A0 8 @57C;LB0B0

         ZeroMemory(request);

         ZeroMemory(result);

         //--- CAB0=>2:0 ?0@0<5B@>2 >?5@0F88

         request.action =TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

         request.position =position_ticket; // B8:5B ?>78F88

         request.symbol=position_symbol; // A8<2>;

         request.volume=volume; // >1J5< ?>78F88

         request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

         request.magic=EXPERT_MAGIC; // MagicNumber ?>78F88

         //--- CAB0=>2:0 F5=K 8 B8?0 >@45@0 2 7028A8<>AB8 >B B8?0 ?>78F88

         request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);

         request.type=ORDER_TYPE_SELL;

         //--- 2K2>4 8=D>@<0F88 > 70:@KB88

         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- >B?@02:0 70?@>A0

         if(!OrderSend(request,result))

            PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

         //--- 8=D>@<0F8O >1 >?5@0F88

         PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

         //---

        }

     }

   return (true);

  }



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

//|                                                                  |

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

bool CheckForCloseSellProfit()

  {

//--- >1JO2;5=85 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request;

   MqlTradeResult result;

   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   for(int i=total-1; i>=0; i--)

     {

      //--- ?0@0<5B@K >@45@0

      ulong position_ticket=PositionGetTicket(i); // B8:5B ?>78F88

      string position_symbol=PositionGetString(POSITION_SYMBOL); // A8<2>;

      int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9

      ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber ?>78F88

      double volume=PositionGetDouble(POSITION_VOLUME); // >1J5< ?>78F88

      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // B8? ?>78F88

      //--- 2K2>4 8=D>@<0F88 > ?>78F88

      PrintFormat("#%I64u %s %s %.2f %s [%I64d]",

                  position_ticket,

                  position_symbol,

                  EnumToString(type),

                  volume,

                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),

                  magic);

      //--- 5A;8 MagicNumber A>2?0405B

      if(type==POSITION_TYPE_SELL && position_symbol==Symbol() && PositionGetDouble(POSITION_PROFIT)>0.0)

        {

         //--- >1=C;5=85 7=0G5=89 70?@>A0 8 @57C;LB0B0

         ZeroMemory(request);

         ZeroMemory(result);

         //--- CAB0=>2:0 ?0@0<5B@>2 >?5@0F88

         request.action =TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

         request.position =position_ticket; // B8:5B ?>78F88

         request.symbol=position_symbol; // A8<2>;

         request.volume=volume; // >1J5< ?>78F88

         request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

         request.magic=EXPERT_MAGIC; // MagicNumber ?>78F88

         //--- CAB0=>2:0 F5=K 8 B8?0 >@45@0 2 7028A8<>AB8 >B B8?0 ?>78F88

         request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);

         request.type=ORDER_TYPE_BUY;

         //--- 2K2>4 8=D>@<0F88 > 70:@KB88

         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- >B?@02:0 70?@>A0

         if(!OrderSend(request,result))

            PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

         //--- 8=D>@<0F8O >1 >?5@0F88

         PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

         //---

        }

     }

   return (true);

  }

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

//|                                                                  |

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

bool CheckForCloseBuyLoss()

  {

//--- >1JO2;5=85 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request;

   MqlTradeResult result;

   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   for(int i=total-1; i>=0; i--)

     {

      //--- ?0@0<5B@K >@45@0

      ulong position_ticket=PositionGetTicket(i); // B8:5B ?>78F88

      string position_symbol=PositionGetString(POSITION_SYMBOL); // A8<2>;

      int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9

      ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber ?>78F88

      double volume=PositionGetDouble(POSITION_VOLUME); // >1J5< ?>78F88

      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // B8? ?>78F88

      //--- 2K2>4 8=D>@<0F88 > ?>78F88

      PrintFormat("#%I64u %s %s %.2f %s [%I64d]",

                  position_ticket,

                  position_symbol,

                  EnumToString(type),

                  volume,

                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),

                  magic);

      //--- 5A;8 MagicNumber A>2?0405B

      if(type==POSITION_TYPE_BUY && position_symbol==Symbol() && PositionGetDouble(POSITION_PROFIT)<0.0)

        {

         //--- >1=C;5=85 7=0G5=89 70?@>A0 8 @57C;LB0B0

         ZeroMemory(request);

         ZeroMemory(result);

         //--- CAB0=>2:0 ?0@0<5B@>2 >?5@0F88

         request.action =TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

         request.position =position_ticket; // B8:5B ?>78F88

         request.symbol=position_symbol; // A8<2>;

         request.volume=volume; // >1J5< ?>78F88

         request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

         request.magic=EXPERT_MAGIC; // MagicNumber ?>78F88

         //--- CAB0=>2:0 F5=K 8 B8?0 >@45@0 2 7028A8<>AB8 >B B8?0 ?>78F88

         request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID);

         request.type=ORDER_TYPE_SELL;

         //--- 2K2>4 8=D>@<0F88 > 70:@KB88

         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- >B?@02:0 70?@>A0

         if(!OrderSend(request,result))

            PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

         //--- 8=D>@<0F8O >1 >?5@0F88

         PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

         //---

        }

     }

   return (true);

  }



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

//|                                                                  |

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

bool CheckForCloseSellLoss()

  {

//--- >1JO2;5=85 70?@>A0 8 @57C;LB0B0

   MqlTradeRequest request;

   MqlTradeResult result;

   int total=PositionsTotal(); // :>;8G5AB2> >B:@KBKE ?>78F89

//--- ?5@51>@ 2A5E >B:@KBKE ?>78F89

   for(int i=total-1; i>=0; i--)

     {

      //--- ?0@0<5B@K >@45@0

      ulong position_ticket=PositionGetTicket(i); // B8:5B ?>78F88

      string position_symbol=PositionGetString(POSITION_SYMBOL); // A8<2>;

      int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9

      ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber ?>78F88

      double volume=PositionGetDouble(POSITION_VOLUME); // >1J5< ?>78F88

      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // B8? ?>78F88

      //--- 2K2>4 8=D>@<0F88 > ?>78F88

      PrintFormat("#%I64u %s %s %.2f %s [%I64d]",

                  position_ticket,

                  position_symbol,

                  EnumToString(type),

                  volume,

                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),

                  magic);

      //--- 5A;8 MagicNumber A>2?0405B

      if(type==POSITION_TYPE_SELL && position_symbol==Symbol() && PositionGetDouble(POSITION_PROFIT)<0.0)

        {

         //--- >1=C;5=85 7=0G5=89 70?@>A0 8 @57C;LB0B0

         ZeroMemory(request);

         ZeroMemory(result);

         //--- CAB0=>2:0 ?0@0<5B@>2 >?5@0F88

         request.action =TRADE_ACTION_DEAL; // B8? B>@3>2>9 >?5@0F88

         request.position =position_ticket; // B8:5B ?>78F88

         request.symbol=position_symbol; // A8<2>;

         request.volume=volume; // >1J5< ?>78F88

         request.deviation=5; // 4>?CAB8<>5 >B:;>=5=85 >B F5=K

         request.magic=EXPERT_MAGIC; // MagicNumber ?>78F88

         //--- CAB0=>2:0 F5=K 8 B8?0 >@45@0 2 7028A8<>AB8 >B B8?0 ?>78F88

         request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK);

         request.type=ORDER_TYPE_BUY;

         //--- 2K2>4 8=D>@<0F88 > 70:@KB88

         PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));

         //--- >B?@02:0 70?@>A0

         if(!OrderSend(request,result))

            PrintFormat("OrderSend error %d",GetLastError()); // 5A;8 >B?@028BL 70?@>A =5 C40;>AL, 2K25AB8 :>4 >H81:8

         //--- 8=D>@<0F8O >1 >?5@0F88

         PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);

         //---

        }

     }

   return (true);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---