OrderSelect needs a 2nd call.. ?!

 

Greetings,

Attached is a part of my script that is suppose to scan throw the open trades and 'snap' when condition is true.
For some odd reason, the condition sets to true even when the terms are not true.

Basically, I know how to go around it by putting a second OrderSelect_BY_TICKET for every switched entry, but I can't figure out why it won't work without.


Does anyone here knows why?


Thank you,


James


int last_active;  //saves the last active trade tickt
double Pips;

int start()
  {
int needleTicket, found, trades, type;
bool deleted=false;
double low2  = iLow(Symbol(),PERIOD_M15,iLowest(Symbol(),PERIOD_M15,MODE_LOW,2,0));
double high2 = iHigh(Symbol(),PERIOD_M15,iHighest(Symbol(),PERIOD_M15,MODE_HIGH,2,0));
Pips  = MarketInfo(Symbol(),MODE_POINT)*10;   // converts Point to Pips (4 digits instead of 5)

  for( int i = (OrdersTotal()-1); i >= 0; i --)
     {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()!=Symbol()) continue;
     needleTicket = OrderTicket();
     if (OrderType()>1) continue; // it is probably a pending order
        else trades++;  // counts the active trades

     deleted=history(last_active); //is the last active order is now history?
//     if (deleted==true) OrderDelete(181071701,White);  // delete another particular order

     switch (needleTicket)
            {
            case 3610080 : found++;
                           OrderSelect(needleTicket, SELECT_BY_TICKET, MODE_TRADES);
                           if (Bid>1.33020||(low2-(1*Pips))>OrderOpenPrice()) do_something(needleTicket); // Buy Trade
                           last_active=OrderTicket();
                           break;

            case 3610081 : found++;
                           OrderSelect(needleTicket, SELECT_BY_TICKET, MODE_TRADES); // Buy Trade
                           if (Bid>1.33020||low2-(1*Pips)>OrderOpenPrice()) do_something(needleTicket);
                           last_active=OrderTicket();
                           break;

            case 3621324 : found++;                                                   // still pending..
                           OrderSelect(needleTicket, SELECT_BY_TICKET, MODE_TRADES);
                           if (Bid<1.32377||OrderOpenPrice()>high2+(1*Pips)) do_something(needleTicket);  // Sell Trade
                           last_active=OrderTicket();
                           break;

            case 3600989 : found++;
                           OrderSelect(needleTicket, SELECT_BY_TICKET, MODE_TRADES);
                           if (Bid<1.31816||OrderOpenPrice()>high2+(1*Pips)) do_something(needleTicket);  // Sell Trade
                           last_active=OrderTicket();
                           break;

            default : if (found<trades) PlaySound ("baby_cry.wav"); Sleep (5000);
            }
     }   //  end of 'for( int i = ...'
//----
   return(0);
  }
 

You should be checking the return value of the OrderSelect function to make sure it worked.

There is no need to use the third parameter on the OrderSelect function when using SELECT_BY_TICKET, and it may mislead you to use it.

 
I'm curious to know what it is you are trying to do. You seem to have hard coded ticket numbers into the switch statement. Are you intending to manually type these in then recompile the program to achieve some effect? This is the sort of thing where you might use an extern int variable so that you can enter the ticket numbers at the start of the run rather than having to recompile the code.
 
JJF:

Greetings,

Wow ! I'm sure your code makes sense to you . . . I hope it does . . . but I am at a loss.

 
dabbler:
I'm curious to know what it is you are trying to do. You seem to have hard coded ticket numbers into the switch statement. Are you intending to manually type these in then recompile the program to achieve some effect? This is the sort of thing where you might use an extern int variable so that you can enter the ticket numbers at the start of the run rather than having to recompile the code.

Well, I wrote this routine to monitor a bunch of open trades when the market is wiggling without a focused direction (or when I want to go to sleep). In such case, I set some conditions to act upon them.

I am not familiar with the extern int command. But in any case, it is not such a hassle to recompile the script with the current trades numbers.

Now, let me explain 'the bug':
When I remove the OrderSelect_BY_TICKET from within the switch entries, the OrderOpenPrice() that the function returns is wrong and not match to the particular OrderTicket().

Can anyone tell me why OrderSelect(i, SELECT_BY_POS, MODE_TRADES); on the main 'For' loop is not enough for the switch choice?
 
JJF:

Can anyone tell me why OrderSelect(i, SELECT_BY_POS, MODE_TRADES); on the main 'For' loop is not enough for the switch choice?
I believe it is, debug your code properly. How do you know it isn't ? have you tried without and printed OrderTicket() to check which order is selected ?
 
JJF:

Well, I wrote this routine to monitor a bunch of open trades when the market is wiggling without a focused direction (or when I want to go to sleep). In such case, I set some conditions to act upon them.

I am not familiar with the extern int command. But in any case, it is not such a hassle to recompile the script with the current trades numbers.

Ok, that makes sense. Thank you.

extern int isn't a command. You just define an int outside of any functions (globally) and by using the extern keyword, when you run the EA you get to change the value if you want.

Reason: