Cycle Operator 'for' questions - page 4

 

Simon,

Ok, let me try adding the stop level to the EA as well just for good measure.

Ah-ha! Freeze Level for USDJPY is 0 and Stop level is 30.

I assume 30 represents 3 because the broker is 5-digit. I tried using the MODE_SPREAD and received 20. Obviously the spread on USDJPY is not 20 but ;) 2.0. It could also be said that 2 pips is equivalent to 20 points.

Thank you.

 

A clue for you . . . .

 

 

Simon,

This seems to be a selecting order issue once again.

OrderClose() function is trying to close an order (not the USDJPY order). It is trying to close the order on the EURUSD chart.

OrderPrint() function has previously been selected before its execution and clearly resembles its purpose as intended.

The red color circling the invalid price must indicate WRONG.

The green color circling the USDJPY must indicate RIGHT.

So if all is true I must focus on red circle and select the order before execution of OrderClose() function.

Thank you.

 
WhooDoo22:


what is Bid and Ask ?

Ask is the latest known seller's price (ask price) for the current symbol.

Bid is the latest known buyer's price (offer price, bid price) of the current symbol.

You already know the reason why the close failed . . . . just think carefully for a minute.


Look at your code,  what price do you use to close the order ?

 

Simon,

Ask/Bid is the latest known seller's/buyer's price for the current symbol.

The price the EA wishes to close the order is of EURUSD symbol price.

Why is the EA dependent on being placed on a USDJPY chart to close its order? The OrderSelect() function was intended to select the ticket number and once the ticket number was selected, the EA could then select

Apparently, the EA must be on the USDJPY symbol chart to close orders on that 


Scratch everything I just said.

This is the solution. The error lies within the third parameter. I am considering specifying the symbol and THEN placing the ask/bid variable within it's parameter (location 3rd parameter of OrderClose() function). This should remove any obscurities from thought.

Thank you.

 
WhooDoo22:.

This is the solution. The error lies within the third parameter. I am considering specifying the symbol and THEN placing the ask/bid variable within it's parameter (location 3rd parameter of OrderClose() function). This should remove any obscurities from thought.

Thank you.

Bid and Ask are Predefined Variables  and are relevant ONLY to the current chart that the EA is on.  If you want Bid or Ask for a different symbol than the chart that the EA is on you can't use the Bid and Ask predefined variables.  You can use MarketInfo() though  ;-)
 

Simon,

The error lies within the third parameter of the OrderClose() function because ;) Ask/Bid is the latest known seller's/buyer's price for the current symbol. If "for the current symbol" means the current chart the EA is placed on then this can be a bit confusing. I should remember that MQL4 has been around a while and has its unique methods of illustration and representation.

The solution is to substitute Ask/Bid predefined variables with an alternative such as...

// solution 1.

MarketInfo("USDJPY",MODE_BID);

// solution 2.

MarketInfo("USDJPY",MODE_ASK);

What say you to this?


I added a version of this EA which has ability to print info of USDJPY order and close it regardless of the chart it is placed upon. I have not yet confirmed whether it has modified the USDJPY order SL and TP yet but will confirm upon verification. The EA produces a final error witch shouldn't be difficult to solve. I am working on this currently. here is a snapshot of the error reading:

invalid ticket error

Thank you.

 
WhooDoo22:

Simon,

The error lies within the third parameter of the OrderClose() function because ;) Ask/Bid is the latest known seller's/buyer's price for the current symbol. If "for the current symbol" means the current chart the EA is placed on then this can be a bit confusing. I should remember that MQL4 has been around a while and has its unique methods of illustration and representation.

The solution is to substitute Ask/Bid predefined variables with an alternative such as...

What say you to this?


I added a version of this EA which has ability to print info of USDJPY order and close it regardless of the chart it is placed upon. I have not yet confirmed whether it has modified the USDJPY order SL and TP yet but will confirm upon verification. The EA produces a final error witch shouldn't be difficult to solve. I am working on this currently. here is a snapshot of the error reading:


Thank you.

And now you see the limitations of your code . . .  you no longer have a USDJPY order,  so your while finds the order at position 0 and it's not USDJPY,  then it finds the next at 1 and it's not USDJPY then it looks at position 2 and the OrderSelect() fails,  this ends the while but the rest of the code is then executed, this gives a failed OrderModify(), a failed OrderPrint(), a failed OrderType() and failed OrderClose() . . .  what is that GetLastError() doing there all on it's own ?
 

Simon,

The cycle 'while' operator is performed as such: If the expression is true, the operator is executed until the expression becomes false. If the expression is false, the control will be given to the next operator.

The code's purpose in the SRC box below, is to do the following:

While variable 'i' (contained within OrderSelect() function) is NOT the USDJPY ticket position number AND the order symbol is not USDJPY, variable 'i' is increased until variable 'i' IS USDJPY ticket position number AND order symbol IS USDJPY. Once the 'while' cycle becomes false, control is then passed below, to the next line of code.

I provided the 'while' header and body for visual aid.

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

i++;

I believe you do understand how the 'while' cycle locates a ticket on USDJPY currency pair even without a ticket currently being on the currency pair but ;) I'm afraid I do not.

How can the 'while' cycle locate a ticket on USDJPY chart if there's no order on a USDJPY chart to locate?

I believe you wrote something along the lines of locating the order at position 0. Can you expound on this or possibly provide a link for study?

Thank you.

 
WhooDoo22:


I believe you do understand how the 'while' cycle locates a ticket on USDJPY currency pair even without a ticket currently being on the currency pair but ;) I'm afraid I do not.

How can the 'while' cycle locate a ticket on USDJPY chart if there's no order on a USDJPY chart to locate?

I believe you wrote something along the lines of locating the order at position 0. Can you expound on this or possibly provide a link for study?

If you want to write code for yourself on you own then you need to perform the task of 3 people,  maybe even 4. The person that drafts the requirements document,  the software engineer and the test engineer. It is not enough to know the syntax backwards and be able to write code . . .  you must understand the problem you are trying to solve and how you are going to solve it,  and you must also know how to test the code to make sure that it meets the requirement.

At a basic level this means going through your code line by line and making sure it does what you wanted it to do,  commonly this is with a range of values for each variable going from their min value, through typical values to max value.


Lets do this with your while loop . . . 

When you declare a int variable without setting it's value it has a starting value of 0,  so when you declare   i   (why not use a variable name that is more descriptive ?) it has a value of 0,  so the first time round the while loop  i  is 0,  so the OrderSelect() tries to select the order at position 0  if that works your OrderSymbol() call will return the correct symbol name for the Order at position 0 and if it isn't "USDJPY" then the expression( OrderSymbol() != "USDJPY" )  will result in a true value,  if the OrderSelect() is true and the symbol check is true,  true and true  =  true  and the next line of code ( this is within the while loop ) is executed.

Now  i  is 1  ,  the OrderSelect() selects the order at position 1 so it results in a value of true,  the symbol check does not find a symbol of USDJPY so returns true,  again true and true = true so  i++  is executed again

Now  i  is 2 ,  
the OrderSelect() tries to select the Order at position 2 but fails . . .  there are only 2 Orders open, at positions 0 & 1, so the OrderSelect() returns false,  what does the OrderSymbol() do ?  there is no valid Order selected  . . .  but even if it returns true for some weird reason,  the OrderSelect() has already returned false,  false and true = false . . .  the while loop has ended and  i   is set at 2 

Now work through the rest of the code bearing in mind that  i  has a value of 2 . . .