| stevehopwoodforex.com https://www.stevehopwoodforex.com/phpBB3/ Print view |
|
| Avoid Trade Context Errors https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=270 |
Page 1 of 1 |
| Author: | magft [ Thu Jan 19, 2012 4:32 pm ] |
| Post subject: | Avoid Trade Context Errors |
George and I have been trying a semaphore method based on this article to stop Error 146 Trade Context Busy errors occuring when using multiple EAs on same Empty4 ie multiple pairs of timeframes. I tried a few things for my ChangeTheColor EA but stil kept getting the odd one. George then suggested replacing calls to OrderSend and OrderModify with a customised version that uses a semaphore (Global Variable) that stops EAs sending trades until the semaphore says they can. Just replace all calls to OrderSend with _OrderSend and OrderModify with _OrderModify and drop the following code into the EA. Code: Select all |
|
| Author: | gaheitman [ Thu Jan 19, 2012 10:56 pm ] |
| Post subject: | Re: Avoid Trade Context Errors |
Just to "see the magic happen" I wrote an ea that enters 20 Buy orders at the current price. The EA waits until a user specified time and then tries to enter them all so that you can coordinate across multiple pairs. I ran three tests, all trying to enter 20 trades for each of 26 currencies all starting at the same time. Test 1: Regular OrderSend(), no error checking. Of the 520 trades sent, only 20 made it! The first currency sent all it's trades successfully and the rest merrily tried and failed with [Trade Context Busy]. Code: Select all Of the 520 trades, 53 still failed with the error check. At the moment of the check, the trade context was free, but when it tried to enter the trade someone else had grabbed the thread and we were blocked. Again, the error was [Trade Context Busy]. Code: Select all Of the 520 trades, 20 failed to enter because of a timeout waiting for the trading thread to become available. The timeout is currently 30 seconds, but that can be changed in the code. No [Trade Context Busy] errors, however. Code: Select all George |
|
| Author: | magft [ Thu Jan 19, 2012 11:33 pm ] |
| Post subject: | Re: Avoid Trade Context Errors |
Nice work George. I actually have use a combination of test 2 and 3 in my EA! I was thinking what about instead of a fixed 100ms wait make it a random value between 50 and 500ms. This way when loads of versions of the EA run at the same time this may give a bit more breathing space for trades. Just a thought. Mike |
|
| Author: | garyfritz [ Fri Jan 20, 2012 3:14 am ] |
| Post subject: | Re: Avoid Trade Context Errors |
It sounds like your test beat on it pretty hard and it worked well, George. But (geeky computer science pedantry here) this is NOT a safe semaphore. You can have situations like this: EA1: Is the semaphore 0? EA2: Is the semaphore 0? EA1: Ah, it is! Great! I'll set it to 1 and place my order. EA2: Ah, it is! Great! I'll set it to 1 and place my order. Apparently that happened seldom enough that it wasn't much of a problem, and the way things work, it worked out OK. Maybe the way threads work in Empty4 it can't happen -- not sure. In any event it looks like your solution improves the situation a WHOLE lot! |
|
| Author: | gaheitman [ Fri Jan 20, 2012 7:33 am ] |
| Post subject: | Re: Avoid Trade Context Errors |
Actually it should be safe after the initial creation of the variable. From that point forward it uses GlobalVariableSetOnCondition() to set the variable. According to the documentation, this provides atomic access to the global variable. From the documentation: bool GlobalVariableSetOnCondition( string name, double value, double check_value) Sets the new value of the existing global variable if the current value equals to the third parameter check_value. If there is no global variable, the function will generate error ERR_GLOBAL_VARIABLE_NOT_FOUND (4058) and return FALSE. When successfully executed, the function returns TRUE, otherwise, it returns FALSE. To get the detailed error information, one has to call the GetLastError() function. If the current value of the global variable differs from the check_value, the function will return FALSE. The function provides atomic access to the global variable, this is why it can be used for providing of a semaphore at interaction of several experts working simultaneously within one client terminal. George |
|
| Author: | garyfritz [ Fri Jan 20, 2012 8:49 pm ] |
| Post subject: | Re: Avoid Trade Context Errors |
Ah! OK, then I withdraw my pedantry. |
|
| Author: | gaheitman [ Fri Jan 20, 2012 9:00 pm ] |
| Post subject: | Re: Avoid Trade Context Errors |
Well, you were right too. I'll change the code to set it to 0 (not in use) to start, and if it makes it into the shell we should certainly use it that way. Or, try to create the variable in Init() when we aren't trying to trade. George |
|
| Author: | jb68 [ Thu Feb 02, 2012 6:32 pm ] |
| Post subject: | Re: Avoid Trade Context Errors |
Hi, Changing the order will not change the race issue. If u want to make it better you can implement a 2 stage individual locking semaphore. This is easy and u can do it with minor changes. EA request access so it check global variable. If no global variable then it will write it's own ID/Magic/Hash into the global. If writing is atomic(or not) even if 2 EAs started to write on the same time only one final ID/Magic/hash will be written. Now the EA will check the ID/Hash/Magic to confirm if is it's own ID/Magic/Magic before sending an order. After order has been sent it will delete the variable (set to 0). |
|
| Author: | mbkennel [ Mon Feb 06, 2012 5:32 am ] |
| Post subject: | Re: Avoid Trade Context Errors |
I don't think this problem can be solved like this. It sounds like it is very similar to the original Ethernet problem (multiple parties wanting to 'talk' on a single wire at the same time), and has the same solution: if you find it is busy, then wait a random time (distributed as an exponential). I'll soon post my LibOrderReliable which has wrappers for most of the Order**** functions and some decent error checking. |
|
| All times are UTC | Page 1 of 1 |
|
Powered by phpBB® Forum Software © phpBB Limited |
|