Questions from Beginners MQL5 MT5 MetaTrader 5 - page 383

 
Vitalie Postolache:

If not external - there is PositionClose

You can also open a position with the same volume in the opposite direction, which will result in a close.

no such function ?
 
Tapochun:

We set two local variables in the OnTick function of the bool type: buyExist = false and sellExist = false.

When a new tick comes, you check if there are any orders that belong to this EA. If there is a sell order, sellExist = true and the same thing with the buy order. And then you check if buyExist = true - do not open a buy order. The same is true for sell. That is all.

What for do we need unnecessary variables if we are going to check anyway if there are open positions?

We have checked if there are any Buy positions - no open Buy, we have opened one. Why do we need variables?

 
Artyom Trishkin:

Why do we need extra variables if we are checking for open positions anyway?

We check for Buy - no Buy open - we open one, we check for Sell - no Sell open - we open one. Why do we need variables?

We can do it this way. I simply presented it in my mind as a separate check function where flags are passed by reference. In this case, my variant performs only one function - check for orders and not check+open. It would be easier to extend the program. In short - one function - one action.
 
Artyom Trishkin:

Why do we need extra variables if we are checking for open positions anyway?

We check for Buy - no Buy open - we open one, we check for Sell - no Sell open - we open one. Why do we need variables?

I thought it would be similar to OrderTotal.
 
le0nid2014:
I thought it would be something like OrderTotal, but here you have to do the rebound and write the function to recognise orders?
How can you do it without brute force? You may have a lot of orders from a lot of Expert Advisors for a lot of symbols at one time. You want your Expert Advisor to process only its own orders, don't you?
 
Tapochun:
You could do that. I just imagined it in my head as a separate check function, where flags are passed by reference. In this case, my variant, performs only one function - check for orders, not check+open. It would be easier to extend the program. In short - one function - one action.

Well, the check is a function call that returns a flag:

if(!Function_check_open_position(symbol, OP_BUY,magic)) function_open_position(symbol,OP_BUY,Lots,stop_loss,take_profit,magic,comment);
if(!Function_check_open_position(symbol,OP_SELL,magic)) Function_open_position(symbol,OP_SELL,Lots,stop_loss,take_profit,magic,comment);

 
le0nid2014:
I thought it would be something like OrderTotal.
Of course, search for a market order by symbol, type and magician, and return the search result: true - yes, false - no.
 
new-rena:
no such function ?
Yeah, my mistake when I said "no inludes", that function is just in the Trade.mqh inluder.
 
Tapochun:
How can you do it without trying too hard? You may have a heap of orders from a heap of Expert Advisors on a heap of symbols at once. And you want your EA to process only your orders, don't you?

it is an initial addition to the main robot . the main robot always has two open orders in both directions (the main robot picks them up, but the main robot itself can not start . When the main robot finishes one of the directions it closes it) and then this EA should again open an order in the market direction where there is no order.

we just need to add order opening conditions to it


extern double Lots = 1.00;

extern int MagicNumb = 698541; //Magic


int start ()

{


if(OrdersTotal() == 0 && ********** )

OrderSend("USDCHF",OP_BUY,Lots,Ask,0,0,0, "5891",MagicNumb,0,Red);


if (OrdersTotal() == 0 && ***********)

OrderSend("USDCHF",OP_SELL,Lots,Bid,0,0,0, "58892",MagicNumb,0,Red);


}

 
le0nid2014:
it is an initial addition to the main robot . the main robot always has two open orders in both directions (the main robot picks them up, but the main robot itself can not start . When the main robot finishes one of the directions it closes it) and then this EA should again open an order in the market direction in which there is no order.
The two EAs should have the same wizards. They should be launched on different charts for one symbol. In general, of course, it is easier to improve the main EA than to build another one.
Reason: