Problem using Variables Tick-by-Tick

 

I am having problems with MT4 in that it does not appear to save variable data on a tick by tick basis, but rather only at end of 1 min bars.

To explain: I have included a simple indicator, it does not display to screen, it is only writes to console (using Print). It is only for example.

In this short program i have created 1 variable, lastClose, which i have made "static". When a tick comes in I check and see if the current close is greater, lessor or equal to the previous lastClose. I print a message to the console and then set the lastClose to = the current Close.

Simple enough.

The lastClose data is only updated at the end of the 1 min bar. So, if there is only 1 tick in the current 1 min bar, the simple code works fine. Where it messes up is when there are multiple ticks in 1 bar.

Say, the last 1 min close was 1.280 then the first tick comes in for a new bar. Say the new price (close) = 1.281, lastClose will = 1.280, and the program Prints "Up..." and the program "tries" or "appears" to update the lastClose to 1.281. Now, say a second tick arrives during this same 1 min bar, Close[0] now == 1.282. lastClose, however, reverts back to 1.280, the close of the last 1 min bar, it does not Hold it's value i set in the last tick (1.281), after the completion of this tick, the new value for lastClose should == 1.282. But, if another tick comes in on the same 1 min bar the lastClose will again be reverted back to 1.280. Now say the new tick close = 1.281. That is a price that is LESS then the last tick, so my program should say that the price has gone DOWN, instead, it still says the price has gone UP because the lastClose value keeps reverting back to 1.280. If the last tick of this current bar =, say, 1.283, and we update the lastClose, it will finally save that value only when we have advanced to the next new 1 min bar.

Now, i am running this on 1 min bars on the chart, so i am not sure how it works on, say a 15 min chart.

But, it appears that MT4 code only Temporarily writes a variable's value durng the course of a tick, but, then reverts that variables data to the last 1 min bar, until the bar has completed, then it finally updates the variable.

Anyone has had similar experience, and/or found a work around?

(NOTE - you can only see the problem i am showing here when there are more than 1 tick per 1 min bar, in an active market, and you need to look at the Print results in the Experts window of the console/terminal.)

Files:
 

Found Work-Around

Well, I found my own work-around to the problem

(The problem was that data in an indicator/expert was only being saved by MT4 at the end of a min bar, and even though i set a value to a variable, it would revert to the previous bar's value for that variable if i get multiple ticks in the same 1 min bar)

I wrote a library file with 2 functions. SaveData and ReadData and I made a local global array in the library for saving the data. In my main Indicator or Expert program, if i need to save and read data on tick by tick basis (within a 1 min bar), i save the data to SaveData(ptr, value) (ptr is the index to the data array in the library function), then to get the data, i call ReadData(ptr).

The data in the library does not revert back to the previous bar's data while gettng multiple ticks in one bar.

I have attached both a new TickData test file and the library function

(Libraries go in the /library sub dir)

Files:
 
unltdsoul:
I am having problems with MT4 in that it does not appear to save variable data on a tick by tick basis, but rather only at end of 1 min bars.

I did the following and it works

static double lastClose;

double b = Bid;

if( b > lastClose )

Print(TimeToStr(Time[0])," Up Close="+DoubleToStr(b,Digits)+" lastClose="+DoubleToStr(lastClose,Digits));

else if( b< lastClose )

Print(TimeToStr(Time[0])," Dn Close="+DoubleToStr(b,Digits)+" lastClose="+DoubleToStr(lastClose,Digits));

else

Print(TimeToStr(Time[0])," NoChange Close="+DoubleToStr(b,Digits)+" lastClose="+DoubleToStr(lastClose,Digits));

lastClose = b;

return(0);

06:33:14 Tick Data USDCHF,M1: 2006.07.04 06:33 Up Close=1.2235 lastClose=0.0000

06:33:14 Tick Data USDCHF,M1: 2006.07.04 06:33 NoChange Close=1.2235 lastClose=1.2235

06:34:25 Tick Data USDCHF,M1: 2006.07.04 06:34 Dn Close=1.2234 lastClose=1.2235

06:34:28 Tick Data USDCHF,M1: 2006.07.04 06:34 Up Close=1.2235 lastClose=1.2234

06:34:29 Tick Data USDCHF,M1: 2006.07.04 06:34 Dn Close=1.2234 lastClose=1.2235

06:34:31 Tick Data USDCHF,M1: 2006.07.04 06:34 Up Close=1.2235 lastClose=1.2234

06:34:32 Tick Data USDCHF,M1: 2006.07.04 06:34 Dn Close=1.2234 lastClose=1.2235

06:34:59 Tick Data USDCHF,M1: 2006.07.04 06:34 Up Close=1.2235 lastClose=1.2234

06:36:20 Tick Data USDCHF,M1: 2006.07.04 06:36 Up Close=1.2237 lastClose=1.2235

06:36:20 Tick Data USDCHF,M1: 2006.07.04 06:36 Dn Close=1.2235 lastClose=1.2237

06:36:20 Tick Data USDCHF,M1: 2006.07.04 06:36 Up Close=1.2236 lastClose=1.2235

Reason: