Bamboozled by this loop!

 

I am a beginner and busy writing an EA. Some of my OrderSend code works very well, but the following code which is similar to those who work well, goes into a loop creating one SellStop order after another in a never ending sequence. I put in a test of "if FirstSellTicket ==0;, but his seems to be totally ignored.

Here is the code - Please help me!

for(i = 0; i <= OrdersTotal()-1; i++) 
                 
        if (OrderSelect(i,SELECT_BY_POS)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_BUY)
        
            {
               double bOpenPrice = OrderOpenPrice();
            }
             
        if (IsTradeContextBusy()) Sleep(10);
               
        if (FirstSellTicket == 0 && bOpenPrice == BLevel)
               
        FirstSellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,CLevel,UseSlippage,BLevel,DLevel,"Sell Stop Order",MagicNumber,0,Red);
       
                  // Error handling
                     if(FirstSellTicket == -1)
                        {
                           ErrorCode = GetLastError();
                           ErrDesc = ErrorDescription(ErrorCode);

                           ErrAlert = StringConcatenate("Open Sell Stop Order - Error ",ErrorCode,": ",ErrDesc);
                           Alert(ErrAlert);

                           ErrLog = StringConcatenate("Lots: ",LotSize," Price: ",CLevel," Stop: ",BLevel," Profit: ",DLevel);
                           Print(ErrLog);
                        } 
                  
         if (FirstSellTicket == 0 && bOpenPrice == CLevel)
                  
         SecondSellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,DLevel,UseSlippage,CLevel,ELevel,"Sell Stop Order",MagicNumber,0,Red);
              
               
               // Error handling
                     if(SecondSellTicket == -1)
                        {
                           ErrorCode = GetLastError();
                           ErrDesc = ErrorDescription(ErrorCode);

                           ErrAlert = StringConcatenate("Open Sell Stop Order - Error ",ErrorCode,": ",ErrDesc);
                           Alert(ErrAlert);

                           ErrLog = StringConcatenate("Lots: ",LotSize," Price: ",DLevel," Stop: ",CLevel," Profit: ",ELevel);
                           Print(ErrLog);
                        }          
 
A question: I can't see from the code you have posted . . . . how do you set FirstSellTicket back to zero so another order can be placed ?
 
RaptorUK:
A question: I can't see from the code you have posted . . . . how do you set FirstSellTicket back to zero so another order can be placed ?


I have written some code for setting FirstSellTicket and other paranmeters back to 0 but it is still commented out. I did not want to complicate things until the code worked.

Here is the planned code:

      /* aOrders = 0;        
       
       for(i = 0; i <= OrdersTotal()-1; i++)   
        
       if (OrderSelect(i,SELECT_BY_POS)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_SELL
              || OrderType() == OP_SELLSTOP
              || OrderType() == OP_BUY
              || OrderType() == OP_BUYSTOP)          
       
         
         aOrders++;
      
      if (aOrders == 0)
      
      {
         Counter = 0;
         FirstBuyTicket = 0;
         SecondBuyTicket = 0;
         FirstSellTicket = 0;
         SecondSellTicket = 0;
         
      }*/
 

ernest02:


I have written some code for setting FirstSellTicket and other paranmeters back to 0 but it is still commented out. I did not want to complicate things until the code worked.

Here is the planned code:

OK, my guess is that you have declared FirstSellTicket as an int and set it at 0 within start() and not made it static . . for each new tick FirstSellTicket is set back to 0 and you get a new trade . . . thus

start()
  {
  .
  .
  .
  .

  int FirstSellTicket = 0;
 
RaptorUK:

OK, my guess is that you have declared FirstSellTicket as an int and set it at 0 within start() and not made it static . . for each new tick FirstSellTicket is set back to 0 and you get a new trade . . . thus


I think you are on the right track! I will try this immediately.

Thanks a lot!!

 
ernest02:


I think you are on the right track! I will try this immediately.

Thanks a lot!!


Alas! I forgot that I declared these variables as global variables outside start().

//---global variables
int            i;
int            Counter = 0;
int            BuyTicket = 0;
int            SellTicket = 0;
int            FirstBuyTicket = 0;
int            SecondBuyTicket = 0;
int            FirstSellTicket = 0;
int            SecondSellTicket = 0;

Here

 
ernest02:


Alas! I forgot that I declared these variables as global variables outside start().

Here

OK, more info . . . OK, what about this, is it a typo ?

if (FirstSellTicket == 0 && bOpenPrice == CLevel)
                  
         SecondSellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,DLevel,UseSlippage,CLevel,ELevel,"Sell Stop Order",MagicNumber,0,Red);


//  should it be this ?

if (SecondSellTicket == 0 && bOpenPrice == CLevel)
                  
         SecondSellTicket = OrderSend(Symbol(),OP_SELLSTOP,LotSize,DLevel,UseSlippage,CLevel,ELevel,"Sell Stop Order",MagicNumber,0,Red);
 
RaptorUK:

OK, more info . . . OK, what about this, is it a typo ?


Yes it is a typo! I copied the code over and forgot to change the variable. BUT this does not rectify the problem!It must be something else.
 
ernest02:

Yes it is a typo! I copied the code over and forgot to change the variable. BUT this does not rectify the problem!It must be something else.


I have fixed the typo but still get the following results:

46 2011.05.13 15:11 sell stop 23 7.47 1.42771 1.42931 1.42621 0.00 9340.93
47 2011.05.13 15:11 delete 23 7.47 1.42771 1.42931 1.42621 0.00 9340.93
48 2011.05.13 15:11 sell stop 24 7.47 1.42771 1.42931 1.42621 0.00 9340.93
49 2011.05.13 15:11 delete 24 7.47 1.42771 1.42931 1.42621 0.00 9340.93
50 2011.05.13 15:12 sell stop 25 7.47 1.42771 1.42931 1.42621 0.00 9340.93
51 2011.05.13 15:12 delete 25 7.47 1.42771 1.42931 1.42621 0.00 9340.93
52 2011.05.13 15:12 sell stop 26 7.47 1.42771 1.42931 1.42621 0.00 9340.93

and so until I stop the system. Something that puzzles me is why are the sell stops deleted? I have no code deleting any transactions.

 
ernest02:

Yes it is a typo! I copied the code over and forgot to change the variable. BUT this does not rectify the problem!It must be something else.

Obviously all I can see is the code you have posted, if there error isn't there then I can't find it . . .

if (FirstSellTicket == 0 && bOpenPrice == BLevel)
               
if (SecondSellTicket == 0 && bOpenPrice == CLevel)

. . means that you can only get a OrderSend if FirstSellTicket or SecondSellTicket is zero, as soon as the orders are placed these variables are no longer zero so you can only get one of each placed . . . . unless you are resetting these variables to zero somewhere else that you aren't showing . . .

 
ernest02:


I have fixed the typo but still get the following results:

46 2011.05.13 15:11 sell stop 23 7.47 1.42771 1.42931 1.42621 0.00 9340.93
47 2011.05.13 15:11 delete 23 7.47 1.42771 1.42931 1.42621 0.00 9340.93
48 2011.05.13 15:11 sell stop 24 7.47 1.42771 1.42931 1.42621 0.00 9340.93
49 2011.05.13 15:11 delete 24 7.47 1.42771 1.42931 1.42621 0.00 9340.93
50 2011.05.13 15:12 sell stop 25 7.47 1.42771 1.42931 1.42621 0.00 9340.93
51 2011.05.13 15:12 delete 25 7.47 1.42771 1.42931 1.42621 0.00 9340.93
52 2011.05.13 15:12 sell stop 26 7.47 1.42771 1.42931 1.42621 0.00 9340.93

and so until I stop the system. Something that puzzles me is why are the sell stops deleted? I have no code deleting any transactions.


OK! I have found the reason why the Sell Stop Orders are deleted!@

2011.08.16 15:44:34 2011.05.13 15:12 MartingaleEA EURUSD,M1: BaseLotSize is1.8682
2011.08.16 15:44:34 2011.05.13 15:12 Tester: pending order is deleted [no enough money]
2011.08.16 15:44:34 2011.05.13 15:12 Tester: PrevBalance: 9340.93, PrevPL: 0.00, PrevEquity 9340.93, PrevMargin: 0.00, NewMargin: 10665, FreeMargin: -1324.14
2011.08.16 15:44:34 2011.05.13 15:12 Tester: not enough money for sell 7.47 EURUSD at 1.42771 sl: 1.42931 tp: 1.42621 [2011.05.13 15:12]
2011.08.16 15:44:34 2011.05.13 15:12 MartingaleEA EURUSD,M1: open #31 sell stop 7.47 EURUSD at 1.42771 sl: 1.42931 tp: 1.42621 ok

Reason: