Martingale Smart

Author: AHARON TZADIK

This code snippet appears to be part of a trading bot or automated trading system, likely written in a custom scripting language or a modified version of C/C++. Let's break down what it does, section by section.

Overall Purpose

The code seems to be designed to calculate the total profit generated by a series of orders. It iterates through a list of orders, checks certain conditions (order type, symbol, magic number), and accumulates the profit from valid orders.

1. Order Selection and Error Handling (First for loop)

for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
{
    if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
    {
        Print("Error");
        continue;
    }
    if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber())
    {
        continue;
    }
  • for(int cnt=OrdersTotal()-1; cnt>=0; cnt--): This loop iterates through the orders in reverse order. OrdersTotal() likely returns the total number of orders. Iterating in reverse is a common practice in some trading systems.
  • if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)): This line attempts to select the order at the current index (cnt). OrderSelect is a function that retrieves order details based on the provided index. SELECT_BY_POS indicates that the selection is based on the position in the order list. MODE_TRADES likely specifies that we're dealing with regular trades (as opposed to other types of orders). If OrderSelect fails (returns false), it means the order at that index is invalid or doesn't exist. The Print("Error") and continue statements handle this error by printing an error message and skipping to the next order.
  • if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber()): This condition checks if the order's symbol and magic number match the current symbol and magic number being processed. If they don't match, the order is skipped. This is a crucial filtering step to ensure that only orders related to the current trading strategy are considered.

2. Profit Calculation (Conditional Profit Accumulation)

if(OrderType() == OP_BUY || OrderType() == OP_SELL)
{
    Profit += OrderProfit();
}
  • if(OrderType() == OP_BUY || OrderType() == OP_SELL): This condition checks if the order is either a buy order (OP_BUY) or a sell order (OP_SELL). This ensures that only buy and sell orders contribute to the profit calculation.
  • Profit += OrderProfit(): If the order is a buy or sell order, its profit (OrderProfit()) is added to the Profit variable.

3. Function Return

return (Profit);
  • return (Profit): The function returns the total calculated Profit.

Key Concepts and Potential Issues

  • Custom Functions: The code relies heavily on custom functions like OrdersTotal(), OrderSelect(), OrderSymbol(), OrderMagicNumber(), OrderType(), OrderProfit(), Symbol(), and MagicNumber(). The behavior of these functions is critical to the correctness of the code, and their definitions are not provided in the snippet.
  • Order Types: OP_BUY and OP_SELL are likely constants representing the order types.
  • Magic Number: The "magic number" is a common concept in automated trading. It's a unique identifier associated with a specific trading strategy or bot. This allows the bot to filter orders and only process those that belong to its own strategy.
  • Reverse Iteration: Iterating through orders in reverse order might be done for a specific reason related to the order processing logic of the trading system.
  • Error Handling: The error handling is basic. A more robust system might log errors to a file or take other corrective actions.
  • Potential for Integer Overflow: If the OrderProfit() values are very large, the Profit variable could potentially overflow, leading to incorrect results. Using a larger data type (e.g., double) for Profit might be necessary.

In Summary

This code snippet is a core component of an automated trading system, responsible for calculating the total profit generated by a series of orders. It filters orders based on their type, symbol, and magic number, and accumulates the profit from valid orders. The code's correctness depends heavily on the definitions of the custom functions it uses.

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 strategyChecks for the total of closed ordersIt Closes Orders by itself
Indicators Used
Bollinger bands indicatorMACD Histogram
Miscellaneous
It issuies visual alerts to the screenIt sends emails
5 Views
0 Downloads
0 Favorites
Martingale Smart
ÿþ//+------------------------------------------------------------------+

//|              Martingale Smart                                    |

//|                   AHARON TZADIK                                  |

//|                                                                  |

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

#property copyright   "AHARON TZADIK"

#property link        "https://5d3071208c5e2.site123.me/"

#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

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

extern double     Multiply=2.0;             // Multiply for Martingale

extern double     Lotsize=0.01;              // Lot size

extern bool       Double_Lotsize=false;//set to true if you want to just double every lotsize,

extern double     Lot_Size_Increment=0.01;//Additional orders will increase by this amount

extern double     TrailingStop=30;          // TrailingStop pips distance

extern int        Stop_Loss=5;            // Stop Loss pips distance

extern int        Take_Profit=5;           // Take Profit pips distance

extern int        MA_PERIOD=1;             // Moving Average Period

extern int        MA_PERIOD1=50;             // Moving Average Period

extern int        MagicNumber=562148;       // Magic Number

extern string     Timeset="Timeset";

extern int        Start=0;                  // Start hour

extern int        End=24;                   // End hour

extern int        Maximum_loss=10;           // Maximum allowed loss

extern int        WaitTime=180;             // Wait time for loss in minutes

int count_0=0;

datetime  TimeSent;

double pips;

int           err;

int total=0;

int /*cnt,*/freeze_level;

//--- price levels for orders and positions

double priceopen,stoploss,takeprofit;

double LOTZ=0;

double  profit  = 0 ;

int iOrders=0;

bool STRATEGY=true;//STRATEGY 1 OR 2

int INDEX2=0;

double PROFIT_SUM1=0;

double PROFIT_SUM2=0;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

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

   if(ticksize==0.00001 || ticksize==0.001)

      pips=ticksize*10;

   else

      pips=ticksize;

   return(INIT_SUCCEEDED);

   freeze_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL);

   if(freeze_level!=0)

     {

      PrintFormat("SYMBOL_TRADE_FREEZE_LEVEL=%d: order or position modification is not allowed,"+

                  " if there are %d points to the activation price",freeze_level,freeze_level);

     }



  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

  }

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

//| Expert tick function                                             |

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

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

      if(Enable_Trailing==true)

     {TRAIL_PROFIT_IN_MONEY2();}  

// Check for New Bar (Compatible with both MQL4 and MQL5)

   static datetime dtBarCurrent=WRONG_VALUE;

   datetime dtBarPrevious=dtBarCurrent;

   dtBarCurrent=(datetime) SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);

   bool NewBarFlag=(dtBarCurrent!=dtBarPrevious);

   if(NewBarFlag)

     {

    //  stop();

   if(USEMOVETOBREAKEVEN)

      MOVETOBREAKEVEN();

      //--- check for history and trading

      if(Bars<100)

        {

         Print("bars less than 100");

         return;

        }

      if(IsTradeAllowed()==false)

        {

         Print("Trade IS NOT Allowed");

         return;

        }

      //----

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

      //|                          Martingale                              |

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

      ///////////////////////////////////////////////////////Martingale :////////////////////////////////////////////////

        {



           {



            //++++ Compute Martingale1 Last History Order

            int     tickets[],      nTickets = GetHistoryOrderByCloseTime(tickets);

            double LastLots=0;

            for(int iTicket = 0; iTicket < nTickets; iTicket++)

               if(OrderSelect(tickets[iTicket], SELECT_BY_TICKET))

                 {

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

                  if(LastLots<OrderLots())

                    {

                     LastLots=OrderLots();

                    }

                  //--- check for history and trading

                  if(Bars<100)

                    {

                     Print("bars less than 100");

                     return;

                    }

                  if(IsTradeAllowed()==false)

                    {

                     Print("Trade IS NOT Allowed");

                     return;

                    }

                  if(profit<0)

                    {

                     STRATEGY=!STRATEGY;

                     Print("profit",profit);

                     if(STRATEGY==false)

                       {

                        if(trade1()==2)

                          {

                           Open_Orders(OP_SELL,LastLots);//count_1++;

                           Trail1();



                          }



                        if(trade1()==1)

                          {

                           Open_Orders(OP_BUY,LastLots);//count_0++;

                           Trail1();



                          }

                       }



                     if(STRATEGY==true)

                       {

                        if(trade()==2)

                          {

                           Open_Orders(OP_SELL,LastLots);//count_1++;

                           Trail1();



                          }



                        if(trade()==1)

                          {

                           Open_Orders(OP_BUY,LastLots);//count_0++;

                           Trail1();



                          }

                       }





                    }

                  if(profit>0)

                    {

                     Print("profit",profit);

                     break;

                    }

                 }

           }

         if(getOpenOrders()==0)

           {

            if(AccountFreeMargin()<(10*Lotsize))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               return;

              }

            // Buy order

            if(trade()==1)

              {

               Open_Orders1(OP_BUY,Lotsize);

               Trail1();

              }

            // Sell Order

            if(trade()==2)

              {

               Open_Orders1(OP_SELL,Lotsize);

               Trail1();

              }

           }



        }

     }

  }

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

//|                           Open_OrderS                             |

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



//============================ Open_Order=======================================

int Open_Orders(int tip,double llots)//martingale

  {

   if(Double_Lotsize==TRUE)

     {

      LOTZ=Multiply*llots;

     }

   else

     {

      LOTZ= llots+Lot_Size_Increment;

     }

   int ticket=-1;

   if(openorderthispair(Symbol())==0)

     {

      if(tip==0)

        {

         while(ticket==-1)

           {

            if(AccountFreeMargin()<(10*LOTZ))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            if((CheckVolumeValue(LotsOptimizedMxs(LOTZ)))==TRUE)

               if((CheckMoneyForTrade(Symbol(),LotsOptimizedMxs(LOTZ),OP_BUY))==TRUE)

                  if((CheckStopLoss_Takeprofit(OP_BUY,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+Take_Profit*pips)))==TRUE)

                     ticket=OrderSend(Symbol(),OP_BUY,LotsOptimizedMxs(LOTZ),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+Take_Profit*pips),"Long 1",MagicNumber,0,PaleGreen);

            if(ticket>0)

              {

               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

               Alert("we just got a buy signal on the ",_Period,"M",_Symbol);

               SendNotification("we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

               SendMail("Order sent successfully","we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

              }

            // if(ticket>-1)

            break;



           }

        }



      if(tip==1)

        {

         ticket=-1;

         while(ticket==-1)

           {

            if(AccountFreeMargin()<(10*LOTZ))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            if((CheckVolumeValue(LotsOptimizedMxs(LOTZ)))==TRUE)

               if((CheckMoneyForTrade(Symbol(),LotsOptimizedMxs(LOTZ),OP_SELL))==TRUE)

                  if((CheckStopLoss_Takeprofit(OP_SELL,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-Take_Profit*pips)))==TRUE)



                     ticket=OrderSend(Symbol(),OP_SELL,LotsOptimizedMxs(LOTZ),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-Take_Profit*pips),"Short 1",MagicNumber,0,Red);

            if(ticket>0)

              {

               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

               Alert("we just got a sell signal on the ",_Period,"M",_Symbol);

               SendMail("Order sent successfully","we just got a sell signal on the "+(string)_Period+"M"+_Symbol);

              }

            // if(ticket>-1)

            break;



           }

        }

     }

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

   return(0);

  }

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

//|                           Open_OrderS1                            |

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

//============================ Open_Order1=======================================

int Open_Orders1(int tip,double llots)

  {



   int ticket=-1;

//if(OrdersTotal()==0)

     {

      if(tip==0)

        {

         while(ticket==-1)

           {

            if(AccountFreeMargin()<(10*llots))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            if((CheckVolumeValue(LotsOptimizedMxs(Lotsize)))==TRUE)

               if((CheckMoneyForTrade(Symbol(),LotsOptimizedMxs(Lotsize),OP_BUY))==TRUE)

                  if((CheckStopLoss_Takeprofit(OP_BUY,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+Take_Profit*pips)))==TRUE)

                     ticket=OrderSend(Symbol(),OP_BUY,LotsOptimizedMxs(Lotsize),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+Take_Profit*pips),"Long 1",MagicNumber,0,PaleGreen);

            if(ticket>0)

              {

               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

               Alert("we just got a buy signal on the ",_Period,"M",_Symbol);

               SendNotification("we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

               SendMail("Order sent successfully","we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

              }

            break;



           }

        }



      if(tip==1)

        {

         ticket=-1;

         while(ticket==-1)

           {

            if(AccountFreeMargin()<(10*llots))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            if((CheckVolumeValue(LotsOptimizedMxs(Lotsize)))==TRUE)

               if((CheckMoneyForTrade(Symbol(),LotsOptimizedMxs(Lotsize),OP_SELL))==TRUE)

                  if((CheckStopLoss_Takeprofit(OP_SELL,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-Take_Profit*pips)))==TRUE)

                     ticket=OrderSend(Symbol(),OP_SELL,LotsOptimizedMxs(Lotsize),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-Take_Profit*pips),"Short 1",MagicNumber,0,Red);

            if(ticket>0)

              {

               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

               Alert("we just got a sell signal on the ",_Period,"M",_Symbol);

               SendMail("Order sent successfully","we just got a sell signal on the "+(string)_Period+"M"+_Symbol);

              }

            break;



           }

        }

     }

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

   return(0);

  }

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

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

//|                           Open_Order                             |

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



//============================ Open_Order=======================================

int Open_Order(int tip,double llots)//martingale

  {

   if(Double_Lotsize==TRUE)

     {

      LOTZ=Multiply*llots;

     }

   else

     {

      LOTZ= llots+Lot_Size_Increment;

     }

   int ticket=-1;

//if(OrdersTotal()==0)

   if(openorderthispair(Symbol())==0)

     {

      if(tip==0)

        {

         while(ticket==-1)

           {



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

                (AccountFreeMarginCheck(Symbol(),OP_BUY,LotsOptimized1Mx(LOTZ))>AccountStopoutLevel()))

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

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

               if(AccountFreeMarginCheck(Symbol(),OP_BUY,LotsOptimized1Mx(LOTZ)))

                  if(CheckMoneyForTrade(Symbol(),LotsOptimized1Mx(LOTZ),OP_BUY))

                     if(ticket>0)

                       {

                        if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

                        Alert("we just got a buy signal on the ",_Period,"M",_Symbol);

                        SendNotification("we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

                        SendMail("Order sent successfully","we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

                       }

            if(AccountFreeMargin()<(10000*llots))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            ticket=OrderSend(Symbol(),OP_BUY,LotsOptimizedMx(LOTZ),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+Take_Profit*pips),"Long 1",MagicNumber,0,PaleGreen);

            if(ticket>-1)

               break;



           }

        }



      if(tip==1)

        {

         ticket=-1;

         while(ticket==-1)

           {



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

                (AccountFreeMarginCheck(Symbol(),OP_SELL,LotsOptimized1Mx(LOTZ))>AccountStopoutLevel()))

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

                   ((AccountEquity()/(AccountEquity()-AccountFreeMarginCheck(Symbol(),OP_SELL,LotsOptimized1Mx(LOTZ)))*100)>AccountStopoutLevel())))

               if(AccountFreeMarginCheck(Symbol(),OP_SELL,LotsOptimized1Mx(LOTZ)))

                  if(CheckMoneyForTrade(Symbol(),LotsOptimized1Mx(LOTZ),OP_SELL))

                     if(ticket>0)

                       {

                        if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

                        Alert("we just got a sell signal on the ",_Period,"M",_Symbol);

                        SendMail("Order sent successfully","we just got a sell signal on the "+(string)_Period+"M"+_Symbol);

                       }

            if(AccountFreeMargin()<(10000*llots))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            ticket=OrderSend(Symbol(),OP_SELL,LotsOptimizedMx(LOTZ),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-Take_Profit*pips),"Short 1",MagicNumber,0,Red);

            if(ticket>-1)

               break;



           }

        }

     }

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

   return(0);

  }

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

//|                           Open_Order1                            |

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

//============================ Open_Order1=======================================

int Open_Order1(int tip,double llots)

  {



   int ticket=-1;

//if(OrdersTotal()==0)

     {

      if(tip==0)

        {

         while(ticket==-1)

           {



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

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

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

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

               if(CheckMoneyForTrade(Symbol(),LotsOptimized1x(),OP_BUY))

                  if(ticket>0)

                    {

                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

                     Alert("we just got a buy signal on the ",_Period,"M",_Symbol);

                     SendNotification("we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

                     SendMail("Order sent successfully","we just got a buy signal on the1 "+(string)_Period+"M"+_Symbol);

                    }

            if(AccountFreeMargin()<(10000*llots))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            ticket=OrderSend(Symbol(),OP_BUY,LotsOptimizedMx(Lotsize),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+Take_Profit*pips),"Long 1",MagicNumber,0,PaleGreen);

            if(ticket>-1)

               break;



           }

        }



      if(tip==1)

        {

         ticket=-1;

         while(ticket==-1)

           {



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

                (AccountFreeMarginCheck(Symbol(),OP_SELL,llots)>AccountStopoutLevel()))

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

                   ((AccountEquity()/(AccountEquity()-AccountFreeMarginCheck(Symbol(),OP_SELL,llots))*100)>AccountStopoutLevel())))

               if(CheckMoneyForTrade(Symbol(),LotsOptimized1x(),OP_SELL))

                  if(ticket>0)

                    {

                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

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

                     Alert("we just got a sell signal on the ",_Period,"M",_Symbol);

                     SendMail("Order sent successfully","we just got a sell signal on the "+(string)_Period+"M"+_Symbol);

                    }

            if(AccountFreeMargin()<(10000*llots))//check for money ?

              {

               Print("We have no money. Free Margin = ",AccountFreeMargin());

               break;

              }

            ticket=OrderSend(Symbol(),OP_SELL,LotsOptimizedMx(Lotsize),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-Take_Profit*pips),"Short 1",MagicNumber,0,Red);

            if(ticket>-1)

               break;



           }

        }

     }

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

   return(0);

  }

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

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

     }

  }

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

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

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

           {

            //--- should it be closed?



            if(Close[1]==upperBB)

               exitbuys();

            //--- check for trailing stop

           }

         else // go to short position

           {

            //--- should it be closed?



            if(Close[1]==lowerBB)

               exitsells();

            //--- check for trailing stop

           }

        }

     }

  }

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

//              IfOrderDoesNotExistBuy

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

int IfOrderDoesNotExistBuy()

  {

   bool exists=false;

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

     {



      if(OrderSelect(i,SELECT_BY_POS)==true && OrderSymbol()==Symbol())

        {

         exists = true;

         return(exists);

        }

      else

        {

         Print("OrderSelect() error - ",(GetLastError()));

        }

     }



   if(exists==false)

     {

      //BuyOrderType();

      // return(exists);

     }

   return(0);

  }

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

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

int trade()

//trading conditions

  {

     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(iMA(Symbol(),0,MA_PERIOD,0,0,0,1)<iMA(Symbol(),0,MA_PERIOD1,0,0,0,1))//BUY

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

      return(1);

   else

      if(iMA(Symbol(),0,MA_PERIOD,0,0,0,1)>iMA(Symbol(),0,MA_PERIOD1,0,0,0,1))//SELL

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



         return(2);

   return(0);

  }

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

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

int trade1()

//trading conditions

  {

   if(iEnvelopes(NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0.2,MODE_LOWER,0)<Ask)//BUY

      return(1);

   else

      if(iEnvelopes(NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0.2,MODE_UPPER,0)>Ask)//SELL



         return(2);

   return(0);

  }

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

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

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

  }

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

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

double ND(double val)

  {

   return(NormalizeDouble(val, Digits));

  }

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

//| Calculate optimal lot size buy                                   |

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

double LotsOptimizedMx(double llots)

  {

   double lots=llots;

//--- minimal allowed volume for trade operations

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

   if(lots<minlot)

     {

      lots=minlot;

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

     }

//--- maximal allowed volume of trade operations

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

   if(lots>maxlot)

     {

      lots=maxlot;

      Print("Volume is greater than the maximal allowed,we use",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;



      Print("Volume is not a multiple of the minimal step ,we use the closest correct volume ",ratio*volume_step);

     }



   return(lots);



  }

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

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

  }

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

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

//| Calculate optimal lot size buy                                   |

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

double LotsOptimized1x()

  {

   double lots=Lotsize;

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

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

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

   return(lots);

  }

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

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

  }

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

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

  {

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

//-- if there is not enough money

// else if(free_margin<0)

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

        (AccountFreeMarginCheck(symb,type,lots)<AccountStopoutLevel()))

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

           ((AccountEquity()/(AccountEquity()-AccountFreeMarginCheck(Symbol(),OP_SELL,lots))*100)<AccountStopoutLevel()))))

     {

      Print("StopOut level  Not enough money for ",OP_SELL," ",lots," ",Symbol());

      return(false);

     }

   else

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

  }

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

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

  }

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

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

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

  }

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

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

//|                                      exitbuys()                  |

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

void exitbuys()

  {

   double result;

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

  {

   double result;

   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

              }



           }

        }



     }

  }



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

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

int GetHistoryOrderByCloseTime(int& tickets[], int dsc=1)

  {

#define ASCENDING -1

   /* https://www.mql5.com/en/forum/137936 zzuegg says history ordering "is not reliable

    * (as said in the doc)" [not in doc] dabbler says "the order of entries is

    * mysterious (by actual test)" */

   int nOrders = 0;

   datetime OCTs[];

   for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--)

      if(

         OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/

         &&  OrderMagicNumber()  == MagicNumber             // my magic number

         &&  OrderSymbol()       == Symbol()             // and my pair.

         &&  OrderType()         <= OP_SELL//Avoid cr/bal https://www.mql5.com/en/forum/126192

      )

        {

         int nextTkt = OrderTicket();

         datetime nextOCT = OrderCloseTime();

         nOrders++;

         ArrayResize(tickets,nOrders);

         ArrayResize(OCTs,nOrders);

         for(iOrders=nOrders - 1; iOrders > 0; iOrders--)       // Insertn sort.

           {

            datetime    prevOCT     = OCTs[iOrders-1];

            if((prevOCT - nextOCT) * dsc >= 0)

               break;

            int         prevTkt = tickets[iOrders-1];

            tickets[iOrders] = prevTkt;

            OCTs[iOrders] = prevOCT;

           }

         tickets[iOrders] = nextTkt;

         OCTs[iOrders] = nextOCT; // Insert.

        }

   return(nOrders);

  }

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

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

//| Calculate optimal lot size buy                                   |

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

double LotsOptimized1Mxs(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 buy                                   |

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

double LotsOptimizedMxs(double llots)

  {

   double lots=llots;

//--- minimal allowed volume for trade operations

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

   if(lots<minlot)

     {

      lots=minlot;

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

     }

//--- maximal allowed volume of trade operations

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

   if(lots>maxlot)

     {

      lots=maxlot;

      Print("Volume is greater than the maximal allowed,we use",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;



      Print("Volume is not a multiple of the minimal step ,we use the closest correct volume ",ratio*volume_step);

     }



   return(lots);



  }

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

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

  }

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

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

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

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

  {



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



              }

           }

        }

     }

   if(PROFIT_SUM1>= Take_Profit_Money)



     {



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

              }

           }

        }



     }



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

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

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

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

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