Cycle Operator 'for' questions - page 11

 
WhooDoo22:


I think you are missing my point . . .  if I add some braces maybe it will be clearer . . .  you did this . . .

I believe I meant to do this...

This is because I wished for the condition...

to be applied to the section of code within braces ("{ }").

 So why haven't you made that change in your latest version of your code . . . .  if you meant to do it why haven't you done it ?

 
WhooDoo22:

Simon,


The  0  in your image is probably from  . . .  


Nah-uh-uh! ;)

Lets take a trip down documentation lane shall we? Hahaha.

datetime OrderCloseTime( )
Returns close time for the currently selected order. If order close time is not 0 then the order selected and has been closed and retrieved from the account history. Open and pending orders close time is equal to 0.

Note: The order must be previously selected by the OrderSelect() function.


First, Lets both have a peek at the OrderClose() function code block I recently coded...

Next lets both have a peek at the shown result in a snapshot of the 'Experts' pane...

Aaah, what do we have here? :)

I'm not sure what point you are trying to make here, can you explain please ?  the Print(OrderCloseTime())  is obviously wrong but you still have it in your code,  why ?  what happened when you added the 2 lines of code I suggested ?
 

Simon,

So why haven't you made that change in your latest version of your code . . . .  if you meant to do it why haven't you done it ?

Give me a sec. to have a look please.

I meant to code this previously but do not mean to code this presently. There is no reason for coding this...

   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))       
      {
      if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
      if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}           
      if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
         {
         OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);
         Print(OrderCloseTime());
         GetLastError();
         }
      }

When I can code this instead...

   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))       
   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
     {
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);
      Print(OrderCloseTime());
     }

All 'if' conditions apply to what is inside the braces ("{}"). Adding braces to this block of code (in the way I meant to code it) is inefficient and serves no purpose.


Thank you.

 

Simon,


I'm not sure what point you are trying to make here, can you explain please ?

   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))       
   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
     {
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);
      Print(OrderCloseTime());
     }

// replace above code block with...

   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))       
   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
     {
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);
//      Print(OrderCloseTime());
     }

Give the EA a spin (without the Print() function) in the terminal on a demo account real quick, then click the 'Experts' pane and visualize the absence of the zero (0) from the rest of the results in the 'Experts' pane.

Notice there is no 0. Hmm, right? ;)

Now give the EA a spin (with the Print() function included).

Notice a 0 in the 'Experts' pane.


You made an educated guess as to why there was a zero in the 'Experts' pane results previously and you were right (scratch-out "Nah-uh-uh! ;)" at the beginning of the message.).

The  0  in your image is probably from  . . .  

Print(OrderCloseTime());

Remember? Sure ya' do. :)


 the Print(OrderCloseTime())  is obviously wrong but you still have it in your code,  why ?

Please explain why you believe it to be wrong.

I have it in the code so to show you a point. The point is: Zero is popping up in the 'Experts' pane because of this piece of code being in the code.


what happened when you added the 2 lines of code I suggested ?

I am currently working expediently on this. :)


Thank you

 
WhooDoo22:

 the Print(OrderCloseTime())  is obviously wrong but you still have it in your code,  why ?

Please explain why you believe it to be wrong.

I have it in the code so to show you a point. The point is: Zero is popping up in the 'Experts' pane because of this piece of code being in the code.


The last image you posted shows that the USDJPY Order was closed at 01:45:35 . . .  the next line of code that follows the line that closed the order is Print(OrderCloseTime());  and it results in a value of 0  . . .   0 as a datetime is Midnight 1st Jan 1970,  that isn't when you closed the Order,  as you correctly pointed out a return value of 0 from OrderCloseTime() can also be when the the Order is open or is a pending Order,  well your Order is closed,  you know it's closed,  your last image proved that . . .   so how can a value of 0 be correct ?   the answer is it cannot be correct,  so that means it is wrong.

 

Simon,

I am currently working on this...

   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))       
   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}           
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
     {
      int ClosedTicketNum = OrderTicket();   //  <---- add this line
      
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);
      
      OrderSelect(ClosedTicketNum, SELECT_BY_TICKET);   //  <---- add this line

      Print(OrderCloseTime());
      GetLastError();
     }

Thank you.

 
WhooDoo22:

Simon,

So why haven't you made that change in your latest version of your code . . . .  if you meant to do it why haven't you done it ?

Give me a sec. to have a look please.

I meant to code this previously but do not mean to code this presently. There is no reason for coding this...

When I can code this instead...

All 'if' conditions apply to what is inside the braces ("{}"). Adding braces to this block of code (in the way I meant to code it) is inefficient and serves no purpose.

OK,  looking at this code . . . .

if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))                                  // 1.
if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}                   // 2.
if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}                  // 3.
if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))   // 4.
     {  
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);                   // 5.
      Print(OrderCloseTime());                                                    // 6.
     }

 I have numbered the lines to make it easy to discuss.

Looking at line 1.  which of the other numbered lines of code will be executed if  OrderStopLoss()  and OrderTakeProfit() are both  0  ?

 

Simon,

Fixed, so it works as intended.

if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}                   // 2.
if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}                  // 3.
if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))                                  // 1.
if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))   // 4.
     {  
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);                   // 5.
      Print(OrderCloseTime());                                                    // 6.
     }

Lines 2 and 3.

Thank you.

 

Simon,

   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}
   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
     {
      int ClosedTicketNum=OrderTicket();
      
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);

      OrderSelect(ClosedTicketNum,SELECT_BY_TICKET);
      
      Print(OrderCloseTime());
     }

Result:

'Experts' pane result.

Why does the order close time represent in such a form (1357306061) ?

Is the reason why the Print() function works because the OrderSelect() function (within the braces) must select the closed order and NOT an open order? This must be done within the braces right? ;) Tricky, tricky.

Thank you.

 

Simon,

Here it comes... BAM!

   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}
   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}
   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))
     {      
      OrderClose(OrderTicket(),OrderLots(),bid_ask,3,CLR_NONE);

      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
      
      Print(OrderCloseTime());
     }

Even better! Hahaha ;)

Thank you.

Reason: