Checking date or time of a bar in a strategy --> no backtest possible  [SOLVED]

Questions about MultiCharts and user contributed studies.
PK1
Posts: 102
Joined: 12 Jun 2011
Has thanked: 42 times
Been thanked: 12 times

Checking date or time of a bar in a strategy --> no backtest possible

Postby PK1 » 27 Apr 2018

Hello,
I'm programming a strategy which checks on Monday before close of the day if some criteria are fulfilled to open an order. The problem is that I couldn't find a way to get the appropriate date and time. It's never the date of the current bar. One can get HLOC but not the time of the bar?

The problem is that I can't access the date and time of a specific bar during development and during backtest. Even if the backtest is having weeks of data with many days fulfilling the criteria NO signal would be calculated because always the last calculated bar is used for the date.

Is this simply not possible or have I overseen some functionality regarding this?

An example of the code:

Code: Select all

if DayOfWeekFromDateTime(LastCalcDateTime) = Monday then
BEGIN
Commentary("It's Monday!");
if Time > 1550 And Time < 1600 then
// During backtest the strategy never ever comes here, except I test during the time and date
Any help would be appreciated!

Edit: Note, it's possible to get date and time after a position has been opened but date and time needs to be checked before the position is open

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

Re: Checking date or time of a bar in a strategy --> no backtest possible

Postby TJ » 27 Apr 2018

What is your chart resolution?

PK1
Posts: 102
Joined: 12 Jun 2011
Has thanked: 42 times
Been thanked: 12 times

Re: Checking date or time of a bar in a strategy --> no backtest possible

Postby PK1 » 27 Apr 2018

M15

PK1
Posts: 102
Joined: 12 Jun 2011
Has thanked: 42 times
Been thanked: 12 times

Re: Checking date or time of a bar in a strategy --> no backtest possible  [SOLVED]

Postby PK1 » 27 Apr 2018

Ok, I've found it!
For explanation:

Using things like this CurrentDate, LastCalcDateTime, LastCalcJDate always references the last actual bar or the last bar when replaying market data.

This is not to be used in a backtest because time or date checks reference only the last real bar not the bar seen in the backtest.
In a backtest there are fields like Symbol_* which get the right time for the seen bar in a backtest. Imho the distinction in using this should be documented in the dictionary.

So Symbol_Date and Symbol_Time should be used in a strategy but not LastCalcDateTime or alike.

MAZINGUER
Posts: 75
Joined: 06 Jan 2017
Has thanked: 9 times
Been thanked: 25 times

Re: Checking date or time of a bar in a strategy --> no backtest possible

Postby MAZINGUER » 27 Apr 2018

hi PK1,
By default, the signals are processed only once and at the close of the bar, both historically and in real, and the orders are generated in the next bar.
The time and date data are also given to the bar's tile.
Therefore, if the resolution of your chart is 15M, it is impossible to generate orders because, you will have the bar that closes at 15:45 and the next bar will close at 16:00, so in no time the condition: if Time> 1550 And Time <1600 is met

Maybe this can be solved by means of the attribute [IntrabarOrderGeneration] that makes the signal evaluate each historical bar four times (in the open, in the high, in the low and in the close) and, in real time, the signal is calculated tick a tick

On the other hand, the words currentDate and currentTime refer to the time you have on your computer and the words Date and Time refer to the closing date and time of the bars of the chart (which do not have to match those of your computer)
The truth is that working with dates and times is delicate because it is easy to fall into errors.

PK1
Posts: 102
Joined: 12 Jun 2011
Has thanked: 42 times
Been thanked: 12 times

Re: Checking date or time of a bar in a strategy --> no backtest possible

Postby PK1 » 27 Apr 2018

@Mazinguer
Thx for the reply. In the meanwhile I've found the solution, but yes with M15 the one condition can't be fulfilled. But thats no big deal either, was just for rough explanation what I wanted to do. Originally I had M5, so I had the condition as stated above. Likely I'll use SessionLastBar but didn't check it and the order should be done before session end, which I'm not sure in that case.

Of course currentdate is not fitting, but according the name LastCalcDateTime should do it but it doesn't because there's a distinction to be made which obviosly is not resulted in a distinction inside that member, rather in the Symbol_* members.


Return to “MultiCharts”