Error number 6 - page 8

 
the autopsy revealed incorrect use of error code 139. in fact it is "trade context is busy", for which there is no special code for some reason. a similar problem was discussed today in the english forum "Trade Dispatcher: all trade context is busy".
there is only one trade context for all EAs. the correct solution is to build your own system of semaphores on global variables.
i have run 4 EAs on 4 minutes. in 10 minutes the error 139 has occurred 7 times
 
the autopsy revealed incorrect use of error code 139. in fact it is "trade context is busy", for which there is no special code for some reason. a similar problem was discussed on the english forum today "Trade Dispatcher: all trade context is busy"
there is only one trade context for all EAs. the correct solution is to build your own semaphore system on global variables.
I have run 4 EAs on 4 min. in 10 min. 7 times error 139


Could you tell me, at least in general terms, what this system of semaphores should be?
1. In particular, how a trade should be made so as not to interfere with others.
2. Slips, checks, timeouts (which were promised to be absent in multithreaded MT, but come on)

And finally. Don't take this as a rebuke. You have a textbook. In it the expert is given. Show me those semaphores there.

Here's my expert. Simple as pants for a ruble twenty. Flip a position every hour. If you really care about traders and profits of brokers (I don't know about people, but I want to get a stable working EA first, and then switch from demo to real), then please, on the example of this EA of mine, show me how I should do it right.

Sincerely,
Quark

P.S. The question of the origin of errors 2, 6, 138 and 4109, which also sometimes appear, remains open.
 
<br / translate="no"> discussed a similar problem today on the English forum "Trade Dispatcher: all trade context is busy"


I have read the English language thread. Yeah... These guys need to learn Russian urgently.
It's not similar, it's the same problem. True, they haven't got errors 2, 6, 138 and 4109 yet. They were only talking about 139.

I don't see the point in IsTradeAllowed, to be honest. Slava himself explained how ten Expert Advisors are allowed to use this function and then suddenly start trading and everybody except the first one gets disappointed.

It would be much better to queue requests where they would pile up, live for some time, and either get executed or deleted. But this is all a dream.

Rather, we would have to create a global variable nTrading, for example, and store there the Expert Advisor's name. And what should other EAs do? Go back to MT3 with its pending orders Or there are other ideas?

By the way, we can do without global variable, something like

if(nPending == OP_BUY) nPending = Buy(); else if(nPending == OP_SELL) nPending = Sell();



where Buy() and Sell() return OP_BUY / OP_SELL on failure and -1 on success.

The disadvantages are obvious - the broker will receive ten orders to open (close) instead of one. If it is automatic, all is well. If it is a human - it will be offended. Worse, if the automaton and the human act according to different logic. For example, an automaton has no queue (as Slava explained to us, one thread, and the orders compete instead of forming a queue), and a human does. Then the trader starts to trade on the real, and he will not even understand why, because he was assured (me - at the seminar-presentation in Alpari) that there is no difference between demo and real.

The second option - each expert and currency is assigned its own Mn, so that the Expert Advisor + currency combination is unique. Then we write the code in such a way that the EA with IM1 will trade in the first second, with IM7 - in the seventh second, etc. after the bar opens. This will give the system one second to trade.

Question to Slava - is a second enough to avoid the problem?

Disadvantages are obvious, scalpers - pipsers will tell you about them :)

Dear developers (and all-all). Thanks for the clarifications. There are still some unanswered questions in this and previous posts. Waiting for an answer.

Quark




 
And one more question. Does the situation under discussion refer to stop orders? Do experts compete for their execution?
 
//+------------------------------------------------------------------+
//|                                                    TestQuark.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

double dStopLoss;
int nHoursToHold;

datetime timePrev = 0;

int nSlip = 5;

double dLotSize = 0.1;

int nMagic = 0;
string SemaphoreName="TradeSemaphore";

//////////////////
int init ()
{
   if(!GlobalVariableCheck(SemaphoreName)) GlobalVariableSet(SemaphoreName,0.0);
	
	timePrev = 0;

	dStopLoss = 110 * Point;
	nHoursToHold = 1;
	
	if(Symbol() == "EURUSD")
		nMagic = 1;
	else if(Symbol() == "EURJPY")
		nMagic = 2;
	else if(Symbol() == "USDCHF")
		nMagic = 3;
	else if(Symbol() == "GBPUSD")
		nMagic = 4;
	else if(Symbol() == "GBPJPY")
		nMagic = 5;
	else if(Symbol() == "GBPCHF")
		nMagic = 6;
	else if(Symbol() == "USDJPY")
		nMagic = 7;
	else if(Symbol() == "AUDUSD")
		nMagic = 8;
	else if(Symbol() == "EURGBP")
		nMagic = 9;
	else if(Symbol() == "USDCAD")
		nMagic = 10;
	else if(Symbol() == "EURCHF")
		nMagic = 11;
	else if(Symbol() == "EURAUD")
		nMagic = 12;
		
	timePrev += nMagic;	// Open nMagic seconds after the new bar

	return(0);	
}

// ------

int deinit()
{
	return(0);
}
// ------
int start()
{
	if(Bars < 5)
		return(0);
	
	// The previous bar just closed
	bool bIsBarEnd = false;
	if(timePrev != Time[0] + nMagic) 
		bIsBarEnd = true;
	timePrev = Time[0] + nMagic;
	
	if(!bIsBarEnd)
		return(0);

	// ------
	
	int nSignal = GetSignal();

	bool bSemaphored=false;
   while(!IsStopped())
     {
      if(GlobalVariableGet(SemaphoreName)==0.0)
        {
         GlobalVariableSet(SemaphoreName,1.0);
         bSemaphored=true;
         break;
        }
      Sleep(1000);
     }

	if(nSignal == OP_BUY) 
		Buy();
	else if(nSignal == OP_SELL) 
		Sell();

	for(int nCnt = OrdersTotal() - 1; nCnt >= 0; nCnt--)
	{
		OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES);

		if(OrderMagicNumber() == nMagic)
		{
			if(CurTime() - OrderOpenTime() > (nHoursToHold - 1) * 60 * 60)
			{
				if(OrderType() == OP_BUY)
					OrderClose(OrderTicket(), OrderLots(), Bid, nSlip, Aqua);
				else if(OrderType() == OP_SELL)
					OrderClose(OrderTicket(), OrderLots(), Ask, nSlip, OrangeRed);
			}
		}
	}

	if(bSemaphored) GlobalVariableSet(SemaphoreName,0.0);	

	return(0);
}
// ------

void Sell()
{
	if(AccountFreeMargin() < 500)
		return;

	dLotSize = GetLotSize();

	int nResult = OrderSend(Symbol(), OP_SELL, dLotSize, Bid, nSlip, Bid + dStopLoss, 
		0, "Friday", nMagic, 0, OrangeRed);

	if(nResult == -1)
	{
		int nError = GetLastError();
		Alert(Symbol() + ", " + nError);
	}
}
// ------
void Buy()
{
	if(AccountFreeMargin() < 500)
		return;

	dLotSize = GetLotSize();

	int nResult = OrderSend(Symbol(), OP_BUY, dLotSize, Ask, nSlip, Ask - dStopLoss, 
		0, "Friday", nMagic, 0, Aqua);

	if(nResult == -1)
	{
		int nError = GetLastError();
		Alert(Symbol() + ", " + nError);
	}
}
// ------

double GetLotSize()
{
	double dLot = 0.1;
	
	return(dLot);
}

// ------

int GetSignal()
{
	int nSignal;
	if(MathMod(Hour(), 2) == 0)
		nSignal = OP_BUY;
	else
		nSignal = OP_SELL;
		
	return(nSignal);
}

//+------------------------------------------------------------------+


if to make a function to set the value of a global variable provided that this variable has a certain value, so that there is no construct

      if(GlobalVariableGet(SemaphoreName)==0.0) { GlobalVariableSet(SemaphoreName,1.0); bSemaphored=true; break; }


then it will be 100% reliable
something like

      if(GlobalVariableSetOnCondition(SemaphoreName,1.0,0)==true) { bSemaphored=true; break; }




 
And one more question. Does the situation under discussion refer to stop orders? Do Expert Advisors compete for their execution?

No. Stops are executed on the server.
 
Rather, we would have to make a global variable nTrading, for example, and write the currently trading Expert Advisor's name in it. And what should the other EAs do? Go back to MT3 with its pending orders or there are other ideas?
For me personally there is a mandatory pause between trades (adjustable, now it is 30 sec). It's hardly an obstacle for profitable trading but it protects from similar situations. It is implemented through a global variable. Semaphore and pause - 2 in one =)

And about the queue - I had an idea to make an EA that executes orders written in a file. And all other experts just write orders to this file.
But it is not very easy for me ( in the sense of competent implementation )... But it is possible to try. By common effort =))
 
Thanks :)

This is the passage I didn't understand:

<br/ translate="no"> if make a function to set the value of a global variable provided there is a certain value in that variable, so there is no construct

if(GlobalVariableGet(SemaphoreName)==0.0)
{
GlobalVariableSet(SemaphoreName,1.0);
bSemaphored=true;
break;
}


Now about the logic of this case. Sorry to pester you, but...

If I understood correctly, we're sitting in a while loop until we manage to set the semaphore. Right? Then we trade, knowing that no one is trading except us. Then we return the semaphore to its original state.

Question: how does while(!IsStopped()) work? ? I thought it was a check for Allow Live Trading.

Question: won't those while and Sleep cause lags in the system?

Question: will Sleep and semaphore be processed correctly in test mode?

Another logical question. Between setting and removing the semaphore we have two (maximum) possibilities to handle orders. First Buy() or Sell() and then, below it, CloseOrder(). Won't these two "activities" compete with each other, though inside the EA, as if there were two Expert Advisors? Or the process is guaranteed to be linear and it will not reach CloseOrder() until Buy() returns?

Thank you in advance.
Quark
 
И еще один вопрос. Относится ли обсуждаемая ситуация к стоп ордерам? Конкурируют ли эксперты за их исполнение?

No. Stops are handled on the server. and in our case there is competition for the client's expert trading flow


I misspoke. Should OrderSend(OP_BUYSTOP... also surround it with code which sets and removes semaphores? Silly question. Sure, you should.
 
I attached the Expert Advisor, posted in the 1st post on the 4th page, to the eur - m15.
Reworked the trade functions (connected my library) and attached it to another euram - m15. Meijic, of course, has been changed.

I will let you know how it turned out;)