| / | Forum |
|
abstract_mind
2008.11.20 20:38
Which model to use
Which working model is the
best for running the EAs?
- approach 1: wait for each incoming tick in order to open/close/modify orders int start() { // do the work here // then go away, until the next tick comes in. } - approach 2: EA is always running and periodically asks fresh information from server in order to open/close/modify orders int start() { __double __ask=0; __while(!IsStopped()) __{ ____Sleep(5000); // sleep sometime ____if(Market_is_closed() ) ________break; // goes away. Resumes at the next tick (at market opening) ____RefreshRates(); // get fresh market information ____if(Ask==__ask) // apparently, no new info _______continue; ........__ask = Ask; // new ask price, could also test for Bid changes ____go_trading(); // go open/close/modify __} }
I personally enjoy and have been using approach 1, but the eventual issue here is whether the server, for some reason, stops broadcasting ticks. There are many hypothesis for this to happen, such as in high market volatility causing overload, server’s workload in processing orders, network traffic jam, ....
In the approach 2, there is always the warranty that the EA is running and it obtains newer rates periodically in order to proceed with trading.
Conclusion: approach 1 seems the one used in every EA I’ve seen. It is the one I've used in assessing EAs. But it seems to depend on the server “benevolence” to receive ticks. Approach 2, on the other hand, seems more reliable, as it is always running and playing the game. I haven't deeply studied this subject yet. So, I would really appreciate some wise opinions on this.
Thanks for the help. :-) Joao |
|
Interview with Alexey Kirienko (Axelforex) An Expert Advisor must make profit grow and cut losses equally stably and accurately. |
|
fbj
2008.11.20 21:59
hi, fwiw... are u not over worrying the data receipt issue? the system was designed so that experts, like any procedural lang's functions, gets called, does its functionality and exits/returns to caller. the expert/start() is at end of day, just a function generally speaking, functions do not do while(1), if did then why is it a function? i think call to refreshRates is not, in essence allowing your expert direct comms with server. if there is data, the client gets it, if not it will not get it regardless of your expert making the refreshRates call. they are not separated. the expert is not a program that can act on it's own - it makes calls into the Client Terminal system - not outside of it iow, if server "stops broadcasting ticks" - the client ie, Terminal will in no way be able to give your expert data it does not have. Data Ticks will be lost IF the expert is still running - the docs state this all over place. eg, if 10 ticks make up a bar then say expert is not in start(). on Client receiving tick.1 the expert start() is called and is up to expert to 'grab' [or not] this tick.1 if expert decides to not exit start() and another 5 ticks are incomming to Client then expert cannot 'get them back' - all builtin calls only return the last incomming data tick. Assumes say u look at Close[0] all time in start() - u only ever see tick.1 ok... maybe this of interest Best to U Here is simple test done demonstrate how: 1. expert that never exits never sees new data ticks = 17348_while(true) . 2. expert that exits always sees new data ticks = 17348_exitASAP . a chunk of experts tab on terminal follows: 2008.11.20 18:50:32 17348_while(true) EURJPY,M30: 119.835, 2008.11.20 18:50:32 and the two sources: //17348_while(true).mq4 //17348_exitASAP.mq4 |
|
fbj
2008.11.20 22:17
forgot refreshrates call in while(true), and below code i put experts tab chunk o/p //17348_while(true).mq4
in faster market see much more of 'exit' expert prints in between the 5sec print of 'while(true)' expert and be even more discrepancy 2008.11.20 19:01:40 17348_while(true) EURJPY,M30: 119.895, 2008.11.20 19:01:40 |
|
Ais
2008.11.20 22:35
Hello
It is very actual topic.
Approach 1 is only method for backtest and optimization purposes. Approach 2 is more suitable for maximum terminal control. Advantages of Approach 2 demonstrates Automation sample for the MetaTrader 4 Client Terminal. As for ticks, Approach 1 may lose new ticks anyway. Best regards
Ais
|
|
fbj
2008.11.20 23:01
//17348_while(true).mq4
2008.11.20 19:54:23 17348_while(true) EURJPY,M30: modeBid/118.775, modeAsk/118.805, 2008.11.20 19:54:23 |
|
solomun
2008.11.21 00:25
Hello, i am new to mql4 programming... so here is the structure of the code that i am using double order; int type; int init() { // some indicators here.... return(0); } int start() { init(); if(order == 0) { if(any condition) { Print("Open SHORT"); order = OrderSend(Symbol(), OP_SELL, vol, Bid, 3, Ask, 0, "", NULL, 0, Red); type = 1; } if(any other opposite condition) { Print("Open LONG"); order = OrderSend(Symbol(), OP_BUY, vol, Ask, 3, Bid, 0, "", NULL, 0, Blue); type = 2; } } if(order != 0) { init(); if(type == 1) // CLOSE SHORT { if( something ) { OrderClose(order, vol, Ask, 3, Red); order = 0; type = 0; } } if(type == 2) // CLOSE LONG { if( opposide something ) { OrderClose(order, vol, Bid, 3, Blue); order = 0; type = 0; } } } return(0); } is that structure good or is bad? there is no magic code... how to add ? and btw if an indicator is not initialized as that should be, what is the error value? 0 ? |
|
fbj
2008.11.21 01:32
fwiw - u would be wise to read MQL4 book will answer your questions and you will see why your structure should be altered... the book really does answer most questions - so many not take this important first step and all times they post they could easily answer own questions by just reading... i even make this mistake and if revisit the book and articles published on this site, many questions are answered. if still unanswered, it is very profitable to learn yourself by reading source code files and doing own tests. |
|
abstract_mind
2008.11.21 01:59
fbj wrote >>
forgot refreshrates call in while(true), and below code i put experts tab chunk o/p //17348_while(true).mq4
in faster market see much more of 'exit' expert prints in between the 5sec print of 'while(true)' expert and be even more discrepancy 2008.11.20 19:01:40 17348_while(true) EURJPY,M30: 119.895, 2008.11.20 19:01:40 This pattern confirms that first approach is more reactive to the market, because it runs with every tick arriving to the terminal. Second approach only runs every 5 seconds. Firts approach stops running if ticks are not delivered. =====> However, the important point is not on the marginal time (seconds), as illustrated in the above log. Imagine that your EA, due to something, didn't receive a tick that last 15 minutes, for a very active instrument, and you lose a great move. Would second approach, on these situations, work better because it is in control and is explicitly requiring fresh information? Therefore, the main point is whether there are undesirable conditions/situations that cause an EA stop receiving ticks even for a very active instrument, in which you might lose a great opportunity for profit or close a position. If these situations do exist, would second approach better overcome them? Thanks for the feedback. |
|
Ais
2008.11.21 03:18
Hello
Tick counter code sample: int start () // 01
{ // 02
static int ali.TicksReported.0 ; ali.TicksReported.0 = iVolume ( Symbol () , PERIOD_M1 , 0 ) ; // 03
static int ali.TicksReported.1 ; ali.TicksReported.1 = iVolume ( Symbol () , PERIOD_M1 , 1 ) ; // 04
// 05
static int ali.OpenTime.0 ; ali.OpenTime.0 = iTime ( Symbol () , PERIOD_M1 , 0 ) ; // 06
static int ali.OpenTime.1 ; ali.OpenTime.1 = iTime ( Symbol () , PERIOD_M1 , 1 ) ; // 07
// 08
static int ali.FrameLength ; ali.FrameLength = TimeCurrent () - ali.OpenTime.0 ; // 09
// 10
static int ali.TicksCounted.0 ; // 11
static int ali.TicksCounted.1 ; // 12
// 13
static int ali.LastOpenTime ; // 14
// 15
static int ali.Flag ; if ( ali.Flag != True ) { ali.Flag = True ; // 16
ali.TicksCounted.0 = ali.TicksReported.0 ; // 17
ali.LastOpenTime = ali.OpenTime.0 ; // 18
Alert ( "Start" ) ; return ; } // 19
// 20
if ( ali.LastOpenTime == ali.OpenTime.0 ) // 21
ali.TicksCounted.0 ++ ; // 22
else { ali.TicksCounted.1 = ali.TicksCounted.0 ; // 23
ali.TicksCounted.0 = 1 ; // 24
ali.LastOpenTime = ali.OpenTime.0 ; // 25
Alert ( "Frame 0 " , // 26
"is closed: " , TimeToStr ( ali.OpenTime.0 , TIME_MINUTES | TIME_SECONDS ) , " " , // 27
"Count: " , ali.TicksCounted.1 , " " , // 28
"Report: " , ali.TicksReported.1 ) ; } // 29
// 30
Alert ( Symbol () , " M1: " , "Frame 0: " , // 31
"Open: " , TimeToStr ( ali.OpenTime.0 , TIME_MINUTES | TIME_SECONDS ) , " " , // 32
"Seconds: " , ali.FrameLength , " " , // 33
"Count: " , ali.TicksCounted.0 , " " , // 34
"Report: " , ali.TicksReported.0 , " " , // 35
" // " , "Frame 1: " , // 36
"Open: " , TimeToStr ( ali.OpenTime.1 , TIME_MINUTES | TIME_SECONDS ) , " " , // 37
"Count: " , ali.TicksCounted.1 , " " , // 38
"Report: " , ali.TicksReported.1 ) ; // 39
} // 40
This sample illustrates that some ticks may be lost even without any computing load.As for advantages of Approach 2, A System: Code Sample 3 demonstrates server inspection in real time. Best regards
Ais
|
|
fbj
2008.11.21 12:15
has anyone considered deinit() in all this? how can it get called if in basically a forever loop in start()? wat if expert chopped dead in water? wat about 1 or more open trades... left open and unmanaged? btw, i asked [reasonably] in depth about the 2.5secs and deinit() and Sleep() etc. but [as usual] my posts never get answered, so i throw above into cook pot ;) . "AAA.AAA.9" how strange that MT compilers lexical analyzer not reject such <identifier> composition... docs clearly state: " Identifiers are used as names of variables, functions, and data types. The length of an identifier cannot exceed 31 character. Symbols you can use: numbers from 0 to 9, Latin capital and small letters a to z, A to Z (recognized as different symbols), the symbol of underlining (_). The first symbol cannot be a number. The identifier must not coincide with any reserved word. " i not see full stop/period in this paragraph. ok, works? but they free to fix and turn off tap any time = no one can complain cuz docs clearly state allowed characters = maybe much work for Ais... . is this drop off of MetaQuotes Corp? a test shows that this invalid identifier is allowed and compiles and runs - why so??? :) |
|
Ais
2008.11.21 12:57
Hello
As for "deinit ()" function, A System: Code Sample 3 stops very correctly.
It checks "IsStopped ()" condition and within "ac.RunTimePeriod = 1000" ms releases cycle. 1000 ms seems like a much less than 2.5 seconds. It is OK to place "deinit ()" in A System: Code Sample 3 and check it. Best regards
Ais
|