NexusFi: Find Your Edge


Home Menu

 





Centered Moving Average not refreshed on new data?


Discussion in NinjaTrader

Updated
    1. trending_up 2,165 views
    2. thumb_up 5 thanks given
    3. group 3 followers
    1. forum 6 posts
    2. attach_file 0 attachments




 
Search this Thread

Centered Moving Average not refreshed on new data?

  #1 (permalink)
TraderWow
Berlin, Germany
 
Posts: 5 since May 2012
Thanks Given: 1
Thanks Received: 2

Hello ninjas,

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:

****************************************************************
public class TCMA5 : Indicator
{
#region Variables
private bool smooth = true; // Default setting is true
private DataSeries mySMA;
private double[] CalValue;
private double[] Sum;
private double[] Sumw;
#endregion

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;
}

protected override void OnBarUpdate()
{
if (CurrentBar <= 5) return;
Print(" ");

mySMA[0] = SMA(Input, 0)[0];

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
}

*****************************************************************************



These are the values from the Output Window. The chart is a daily chart of the stock XOM:

*****************************************
CalValue[0] = 88.6862222222222
CalValue[1] = 88.5771698113207
CalValue[2] = 88.4696666666667
CalValue[3] = 88.3572727272727
CalValue[4] = 88.2442253521127
CalValue[5] = 88.1388
CalValue[6] = 88.0338461538461
CalValue[7] = 87.925625
CalValue[8] = 87.8082716049383
CalValue[9] = 87.7025925925926
CalValue[10] = 87.6198765432098
CalValue[11] = 87.5351851851851
CalValue[12] = 87.4653086419753
CalValue[13] = 87.4077777777778
CalValue[14] = 87.3669135802469
CalValue[15] = 87.3176543209876
CalValue[16] = 87.307037037037
CalValue[17] = 87.3366666666666
CalValue[18] = 87.3922222222222
CalValue[19] = 87.4985185185185

***************************************************


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?

**************************************
CalValue[0] = 88.6862222222222
CalValue[1] = 88.3988888888889
CalValue[2] = 88.2133333333333
CalValue[3] = 88.048
CalValue[4] = 87.9091111111111
CalValue[5] = 87.8384444444444
CalValue[6] = 87.5957777777777
CalValue[7] = 87.5348888888889
CalValue[8] = 87.4088888888889
CalValue[9] = 87.4468888888888
CalValue[10] = 87.4566666666666
CalValue[11] = 87.3546666666666
CalValue[12] = 87.3675555555555
CalValue[13] = 87.3731111111111
CalValue[14] = 87.3708888888888
CalValue[15] = 87.0395555555555
CalValue[16] = 87.1595555555555
CalValue[17] = 87.2768888888888
CalValue[18] = 87.3608888888888
CalValue[19] = 87.6359999999999
************************************************

Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
Cheap historycal L1 data for stocks
Stocks and ETFs
ZombieSqueeze
Platforms and Indicators
What broker to use for trading palladium futures
Commodities
MC PL editor upgrade
MultiCharts
 
  #2 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103

 
Code
if (CurrentBar <= 5) return;
Print(" ");

mySMA[0] = SMA(Input, 0)[0];

for (int i = 20; i >= 0; i--)
{
Sum[i] = 9 * mySMA[i];
....


You forgot to set a value for the DataSeries mySMA for the first 6 bars.

Also for CurrentBar = 6 you are accessing mySMA[20]. Obviously this cannot work, as mySMA[20] is not defined for CurrentBar = 6.

Reply With Quote
Thanked by:
  #3 (permalink)
TraderWow
Berlin, Germany
 
Posts: 5 since May 2012
Thanks Given: 1
Thanks Received: 2


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:

==========================================
:
:
:
New: CalValue[0] = 88.37875
New: CalValue[1] = 88.45875
New: CalValue[2] = 88.3376923076923
New: CalValue[3] = 88.2317948717948
New: CalValue[4] = 88.0007692307692

New: CalValue[0] = 88.6925
New: CalValue[1] = 88.5190625
New: CalValue[2] = 88.5451282051282
New: CalValue[3] = 88.3376923076923
New: CalValue[4] = 88.2317948717948

New: CalValue[0] = 89.10125
New: CalValue[1] = 88.914375
New: CalValue[2] = 88.7094871794872
New: CalValue[3] = 88.5451282051282
New: CalValue[4] = 88.3376923076923
==============================================


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?

Thanks a lot for your help!

Reply With Quote
  #4 (permalink)
TraderWow
Berlin, Germany
 
Posts: 5 since May 2012
Thanks Given: 1
Thanks Received: 2

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

Reply With Quote
  #5 (permalink)
TraderWow
Berlin, Germany
 
Posts: 5 since May 2012
Thanks Given: 1
Thanks Received: 2

Problem solved! The technical team from NinjaTrader fixed the codes for me. The fix works beautifully!

Reply With Quote
  #6 (permalink)
 Taggart 
Los Angeles, CA
 
Experience: Advanced
Platform: NinjaTrader
Broker: AMP
Trading: ES
Posts: 30 since Oct 2009
Thanks Given: 184
Thanks Received: 22


TraderWow View Post
Problem solved! The technical team from NinjaTrader fixed the codes for me. The fix works beautifully!

Would it be possible to share the fix with us as a zip folder or .cs file?

Thanks in advance.

Reply With Quote
  #7 (permalink)
TraderWow
Berlin, Germany
 
Posts: 5 since May 2012
Thanks Given: 1
Thanks Received: 2

No problem! The following is the fix on the last part of the script. Basically, we need to set the condition using if (p == 0).


/// Plot the array CalValue
for (int p = 0; p <= 20-1; p++)
{
Print("CalValue[" + p.ToString() + "] = " + CalValue[p].ToString());
if (p == 0)
Values[0].Set(CalValue[p])
else
Values[0].Set(p, CalValue[p]);
}

Reply With Quote
Thanked by:




Last Updated on October 6, 2013


© 2024 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 - Privacy Policy - Downloads - Top
no new posts