About usage of TimeCurrent in EA and TI

 

Greetings everyone,

I have a quick question. I'm trying to rewrite most of my indicators so they work through history (I want to use them in visual testing through history - sort of forward test) and I'm having a royal pain regarding the TimeCurrent function.

Seems it works in a way that if you're using TimeCurrent inside an EA, TimeCurrent returns the modeled server time (so correctly takes tester modeled time).

If I use the same function in a TI, it always returns the latest known server time and NOT modeled time. Took me way too long to figure this out and had to rewrite some code using Time[0] which is imo very ugly to use as a modeled current time. This workaround serves my purpose I suppose, but would be nice if TimeCurrent worked in a TI as advertised as well.

Anybody else had this problem (could be I'm doing something - or many things - wrong) and possibly found a better solution?

Thanks in advance,

Fc.

 

I want to use them in visual testing through history - sort of forward test.

Just curious, are you trying to do one of those back-test with indicator instead of with the back-tester? I'm asking because I've taught about doing something like this as kinda a workaround for "faster-test speed" and "multi-currency sims" but the outlook seemed too-much work as I don't like Arrays.

 

those back-test with indicator instead of with the back-tester?

Not really sure what you mean.

I have some indicators that I developed that are purely line drawing and no array usage (Array usage would probably be better for it - because of mql4 mechanics -, but I hate the idea of using arrays to draw me a straight line).

So what I did, I rewrote them so that if they're used in strategy tester, they work the same way that they would in real time. Basically this means that you need to find the modeled time with timeseries, convert that to barshift and feed that barshift to iSomething.

Well the funny thing is that to get the modeled time in the first place, the TimeCurrent does not seem to work in TI. I can get it to work in EA that I'm using to slow down speed 32 ( I actually made a simple speed lever, that if I drag it up and down on the screen, it varies the speed of time (at maximum ST speed - 32), quite ingeniuos, really) and also using it to show the modeled time with TimeCurrent, and it works there perfectly.

However it is somewhat limiting having to use a different and clumsy method where TimeCurrent should suffice.

 

Ok.. just so I understand you correctly. You're trying to do Visual Testing walk-forward but Outside the back-tester. You've found a way to make it auto-scroll and using shift with iTime[0] but would like a easier process.

Well, I think you're on the right track, and probably only track if you're doing this Outside the back-tester. However, why not do this within the Back-tester? You've already figured out custom speeds for that and also, there's lots of codes within the code base that gives ideas on Visual Manual Testing using the back-tester.

Added: I guess my big question here is (for what purpose - what good is this suppose to do). Most indicators I've seen uses time[array], time[0] returns the open-time and not time-current. The back-tester will only return open-time on time 0. If your purpose here is to make the indicator work in backtester as it would in real-life then TimeCurrent() is Not the best Judge of that. Because back-tester time-current and real-life time-current can never be the same. Unless you have nano-seconds Tick date or something along those lines.

This is just my Opinion tho.. Hope someone else can shine some light on this :)

 

No no, I'm using it inside strategy tester or back-tester.

Reason why i need specific dates surrounding the tester's current time position is that my indicators only work in specific time interval, and that interval only is shown with the lines. No rocket science there.

However the problem is when you try to pinpoint the current time position (time model) in strategy (back) tester when adding indicators to the chart. An indicator added to the visual (strategy or back) test chart incorrectly recognizes TimeCurrent as real server last known time instead of tester-modeled time.

EDIT: I'm using this to teach myself how to trade forex.... I add the indicators I usually use and that have proven themselves to be most reliable and then let the market flow. That's why I said that it's kinda-forward test. I'm good at coding MQL4, but total noob when trading :) trying to give myself lessons thorugh history without losing any real money.

 
forexCoder:

Seems it works in a way that if you're using TimeCurrent inside an EA, TimeCurrent returns the modeled server time (so correctly takes tester modeled time).

If I use the same function in a TI, it always returns the latest known server time and NOT modeled time.

Anybody else had this problem

I just ran in to this myself. I have my CI in the tester.tpl and have
In EA
int start(){
  datetime now = TimeCurrent();
  Print("now=",TimeToStr(now, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
//2012.07.20 17:11:15   2008.03.18 22:00  WHRea35 EURUSD,H1: now=2008.03.18 22:00:00
// ^^ Current Time ^^                                     ^^ Tester modeled Time[0] ^^
In CI
int start(){
   datetime now = TimeCurrent();
   Log("Now=",TimeToStr(now, TIME_MINUTES|TIME_SECONDS|TIME_DATE)
   ," t0=",TimeToStr(Time[0], TIME_MINUTES|TIME_SECONDS|TIME_DATE) ...
   //[11160] WHR TOR: Now=2012.07.20 19:59:59 t0=2008.03.18 22:00:00 ...
   //        ^^ Market is currently closed ^^ ^^ Tester modeled Time[0] ^^
   :
///////////////////////////////////////////////////////////////////////////////
/** Log
* send information to OutputDebugString() to be viewed and logged by
* SysInternal's DebugView (free download from microsoft) This is ideal for
* debugging as an alternative to Print(). The function will take up to 10
* stringa (or numeric) arguments to be concatenated into one debug message.
//*/
#import "kernel32.dll"
   void OutputDebugStringA(string msg);
#import
void Log(string s1,    string s2="", string s3="", string s4="", string s5="",
         string s6="", string s7="", string s8="", string s9="", string s10=""){
    if (IsDllsAllowed()){
        string out  = WindowExpertName() + ": "
                    + s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
        OutputDebugStringA(out);
    }
}

Version: 4.00 Build 419 (31 Mar 2012)
 

To WHRoeder

This is one of the "features" of MQL4 that I've known about for some time - I was checking back here to see if anyone had a more elegant solution than using Time[0] instead of TimeCurrent?

You can use iTime(Symbol(), PERIOD_M1, 0) to give the most accurate equivalent, if running a custom TI on a different timeframe, but then you are limited by how far back your 1-minute data goes.

Frustrating....

 
Unnecessary in indicators. Just calculate the bars
int counted = IndicatorCounted();
if (counted < N) counted = N; // Handle Look back, a iMA(length) N=length
for(int iBar = Bars - 1 - N; iBar >= 0; iBar--){ datetime now = Time[iBar]; ..
 
WHRoeder:
Unnecessary in indicators. Just calculate the bars

Thank you for your help, I have finished my indicator. The result is this code which I am attaching. It shows the moving price average of the daily range, calculated on Median Price with Period=1 (1 day) , and displays it on a timeframe smaller than the daily.

The equivalent in code would be iMA(Symbol(), PERIOD_D1, 1, 0, MODE_SMA, PRICE_MEDIAN, 0), but this iMA function would not work if ran on, say ,15 minute timeframe. So I wrote the custom indicator instead.

Median Price = (High + Low) /2



	          
Files:
median.mq4  2 kb
 
HarriMQL4: The equivalent in code would be iMA(Symbol(), PERIOD_D1, 1, 0, MODE_SMA, PRICE_MEDIAN, 0),
That won't work in the tester since you can't get bar zero data for other time frames
#define HR2400      86400           // 24 * 3600
datetime TimeOfDay(datetime when){  return( when % HR2400          );       }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );       }
///
datetime now = Time[0],
         bod = DateOfDay(now);
int     iBod = iBarShift(NULL,0, bod),
        iHH  = iHighest(NULL,0, iBod+1,0);
double  HHofDay = High[iHH];
 

Hi WHRoeder

Sorry, but that line of code  - iMA(Symbol(), PERIOD_D1, 1, 0, MODE_SMA, PRICE_MEDIAN, 0) - works in my version of tester, in that it will return values to screen when tester is run in visual mode and Comment function used.

What is your code fragment above supposed to do/show?

Reason: