How to avoid opening additional positions.

 

Hello there sirs, 


I've run into a strange situation where my EA placed multiple orders even though I'm only alloting to perform CTrade PositionOpen if there is no open position.


This is a very simplistic approach from what I'm using:

void OnTick()
{
  if(PositionSelect(_Symbol) == false) {
    double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    CTrade trade;
    trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, 1, ask, ask - 100, ask + 80, "going long");
  }

}

Is there any particular case where PositionSelect would return false after a PositionOpen is sent?

I'm willing to believe that from the PositionOpen until there is a position, there are many thing between these steps that, on a very figh frequency market, another onTick would be processed before a the position is in place.


Check what happened to me live, the EA was trying to by only 1 volume/amount and ended buying 3. :(


I've thought about many ways to avoid this, but as awesome as MT5 is, I bet I'm missing something.

Thanks for any possible help.

 

After you have formed a trade order - you need to switch to the "tracking" mode. That is, you must strictly control the execution of a trade order. Until a trade order is executed, you cannot trade.

An example of a template that separates the execution of a trade order: 

We catch the transaction

How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Vladimir Karputov:

After you have formed a trade order - you need to switch to the "tracking" mode. That is, you must strictly control the execution of a trade order. Until a trade order is executed, you cannot trade.

An example of a template that separates the execution of a trade order: 

We catch the transaction


Thanks a lot for you input Valdmir.


I found out a similar issue from long time ago in this forum, huge thread and it seems no definitive solution was found.


I'm going to study your approach, I'm afraid of these super complex coding to handle something that, IMO, the framework should do by ourselves somehow.

 
How do I open a different batch of one lot after I open a deal?
 
rsikora #:


Thanks a lot for you input Valdmir.


I found out a similar issue from long time ago in this forum, huge thread and it seems no definitive solution was found.


I'm going to study your approach, I'm afraid of these super complex coding to handle something that, IMO, the framework should do by ourselves somehow.

easiest solution to avoid this type of problem is : Executing your code once per candle closure.

 
I need help writing a code that opens multiple positions together when we open one of them
 
  1. Emado Btc #: I need help writing a code

    Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)


  2. Emado Btc #: How do I open a different batch of one lot after I open a deal?

    Don't hijack other threads for your off-topic post. Next time, make your own, new, thread.

 
rsikora:

Hello there sirs, 


I've run into a strange situation where my EA placed multiple orders even though I'm only alloting to perform CTrade PositionOpen if there is no open position.


This is a very simplistic approach from what I'm using:

Is there any particular case where PositionSelect would return false after a PositionOpen is sent?

I'm willing to believe that from the PositionOpen until there is a position, there are many thing between these steps that, on a very figh frequency market, another onTick would be processed before a the position is in place.


Check what happened to me live, the EA was trying to by only 1 volume/amount and ended buying 3. :(


I've thought about many ways to avoid this, but as awesome as MT5 is, I bet I'm missing something.

Thanks for any possible help.


Try this solution:

void OnTick()  {
MqlRates cnt[]; if(CopyRates(_Symbol,_Period,0,2,cnt)!=2) return;
if(cnt[1].tick_volume>1) return;

//--- your code here ---

   
}


Have a nice coding :-)

 
Nino Guevara Ruwano #: Try this solution:

if(cnt[1].tick_volume>1) return;

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.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

 
William Roeder #:

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.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)


Ok, thank you for the instructions you gave, I have seen the MT4 link you gave.

Does that mean your advice is to use time?

Because there is no Time[0] in MT5, then I will use TimeCurrent (), with the following code:

void OnTick() {
static datetime Time0=0;
if(TimeCurrent()>=Time0) {//start if

//--- your code here ---

Time0=TimeCurrent();


 }//end if
}   


Is this OK?

 
Nino Guevara Ruwano #:


Ok, thank you for the instructions you gave, I have seen the MT4 link you gave.

Does that mean your advice is to use time?

Because there is no Time[0] in MT5, then I will use TimeCurrent (), with the following code:


Is this OK?

No, TimeCurrent() updates on each new tick, not bar.

You need to use iTime() or something equivalent (CopyTime...).

iTime(NULL,0,0)