Error number 6 - page 10

 
[quote
Your example is a better one.
[/quote]

This example doesn't work. It gives errors. Could you, as a more experienced developer, help make it work?
Respectfully,
Quark
 
<br/ translate="no"> Question: how does while(!IsStopped()) ? It's not quite clear from the help, I thought it was a check on Allow Live Trading.

this is a check to see if they want to close the Expert Advisor externally

Question: these while and Sleep will not slow down the system?

the system will not be slowed down. only the single Expert Advisor

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

It is more difficult to deal with semaphores. During testing, real working EAs may compete for this semaphore. You don't need semaphores at all, since only one EA is being tested - we cannot organize simultaneous testing of interacting EAs. to avoid semaphore activity in testing, use the IsTesting function

Also concerning the logic. Between setting and removing the semaphore we have two (maximum) possibilities to handle orders. First Buy() or Sell() and then, below, 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 get to CloseOrder() until Buy() returns?

These activities will not compete with each other since trade operations are now synchronous, i.e. expert waits until the trade operation is completed. "the process is guaranteed to be linear"
 
This example does not work. It gives errors. Could you, as a more experienced developer, help make it work? <br/ translate="no">

I won't force it. Wait a couple of days for GlobalVariableSetOnCondition function, then all access control problems will be solved
 
Этот пример не работает. Дает ошибки. Не могли бы Вы, как более опытный разработчик, помочь заставить его работать?

I won't force it. wait a couple of days for GlobalVariableSetOnCondition function, then all access delimitation problems will be solved


If you mean that you plan to introduce the GlobalVariableSetOnCondition function into MT, could you say a few words beforehand about it?

Including how you propose to use it in this situation?

A couple of days - counting weekends?
 
Slava, in addition to the useful advice, for which I thank you very much, I have come up with one idea :) Or rather, a suggestion. I have already mentioned it.

Why don't you make a queue of orders in MT. That is, you can asynchronously write a request to work on a position there, and MT in a separate thread will process that request when there is time, and with a guarantee that no one else will get in there.

Quark
 
If you mean that you plan to introduce the GlobalVariableSetOnCondition function in MT, could you say something about it beforehand? <br/ translate="no">
Including how you propose to use it in this situation?

A couple of days - counting weekends?

I already showed an example
if(GlobalVariableSetOnCondition(SemaphoreName, 1.0, 0.0)==true) { bSemaphored=true; break; }


if a global variable has a value of 0.0, then set the value to 1.0 and return true. otherwise, don't set anything. check and change the value in one function call.

"a couple of days" - I said that just in case. hopefully we'll post it today

 
Do you want to have an order queue in MT. That is, you can asynchronously write a request to work on a position there, and MT will process that request in a separate thread when there is time, and with the guarantee that no one else will get in there. <br / translate="no">.

We will not make a queue of orders
 
<br / translate="no"> I already showed an example


Just didn't say what it is :) I thought the function was suggested to write to me.


I said "a couple of days" just in case. hopefully we'll post it today.


Thanks.
 
Optional.

1. In these 100 posts there were several bugs found while working on the Expert Advisor, and not directly related to it. For example, I noticed that Expert Advisors continue trading even after removal of the Allow Live Trading checkbox. I hope you will not forget about them.

2. Below is the code of my Expert Advisor again. It generates errors 1, 129, 138. Could you a) tell me what 138(requote) is, why it occurs, whether it is related to semaphores, and how to fix it? b) Why 129? The prices are printed together with the error, they seem to be correct. c) About 1, too.

Let me remind you of the testing method: 12 minute windows for 12 currencies.

Respectfully,
Quark

double dStopLoss;
int nHoursToHold;

datetime timePrev = 0;
int nBars;
int nDelaySeconds = 3;

int nSlip = 50;

double dLotSize = 0.1;

int nMagic = 0;
int nDigits;

string strTradeSemaphore = "TradeSemaphore";

//////////////////
int init ()
{
	nBars = Bars;

	if(!IsTesting() && !GlobalVariableCheck(strTradeSemaphore)) 
		GlobalVariableSet(strTradeSemaphore, 0.0);
	
	dStopLoss = 110 * Point;
	nHoursToHold = 1;

	nDigits = MarketInfo( Symbol(), MODE_DIGITS );
	
	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;
		
	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;
*/

	bool bIsBarEnd = false;
	if(nBars != Bars)
	{
		if(IsTesting() || (!IsTesting() && CurTime() > Time[0] + nMagic * nDelaySeconds))
		{
			bIsBarEnd = true;
			nBars = Bars;
		}
	}
	
	if(!bIsBarEnd)
		return(0);

	// ------
	
	if(!IsTesting())
	{
		while(!IsStopped())
		{
			if(GlobalVariableGet(strTradeSemaphore) == 0.0)
				GlobalVariableSet(strTradeSemaphore, nMagic);

			if(GlobalVariableGet(strTradeSemaphore) == nMagic)
				break;
		
			Sleep(1000);
		}
	}
	
	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);
			}
		}
	}

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

	if(!IsTesting())
		GlobalVariableSet(strTradeSemaphore, 0.0);	
	
	return(0);
}
// ------

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

	dLotSize = GetLotSize();

	double dNormalizer = MathPow(10, nDigits);
	double dBid = Bid;//MathFloor(Bid * dNormalizer) / dNormalizer; //NormalizeDouble(Bid, nDigits);
	double dStop = Bid + dStopLoss;//MathFloor((Bid + dStopLoss) * dNormalizer) / dNormalizer; //NormalizeDouble(Bid + dStopLoss, nDigits);

	int nResult = OrderSend(Symbol(), OP_SELL, dLotSize, dBid, 
		nSlip, dStop, 0, "Friday", nMagic, 0, OrangeRed);

	if(nResult == -1)
	{
		int nError = GetLastError();
		Alert(Symbol() + ", sell: " + dBid + ", Stop: " + dStop + ", error: " + nError);
	}
}

// ------

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

	dLotSize = GetLotSize();

	double dNormalizer = MathPow(10, nDigits);
	double dAsk = Ask;//MathFloor(Ask * dNormalizer) / dNormalizer; //NormalizeDouble(Bid, nDigits);
	double dStop = Ask - dStopLoss;//MathFloor((Ask - dStopLoss) * dNormalizer) / dNormalizer; //NormalizeDouble(Bid + dStopLoss, nDigits);

	int nResult = OrderSend(Symbol(), OP_BUY, dLotSize, dAsk, 
		nSlip, dStop, 0, "Friday", nMagic, 0, Aqua);

	if(nResult == -1)
	{
		int nError = GetLastError();
		Alert(Symbol() + ", buy: " + dAsk + 
			", Stop: " + dStop + ", error: " + 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);
}
 
error 138 requote can easily occur. potentially with 10 working EAs the waiting time for semaphore release can be 30 seconds or more. during this time the price can easily change. in this case use RefreshRates function and take a new bid or ask value. or ask MarketInfo for a new price. 129 is the same error, wrong price - the price has gone too far and there were several price changes during the waiting time. 1 - this is not an error at all. it appears when you are trying to modify an order with the same values that have already been set. it is strange that it occurs when you manually confirm a trade on rejection