Semaphore Line

Author: Copyright © 2021, Aleksandr Klapatyuk
Price Data Components
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reached
Miscellaneous
It plays sound alertsIt issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Semaphore Line
ÿþ//+------------------------------------------------------------------+

//|                                               Semaphore Line.mq5 |

//|                           Copyright © 2021, Aleksandr Klapatyuk. |

//|                            https://www.mql5.com/ru/users/sanalex |

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

#property copyright   "Copyright © 2021, Aleksandr Klapatyuk"

#property link        "https://www.mql5.com/ru/users/sanalex"

#property description "Copyright © 2021, Vladimir Karputov"

#property description "http://wmua.ru/slesar/"

#property description "Klymenko Roman (needtome@icloud.com)"

#property description "https://www.mql5.com/ru/users/needtome"

#property description "Copyright © 2021, SEM "

#property description "https://www.mql5.com/ru/users/seliveru"

#property version     "1.026"

//---

#define InpMagic 131889285

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

CPositionInfo  m_position; // trade position object

CTrade         m_trade;    // trading object

CSymbolInfo    m_symbol;   // symbol info object

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

//| ENUM_PROFIT_COMMAND                                              |

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

enum ENUM_FILTER_OFF_ON

  {

   Filte_OFF=0,        // Filter ON

   Filte_ON=1,         // Filter OFF

  };

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

//| ENUM_PROFIT_COMMAND                                              |

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

enum ENUM_PROFIT_COMMAND

  {

   Profit_tpl_b=0,     // Close

   Profit_tpl_s=1,     // Close + Open + '.tpl'

  };

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

//| ENUM_FILTER_COMMAND                                              |

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

enum ENUM_FILTER_COMMAND

  {

   Filter_Off=0,      // OFF

   Filter_Stop=1,     // Filter Line Up:Down

   Filter_All=2,      // Filter Line Up:Down:Middle

  };

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

//| ENUM_TRADE_COMMAND                                               |

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

enum ENUM_TRADE_COMMAND

  {

   Turn_Off=0,        // TURN  OFF

   Close_AllExt=1,    // CloseAll + ExpertRemoves

   Close_All_All=2,   // CloseAll

   CloseAll_Profit=3, // CloseAllProfit

   close_tpl_b=4,     // Close Sell + Open Buy + '.tpl'

   close_tpl_s=5,     // Close Buy + Open Sell + '.tpl'

   tpl=6,             // '.tpl'

   close_all_tpl=7,   // Close Buy's Sell's + '.tpl'

   UpName=8,          // Line UpName

   DownName=9,        // Line DownName

   UpName_s=10,       // Line UpName + Open Sell

   UpName_b=11,       // Line UpName + Open Buy

   DownName_b=12,     // Line DownName + Open Buy

   DownName_s=13,     // Line DownName + Open Sell

   close_buys=14,     // Close All Buy's

   close_sells=15,    // Close All Sell's

   close_all=16,      // Close All Buy's and Sell's

   open_buy=17,       // Open  Buy

   open_sell=18,      // Open  Sell

   close_open_b=19,   // Close Sell + Open Buy

   close_open_s=20,   // Close Buy + Open Sell

   open_buy_sell=21,  // Open  Buy and Sell

  };

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

input group    "---- Balans Parameters ----"

input string   Template                     = "ADX";             // <O H01;>=0(without '.tpl')

input double   TargetProfit                 = 1000000;           // 0;0=A + @81K;L(?@81028BL : 10;0=AC)

input double   TargetLoss                   = 0;                 // 0;0=A - #1KB>:(>B=OBL >B 10;0=A0)

input group    "---- Time command ----"

input datetime InpHoursFrom                 = D'1970.01.01';     // Time

input ENUM_TRADE_COMMAND InpTimeCommand     = tpl;               // Time command:

input string   Template_1                   = "Momentum";        // <O H01;>=0(without '.tpl')

input group    "---- Obj: VLine ----"

input string   InpObjVLineName              = "<O VLine";       // Obj: VLine

input ENUM_TRADE_COMMAND InpVLineCommand    = Turn_Off;          // Obj: command:

input group    "---- Start Stop ----"

input datetime HoursFrom                    = D'1970.01.02';     // 1 @5<O: AB0@B

input datetime HoursTo                      = D'1970.01.01';     // @5<O: AB>?

input datetime HoursFrom2                   = D'1970.01.01';     // 2 @5<O: AB0@B

input group    "---- Time:Start Stop ----"

input int      HoursFrom1                   = 0;                 // @5<O: AB0@B

input int      HoursTo1                     = 24;                // @5<O: AB>?

input group    "---- TP SL Position ----"

input int      InpTakeProfit                = 90000;             // Take Profit("0"-No.5<100)(1.00045-1.00055=1 pips)

input int      InStopLoss                   = 90000;             // Stop Loss("0"-No.5<100)(1.00045-1.00055=1 pips)

input group    "---- Lots Parameters ----"

input double   InpLots1                     = 0.01;              // : Lots 1

input int      InpLots_01                   = 1500;              // In currency >< In points

input double   InpLots2                     = 0.02;              // : Lots 2

input int      InpLots_02                   = 2800;              // In currency >< In points

input double   InpLots3                     = 0.04;              // : Lots 3

input int      InpLots_03                   = 4300;              // In currency >< In points

input double   InpLots4                     = 0.08;              // : Lots 4

input group    "---- Balans Parameters 2 ----"

input double   TargetProfit_1               = 1000000;           // 1I0O @81K;L 0;NB5 '0'--> 5;L7O

input double   TargetLoss_1                 = 1000000;           // 1I89 #1KB>: 0;NB5 '0'--> 5;L7O

input group    "---- In currency><In points ---"

input double   InpPoints                    = 10000;             // :Gold=0.133/:JPY=100/:USD=10000

input bool     InpExcPoi                    = false;             // In currency= false; In points= true;

input group    "---- TP SL In currency ----"

input double   InpTProfit                   = 40000;             // In currency TP >< In points TP

input double   InpStopLoss                  = 1000000;           // In currency SL >< In points SL

input ENUM_PROFIT_COMMAND InpProfitCommand  = Profit_tpl_b;      // TP SL: command Close

input string   Template_2                   = "Momentum";        // <O H01;>=0(without '.tpl')

input group    "---- Trailing Line: ----"

input bool     InpStart                     = false;             // Start Trailing Line

input ENUM_FILTER_OFF_ON FilterTrailUp      = Filte_OFF;         // Filter OFF: ON:

input string   InpObjUpName                 = "<O 8=88 Up";    // Obj: Up (Trailing Horizontal Line) or Trend Line)

input ENUM_TRADE_COMMAND InpTradeCommand    = close_sells;       // Obj:  command: UP

input ENUM_FILTER_OFF_ON FilterTrailLow     = Filte_OFF;         // Filter OFF: ON:

input string   InpObjDownName               = "<O 8=88 Down";  // Obj: Down (Trailing Horizontal Line) or Trend Line)

input ENUM_TRADE_COMMAND InTradeCommand     = close_buys;        // Obj:  command: DOWN

input double   InpObjTrailingStop           = 0;                 // Obj: Trailing Stop (distance from price to object, in pips)

input double   InpObjTrailingStep           = 0;                 // Obj: Trailing Step, in pips (1.00045-1.00055=1 pips)

input group    "---- Indicators: UP ----"

input ENUM_FILTER_OFF_ON FilterIndicUp      = Filte_OFF;         // Filter OFF: ON:

input string   short_name1                  = "<O =48:0B>@0";  // Name Indicators "UP"

input ENUM_TIMEFRAMES Periodbuy             = PERIOD_CURRENT;    // Period Indicators

input bool     InpIndicators1               = false;             // Indicators: Start (true)

input ENUM_TRADE_COMMAND InpTradeCommandY1  = Turn_Off;          // Trade command: ( Sell )

input ENUM_TRADE_COMMAND InpTradeCommandU1  = open_buy;          // Trade command: ( Buy )

input group    "---- Indicators: DOWN ----"

input ENUM_FILTER_OFF_ON FilterIndicLow     = Filte_OFF;         // Filter OFF: ON:

input string   short_name                   = "<O =48:0B>@0";  // Name Indicators "DOWN"

input ENUM_TIMEFRAMES Periodsell            = PERIOD_CURRENT;    // Period Indicators

input bool     InpIndicators                = false;             // Indicators: Start (true)

input ENUM_TRADE_COMMAND InpTradeCommandY   = open_sell;         // Trade command: ( Sell )

input ENUM_TRADE_COMMAND InpTradeCommandU   = Turn_Off;          // Trade command: ( Buy )

input group    "---- Time ChartIndicatorAdd ----"

input string   InpIndi_name                 = "<O =48:0B>@0";  // Installation Indicator Name

input datetime InpHoursFrom_1               = D'1970.01.01';     // Time Installation Indicator

input group    "---- Indicator Delete ----"

input string   Inpshort_name                = "<O =48:0B>@0";  // Delete Indicator Name

input string   Inpshort_name_1              = "<O =48:0B>@0";  // Delete Indicator Name

input bool     Inpres                       = false;             // Delete All Indicators

input group    "---- Trade Line Name 1 ----"

input ENUM_FILTER_OFF_ON FilterTradeUp1     = Filte_OFF;         // Filter OFF: ON:

input ENUM_TIMEFRAMES Period_1              = PERIOD_CURRENT;    // Period

input ENUM_TRADE_COMMAND InpAverage_x       = open_buy;          // Trade command: UP

input string   InpNameAverage              = "Line Name 1";      // Line Name 1 (Horizontal Line or Trend Line)

input ENUM_TRADE_COMMAND InpAverage         = open_sell;         // Trade command: DOWN

input bool     InpDelLine                   = false;             // Delete (Horizontal Line or Trend Line)

input group    "---- Trade Line Name 2 ----"

input ENUM_FILTER_OFF_ON FilterTradeUp2     = Filte_OFF;         // Filter OFF: ON:

input ENUM_TIMEFRAMES Period_2              = PERIOD_CURRENT;    // Period

input ENUM_TRADE_COMMAND InpAverage0_0      = open_buy;          // Trade command: UP

input string   InpNameAverage0              = "Line Name 2";     // Line Name 2 (Horizontal Line or Trend Line)

input ENUM_TRADE_COMMAND InpAverage0        = open_sell;         // Trade command: DOWN

input bool     InpDelLine_0                 = false;             // Delete (Horizontal Line or Trend Line)

input group    "---- Trade Line Name 3 ----"

input ENUM_FILTER_OFF_ON FilterTradeLow3    = Filte_OFF;         // Filter OFF: ON:

input ENUM_TIMEFRAMES Period_3              = PERIOD_CURRENT;    // Period

input ENUM_TRADE_COMMAND InpAverage1_1      = open_buy;          // Trade command: UP

input string   InpNameAverage1              = "Line Name 3";     // Line Name 3 (Horizontal Line or Trend Line)

input ENUM_TRADE_COMMAND InpAverage1        = open_sell;         // Trade command: DOWN

input bool     InpDelLine_1                 = false;             // Delete (Horizontal Line or Trend Line)

input group    "---- Trade Line Name 4 ----"

input ENUM_FILTER_OFF_ON FilterTradeLow4    = Filte_OFF;         // Filter OFF: ON:

input ENUM_TIMEFRAMES Period_4              = PERIOD_CURRENT;    // Period

input ENUM_TRADE_COMMAND InpAverage2_2      = open_buy;          // Trade command: UP

input string   InpNameAverage2              = "Line Name 4";     // Line Name 4 (Horizontal Line or Trend Line)

input ENUM_TRADE_COMMAND InpAverage2        = open_sell;         // Trade command: DOWN

input bool     InpDelLine_2                 = false;             // Delete (Horizontal Line or Trend Line)

input group    "---- Button: ----"

input ENUM_TRADE_COMMAND InpTradeCommandBut = open_buy;          // Obj(BUY):  command:Button: UP

input ENUM_TRADE_COMMAND InTradeCommandBut  = open_sell;         // Obj(SELL):  command:Button: DOWN

input int      TrailingStop_STOP_LEVEL      = 36;                // Button: Trailing Stop LEVEL

input group    "---- FILTER:Command ----"

input ENUM_FILTER_COMMAND ObjFilterStopUp   = Filter_Off;        // Filter

input string   InpNameupper                 = "<O 8=88 Upper"; // FILTER Upper (Horizontal Line or Trend Line)

input ENUM_FILTER_COMMAND ObjFilterStopLow  = Filter_Off;        // Filter

input string   InpNamelower                 = "<O 8=88 Lower"; // FILTER Lower (Horizontal Line or Trend Line)

input group    "---- :(+): Line:Middle ----"

input string   InpNameaverageUp             = "<O Middle:Up";   // FILTER Up (Horizontal Line or Trend Line)

input string   InpNameaverageLow            = "<O Middle:Low";  // FILTER Low (Horizontal Line or Trend Line)

input group    "---- Revers Buy><Sell ----"

input bool     ObjRevers                    = false;             // Revers

//---

datetime InpHoursTo=D'1970.01.01';           // @5<O: AB>?

datetime InpHoursTo_1=D'1970.01.01';         // @5<O: AB>?

datetime HoursTo2=D'1970.01.01';             // @5<O: AB>?

uint     maxLimits=1;                        // >;-2> >78F88 B:@KBL 2 >4=C AB>@>=C

bool     InpPrintLog=false;                  // Print log

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

int      handle_iCustom;                     // variable for storing the handle of the iStochastic indicator

int      handle_iCustom1;                    // variable for storing the handle of the iStochastic indicator

int      indicator_handle=INVALID_HANDLE;    //

int      ticks_to_close_Exp=1;               // :>;8G5AB2> B8:>2 4> A=OB8O M:A?5@B0

int      ticks_to_close=1;                   // :>;8G5AB2> B8:>2

int      ticks_to_close_1=1;                 // :>;8G5AB2> B8:>2

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

ENUM_TIMEFRAMES TimeFrame;                   // Change TimeFrame - Current = dont changed

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

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

ushort   InpObjTrailingFrequency=10;         // Obj: Trailing, in seconds (< "10" -> only on a new bar)

ushort   InpSignalsFrequency=1;              // Search signals, in seconds (min value "1")

string   txt;                                //

//---

bool     ExtTurnOff              = false;    //

bool     ExtUpName               = false;    //

bool     ExtDownName             = false;    //

bool     ExtNeedCloseBuy         = false;    //

bool     ExtNeedCloseSell        = false;    //

bool     ExtNeedCloseAll         = false;    //

bool     ExtNeedOpenBuy          = false;    //

bool     ExtNeedOpenSell         = false;    //

bool     ExtNeedOpenBuySell      = false;    //

bool     ExtCloseAllExt          = false;    //

bool     ExtCloseAll             = false;    //

bool     ExtCloseAllProfit       = false;    //

bool     ExtFilter_Off           = false;    //

bool     ExtFilter_Stop          = false;    //

bool     ExtFilter_All           = false;    //

bool     ExFilte_OFF             = false;    //

bool     ExFilte_ON              = false;    //

//---

datetime ExtPrevBarsY            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBarsY1           = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastUpper            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevUpper            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastAverage          = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevAverage          = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastLower            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevLower            = 0;        // "0" -> D'1970.01.01 00:00';

//---

datetime ExtLastINAverage        = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBars             = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastINAverage0       = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBars0            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastINAverage1       = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBars1            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastINAverage2       = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBars2            = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastSignalsRx        = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBarsRx           = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtLastSignalsSx        = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtPrevBarsSx           = 0;        // "0" -> D'1970.01.01 00:00';

datetime ExtVLinetime            = 0;        // "0" -> D'1970.01.01 00:00';

//---

double   m_obj_trailing_stop     = 0.0;      // Obj: Trailing Stop   -> double

double   m_obj_trailing_step     = 0.0;      // Obj: Trailing Step   -> double

datetime m_obj_last_trailing     = 0;        // "0" -> D'1970.01.01 00:00';

datetime m_prev_bars             = 0;        // "0" -> D'1970.01.01 00:00';

double   m_obj_up_price          = 0.0;      //

double   m_obj_down_price        = 0.0;      //

double   m_Point                 = 0.0;      //

double   m_stop_loss             = 0.0;      // Stop Loss   -> double

double   m_take_profit           = 0.0;      // Take Profit -> double

//---

string   m_name[]= {"BUY","SELL","BUY_Close","SELL_Close","CLOSE_ALL","Trailing","without '.tpl'"};

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

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

      return(INIT_FAILED);;

   RefreshRates();

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;

   m_adjusted_point=m_symbol.Point()*digits_adjust;

//--- set default deviation for trading in adjusted points

   m_stop_loss           = InStopLoss*m_adjusted_point;

   m_take_profit         = InpTakeProfit*m_adjusted_point;

   m_obj_trailing_stop   = InpObjTrailingStop*m_adjusted_point;

   m_obj_trailing_step   = InpObjTrailingStep*m_adjusted_point;

   m_Point               = InpPoints*m_adjusted_point;

//--- set default deviation for trading in adjusted points

   m_trade.SetDeviationInPoints(3*digits_adjust);

//---

   int _y=100;

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

     {

      ButtonCreate(m_name[i],5,_y,130,20,10);

      _y=_y+25;

     }

//--- create handle of the indicator handle_iCustom

   if(InpIndicators)

     {

      handle_iCustom=iCustom(m_symbol.Name(),Periodsell,short_name);

      //--- if the handle is not created

      if(handle_iCustom==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the handle_iCustom indicator for the symbol %s/%s, error code %d",

                     m_symbol.Name(),

                     EnumToString(Period()),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

     }

//--- create handle of the indicator handle_iCustom

   if(InpIndicators1)

     {

      handle_iCustom1=iCustom(m_symbol.Name(),Periodbuy,short_name1);

      //--- if the handle is not created

      if(handle_iCustom1==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the handle_iCustom indicator for the symbol %s/%s, error code %d",

                     m_symbol.Name(),

                     EnumToString(Period()),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

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

     {

      ObjectDelete(0,Symbol()+m_name[i]);

     };

   EventKillTimer();

//---

   Print(TimeCurrent(),": ",__FUNCTION__," reason code = ",reason);

   Comment("");

   ObjectsDeleteAll(0,"cm");

//---

  }

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

//| Timer function                                                   |

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

void OnTick(void)

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return;

//---

   if(m_position.Select(Symbol()))

     {

      ProfitOnTick();

      ProfitOnTick1();

      ProfitOnTick2();

     }

   if(ProfitTarget())

     {

      return;

     }

   if(ProfitTarget_1())

     {

      return;

     }

//---

   if(m_symbol.Name(Symbol()))

     {

      TimeSession();

      SessionTime();

      Obj_Obj();

     }

//---

   if((HoursFrom<HoursTo && TimeLocal()>=HoursFrom && TimeLocal()<HoursTo) ||

      (HoursFrom>HoursTo && (TimeLocal()<HoursTo || TimeLocal()>=HoursFrom)) ||

      (HoursFrom2<HoursTo2 && TimeLocal()>=HoursFrom2 && TimeLocal()<HoursTo2) ||

      (HoursFrom2>HoursTo2 && (TimeLocal()<HoursTo2 || TimeLocal()>=HoursFrom2)))

     {

      MqlDateTime currTime;

      TimeLocal(currTime);

      int hour0 = currTime.hour;

      if((HoursFrom1 < HoursTo1 && hour0 >= HoursFrom1 && hour0 < HoursTo1) ||

         (HoursFrom1 > HoursTo1 && (hour0 < HoursTo1 || hour0 >= HoursFrom1)))

        {

         ExpertOnTick();

        }

     }

  }

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

//| Expert tick function                                             |

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

void ExpertOnTick(void)

  {

   ButtonOnTick();

   Optimized();

   OnTickInpUpper();

   OnTickInpLower();

   OnOffFilter();

   if(InpStart)

     {

      OnTickObjTrailing();

     }

   switch(ObjFilterStopUp)

     {

      case Filter_Off:

         ExtFilter_Off=true;

         FilterOnTickUp();

         break;

     }

   switch(ObjFilterStopLow)

     {

      case Filter_Off:

         ExtFilter_Off=true;

         FilterOnTickLow();

         break;

     }

//---

  }

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

//| Expert tick function                                             |

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

void FilterOnTickUp(void)

  {

   TrendOpened();

   TrendOpened0();

   OnTickInpNameRx();

   if(InpIndicators1 && SearchTradingSignals1())

     {

      return;

     }

//---

  }

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

//| Expert tick function                                             |

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

void FilterOnTickLow(void)

  {

   TrendOpened1();

   TrendOpened2();

   OnTickInpNameSx();

   if(InpIndicators && SearchTradingSignals())

     {

      return;

     }

//---

  }

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

//| Expert tick function                                             |

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

void OnOffFilter(void)

  {

   switch(FilterTrailUp)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         OnTickInpNameRx();

         break;

     }

   switch(FilterTrailLow)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         OnTickInpNameSx();

         break;

     }

   switch(FilterIndicUp)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         if(InpIndicators1 && SearchTradingSignals1())

           {

            return;

           }

         break;

     }

   switch(FilterIndicLow)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         if(InpIndicators && SearchTradingSignals())

           {

            return;

           }

         break;

     }

   switch(FilterTradeUp1)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         TrendOpened();

         break;

     }

   switch(FilterTradeUp2)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         TrendOpened0();

         break;

     }

   switch(FilterTradeLow3)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         TrendOpened1();

         break;

     }

   switch(FilterTradeLow4)

     {

      case Filte_OFF:

         ExFilte_OFF=true;

         break;

      case Filte_ON:

         ExFilte_ON=true;

         TrendOpened2();

         break;

     }

//---

  }

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

//| start function                                                   |

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

void CloseAll(void)

  {

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

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

        {

         ClosePosition(m_position.Symbol()); // close a position by the specified symbo

        }

  }

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

//| start function                                                   |

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

void CloseAllProfit(void)

  {

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

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

        {

         if(m_position.Commission()+m_position.Swap()+m_position.Profit()>0.0)

            ClosePosition(m_position.Symbol()); // close a position by the specified symbo

        }

  }

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

//| start function                                                   |

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

void CloseAllLoss(void)

  {

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

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

        {

         if(m_position.Commission()+m_position.Swap()+m_position.Profit()<0.0)

            ClosePosition(m_position.Symbol());  // close a position by the specified symbo

        }

  }

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

//| Close selected position                                          |

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

void ClosePosition(const string symbol)

  {

   if(InitTrade(symbol))

      m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbo

   PlaySound("ok.wav");

  }

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

//| Init trade object                                                |

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

bool InitTrade(const string symbol)

  {

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

      return(false);

//---

   if(IsFillingTypeAllowed(symbol,SYMBOL_FILLING_FOK))

      m_trade.SetTypeFilling(ORDER_FILLING_FOK);

   else

      if(IsFillingTypeAllowed(symbol,SYMBOL_FILLING_IOC))

         m_trade.SetTypeFilling(ORDER_FILLING_IOC);

      else

         m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

//---

   return(true);

//---

  }

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

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

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

bool IsFillingTypeAllowed(string symbol,int fill_type)

  {

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

   int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);

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

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

  }

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

//| start function                                                   |

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

void ExpertRemoves(void)

  {

   static int tick_counter=0;

//---

   tick_counter++;

   Comment("\n> 2K3@C7:8 M:A?5@B0 ",__FILE__," >AB0;>AL ",

           (ticks_to_close_Exp-tick_counter)," B8:>2 ");

//--- 4>

   if(tick_counter>=ticks_to_close_Exp)

     {

      ExpertRemove();

      Print(TimeCurrent(),": ",__FUNCTION__," M:A?5@B 1C45B 2K3@C65=");

     }

   Print("tick_counter = ",tick_counter);

//---

  }

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

//| start function                                                   |

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

void DeleteChart(void)

  {

   long currChart,prevChart=ChartFirst();

   int i=0,limit=100;

   bool errTemplate;

   while(i<limit)

     {

      currChart=ChartNext(prevChart);

      if(TimeFrame!=PERIOD_CURRENT)

        {

         ChartSetSymbolPeriod(prevChart,ChartSymbol(prevChart),TimeFrame);

        }

      errTemplate=ChartApplyTemplate(prevChart,Template+".tpl");

      if(!errTemplate)

        {

         Print("Error ",ChartSymbol(prevChart),"-> ",GetLastError());

        }

      if(currChart<0)

         break;

      Print(i,ChartSymbol(currChart)," ID =",currChart);

      prevChart=currChart;

      i++;

     }

  }

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

//| start function                                                   |

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

void DeleteChart_1(void)

  {

   long prevChart=0;

   bool errTemplate;

   if(TimeFrame!=PERIOD_CURRENT)

     {

      ChartSetSymbolPeriod(prevChart,ChartSymbol(prevChart),TimeFrame);

     }

   errTemplate=ChartApplyTemplate(prevChart,Template_1+".tpl");

   if(!errTemplate)

     {

      Print("Error ",ChartSymbol(prevChart),"-> ",GetLastError());

     }

  }

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

//| start function                                                   |

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

void DeleteChart_2(void)

  {

   long prevChart=0;

   bool errTemplate;

   if(TimeFrame!=PERIOD_CURRENT)

     {

      ChartSetSymbolPeriod(prevChart,ChartSymbol(prevChart),TimeFrame);

     }

   errTemplate=ChartApplyTemplate(prevChart,Template_2+".tpl");

   if(!errTemplate)

     {

      Print("Error ",ChartSymbol(prevChart),"-> ",GetLastError());

     }

  }

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

//| start function                                                   |

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

void DeletePending(void)

  {

   Print("!@01>B0;0 DC=:F8O 70:@K205< 2A5 2KAB02;5==K5 >@45@0");

//----

   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);

         bool _Close=true;

         OrderCloseByTicket(_Close,symbol,ticket,Deviation_);

        }

      if(!OrdersTotal())

         break;

      Sleep(SLEEPTIME*1000);

     }

//----

  }

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

//| 0:@K205< 4;8==CN ?>78F8N                                        |

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

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(): ",m_trade.ResultComment());

      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(): ",m_trade.ResultComment());

         return(false);

        }

//----

   return(true);

  }

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

//| Check for long position closing                                  |

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

bool ProfitTarget(void)

  {

   bool res=false;

   if(AccountInfoDouble(ACCOUNT_EQUITY)<=TargetLoss ||

      AccountInfoDouble(ACCOUNT_EQUITY)>=TargetProfit)

     {

      CloseAllProfit();

      CloseAllLoss();

      CloseAll();

      DeletePending();

      Sleep(SLEEPTIME*1000);

      CloseAllProfit();

      CloseAllLoss();

      Sleep(SLEEPTIME*1000);

      CloseAll();

      DeletePending();

      ExpertRemoves();

      DeleteChart();

      PlaySound("expert.wav");

      res=true;

     }

//--- result

   return(res);

  }

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

//| Check ProfitTarget closing                                       |

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

bool ProfitTarget_1(void)

  {

   bool res=false;

   double growth=0.00;

   static double bal=0.00;

   bal=AccountInfoDouble(ACCOUNT_BALANCE);

   growth=growth+bal-AccountInfoDouble(ACCOUNT_EQUITY);

   if(growth<=-TargetProfit_1 || growth>=TargetLoss_1)

     {

      CloseAllProfit();

      CloseAllLoss();

      CloseAll();

      DeletePending();

      Sleep(SLEEPTIME*1000);

      CloseAllProfit();

      CloseAllLoss();

      Sleep(SLEEPTIME*1000);

      CloseAll();

      DeletePending();

      res=true;

     }

//--- result

   return(res);

  }

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

//| Check for long position closing                                  |

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

bool ProfitOnTick(void)

  {

   bool res=false;

   double PROFIT_BUY=0.00;

   double PROFIT_SELL=0.00;

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

     {

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

      if(position_GetSymbol==m_symbol.Name())

        {

         if(!InpExcPoi)

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);

              }

            else

              {

               PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);

              }

           }

         if(InpExcPoi)

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               PROFIT_BUY=PROFIT_BUY+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                      SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

              }

            else

              {

               PROFIT_SELL=PROFIT_SELL+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                        SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

              }

           }

        }

     }

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

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

         if(m_position.Symbol()==m_symbol.Name())

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if(PROFIT_BUY<-InpStopLoss || PROFIT_BUY>=InpTProfit) // if the profit

                  switch(InpProfitCommand)

                    {

                     case  Profit_tpl_b:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        break;

                     case  Profit_tpl_s:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        Alert("DeleteChart ''"+Template_2+"' on "+m_symbol.Name());

                        DeleteChart_2();

                        break;

                    }

              }

            res=true;

           }

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

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

         if(m_position.Symbol()==m_symbol.Name())

           {

            if(m_position.PositionType()==POSITION_TYPE_SELL)

              {

               if(PROFIT_SELL<-InpStopLoss || PROFIT_SELL>=InpTProfit) // if the profit

                  switch(InpProfitCommand)

                    {

                     case  Profit_tpl_b:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        break;

                     case  Profit_tpl_s:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        Alert("DeleteChart ''"+Template_2+"' on "+m_symbol.Name());

                        DeleteChart_2();

                        break;

                    }

              }

            res=true;

           }

//--- result

   return(res);

  }

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

//| Check for long position closing     m_take_profit                |

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

void ProfitOnTick1(void)

  {

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

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

         if(m_position.Symbol()==m_symbol.Name())

           {

            double price_current = m_position.PriceCurrent();

            double price_open    = m_position.PriceOpen();

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if((price_current-price_open)>=m_take_profit) // if the profit

                  ClosePosition(m_position.Symbol()); // close a position by the specified symbo

              }

            else

               if(m_position.PositionType()==POSITION_TYPE_SELL)

                 {

                  if((price_open-price_current)>=m_take_profit) // if the profit

                     ClosePosition(m_position.Symbol()); // close a position by the specified symbo

                 }

           }

//---

  }

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

//| Check for long position closing     m_stop_loss                  |

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

void ProfitOnTick2(void)

  {

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

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

         if(m_position.Symbol()==m_symbol.Name())

           {

            double price_current = m_position.PriceCurrent();

            double price_open    = m_position.PriceOpen();

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if((price_current-price_open)<=-m_stop_loss) // if the loss

                  ClosePosition(m_position.Symbol()); // close a position by the specified symbo

              }

            else

               if(m_position.PositionType()==POSITION_TYPE_SELL)

                 {

                  if((price_open-price_current)<=-m_stop_loss) // if the loss

                     ClosePosition(m_position.Symbol()); // close a position by the specified symbo

                 }

           }

//---

  }

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

//| Calculate optimal lot size                                       |

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

double OptimizedBuy(void)

  {

   double PROFIT_BUY=0.00;

   double PROFIT_SELL=0.00;

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

     {

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

      if(position_GetSymbol==m_symbol.Name())

        {

         if(!InpExcPoi)

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);

              }

            else

              {

               PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);

              }

           }

         if(InpExcPoi)

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               PROFIT_BUY=PROFIT_BUY+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                      SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

              }

            else

              {

               PROFIT_SELL=PROFIT_SELL+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                        SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

              }

           }

        }

     }

   double Lots=InpLots1;

   double ab=PROFIT_BUY;

   if(ab<-1 && ab>=-InpLots_01)

      Lots=InpLots1;

   if(ab<-InpLots_01 && ab>=-InpLots_02)

      Lots=InpLots2;

   if(ab<-InpLots_02 && ab>=-InpLots_03)

      Lots=InpLots3;

   if(ab<-InpLots_03)

      Lots=InpLots4;

//--- return trading volume

   return(Lots);

  }

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

//| Calculate optimal lot size                                       |

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

double OptimizedSell(void)

  {

   double PROFIT_BUY=0.00;

   double PROFIT_SELL=0.00;

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

     {

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

      if(position_GetSymbol==m_symbol.Name())

        {

         if(!InpExcPoi)

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);

              }

            else

              {

               PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);

              }

           }

         if(InpExcPoi)

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               PROFIT_BUY=PROFIT_BUY+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                      SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

              }

            else

              {

               PROFIT_SELL=PROFIT_SELL+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                        SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

              }

           }

        }

     }

   double Lots=InpLots1;

   double ab=PROFIT_SELL;

   if(ab<-1 && ab>=-InpLots_01)

      Lots=InpLots1;

   if(ab<-InpLots_01 && ab>=-InpLots_02)

      Lots=InpLots2;

   if(ab<-InpLots_02 && ab>=-InpLots_03)

      Lots=InpLots3;

   if(ab<-InpLots_03)

      Lots=InpLots4;

//--- return trading volume

   return(Lots);

  }

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

//| Calculate optimal lot size                                       |

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

void Optimized(void)

  {

   double PROFIT_BUY=0.00;

   double PROFIT_SELL=0.00;

   double PROFIT_BUY_1=0.00;

   double PROFIT_SELL_1=0.00;

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

     {

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

      if(position_GetSymbol==m_symbol.Name())

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);

           }

         else

           {

            PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);

           }

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            PROFIT_BUY_1=PROFIT_BUY_1+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                       SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

           }

         else

           {

            PROFIT_SELL_1=PROFIT_SELL_1+(PositionGetDouble(POSITION_PROFIT)/PositionGetDouble(POSITION_VOLUME)/

                                         SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)*m_Point);

           }

        }

     }

   StringConcatenate(txt,"SELL 2 ?C=:B0E=",DoubleToString(PROFIT_SELL_1,2));

   DrawLABEL(2,"cm 2",txt,5,30,clrDarkOrange,ANCHOR_RIGHT);

   StringConcatenate(txt,"SELL 2 20;NB5=",DoubleToString(PROFIT_SELL,2));

   DrawLABEL(2,"cm 3",txt,5,45,clrDarkOrange,ANCHOR_RIGHT);

   StringConcatenate(txt,"BUY 2 ?C=:B0E=",DoubleToString(PROFIT_BUY_1,2));

   DrawLABEL(2,"cm 4",txt,5,75,clrMediumSeaGreen,ANCHOR_RIGHT);

   StringConcatenate(txt,"BUY 2 20;NB5=",DoubleToString(PROFIT_BUY,2));

   DrawLABEL(2,"cm 5",txt,5,90,clrMediumSeaGreen,ANCHOR_RIGHT);

   StringConcatenate(txt,"!@54AB20=",DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY),2));

   DrawLABEL(3,"cm 6",txt,5,25,clrLime,ANCHOR_RIGHT);

  }

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

//|   DrawLABEL                                                      |

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

void DrawLABEL(int c,string name,string text,int X,int Y,color clr,int ANCHOR=ANCHOR_LEFT,int FONTSIZE=8)

  {

   if(ObjectFind(0,name)==-1)

     {

      ObjectCreate(0,name,OBJ_LABEL,0,0,0);

      ObjectSetInteger(0,name,OBJPROP_CORNER,c);

      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,X);

      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,Y);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);

      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FONTSIZE);

      ObjectSetString(0,name,OBJPROP_FONT,"Arial");

      ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR);

     }

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetInteger(0,name,OBJPROP_COLOR,clr);

  }

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

//| Check for long position closing                                  |

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

bool LongObjClosed(void)

  {

   bool res=false;

//--- should it be closed?

   if(!ObjRevers)

     {

      if(ExtNeedCloseBuy)

        {

         //--- close position

         double level;

         if(FreezeStopsLevels(level))

            ClosePositions(POSITION_TYPE_BUY,level);

         //--- processed and cannot be modified

         res=true;

        }

      else

         ExtNeedCloseBuy=false;

     }

//--- result

   if(ObjRevers)

     {

      if(ExtNeedCloseBuy)

        {

         //--- close position

         double level;

         if(FreezeStopsLevels(level))

            ClosePositions(POSITION_TYPE_SELL,level);

         //--- processed and cannot be modified

         res=true;

        }

      else

         ExtNeedCloseBuy=false;

     }

//--- result

   return(res);

  }

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

//| Check for short position closing                                 |

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

bool ShortObjClosed(void)

  {

   bool res=false;

//--- should it be closed?

   if(!ObjRevers)

     {

      if(ExtNeedCloseSell)

        {

         //--- close position

         double level;

         if(FreezeStopsLevels(level))

            ClosePositions(POSITION_TYPE_SELL,level);

         //--- processed and cannot be modified

         res=true;

        }

      else

         ExtNeedCloseSell=false;

     }

//--- result

   if(ObjRevers)

     {

      if(ExtNeedCloseSell)

        {

         //--- close position

         double level;

         if(FreezeStopsLevels(level))

            ClosePositions(POSITION_TYPE_BUY,level);

         //--- processed and cannot be modified

         res=true;

        }

      else

         ExtNeedCloseSell=false;

     }

//--- result

   return(res);

  }

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

//| Check for short position closing                                 |

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

bool LongShortObjClosed(void)

  {

   bool res=false;

//--- should it be closed?

   if(ExtNeedCloseAll)

     {

      //--- close position

      double level;

      if(FreezeStopsLevels(level))

         ClosePositions(POSITION_TYPE_BUY,level);

      if(FreezeStopsLevels(level))

         ClosePositions(POSITION_TYPE_SELL,level);

      //--- processed and cannot be modified

      res=true;

     }

   else

      ExtNeedCloseAll=false;

//--- result

   return(res);

  }

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

//| Check for long position opening                                  |

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

bool LongObjOpened(void)

  {

   bool res=false;

//--- check for long position (BUY) possibility

   if(!ObjRevers)

     {

      if(ExtNeedOpenBuy)

        {

         double price=m_symbol.Ask();

         for(uint i=0; i<maxLimits; i++)

           {

            //--- open position

            if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_BUY,OptimizedBuy(),price,0.0,0.0))

               printf("Position by %s to be opened",m_symbol.Name());

            else

              {

               printf("Error opening BUY position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());

               printf("Open parameters : price=%f,TP=%f",price,0.0);

              }

            res=true;

           }

         ExtNeedOpenBuy=false;

         //--- in any case we must exit from expert

        }

     }

//--- check for long position (BUY) possibility

   if(ObjRevers)

      if(ExtNeedOpenBuy)

        {

           {

            double price=m_symbol.Bid();

            for(uint y=0; y<maxLimits; y++)

              {

               if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_SELL,OptimizedSell(),price,0.0,0.0))

                  printf("Position by %s to be opened",m_symbol.Name());

               else

                 {

                  printf("Error opening SELL position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());

                  printf("Open parameters : price=%f,TP=%f",price,0.0);

                 }

               res=true;

              }

            ExtNeedOpenBuy=false;

            //--- in any case we must exit from expert

           }

        }

//--- result

   return(res);

  }

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

//| Check for short position opening                                 |

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

bool ShortObjOpened(void)

  {

   bool res=false;

//--- check for short position (SELL) possibility

   if(!ObjRevers)

     {

      if(ExtNeedOpenSell)

        {

         double price=m_symbol.Bid();

         for(uint y=0; y<maxLimits; y++)

           {

            if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_SELL,OptimizedSell(),price,0.0,0.0))

               printf("Position by %s to be opened",m_symbol.Name());

            else

              {

               printf("Error opening SELL position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());

               printf("Open parameters : price=%f,TP=%f",price,0.0);

              }

            res=true;

           }

         ExtNeedOpenSell=false;

         //--- in any case we must exit from expert

        }

     }

//--- check for short position (SELL) possibility

   if(ObjRevers)

     {

      if(ExtNeedOpenSell)

        {

         double price=m_symbol.Ask();

         for(uint i=0; i<maxLimits; i++)

           {

            //--- open position

            if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_BUY,OptimizedBuy(),price,0.0,0.0))

               printf("Position by %s to be opened",m_symbol.Name());

            else

              {

               printf("Error opening BUY position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());

               printf("Open parameters : price=%f,TP=%f",price,0.0);

              }

            res=true;

           }

         ExtNeedOpenSell=false;

         //--- in any case we must exit from expert

        }

     }

//--- result

   return(res);

  }

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

//| Check for short position opening                                 |

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

bool LongShortObjOpened(void)

  {

   bool res=false;

//--- check for short position (BUY) (SELL) possibility

   if(ExtNeedOpenBuySell)

     {

      double price=m_symbol.Ask();

      for(uint i=0; i<maxLimits; i++)

        {

         //--- open position

         if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_BUY,OptimizedBuy(),price,0.0,0.0))

            printf("Position by %s to be opened",m_symbol.Name());

         else

           {

            printf("Error opening BUY position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());

            printf("Open parameters : price=%f,TP=%f",price,0.0);

           }

         res=true;

        }

      double price0=m_symbol.Bid();

      for(uint y=0; y<maxLimits; y++)

        {

         if(m_trade.PositionOpen(m_symbol.Name(),ORDER_TYPE_SELL,OptimizedSell(),price0,0.0,0.0))

            printf("Position by %s to be opened",m_symbol.Name());

         else

           {

            printf("Error opening SELL position by %s : '%s'",m_symbol.Name(),m_trade.ResultComment());

            printf("Open parameters : price=%f,TP=%f",price0,0.0);

           }

         res=true;

        }

      ExtNeedOpenBuySell=false;

      //--- in any case we must exit from expert

     }

//--- result

   return(res);

  }

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

//| Expert tick function                                             |

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

void OnTickObjTrailing(void)

  {

//---

   if(InpObjTrailingFrequency>=10) // trailing no more than once every 10 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-m_obj_last_trailing>10)

        {

         if(RefreshRates())

            ObjTrailing();

         else

            return;

         m_obj_last_trailing=time_current;

        }

     }

//--- we work only at the time of the birth of new bar

   datetime time_0=iTime(m_symbol.Name(),Period(),0);

   if(time_0==m_prev_bars)

      return;

   m_prev_bars=time_0;

   if(InpObjTrailingFrequency<10) // trailing only at the time of the birth of new bar

     {

      if(RefreshRates())

         ObjTrailing();

     }

  }

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

//| Object Trailing                                                  |

//|   InpObjTrailingStop: min distance from price to object          |

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

void ObjTrailing(void)

  {

   if(InpObjTrailingStop==0)

      return;

   double new_up_price        = 0.0;   //

   double new_down_price      = 0.0;   //

   double current_up_price    = ObjectGetDouble(0,InpObjUpName,OBJPROP_PRICE);

   double current_down_price  = ObjectGetDouble(0,InpObjDownName,OBJPROP_PRICE);

   if(current_up_price>0.0)

     {

      if(current_up_price>(m_symbol.Ask()+(m_obj_trailing_stop+m_obj_trailing_step)))

        {

         m_obj_up_price=m_symbol.NormalizePrice(m_symbol.Ask()+m_obj_trailing_stop);

         HLineMove(0,InpObjUpName,m_obj_up_price);

        }

     }

   if(current_down_price>0.0)

     {

      if(current_down_price<m_symbol.Bid()-(m_obj_trailing_stop+m_obj_trailing_step))

        {

         m_obj_down_price=m_symbol.NormalizePrice(m_symbol.Bid()-m_obj_trailing_stop);

         HLineMove(0,InpObjDownName,m_obj_down_price);

        }

     }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      if(InpPrintLog)

         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");

      return(false);

     }

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

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

     {

      if(InpPrintLog)

         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");

      return(false);

     }

//---

   return(true);

  }

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

//| Move horizontal line                                             |

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

bool HLineMove(const long   chart_ID=0,   // chart's ID

               const string name="HLine", // line name

               double       price=0)      // line price

  {

//--- if the line price is not set, move it to the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move a horizontal line

   if(!ObjectMove(chart_ID,name,0,0,price))

     {

      Print(__FUNCTION__,

            ": failed to move the horizontal line! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Check Freeze and Stops levels                                    |

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

bool FreezeStopsLevels(double &level)

  {

//--- check Freeze and Stops levels

   if(!RefreshRates() || !m_symbol.Refresh())

      return(false);

//--- FreezeLevel -> for pending order and modification

   double freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();

   if(freeze_level==0.0)

      freeze_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;

//--- StopsLevel -> for TakeProfit and StopLoss

   double stop_level=m_symbol.StopsLevel()*m_symbol.Point();

   if(stop_level==0.0)

      stop_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;

   if(freeze_level<=0.0 || stop_level<=0.0)

      return(false);

   level=(freeze_level>stop_level)?freeze_level:stop_level;

   double spread=m_symbol.Spread()*m_adjusted_point;

   level=(level>spread)?level:spread;

//---

   return(true);

  }

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

//| Close positions                                                  |

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

void ClosePositions(const ENUM_POSITION_TYPE pos_type,const double level)

  {

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

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

         if(m_position.Symbol()==m_symbol.Name()/* && m_position.Magic()==InpMagic*/)

            if(m_position.PositionType()==pos_type)

              {

               if(m_position.PositionType()==POSITION_TYPE_BUY)

                 {

                  bool take_profit_level=(m_position.TakeProfit()!=0.0 && m_position.TakeProfit()-m_position.PriceCurrent()>=level) || m_position.TakeProfit()==0.0;

                  bool stop_loss_level=(m_position.StopLoss()!=0.0 && m_position.PriceCurrent()-m_position.StopLoss()>=level) || m_position.StopLoss()==0.0;

                  if(take_profit_level && stop_loss_level)

                     m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol

                 }

               if(m_position.PositionType()==POSITION_TYPE_SELL)

                 {

                  bool take_profit_level=(m_position.TakeProfit()!=0.0 && m_position.PriceCurrent()-m_position.TakeProfit()>=level) || m_position.TakeProfit()==0.0;

                  bool stop_loss_level=(m_position.StopLoss()!=0.0 && m_position.StopLoss()-m_position.PriceCurrent()>=level) || m_position.StopLoss()==0.0;

                  if(take_profit_level && stop_loss_level)

                     m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol

                 }

               PlaySound("ok.wav");

              }

  }

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

//| Get value of buffers                                             |

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

double iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[])

  {

   bool result=true;

   if(!ArrayIsDynamic(arr_buffer))

     {

      Print("This a no dynamic array!");

      return(false);

     }

   ArrayFree(arr_buffer);

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);

   if(copied!=count)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

   return(result);

  }

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

//| Expert tick function                                             |

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

void OnTickInpUpper(void)

  {

   if(InpSignalsFrequency>=SLEEPTIME) // search for trading signals no more than once every 1 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-ExtLastUpper>SLEEPTIME)

        {

         MqlRates rates[],rates_1[];

         //ArraySetAsSeries(rates,true);

         int start_pos=0,count=1;

         if(CopyRates(m_symbol.Name(),Period(),start_pos,count,rates)!=count)

           {

            ExtPrevUpper=0;

            return;

           }

         int start_pos_1=0,count_1=1;

         if(CopyRates(m_symbol.Name(),Period(),start_pos_1,count_1,rates_1)!=count_1)

           {

            ExtPrevAverage=0;

            return;

           }

         double price_line=0.0,price_line_1=0.0;

         if(ObjectFind(0,InpNameaverageUp)>=0 || ObjectFind(0,InpNameupper)>=0)

           {

            long object_type_1=ObjectGetInteger(0,InpNameaverageUp,OBJPROP_TYPE);

            if(object_type_1==OBJ_HLINE)

               price_line_1=ObjectGetDouble(0,InpNameaverageUp,OBJPROP_PRICE);

            else

               if(object_type_1==OBJ_TREND)

                  price_line_1=ObjectGetValueByTime(0,InpNameaverageUp,rates_1[0].time,0);

            long object_type=ObjectGetInteger(0,InpNameupper,OBJPROP_TYPE);

            if(object_type==OBJ_HLINE)

               price_line=ObjectGetDouble(0,InpNameupper,OBJPROP_PRICE);

            else

               if(object_type==OBJ_TREND)

                  price_line=ObjectGetValueByTime(0,InpNameupper,rates[0].time,0);

            if(price_line>0.0 || price_line_1>0.0)

              {

               switch(ObjFilterStopUp)

                 {

                  case Filter_Off:

                     ExtFilter_Off=true;

                     break;

                  case Filter_Stop:

                     ExtFilter_Stop=true;

                     if(rates[0].open>price_line)

                       {

                        TrendOpened();

                        TrendOpened0();

                        OnTickInpNameRx();

                        if(InpIndicators1 && SearchTradingSignals1())

                          {

                           return;

                          }

                       }

                     break;

                  case Filter_All:

                     ExtFilter_All=true;

                     if(rates[0].open<price_line && rates_1[0].close>price_line_1)

                       {

                        TrendOpened();

                        TrendOpened0();

                        OnTickInpNameRx();

                        if(InpIndicators1 && SearchTradingSignals1())

                          {

                           return;

                          }

                       }

                     break;

                 }

              }

           }

         ExtLastUpper=time_current;

         ExtPrevAverage=time_current;

        }

     }

  }

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

//| Expert tick function                                             |

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

void OnTickInpLower(void)

  {

   if(InpSignalsFrequency>=SLEEPTIME) // search for trading signals no more than once every 1 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-ExtLastLower>SLEEPTIME)

        {

         MqlRates rates[],rates_1[];

         //ArraySetAsSeries(rates,true);

         int start_pos=0,count=1;

         if(CopyRates(m_symbol.Name(),Period(),start_pos,count,rates)!=count)

           {

            ExtPrevLower=0;

            return;

           }

         int start_pos_1=0,count_1=1;

         if(CopyRates(m_symbol.Name(),Period(),start_pos_1,count_1,rates_1)!=count_1)

           {

            ExtPrevAverage=0;

            return;

           }

         double price_line=0.0,price_line_1=0.0;

         if(ObjectFind(0,InpNameaverageLow)>=0 || ObjectFind(0,InpNamelower)>=0)

           {

            long object_type_1=ObjectGetInteger(0,InpNameaverageLow,OBJPROP_TYPE);

            if(object_type_1==OBJ_HLINE)

               price_line_1=ObjectGetDouble(0,InpNameaverageLow,OBJPROP_PRICE);

            else

               if(object_type_1==OBJ_TREND)

                  price_line_1=ObjectGetValueByTime(0,InpNameaverageLow,rates_1[0].time,0);

            long object_type=ObjectGetInteger(0,InpNamelower,OBJPROP_TYPE);

            if(object_type==OBJ_HLINE)

               price_line=ObjectGetDouble(0,InpNamelower,OBJPROP_PRICE);

            else

               if(object_type==OBJ_TREND)

                  price_line=ObjectGetValueByTime(0,InpNamelower,rates[0].time,0);

            if(price_line>0.0 || price_line_1>0.0)

              {

               switch(ObjFilterStopLow)

                 {

                  case Filter_Off:

                     ExtFilter_Off=true;

                     break;

                  case Filter_Stop:

                     ExtFilter_Stop=true;

                     if(rates[0].open<price_line)

                       {

                        TrendOpened1();

                        TrendOpened2();

                        OnTickInpNameSx();

                        if(InpIndicators && SearchTradingSignals())

                          {

                           return;

                          }

                       }

                     break;

                  case Filter_All:

                     ExtFilter_All=true;

                     if(rates[0].open>price_line && rates_1[0].close<price_line_1)

                       {

                        TrendOpened1();

                        TrendOpened2();

                        OnTickInpNameSx();

                        if(InpIndicators && SearchTradingSignals())

                          {

                           return;

                          }

                       }

                     break;

                 }

              }

           }

         ExtLastLower=time_current;

         ExtPrevAverage=time_current;

        }

     }

  }

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

//| Search trading signals                                           |

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

bool SearchTradingSignals(void)

  {

//--- we work only at the time of the birth of new bar

   datetime time_0=iTime(m_symbol.Name(),Periodsell,0);

   if(time_0==ExtPrevBarsY)

      return(false);

   ExtPrevBarsY=time_0;

   if(!RefreshRates())

     {

      ExtPrevBarsY=0;

      return(false);

     }

//---

   double main[],signal[];

   ArraySetAsSeries(main,true);

   ArraySetAsSeries(signal,true);

   int start_pos=0,count=3;

   if(!iGetArray(handle_iCustom,MAIN_LINE,start_pos,count,main) ||

      !iGetArray(handle_iCustom,SIGNAL_LINE,start_pos,count,signal))

     {

      ExtPrevBarsY=0;

      return(false);

     }

   if(main[1]>signal[1])

     {

      switch(InpTradeCommandY)

        {

         case Turn_Off:

            ExtTurnOff=true;

            break;

         case Close_AllExt:

            ExtCloseAllExt=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            DeletePending();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            Sleep(SLEEPTIME*1000);

            CloseAll();

            DeletePending();

            ExpertRemoves();

            DeleteChart();

            break;

         case Close_All_All:

            ExtCloseAll=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            break;

         case CloseAll_Profit:

            ExtCloseAllProfit=true;

            CloseAllProfit();

            CloseAllProfit();

            CloseAllProfit();

            break;

         case  close_tpl_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  close_tpl_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case tpl:

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case close_all_tpl:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  UpName:

            ExtUpName=true;

            Obj_SELLSTOP();

            break;

         case  DownName:

            ExtDownName=true;

            Obj_BUYSTOP();

            break;

         case  UpName_s:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  UpName_b:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_b:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_s:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_buys:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            break;

         case  close_sells:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            break;

         case close_all:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            break;

         case open_buy:

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case open_sell:

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_open_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  close_open_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         default:

            ExtNeedOpenBuySell=true;

            LongShortObjOpened();

            break;

        }

      PlaySound("ok.wav");

     }

   if(main[1]<signal[1])

     {

      switch(InpTradeCommandU)

        {

         case Turn_Off:

            ExtTurnOff=true;

            break;

         case Close_AllExt:

            ExtCloseAllExt=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            DeletePending();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            Sleep(SLEEPTIME*1000);

            CloseAll();

            DeletePending();

            ExpertRemoves();

            DeleteChart();

            break;

         case Close_All_All:

            ExtCloseAll=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            break;

         case CloseAll_Profit:

            ExtCloseAllProfit=true;

            CloseAllProfit();

            CloseAllProfit();

            CloseAllProfit();

            break;

         case  close_tpl_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  close_tpl_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case tpl:

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case close_all_tpl:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  UpName:

            ExtUpName=true;

            Obj_SELLSTOP();

            break;

         case  DownName:

            ExtDownName=true;

            Obj_BUYSTOP();

            break;

         case  UpName_s:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  UpName_b:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_b:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_s:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_buys:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            break;

         case  close_sells:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            break;

         case close_all:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            break;

         case open_buy:

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case open_sell:

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_open_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  close_open_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         default:

            ExtNeedOpenBuySell=true;

            LongShortObjOpened();

            break;

        }

      PlaySound("ok.wav");

     }

//---

   return(true);

  }

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

//| Search trading signals                                           |

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

bool SearchTradingSignals1(void)

  {

//--- we work only at the time of the birth of new bar

   datetime time_0=iTime(m_symbol.Name(),Periodbuy,0);

   if(time_0==ExtPrevBarsY1)

      return(false);

   ExtPrevBarsY1=time_0;

   if(!RefreshRates())

     {

      ExtPrevBarsY1=0;

      return(false);

     }

//---

   double main[],signal[];

   ArraySetAsSeries(main,true);

   ArraySetAsSeries(signal,true);

   int start_pos=0,count=3;

   if(!iGetArray(handle_iCustom1,MAIN_LINE,start_pos,count,main) ||

      !iGetArray(handle_iCustom1,SIGNAL_LINE,start_pos,count,signal))

     {

      ExtPrevBarsY1=0;

      return(false);

     }

   if(main[1]>signal[1])

     {

      switch(InpTradeCommandY1)

        {

         case Turn_Off:

            ExtTurnOff=true;

            break;

         case Close_AllExt:

            ExtCloseAllExt=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            DeletePending();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            Sleep(SLEEPTIME*1000);

            CloseAll();

            DeletePending();

            ExpertRemoves();

            DeleteChart();

            break;

         case Close_All_All:

            ExtCloseAll=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            break;

         case CloseAll_Profit:

            ExtCloseAllProfit=true;

            CloseAllProfit();

            CloseAllProfit();

            CloseAllProfit();

            break;

         case  close_tpl_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  close_tpl_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case tpl:

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case close_all_tpl:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  UpName:

            ExtUpName=true;

            Obj_SELLSTOP();

            break;

         case  DownName:

            ExtDownName=true;

            Obj_BUYSTOP();

            break;

         case  UpName_s:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  UpName_b:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_b:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_s:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_buys:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            break;

         case  close_sells:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            break;

         case close_all:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            break;

         case open_buy:

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case open_sell:

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_open_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  close_open_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         default:

            ExtNeedOpenBuySell=true;

            LongShortObjOpened();

            break;

        }

      PlaySound("ok.wav");

     }

   if(main[1]<signal[1])

     {

      switch(InpTradeCommandU1)

        {

         case Turn_Off:

            ExtTurnOff=true;

            break;

         case Close_AllExt:

            ExtCloseAllExt=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            DeletePending();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            Sleep(SLEEPTIME*1000);

            CloseAll();

            DeletePending();

            ExpertRemoves();

            DeleteChart();

            break;

         case Close_All_All:

            ExtCloseAll=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            break;

         case CloseAll_Profit:

            ExtCloseAllProfit=true;

            CloseAllProfit();

            CloseAllProfit();

            CloseAllProfit();

            break;

         case  close_tpl_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  close_tpl_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case tpl:

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case close_all_tpl:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  UpName:

            ExtUpName=true;

            Obj_SELLSTOP();

            break;

         case  DownName:

            ExtDownName=true;

            Obj_BUYSTOP();

            break;

         case  UpName_s:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  UpName_b:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_b:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_s:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_buys:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            break;

         case  close_sells:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            break;

         case close_all:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            break;

         case open_buy:

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case open_sell:

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_open_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  close_open_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         default:

            ExtNeedOpenBuySell=true;

            LongShortObjOpened();

            break;

        }

      PlaySound("ok.wav");

     }

//---

   return(true);

  }

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

//| ChartIndicatorDelete                                             |

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

bool IndicatorDelete(void)

  {

     {ActionsOnTheChart(0);}

//---

   return(true);

  }

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

//|                                                                  |

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

bool ActionsOnTheChart(const long chart_id)

  {

   int sub_windows_total =-1;

   int indicators_total  =0;

//---

   if(!ChartWindowsTotal(chart_id,sub_windows_total))

     {

      return(false);

     }

//---

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

     {

      indicators_total=ChartIndicatorsTotal(chart_id,i);

      //---

      if(indicators_total>0)

        {

         ChIndicatorsDelete(chart_id,i,indicators_total);

        }

     }

//---

   return(true);

  }

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

//|                                                                  |

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

bool ChartWindowsTotal(const long chart_ID,int &sub_windows_total)

  {

   long value=-1;

//---

   if(!ChartGetInteger(chart_ID,CHART_WINDOWS_TOTAL,0,value))

     {Print(__FUNCTION__," Error = ",GetLastError()); return(false);}

//---

   sub_windows_total=(int)value;

//---

   return(true);

  }

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

//|                                                                  |

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

bool ChIndicatorsDelete(const long  chart_id,

                        const int   sub_window,

                        const int   indicators_total)

  {

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

     {

      string indicator_name=ChartIndicatorName(chart_id,sub_window,i);

      //---

      ChIndicatorDelete(indicator_name,chart_id,sub_window);

      ChartRedraw();

     }

//---

   return(true);

  }

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

//|                                                                  |

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

bool ChIndicatorDelete(const string shortname,

                       const long   chartid=0,

                       const int    subwindow=0)

  {

   bool res=ChartIndicatorDelete(chartid,subwindow,Inpshort_name);

   bool res2=ChartIndicatorDelete(chartid,subwindow,Inpshort_name_1);

   if(Inpres)

      bool res0=ChartIndicatorDelete(chartid,subwindow,shortname);

//--- ?@>0=0;878@C5< @57C;LB0B 2K7>20 ChartIndicatorDelete()

   if(!res)

     {

      PrintFormat("%s",shortname,subwindow,GetLastError());

     }

//---

   return(true);

  }

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

//| $C=:F8O ?@>25@:8 8 4>102;5=8O 8=48:0B>@0 =0 3@0D8:               |

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

bool AddIndicator(void)

  {

//--- 2K2>48<>5 A>>1I5=85

   string message;

//--- ?@>25@8< =0 A>2?045=85 A8<2>; 8=48:0B>@0 8 A8<2>; 3@0D8:0

   if(m_symbol.Name()!=m_symbol.Name())

     {

      message="5<>=AB@0F8O 8A?>;L7>20=8O DC=:F88 Demo_ChartIndicatorAdd():";

      message=message+"\r\n";

      message=message+"5;L7O =0 3@0D8: 4>1028BL 8=48:0B>@, @0AAG8B0==K9 =0 4@C3>< A8<2>;5.";

      message=message+"\r\n";

      message=message+"#:068B5 2 A2>9AB20E M:A?5@B0 A8<2>; 3@0D8:0 - "+m_symbol.Name()+".";

      Alert(message);

      //--- 4>A@>G=K9 2KE>4, =5 1C45< 4>102;OBL 8=48:0B>@ =0 3@0D8:

      return false;

     }

//--- ?@>25@8< =0 A>2?045=85 B09<D@59< 8=48:0B>@0 8 B09<D@59< 3@0D8:0

   if(Period()!=Period())

     {

      message="5;L7O =0 3@0D8: 4>1028BL 8=48:0B>@, @0AAG8B0==K9 =0 4@C3>< B09<D@59<5.";

      message=message+"\r\n";

      message=message+"#:068B5 2 A2>9AB20E M:A?5@B0 B09<D@59< 3@0D8:0 - "+EnumToString(Period())+".";

      Alert(message);

      //--- 4>A@>G=K9 2KE>4, =5 1C45< 4>102;OBL 8=48:0B>@ =0 3@0D8:

      return false;

     }

//--- 2A5 ?@>25@:8 ?@>H;8, A8<2>; 8 ?5@8>4 8=48:0B>@0 A>>B25BAB2CNB 3@0D8:C

   if(indicator_handle==INVALID_HANDLE)

     {

      Print(__FUNCTION__,"  !>7405< 8=48:0B>@");

      indicator_handle=iCustom(m_symbol.Name(),Period(),InpIndi_name);

      if(indicator_handle==INVALID_HANDLE)

        {

         Print("5 C40;>AL A>740BL 8=48:0B>@. >4 >H81:8 ",GetLastError());

        }

     }

//--- A1@>A8< :>4 >H81:8

   ResetLastError();

//--- =0:;04K205< 8=48:0B>@ =0 3@0D8:

   Print(__FUNCTION__,"  >102;O5< 8=48:0B>@ =0 3@0D8:");

   Print("=48:0B>@ ?>AB@>5= =0 ",m_symbol.Name(),"/",EnumToString(Period()));

//--- ?>;CG8< =><5@ =>2>3> ?>4>:=0, 2 :>B>@>5 4>1028< 8=48:0B>@

   int subwindow=NULL;

   PrintFormat(">102;O5< 8=48:0B>@ =0 >:=> %d 3@0D8:0",subwindow);

   if(!ChartIndicatorAdd(0,subwindow,indicator_handle))

     {

      PrintFormat("5 C40;>AL 4>1028BL 8=48:0B>@ =0 >:=> %d 3@0D8:0. >4 >H81:8  %d",

                  subwindow,GetLastError());

     }

//--- 4>102;5=85 8=48:0B>@0 =0 3@0D8: ?@>H;> CA?5H=>

   return(true);

  }

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

//| Search trading signals                                                                 |

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

bool SessionTime(void)

  {

   bool res=false;

//---

   if((InpHoursFrom<InpHoursTo && TimeLocal()>=InpHoursFrom && TimeLocal()<InpHoursTo) ||

      (InpHoursFrom>InpHoursTo && (TimeLocal()<InpHoursTo || TimeLocal()>=InpHoursFrom)))

     {

      if(ticks_to_close>0)

        {

         ticks_to_close--;

           {

            switch(InpTimeCommand)

              {

               case Turn_Off:

                  ExtTurnOff=true;

                  break;

               case Close_AllExt:

                  ExtCloseAllExt=true;

                  CloseAllProfit();

                  CloseAllLoss();

                  CloseAll();

                  DeletePending();

                  Sleep(SLEEPTIME*1000);

                  CloseAllProfit();

                  CloseAllLoss();

                  Sleep(SLEEPTIME*1000);

                  CloseAll();

                  DeletePending();

                  ExpertRemoves();

                  DeleteChart();

                  break;

               case Close_All_All:

                  ExtCloseAll=true;

                  CloseAllProfit();

                  CloseAllLoss();

                  CloseAll();

                  Sleep(SLEEPTIME*1000);

                  CloseAllProfit();

                  CloseAllLoss();

                  CloseAll();

                  break;

               case CloseAll_Profit:

                  ExtCloseAllProfit=true;

                  CloseAllProfit();

                  CloseAllProfit();

                  CloseAllProfit();

                  break;

               case  close_tpl_b:

                  ExtNeedCloseSell=true;

                  ShortObjClosed();

                  ExtNeedOpenBuy=true;

                  LongObjOpened();

                  Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                  DeleteChart_1();

                  break;

               case  close_tpl_s:

                  ExtNeedCloseBuy=true;

                  LongObjClosed();

                  ExtNeedOpenSell=true;

                  ShortObjOpened();

                  Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                  DeleteChart_1();

                  break;

               case tpl:

                  Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                  DeleteChart_1();

                  break;

               case close_all_tpl:

                  ExtNeedCloseAll=true;

                  LongShortObjClosed();

                  Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                  DeleteChart_1();

                  break;

               case  UpName:

                  ExtUpName=true;

                  Obj_SELLSTOP();

                  break;

               case  DownName:

                  ExtDownName=true;

                  Obj_BUYSTOP();

                  break;

               case  UpName_s:

                  ExtUpName=true;

                  Obj_SELLSTOP();

                  ExtNeedOpenSell=true;

                  ShortObjOpened();

                  break;

               case  UpName_b:

                  ExtUpName=true;

                  Obj_SELLSTOP();

                  ExtNeedOpenBuy=true;

                  LongObjOpened();

                  break;

               case  DownName_b:

                  ExtDownName=true;

                  Obj_BUYSTOP();

                  ExtNeedOpenBuy=true;

                  LongObjOpened();

                  break;

               case  DownName_s:

                  ExtDownName=true;

                  Obj_BUYSTOP();

                  ExtNeedOpenSell=true;

                  ShortObjOpened();

                  break;

               case  close_buys:

                  ExtNeedCloseBuy=true;

                  LongObjClosed();

                  break;

               case  close_sells:

                  ExtNeedCloseSell=true;

                  ShortObjClosed();

                  break;

               case close_all:

                  ExtNeedCloseAll=true;

                  LongShortObjClosed();

                  break;

               case open_buy:

                  ExtNeedOpenBuy=true;

                  LongObjOpened();

                  break;

               case open_sell:

                  ExtNeedOpenSell=true;

                  ShortObjOpened();

                  break;

               case  close_open_b:

                  ExtNeedCloseSell=true;

                  ShortObjClosed();

                  ExtNeedOpenBuy=true;

                  LongObjOpened();

                  break;

               case  close_open_s:

                  ExtNeedCloseBuy=true;

                  LongObjClosed();

                  ExtNeedOpenSell=true;

                  ShortObjOpened();

                  break;

               default:

                  ExtNeedOpenBuySell=true;

                  LongShortObjOpened();

                  break;

              }

            PlaySound("request.wav");

           }

         res=true;

        }

     }

//---

   return(res);

  }

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

//| Search trading signals                                                                 |

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

bool TimeSession(void)

  {

   bool res=false;

//---

   if((InpHoursFrom_1<InpHoursTo_1 && TimeLocal()>=InpHoursFrom_1 && TimeLocal()<InpHoursTo_1) ||

      (InpHoursFrom_1>InpHoursTo_1 && (TimeLocal()<InpHoursTo_1 || TimeLocal()>=InpHoursFrom_1)))

     {

      if(ticks_to_close_1>0)

        {

         ticks_to_close_1--;

         Alert("ChartIndicatorAdd ''"+InpIndi_name+"' on "+m_symbol.Name());

         AddIndicator();

        }

      res=true;

     }

//---

   return(res);

  }

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

//| Check for long position opening                                  |

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

bool TrendOpened(void)

  {

   bool res=false;

   datetime time_0=iTime(m_symbol.Name(),Period_1,0);

   if(time_0==ExtPrevBars)

      return(true);

   ExtPrevBars=time_0;

     {

      if(ObjectFind(0,InpNameAverage)<0)

         return(true);

      MqlRates ratesAverage[];

      ArraySetAsSeries(ratesAverage,true);

      int start_pos=0,count=3;

      if(CopyRates(m_symbol.Name(),Period(),start_pos,count,ratesAverage)!=count)

         return(false);

      if(ratesAverage[0].time==ExtLastINAverage)

         return(true);

      double price_line=0.0;

      long object_type=ObjectGetInteger(0,InpNameAverage,OBJPROP_TYPE);

      if(object_type==OBJ_HLINE)

         price_line=ObjectGetDouble(0,InpNameAverage,OBJPROP_PRICE);

      else

         if(object_type==OBJ_TREND)

            price_line=ObjectGetValueByTime(0,InpNameAverage,ratesAverage[1].time,0);

      double value_by_time=price_line;

      if(value_by_time==0.0)

         return(true);

      if(ratesAverage[1].open<value_by_time && ratesAverage[1].close>value_by_time)

        {

         switch(InpAverage_x)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine)

            ObjectsDeleteAll(0,InpNameAverage);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

      if(ratesAverage[1].open>value_by_time && ratesAverage[1].close<value_by_time)

        {

         switch(InpAverage)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine)

            ObjectsDeleteAll(0,InpNameAverage);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

     }

//--- result

   return(res);

  }

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

//| Check for long position opening                                  |

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

bool TrendOpened0(void)

  {

   bool res=false;

   datetime time_0=iTime(m_symbol.Name(),Period_2,0);

   if(time_0==ExtPrevBars0)

      return(true);

   ExtPrevBars0=time_0;

     {

      if(ObjectFind(0,InpNameAverage0)<0)

         return(true);

      MqlRates ratesAverage[];

      ArraySetAsSeries(ratesAverage,true);

      int start_pos=0,count=3;

      if(CopyRates(m_symbol.Name(),Period(),start_pos,count,ratesAverage)!=count)

         return(false);

      if(ratesAverage[0].time==ExtLastINAverage0)

         return(true);

      double price_line=0.0;

      long object_type=ObjectGetInteger(0,InpNameAverage0,OBJPROP_TYPE);

      if(object_type==OBJ_HLINE)

         price_line=ObjectGetDouble(0,InpNameAverage0,OBJPROP_PRICE);

      else

         if(object_type==OBJ_TREND)

            price_line=ObjectGetValueByTime(0,InpNameAverage0,ratesAverage[1].time,0);

      double value_by_time=price_line;

      if(value_by_time==0.0)

         return(true);

      if(ratesAverage[1].open<value_by_time && ratesAverage[1].close>value_by_time)

        {

         switch(InpAverage0_0)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine_0)

            ObjectsDeleteAll(0,InpNameAverage0);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

      if(ratesAverage[1].open>value_by_time && ratesAverage[1].close<value_by_time)

        {

         switch(InpAverage0)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine_0)

            ObjectsDeleteAll(0,InpNameAverage0);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

     }

//--- result

   return(res);

  }

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

//| Check for long position opening                                  |

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

bool TrendOpened1(void)

  {

   bool res=false;

   datetime time_0=iTime(m_symbol.Name(),Period_3,0);

   if(time_0==ExtPrevBars1)

      return(true);

   ExtPrevBars1=time_0;

     {

      if(ObjectFind(0,InpNameAverage1)<0)

         return(true);

      MqlRates ratesAverage[];

      ArraySetAsSeries(ratesAverage,true);

      int start_pos=0,count=3;

      if(CopyRates(m_symbol.Name(),Period(),start_pos,count,ratesAverage)!=count)

         return(false);

      if(ratesAverage[0].time==ExtLastINAverage1)

         return(true);

      double price_line=0.0;

      long object_type=ObjectGetInteger(0,InpNameAverage1,OBJPROP_TYPE);

      if(object_type==OBJ_HLINE)

         price_line=ObjectGetDouble(0,InpNameAverage1,OBJPROP_PRICE);

      else

         if(object_type==OBJ_TREND)

            price_line=ObjectGetValueByTime(0,InpNameAverage1,ratesAverage[1].time,0);

      double value_by_time=price_line;

      if(value_by_time==0.0)

         return(true);

      if(ratesAverage[1].open<value_by_time && ratesAverage[1].close>value_by_time)

        {

         switch(InpAverage1_1)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine_1)

            ObjectsDeleteAll(0,InpNameAverage1);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

      if(ratesAverage[1].open>value_by_time && ratesAverage[1].close<value_by_time)

        {

         switch(InpAverage1)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine_1)

            ObjectsDeleteAll(0,InpNameAverage1);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

     }

//--- result

   return(res);

  }

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

//| Check for long position opening                                  |

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

bool TrendOpened2(void)

  {

   bool res=false;

   datetime time_0=iTime(m_symbol.Name(),Period_4,0);

   if(time_0==ExtPrevBars2)

      return(true);

   ExtPrevBars2=time_0;

     {

      if(ObjectFind(0,InpNameAverage2)<0)

         return(true);

      MqlRates ratesAverage[];

      ArraySetAsSeries(ratesAverage,true);

      int start_pos=0,count=3;

      if(CopyRates(m_symbol.Name(),Period(),start_pos,count,ratesAverage)!=count)

         return(false);

      if(ratesAverage[0].time==ExtLastINAverage2)

         return(true);

      double price_line=0.0;

      long object_type=ObjectGetInteger(0,InpNameAverage2,OBJPROP_TYPE);

      if(object_type==OBJ_HLINE)

         price_line=ObjectGetDouble(0,InpNameAverage2,OBJPROP_PRICE);

      else

         if(object_type==OBJ_TREND)

            price_line=ObjectGetValueByTime(0,InpNameAverage2,ratesAverage[1].time,0);

      double value_by_time=price_line;

      if(value_by_time==0.0)

         return(true);

      if(ratesAverage[1].open<value_by_time && ratesAverage[1].close>value_by_time)

        {

         switch(InpAverage2_2)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine_2)

            ObjectsDeleteAll(0,InpNameAverage2);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

      if(ratesAverage[1].open>value_by_time && ratesAverage[1].close<value_by_time)

        {

         switch(InpAverage2)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         if(InpDelLine_2)

            ObjectsDeleteAll(0,InpNameAverage2);

         IndicatorDelete();

         PlaySound("ok.wav");

         res=true;

        }

     }

//--- result

   return(res);

  }

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

//| Expert tick function                                             |

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

void OnTickInpNameRx(void)

  {

   if(InpSignalsFrequency>=SLEEPTIME) // search for trading signals no more than once every 10 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-ExtLastSignalsRx>SLEEPTIME)

        {

         MqlRates rates[];

         //ArraySetAsSeries(rates,true);

         int start_pos=0,count=1;

         if(CopyRates(m_symbol.Name(),Period(),start_pos,count,rates)!=count)

           {

            ExtPrevBarsRx=0;

            return;

           }

         double price_line=0.0;

         if(ObjectFind(0,InpObjUpName)>=0)

           {

            long object_type=ObjectGetInteger(0,InpObjUpName,OBJPROP_TYPE);

            if(object_type==OBJ_HLINE)

               price_line=ObjectGetDouble(0,InpObjUpName,OBJPROP_PRICE);

            else

               if(object_type==OBJ_TREND)

                  price_line=ObjectGetValueByTime(0,InpObjUpName,rates[0].time,0);

            if(price_line>0.0)

              {

               if((rates[0].open>price_line && rates[0].close<price_line) ||

                  (rates[0].open<price_line && rates[0].close>price_line))

                 {

                  switch(InpTradeCommand)

                    {

                     case Turn_Off:

                        ExtTurnOff=true;

                        break;

                     case Close_AllExt:

                        ExtCloseAllExt=true;

                        CloseAllProfit();

                        CloseAllLoss();

                        CloseAll();

                        DeletePending();

                        Sleep(SLEEPTIME*1000);

                        CloseAllProfit();

                        CloseAllLoss();

                        Sleep(SLEEPTIME*1000);

                        CloseAll();

                        DeletePending();

                        ExpertRemoves();

                        DeleteChart();

                        break;

                     case Close_All_All:

                        ExtCloseAll=true;

                        CloseAllProfit();

                        CloseAllLoss();

                        CloseAll();

                        Sleep(SLEEPTIME*1000);

                        CloseAllProfit();

                        CloseAllLoss();

                        CloseAll();

                        break;

                     case CloseAll_Profit:

                        ExtCloseAllProfit=true;

                        CloseAllProfit();

                        CloseAllProfit();

                        CloseAllProfit();

                        break;

                     case  close_tpl_b:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case  close_tpl_s:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case tpl:

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case close_all_tpl:

                        ExtNeedCloseAll=true;

                        LongShortObjClosed();

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case  UpName:

                        ExtUpName=true;

                        Obj_SELLSTOP();

                        break;

                     case  DownName:

                        ExtDownName=true;

                        Obj_BUYSTOP();

                        break;

                     case  UpName_s:

                        ExtUpName=true;

                        Obj_SELLSTOP();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     case  UpName_b:

                        ExtUpName=true;

                        Obj_SELLSTOP();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case  DownName_b:

                        ExtDownName=true;

                        Obj_BUYSTOP();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case  DownName_s:

                        ExtDownName=true;

                        Obj_BUYSTOP();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     case  close_buys:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        break;

                     case  close_sells:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        break;

                     case close_all:

                        ExtNeedCloseAll=true;

                        LongShortObjClosed();

                        break;

                     case open_buy:

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case open_sell:

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     case  close_open_b:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case  close_open_s:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     default:

                        ExtNeedOpenBuySell=true;

                        LongShortObjOpened();

                        break;

                    }

                  ObjectsDeleteAll(0,InpObjUpName);

                  IndicatorDelete();

                  PlaySound("ok.wav");

                  Sleep(SLEEPTIME*1000);

                  if(Obj_SELLSTOP())

                     ExtUpName=true;

                 }

              }

           }

         ExtLastSignalsRx=time_current;

        }

     }

  }

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

//| Expert tick function                                             |

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

void OnTickInpNameSx(void)

  {

   if(InpSignalsFrequency>=SLEEPTIME) // search for trading signals no more than once every 10 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-ExtLastSignalsSx>SLEEPTIME)

        {

         MqlRates rates[];

         //ArraySetAsSeries(rates,true);

         int start_pos=0,count=1;

         if(CopyRates(m_symbol.Name(),Period(),start_pos,count,rates)!=count)

           {

            ExtPrevBarsSx=0;

            return;

           }

         double price_line=0.0;

         if(ObjectFind(0,InpObjDownName)>=0)

           {

            long object_type=ObjectGetInteger(0,InpObjDownName,OBJPROP_TYPE);

            if(object_type==OBJ_HLINE)

               price_line=ObjectGetDouble(0,InpObjDownName,OBJPROP_PRICE);

            else

               if(object_type==OBJ_TREND)

                  price_line=ObjectGetValueByTime(0,InpObjDownName,rates[0].time,0);

            if(price_line>0.0)

              {

               if((rates[0].open>price_line && rates[0].close<price_line) ||

                  (rates[0].open<price_line && rates[0].close>price_line))

                 {

                  switch(InTradeCommand)

                    {

                     case Turn_Off:

                        ExtTurnOff=true;

                        break;

                     case Close_AllExt:

                        ExtCloseAllExt=true;

                        CloseAllProfit();

                        CloseAllLoss();

                        CloseAll();

                        DeletePending();

                        Sleep(SLEEPTIME*1000);

                        CloseAllProfit();

                        CloseAllLoss();

                        Sleep(SLEEPTIME*1000);

                        CloseAll();

                        DeletePending();

                        ExpertRemoves();

                        DeleteChart();

                        break;

                     case Close_All_All:

                        ExtCloseAll=true;

                        CloseAllProfit();

                        CloseAllLoss();

                        CloseAll();

                        Sleep(SLEEPTIME*1000);

                        CloseAllProfit();

                        CloseAllLoss();

                        CloseAll();

                        break;

                     case CloseAll_Profit:

                        ExtCloseAllProfit=true;

                        CloseAllProfit();

                        CloseAllProfit();

                        CloseAllProfit();

                        break;

                     case  close_tpl_b:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case  close_tpl_s:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case tpl:

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case close_all_tpl:

                        ExtNeedCloseAll=true;

                        LongShortObjClosed();

                        Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

                        DeleteChart_1();

                        break;

                     case  UpName:

                        ExtUpName=true;

                        Obj_SELLSTOP();

                        break;

                     case  DownName:

                        ExtDownName=true;

                        Obj_BUYSTOP();

                        break;

                     case  UpName_s:

                        ExtUpName=true;

                        Obj_SELLSTOP();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     case  UpName_b:

                        ExtUpName=true;

                        Obj_SELLSTOP();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case  DownName_b:

                        ExtDownName=true;

                        Obj_BUYSTOP();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case  DownName_s:

                        ExtDownName=true;

                        Obj_BUYSTOP();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     case  close_buys:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        break;

                     case  close_sells:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        break;

                     case close_all:

                        ExtNeedCloseAll=true;

                        LongShortObjClosed();

                        break;

                     case open_buy:

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case open_sell:

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     case  close_open_b:

                        ExtNeedCloseSell=true;

                        ShortObjClosed();

                        ExtNeedOpenBuy=true;

                        LongObjOpened();

                        break;

                     case  close_open_s:

                        ExtNeedCloseBuy=true;

                        LongObjClosed();

                        ExtNeedOpenSell=true;

                        ShortObjOpened();

                        break;

                     default:

                        ExtNeedOpenBuySell=true;

                        LongShortObjOpened();

                        break;

                    }

                  ObjectsDeleteAll(0,InpObjDownName);

                  IndicatorDelete();

                  PlaySound("ok.wav");

                  Sleep(SLEEPTIME*1000);

                  if(Obj_BUYSTOP())

                     ExtDownName=true;

                 }

              }

           }

         ExtLastSignalsSx=time_current;

        }

     }

  }

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

//| start function                                                   |

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

bool Obj_SELLSTOP(void)

  {

   bool res=false;

// F5=0 4;O 2KAB02;5=8O >@45@>2 A5B:8

   double curPrice;

// B5:CI0O F5=0 ?> 8=AB@C<5=BC

   MqlTick lastme;

   SymbolInfoTick(m_symbol.Name(),lastme);

// 5A;8 B5:CI59 F5=K ?>;CG8BL =5 C40;>AL, >B<5=O5< 2KAB02;5=85 A5B:8

   if(lastme.bid==0)

     {

      return(true);

     }

// <8=8<0;L=>5 @0AAB>O=85 >B F5=K, =0 :>B>@>< <>6=> AB028BL AB>? ;>AAK

   double minStop=SymbolInfoDouble(m_symbol.Name(),SYMBOL_POINT)*SymbolInfoInteger(m_symbol.Name(),SYMBOL_TRADE_STOPS_LEVEL);

// 2KAB02;O5< >@45@0 2 Long

   curPrice=lastme.bid;

   for(uint i=0; i<1; i++)

     {

      curPrice+=m_obj_trailing_stop;

      if(curPrice-lastme.ask<minStop)

         continue;

      CreateHline(0,0,InpObjUpName,curPrice,clrRed,0,0,1,1,1,1,2);

      PlaySound("tick.wav");

     }

   res=true;

//---

   return(true);

  }

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

//| start function                                                   |

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

bool Obj_BUYSTOP(void)

  {

   bool res=false;

// F5=0 4;O 2KAB02;5=8O >@45@>2 A5B:8

   double curPrice;

// B5:CI0O F5=0 ?> 8=AB@C<5=BC

   MqlTick lastme;

   SymbolInfoTick(m_symbol.Name(),lastme);

// 5A;8 B5:CI59 F5=K ?>;CG8BL =5 C40;>AL, >B<5=O5< 2KAB02;5=85 A5B:8

   if(lastme.bid==0)

     {

      return(true);

     }

// <8=8<0;L=>5 @0AAB>O=85 >B F5=K, =0 :>B>@>< <>6=> AB028BL AB>? ;>AAK

   double minStop=SymbolInfoDouble(m_symbol.Name(),SYMBOL_POINT)*SymbolInfoInteger(m_symbol.Name(),SYMBOL_TRADE_STOPS_LEVEL);

// 2KAB02;O5< >@45@0 2 Short

   curPrice=lastme.ask;

   for(uint i=0; i<1; i++)

     {

      curPrice-=m_obj_trailing_stop;

      if(lastme.bid-curPrice<minStop)

         continue;

      CreateHline(0,0,InpObjDownName,curPrice,clrBlue,0,0,1,1,1,1,2);

      PlaySound("tick.wav");

     }

   res=true;

//---

   return(true);

  }

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

//| start function                                                   |

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

void Obj_Obj(void)

  {

   if(ObjectFind(0,InpObjVLineName)>=0)

     {

      ExtVLinetime=(datetime)(ObjectGetInteger(0,InpObjVLineName,OBJPROP_TIME,0));

      if(ExtVLinetime<TimeLocal())

        {

         switch(InpVLineCommand)

           {

            case Turn_Off:

               ExtTurnOff=true;

               break;

            case Close_AllExt:

               ExtCloseAllExt=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               DeletePending();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               Sleep(SLEEPTIME*1000);

               CloseAll();

               DeletePending();

               ExpertRemoves();

               DeleteChart();

               break;

            case Close_All_All:

               ExtCloseAll=true;

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               Sleep(SLEEPTIME*1000);

               CloseAllProfit();

               CloseAllLoss();

               CloseAll();

               break;

            case CloseAll_Profit:

               ExtCloseAllProfit=true;

               CloseAllProfit();

               CloseAllProfit();

               CloseAllProfit();

               break;

            case  close_tpl_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  close_tpl_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case tpl:

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case close_all_tpl:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

               DeleteChart_1();

               break;

            case  UpName:

               ExtUpName=true;

               Obj_SELLSTOP();

               break;

            case  DownName:

               ExtDownName=true;

               Obj_BUYSTOP();

               break;

            case  UpName_s:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  UpName_b:

               ExtUpName=true;

               Obj_SELLSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_b:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  DownName_s:

               ExtDownName=true;

               Obj_BUYSTOP();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_buys:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               break;

            case  close_sells:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               break;

            case close_all:

               ExtNeedCloseAll=true;

               LongShortObjClosed();

               break;

            case open_buy:

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case open_sell:

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            case  close_open_b:

               ExtNeedCloseSell=true;

               ShortObjClosed();

               ExtNeedOpenBuy=true;

               LongObjOpened();

               break;

            case  close_open_s:

               ExtNeedCloseBuy=true;

               LongObjClosed();

               ExtNeedOpenSell=true;

               ShortObjOpened();

               break;

            default:

               ExtNeedOpenBuySell=true;

               LongShortObjOpened();

               break;

           }

         ObjectsDeleteAll(0,InpObjVLineName);

         IndicatorDelete();

         PlaySound("timeout.wav");

        }

     }

//---

  }

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

//|  !>740=85 3>@87>=B0;L=>3>, F5=>2>3> C@>2=O                       |

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

void CreateHline(long ch_id,int sub_window,

                 string name,double price,

                 color clr,ENUM_LINE_STYLE style,

                 int width,bool back,

                 bool selectable,bool selected,

                 bool hidden,long z_order)

  {

   ObjectCreate(ch_id,name,OBJ_HLINE,sub_window,0,price);

   ObjectSetInteger(ch_id,name,OBJPROP_COLOR,clr);

   ObjectSetInteger(ch_id,name,OBJPROP_STYLE,style);

   ObjectSetInteger(ch_id,name,OBJPROP_WIDTH,width);

   ObjectSetInteger(ch_id,name,OBJPROP_BACK,back);

   ObjectSetInteger(ch_id,name,OBJPROP_SELECTABLE,selectable);

   ObjectSetInteger(ch_id,name,OBJPROP_SELECTED,selected);

   ObjectSetInteger(ch_id,name,OBJPROP_HIDDEN,hidden);

   ObjectSetInteger(ch_id,name,OBJPROP_ZORDER,z_order);

  }

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

//|                                                                  |

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

void _Trailing(void)

  {

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

     {

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

      if(position_GetSymbol==m_symbol.Name())

        {

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

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

         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;

         double v=10;

         if(TrailingStop_STOP_LEVEL>_SYMBOL_TRADE_STOPS_LEVEL)

           {_SYMBOL_TRADE_STOPS_LEVEL=TrailingStop_STOP_LEVEL;};

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

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

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

              {

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

                 {

                  //--- 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*v;

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

              {

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

                 {

                  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 ButtonCreate(string name,int Xdist,

                  int Ydist,int Xsize,int Ysize,int FONTSIZE=12)

  {

   if(ObjectFind(0,m_symbol.Name()+name)<0)

      ObjectCreate(0,m_symbol.Name()+name,OBJ_BUTTON,0,100,100);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_COLOR,clrGray);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_BGCOLOR,clrBlack);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_XDISTANCE,Xdist);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_YDISTANCE,Ydist);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_XSIZE,Xsize);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_YSIZE,Ysize);

   ObjectSetString(0,m_symbol.Name()+name,OBJPROP_FONT,"Sans Serif");

   ObjectSetString(0,m_symbol.Name()+name,OBJPROP_TEXT,name);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_FONTSIZE,FONTSIZE);

   ObjectSetInteger(0,m_symbol.Name()+name,OBJPROP_SELECTABLE,false);

  }

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

//| Check for long position closing                                  |

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

bool ButtonOnTick(void)

  {

   bool res=false;

   double level;

   if(ObjectGetInteger(0,m_symbol.Name()+"without '.tpl'",OBJPROP_STATE,0)==true)

     {

      Alert("DeleteChart ''"+Template+"' on "+m_symbol.Name());

      DeleteChart();

      ObjectSetInteger(0,m_symbol.Name()+"without '.tpl'",OBJPROP_STATE,false);

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"SELL_Close",OBJPROP_STATE,0)==true)

     {

      if(FreezeStopsLevels(level))

         ClosePositions(POSITION_TYPE_SELL,level);

      PlaySound("ok.wav");

      ObjectSetInteger(0,m_symbol.Name()+"SELL_Close",OBJPROP_STATE,false);

      ObjectSetInteger(0,m_symbol.Name()+"SELL",OBJPROP_STATE,false);

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"BUY_Close",OBJPROP_STATE,0)==true)

     {

      if(FreezeStopsLevels(level))

         ClosePositions(POSITION_TYPE_BUY,level);

      PlaySound("ok.wav");

      ObjectSetInteger(0,m_symbol.Name()+"BUY_Close",OBJPROP_STATE,false);

      ObjectSetInteger(0,m_symbol.Name()+"BUY",OBJPROP_STATE,false);

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"CLOSE_ALL",OBJPROP_STATE,0)==true)

     {

      CloseAllProfit();

      CloseAllLoss();

      CloseAll();

      DeletePending();

      Sleep(SLEEPTIME*1000);

      CloseAllProfit();

      CloseAllLoss();

      Sleep(SLEEPTIME*1000);

      CloseAll();

      DeletePending();

      ExpertRemoves();

      DeleteChart();

      PlaySound("expert.wav");

      ObjectSetInteger(0,m_symbol.Name()+"CLOSE_ALL",OBJPROP_STATE,false);

      ObjectSetInteger(0,m_symbol.Name()+"CLOSE",OBJPROP_STATE,false);

     }

   double PROFIT_BUY=0.00;

   double PROFIT_SELL=0.00;

   double PROFIT_CLOSE=0.00;

   double PROFIT_BUY_Lot=0.00;

   double PROFIT_SELL_Lot=0.00;

   double PROFIT_CLOSE_Lot=0.00;

   int _all =0;

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

     {

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

      if(position_GetSymbol==m_symbol.Name())

        {

         PROFIT_CLOSE_Lot=AccountInfoDouble(ACCOUNT_EQUITY);

         _all=_all+1;

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);

            PROFIT_BUY_Lot=PROFIT_BUY_Lot+PositionGetDouble(POSITION_VOLUME);

           }

         else

           {

            PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);

            PROFIT_SELL_Lot=PROFIT_SELL_Lot+PositionGetDouble(POSITION_VOLUME);

           }

           {

            PROFIT_CLOSE=AccountInfoDouble(ACCOUNT_PROFIT);

            PROFIT_CLOSE_Lot=AccountInfoDouble(ACCOUNT_PROFIT);

           }

        }

     }

   ObjectSetString(0,m_symbol.Name()+"BUY_Close",OBJPROP_TEXT,"BUY Close ("+DoubleToString(PROFIT_BUY,2)+")");  //"BUY","SELL"

   if(PROFIT_BUY>0)

     {

      ObjectSetInteger(0,Symbol()+"BUY_Close",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(PROFIT_BUY==0)

        {

         ObjectSetInteger(0,m_symbol.Name()+"BUY_Close",OBJPROP_BGCOLOR,clrBlack);

        }

      else

        {

         ObjectSetInteger(0,m_symbol.Name()+"BUY_Close",OBJPROP_BGCOLOR,clrCrimson);

        };

   ObjectSetString(0,m_symbol.Name()+"SELL_Close",OBJPROP_TEXT,"SELL Close ("+DoubleToString(PROFIT_SELL,2)+")");  //"BUY","SELL"

   if(PROFIT_SELL>0)

     {

      ObjectSetInteger(0,m_symbol.Name()+"SELL_Close",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(PROFIT_SELL==0)

        {

         ObjectSetInteger(0,m_symbol.Name()+"SELL_Close",OBJPROP_BGCOLOR,clrBlack);

        }

      else

        {

         ObjectSetInteger(0,m_symbol.Name()+"SELL_Close",OBJPROP_BGCOLOR,clrCrimson);

        }

   ObjectSetString(0,m_symbol.Name()+"CLOSE_ALL",OBJPROP_TEXT,"CLOSE ALL ("+DoubleToString(PROFIT_CLOSE,2)+")");  //"BUY","SELL"

   if(PROFIT_CLOSE>0)

     {

      ObjectSetInteger(0,m_symbol.Name()+"CLOSE_ALL",OBJPROP_BGCOLOR,clrLimeGreen);

     }

   else

      if(PROFIT_CLOSE==0)

        {

         ObjectSetInteger(0,m_symbol.Name()+"CLOSE_ALL",OBJPROP_BGCOLOR,clrBlack);

        }

      else

        {

         ObjectSetInteger(0,m_symbol.Name()+"CLOSE_ALL",OBJPROP_BGCOLOR,clrCrimson);

        }

   ObjectSetString(0,m_symbol.Name()+"BUY",OBJPROP_TEXT,"BUY Lot ("+DoubleToString(PROFIT_BUY_Lot,2)+")");  //"BUY","SELL"

   ObjectSetString(0,m_symbol.Name()+"SELL",OBJPROP_TEXT,"SELL Lot ("+DoubleToString(PROFIT_SELL_Lot,2)+")");  //"BUY","SELL"

   ObjectSetString(0,m_symbol.Name()+"CLOSE",OBJPROP_TEXT,"CLOSE Lot ("+DoubleToString(PROFIT_CLOSE_Lot,2)+")");  //"BUY","SELL"

   ObjectSetString(0,m_symbol.Name()+"Trailing",OBJPROP_TEXT,"Tral = "+DoubleToString(_all,0)

                   +", all = "+DoubleToString(PositionsTotal(),0)+".");  //"BUY","SELL"

   if(ObjectGetInteger(0,m_symbol.Name()+"BUY",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,m_symbol.Name()+"BUY",OBJPROP_BGCOLOR,clrDeepSkyBlue);

     }

   else

     {

      ObjectSetInteger(0,m_symbol.Name()+"BUY",OBJPROP_BGCOLOR,clrBlack);

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"SELL",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,m_symbol.Name()+"SELL",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,m_symbol.Name()+"SELL",OBJPROP_BGCOLOR,clrBlack);

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"CLOSE",OBJPROP_STATE,0)==true)

     {

      ObjectSetInteger(0,m_symbol.Name()+"CLOSE",OBJPROP_BGCOLOR,clrRed);

     }

   else

     {

      ObjectSetInteger(0,m_symbol.Name()+"CLOSE",OBJPROP_BGCOLOR,clrBlack);

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"Trailing",OBJPROP_STATE,0)==true)

     {_Trailing();};

   if(ObjectGetInteger(0,m_symbol.Name()+"SELL",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,m_symbol.Name()+"SELL",OBJPROP_STATE,0);

      switch(InTradeCommandBut)

        {

         case Turn_Off:

            ExtTurnOff=true;

            break;

         case Close_AllExt:

            ExtCloseAllExt=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            DeletePending();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            Sleep(SLEEPTIME*1000);

            CloseAll();

            DeletePending();

            ExpertRemoves();

            DeleteChart();

            break;

         case Close_All_All:

            ExtCloseAll=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            break;

         case CloseAll_Profit:

            ExtCloseAllProfit=true;

            CloseAllProfit();

            CloseAllProfit();

            CloseAllProfit();

            break;

         case  close_tpl_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  close_tpl_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case tpl:

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case close_all_tpl:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  UpName:

            ExtUpName=true;

            Obj_SELLSTOP();

            break;

         case  DownName:

            ExtDownName=true;

            Obj_BUYSTOP();

            break;

         case  UpName_s:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  UpName_b:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_b:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_s:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_buys:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            break;

         case  close_sells:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            break;

         case close_all:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            break;

         case open_buy:

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case open_sell:

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_open_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  close_open_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         default:

            ExtNeedOpenBuySell=true;

            LongShortObjOpened();

            break;

        }

      PlaySound("ok.wav");

      res=true;

     }

   if(ObjectGetInteger(0,m_symbol.Name()+"BUY",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,m_symbol.Name()+"BUY",OBJPROP_STATE,0);

      switch(InpTradeCommandBut)

        {

         case Turn_Off:

            ExtTurnOff=true;

            break;

         case Close_AllExt:

            ExtCloseAllExt=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            DeletePending();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            Sleep(SLEEPTIME*1000);

            CloseAll();

            DeletePending();

            ExpertRemoves();

            DeleteChart();

            break;

         case Close_All_All:

            ExtCloseAll=true;

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            Sleep(SLEEPTIME*1000);

            CloseAllProfit();

            CloseAllLoss();

            CloseAll();

            break;

         case CloseAll_Profit:

            ExtCloseAllProfit=true;

            CloseAllProfit();

            CloseAllProfit();

            CloseAllProfit();

            break;

         case  close_tpl_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  close_tpl_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case tpl:

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case close_all_tpl:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            Alert("DeleteChart ''"+Template_1+"' on "+m_symbol.Name());

            DeleteChart_1();

            break;

         case  UpName:

            ExtUpName=true;

            Obj_SELLSTOP();

            break;

         case  DownName:

            ExtDownName=true;

            Obj_BUYSTOP();

            break;

         case  UpName_s:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  UpName_b:

            ExtUpName=true;

            Obj_SELLSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_b:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  DownName_s:

            ExtDownName=true;

            Obj_BUYSTOP();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_buys:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            break;

         case  close_sells:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            break;

         case close_all:

            ExtNeedCloseAll=true;

            LongShortObjClosed();

            break;

         case open_buy:

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case open_sell:

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         case  close_open_b:

            ExtNeedCloseSell=true;

            ShortObjClosed();

            ExtNeedOpenBuy=true;

            LongObjOpened();

            break;

         case  close_open_s:

            ExtNeedCloseBuy=true;

            LongObjClosed();

            ExtNeedOpenSell=true;

            ShortObjOpened();

            break;

         default:

            ExtNeedOpenBuySell=true;

            LongShortObjOpened();

            break;

        }

      PlaySound("ok.wav");

      res=true;

     }

//--- result

   return(res);

  }

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

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 ---