send order

 

hi guys , i wanted programing a expert that do trade after close the current open order, but it some times dont work, do u have any idea whats the problem?



as u see it opens many trades in same time,but not before 15:18




 
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
int stop= 190;
int tp= 380;
double a[18];
static double n ;
int x ;
char y=0;
int lo,hi =0;
double fasele1,fasele2=0;
//=========================================================
double lot(int q)
  {
   a[0]=n*(0.26);
   a[1]=n*(0.65);
   a[2]=n*(1.26);
   a[3]=0.01;
   a[4]=0.01;
   a[5]=0.01;
   a[6]=0.01;
   a[7]=0.01;
   a[8]=0.01;
   a[9]=0.01;
   a[10]=0.01;
   a[11]=0.01;
   a[12]=0.01;
   a[13]=0.01;
   a[14]=0.01;
   a[15]=0.01;
   a[16]=0.01;
   a[17]=0.01;
//Print("khar ke nistam"+n+"margi =  "+ AccountBalance());
   return a[q];
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(OrdersTotal()==0)
     {
      
       double bozorgtare =0;
       //Print ("bozorgtare = "+bozorgtare);
      for(int i=1;NormalizeDouble(bozorgtare,Digits)<=0.00570 ; i++)
        {

         hi=iHighest(NULL,PERIOD_H1,MODE_HIGH,i,0);
         fasele1=High[hi]-Bid;
          lo=iLowest(NULL,PERIOD_H1,MODE_LOW,i,0);
         fasele2=Bid-Low[lo];
         if(NormalizeDouble(fasele1,Digits)>NormalizeDouble(fasele2,Digits))bozorgtare=fasele1;
         else if(NormalizeDouble(fasele2,Digits)>NormalizeDouble(fasele1,Digits))bozorgtare=fasele2;  
            
            char first=0;
         //Print("first "+first);
         if(first==0)
           {
            //Print("i = "+i);
          //  Print("fasele1 ="+fasele1);
          //  Print("fasele 2 ="+fasele2);
          //  Print(bozorgtare);
            if(fasele1>=0.0057&& first==0)
              {
               first++;
               open(OP_BUY,Ask,clrGreen,stop,tp,Bid);
                lo=0;hi =0; fasele1=0;fasele2=0;
      
              }

          //  Print("fasele 2 ="+fasele2);
            
            if(fasele2>=0.0057&& first==0)
              {
               first++;
               open(OP_SELL,Bid,clrRed,-stop,-tp,Ask);
               lo=0;hi =0; fasele1=0;fasele2=0;
              }
           }

        }
     }

  }
//+------------------------------------------------------------------+
void history_x_n()
  {
   char limit =0;
   char last_profit=0;
   if(OrdersHistoryTotal()>0)
     {
      for(int i=OrdersHistoryTotal()-1; i>=0; i--)
        {
         bool pes =  OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
         if(OrderMagicNumber()==1111)
           {
            if(OrderProfit()<0 && limit ==0)
              {
               last_profit ++;
              }
            else
               if(OrderProfit()>=0 && limit==0)
                 {
                  limit++;
                 }
           }
        }
      x=last_profit-y;
      if(x>0)
        {
         if(OrderSelect(OrdersHistoryTotal()-x,SELECT_BY_POS,MODE_HISTORY))
            n=NormalizeDouble((StrToDouble(OrderComment())/10000),2);
        }

      if(x<=0)
        {
         SendNotification("YOUR ACCOUNT BALANCE NOW IS = "+DoubleToStr(AccountBalance(),1));
         if(AccountBalance()>=3000.0)
            SendNotification("CHANGE YOUR ACCONT TO ECN/MODIFY THE TP AND STOP");
         y=0;
         n=NormalizeDouble((AccountBalance()/10000),2);
        }
     }
   else
      if(OrdersHistoryTotal()<=0)
        {
         n=NormalizeDouble((AccountBalance()/10000),2);
        }
  }
//==============================================================
bool status()
  {
   if(OrdersTotal()>0)
      return false;
   else
      return true;
  }
//==========================================================
void open(int type,double price,color cl,int s,int t,double end)
  {
   history_x_n();
   double spread= Ask-Bid;
   int tick =OrderSend(Symbol(),type,lot(x),price,1,0,0,DoubleToStr(AccountBalance(),1),1111,0,cl);
   if(tick<=0)
     {
      Print("ERROR FOR SEND ORDER = " +IntegerToString(GetLastError()));
     }
   else
     {
      bool select= OrderSelect(tick,SELECT_BY_TICKET,MODE_TRADES);
      bool mod=OrderModify(tick,OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-(s*Point+spread),Digits),NormalizeDouble
                           (OrderOpenPrice()+ (t*Point-spread),Digits),0,clrWhite);
      if(mod==false)
        {
         bool closes =OrderClose(tick,OrderLots(),end,1);
         if(closes)
           {
            y++;
            SendNotification("MODIFY FAILED / I CLOSE THE ORDER,/n, MODIFY ERROR ");
           }
         else
           {
            SendNotification("MODIFY FAILED / I cant CLOSE THE ORDER,/n, CHECK YOURSELF NOW");
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
static double n ;
int x ;
char y=0;
int lo,hi =0;
double fasele1,fasele2=0;
  1. Globally declared variables are all static. Remove the redundant static, as it is confusing.
  2. n, x, lo, and fase1e1, all have random values.
  3.       for(int i=OrdersHistoryTotal()-1; i>=0; i--)
            {
             bool pes =  OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
             if(OrderMagicNumber()==1111)

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (1 February 2011)

    You need one Magic Number for each symbol/timeframe/strategy.

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020.06.08)

    3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
                "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

      Broker History
      FXCM
      Commission - <TICKET>
      Rollover - <TICKET>

      >R/O - 1,000 EUR/USD @0.52

      #<ticket>  N/A
      OANDA
      Balance update
      Financing (Swap: One entry for all open orders.)

 
William Roeder #:
  1. Globally declared variables are all static. Remove the redundant static, as it is confusing.
  2. n, x, lo, and fase1e1, all have random values.
  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (1 February 2011)

    You need one Magic Number for each symbol/timeframe/strategy.

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020.06.08)

    3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
                "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

      Broker History
      FXCM
      Commission - <TICKET>
      Rollover - <TICKET>

      >R/O - 1,000 EUR/USD @0.52

      #<ticket>  N/A
      OANDA
      Balance update
      Financing (Swap: One entry for all open orders.)

Yea, but I didn't get why, it opened many trades in same time? I mentioned if (order's totall<0)
and then I mentioned if first =0