Welcome to NexusFi: the best trading community on the planet, with over 150,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 -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Centered Moving Average not refreshed on new data?
I have been trying to build an indicator to smooth a centered Moving Average, but somehow the indicator never plots the "refreshed" values. Although I have searched through the forum and read all similar topics (plus all 1027 pages of the NT version 7 Help Guide), I am still unable to fix my indicator. Please give me some hints on what could go wrong. Thanks a lot in advance!
I used the Output window and the chart "Show Data Box" to debug. The array CalValue holds the indicator values, but these values do not match those in the "Show Data Box". Is there something wrong with the Set() command? Or I should shift the bar back or something? I even removed the script from the indicator folder, used a new name for the script, and even re-installed NT 7 but the same problem still persisted.
Please see the script and the Output window below:
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Middle"));
mySMA = new DataSeries(this);
CalValue = new double[50];
Sum = new double[50]; // NT7 limits the number of available bars for calculations at 256
Sumw = new double[50];
Overlay = true;
}
for (int i = 20; i >= 0; i--)
{
Sum[i] = 9 * mySMA[i];
Sumw[i] = 9;
int k = 8;
for (int j = 1; j <= 8; j++)
{
Sum[i] += k * mySMA[i+j];
Sumw[i] += k;
if (smooth==true & j <= i)
{
Sum[i] += k * mySMA[i-j];
Sumw[i] += k;
}
k--;
}
CalValue[i] = Sum[i] / Sumw[i];
}
/// Plot the array CalValue
for (int p = 0; p <= 20-1; p++)
{
Print("CalValue[" + p.ToString() + "] = " + CalValue[p].ToString());
}
Values[0].Set(CalValue[0]);
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Middle
{
get { return Values[0]; }
}
[Description("")]
[GridCategory("Parameters")]
public bool Smooth
{
get { return smooth; }
set { smooth = value; }
}
#endregion
}
However, each time the chart plots the following values. It looks like the ninjascript somehow ignores the section "if (smooth==true & j <= i)". I am puzzled. What's wrong?
Thanks a lot for the hint, Fat Tails! I fixed the part that you mentioned.
By the way, I think I now understand the issue. It's the assignment of values and thus plotting the correct index!!
I did an experiment and found the following things:
1) On a chart, I limited the number of candlebars to 43 bars. Because the calculations will not involve the "oldest" bar (the 42th bar) and the "newest" bar (the 0th bar), it is expected that there will be 41 groups of data.
2) Next, I checked the Output window and counted the number of group. Sure enough, there are exactly 41 groups of data. This means that the indicator will show on the chart from the 41st bar to the 1st bar. (I don't understand why the indicator starts on the 1st bar instead of the 0th bar. Any idea?)
3) If I write Values[0].Set(CalValue[0]), this means that only the 0th element of each group will be plotted. Sure enough, the "Data Box" verifies that each of the bar (the 41st to the 1st) shows the indicator value of the 0th element.
Here is my need for help --- I only want to plot the values of the "most recent" group. For example, I have the following results from the Output window:
I only want the LAST group of data and have them plotted as follows:
CalValue[0] = 89.10125 on the current bar (bar 0)
CalValue[1] = 88.914375 on the previous bar (bar 1)
CalValue[2] = 88.7094871794872 on the two-day-ago bar (bar 2)
CalValue[3] = 88.5451282051282 on the three-day-ago bar (bar 3)
CalValue[4] = 88.3376923076923 on the four-day-ago bar (bar 4)
What codes should I write, or how do I structure my array and indexes in order to get my plot correct?
Ich lebe in Pankow! 20 minuten von Berlin!! (Sorry I cannot reply via PM because I am not yet allowed to use that feature. So, I will have to omit most of my PM response here.) Anyway, cheers for the great Oktoberfest!!!
I am still trying different ways to pick only the most recent array and have its values plotted. Sighhhhhhh