Cycle Operator 'for' questions - page 10

 
WhooDoo22:

I fixed the "invalid ticket" error for the coding exercise you advised me to complete. I attached a copy of the expert advisor for your personal viewing.

OrderClose() function section of this exercise I believe to be complete.

Print() function section of this exercise I believe to be complete.

All I believe remaing is the third section, OrderModify() function.

Thank you.

You haven't fixed the issue,  you have worked around it,  this is typical when the issue is not understood.  As I already said,  fix the cause not the symptom . . .

You are still not printing the USDJPY Ticket number,  please check the original requirement.

Your MarketInfo() calls are wrong even if they do work . . . 

 

Simon,

OrderModify() function is executed without error.

OrderPrint() function is executed without error.

OrderClose() function is executed without error.

Please post your response.

Thank you.

 
WhooDoo22:


Please post your response.


RaptorUK:

You haven't fixed the issue,  you have worked around it,  this is typical when the issue is not understood.  As I already said,  fix the cause not the symptom . . .

You are still not printing the USDJPY Ticket number,  please check the original requirement.

Your MarketInfo() calls are wrong even if they do work . . . 

And in addition . . .

if you have the same situation as before,  where there is no USDJPY order you will have failed  OrderStopLoss(), OrderTakeProfit(), OrderSelect() and OrderType() calls

what is this meant to do ?  what is your expectation of this line of code ?  please answer without looking up it's documentation.

GetLastError();

 In the final block of code,  why do want to execute this line of code . . . . 

   if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}

only if . . .

   if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))       

 but you will always execute this line of code, and all the code following it,  regardless of the TP and SL ?

   if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);} 

 

 Your MarketInfo() calls are still wrong . . .  look at the documentation to find out why.

 

Simon,


You haven't fixed the issue,  you have worked around it...

What issue do you believe I haven't fixed yet?


You are still not printing the USDJPY Ticket number...

Order ticket number.



Your MarketInfo() calls are wrong even if they do work . . .

MetaEditor dictionary MarketInfo() function example:

Sample:

// MarketInfo() function example;

   double bid   =MarketInfo("EURUSD",MODE_BID);
   double ask   =MarketInfo("EURUSD",MODE_ASK);
   double point =MarketInfo("EURUSD",MODE_POINT);
   int    digits=MarketInfo("EURUSD",MODE_DIGITS);
   int    spread=MarketInfo("EURUSD",MODE_SPREAD);

Here is a sample of my OrderClose() function code block:

// OrderClose() function code block example;

   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();
     }

What do you believe is wrong with my OrderClose() function code sample format? It appears nearly identical to the 'MarketInfo()' sample provided by MetaEditor dictionary.


And in addition . . .


what is this meant to do ?  what is your expectation of this line of code ?  please answer without looking up it's documentation.

GetLastError();

The GetLastError() function prints an error to 'Experts' page while running tests. If the function finds no error, zero (0) is printed. Notice the zero printed in the snapshot I provided above.


 In the final block of code,  why do want to execute this line of code . . . .

if(OrderType()==OP_BUY){bid_ask=MarketInfo("USDJPY",MODE_BID);}

only if . . .

if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))

but you will always execute this line of code, and all the code following it,  regardless of the TP and SL ?

if(OrderType()==OP_SELL){bid_ask=MarketInfo("USDJPY",MODE_ASK);}


Yeah, I am glad you brought this up because I am going to explain my reasoning behind doing this.

If all other code blocks are omitted from the program execution using multi line comments "/* - */" and the only two code blocks remaining within the Start() and Return(0) functions are...

   int i;
   double bid_ask,
          OM_Param_3,
          OM_Param_4;

// code block 1;   

while(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true && OrderSymbol()!="USDJPY")
   
   i++;

// code block 2;

   if((OrderStopLoss()==0)&&(OrderTakeProfit()==0))
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderSymbol()=="USDJPY"))   
     {
      Print(OrderTicket());     
      GetLastError();
     }

beneath code block two is the function call:

     {
      Print(OrderTicket());     
      GetLastError();
     }

Print() function continues to print the number to the experts pane indefinitely and continues to print. I believed if I added...

if((OrderStopLoss()==0)&&(OrderTakeProfit()==0))

the Print() function would be executed only once because once the OrderModify() function directly below this was executed, the SL and TP would no longer be zero and would cause the Print() function to hault.

Now I realize this is not the case because of how a program is executed. I understand a program is executed from top to bottom so when the program finished its entire cycle the first time it would try to execute from the top to bottom again and would fail to print the ticket to the 'Experts' pane because there isn't an order on the USDJPY currency pair.


Thank you.

 
WhooDoo22:

Simon,


You haven't fixed the issue,  you have worked around it...

What issue do you believe I haven't fixed yet?

 The underlying issue that is caused by the way you have codded your while loop . . . the issue that was causing Invalid ticket for OrderClose function  and  Unknown ticket 2 for OrderModify   you haven't fixed thee cause you have addressed the symptom.

 

WhooDoo22:

Your MarketInfo() calls are wrong even if they do work . . . 

MetaEditor dictionary MarketInfo() function example:


 Sorry,  my mistake,  you have fixed that issue . . . do you know what the problem was that I was referring to ?

 

WhooDoo22:

Simon,

what is this meant to do ?  what is your expectation of this line of code ?  please answer without looking up it's documentation.

The GetLastError() function prints an error to 'Experts' page while running tests. If the function finds no error, zero (0) is printed. Notice the zero printed in the snapshot I provided above.


Nope,  GetLastError() does not print to the log . . .  it    "The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized. So, the next call for GetLastError() will return 0."

The  0  in your image is probably from  . . .  

Print(OrderCloseTime());

  . . .  and is an error as the Order is no longer selected as it is no longer in the Order pool as it has now been closed so is in the History pool.  to do what you are trying to do you would need something like 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();
     }

 . . . .  add these two lines and try it.

 

WhooDoo22:

Print() function continues to print the number to the experts pane indefinitely and continues to print. I believed if I added...

if((OrderStopLoss()==0)&&(OrderTakeProfit()==0))

 the Print() function would be executed only once because once the OrderModify() function directly below this was executed, the SL and TP would no longer be zero and would cause the Print() function to hault.

Now I realize this is not the case because of how a program is executed. I understand a program is executed from top to bottom so when the program finished its entire cycle the first time it would try to execute from the top to bottom again and would fail to print the ticket to the 'Experts' pane because there isn't an order on the USDJPY currency pair.

I think you are missing my point . . .  if I add some braces maybe it will be clearer . . .  you did 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();
     }

  . . .  did you mean to do 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();
         }
      )
 
WhooDoo22:

Simon,

You are still not printing the USDJPY Ticket number...


The requirement was . . .  "print the ticket number to the log in the form  "Ticket number for USDJPY =   " " 

 

Simon,


The underlying issue that is caused by the way you have codded your while loop . . . the issue that was causing Invalid ticket for OrderClose function  and  Unknown ticket 2 for OrderModify   you haven't fixed thee cause you have addressed the symptom.

I wish to discuss this issue with you in fine detail by itself, allowing me to focus my thoughts on resolving the issue without any distraction from other points.


Sorry,  my mistake,  you have fixed that issue . . . do you know what the problem was that I was referring to ?

Yes, thank you for bringing it up!


"The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized. So, the next call for GetLastError() will return 0."

MetaEditor dictionary reads:

int GetLastError( )
The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized. So, the next call for GetLastError() will return 0.

This function returns the last occurred error, then the value of special last error variable where the last error code is stored will be zeroized...

Green is understood and yellow is not understood.


 . . . .  add these two lines and try it.

   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();
     }

Will do.


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...

   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();
         }
      }

This is because I wished for the condition...

if((OrderStopLoss()!=0)&&(OrderTakeProfit()!=0))

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


The requirement was . . .  "print the ticket number to the log in the form  "Ticket number for USDJPY =   " "

It can be done.


Thank you.

 
WhooDoo22:


"The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized. So, the next call for GetLastError() will return 0."

MetaEditor dictionary reads:

int GetLastError( )
The function returns the last occurred error, then the value of special last_error variable where the last error code is stored will be zeroized. So, the next call for GetLastError() will return 0.

This function returns the last occurred error, then the value of special last error variable where the last error code is stored will be zeroized...

Green is understood and yellow is not understood.

OK,  as you can see from the Documentation GetLastError() prints nothing . . .  it just  returns the error number  . . .  so to use it you need to do something with it  . . .

Print("The last error was error number: ", GetLastError());

//  or . . .

int ErrorNumber = GetLastError();

//  or . . .

if(GetLastError() == 130)

When you call GetLast Error() it returns the error and then clears to zero the variable it was using internally to hold the last error . . .  so if you did this . . .

OrderDelete(-1);   //  should produce an error 4108 or 3

Print("Last error was number ", GetLastError());    //  should print - Last error was number 4108

Print("Last error was number ", GetLastError());    //  should print - Last error was number 0  the previous call cleared the GetlastError internal variable
 

 the 2nd call to GetLastError() will return 0 as the first call zeroed the variable that holds the error number . . .  also bear in mind that only the most recent error is held.

 
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 ("{ }").

But you didn't . . .   you missed it in your design,  then missed it when you coded,  then missed it when you read through your code and checked that it would do what you meant it to,  then you missed it when you tested . . . .   braces are one of those fundamental things that you must know like the back of your hand.

You need to understand why you missed this and address that problem . . .   no one else can do that for you,   unless you want to pay them to code for you.

 

Simon,


The  0  in your image is probably from  . . .  

Print(OrderCloseTime());


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...

   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());
     }

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

'Experts' pane result.

Aaah, what do we have here? :)


Please post a response for the newly attached expert advisor. Does it yet meets all criteria your exercise included?


The underlying issue that is caused by the way you have codded your while loop . . . the issue that was causing Invalid ticket for OrderClose function  and  Unknown ticket 2 for OrderModify   you haven't fixed thee cause you have addressed the symptom.

I wish to discuss this issue with you in fine detail by itself, allowing me to focus my thoughts on resolving the issue without any distraction from other points.


Thank you.

Reason: