Sometimes i get the INVALID PRICE error

 

I`ve been able to make my EA work, it should trade haramis, but it`s not trading all of them, sometimes i get the INVALID PRICE error


Could someone help me please?



The code places a buystop and a sellstop order at the high and low of the harami


void OnTick()
  {
  
    CopyRates(Symbol(), Period (), 0, 3, rates);
    
    double maxCurrent = rates[1].high;
    double minCurrent = rates[1].low;
    
    double maxPrev = rates[2].high;
    double minPrev = rates[2].low;
    
    double newHigh = rates[0].high;
    double newLow = rates[0].low;
    
    
    if(maxPrev > maxCurrent && minPrev < minCurrent) 
    
    
    {
    
      
    if(OrdersTotal()==0 && PositionsTotal()==0) 
    
    
    {
      
      
    if(maxCurrent < newHigh && minCurrent < newLow)
    {
   
           
         trade.BuyStop(5, maxCurrent + 0.00010, _Symbol, minCurrent + 0.00010, maxCurrent+ 0.00110, 0, 0, "");
  
}
else

     if(maxCurrent > newHigh && minCurrent > newLow)

           {  trade.SellStop(5, minCurrent - 0.00010, _Symbol, maxCurrent + 0.00010, minCurrent - 0.00110, 0, 0, "");
           
            }
            
           }
       }
         
   }
 
Leonardo Fernandez: sometimes i get the INVALID PRICE error … The code places a buystop and a sellstop order at the high and low of the harami
  1. You enter the trade, get stopped out, then try to open the same trade again. What do you expect?
  2. Try opening only at the start of a new bar.
 
William Roeder:
  1. You enter the trade, get stopped out, then try to open the same trade again. What do you expect?
  2. Try opening only at the start of a new bar.

Thank you for replying,


Could you please point me a direction of how i could do that? I am fairly new to mql5

 
Use this and you will find "new bar."
 
William Roeder:
Use this and you will find "new bar."

    I have at least 25 tabs open trying to find an answer, i am struggling to do that, i though that maybe someone could point a direction, maybe point out i got a line of code wrong or if it was a big code than just tell me what i should study, to find the answer of a question first it need to be already asked and none of the answers i found were able to satisfy what i needed to know.

    I came here because i wanted to know what i need to study to solve my problem, no need to be rude.


   I cant understand why the EA works fine for some candles and sometimes it just dont for others even though they follow the same rules.

 
Leonardo Fernandez:

    I have at least 25 tabs open trying to find an answer, i am struggling to do that, i though that maybe someone could point a direction, maybe point out i got a line of code wrong or if it was a big code than just tell me what i should study, to find the answer of a question first it need to be already asked and none of the answers i found were able to satisfy what i needed to know.

    I came here because i wanted to know what i need to study to solve my problem, no need to be rude.


   I cant understand why the EA works fine for some candles and sometimes it just dont for others even though they follow the same rules.

Because you don't your check prices against the market. There are rules to follow about open/stoploss/takeprofit.

See here (mql4 link but it works the same (usually) with mql5).

Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  • book.mql4.com
Tables below show calculation values that limit the conduction of trades when opening, closing, placing, deleting or modifying orders. To get the minimum distance to StopLevel and freezing distance FreezeLevel the MarketInfo() function should be called. Requirements. Correct prices used when performing trade operations. Order Type Open Price...
 
Leonardo Fernandez:

I`ve been able to make my EA work, it should trade haramis, but it`s not trading all of them, sometimes i get the INVALID PRICE error


Could someone help me please?



The code places a buystop and a sellstop order at the high and low of the harami


I notice that you do calculations with doubles before arriving at a price which you submit. Sometimes these calculations result in rounding errors. For example you get a value like 1.23000001. If you submit such a value you will get an error. In those cases you have to round the resulting value to the nearest valid price before submitting it.
Reason: