I must be really missing something here. I’ve coded the following, but it only closes the orders if the manual confirmation is active and I hit close in the confirmation box. Otherwise, it sometimes closes 1 out of 4-6 open orders, sometimes closes none. The alert line verifies that all the variables are set correctly, but it just won’t close the orders. HELP!
if((profit>=1.5)||(profit<-1.5))
{
Alert("Profit is ",profit);
for(x=totOrders-1;x>=0;x--)
{
if(OrderSelect(x,SELECT_BY_POS)==true)
{
switch(type)
{
case OP_BUY : OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,0);
break;
case OP_SELL : OrderClose( OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,0);
}
Alert("Closing order # ",OrderTicket()," which is in position ",x);
}
else
{
Alert("Error # ",GetLastError());
}
}
}
You know I found some code that allows you to have key board shortcuts. One of the features is to close all orders. i can’t get this thing to work though, I put it in meta editor and complied it but I can’t figure out how it works. Take a look at it and see if you can figure it out.
int start()
{
if (GetAsyncKeyState(VK_LCONTROL) && GetAsyncKeyState(VK_0))
Alert (“The ‘ctrl+0’ keys have been pressed, do you want me to do something?”);
return(0);
}
You’re probably getting slipped. Your parameters are telling the orderclose() function to close it at the price you specify, and only the price you specify. Just to see if this is it, try:
The “3” will close the order if the price is within 3 pips of the price you specified.
Alternatively, you may also need to pause after sending each order (add a Sleep(x) at the end of the loop, where “x” is the number of milliseconds to pause). The “trade context” can only process one order at a time. If your broker doesn’t close your trade the instant it gets the order to, the trade context may still be busy when the next orderclose() is received.
The code you posted doesn’t do anything. You’re defining the keyboard shortcuts, but more has to be added to actually do something with them once they’re defined. This looks like it is the beginning of what you’re expecting it to be (as far as defining the “virtual” keys), but more code is needed actually act on them.
This part:
if (GetAsyncKeyState(VK_LCONTROL) && GetAsyncKeyState(VK_0))
Alert (“The ‘ctrl+0’ keys have been pressed, do you want me to do something?”);
return(0);
is the part that would need to be modified to actually do something based on which key is pressed.