MQL4 - automated forex trading   /  

Forum

Order serliazer?

Back to topics list To post a new topic, please log in or register

avatar
90
Michal 2006.01.15 15:59 
Hi everybody,

from some time I'm thinking about correct handeling of #trade context busy error. Does anybody has any general and portable solution?

Michal
Expert Advisors Based on Popular Trading Systems and Alchemy of Trading Robot Optimization (Cont.)

Expert Advisors Based on Popular Trading Systems and Alchemy of Trading Robot Optimization (Cont.)

In this article the author continues to analyze implementation algorithms of simplest trading systems and introduces recording of optimization results in backtesting into one html file in the form of a table. The article will be useful for beginning traders and EA writers.


avatar
Moderator
5198
stringo 2006.01.16 14:16 
see example
//+------------------------------------------------------------------+ //| trade.mq4 | //| Copyright © 2006, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, MetaQuotes Software Corp." #property link "http://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); } //+------------------------------------------------------------------+

avatar
90
Michal 2006.01.17 10:44 
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?
Back to topics list  

To add comments, please log in or register