Trend Line By Angle

Author: AHARON TZADIK
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself Checks for the total of closed orders
Indicators Used
MACD HistogramBollinger bands indicator
0 Views
0 Downloads
0 Favorites
Trend Line By Angle
ÿþ//+------------------------------------------------------------------+

//|                          Trend Line By Angle                     |

//|             http://algorithmic-trading-ea-mt4.000webhostapp.com/ |

//|                                                    AHARON TZADIK |

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

#property copyright   "AHARON TZADIK"

#property link        "http://algorithmic-trading-ea-mt4.000webhostapp.com/"

#property version   "1.00"

#property strict









extern bool   Use_TP_In_Money=false;

extern double TP_In_Money=10;

extern bool   Use_TP_In_percent=false;

extern double TP_In_Percent=10;

//--------------------------------------------------------------------

//--------------------------------------------------------------------

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

input    string               txt32                    = "------------[Money Trailing Stop]---------------"; //                                           

input    bool                 Enable_Trailing           = true; //Enable_Trailing

input    double               Take_Profit_Money       = 40.0; //Take Profit In Money (in current currency)

input    double               Stop_Loss_Money         = 10; //Stop Loss In Money(in current currency)

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

//---------------------------------------------------------------------------------

extern bool        Exit=false;//Enable Exit strategy

//--- description

#property description "Script draws \"Trend Line By Angle\" graphical object."

#property description "Anchor point coordinates are set in percentage of the size of"

#property description "the chart window."

//--- display window of the input parameters during the script's launch

#property script_show_inputs

//--- input parameters of the script

input string          InpName="Trend";     // Line name

input int             InpDate1=50;         // 1 st point's date, %

input int             InpPrice1=75;        // 1 st point's price, %

input int             InpAngle=0;          // Line's slope angle

input color           InpColor=clrRed;     // Line color

input ENUM_LINE_STYLE InpStyle=STYLE_DASH; // Line style

input int             InpWidth=1;          // Line width

input bool            InpBack=false;       // Background line

input bool            InpSelection=true;   // Highlight to move

input bool            InpRayRight=true;    // Line's continuation to the right

input bool            InpHidden=true;      // Hidden in the object list

input long            InpZOrder=0;         // Priority for mouse click

extern bool           USESTOP=true;//Enable "STOP"

extern double         Lots=0.01;              //Lots size

input  double         MaximumRisk   =0.02;

input  double         DecreaseFactor=3;

extern double         Stop_Loss=20;           //Stop Loss

input  double         TakeProfit=50;          //TakeProfit

input  int            MagicNumber=1111;

input  int            NumberOfTrades=3;

extern double         TrailingStop=40;        //TrailingStop

extern bool           USEMOVETOBREAKEVEN=true;//Enable "no loss"

extern double         WHENTOMOVETOBE=10;      //When to move break even

extern double         PIPSTOMOVESL=5;         //How much pips to move sl

//input  int           Multiply=3;

double buyprice;

bool result;

double priceopen,stoploss,takeprofit;

int           ticket,err,T;

double        pips;

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

int total=0;

double Lot;

int INDEX2=0;

double PROFIT_SUM1=0;

double PROFIT_SUM2=0;

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

//| Expert initialization function                                   |

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



int OnInit()



//-----------------------------------------------------------------------------------------------------------

  {

   b1();

   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);

   if(ticksize==0.00001 || Point==0.01)

      pips=ticksize*10;

   else

      pips=ticksize;



   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

   ObjectDelete("BUY");

   ObjectDelete("SELL");

   ObjectDelete("CLOSE");

  }

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

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

//|                                                                  |

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

void OnTick()

  {

      if(Exit=true)

  {

      Close_BUY();

      Close_SELL();

  }

   if(Use_TP_In_Money)

     { Take_Profit_In_Money();}

   if(Use_TP_In_percent)

     { Take_Profit_In_percent();}

//--- check correctness of the input parameters

   if(InpDate1<0 || InpDate1>100 || InpPrice1<0 || InpPrice1>100)

     {

      Print("Error! Incorrect values of input parameters!");

      return;

     }

//--- number of visible bars in the chart window

   int bars=(int)ChartGetInteger(0,CHART_VISIBLE_BARS);

//--- price array size

   int accuracy=1000;

//--- arrays for storing the date and price values to be used

//--- for setting and changing line anchor points' coordinates

   datetime date[];

   double   price[];

//--- memory allocation

   ArrayResize(date,bars);

   ArrayResize(price,accuracy);

//--- fill the array of dates

   ResetLastError();

   if(CopyTime(Symbol(),Period(),0,bars,date)==-1)

     {

      Print("Failed to copy time values! Error code = ",GetLastError());

      return;

     }

//--- fill the array of prices

//--- find the highest and lowest values of the chart

   double max_price=ChartGetDouble(0,CHART_PRICE_MAX);

   double min_price=ChartGetDouble(0,CHART_PRICE_MIN);

//--- define a change step of a price and fill the array

   double step=(max_price-min_price)/accuracy;

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

      price[i]=min_price+i*step;

//--- define points for drawing the line

   int d1=InpDate1*(bars-1)/100;

   int p1=InpPrice1*(accuracy-1)/100;

//--- create a trend line

   if(!TrendByAngleCreate(0,InpName,0,date[d1],price[p1],InpAngle,InpColor,InpStyle,

                          InpWidth,InpBack,InpSelection,InpRayRight,InpHidden,InpZOrder))

     {

      return;

     }

//--- redraw the chart and wait for 1 second

   ChartRedraw();

   Sleep(1000);

//--- now, move and rotate the line

//--- loop counter

   int v_steps=accuracy/2;

//--- move the anchor point and change the line's slope angle

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

     {

      //--- use the following value

      if(p1>1)

         p1-=1;

      //--- move the point

      if(!TrendPointChange(0,InpName,date[d1],price[p1]))

         return;

      if(!TrendAngleChange(0,InpName,18*(i+1)))

         return;

      //--- check if the script's operation has been forcefully disabled

      if(IsStopped())

         return;

      //--- redraw the chart

      ChartRedraw();

     }

//--- 1 second of delay

   Sleep(1000);

//--- delete from the chart

   TrendDelete(0,InpName);

   ChartRedraw();

//--- 1 second of delay

   Sleep(1000);

//---



//---

   if(USESTOP)

      stop();

   if(USEMOVETOBREAKEVEN)

      MOVETOBREAKEVEN();

   Trail1();

//------------------------------------------------------------------------------

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

   double  MacdMAIN0=iMACD(NULL,PERIOD_MN1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   double  MacdSIGNAL0=iMACD(NULL,PERIOD_MN1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);

   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="BUY")

     {

      if((MacdMAIN0>0 && MacdMAIN0>MacdSIGNAL0) || (MacdMAIN0<0 && MacdMAIN0>MacdSIGNAL0)) 

      BUY();

     }

   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="SELL")

     {    

      if((MacdMAIN0>0 && MacdMAIN0<MacdSIGNAL0) || (MacdMAIN0<0 && MacdMAIN0<MacdSIGNAL0))

      SELL();

     }

   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="CLOSE")

     {

      exitbuys();

      exitsells();

     }

//---



  }

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

//---  (BUY) possibility

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

//| BUY                      BUY                 BUY                 |

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

//------------------------------------------------------------------------------------

void BUY()

  {

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

     {

      ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+TakeProfit*pips),"BUY",MagicNumber+i,0,PaleGreen);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("BUY order opened : ",OrderOpenPrice());

        }

      else

         Print("Error opening BUY order : ",GetLastError());

      // return;

     }

  }

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

//--- (SELL) possibility

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

//| SELL             SELL                       SELL                 |

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

//------------------------------------------------------------------------------------

void SELL()

  {

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

     {

      ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-TakeProfit*pips),"SELL",MagicNumber+i,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("SELL order opened : ",OrderOpenPrice());

        }

      else

         Print("Error opening SELL order : ",GetLastError());

      //return;

     }

  }

//--- exit from the "no opened orders" block



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

//|   stop                                                           |

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

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

void stop()

  {

   int cnt;

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

   double middleBB=iBands(Symbol(),0,20, 2,0,0,MODE_MAIN,1);//middle

   double lowerBB=iBands(Symbol(),0,20, 2,0,0,MODE_LOWER,1);//lower

   double upperBB=iBands(Symbol(),0,20, 2,0,0,MODE_UPPER,1);//upper

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

   double  MacdMAIN=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   double  MacdSIGNAL=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);

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

   for(cnt=0; cnt<total; cnt++)

     {

      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))

         continue;

      if(OrderType()<=OP_SELL &&   // check for opened position

         OrderSymbol()==Symbol())  // check for symbol

        {

         //--- long position is opened

         if(OrderType()==OP_BUY)

           {

            //--- should it be closed?



            if(Close[1]==upperBB)

               exitbuys1();

            //--- check for trailing stop

           }

         else // go to short position

           {

            //--- should it be closed?



            if(Close[1]==lowerBB)

               exitsells1();

            //--- check for trailing stop

           }

        }

     }

  }

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

//| Trailing stop loss                                               |

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

// --------------- ----------------------------------------------------------- ------------------------

void Trail1()

  {

   total=OrdersTotal();

//--- it is important to enter the market correctly, but it is more important to exit it correctly...

   for(int cnt=0; cnt<total; cnt++)

     {

      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))

         continue;

      if(OrderType()<=OP_SELL &&   // check for opened position

         OrderSymbol()==Symbol())  // check for symbol

        {

         //--- long position is opened

         if(OrderType()==OP_BUY)

           {



            //--- check for trailing stop

            if(TrailingStop>0)

              {

               if(Bid-OrderOpenPrice()>pips*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-pips*TrailingStop)

                    {



                     RefreshRates();

                     stoploss=Bid-(pips*TrailingStop);

                     takeprofit=OrderTakeProfit()+pips*TrailingStop;

                     double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);

                     if(stoploss<StopLevel*pips)

                        stoploss=StopLevel*pips;

                     string symbol=OrderSymbol();

                     double point=SymbolInfoDouble(symbol,SYMBOL_POINT);

                     if(MathAbs(OrderStopLoss()-stoploss)>point)

                        if((pips*TrailingStop)>(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL)*pips)



                           //--- modify order and exit

                           if(CheckStopLoss_Takeprofit(OP_BUY,stoploss,takeprofit))

                              if(OrderModifyCheck(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit))

                                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Green))

                                    Print("OrderModify error ",GetLastError());

                     return;

                    }

                 }

              }

           }

         else // go to short position

           {

            //--- check for trailing stop

            if(TrailingStop>0)

              {

               if((OrderOpenPrice()-Ask)>(pips*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+pips*TrailingStop)) || (OrderStopLoss()==0))

                    {



                     RefreshRates();

                     stoploss=Ask+(pips*TrailingStop);

                     takeprofit=OrderTakeProfit()-pips*TrailingStop;

                     double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);

                     if(stoploss<StopLevel*pips)

                        stoploss=StopLevel*pips;

                     if(takeprofit<StopLevel*pips)

                        takeprofit=StopLevel*pips;

                     string symbol=OrderSymbol();

                     double point=SymbolInfoDouble(symbol,SYMBOL_POINT);

                     if(MathAbs(OrderStopLoss()-stoploss)>point)

                        if((pips*TrailingStop)>(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL)*pips)



                           //--- modify order and exit

                           if(CheckStopLoss_Takeprofit(OP_SELL,stoploss,takeprofit))

                              if(OrderModifyCheck(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit))

                                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Red))

                                    Print("OrderModify error ",GetLastError());

                     return;

                    }

                 }

              }

           }

        }

     }

  }

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

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

//|                          MOVE TO BREAK EVEN                               |

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

void MOVETOBREAKEVEN()



  {

   for(int b=OrdersTotal()-1; b>=0; b--)

     {

      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))

         if(OrderMagicNumber()!=MagicNumber)

            continue;

      if(OrderSymbol()==Symbol())

         if(OrderType()==OP_BUY)

            if(Bid-OrderOpenPrice()>WHENTOMOVETOBE*pips)

               if(OrderOpenPrice()>OrderStopLoss())

                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PIPSTOMOVESL*pips),OrderTakeProfit(),0,CLR_NONE))

                     Print("eror");

     }



   for(int s=OrdersTotal()-1; s>=0; s--)

     {

      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))

         if(OrderMagicNumber()!=MagicNumber)

            continue;

      if(OrderSymbol()==Symbol())

         if(OrderType()==OP_SELL)

            if(OrderOpenPrice()-Ask>WHENTOMOVETOBE*pips)

               if(OrderOpenPrice()<OrderStopLoss())

                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PIPSTOMOVESL*pips),OrderTakeProfit(),0,CLR_NONE))

                     Print("eror");

     }

  }

//--------------------------------------------------------------------------------------

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

//|                    exit                                          |

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

void exit()

  {

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {



         //if(OrderType()==OP_SELL)

           {

            if((Ask==OrderTakeProfit()) || Bid==OrderStopLoss())//If one order closed than close all

               if(OrderType()==OP_SELL)

                 {

                  result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red);//actual order closing

                  if(result!=true)//if it did not close

                    {

                     err=GetLastError();

                     Print("LastError = ",err);//get the reason why it didn't close

                    }

                 }

               else

                  if(OrderType()==OP_BUY)

                    {

                     result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red);//actual order closing

                     if(result!=true)//if it did not close

                       {

                        err=GetLastError();

                        Print("LastError = ",err);//get the reason why it didn't close

                       }

                    }

           }



        }

     }

  }

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

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

int openorderthispair(string pair)

  {

   total=0;

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

     {

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         Print("eror");//ÑÕÓçÙÝ ÐÝ ÙéàÔ âáçÔ äêÕ×Ô ÑæÞÓ ÔàÕÛ×Ù

      if(OrderSymbol()==pair)

         total++;

     }

   return(total);

  }

//-----------------------------------------------------------------------------------------------------------



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

bool CheckMoneyForTrade(string symb,double lots,int type)

  {

   double free_margin=AccountFreeMarginCheck(symb,type,lots);

//-- if there is not enough money

   if(free_margin<0)

     {

      string oper=(type==OP_BUY)? "Buy":"Sell";

      Print("Not enough money for ",oper," ",lots," ",symb," Error code=",GetLastError());

      return(false);

     }

//--- checking successful

   return(true);

  }

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

//| Check the correctness of the order volume                        |

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

bool CheckVolumeValue(double volume/*,string &description*/)



  {

   double lot=volume;

   int    orders=OrdersHistoryTotal();     // history orders total

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

//--- select lot size

//--- maximal allowed volume of trade operations

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(lot>max_volume)



      Print("Volume is greater than the maximal allowed ,we use",max_volume);

//  return(false);



//--- minimal allowed volume for trade operations

   double minlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(lot<minlot)



      Print("Volume is less than the minimal allowed ,we use",minlot);

//  return(false);



//--- get minimal step of volume changing

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

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

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

     {

      Print("Volume is not a multiple of the minimal step ,we use, the closest correct volume is %.2f",

            volume_step,ratio*volume_step);

      //   return(false);

     }

//  description="Correct volume value";

   return(true);

  }

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

//| Calculate optimal lot size buy                                   |

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

double LotsOptimized1Mx(double llots)

  {

   double lots=llots;

//--- minimal allowed volume for trade operations

   double minlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(lots<minlot)

     { lots=minlot; }

//--- maximal allowed volume of trade operations

   double maxlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(lots>maxlot)

     { lots=maxlot;  }

//--- get minimal step of volume changing

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

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

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

     {  lots=ratio*volume_step;}

   if(((AccountStopoutMode()==1) &&

       (AccountFreeMarginCheck(Symbol(),OP_BUY,lots)>AccountStopoutLevel()))

      || ((AccountStopoutMode()==0) &&

          ((AccountEquity()/(AccountEquity()-AccountFreeMarginCheck(Symbol(),OP_BUY,lots))*100)>AccountStopoutLevel())))

      return(lots);

   /* else  Print("StopOut level  Not enough money for ",OP_SELL," ",lot," ",Symbol());*/

   return(0);

  }

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

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

int b1()

  {

   int chart_ID=0;

   string name="BUY";

   if(!ObjectCreate(0,name,OBJ_BUTTON,0,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create the button! Error code = ",GetLastError());

      return(false);

     }

//--- set button coordinates

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,150);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,50);

//--- set button size

   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,100);

   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,100);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,0);

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,"BUY");

//--- set text font

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

//--- set font size

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,12);

//--- set text color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clrBlue);

//--- set background color

   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,clrGray);

//--- set border color

   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,clrBlack);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,false);

//--- set button state

   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,false);

//--- enable (true) or disable (false) the mode of moving the button by mouse

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,false);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,false);

//--- hide ( true)or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,false);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,0);

//--- successful execution



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

//| SELL                                                             |

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

   string name1="SELL";

   if(!ObjectCreate(0,name1,OBJ_BUTTON,0,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create the button! Error code = ",GetLastError());

      return(false);

     }

//--- set button coordinates

   ObjectSetInteger(chart_ID,name1,OBJPROP_XDISTANCE,250);

   ObjectSetInteger(chart_ID,name1,OBJPROP_YDISTANCE,50);

//--- set button size

   ObjectSetInteger(chart_ID,name1,OBJPROP_XSIZE,100);

   ObjectSetInteger(chart_ID,name1,OBJPROP_YSIZE,100);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name1,OBJPROP_CORNER,0);

//--- set the text

   ObjectSetString(chart_ID,name1,OBJPROP_TEXT,"SELL");

//--- set text font

   ObjectSetString(chart_ID,name1,OBJPROP_FONT,"Arial");

//--- set font size

   ObjectSetInteger(chart_ID,name1,OBJPROP_FONTSIZE,12);

//--- set text color

   ObjectSetInteger(chart_ID,name1,OBJPROP_COLOR,clrRed);

//--- set background color

   ObjectSetInteger(chart_ID,name1,OBJPROP_BGCOLOR,clrGray);

//--- set border color

   ObjectSetInteger(chart_ID,name1,OBJPROP_BORDER_COLOR,clrBlack);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name1,OBJPROP_BACK,false);

//--- set button state

   ObjectSetInteger(chart_ID,name1,OBJPROP_STATE,false);

//--- enable (true) or disable (false) the mode of moving the button by mouse

   ObjectSetInteger(chart_ID,name1,OBJPROP_SELECTABLE,false);

   ObjectSetInteger(chart_ID,name1,OBJPROP_SELECTED,false);

//--- hide ( true)or display (false) graphical object name1 in the object list

   ObjectSetInteger(chart_ID,name1,OBJPROP_HIDDEN,false);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name1,OBJPROP_ZORDER,0);

//--- successful execution



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

//| CLOSE                                                             |

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

   string name2="CLOSE";

   if(!ObjectCreate(0,name2,OBJ_BUTTON,0,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create the button! Error code = ",GetLastError());

      return(false);

     }

//--- set button coordinates

   ObjectSetInteger(chart_ID,name2,OBJPROP_XDISTANCE,350);

   ObjectSetInteger(chart_ID,name2,OBJPROP_YDISTANCE,50);

//--- set button size

   ObjectSetInteger(chart_ID,name2,OBJPROP_XSIZE,100);

   ObjectSetInteger(chart_ID,name2,OBJPROP_YSIZE,100);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name2,OBJPROP_CORNER,0);

//--- set the text

   ObjectSetString(chart_ID,name2,OBJPROP_TEXT,"CLOSE");

//--- set text font

   ObjectSetString(chart_ID,name2,OBJPROP_FONT,"Arial");

//--- set font size

   ObjectSetInteger(chart_ID,name2,OBJPROP_FONTSIZE,12);

//--- set text color

   ObjectSetInteger(chart_ID,name2,OBJPROP_COLOR,clrRed);

//--- set background color

   ObjectSetInteger(chart_ID,name2,OBJPROP_BGCOLOR,clrGray);

//--- set border color

   ObjectSetInteger(chart_ID,name2,OBJPROP_BORDER_COLOR,clrBlack);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name2,OBJPROP_BACK,false);

//--- set button state

   ObjectSetInteger(chart_ID,name2,OBJPROP_STATE,false);

//--- enable (true) or disable (false) the mode of moving the button by mouse

   ObjectSetInteger(chart_ID,name2,OBJPROP_SELECTABLE,false);

   ObjectSetInteger(chart_ID,name2,OBJPROP_SELECTED,false);

//--- hide ( true)or display (false) graphical object name1 in the object list

   ObjectSetInteger(chart_ID,name2,OBJPROP_HIDDEN,false);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name2,OBJPROP_ZORDER,0);

//--- successful execution

   return(true);





  }

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

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

double NDTP(double val)

  {

   RefreshRates();

   double SPREAD=MarketInfo(Symbol(),MODE_SPREAD);

   double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL);

   if(val<StopLevel*pips+SPREAD*pips)

      val=StopLevel*pips+SPREAD*pips;

// double STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);

//int Stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);



//if (Stops_level*pips<val-Bid)

//val=Ask+Stops_level*pips;

   return(NormalizeDouble(val, Digits));

// return(val);

  }

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

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

//| Calculate optimal lot size buy                                   |

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

double LotsOptimized1Mx1(double llots)

  {

   double lots=llots;

//--- minimal allowed volume for trade operations

   double minlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(lots<minlot)

     { lots=minlot; }

//--- maximal allowed volume of trade operations

   double maxlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(lots>maxlot)

     { lots=maxlot;  }

//--- get minimal step of volume changing

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

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

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

     {  lots=ratio*volume_step;}

   if(((AccountStopoutMode()==1) &&

       (AccountFreeMarginCheck(Symbol(),OP_BUY,lots)>AccountStopoutLevel()))

      || ((AccountStopoutMode()==0) &&

          ((AccountEquity()/(AccountEquity()-AccountFreeMarginCheck(Symbol(),OP_BUY,lots))*100)>AccountStopoutLevel())))

      return(lots);

   /* else  Print("StopOut level  Not enough money for ",OP_SELL," ",lot," ",Symbol());*/

   return(0);

  }

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

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

//| Calculate optimal lot size                                       |

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

double LotsOptimized()

  {

   double lot=Lots;

   int    orders=OrdersHistoryTotal();     // history orders total

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

//--- select lot size

   if(MaximumRisk>0)

     {

      lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);

     }

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

   if(DecreaseFactor>0)

     {

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

        {

         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)

           {

            Print("Error in history!");

            break;

           }

         if(OrderSymbol()!=Symbol() /*|| OrderType()>OP_SELL*/)

            continue;

         //---

         if(OrderProfit()>0)

            break;

         if(OrderProfit()<0)

            losses++;

        }

      if(losses>1)

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

     }

//--- minimal allowed volume for trade operations

   double minlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(lot<minlot)

     { lot=minlot; }

// Print("Volume is less than the minimal allowed ,we use",minlot);}

// lot=minlot;



//--- maximal allowed volume of trade operations

   double maxlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(lot>maxlot)

     { lot=maxlot;  }

//  Print("Volume is greater than the maximal allowed,we use",maxlot);}

// lot=maxlot;



//--- get minimal step of volume changing

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

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

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

     {  lot=ratio*volume_step;}

   return(lot);

   /* else  Print("StopOut level  Not enough money for ",OP_SELL," ",lot," ",Symbol());

      return(0);*/

  }

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



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

double ND(double val)

  {

   return(NormalizeDouble(val, Digits));

  }

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

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

int getOpenOrders()

  {



   int Orders=0;

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)

        {

         continue;

        }

      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber)

        {

         continue;

        }

      Orders++;

     }

   return(Orders);

  }

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

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

//| Checking the new values of levels before order modification      |

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

bool OrderModifyCheck(int Ticket,double price,double sl,double tp)

  {

//--- select order by ticket

   if(OrderSelect(Ticket,SELECT_BY_TICKET))

     {

      //--- point size and name of the symbol, for which a pending order was placed

      string symbol=OrderSymbol();

      double point=SymbolInfoDouble(symbol,SYMBOL_POINT);

      //--- check if there are changes in the Open price

      bool PriceOpenChanged=true;

      int type=OrderType();

      if(!(type==OP_BUY || type==OP_SELL))

        {

         PriceOpenChanged=(MathAbs(OrderOpenPrice()-price)>point);

        }

      //--- check if there are changes in the StopLoss level

      bool StopLossChanged=(MathAbs(OrderStopLoss()-sl)>point);

      //--- check if there are changes in the Takeprofit level

      bool TakeProfitChanged=(MathAbs(OrderTakeProfit()-tp)>point);

      //--- if there are any changes in levels

      if(PriceOpenChanged || StopLossChanged || TakeProfitChanged)

         return(true);  // order can be modified

      //--- there are no changes in the Open, StopLoss and Takeprofit levels

      else

         //--- notify about the error

         PrintFormat("Order #%d already has levels of Open=%.5f SL=%.5f TP=%.5f",

                     Ticket,OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());

     }

//--- came to the end, no changes for the order

   return(false);       // no point in modifying

  }

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

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

bool CheckStopLoss_Takeprofit(ENUM_ORDER_TYPE type,double SL,double TP)

  {

//--- get the SYMBOL_TRADE_STOPS_LEVEL level

   int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);

   if(stops_level!=0)

     {

      PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must"+

                  " not be nearer than %d points from the closing price",stops_level,stops_level);

     }

//---

   bool SL_check=false,TP_check=false;

//--- check only two order types

   switch(type)

     {

      //--- Buy operation

      case  ORDER_TYPE_BUY:

        {

         //--- check the StopLoss

         SL_check=(Bid-SL>stops_level*_Point);

         if(!SL_check)

            PrintFormat("For order %s StopLoss=%.5f must be less than %.5f"+

                        " (Bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)",

                        EnumToString(type),SL,Bid-stops_level*_Point,Bid,stops_level);

         //--- check the TakeProfit

         TP_check=(TP-Bid>stops_level*_Point);

         if(!TP_check)

            PrintFormat("For order %s TakeProfit=%.5f must be greater than %.5f"+

                        " (Bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)",

                        EnumToString(type),TP,Bid+stops_level*_Point,Bid,stops_level);

         //--- return the result of checking

         return(SL_check&&TP_check);

        }

      //--- Sell operation

      case  ORDER_TYPE_SELL:

        {

         //--- check the StopLoss

         SL_check=(SL-Ask>stops_level*_Point);

         if(!SL_check)

            PrintFormat("For order %s StopLoss=%.5f must be greater than %.5f "+

                        " (Ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)",

                        EnumToString(type),SL,Ask+stops_level*_Point,Ask,stops_level);

         //--- check the TakeProfit

         TP_check=(Ask-TP>stops_level*_Point);

         if(!TP_check)

            PrintFormat("For order %s TakeProfit=%.5f must be less than %.5f "+

                        " (Ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)",

                        EnumToString(type),TP,Ask-stops_level*_Point,Ask,stops_level);

         //--- return the result of checking

         return(TP_check&&SL_check);

        }

      break;

     }

//--- a slightly different function is required for pending orders

   return false;

  }

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



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

//|                                      exitbuys()                  |

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

void exitbuys()

  {

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderType()==OP_BUY && OrderSymbol()==Symbol()/* && OrderMagicNumber()==MagicNumber*/)

           {

            result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);

            if(result!=true)//if it did not close

              {

               err=GetLastError();

               Print("LastError = ",err);//get the reason why it didn't close

              }



           }

        }



     }

  }

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

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

//|                    exitsells()                                   |

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

void exitsells()

  {

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {



         if(OrderType()==OP_SELL && OrderSymbol()==Symbol()/* && OrderMagicNumber()==MagicNumber*/)

           {

            result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);

            if(result!=true)//if it did not close

              {

               err=GetLastError();

               Print("LastError = ",err);//get the reason why it didn't close

              }



           }

        }



     }

  }

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

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

//|                                      exitbuys()                  |

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

void exitbuys1()

  {

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

           {

            result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);

            if(result!=true)//if it did not close

              {

               err=GetLastError();

               Print("LastError = ",err);//get the reason why it didn't close

              }



           }

        }



     }

  }

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

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

//|                    exitsells()                                   |

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

void exitsells1()

  {

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {



         if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

           {

            result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);

            if(result!=true)//if it did not close

              {

               err=GetLastError();

               Print("LastError = ",err);//get the reason why it didn't close

              }



           }

        }



     }

  }

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

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

//| Create a trend line by angle                                     |

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

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

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

                        const int             sub_window=0,      // subwindow index

                        datetime              time=0,            // point time

                        double                price=0,           // point price

                        const double          angle=45.0,        // slope angle

                        const color           clr=clrRed,        // line color

                        const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                        const int             width=1,           // line width

                        const bool            back=false,        // in the background

                        const bool            selection=true,    // highlight to move

                        const bool            ray_right=true,    // line's continuation to the right

                        const bool            hidden=true,       // hidden in the object list

                        const long            z_order=0)         // priority for mouse click

  {

//--- create the second point to facilitate dragging the trend line by mouse

   datetime time2=0;

   double   price2=0;

//--- set anchor points' coordinates if they are not set

   ChangeTrendEmptyPoints(time,price,time2,price2);

//--- reset the error value

   ResetLastError();

//--- create a trend line using 2 points

   if(!ObjectCreate(chart_ID,name,OBJ_TRENDBYANGLE,sub_window,time,price,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create a trend line! Error code = ",GetLastError());

      return(false);

     }

//--- change trend line's slope angle; when changing the angle, coordinates of the second

//--- point of the line are redefined automatically according to the angle's new value

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the line by mouse

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- enable (true) or disable (false) the mode of continuation of the line's display to the right

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution

   return(true);

  }

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

//| Change trend line anchor point's coordinates                     |

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

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

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

                      datetime     time=0,           // anchor point time coordinate

                      double       price=0)          // anchor point price coordinate

  {

//--- if point position is not set, move it to the current bar having Bid price

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move trend line's anchor point

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

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Change trend line's slope angle                                  |

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

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

                      const string name="TrendLine", // trend line name

                      const double angle=45)         // trend line's slope angle

  {

//--- reset the error value

   ResetLastError();

//--- change trend line's slope angle

   if(!ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle))

     {

      Print(__FUNCTION__,

            ": failed to change the line's slope angle! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Delete the trend line                                            |

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

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

                 const string name="TrendLine") // line name

  {

//--- reset the error value

   ResetLastError();

//--- delete a trend line

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a trend line! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Check the values of trend line's anchor points and set default   |

//| values for empty ones                                            |

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

void ChangeTrendEmptyPoints(datetime &time1,double &price1,

                            datetime &time2,double &price2)

  {

//--- if the first point's time is not set, it will be on the current bar

   if(!time1)

      time1=TimeCurrent();

//--- if the first point's price is not set, it will have Bid value

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- set coordinates of the second, auxiliary point

//--- the second point will be 9 bars left and have the same price

   datetime second_point_time[10];

   CopyTime(Symbol(),Period(),time1,10,second_point_time);

   time2=second_point_time[0];

   price2=price1;

  }

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

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

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

//----------------------------------------- TP_In_Money -----------------------------------------------

void Take_Profit_In_Money()

  {

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

//|                                                                  |

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

   if((TP_In_Money != 0))

     {

         PROFIT_SUM1 = 0;

      for(int i=OrdersTotal(); i>0; i--)

        {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

           {

            if(OrderSymbol()==Symbol())

              {

               if(OrderType() == OP_BUY || OrderType() == OP_SELL)

                 {

                  PROFIT_SUM1 = (PROFIT_SUM1 + OrderProfit());

                 }

              }

           }

        }

      if((PROFIT_SUM1 >= TP_In_Money))

        {

         RemoveAllOrders();

        }

     }

  }

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

//------------------------------------------------ TP_In_Percent -------------------------------------------------

double Take_Profit_In_percent()

  {

   if((TP_In_Percent != 0))

     {

      double TP_Percent = ((TP_In_Percent * AccountBalance()) / 100);

      double  PROFIT_SUM = 0;

      for(int i=OrdersTotal(); i>0; i--)

        {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

           {

            if(OrderSymbol()==Symbol())

              {

               if(OrderType() == OP_BUY || OrderType() == OP_SELL)

                 {

                  PROFIT_SUM = (PROFIT_SUM + OrderProfit());

                 }

              }

           }

         if(PROFIT_SUM >= TP_Percent)

           {

            RemoveAllOrders();

           }

        }



     }

   return(0);

  }

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

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

//       CLOSE &&  Remove  All    Orders

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

void RemoveAllOrders()

  {

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

     {

      if(!OrderSelect(i,SELECT_BY_POS))

         Print("ERROR");

      if(OrderSymbol() != Symbol())

         continue;

      double price = MarketInfo(OrderSymbol(),MODE_ASK);

      if(OrderType() == OP_BUY)

         price = MarketInfo(OrderSymbol(),MODE_BID);

      if(OrderType() == OP_BUY || OrderType() == OP_SELL)

        {

         if(!OrderClose(OrderTicket(), OrderLots(),price,5))

            Print("ERROR");

        }

      else

        {

         if(!OrderDelete(OrderTicket()))

            Print("ERROR");

        }

      Sleep(100);

      int error = GetLastError();

      // if(error > 0)

      // Print("Unanticipated error: ", ErrorDescription(error));

      RefreshRates();

     }

  }

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

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

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

//|                                                                  |

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

int TRAIL_PROFIT_IN_MONEY2()

  {

// double TP_Percent = ((TP_In_Money * AccountBalance()) / 100);

// double SL_Percent = ((SL_In_Money * AccountBalance()) / 100);

   PROFIT_SUM1 = 0;

   PROFIT_SUM2 = 0;

   INDEX2 = 0;

   double PROFIT_SUM3 = 0;

   for(int j=OrdersTotal(); j>0; j--)

     {

      if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol())

           {

            if(OrderType() == OP_BUY || OrderType() == OP_SELL)

              {

               PROFIT_SUM1  = PROFIT_SUM1 + (OrderProfit() + OrderCommission() + OrderSwap());

               // Print("PROFIT_SUM1",PROFIT_SUM1);

              }

           }

        }

     }

   if(PROFIT_SUM1>= Take_Profit_Money)

      // Print("PROFIT_SUM1",PROFIT_SUM1);

     {



      for(int i=OrdersTotal(); i>0; i--)

        {

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

           {

            if(OrderSymbol()==Symbol())

              {



               if(OrderType() == OP_BUY || OrderType() == OP_SELL)





                 {

                  PROFIT_SUM2  = PROFIT_SUM2 + (OrderProfit() + OrderCommission() + OrderSwap());

                 }

               if(PROFIT_SUM1>= PROFIT_SUM3)

                 {PROFIT_SUM3=PROFIT_SUM1;}

               if(PROFIT_SUM2<=PROFIT_SUM3-Stop_Loss_Money)

                  RemoveAllOrders();

              }

           }

        }



     }

// if(PROFIT_SUM2<=SL_In_Money)

//  RemoveAllOrders();

   return(0);

  }

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

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

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

int Close_BUY()

  {

//-------------------------------------------------------------------------------------------------

   double  MacdMAIN0=iMACD(NULL,PERIOD_MN1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   double  MacdSIGNAL0=iMACD(NULL,PERIOD_MN1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);

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

   int count_0 = 0;

   for(int pos_4 = OrdersTotal() - 1; pos_4 >= 0; pos_4--)

     {

      bool   cg = OrderSelect(pos_4, SELECT_BY_POS, MODE_TRADES);

      if(OrderSymbol() != Symbol())

         continue;

      if(OrderSymbol() == Symbol())

         if(OrderType() == OP_BUY)

            if((MacdMAIN0>0 && MacdMAIN0<MacdSIGNAL0) || (MacdMAIN0<0 && MacdMAIN0<MacdSIGNAL0))

               Close_All_Buy_Trades();



     }

   return (count_0);

  }

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

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

int Close_SELL()

  {

//-------------------------------------------------------------------------------------------------

   double  MacdMAIN0=iMACD(NULL,PERIOD_MN1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   double  MacdSIGNAL0=iMACD(NULL,PERIOD_MN1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);

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

   int count_0 = 0;

   for(int pos_4 = OrdersTotal() - 1; pos_4 >= 0; pos_4--)

     {

      bool   cg = OrderSelect(pos_4, SELECT_BY_POS, MODE_TRADES);

      if(OrderSymbol() != Symbol() )

         continue;

      if(OrderSymbol() == Symbol())

         if(OrderType() == OP_SELL)

             if((MacdMAIN0>0 && MacdMAIN0>MacdSIGNAL0) || (MacdMAIN0<0 && MacdMAIN0>MacdSIGNAL0)) 

               Close_All_Sell_Trades();

     }

   return (count_0);

  }

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

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

//|                                                                  |

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

void Close_All_Buy_Trades()

  {

   double CurrentPairProfit=CalculateProfit();

   for(int trade=OrdersTotal()-1; trade>=0; trade--)

     {

      if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

         Print("Error");

      if(OrderSymbol()==Symbol())

        {

         if(OrderSymbol()==Symbol() )

           {

            if(OrderType() == OP_BUY)

               if(!OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue))

                  Print("Error");

              if(OrderType() == OP_SELL)

                if(!OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red))

               Print("Error");

           }

         Sleep(1000);





        }

     }

  }

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

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

//|                                                                  |

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

void Close_All_Sell_Trades()

  {

   double CurrentPairProfit=CalculateProfit();

   for(int trade=OrdersTotal()-1; trade>=0; trade--)

     {

      if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

         Print("Error");

      if(OrderSymbol()==Symbol())

        {

         if(OrderSymbol()==Symbol())

           {

             if(OrderType() == OP_BUY)

             if(!OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue))

                Print("Error");

            if(OrderType() == OP_SELL)

               if(!OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red))

                  Print("Error");

           }

         Sleep(1000);





        }

     }

  }

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

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

double CalculateProfit()

  {

   double Profit=0;

   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)

     {

      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))

         Print("Error");

      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber)

         continue;

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

         if(OrderType()==OP_BUY || OrderType()==OP_SELL)

            Profit+=OrderProfit();

     }

   return (Profit);

  }

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

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