Someone help me Why happen multiple open orders in each bar of my EA

 

I coded MA crossover EA .After one order successful opened and get take profit,next open order happen multiple times(around 100 orders)

in each bar.What wrong with that.Someone help me.

void OnTick()
  {
//---
       double SlowMovingAverage = iMA(NULL ,0 ,100,0,MODE_EMA,PRICE_CLOSE,0);
      
       double LastSlowMovingAverage = iMA(NULL ,0 ,100,0,MODE_EMA,PRICE_CLOSE,1);
 
       double FastMovingAverage = iMA(NULL ,0 ,10,0,MODE_EMA,PRICE_CLOSE,0);
      
       double LastFastMovingAverage = iMA(NULL ,0 ,10,0,MODE_EMA,PRICE_CLOSE,1);
      
     if (OrdersTotal() ==0 )
      
     if( (LastFastMovingAverage<LastSlowMovingAverage)&& (FastMovingAverage>=SlowMovingAverage) )
       { 
         OrderSend(_Symbol ,OP_BUY , 1 , Ask , 1 ,Ask-200*_Point ,Ask+800*Point,NULL,0,0,Green);
  
       }
   
      
     if( (LastFastMovingAverage>LastSlowMovingAverage)&& (FastMovingAverage<=SlowMovingAverage) ) 
       {
         OrderSend(_Symbol ,OP_SELL , 1 , Bid , 1 ,Bid+200*_Point ,Bid-800*Point,NULL,0,0,Red);
       }
      
  
       }
Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
void OnTick()
  {
//--- 
       double SlowMovingAverage = iMA(NULL ,0 ,100,0,MODE_EMA,PRICE_CLOSE,0);
       
       double LastSlowMovingAverage = iMA(NULL ,0 ,100,0,MODE_EMA,PRICE_CLOSE,1);
 
       double FastMovingAverage = iMA(NULL ,0 ,10,0,MODE_EMA,PRICE_CLOSE,0);
       
       double LastFastMovingAverage = iMA(NULL ,0 ,10,0,MODE_EMA,PRICE_CLOSE,1);
       
       if(OrdersTotal() ==0 )
         {
         if( (LastFastMovingAverage<LastSlowMovingAverage)&& (FastMovingAverage>=SlowMovingAverage) )
           {  
            OrderSend(_Symbol ,OP_BUY , 1 , Ask , 1 ,Ask-200*_Point ,Ask+800*Point,NULL,0,0,Green);
           }
         if( (LastFastMovingAverage>LastSlowMovingAverage)&& (FastMovingAverage<=SlowMovingAverage) )  
           { 
            OrderSend(_Symbol ,OP_SELL , 1 , Bid , 1 ,Bid+200*_Point ,Bid-800*Point,NULL,0,0,Red);
           }
         }
  }
 
forexsender: ,next open order happen multiple times(around 100 orders)
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. double SlowMovingAverage = iMA(NULL ,0 ,100,0,MODE_EMA,PRICE_CLOSE,0);
    double FastMovingAverage = iMA(NULL ,0 ,10,0,MODE_EMA,PRICE_CLOSE,0);
    You are using the current bar (zero.) The MA could cross and uncross multiple times as price fluctuates. Do you wish to enter when there is no real cross?
  4. OrderSend(_Symbol ,OP_BUY , 1 , Ask , 1 ,Ask-200*_Point ,Ask+800*Point,NULL,0,0,Green);
    OrderSend(_Symbol ,OP_SELL , 1 , Bid , 1 ,Bid+200*_Point ,Bid-800*Point,NULL,0,0,Red);
    You buy at the Ask and sell at the Bid. (Your stops have 20 PIP minus spread for buys and 20 PIP plus spread for sells.)
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 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.)

  5. Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. You should be using strict - don't just silence the compiler, it is trying to help you. Always use strict. Fixing the warnings will save you hours of debugging.
              Program Properties (#property) - Preprocessor - Language Basics - MQL4 Reference
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.
  6. Your if(OrdersTotal() == 0) only applied to the buys, you had no check on the sell, combined with № 3 and you get an open sell per tick. Compare your code to @Marco vd Heijde's:
           if(OrdersTotal() ==0 )
             {
             if( (LastFastMovingAverage< …
             if( (LastFastMovingAverage> …
             }

  7. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

 
thanks you sir very much
Reason: