This is totally flabbergasting!

 

I have written an EA and get the most mystifying results from a piece of code that i have written. It works well with Buy orders but refuses to work with Sell orders. I have gone through the system code a hundred times. Even rewritten it - but the same un-explainable result keeps on happening. PLEASE HELP!

First let me give you the code:

              int Sell = 0;
              int SPending = 0;
              int Buy = 0;
              int BPending = 0;
              
             for (Count = OrdersTotal()-1; Count >= 0; Count--)
                {
                       if (OrderSelect(Count, SELECT_BY_POS)
                       && OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)

                       
                       {
                          if (OrderType()== OP_SELL)Sell = 1;
                          else if (OrderType()== OP_SELLSTOP)SPending = 1;
                          else if (OrderType()== OP_BUY)Buy = 1;
                          else if (OrderType()== OP_BUYSTOP)BPending = 1;
                       }
                    }
                    

      Print("Sell is ", Sell);
      Print("SPending is ", SPending);
      Print("Buy is ", Buy);
      Print("BPending is ", BPending);

And here is the results taken from the tester journal. See how it works with Buy Stop and Market orders but refuses to work with Sell Stop and Market orders:

First Buy orders:

17:38:40 2011.05.13 21:28 ForexRebel EURUSD,M1: open #2 buy stop 2.09 EURUSD at 1.41169 sl: 1.40869 tp: 1.41320 ok


17:38:40 2011.05.13 21:28 ForexRebel EURUSD,M1: Sell is 0

17:38:40 2011.05.13 21:28 ForexRebel EURUSD,M1: SPending is 0

17:38:40 2011.05.13 21:28 ForexRebel EURUSD,M1: Buy is 0

17:38:40 2011.05.13 21:28 ForexRebel EURUSD,M1: BPending is 1

17:38:40 2011.05.13 21:29 Tester: order #2, buy 2.09 EURUSD is opened at 1.41169


17:38:40 2011.05.13 21:29 ForexRebel EURUSD,M1: Sell is 0

17:38:40 2011.05.13 21:29 ForexRebel EURUSD,M1: SPending is 0

17:38:40 2011.05.13 21:29 ForexRebel EURUSD,M1: Buy is 1

17:38:40 2011.05.13 21:29 ForexRebel EURUSD,M1: BPending is 0

Now Sell orders:

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: open #4 sell stop 2.01 EURUSD at 1.42228 sl: 1.42528 tp: 1.42082 ok


17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: Sell is 0

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: SPending is 0 !!!!!!!!

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: Buy is 0

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: BPending is 0

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: open #5 sell stop 2.01 EURUSD at 1.42228 sl: 1.42528 tp: 1.42082 ok

17:38:49 2011.05.16 19:21 Tester: order #4, sell 2.01 EURUSD is opened at 1.42228

17:38:49 2011.05.16 19:21 Tester: order #5, sell 2.01 EURUSD is opened at 1.42228


17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: Sell is 0 !!!!!!!!!!!!!!!!!!

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: SPending is 0

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: Buy is 0

17:38:49 2011.05.16 19:21 ForexRebel EURUSD,M1: BPending is 0

What's going on?

 
Are you by any chance using different Magic Numbers for buys and sells ?
 
RaptorUK:
Are you by any chance using different Magic Numbers for buys and sells ?

No! Using the same Magic Number!
 
ernest02:

No! Using the same Magic Number!


The code does work. I stuck in into one of mine and forced a buy and sell

2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: close #1 buy 0.14 EURUSD at 1.41772 sl: 1.41722 tp: 1.42772 at price 1.41743
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #2 buy stop 0.14 EURUSD at 1.41802 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #3 buy stop 0.14 EURUSD at 1.41832 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #4 buy stop 0.14 EURUSD at 1.41862 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #5 buy stop 0.14 EURUSD at 1.41892 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: close #6 sell 0.14 EURUSD at 1.41743 sl: 1.41793 tp: 1.40743 at price 1.41772
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #7 sell stop 0.14 EURUSD at 1.41713 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #8 sell stop 0.14 EURUSD at 1.41683 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #9 sell stop 0.14 EURUSD at 1.41653 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #10 sell stop 0.14 EURUSD at 1.41623 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: BPending is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Buy is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: SPending is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Sell is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #10 sell stop 0.14 EURUSD at 1.41623 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #10 sell stop 0.14 EURUSD at 1.41623 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #9 sell stop 0.14 EURUSD at 1.41653 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #9 sell stop 0.14 EURUSD at 1.41653 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #8 sell stop 0.14 EURUSD at 1.41683 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #8 sell stop 0.14 EURUSD at 1.41683 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #7 sell stop 0.14 EURUSD at 1.41713 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #7 sell stop 0.14 EURUSD at 1.41713 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #6 sell 0.14 EURUSD at 1.41743 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #6 sell 0.14 EURUSD at 1.41743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #5 buy stop 0.14 EURUSD at 1.41892 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #5 buy stop 0.14 EURUSD at 1.41892 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #4 buy stop 0.14 EURUSD at 1.41862 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #4 buy stop 0.14 EURUSD at 1.41862 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #3 buy stop 0.14 EURUSD at 1.41832 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #3 buy stop 0.14 EURUSD at 1.41832 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #2 buy stop 0.14 EURUSD at 1.41802 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #2 buy stop 0.14 EURUSD at 1.41802 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #1 buy 0.14 EURUSD at 1.41772 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #1 buy 0.14 EURUSD at 1.41772 ok

I did change one thing because I did not see where initialized Count. I don't think this is the reason.

for (int Count = OrdersTotal()-1; Count >= 0; Count--)

Do you have long only enabled in your Testing Tab in expert properties? I can recreate your output if I select long only option and run the same test as above

2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: BPending is 1
2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Buy is 1
2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: SPending is 0
2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Sell is 0

 
danjp:


The code does work. I stuck in into one of mine and forced a buy and sell

2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: close #1 buy 0.14 EURUSD at 1.41772 sl: 1.41722 tp: 1.42772 at price 1.41743
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #2 buy stop 0.14 EURUSD at 1.41802 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #3 buy stop 0.14 EURUSD at 1.41832 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #4 buy stop 0.14 EURUSD at 1.41862 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #5 buy stop 0.14 EURUSD at 1.41892 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: close #6 sell 0.14 EURUSD at 1.41743 sl: 1.41793 tp: 1.40743 at price 1.41772
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #7 sell stop 0.14 EURUSD at 1.41713 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #8 sell stop 0.14 EURUSD at 1.41683 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #9 sell stop 0.14 EURUSD at 1.41653 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: delete #10 sell stop 0.14 EURUSD at 1.41623 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: BPending is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Buy is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: SPending is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Sell is 1
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #10 sell stop 0.14 EURUSD at 1.41623 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #10 sell stop 0.14 EURUSD at 1.41623 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #9 sell stop 0.14 EURUSD at 1.41653 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #9 sell stop 0.14 EURUSD at 1.41653 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #8 sell stop 0.14 EURUSD at 1.41683 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #8 sell stop 0.14 EURUSD at 1.41683 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #7 sell stop 0.14 EURUSD at 1.41713 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #7 sell stop 0.14 EURUSD at 1.41713 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #6 sell 0.14 EURUSD at 1.41743 sl: 1.41793 tp: 1.40743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #6 sell 0.14 EURUSD at 1.41743 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #5 buy stop 0.14 EURUSD at 1.41892 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #5 buy stop 0.14 EURUSD at 1.41892 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #4 buy stop 0.14 EURUSD at 1.41862 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #4 buy stop 0.14 EURUSD at 1.41862 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #3 buy stop 0.14 EURUSD at 1.41832 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #3 buy stop 0.14 EURUSD at 1.41832 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #2 buy stop 0.14 EURUSD at 1.41802 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #2 buy stop 0.14 EURUSD at 1.41802 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Calculated lot size (Risk = 3%) = 0.14
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: modify #1 buy 0.14 EURUSD at 1.41772 sl: 1.41722 tp: 1.42772 ok
2011.10.21 13:39:15 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: open #1 buy 0.14 EURUSD at 1.41772 ok

I did change one thing because I did not see where initialized Count. I don't think this is the reason.

Do you have long only enabled in your Testing Tab in expert properties? I can recreate your output if I select long only option and run the same test as above

2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: BPending is 1
2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Buy is 1
2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: SPending is 0
2011.10.21 13:45:24 2011.08.09 00:00 Invictus_v2.1 EURUSD,M5: Sell is 0


No! I have both Long & Short activated in Expert Properties settings. That's why it is so mystifying!!!

Should I retype my code? Could there be some invisible character somewhere causing the problem?

I am grabbing at straws at the moment!

 
ernest02:


No! I have both Long & Short activated in Expert Properties settings. That's why it is so mystifying!!!

Should I retype my code? Could there be some invisible character somewhere causing the problem?

I am grabbing at straws at the moment!


Try this, 2 reasons OrderSelect can fail, Also check to see if you are really returning an order when you think you are?

              int Sell = 0;
              int SPending = 0;
              int Buy = 0;
              int BPending = 0;
              int err = GetLastError();
                  err=0;
              
             for (Count = OrdersTotal()-1; Count >= 0; Count--)
                {
                       if (OrderSelect(Count, SELECT_BY_POS)
                       && OrderMagicNumber() == MagicNumber
                       && OrderSymbol() == Symbol()
                       && OrderCloseTime() == 0)

                       
                       {
                          if (OrderType()== OP_SELL)Sell = 1;
                          else if (OrderType()== OP_SELLSTOP)SPending = 1;
                          else if (OrderType()== OP_BUY)Buy = 1;
                          else if (OrderType()== OP_BUYSTOP)BPending = 1;
                       }
                       else
                       { 
                          Print("I am not selecting an order");
                          err=GetLastError();
                          if(err)
                              Print("OrderSelect() has failed" + err);
                       }

                    }
                    

      Print("Sell is ", Sell);
      Print("SPending is ", SPending);
      Print("Buy is ", Buy);
      Print("BPending is ", BPending);
 
ernest02:

No! I have both Long & Short activated in Expert Properties settings. That's why it is so mystifying!!!

Should I retype my code? Could there be some invisible character somewhere causing the problem?

  1. for (Count = OrdersTotal()-1; Count >= 0; Count--)
    {
           if (OrderSelect(Count, SELECT_BY_POS)
           && OrderMagicNumber() == MagicNumber
           && OrderSymbol() == Symbol()
           && OrderCloseTime() == 0)
    
           
           {
              if (OrderType()== OP_SELL)Sell = 1;
              else if (OrderType()== OP_SELLSTOP)SPending = 1;
              else if (OrderType()== OP_BUY)Buy = 1;
              else if (OrderType()== OP_BUYSTOP)BPending = 1;
           } else ...
        }
    A) There's no need to double indent on this. B) On Current orders, OrderCloseTime() will ALWAYS be zero or it would be in the history list.
    for (Count = OrdersTotal()-1; Count >= 0; Count--) if(
       OrderSelect(Count, SELECT_BY_POS)
    && OrderMagicNumber() == MagicNumber
    && OrderSymbol() == Symbol()
    ){
                   if (OrderType()== OP_SELL)     Sell++;
              else if (OrderType()== OP_SELLSTOP) SPending++;
              else if (OrderType()== OP_BUY)      Buy++;
              else if (OrderType()== OP_BUYSTOP)  BPending++1;
    }
  2. There can not be a invisible character - or it wouldn't compile.
 

Guys - thanks a lot for trying to help me.

WHRoeder - the change in code you suggested made no difference - but thanks in any case for your trouble.

Danjp - I added the code you suggested and although there were Sell orders opened I got the message:"I am not selecting an order".

Furthermore, there was NO error reported.

The mystery is deepening!

 
ernest02:

Guys - thanks a lot for trying to help me.

WHRoeder - the change in code you suggested made no difference - but thanks in any case for your trouble.

Danjp - I added the code you suggested and although there were Sell orders opened I got the message:"I am not selecting an order".

Furthermore, there was NO error reported.

The mystery is deepening!


Take each parameter off the OrderSelect one at a time, remove OrderSymbol() == Symbol(), run it, then OrderMagicNumber() == MagicNumber and run again, see if you get any records back on each of those runs. Put a Print("Count = " + Count); right before the Select inside the for loop. You never know maybe Count is wrong. Post if you figure this out. I am curious now. Are you calling this function one time and you have both BUY and SELL orders open\pending, or are you calling it after the BUY and again after the SELL?

 

Guys! I am red in the face! I made a stupid mistake.

Danjp your last advice helped me find the problem. RaptorUK - you were on the right track right from the start.

Here are my two OrderSend() commands:

SellTicket = OrderSend(Symbol(),OP_SELLSTOP,dLotSize,dSellPrice,UseSlippage,SellStopLoss,SellProfitTake,"ForexRebel",Red);

BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,dLotSize,dBuyPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"ForexRebel",MagicNumber,0,Green);

Because the last part of the command cannot be seen on the screen if you do not deliberately scroll to the right to check the last two operators, I never picked it up.

Cannot understand why my Sell command was accepted without making provision (even just with commas) for two of the parameters.

What is most amazing and distressing is the correct code has reduced my profitability of the EA tremendously! I am going back to the error code! :-)

Thank you for your efforts. Hope you are not too cross with me!

 
ernest02:

Cannot understand why my Sell command was accepted without making provision (even just with commas) for two of the parameters.

What is most amazing and distressing is the correct code has reduced my profitability of the EA tremendously! I am going back to the error code! :-)

Thank you for your efforts. Hope you are not too cross with me!

Cross ? nope, happy you found the answer . . and that my logic was sound ;-)

Your Sell order was accepted because the last four variables passed to OrderSend are optional, if you don't send them they are given their default values https://docs.mql4.com/trading/OrderSend so your MagicNumber was set to Red (I would have thought that this would have give a compile error), expiration set to 0 and arrow_color is set to CLR_NONE.

Reason: