You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
if (err==146)
{
while (IsTradeContextBusy())
{
if (IsTradeAllowed()) break;
else
Sleep(1000*1,1);
}}
I think this is a faster solution. Please share your comments.
Thank you.
How would you feel about this variant:
This seems to me to be a faster solution.I work this way . Comment.
Thank you.
I'll be fine with it. With understanding. The pause is less, it works faster... But there is some unjustified redundancy in your version.
The IsTradeContextBusy function returns the flag of trade thread occupancy. There was no such function before the 195th build at all. So we used the IsTradeAllowed function that returns a sign that the EA is allowed to trade and the trade flow is free.
We allow the EA to trade by checking "Allow EA to trade" box in the EA properties dialog box (F7 key).
The error 146 (Trade Stream is busy) has nothing to do with allowing the Expert Advisor to trade. Therefore, let us have a look at the properties of the IsTradeAllowed function as far as they relate to the trade thread only.
So, the trade thread is free and the IsTradeAllowed function returns True. The trade thread is busy, the IsTradeAllowed function returns False. Now let's take a look at the values returned by the IsTradeContextBusy function. The trade thread is free, the IsTradeContextBusy function returns False. The trade thread is busy, the IsTradeContextBusy function returns True. We can see that for the same trade flow states, the values of the IsTradeAllowed and IsTradeContextBusy functions are opposite. Moreover, these functions duplicate each other rather than complementing each other with respect to the trade flow states. Therefore, one of them can be excluded. Which one? The IsTradeAllowed function, as I have already mentioned above, apart from the trade flow state flag, also returns the flag allowing the EA to trade which in this situation, i.e. when handling error 146 (Trade Flow is busy), is not necessary to us. Therefore, it will be necessary and sufficient to use only one function IsTradeContextBusy. If you carry out the above abbreviations, your code will be reduced to mine:
with the only difference being the size of the pause. But this is a matter of taste, personal preference, trading style and so on. You could put a pause of 0.1 second. It's not a big deal... I just prefer 11 seconds.Two errors were found in the SetOrder function:
Attention! The post with SetOrder function for online trading has been corrected. The function is a bit longer. It didn't fit into the whole post, so it had to be moved to the trailer.
In this post I decided to give the main points explaining how the SetOrder function works. I do not work with orders myself, i.e. I am in someone else's territory here. Perhaps someone who knows how the SetOrder function works will suggest improvements or find errors.
1. In the first lines of code, local variables are declared and some of them are initialized. For example, in lsComm, the name of the EA and the name of the timeframe returned by the GetNameTF function are written. By the way, I am not going to dwell on such functions as GetNameOP, GetNameTF, Message, etc., only if someone has questions about them.
2. Checking received parameters. If sy is empty, it is initialized with the name of the current tool. The variable for the colour of the icon on the chart is initialised by the type of the operation. If the non-zero expiry time of the order is less than the current time, it is reset to zero.
3. the body of the cycle of trading attempts, the number of which is limited by the value of the global variable NumberOfTry. The operations performed inside the main loop of the SetOrder function go further.
4. If the SetOrder function is not executed in the tester, we have the opportunity to terminate its operation. Waiting for the trade flow release cycle is performed here. Market environment variables are updated and current time is recorded.
5. Sending a request to a trade server. If the ticket is positive, the SetOrder function is terminated.
6. If the ticket is negative, the processing of execution errors is performed.
7. In the case of errors 128 (Waiting time has expired for deal execution), 142 (Order has been queued up) and 143 (Order accepted by dealer for execution) we pause for 66 seconds. After the pause, using the ExistOrders function (which we will describe later) we check if the order has already been set in the time interval between the server's request and the current moment. If the order has been set, exit the function.
8. The item size and the Bid and Ask prices are stored in the local variables.
9. In case of error 130 (Incorrect stops) the price levels of the order, stop and take out are corrected if possible.
10. The information which can help to solve the problem or find the error later is displayed in the report.
At the end we process other errors. Some of them show a long pause (5 minutes), others block the Expert Advisor, others allow further attempts to trade, etc.
That's it! End of description!
The ExistOrders() function.
Returns a flag for the existence of an order. Answers the question if the order is set or not. You can use this function to request any order, as well as more specific ones. The request filter is configured using the function parameters:
Examples of how to use the ExistOrders() function.
1. Check availability of any order
2. Check availability of any order for any instrument on the current chart
3. Check presence of BuyLimit order on any instrument
4. Check if there is a SellStop order with magic number 123456 on EURUSD
5. Check the availability of any order with a setup time of 2 hours or less
In the trailer there is a script to test the ExistOrders function. The first 4 examples in the script are commented out.Warning! In the post of 12.03.2008 07:24 I have replaced the SetOrder.mq4 attachment