Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 887
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Thank you! The error has disappeared, but the position still won't close.
Not enough code to tell exactly what the reason is
Excerpt:
void CritCloseBuy()
{
if (((Yellow_0-Red_0)<(Yellow_1-Red_1) && ((MA_0<MA_1)&&(MA_1>MA_2)) || ((Yellow_0-Red_0)>(Yellow_1-Red_1)&& ((MB_0<MB_1)&&(MB_1>MB_2))))
while(true)
{
ord_close=OrderClose(OrderTicket(),0.1,MarketInfo(OrderSymbol(),MODE_BID),0,Red);
if (ord_close = false)
{
Comment(" BAYposition closing failed with error #",GetLastError());//Message of the error
break;
}
else
{
Ord=0;
break;
}
}
Could be the reason for the opening:
void CritOpen()
{
if (///condition for opening//)
while(true)
{
ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,0,NULL,10011,0,clrNONE); //open order BAY, MAGIC 10011
if(ticket<=0)
{
Comment("Opening of position BAY-10011 failed with error #",GetLastError());
break;
}
else
{
Comment("BAY-10011 position opened with lot 0.1,price", Ask);
Ord=(Ord+1);
break;
}
}
Excerpt:
before ord_close=OrderClose(OrderTicket(),0.1,MarketInfo(OrderSymbol(),MODE_BID),0,Red);
it would be nice to select the order using the OrderSelect() function. I don't see the purpose of such a loop around the close function...
before ord_close=OrderClose(OrderTicket(),0.1,MarketInfo(OrderSymbol(),MODE_BID),0,Red);
it would be nice to select the order using the OrderSelect() function.
I have the only one. Well, actually it was so at first. I started to search for the cause using the groping method. I do not understand the sense of the loop.
I just cheat by analogy: if I opened with loop, so I should close too...
Igor Kim has our example:
void start() {
ManagePositions();
}
//+----------------------------------------------------------------------------+
//| Close positions at market price|
//| Parameters:|
//| sym - instrument name ("" - current symbol)|
//| op - operation(-1 - any position)|
//| mn - MagicNumber(-1 - any magik) |
//+----------------------------------------------------------------------------+
void ClosePositions(string sym="", int op=-1, int mn=-1) {
int i, k=OrdersTotal();
if (sym=="") sym=Symbol();
for (i=0; i<k; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==sym && (op<0 || OrderType()==op)) {
if (mn<0 || OrderMagicNumber()==mn) {
if (OrderType()==OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy);
}
if (OrderType()==OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell);
}
}
}
}
}
}
But the compiler requires checking the return value of OrderClose
Igor Kim has our example:
But the compiler requires the OrderClose return value to be checked
Well, do it like in the example - first select and then close. It doesn't matter if it is the only order or a million orders, because each order must be selected first.
The check is very simple if(!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell)) Print("Closing error #", GetLastError());
Igor Kim has ours as an example:
...
But the compiler requires the OrderClose return value to be checked
So do as in the example - select first and then close. It doesn't matter if there is only one order or a million orders, each one has to be selected first.
The check is very simple if(!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell)) Print("Closing error #", GetLastError());
I did the following: if (!OrderClose(OrderTicket(), OrderLots(), Bid, 0, Red))
{
Comment("The BAYposition closure failed with error #",GetLastError());
break;
}
else
{
Ord=0;
break;
}
Keeps demanding "return value should be checked .
Made it like this:
ord_close=OrderClose(OrderTicket(), OrderLots(), Bid, 0, Red);
if (ord_close = false)
{
Comment("BAY position closing failed with error #",GetLastError()); //Message of error
break;
}
else
{
Ord=0;
break;
}
Silent. But in both cases it does not close the position
ZS: I think there is an error in defining close and open conditions
The above post shows an example of a close condition. What could be wrong there?
It is silent. But in both cases it does not close the position
ZS: I think there is an error in the definition of conditions for closing and opening positions
The post above gives an example of a close condition. What could be wrong there?
Well if silently, with no errors, does not close, then the closing condition is not right. We need to rethink and rewrite this very condition.
The brackets here are somehow wrongly arranged:
if (((Yellow_0-Red_0)<(Yellow_1-Red_1) && ((MA_0<MA_1)&&(MA_1>MA_2))) || ((Yellow_0-Red_0)>(Yellow_1-Red_1) && ((MB_0<MB_1)&&(MB_1>MB_2))))
How about this?
if (((Yellow_0-Red_0<Yellow_1-Red_1) && (MA_0<MA_1 && MA_1>MA_2)) || ((Yellow_0-Red_0>Yellow_1-Red_1) && (MB_0<MB_1 && MB_1>MB_2)))