Download MetaTrader 5

Why does "nearPosition" become "false" when it should be "true"?

To add comments, please log in or register
Do you have questions on how to turn your idea into reality? Ask them on the Forum!
moonsboy
889
moonsboy 2014.11.12 08:00 
Dears, When testing my EA, many orders sometimes were repeatedly opened at a same position. Finally I found the issue came from this sentence, "if(int(Time[0]-OrderOpenTime())>CandleInterval*PeriodSeconds(0)) nearPosition=false;". Below is the program with more details, which to prevent order opened repeatedly with previous existing order. "CandleInterval" here defined as '2'(int type). I'm confused what wrong with that sentence. How can "nearPosition" become "false" when it should be "true"? No other sentence to assign value for "nearPosition". Looking foward to your valuable advice on this. for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue; if(OrderSymbol()!=Symbol()) continue; nearPosition=true; if(int(Time[0]-OrderOpenTime())>CandleInterval*PeriodSeconds(0)) nearPosition=false; }
Keith Watford
Moderator
9601
Keith Watford 2014.11.12 09:42  

Please insert sections of code using the SRC button.

I'm afraid that your post in this format is confusing and you will find that many people will not be bothered to try to decipher it. 

moonsboy
889
moonsboy 2014.11.13 01:29  
Dear GumRai, how to use SRC button? I saw it's under "attach file", but it doesn't work on my computer. It doesn't work to quote by "reply" on my computer either. What's wrong?

for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
if(OrderSymbol()!=Symbol()) continue; nearPosition=true;
if(int(Time[0]-OrderOpenTime())>CandleInterval*PeriodSeconds(0)) nearPosition=false;
}
Keith Watford
Moderator
9601
Keith Watford 2014.11.13 01:54  

The SRC button is at the top of the reply panel

Click it , paste your code into the box and click insert

for(int i=OrdersTotal()-1; i>=0; i--) 
{ 
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue; 
if(OrderSymbol()!=Symbol()) continue; nearPosition=true; 
if(int(Time[0]-OrderOpenTime())>CandleInterval*PeriodSeconds(0)) nearPosition=false; 
} 

 I would indent the code personally, makes it easier to follow

  
for(int i=OrdersTotal()-1; i>=0; i--) 
{ 
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
   continue; 
if(OrderSymbol()!=Symbol())
   continue; 
nearPosition=true; 
if(int(Time[0]-OrderOpenTime())>CandleInterval*PeriodSeconds(0))
   nearPosition=false; 
} 

 I don't know what you expect from this, but the value of nearPosition is set to true at every pass of the loop where the symbol is the chart symbol. It will only be set to false  by the check on the trade at position 0 or the lowest index where the symbol is the chart symbol.

moonsboy
889
moonsboy 2014.11.13 02:09  
is '0' the earliest order or 'OrdersTotal()-1' the earliest?
Keith Watford
Moderator
9601
Keith Watford 2014.11.13 05:44  
jollydragon:
is '0' the earliest order or 'OrdersTotal()-1' the earliest?

I don't think that it is reliable to assume that the orders are definitely in chronological order.

If you want to find the earliest, check the order open time to find it in the loop

moonsboy
889
moonsboy 2014.11.13 06:08  
dear GumRai,
if it's in the chart symbol, can any special case happen as you concern?
Could you please share some examples?
Keith Watford
Moderator
9601
Keith Watford 2014.11.13 12:13  
jollydragon:
dear GumRai,
if it's in the chart symbol, can any special case happen as you concern?
Could you please share some examples?

Sorry, I don't understand the question.

Here is an example (not compiled, not tested)

Keith Watford
Moderator
9601
Keith Watford 2014.11.13 12:15  

This is happening too often now. I put code in SRC but it is not appearing in the post. Sometimes works, sometimes not 

      datetime earliest_order_time=TimeCurrent();
      int earliest_ticket=-1;
      for(int x=OrdersTotal()-1;x>=0;x--)
        {
         if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
            if(OrderSymbol()==Symbol())
              {
               if(OrderOpenTime()<earliest_order_time)
                 {
                  earliest_order_time=OrderOpenTime();
                  earliest_ticket=OrderTicket();
                 }
              }
        }
      if(earliest_ticket!=-1)
        {
        //Do whatever you want to do
        }
moonsboy
889
moonsboy 2014.11.14 02:22  
Sorry, it's no what I wanted to ask.
If you use OrderSelect to select 'OrdersTotal()-1' , it will be the earliest order open or pending.
if select '0', it will be the latest order. Correct?
However, you said "I don't think that it is reliable to assume that the orders are definitely in chronological order."
Then I asked why and if you have any examples for "not reiable".
Keith Watford
Moderator
9601
Keith Watford 2014.11.14 03:11  

Ï don't know in what order the orders are stored.

I have read in the forum that it is best not to assume that they are stored in any set order. That is why I will check as in the example code that I posted.

Note that once pending orders are triggered, their OrderOpenTime changes to the trigger time. 

12
To add comments, please log in or register