What is wrong with this small pice of code?

 

I want to check if there are any open OP_BUYSTOP orders and when there are none to place a new buy stop order. But the code does not work. Can anyone please tell me what is wrong with this piece of code?

               for(i = OrdersTotal()-1; i >= 0; i--) 
                   {
                    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
                    && OrderSymbol() == Symbol() 
                    && OrderMagicNumber() == MagicNumber
                    && OrderType() == OP_BUYSTOP
                   && OrderCloseTime() > 0)
        
              
              while(IsTradeContextBusy()) Sleep(10);
       
BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,BuyPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Stop Order",MagicNumber,0,Green);
       
 
ernest02:

But the code does not work. Can anyone please tell me what is wrong with this piece of code?

You need to be a little more specific about what you mean by "does not work" . . . does not compile ? there is a missing brace } or . . there are existing trades but a new BUYSTOP gets opened anyway ? can you be more specific please ?
 

You're going through the list of current orders (open or pending) looking for a Buy Stop. By DEFINITION a Buy Stop has never opened so it will NEVER have a OrderCloseTime() > 0 and the loop does nothing.

Loop and count, call orderSend only if the count==0

 
WHRoeder:

You're going through the list of current orders (open or pending) looking for a Buy Stop. By DEFINITION a Buy Stop has never opened so it will NEVER have a OrderCloseTime() > 0 and the loop does nothing.

Loop and count, call orderSend only if the count==0

Thanks WHRoeder! I could not figure out why the program was out of control creating hundreds of stop orders when I wanted to create one only if there were none.

Wish there was a course where one could learn the basics of the internal workings of every operator/command/etc. It is so difficult when one has to guess how things REALLY work!

 
WHRoeder:

You're going through the list of current orders (open or pending) looking for a Buy Stop. By DEFINITION a Buy Stop has never opened so it will NEVER have a OrderCloseTime() > 0 and the loop does nothing.

Loop and count, call orderSend only if the count==0

I have changed the code to what I think you said I must do, but now the program does not place ANY orders! Here is the changed code:

         for(i = OrdersTotal()-1; i >= 0; i--) 
                   
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_BUYSTOP)
       
       {       
       Orders++;
         
       if (Orders == 0)
 
       if (IsTradeContextBusy()) Sleep(10);
       
       BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,BuyPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Stop   Order",MagicNumber,0,Green);
       
       // Error handling
            if(BuyTicket == -1)
               {
                  ErrorCode = GetLastError();
                  string ErrDesc = ErrorDescription(ErrorCode);

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

                  string ErrLog = StringConcatenate("Ask: ",Ask," Lots: ",LotSize," Price: ",BuyPrice," Stop: ",BuyStopLoss," Profit: ",BuyTakeProfit);
                  Print(ErrLog);
               }       
            }    
 

Take everything OUT of the LOOP

if (Orders == 0) DO WHAT?
Orders=0;
for(i = OrdersTotal()-1; i >= 0; i--) if(
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
&& OrderSymbol()      == Symbol() 
&& OrderMagicNumber() == MagicNumber
&& OrderType()        == OP_BUYSTOP)
{       
       Orders++;
}         
if (Orders == 0)
{
       if (IsTradeContextBusy()) Sleep(10);
What should happen after the buyStop opens? Orders will be zero
 

You need to look at your code more carefully . . . take your time, think about it . . .

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_BUYSTOP)
           
            // if this  ^ is true everything in the {} below is executed . . .

       {       
       Orders++;               //  at this point Orders has just been just been incremented , was it negative before ?  
         
       if (Orders == 0)                          //  is this  ever true ?
             if (IsTradeContextBusy()) Sleep(10);   //  will this ever happen. . .
 
RaptorUK:

You need to look at your code more carefully . . . take your time, think about it . . .


Thanks a lot for your help guys!

WH Roeder - when the program starts (when there are no orders) I want to place two stop orders - one Buy and one Sell. Then as the Buy or Sell orders are closed by the StopLoss I want to create a new Buy/Stop order in the place of the one closed. Therefore, later in the program I repeat this code for the Sell Stop Order.

So when I get a result that Orders == 0 I want to place a new stop order.

I am going to try the changes to the code as suggested and will report back.

RaptorUK - I do think about what the code must/should do. but because of my inexperience I do not know exactly how the operators work. I need to understand what happens behind the code - if you know what I mean.

 
ernest02:

RaptorUK - I do think about what the code must/should do. but because of my inexperience I do not know exactly how the operators work. I need to understand what happens behind the code - if you know what I mean.

I do know what you mean . . . but pseudo code or a flowchart (or even just a decent amount of thought) needs to come first, you need to be clear how you are tackling the problem A badly designed solution won't be made better by the best code syntax. When you know what you want to do but don't know the code for it look it up or ask . . .

A mix of code and pseudo code . . .

Orders = 0;

loop through the orders doing {

Check order symbol, check order magic no.
check if order is pending buy or activated buy

if these conditions are met . . . Orders++;  }

if Orders == 0;
{  place your new pending buy order  }

Which is basically what WHRoeder said up there ^

 
RaptorUK:

I do know what you mean . . . but pseudo code or a flowchart (or even just a decent amount of thought) needs to come first, you need to be clear how you are tackling the problem A badly designed solution won't be made better by the best code syntax. When you know what you want to do but don't know the code for it look it up or ask . . .

Which is basically what WHRoeder said up there ^

You are absolutely right! Good advice! I code with only an idea in my mind before I have thought it through properly. If I write down what I want to achieve with the algorithm before I start coding my logic will be better when coding.

Thanks! I will remember that!

 
ernest02:

You are absolutely right! Good advice! I code with only an idea in my mind before I have thought it through properly. If I write down what I want to achieve with the algorithm before I start coding my logic will be better when coding.

Thanks! I will remember that!



I have now changed the code according to the example given by WHRoeder, but now it places NO transactions! THis is even at the start of the program when no transactions have been placed yet. Here is the code"

         bOrders = 0;
 
         for(i = OrdersTotal()-1; i >= 0; i--) 
                   
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_BUYSTOP)
       
       {       
       bOrders++;
       }
        
       if (bOrders == 0)
 
       if (IsTradeContextBusy()) Sleep(10);
       
       BuyTicket = OrderSend(Symbol(),OP_BUYSTOP,LotSize,BuyPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Stop Order",MagicNumber,0,Green);
       
       // Error handling
            if(BuyTicket == -1)
               {
                  ErrorCode = GetLastError();
                  string ErrDesc = ErrorDescription(ErrorCode);

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

                  string ErrLog = StringConcatenate("Ask: ",Ask," Lots: ",LotSize," Price: ",BuyPrice," Stop: ",BuyStopLoss," Profit: ",BuyTakeProfit);
                  Print(ErrLog);
               }