see example
//+------------------------------------------------------------------+ //| trade.mq4 | //| Copyright © 2006, MetaQuotes Software Corp. | //| https://www.metaquotes.net// | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, MetaQuotes Software Corp." #property link "https://www.metaquotes.net//" #include <stderror.mqh> #include <stdlib.mqh> extern int ExtStopLoss=20; extern int ExtTakeProfit=50; string ExtTradeSemaphore="trade_semaphore"; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void init() { //---- check for our semaphore if(GlobalVariableCheck(ExtTradeSemaphore)) { int day_checked=GlobalVariableGet(ExtTradeSemaphore); if(day_checked!=0) { int semaphore_check=Day(); //---- it may be wrong value in the our semaphore if(day_checked<semaphore_check-1 || day_checked>semaphore_check) GlobalVariableSet(ExtTradeSemaphore,0); } } else GlobalVariableSet(ExtTradeSemaphore,0); // semaphore created with "free" state } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { bool semaphored=false; int ticket,error; //---- while(!IsStopped() && !IsTesting()) { //---- check for zero value (free state) and occupate our semaphore with day number if(!GlobalVariableSetOnCondition(ExtTradeSemaphore,Day(),0)) { if(GetLastError()==ERR_GLOBAL_VARIABLE_NOT_FOUND) { //---- create with "busy" state GlobalVariableSet(ExtTradeSemaphore,Day()); semaphored=true; break; } //---- wait for half-second and check again else Sleep(500); } else { semaphored=true; break; } } //---- while(!IsStopped() && IsTradeAllowed()) { //---- ask price may be changed when waiting RefreshRates(); ticket=OrderSend(Symbol(),OP_BUY,1.0,Ask,3,Bid-ExtStopLoss*Point,Bid+ExtTakeProfit*Point); if(ticket<=0) { error=GetLastError(); Print("Error = ",ErrorDescription(error)); if(error==133) break; // trade is disabled if(error==134) break; // not enough money } else break; //---- 10 seconds wait Sleep(10000); } //---- free our semaphore if(semaphored) GlobalVariableSet(ExtTradeSemaphore,0); return(0); } //+------------------------------------------------------------------+
Thank you. I know the solution with a global variable. However, there are some flaws
with this method which I don't like:
- you need to do this for every interaction with the server. Not only OrderSend, but also close, delete and modify. Soon the EA code is poluted with settings/resettings semaphores. It can easily go out of control;
- you need to wait for an arbitry amount of time. I don't like this. I want the order be executed as soon as possible, and no one millisecond later.
A proper solution would be a local FIFO with orders. I wonder if such a solution already exists?
- you need to do this for every interaction with the server. Not only OrderSend, but also close, delete and modify. Soon the EA code is poluted with settings/resettings semaphores. It can easily go out of control;
- you need to wait for an arbitry amount of time. I don't like this. I want the order be executed as soon as possible, and no one millisecond later.
A proper solution would be a local FIFO with orders. I wonder if such a solution already exists?
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
from some time I'm thinking about correct handeling of #trade context busy error. Does anybody has any general and portable solution?
Michal