Login Page - Create Account

Support Board


Date/Time: Mon, 25 Nov 2024 18:47:00 +0000



Exponential Moving Averages Calculation

View Count: 2160

[2014-02-23 03:23:46]
User47706 - Posts: 22
Hello Support Engineers,

I have a C++ program which is organically replicating your EMA values to within the ten millionth exponent(decimal) across multiple time frames (2 seconds through 60 minutes) with remarkable accuracy and frequency. However, on some occasions my programmatic calculations show variances in the ten thousands. Perplexed, I attacked these variances assuming that my program was intermittently failing to calculate correctly. What my analysis has brought me to is the realization that seemingly all of my self-defined unacceptable variances occur immediately following a bar of data that closes at the same price as the bar of data preceding it (my base for the EMA calculation is the Close Price). I am using your price data files as my sole data source.

I do NOT believe anything is wrong with your EMA calculations. Many years of experience with your platform has my confidence in your service/talents at an all time high.

So, can you tell me what you do special or different in your calculations of EMA when a base input value is encountered to be the same as what has presented before it? Are you decaying that one base (Close Price) value, possibly?


Date Time Of Last Edit: 2014-02-23 03:27:31
[2014-02-23 04:35:01]
Sierra Chart Engineering - Posts: 104368
This is the actual code:

SCFloatArrayRef ExponentialMovingAverage_S(SCFloatArrayRef In, SCFloatArrayRef Out, int Index, int Length)
{
  if (Index >= In.GetArraySize())
    return Out;

  if(Index < 1 || Length < 1)
    return Out;

  if (Index < Length -1)
    Length = Index+1;

  
  float Multiplier1 = 2.0f / (Length + 1);
  float Multiplier2 = 1.0f - Multiplier1;
  float PreviousMovingAverageValue = Out[Index - 1 ];
  //Check for a previous moving average value of 0 so we can properly initialize the previous value, and also check for out of range values.
  if (PreviousMovingAverageValue == 0.0f || !((PreviousMovingAverageValue > -FLT_MAX) && (PreviousMovingAverageValue < FLT_MAX)))
    Out[Index - 1] = In[Index-1];
  
  Out[Index] = (Multiplier1 * In[Index]) + (Multiplier2 * Out[Index - 1]);
  
  
  return Out;
}

Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2014-02-23 18:31:54]
User47706 - Posts: 22
Your code sample shows me what I was looking for:

. . . PreviousMovingAverageValue > -FLT_MAX . . . < FLT_MAX . . .


This is a range filter which I find conceptually challenging when it is applied to exponential moving average data. But, so be it.

Can you show me the code which populates FLT_MAX ?

Thanks,
Craig
[2014-02-23 19:49:59]
vegasfoster - Posts: 444
Maximum floating point number http://www.cplusplus.com/reference/cfloat/
[2014-02-23 20:15:29]
Sierra Chart Engineering - Posts: 104368
With floating-point calculations sometimes there could be results which are not valid numbers. The main case would be dividing by 0. This code is designed to catch numbers which are not actually valid floating-point numbers and ignore them:
!((PreviousMovingAverageValue > -FLT_MAX) && (PreviousMovingAverageValue < FLT_MAX))
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account