Multiple trade entries on single buy or sell entry

 

Hi,

I wrote a mql5 ea code to buy to open on stochastic oversold and sell to open on stochastic overbought, at %K and %D crossover.  However when it hits the necessary requirements for entry the buy and sell are executed but multiple buys are triggered, or multiple sell are triggered and multiple tickets at the same time.  I intend to have only one entry, that is, one ticket at any one time. That is, to close an open position before an entry at the overbought and oversold areas.

My sample code for overbought is like this, for sell-to-open: [it seems by this code, multiple open ticket iteration happens. I intend to have only one ticket entry at a time]. Please assist. 

// SELL SIGNAL

  // if both values are above 80

   if (KValue2>80&&DValue2>80)

   // if the K value has crossed the D value from above

  if ((KValue1<DValue1)&&(KValue2>DValue2))

        {

       trade.PositionClose(_Symbol);  // close the existing position        

       trade.Sell(0.10,NULL,Bid,0,NULL); // and sell-to-open new position

       signal="sell";

 }
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
When a graphical object is created using the ObjectCreate() function, it's necessary to specify the type of object being created, which can be one of the values of the ENUM_OBJECT enumeration. Further specifications of object properties are possible using functions for working with graphical objects.
 
 if ((KValue1<DValue1)&&(KValue2>DValue2))
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1
  3. Or, only process new candles.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  4. Or check for open order(s) before opening more.
 

So you can allow it to only open an order when

if(OrdersTotal() == 0)
   {
   // Do something...
   }
Reason: