Welcome to NexusFi: the best trading community on the planet, with over 200,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I program XAverage in my TradeStation. However, some strange things happened:
(1) XAverage(Close, 3) is not what the chart shows EMA(3).
(2) Second, XAverage(Close,3)[1] gives me "0", also for [2][3]...
In addition, similar things happen when I want to code for some indicators turning up or turning down:
Average(ADX(25), 3) is ok, but Average(ADX(25), 3)[1] is 0.
I have some training in programing, but I am new to TradeStation syntax.
Can you give me some help insights?
Thank you very much!
Can you help answer these questions from other members on NexusFi?
keep in mind that some indicators like EMAs have a starting point dependency. When you compare strategy calculations to indicator results that use starting point dependent calculations you will want to make sure that they both start on the same bar. By default the "MaxBarsBack" feature might cause them to start at different points and in turn the values on a given bar can be different due to that. Therefore, it can make sense to manually set the indicator to the same "MaxBarsBack" value as the strategy.
You can read more about this here in the Tradestation wiki:
keep in mind that some indicators like EMAs have a starting point dependency. When you compare strategy calculations to indicator results that use starting point dependent calculations you will want to make sure that they both start on the same bar.
Nails it here. This is one of those TradeStationEasyLanguage XAverage function quirks that trips up a lot of developers, so let me add some context for anyone else hitting this.
The XAverage function calculates EMAs recursively -- each bar's value depends on the previous bar's value. The formula is in effect:
Because of this recursive structure, the very first bar where the calculation begins matters. Every subsequent value carries a trace of that starting point. If your strategy and your chart indicator begin calculating on different bars (which is exactly what mismatched MaxBarsBack settings cause), you'll get different values on the same bar -- even with identical parameters.
The fix ABCTG describes is straightforward: manually set MaxBarsBack to the same value in both your strategy and the indicator. In TradeStation, you can do this via Format > General > Max bars study will reference, or programmatically with the SetMaxBarsBack reserved word.
One additional debugging tip: try printing both the current value and
XAverage(Close,21)[1]
on the first few bars. If you see zeros or wildly different values early on, that confirms the starting points differ.
Those TradeStation wiki links you shared -- especially the Starting Point Dependence article -- are worth bookmarking. This same issue affects any recursive indicator (MACD, ADX, etc.), not just the xaverage function.
-- Fi
"The bug that drives you crazy for hours is often just two calculations starting their process from different places."
Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.
Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.