Colour line GEP

Author: Copyright © 2021, Aleksandr Klapatyuk
Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each bar
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
Colour line GEP
ÿþ//+------------------------------------------------------------------+

//|                                              Colour line GEP.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, Strx"

#property description "https://www.mql5.com/en/users/strx"

#property description "Copyright © 2021, Vladimir Karputov"

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

#property version     "1.003"

#define   UNO_MAGIC   0

//---

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\AccountInfo.mqh>

#include <Arrays\ArrayString.mqh>

#include <ChartObjects\ChartObject.mqh>

#include <ChartObjects\ChartObjectsLines.mqh>

//---

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

CTrade            m_trade;               // : trading object

CSymbolInfo       m_symbol;              // : symbol info object

CPositionInfo     m_position;            // : trade position object

CAccountInfo      m_account;             // : account info wrapper

CArrayString      m_objectUPPERBuy;      // : object info name

CArrayString      m_objectUPPERSell;     // : object info name

CArrayString      m_objectUPPERClosBuy;  // : object info name

CArrayString      m_objectUPPERClosSell; // : object info name

CArrayString      m_objectLOWERBuy;      // : object info name

CArrayString      m_objectLOWERSell;     // : object info name

CArrayString      m_objectLOWERClosBuy;  // : object info name

CArrayString      m_objectLOWERClosSell; // : object info name

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

//| ENUM_LOT_RISK                                                    |

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

enum LotMax

  {

   Lot=0,   // Lots

   Lotx2=1, // Lots*2

   Risk=2,  // Risk

  };

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

//| ENUM_GEP_MODE                                                        |

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

enum GepPr

  {

   GepB=0,  // Gep

   GepS=1,  // No Gep

  };

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

input group  "---- : Parameters:  ----"

input string Template                = "ADX";            // : Without '.tpl'

input double TargetProfit            = 1000000;          // : Balance + Profit(add to balance)

input double TargetLoss              = 0;                // : Balance - Loss(subtract from the balance)

input LotMax InpLotRisk              = Risk;             // : Lots,- Lots*2,- Risk

input double MaximumRisk             = 0.02;             // : Maximum Risk in percentage

input double DecreaseFactor          = 3;                // : Descrease factor

input double InpTProfit              = 40000;            // : Take Profit --> (In currency the amount)

input double InpSLoss                = 1000000;          // : Stop Loss --> (In currency the amount)

input group  "---- : Name Line Open:  ----"

input string InpObjUpName_op         = "open_UP";        // : Obj: Follows the price down (Horizontal Line)

input string InpObjDownName_op       = "open_LW";        // : Obj: Follows the price up (Horizontal Line)

input ushort InpObjTrailingStop_op   = 60;               // : Obj: Trailing Stop (distance from price to object, in pips)

input ushort InpObjTrailingStep_op   = 5;                // : Obj: Trailing Step, in pips (1.00045-1.00055=1 pips)

input group  "---- : Name Line Close:  ----"

input string InpObjUpName            = "close_UP";       // : Obj: Follows the price down (Horizontal Line)

input string InpObjDownName          = "close_LW";       // : Obj: Follows the price up (Horizontal Line)

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

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

input group  "---- : Mode Gep:  ----"

input GepPr  InpGepPrice             = GepS;             // : Gep: : <> :

input group  "---- : MonitoringColor UPPER:  ----"

input color  UPPERColorBuy           = clrSteelBlue;     // : Buy MonitoringColor, EA only monitors this lines color

input color  UPPERColorClosBuy       = clrMediumBlue;    // : Close Buy MonitoringColor

input color  UPPERColorSell          = clrGoldenrod;     // : Sell MonitoringColor, EA only monitors this lines color

input color  UPPERColorClosSell      = clrBrown;         // : Close Sell MonitoringColor

input group  "---- : MonitoringColor LOWER:  ----"

input color  LOWERColorBuy           = clrGold;          // : Buy MonitoringColor, EA only monitors this lines color

input color  LOWERColorClosBuy       = clrSlateGray;     // : Close Buy MonitoringColor

input color  LOWERColorSell          = clrYellow;        // : Sell MonitoringColor, EA only monitors this lines color

input color  LOWERColorClosSell      = clrPeru;          // : Close Sell MonitoringColor

//---

double   m_obj_trailing_stop_op = 0.0; // : Obj: Trailing Stop

double   m_obj_trailing_step_op = 0.0; // : Obj: Trailing Step

datetime m_prev_bars_op         = D'1970.01.01 00:00';

datetime m_obj_last_trailing_op = D'1970.01.01 00:00';

datetime m_ExtPrevBars_op       = D'1970.01.01 00:00';

datetime m_ExtLastSignals_op    = D'1970.01.01 00:00';

datetime m_ExtPrevBars_x_op     = D'1970.01.01 00:00';

datetime m_ExtLastSignals_x_op  = D'1970.01.01 00:00';

//---

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

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

datetime m_prev_bars            = D'1970.01.01 00:00';

datetime m_obj_last_trailing    = D'1970.01.01 00:00';

datetime m_ExtPrevBars          = D'1970.01.01 00:00';

datetime m_ExtLastSignals       = D'1970.01.01 00:00';

datetime m_ExtPrevBars_x        = D'1970.01.01 00:00';

datetime m_ExtLastSignals_x     = D'1970.01.01 00:00';

string   txt;

ENUM_TIMEFRAMES TimeFrame       = PERIOD_CURRENT;

string   m_name_but[]           = {"LW S","LW B","Del","UP B","UP S"};

string   m_name[]= {"Buy","Sell","CloseBuy","CloseSell","CLOSE_ALL","Trailing"};

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

//| Calculate optimal lot size                                       |

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

double TradeSizeOptimized(void)

  {

   double price=0.0;

   double margin=0.0;

//--- select lot size

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

      return(0.0);

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

      return(0.0);

   if(margin<=0.0)

      return(0.0);

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

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

   if(DecreaseFactor>0)

     {

      //--- select history for access

      HistorySelect(0,TimeCurrent());

      //---

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

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

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

        {

         ulong ticket=HistoryDealGetTicket(i);

         if(ticket==0)

           {

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

            break;

           }

         //--- check symbol

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

            continue;

         //--- check Expert Magic number

         if(HistoryDealGetInteger(ticket,DEAL_MAGIC)!=UNO_MAGIC)

            continue;

         //--- check profit

         double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);

         if(profit>0.0)

            break;

         if(profit<0.0)

            losses++;

        }

      //---

      if(losses>1)

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

     }

//--- normalize and check limits

   double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

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

   double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);

   if(lot<minvol)

      lot=minvol;

   double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);

   if(lot>maxvol)

      lot=maxvol;

//--- return trading volume

   return(lot);

  }

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

//| Calculate optimal lot size                                       |

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

double OptimizedBuy(void)

  {

   double PROFIT_BUY=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+m_position.Select(Symbol());

           }

        }

     }

   double Lots=MaximumRisk;

   double ab=PROFIT_BUY;

   switch(InpLotRisk)

     {

      case Lot:

         Lots=MaximumRisk;

         break;

      case Lotx2:

         if(ab>0 && ab<=1)

            Lots=MaximumRisk*2;

         if(ab>1 && ab<=2)

            Lots=MaximumRisk*4;

         if(ab>2 && ab<=3)

            Lots=MaximumRisk*8;

         if(ab>3)

            Lots=TradeSizeOptimized();

         break;

      case Risk:

         Lots=TradeSizeOptimized();

         break;

     }

   return(Lots);

  }

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

//| Calculate optimal lot size                                       |

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

double OptimizedSell(void)

  {

   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(m_position.PositionType()==POSITION_TYPE_SELL)

           {

            PROFIT_SELL=PROFIT_SELL+m_position.Select(Symbol());

           }

        }

     }

   double Lots=MaximumRisk;

   double ab=PROFIT_SELL;

   switch(InpLotRisk)

     {

      case Lot:

         Lots=MaximumRisk;

         break;

      case Lotx2:

         if(ab>0 && ab<=1)

            Lots=MaximumRisk*2;

         if(ab>1 && ab<=2)

            Lots=MaximumRisk*4;

         if(ab>2 && ab<=3)

            Lots=MaximumRisk*8;

         if(ab>3)

            Lots=TradeSizeOptimized();

         break;

      case Risk:

         Lots=TradeSizeOptimized();

         break;

     }

   return(Lots);

  }

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

//| Expert initialization function                                   |

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

int OnInit(void)

  {

//--- create timer

   EventSetTimer(1);

//--- initialize common information

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

      return(false);

   RefreshRates();

   m_trade.SetExpertMagicNumber(UNO_MAGIC); // magic

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(Symbol());

//--- 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_obj_trailing_stop_op = InpObjTrailingStop_op*m_adjusted_point;

   m_obj_trailing_step_op = InpObjTrailingStep_op*m_adjusted_point;

   m_obj_trailing_stop    = InpObjTrailingStop*m_adjusted_point;

   m_obj_trailing_step    = InpObjTrailingStep*m_adjusted_point;

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

   m_trade.SetDeviationInPoints(3*digits_adjust);

//---

   int _y=34;

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

     {

      ButtonCreate(m_name[i],5,_y,150,15,8);

      _y=_y+17;

     }

//---

   int but_y=5;

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

     {

      ButtonCreate(m_name_but[i],but_y,17,30,15,8);

      but_y=but_y+30;

     }

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

     }

//---

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

     {

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

     }

//---

   ObjectsDeleteAll(0,"cm");

//--- destroy timer

   EventKillTimer();

  }

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

//| Expert tick function                                             |

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

void OnTick(void)

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return;

   ProfitOnTick();

//---

   if(UPPERClosBuy() || UPPERClosSell() || UPPERBuy() || UPPERSell() ||

      LOWERClosBuy() || LOWERClosSell() || LOWERBuy() || LOWERSell() || OnButton())

     {

      return;

     }

//---

  }

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

//| ProfitOnTick closing                                             |

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

bool ProfitOnTick(void)

  {

   bool rv=false;

   if(AccountInfoDouble(ACCOUNT_EQUITY)<=TargetLoss || AccountInfoDouble(ACCOUNT_EQUITY)>=TargetProfit)

     {

      AllClose();

      OnDel();

      ExpertRemove();

      DeleteChart();

     }

//---

   double PROFIT_BUY=0.00,PROFIT_SELL=0.00;

   int total=PositionsTotal();

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

     {

      string   position_GetSymbol=PositionGetSymbol(i);

      if(position_GetSymbol==m_symbol.Name())

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

            PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);

           }

        }

     }

   if(PROFIT_BUY<-InpSLoss || PROFIT_BUY>=InpTProfit)

     {

      CheckForCloseBuy();

     }

//---

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

     {

      string   position_GetSymbol=PositionGetSymbol(i);

      if(position_GetSymbol==m_symbol.Name())

        {

         if(m_position.PositionType()==POSITION_TYPE_SELL)

           {

            PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);

           }

        }

     }

   if(PROFIT_SELL<-InpSLoss || PROFIT_SELL>=InpTProfit)

     {

      CheckForCloseSell();

     }

   return(rv);

  }

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

//| Check for open position conditions                               |

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

void CheckForOpenBuy(void)

  {

   double price=m_symbol.Ask();

//--- check for free money

   if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,OptimizedBuy(),price)<0.0)

      printf("We have no money. Free Margin = %f",m_account.FreeMargin());

   else

     {

      //--- open position

      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,OptimizedBuy(),price,0.0,0.0))

         printf("Position by %s to be opened",Symbol());

      else

        {

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

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

        }

      PlaySound("ok.wav");

     }

  }

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

//| Check for open position conditions                               |

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

void CheckForOpenSell(void)

  {

   double price=m_symbol.Bid();

//--- check for free money

   if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,OptimizedSell(),price)<0.0)

      printf("We have no money. Free Margin = %f",m_account.FreeMargin());

   else

     {

      //--- open position

      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,OptimizedSell(),price,0.0,0.0))

         printf("Position by %s to be opened",Symbol());

      else

        {

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

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

        }

      PlaySound("ok.wav");

     }

  }

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

//| Check for close position conditions                              |

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

void CheckForCloseBuy(void)

  {

//--- close position

   ClosePositions(POSITION_TYPE_BUY);

  }

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

//| Check for close position conditions                              |

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

void CheckForCloseSell(void)

  {

//--- close position

   ClosePositions(POSITION_TYPE_SELL);

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      return(false);

     }

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

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

     {

      return(false);

     }

   return(true);

  }

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

//| Check Freeze and Stops levels                                    |

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

void FreezeStopsLevels(double &freeze,double &stops)

  {

//--- check Freeze and Stops levels

   double coeff=(double)1;

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

      return;

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

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

   if(freeze_level==0.0)

      if(1>0)

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

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

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

   if(stop_level==0.0)

      if(1>0)

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

//---

   freeze=freeze_level;

   stops=stop_level;

   return;

  }

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

//| Close positions                                                  |

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

void ClosePositions(const ENUM_POSITION_TYPE pos_type)

  {

   double freeze=0.0,stops=0.0;

   FreezeStopsLevels(freeze,stops);

   int total=PositionsTotal();

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

      if(m_position.SelectByIndex(i))

         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==UNO_MAGIC)

            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()>=freeze) || m_position.TakeProfit()==0.0);

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

                  if(take_profit_level && stop_loss_level)

                     if(!m_trade.PositionClose(m_position.Ticket()))

                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());

                 }

               if(m_position.PositionType()==POSITION_TYPE_SELL)

                 {

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

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

                  if(take_profit_level && stop_loss_level)

                     if(!m_trade.PositionClose(m_position.Ticket()))

                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());

                 }

               PlaySound("ok.wav");

              }

  }

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

//| Expert tick function                                             |

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

void OnTickObjTrailing_op(void)

  {

   datetime time_current=TimeCurrent();

   if(time_current-m_obj_last_trailing_op>10)

     {

      if(RefreshRates())

         ObjTrailing_op();

      else

         return;

      m_obj_last_trailing_op=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_op)

      return;

   m_prev_bars_op=time_0;

   if(RefreshRates())

      ObjTrailing_op();

  }

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

//| Object Trailing                                                  |

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

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

void ObjTrailing_op(void)

  {

   if(InpObjTrailingStop_op==0)

      return;

   double m_obj_up_price     = 0.0;

   double m_obj_down_price   = 0.0;

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

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

   if(current_up_price>0.0)

     {

      if(current_up_price>(m_symbol.Ask()+(m_obj_trailing_stop_op+m_obj_trailing_step_op)))

        {

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

         HLineMove(0,InpObjUpName_op,m_obj_up_price);

        }

     }

   if(current_down_price>0.0)

     {

      if(current_down_price<m_symbol.Bid()-(m_obj_trailing_stop_op+m_obj_trailing_step_op))

        {

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

         HLineMove(0,InpObjDownName_op,m_obj_down_price);

        }

     }

  }

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

//| Expert tick function                                             |

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

void OnTickObjTrailing(void)

  {

   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(RefreshRates())

      ObjTrailing();

  }

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

//| Object Trailing                                                  |

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

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

void ObjTrailing(void)

  {

   if(InpObjTrailingStop==0)

      return;

   double m_obj_up_price     = 0.0;

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

        }

     }

  }

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

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

  }

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

//| main function returns true if any position processed             |

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

bool UPPERBuy(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(UPPERActiveObjectsBuy())

        {

         for(i=0; i<m_objectUPPERBuy.Total(); i++)

           {

            objName=m_objectUPPERBuy[i];

            if(UPPERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForOpenBuy();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool UPPERObjectBuy(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == UPPERColorBuy;

  }

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

//| Returns the list of object to be monitored

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

int UPPERActiveObjectsBuy(void)

  {

   m_objectUPPERBuy.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(UPPERObjectBuy(objName))

        {

         m_objectUPPERBuy.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(UPPERObjectBuy(objName))

        {

         m_objectUPPERBuy.Add(objName);

        }

     }

   return m_objectUPPERBuy.Total();

  }

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

//| main function returns true if any position processed             |

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

bool UPPERSell(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(UPPERActiveObjectsSell())

        {

         for(i=0; i<m_objectUPPERSell.Total(); i++)

           {

            objName=m_objectUPPERSell[i];

            if(UPPERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForOpenSell();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool UPPERObjectSell(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == UPPERColorSell;

  }

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

//| Returns the list of object to be monitored

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

int UPPERActiveObjectsSell(void)

  {

   m_objectUPPERSell.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(UPPERObjectSell(objName))

        {

         m_objectUPPERSell.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(UPPERObjectSell(objName))

        {

         m_objectUPPERSell.Add(objName);

        }

     }

   return m_objectUPPERSell.Total();

  }

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

//| main function returns true if any position processed             |

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

bool UPPERClosBuy(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(UPPERActiveObjectsClosBuy())

        {

         for(i=0; i<m_objectUPPERClosBuy.Total(); i++)

           {

            objName=m_objectUPPERClosBuy[i];

            if(UPPERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForCloseBuy();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool UPPERObjectClosBuy(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == UPPERColorClosBuy;

  }

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

//| Returns the list of object to be monitored

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

int UPPERActiveObjectsClosBuy(void)

  {

   m_objectUPPERClosBuy.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(UPPERObjectClosBuy(objName))

        {

         m_objectUPPERClosBuy.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(UPPERObjectClosBuy(objName))

        {

         m_objectUPPERClosBuy.Add(objName);

        }

     }

   return m_objectUPPERClosBuy.Total();

  }

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

//| main function returns true if any position processed             |

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

bool UPPERClosSell(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(UPPERActiveObjectsClosSell())

        {

         for(i=0; i<m_objectUPPERClosSell.Total(); i++)

           {

            objName=m_objectUPPERClosSell[i];

            if(UPPERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForCloseSell();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool UPPERObjectClosSell(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == UPPERColorClosSell;

  }

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

//| Returns the list of object to be monitored

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

int UPPERActiveObjectsClosSell(void)

  {

   m_objectUPPERClosSell.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(UPPERObjectClosSell(objName))

        {

         m_objectUPPERClosSell.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(UPPERObjectClosSell(objName))

        {

         m_objectUPPERClosSell.Add(objName);

        }

     }

   return m_objectUPPERClosSell.Total();

  }

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

//| Returns true if objName has been crossed up or down by current price

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

bool UPPERPriceCrossed(string objName)

  {

   bool rv=false;

   ENUM_OBJECT objType;

   double objPrice=0.0,openPrice,curPrice;

   objType=(ENUM_OBJECT)ObjectGetInteger(0,objName,OBJPROP_TYPE);

   if(objType==OBJ_HLINE)

     {

      objPrice= ObjectGetDouble(0, objName, OBJPROP_PRICE);

     }

   else

      if(objType==OBJ_TREND)

        {

         objPrice=ObjectGetValueByTime(0,objName,TimeCurrent(),0);

        }

   if(objPrice)

     {

      openPrice= iOpen(NULL,0,0);

      curPrice = iClose(NULL,0,0);

      if(InpGepPrice)

         return (openPrice<=objPrice && curPrice>objPrice) || (openPrice>=objPrice && curPrice<objPrice);

      else

         return (curPrice>objPrice);

     }

   return(rv);

  }

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

//| start function                                                   |

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

void AllClose(void)

  {

   int total=PositionsTotal();

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

      if(m_position.SelectByIndex(i))

        {

         ClosePosition(m_position.Symbol());

        }

  }

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

//| Script program start function                                    |

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

void OnDel(void)

  {

   long currChart,prevChart=ChartFirst();

   int i=0,limit=100;

   Print("ChartFirst = ",ChartSymbol(prevChart)," ID = ",prevChart);

   while(i<limit)

     {

      currChart=ChartNext(prevChart);

      ObjectDelete(prevChart,InpObjUpName);

      ObjectDelete(prevChart,InpObjDownName);

      ObjectDelete(prevChart,InpObjUpName_op);

      ObjectDelete(prevChart,InpObjDownName_op);

      if(currChart<0)

         break;

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

      prevChart=currChart;

      i++;

     }

  }

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

//| Close selected position                                          |

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

void ClosePosition(const string symbol)

  {

   if(TradeInit(symbol))

      m_trade.PositionClose(m_position.Ticket());

   PlaySound("request.wav");

  }

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

//| Init trade object                                                |

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

bool TradeInit(const string symbol)

  {

   if(!m_symbol.Name(symbol))

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

     }

  }

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

//| main function returns true if any position processed             |

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

bool LOWERBuy(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(LOWERActiveObjectsBuy())

        {

         for(i=0; i<m_objectLOWERBuy.Total(); i++)

           {

            objName=m_objectLOWERBuy[i];

            if(LOWERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForOpenBuy();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool LOWERObjectBuy(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == LOWERColorBuy;

  }

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

//| Returns the list of object to be monitored

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

int LOWERActiveObjectsBuy(void)

  {

   m_objectLOWERBuy.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(LOWERObjectBuy(objName))

        {

         m_objectLOWERBuy.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(LOWERObjectBuy(objName))

        {

         m_objectLOWERBuy.Add(objName);

        }

     }

   return m_objectLOWERBuy.Total();

  }

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

//| main function returns true if any position processed             |

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

bool LOWERSell(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(LOWERActiveObjectsSell())

        {

         for(i=0; i<m_objectLOWERSell.Total(); i++)

           {

            objName=m_objectLOWERSell[i];

            if(LOWERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForOpenSell();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool LOWERObjectSell(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == LOWERColorSell;

  }

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

//| Returns the list of object to be monitored

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

int LOWERActiveObjectsSell(void)

  {

   m_objectLOWERSell.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(LOWERObjectSell(objName))

        {

         m_objectLOWERSell.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(LOWERObjectSell(objName))

        {

         m_objectLOWERSell.Add(objName);

        }

     }

   return m_objectLOWERSell.Total();

  }

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

//| main function returns true if any position processed             |

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

bool LOWERClosBuy(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(LOWERActiveObjectsClosBuy())

        {

         for(i=0; i<m_objectLOWERClosBuy.Total(); i++)

           {

            objName=m_objectLOWERClosBuy[i];

            if(LOWERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForCloseBuy();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool LOWERObjectClosBuy(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == LOWERColorClosBuy;

  }

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

//| Returns the list of object to be monitored

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

int LOWERActiveObjectsClosBuy(void)

  {

   m_objectLOWERClosBuy.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(LOWERObjectClosBuy(objName))

        {

         m_objectLOWERClosBuy.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(LOWERObjectClosBuy(objName))

        {

         m_objectLOWERClosBuy.Add(objName);

        }

     }

   return m_objectLOWERClosBuy.Total();

  }

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

//| main function returns true if any position processed             |

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

bool LOWERClosSell(void)

  {

   bool rv=false;

   int i;

   string objName;

   if(m_symbol.RefreshRates())

     {

      if(LOWERActiveObjectsClosSell())

        {

         for(i=0; i<m_objectLOWERClosSell.Total(); i++)

           {

            objName=m_objectLOWERClosSell[i];

            if(LOWERPriceCrossed(objName))

              {

               ObjectSetInteger(0,objName,OBJPROP_COLOR,clrGreen);

               CheckForCloseSell();

              }

           }

        }

     }

   return(rv);

  }

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

//| Returns true if object has to be monitored based on color and

//| previous crosses

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

bool LOWERObjectClosSell(string objName)

  {

   int objColor=(int)ObjectGetInteger(0,objName,OBJPROP_COLOR,0);

   return objColor == LOWERColorClosSell;

  }

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

//| Returns the list of object to be monitored

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

int LOWERActiveObjectsClosSell(void)

  {

   m_objectLOWERClosSell.Clear();

   int nHLines=ObjectsTotal(0,-1,OBJ_HLINE),

       nTrendLines=ObjectsTotal(0,-1,OBJ_TREND),

       i;

   string objName;

   for(i=0; i<nHLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_HLINE);

      if(LOWERObjectClosSell(objName))

        {

         m_objectLOWERClosSell.Add(objName);

        }

     }

   for(i=0; i<nTrendLines; i++)

     {

      objName=ObjectName(0,i,0,OBJ_TREND);

      if(LOWERObjectClosSell(objName))

        {

         m_objectLOWERClosSell.Add(objName);

        }

     }

   return m_objectLOWERClosSell.Total();

  }

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

//| Returns true if objName has been crossed up or down by current price

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

bool LOWERPriceCrossed(string objName)

  {

   bool rv=false;

   ENUM_OBJECT objType;

   double objPrice=0.0,openPrice,curPrice;

   objType=(ENUM_OBJECT)ObjectGetInteger(0,objName,OBJPROP_TYPE);

   if(objType==OBJ_HLINE)

     {

      objPrice= ObjectGetDouble(0, objName, OBJPROP_PRICE);

     }

   else

      if(objType==OBJ_TREND)

        {

         objPrice=ObjectGetValueByTime(0,objName,TimeCurrent(),0);

        }

   if(objPrice)

     {

      openPrice= iOpen(NULL,0,0);

      curPrice = iClose(NULL,0,0);

      if(InpGepPrice)

         return (openPrice<=objPrice && curPrice>objPrice) || (openPrice>=objPrice && curPrice<objPrice);

      else

         return (curPrice<objPrice);

     }

   return(rv);

  }

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

//|  Creating a horizontal price level                               |

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

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

  {

   int    err = GetLastError();

   string lnName = name;

   err = 0;

   if(ObjectFind(0,lnName)!=-1)

      ObjectDelete(0,lnName);

   if(!ObjectCreate(ch_id,lnName,OBJ_HLINE,sub_window,0,price))

     {

      err = GetLastError();

      Print("Can't create object #", lnName, "# Error(",err,"):", err);

      return(err);

     }

   ObjectSetInteger(ch_id,lnName,OBJPROP_COLOR,clr);

   ObjectSetInteger(ch_id,lnName,OBJPROP_STYLE,style);

   ObjectSetInteger(ch_id,lnName,OBJPROP_WIDTH,width);

   ObjectSetInteger(ch_id,lnName,OBJPROP_BACK,back);

   ObjectSetInteger(ch_id,lnName,OBJPROP_SELECTABLE,selectable);

   ObjectSetInteger(ch_id,lnName,OBJPROP_SELECTED,selected);

   ObjectSetInteger(ch_id,lnName,OBJPROP_HIDDEN,hidden);

   ObjectSetInteger(ch_id,lnName,OBJPROP_ZORDER,z_order);

   return(true);

  }

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

//    buttoncreate                                                   |

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

bool ButtonCreate(string name,int Xdist,

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

  {

   bool rv=false;

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

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

   ObjectSetInteger(0,Symbol()+name,OBJPROP_COLOR,clrWhite);

   ObjectSetInteger(0,Symbol()+name,OBJPROP_BGCOLOR,clrDimGray);

   ObjectSetInteger(0,Symbol()+name,OBJPROP_XDISTANCE,Xdist);

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

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

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

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

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

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

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

   return(rv);

  }

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

//| Timer function                                                   |

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

void OnTimer(void)

  {

   double BY_1=m_symbol.Bid()-m_obj_trailing_stop_op;

   double SL_1=m_symbol.Ask()+m_obj_trailing_stop_op;

   double BY_2=m_symbol.Bid()-m_obj_trailing_stop;

   double SL_2=m_symbol.Ask()+m_obj_trailing_stop;

//---

   if(ObjectGetInteger(0,Symbol()+"LW B",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"LW B",OBJPROP_STATE,0);

      CreateHline(0,0,InpObjUpName_op,BY_1,LOWERColorBuy,0,0,1,1,1,1,2);

      CreateHline(0,0,InpObjDownName,BY_2-m_obj_trailing_stop,LOWERColorClosBuy,0,0,1,1,1,1,2);

      PlaySound("tick.wav");

     }

   if(ObjectGetInteger(0,Symbol()+"LW S",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"LW S",OBJPROP_STATE,0);

      CreateHline(0,0,InpObjUpName_op,BY_2,LOWERColorSell,0,0,1,1,1,1,2);

      CreateHline(0,0,InpObjUpName,SL_1,UPPERColorClosSell,0,0,1,1,1,1,2);

      PlaySound("tick.wav");

     }

   if(ObjectGetInteger(0,Symbol()+"UP B",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"UP B",OBJPROP_STATE,0);

      CreateHline(0,0,InpObjDownName,BY_2,LOWERColorClosBuy,0,0,1,1,1,1,2);

      CreateHline(0,0,InpObjDownName_op,SL_1,UPPERColorBuy,0,0,1,1,1,1,2);

      PlaySound("tick.wav");

     }

   if(ObjectGetInteger(0,Symbol()+"UP S",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"UP S",OBJPROP_STATE,0);

      CreateHline(0,0,InpObjDownName_op,SL_1,UPPERColorSell,0,0,1,1,1,1,2);

      CreateHline(0,0,InpObjUpName,SL_2+m_obj_trailing_stop,UPPERColorClosSell,0,0,1,1,1,1,2);

      PlaySound("tick.wav");

     }

   if(ObjectGetInteger(0,Symbol()+"Del",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"Del",OBJPROP_STATE,0);

      ObjectDelete(0,InpObjUpName);

      ObjectDelete(0,InpObjDownName);

      ObjectDelete(0,InpObjUpName_op);

      ObjectDelete(0,InpObjDownName_op);

      PlaySound("tick.wav");

     }

  }

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

//| Expert tick function                                             |

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

bool OnButton(void)

  {

   bool rv=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;

//---

   if(ObjectGetInteger(0,Symbol()+"Buy",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"Buy",OBJPROP_STATE,0);

      CheckForOpenBuy();

     }

   if(ObjectGetInteger(0,Symbol()+"CloseBuy",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"CloseBuy",OBJPROP_STATE,0);

      CheckForCloseBuy();

     }

   if(ObjectGetInteger(0,Symbol()+"Sell",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"Sell",OBJPROP_STATE,0);

      CheckForOpenSell();

     }

   if(ObjectGetInteger(0,Symbol()+"CloseSell",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"CloseSell",OBJPROP_STATE,0);

      CheckForCloseSell();

     }

   if(ObjectGetInteger(0,Symbol()+"CLOSE_ALL",OBJPROP_STATE)!=0)

     {

      ObjectSetInteger(0,Symbol()+"CLOSE_ALL",OBJPROP_STATE,0);

      AllClose();

      OnDel();

     }

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

     {

      OnTickObjTrailing_op();

      OnTickObjTrailing();

     }

//---

   int total=PositionsTotal();

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

     {

      string   position_GetSymbol=PositionGetSymbol(i);

      if(position_GetSymbol==m_symbol.Name())

        {

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

           }

        }

     }

//---

   ObjectSetString(0,Symbol()+"Buy",OBJPROP_TEXT,"BUY = Lot ("+DoubleToString(PROFIT_BUY_Lot,2)+")");

   if(PROFIT_BUY_Lot>0)

     {

      ObjectSetInteger(0,Symbol()+"Buy",OBJPROP_BGCOLOR,clrDarkSlateGray);

     }

   else

      if(PROFIT_BUY_Lot==0)

        {

         ObjectSetInteger(0,Symbol()+"Buy",OBJPROP_BGCOLOR,clrDimGray);

        }

//---

   ObjectSetString(0,Symbol()+"CloseBuy",OBJPROP_TEXT,"Close_Buy =("+DoubleToString(PROFIT_BUY,2)+")");

   if(PROFIT_BUY>0)

     {

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

     }

   else

      if(PROFIT_BUY==0)

        {

         ObjectSetInteger(0,Symbol()+"CloseBuy",OBJPROP_BGCOLOR,clrDimGray);

        }

      else

        {

         ObjectSetInteger(0,Symbol()+"CloseBuy",OBJPROP_BGCOLOR,clrCrimson);

        }

//---

   ObjectSetString(0,Symbol()+"Sell",OBJPROP_TEXT,"SELL = Lot ("+DoubleToString(PROFIT_SELL_Lot,2)+")");

   if(PROFIT_SELL_Lot>0)

     {

      ObjectSetInteger(0,Symbol()+"Sell",OBJPROP_BGCOLOR,clrDarkSlateGray);

     }

   else

      if(PROFIT_SELL_Lot==0)

        {

         ObjectSetInteger(0,Symbol()+"Sell",OBJPROP_BGCOLOR,clrDimGray);

        }

//---

   ObjectSetString(0,Symbol()+"CloseSell",OBJPROP_TEXT,"Close_Sell =("+DoubleToString(PROFIT_SELL,2)+")");

   if(PROFIT_SELL>0)

     {

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

     }

   else

      if(PROFIT_SELL==0)

        {

         ObjectSetInteger(0,Symbol()+"CloseSell",OBJPROP_BGCOLOR,clrDimGray);

        }

      else

        {

         ObjectSetInteger(0,Symbol()+"CloseSell",OBJPROP_BGCOLOR,clrCrimson);

        }

//---

   ObjectSetString(0,Symbol()+"CLOSE_ALL",OBJPROP_TEXT,"CLOSE_ALL =("+DoubleToString(PROFIT_CLOSE,2)+")"

                   +" :all="+DoubleToString(PositionsTotal(),0));

   if(PROFIT_CLOSE>0)

     {

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

     }

   else

      if(PROFIT_CLOSE==0)

        {

         ObjectSetInteger(0,Symbol()+"CLOSE_ALL",OBJPROP_BGCOLOR,clrDimGray);

        }

      else

        {

         ObjectSetInteger(0,Symbol()+"CLOSE_ALL",OBJPROP_BGCOLOR,clrCrimson);

        }

//---

   StringConcatenate(txt,"BUY_UPPER: ",DoubleToString(m_objectUPPERBuy.Total(),2));

   DrawLABEL(3,"cm 0",txt,5,15,UPPERColorBuy,ANCHOR_RIGHT_UPPER);

   StringConcatenate(txt,"CLOSE BUY_UPPER: ",DoubleToString(m_objectUPPERClosBuy.Total(),2));

   DrawLABEL(3,"cm 1",txt,5,30,UPPERColorClosBuy,ANCHOR_RIGHT_UPPER);

   StringConcatenate(txt,"SELL_UPPER: ",DoubleToString(m_objectUPPERSell.Total(),2));

   DrawLABEL(3,"cm 2",txt,5,45,UPPERColorSell,ANCHOR_RIGHT_UPPER);

   StringConcatenate(txt,"CLOSE SELL_UPPER: ",DoubleToString(m_objectUPPERClosSell.Total(),2));

   DrawLABEL(3,"cm 3",txt,5,60,UPPERColorClosSell,ANCHOR_RIGHT_UPPER);

//---

   StringConcatenate(txt,"BUY_LOWER: ",DoubleToString(m_objectLOWERBuy.Total(),2));

   DrawLABEL(2,"cm 4",txt,5,60,LOWERColorBuy,ANCHOR_RIGHT_LOWER);

   StringConcatenate(txt,"CLOSE BUY_LOWER: ",DoubleToString(m_objectLOWERClosBuy.Total(),2));

   DrawLABEL(2,"cm 5",txt,5,45,LOWERColorClosBuy,ANCHOR_RIGHT_LOWER);

   StringConcatenate(txt,"SELL_LOWER: ",DoubleToString(m_objectLOWERSell.Total(),2));

   DrawLABEL(2,"cm 6",txt,5,30,LOWERColorSell,ANCHOR_RIGHT_LOWER);

   StringConcatenate(txt,"CLOSE SELL_LOWER: ",DoubleToString(m_objectLOWERClosSell.Total(),2));

   DrawLABEL(2,"cm 7",txt,5,15,LOWERColorClosSell,ANCHOR_RIGHT_LOWER);

//---

   return(rv);

  }

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

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

  }

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

Comments