NexusFi: Find Your Edge


Home Menu

 





Need help with XAverage and so on...


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one ABCTG with 2 posts (1 thanks)
    2. looks_two ReximusMaximus with 1 posts (1 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 OperaMask with 1 posts (0 thanks)
    1. trending_up 3,932 views
    2. thumb_up 2 thanks given
    3. group 3 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
OperaMask
Davis, CA/USA
 
Posts: 1 since Apr 2014
Thanks Given: 0
Thanks Received: 0

Hi all,

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!


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Topstep Acquires The Futures Desk -- Prop Firm Consolida …
Funded Trading Evaluation Firms
Memorandum Watch: How the 60-Day MOU Framework Makes May …
Prediction Markets & Event Contracts
Thursday May 28: GDP + Core PCE + Jobless Claims All at …
Traders Hideout
Iran Update May 8: Still Reviewing MOU, Demands Reparati …
Traders Hideout
Netherlands & Germany Surge as World Cup Field Narro …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Big Mike in Ecuador
196 thanks
Sober Journey With S&P
27 thanks
30 Sessions
20 thanks
Volume Indicators
8 thanks
BERN ALGOS algo trading journal
8 thanks
  #3 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,448 since Apr 2013
Thanks Given: 494
Thanks Received: 1,642


OperaMask,

I am not exactly sure what is happening on your end, but this code
 
Code
Plot1(XAverage(Close, 3));
Plot2(XAverage(Close, 3)[1]);
produces two EMAs where the second one is returning the previous bar's value from the first XAverage.

What do you mean with " XAverage(Close, 3) is not what the chart shows EMA(3)."?

Regards,
ABCTG


Follow me on X Reply With Quote
  #4 (permalink)
 ReximusMaximus 
SAINT LOUIS
 
Experience: Advanced
Posts: 40 since Aug 2019
Thanks Given: 7
Thanks Received: 23


ABCTG View Post
OperaMask,

I am not exactly sure what is happening on your end, but this code
 
Code
Plot1(XAverage(Close, 3));
Plot2(XAverage(Close, 3)[1]);
produces two EMAs where the second one is returning the previous bar's value from the first XAverage.

What do you mean with " XAverage(Close, 3) is not what the chart shows EMA(3)."?

Regards,
ABCTG

I am having an issue that sounds like it's related to OP's:

Using this code to produce a 21 EMA:

 
Code
ema = XAverage(close,21);

I print the variable "ema" on a daily chart, and the value printed doesn't match the value shown on the chart.

I noticed this when trades were being placed that looked wrong, and started troubleshooting with the print function.

Is this an issue with the daily bars, or is it some sort of known issue, or am I just missing something very obvious?


Reply With Quote
Thanked by:
  #5 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,448 since Apr 2013
Thanks Given: 494
Thanks Received: 1,642

ReximusMaximus,

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:

Starting Point Dependence
The surprising MACD indicator
Understanding MaxBarsBack

Regards,

ABCTG


ReximusMaximus View Post
I am having an issue that sounds like it's related to OP's:

Using this code to produce a 21 EMA:

 
Code
ema = XAverage(close,21);

I print the variable "ema" on a daily chart, and the value printed doesn't match the value shown on the chart.

I noticed this when trades were being placed that looked wrong, and started troubleshooting with the print function.

Is this an issue with the daily bars, or is it some sort of known issue, or am I just missing something very obvious?


Follow me on X Reply With Quote
  #6 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


ABCTG View Post
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.

@ABCTG,

Nails it here. This is one of those TradeStation EasyLanguage 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:

 
Code
XAverage = PreviousXAverage + (2/(Length+1)) * (Price - PreviousXAverage)
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
 
Code
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."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

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.
Reply With Quote




Last Updated on March 5, 2026


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts