Strange behaviour of Indicators when called from Strategies

How to create strategies and indicators
Message
Author
User avatar
Tantalus
Posts: 302
Joined: Fri Mar 23, 2007 3:51 pm
Contact:

Strange behaviour of Indicators when called from Strategies

#1 Postby Tantalus » Tue Jan 03, 2012 2:44 pm

After spending many, many confusing and frustrating hours debugging, I have found out something very bizarre about the way that FT Indicators are handled when they are called from FT Strategies. I'm sharing this in the hope that others will not have to work so hard to get their systems working.

In History mode, an indicator is fed an entire bar of price data at a time, and calculates based on that data just as one might expect. However, in Testing mode, indicators are handled differently.

Specifically, in Testing mode the price data is fed to the Strategy being run on a tick-by-tick basis. If the Strategy calls an indicator from its code (using the CreateIndicator() and GetIndicatorValue() functions) the indicator will only be passed the price data that the Strategy has at the time of the most recent tick. It will receive an Open, High, Low and Close value, but they will be of an incomplete bar, such as the Strategy is seeing.

The Indicator also receives an Index value which tells it which bar it's calculating, but it gets the Index from the Strategy, which is different from the Index value it would normally get if it were running in History mode. In a Strategy, the Index of the current bar is always 0, and the index of the previous bar is always 1.

Here's the weird thing: When in Indicator is called by a Strategy, it receives one extra tick - that is, if the Strategy is processing a bar made up of four ticks, the indicator will actually be run five times -- it receives the first tick twice.

What makes it even weirder is that the first time it receives the first tick, it receives the wrong Index value. Like the Strategy, the current Index for the Indicator should always be 0, but for some reason the first (false) tick the Indicator receives comes with an Index value of 1.

Here's what it looks like:

Code: Select all

Tick #1        Index = 1     <--  This is the extra tick
Tick #1        Index = 0
Tick #2        Index = 0
Tick #3        Index = 0
Tick #4        Index = 0


Now in most cases this wouldn't cause any problems. If you're using M1 data and/or your Strategy checks the Indicator values for previous conditions such as MA crossovers, this little anomaly shouldn't cause you any grief. I ran into it because I programmed the Indicator to perform a certain calculation only on the first tick it receives and report that value to the Strategy as Buffer[Index], expecting to see the value in the current Index which is 0. Because the first tick that the Indicator sees is false (a duplicate of tick #1 that is mysteriously labeled as Index 1) my value was always being written to the wrong place in the buffer, over-writing the value that was always there.

My solution was to program the indicator to ignore the first tick (the false one) and report the value on the second tick (the first true tick). Weird, but it worked.

So, maybe Mike or Someone at FT can explain this weird behaviour (or fix the program to stop it) but for now at least it's known publicly, and it can be successfully programmed around.

Cheers-
Tantalus Research - Developing 21st Century Trading Systems.

dackjaniels
Posts: 151
Joined: Tue Feb 24, 2009 1:03 pm

#2 Postby dackjaniels » Mon Jan 09, 2012 2:04 pm

I've never noticed this before. Thanks for describing it Tantalus, I will keep it in mind.

Steve

User avatar
Tantalus
Posts: 302
Joined: Fri Mar 23, 2007 3:51 pm
Contact:

#3 Postby Tantalus » Mon Jan 09, 2012 4:22 pm

I'm doing some unusual, out-of-the-box type of stuff, so I expect very few people ever run into this (or that it is ever a problem for them). 99.9% of all strategies/indicators will likely never be affected by this.

My real point in posting this was to see if Mike and the guys could shed a little light on this unusual way of doing things. I'm quite curious if they really intended that the first tick of a bar be processed twice (once with an as-yet-unupdated Index value). I can't see why it would be intentional... it might have been an unfortunate side-effect that they decided not to worry about given that almost nobody would ever find it to be a problem.

The most interesting thing (and often the trickiest) about programming for FT is understanding that indicators run differently in the History mode and in the Testing mode. More than once I have developed an indicator that worked fine in one mode but goofed up in the other. Without a clear and detailed understanding of the differences in the two modes, it's treacherous to program indys that are more adventurous than your standard time series filters (MAs, etc.)
Tantalus Research - Developing 21st Century Trading Systems.

dackjaniels
Posts: 151
Joined: Tue Feb 24, 2009 1:03 pm

#4 Postby dackjaniels » Mon Jan 09, 2012 5:45 pm

Tantalus wrote:The most interesting thing (and often the trickiest) about programming for FT is understanding that indicators run differently in the History mode and in the Testing mode. More than once I have developed an indicator that worked fine in one mode but goofed up in the other.


Very true, have done this myself more than once.

Stonev
Posts: 204
Joined: Thu Jan 28, 2010 2:20 pm

#5 Postby Stonev » Tue Jan 10, 2012 6:12 am

Hi guys!
Yes, often meet this "strange" in indicators,
moreover, on every first tick of every new bar in the testing mode.
do not tell anyone, I think it's ... Mike's backdoor :))))

fx_pete
Posts: 17
Joined: Mon Jan 30, 2012 2:31 pm

any feedback on this yet?

#6 Postby fx_pete » Fri Feb 03, 2012 5:24 pm

I am very curious about stuff like this since I will eventually be doing out of the box stuff too ....

peter


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 18 guests