Code opening and closing hundreds of orders

 

Hello

I have the code below.

I get a crossover from an indicator and open 1 order at a time.

I want to also close the order when the indicator reaches the overbought or oversold area, (>50)

but after it does so it opens and close Many orders.

Can anyone help?


void OnTick()

  {

   string signal="";

   double WPR = iWPR(_Symbol,_Period,20,0);

   double high = iCustom(_Symbol,_Period,"custom",0,0);

   double low = iCustom(_Symbol,_Period,"custom",1,0);

   double highpre = iCustom(_Symbol,_Period,"custom",0,1);

   double lowpre = iCustom(_Symbol,_Period,"custom",1,1);

   double ATR = iATR(_Symbol,_Period,14,0);

   

   if (high > low)

   if (lowpre > highpre)  

      {

         signal = "buy";

      }

   if (high < low)

   if (lowpre > highpre)

      {

         signal = "sell";

      }

      

    if (signal == "buy" && WPR > -42)

      {

         if(OrdersTotal() == 0)

            {OrderSend(_Symbol,OP_BUY,0.1,Ask,3,Ask-ATR*2.5,0,NULL,0,0,Green);}

         if(OrdersTotal() == 1 && checkifbuy() == FALSE)

         {

           CloseSellPositions();

           OrderSend(_Symbol,OP_BUY,0.1,Ask,3,Ask-ATR*2.5,0,NULL,0,0,Green);

         } 

      }

    if (signal == "sell" && WPR < -42)

      {

         if (OrdersTotal() == 0)

            {OrderSend(_Symbol,OP_SELL,0.1,Bid,3,Bid+ATR*2.5,0,NULL,0,0,Red);}

         if(OrdersTotal() == 1 && checkifbuy() == TRUE)

            {

              CloseBuyPositions();

              OrderSend(_Symbol,OP_SELL,0.1,Bid,3,Bid+ATR*2.5,0,NULL,0,0,Red);

            }

            

      }

    if(

      (OrdersTotal() == 1)

       (checkifbuy() == TRUE)

      && (high >50)

        )

        {CloseBuyPositions();}

    if(

      (OrdersTotal() == 1)

       (checkifbuy() == False)

      && (low >50)

        )

        {CloseSellPositions();}

  }



void CloseSellPositions()

   {

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

         {

            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

            string CurrencyPair = OrderSymbol();

            if (_Symbol == CurrencyPair)

            if (OrderType() == OP_SELL)

               {

                  OrderClose(OrderTicket(),OrderLots(),Ask,3,NULL);

               }

          }

    }



void CloseBuyPositions()

   {

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

         {

            OrderSelect(j,SELECT_BY_POS,MODE_TRADES);

            string CurrencyPair1 = OrderSymbol();

            if (_Symbol == CurrencyPair1)

            if (OrderType() == OP_BUY)

               {

                  OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);

               }

          }

    } 



bool checkifbuy()

   {

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

         {

            OrderSelect(k,SELECT_BY_POS,MODE_TRADES);

            string CurrencyPair2 = OrderSymbol();

            if (_Symbol == CurrencyPair2)

            if (OrderType() == OP_BUY)

               {

                  return(TRUE);

            

               }

            else return(FALSE);

          }

    } 
 

Look at the example of th MACD (2 ema crossing) on your pc: ‪...\MQL5\Experts\Examples\MACD\MACD Sample.mq5

Start from there and add another indicator is faster and you don't step into many possible errors.

And when you post code please you the code formation either by the icon </> or Alt+S.

 
  1. kasako:but after it does so it opens and close Many orders.

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)


  2.            OrderSend(_Symbol,OP_BUY,0.1,Ask,3,Ask-ATR*2.5,0,NULL,0,0,Green);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 

Thanks a lot for your comments William. 

My mistake, I debugged the code and found the mistake. Its just sometimes someone gets stuck on some simple stupid errors.

Didn't take into attention the spread, thanks a lot.