Hi all,
I tried to code 2 ordersend together but only one will be sent
OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, 0, "B", 100, 0, Blue);
OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, Ask + TakeProfit, "B", 200, 0, Blue);
Is there any work ard because my EA will trailing the first one and the second will be based on takeprofit close.
Thanks
what sort of errors did u get eg trade context busy? if so u need to detect this and resend yr order
what sort of errors did u get eg trade context busy? if so u need to detect this and resend yr order
I suppose so.
Any examples that I could follow?
Any examples that I could follow?
As ronaldosim is implying, it's just a question of checking GetLastError() if an order fails - i.e. if the ticket number returned by OrderSend is <= 0.
Based on your example code, I'd guess that your problem is one of the following three cases, in descending order of probability:
- By the time you come to place the second order, the market has moved and the Ask price is no longer valid. If so, you will see error #129. You can solve this either by doing a RefreshRates() to update the value of Ask, or by using MarketInfo(Symbol(), MODE_ASK) instead of Ask.
- The t/p on the second order is tighter than your broker allows. If so, you will see error #130.
- MetaTrader's trading thread is busy. If so, you'll probably see error #146.
can this function work if i put them in between
ToTrade();
Ticket = OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, Ask + TakeProfit, "RangerBreak.B", 10703234, 0, Blue);
if(Ticket < 0) Print("Error in OrderSend : ", GetLastError());
ToTrade();
Ticket = OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, 0, "RangerBreak.B", 10703234, 0, Blue);
if(Ticket < 0) Print("Error in OrderSend : ", GetLastError());
void ToTrade()
{
for(int i = 0; i < 60; i++)
{
if(IsTradeAllowed() == false || IsTradeContextBusy() == true)
{
Sleep(1000);
}
else
{
RefreshRates();
break;
}
}
}
can this function work if i put them in between
ToTrade();
Ticket = OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, Ask + TakeProfit, "RangerBreak.B", 10703234, 0, Blue);
if(Ticket < 0) Print("Error in OrderSend : ", GetLastError());
ToTrade();
Ticket = OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, 0, "RangerBreak.B", 10703234, 0, Blue);
if(Ticket < 0) Print("Error in OrderSend : ", GetLastError());
void ToTrade()
{
for(int i = 0; i < 60; i++)
{
if(IsTradeAllowed() == false || IsTradeContextBusy() == true)
{
Sleep(1000);
}
else
{
RefreshRates();
break;
}
}
}
i would prefer this than ToTrade()
while(IsTradeContextBusy()) {Print("Trade context is busy. Please wait");}
if (IsTradeAllowed())
{
Send yr orders
}
i would prefer this than ToTrade()
while(IsTradeContextBusy()) {Print("Trade context is busy. Please wait");}
if (IsTradeAllowed())
{
Send yr orders
}
then if IsTradeAllowed() is false and passed the condition to buy?
then if IsTradeAllowed() is false and passed the condition to buy?
If you're concerned about error #146 (trade context busy), I'd have a look at the suggestions in 'Error 146 ("Trade context busy") and How to Deal with It'. Under heavy load (e.g. hundreds/thousands of orders per hour) I consistently get serious problems in MetaTrader if I don't use the techniques described in sections 5 and 6 of this article - albeit at the cost of implementing what's effectively co-operative multi-tasking between EAs.
But I'd be surprised from your example code if error #146 is your problem. It still looks more likely to be the ask price being out of date by the time you come to place the second order.
i got the error 138 even using my ToTrade function
What happens if you use MarketInfo(Symbol(), MODE_ASK) instead of Ask?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
I tried to code 2 ordersend together but only one will be sent
OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, 0, "B", 100, 0, Blue);
OrderSend(Symbol(), OP_BUY, LotSize(Risk), Ask, 3, Ask - StopLoss, Ask + TakeProfit, "B", 200, 0, Blue);
Is there any work ard because my EA will trailing the first one and the second will be based on takeprofit close.
Thanks