Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 843

 
evillive:

int OrdersTotal(); - returns number, not true-false, that's what's wrong. There are no orders, it means 0, and if there are orders, it is not only 1.

And there are no closed orders only on a freshly opened account that we haven't traded on yet. Again, this is an incorrect use of the function.

And why RefreshRates(); AFTER the order has been sent to the server? It's like to pay for something first and then ask "how much is it?

Thanks, got it.
 
evillive:

int OrdersTotal(); - returns number, not true-false, that's what's wrong. There are no orders, it means 0, and if there are orders, it is not only 1.

And there are no closed orders only on a freshly opened account that we haven't traded on yet. Again, this is a misapplication of the function.

But why should RefreshRates(); AFTER the order has been sent to the server? It's like paying for something and then asking "how much does it cost?

Once I've heard the expression "The name of the Russian capital has two errors" and was shocked. Where could they be found there? Then I realised that you can make two mistakes.

What I mean is that there's also more than one error in the code line we're discussing.

if (!OrdersTotal()==true)

if(negation -> expression == true) Of course we can understand, like Artem explains, if 0 then false everything else is true Then it turns out if we deny that there are open orders then... oops... it is easier to write Moscow with two mistakes.

 
Escapee:

I have a chart in front of me. I see that an order is open on it. I decide to close it. that's it. nothing else is needed.

(I ask because. I can close it with a "one-click trade" cross, but I find it very convenient to use hot keys)

Here I am telling you how to do it programmatically right.

1. You have a chart in front of you with one order. You can see its ticket number, and the program? The program needs to know this number.

2. You want to close it. So you can see that the order with such a ticket is opened. And the program? The program does not know it. So the program needs to know whether the order with such a ticket is opened or not.

3. You can click on the cross, and that is it. And the program needs to give a command.

Therefore, the conclusion is as follows:

1. We find out if there is an open position

2. If so, we look for the ticket of the required order

3. We select the order based on the found ticket. If we have successfully selected it, then...

4. Check the time of closing of the order selected by the ticket. If the time of closing is equal to zero, then...

5. We close it.

 

I would correct your answer like this.

artmedia70:


1. In front of you is a chart with one order. You can see its ticket number

2. You want to close it.

3. You can click on the cross and the dot...

5. Close that.

 
AlexeyVik:

I would correct your answer like this.

Are you still kidding?
 
artmedia70:
Still kidding?

Yeah. It's the day off.


 
artmedia70:


i.e. it is not possible to make a closing script similar to the opening script? Because it's so simple... Iwas thinking...
 
Escapee:
So, we cannot make a close script similar to the open order script? Because it is so simple...I was thinking...

Opening an order is not so simple either - not with a single trade order sending command. All these commands need to be "wrapped up" in a full function with checking the trade server's return codes and reacting to those responses. Checking for permissible levels, recalculating them if an incorrect calculated level is set in the order, etc., etc.

And closing - the same thing: a separate function of searching for the right order, which calls a full-fledged function of closing, rather than just a line of code with sending an order to the server.

 
artmedia70:

Opening an order is not so simple either - not with a single trade order sending command. All these commands need to be "wrapped up" in a full function with checking the trade server's return codes and reacting to those responses. Checking for valid levels, recalculating them if the order specifies an incorrect calculated level, etc., etc.

Wait... finally understand what I need...

Here's the order opening I needed - OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0); . And this script works.

Now I need to close......

 
Escapee:

Wait... Understand finally what I need...

Here is the order opening I needed - OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0); . And this script works.

Now I need a close......

It's not a script. It's just a command to open an order. The OrderSend function returns the order ticket if the opening was successful. But before closing it must first determine the ticket of the order. To determine it, we first need to select the order from the list of orders by its index. If there is only 1 order open, its index in the list will be 0. Accordingly, if there is only one open order, we can do this without looping and checking for a symbol match and magic match. It is not very smart, but still. Exclusively for the sake of example.

if(OrderSelect(0, SELECT_BY_POS))
if(OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 30, clrNONE))
Print("Ордер закрыт");
Such an undocumented trick in mql4 can be used to close an order OrderClosePrice() instead of Ask or Bid, and in this case, we don't have to distinguish between the order types. It closes in any case.