Lookback previous 1 tick values  [SOLVED]

Questions about MultiCharts and user contributed studies.
evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Lookback previous 1 tick values

Postby evdl » 11 Aug 2014

I would like to look back at previous 1 tick values on a chart with an higher resolution, let's say 1 minute. I use IOG and barmagnifier (set on 1 tick) when backtesting. Try to get the close of 10 ticks back.

Normally when you want to reference the close of the previous bar, you would use this to get the close of 10 minutes back (with IOG and barmagnifier set on 1 tick):

Code: Select all

If barstatus(1) = 2 then begin
close_10barsback = close[10];
end;


I assumed with IOG and barmagnifier, when I want to get the close of 10 ticks back I would use this:

Code: Select all

close_10ticksback = close[10];


Without the barstatus statement, I would reference the tick data.(every tick is a bar then)

But it seems the close[1] or any reference with [1] is always the barclose of the chart resolution. But when you use for example "close", you will get the close of 1 tick. And with the use of the barstatus statement, you will get the close of the chart resolution (in this case 1 min).

This made me doubt about how this worked in the past, but I could be wrong. Tried solving this with array's but I couldn't find a way to reference past bars without the [10] statement. Which is also used with an array.

Does someone have an idea how to solve this?

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Lookback previous 1 tick values  [SOLVED]

Postby evdl » 11 Aug 2014

Solved it!

After a whole afternoon of trying, I could not get it to work with a static array, but with a dynamic array it finally works. With the chart resolution on a higher timeframe and IOG and barmagnifier at 1 tick, with the code below, you can reference historical tick data.

Code: Select all

Vars: // tick close variabels
Intrabarpersist Close_2ticksback(0),
Intrabarpersist Close_5ticksback(0),
Intrabarpersist Close_10ticksback(0),
Intrabarpersist Tick_close(0),
Intrabarpersist Count(0);

Array: Intrabarpersist Tick_close_array[](0);

// add to count every tick
Count = count +1;

// expand the array by one
Array_setmaxindex(Tick_close_array, count);

// Post data to array
Tick_close_array[count] = close; // tick close

// calculate variabels
Tick_close = Tick_close_array[count];

If count > 10 and count <> 0 then begin // start when minimal count > 10
// get close of 2 ticks back
Close_2ticksback = Tick_close_array[count -2];
// get close of 5 ticks back
Close_5ticksback = Tick_close_array[count -5];
// get close of 10 ticks back
Close_10ticksback = Tick_close_array[count -10];
end;

// reset the array at the end of the session/day
If time >= 2203 then begin // this is end time of the chart
// decline the array to zero
Array_setmaxindex(Tick_close_array,0);
Count = 0;
end;

Print("Date: ",formatdate("dd-MM-yyyy ",eldatetodatetime(date))," Tijd: ", FormatTime("HH:mm:ss", el_timetodatetime_s(time_s)),
" Tick_close:",Tick_close," Count:", Count ," Tick[2];", Close_2ticksback, " Tick[5];", Close_5ticksback, " Tick[10];", Close_10ticksback);

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Lookback previous 1 tick values

Postby TJ » 11 Aug 2014

Solved it!

After a whole afternoon of trying, I could not get it to work with a static array, but with a dynamic array it finally works. With the chart resolution on a higher timeframe and IOG and barmagnifier at 1 tick, with the code below, you can reference historical tick data.
::
Congratulations on your success.
Thank you for sharing your hard work.


Return to “MultiCharts”