# Having problems with OrderClose

10

Hi,

I am new to programming MQL4, and have been looking at the MQL4 book examples. I am able to OrderSelect successfully, but when I get to the while function the EA freezes. It also says uninitialized variable for Ticket, and Lot. Any ideas?

```bool res;

res = OrderSelect(1, SELECT_BY_TICKET);

if(res == false)
{
}
else
{
Ticket = OrderTicket();
Tip    = OrderType();
Lot    = OrderLots();
}
while(res==true)
{
if(Tip==0 && Cls_B==true)
{
ticket = OrderClose(Ticket, Lot, Ask, 10*10, 0);
if(ticket <0)

{
}

else
{
if(Tip==1 && Cls_S==true)
{
ticket = OrderClose(Ticket, Lot, Ask, 10*10, 0);
if(ticket <0)

{
}```
Moderator
10570

res = OrderSelect(1, SELECT_BY_TICKET);

Why are you selecting ticket #1 ?

This will certainly only exist in the strategy tester

if(res == false)
{
}
else
{
Ticket =
OrderTicket();
Tip    =
OrderType();
Lot    =
OrderLots();
}

The highlighted variables are only assigned values if the orderselect is successful

while(res==true)

where is res set to false or where is the break? You risk being stuck in a continuous loop

if(Tip==0 && Cls_B==true)
{
ticket = OrderClose(Ticket, Lot, Ask, 10*10, 0);
if(ticket <0)

{

if Tip==0, then it is a buy order. Buy orders are close at Bid, not Ask. Just use OrderClosePrice(), it is simpler and can be used for buy and sell orders.

OrderClose() returns a bool, so check whether it returns true or false, not numbers

10

OrderSelect(1, SELECT_BY_TICKET)

I thought it selects the position of the order. For example: if there is 2 orders, it would take the order in position 1. Or 1/1.

I think I misunderstood the options here, does this reflect my thoughts?

`res = OrderSelect(0, SELECT_BY_POS, MODE_TRADES);`

while(res == true)

If I add a break; at the end, that'll work?

OrderClosePrice()

added this, and using it, although I am getting an error for variable CP - possible loss of data due to type conversion

EDIT:

I decided to isolate the first order on my strategy tester, which was a sell order. It looks like the criteria for the Cls_S isn't being met, although the same criteria is used earlier in my code to activate a Opn_B order. Any ideas why this might be happening?

```bool res;

if(res == true)
{
Ticket = OrderTicket();
Tip    = OrderType();
Lot    = OrderLots();
CP     = OrderClosePrice();
}
else
{
}

while(res == true)
{
if(Tip==0 && Cls_B==true)
{
closeticket = OrderClose(Ticket, Lot, CP, 10*10, 0);
if(closeticket == false)

{

}
else
{

if(Tip==1 && Cls_S==true)
{
closeticket = OrderClose(Ticket, Lot, CP, 10*10, 0);
if(closeticket == false)

{

}
break;```
Moderator
10570

We don't know what the conditions for Cls_S to be true are. We don't know whether Cls_S is a bool or not.

We don't know what these variables are

Ticket = OrderTicket();
Tip    = OrderType();
Lot    = OrderLots();
CP     = OrderClosePrice();
if CP and/or Lot are integers, they will be incorrect values