![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Aug 2012
Posts: 15
Thanks: 2
Thanked 0 times in 0 posts
|
Hey, I'm trying to put my own data into a DataSeries so that I can run Indicator methods on it.
So for example, I set the last 12 values of a DataSeries to be 100.0 and then pass it into an EMA. Here's the example code: Code:
for( int i=0; i<12; i++ )
{
testSeries.Set( i, 100.0 );
}
IDataSeries emaTest = EMA( testSeries, 5 );
for( int i=0; i<5; i++ )
{
Print( "VALUE: " + testSeries[i].ToString() );
Print( "EMA: " + emaTest[i].ToString() );
}
Code:
VALUE: 100 EMA: 109.097235188783 VALUE: 100 EMA: 113.645852783175 VALUE: 100 EMA: 120.468779174763 VALUE: 100 EMA: 130.703168762144 VALUE: 100 EMA: 146.054753143216 What am I doing wrong here? Is there a bug in the EMA method? |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
|
LiquidDrift, the EMA is an infinite filter and as such would always take all values into consideration (although the longer the series gets their weights get really small).
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Senior Member
|
Quote:
Code:
IDataSeries emaTest = EMA( testSeries, 5 ); |
|
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Aug 2012
Posts: 15
Thanks: 2
Thanked 0 times in 0 posts
|
OK, thanks, I figured out that indeed it is an infinite series and older entries in my "testSeries" were throwing off the values.
@koganam - What am I doing wrong there? I've been populating dataseries that way all over the place with no problems yet. I'm coming from a C++ background, so I may be doing improper assignment for sure. What is the correct way? Thanks! |
|
|
|
|
|
#5 | |
|
Senior Member
|
Quote:
The problem lies in your code. As you have written it, on each barUpdate, you are redefining and initializing an Interface, IDataSeries to an unknown state. To correctly do what you want, you should declare a class variable of type EMA (EMA is a class, hence object). You then assign/instantiate this named instance of an EMA (in either Initialize(), or preferably in OnStartUp(), or even reinitialize it every time in OnBarUpdate() like you have done. But it must be a named instance of the class, not a new declaration of the Interface. Here is what I mean: Code:
private EMA emaTest; Code:
protected override void Initialize()
{
this.testSeries = new DataSeries(this);
}
Code:
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
// Plot0.Set(Close[0]);
if (CurrentBar < 5) return;
for( int i=0; i<12; i++ )
{
testSeries.Set( i, 100.0 );
}
emaTest = EMA( testSeries, 5 ); //this statement can go in Initialize(), or OnStartUp(), which is the most efficient place for it.
for( int i=0; i<5; i++ )
{
Print( "VALUE: " + testSeries[i].ToString() );
Print( "EMA: " + emaTest[i].ToString() );
}
}
|
|
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Aug 2012
Posts: 15
Thanks: 2
Thanked 0 times in 0 posts
|
Ah, I see, I did not know what a C# Interface was, that was helpful thanks.
I'm still having problems with this however. It appears that DataSeries data lingers in the EMA, even if the DataSeries has been completely overwritten. For example: Code:
++barNum;
if( barNum == 40 || barNum == 100 )
{
Print( "-------------------------" );
int cnt = Math.Min( testSeries.Count, 256 );
for( int i=1; i<cnt; i++ )
{
testSeries.Set( i, 100.0 );
}
testSeries.Set( 0, 500.0 );
emaTest = EMA( testSeries, 5 );
for( int i=0; i<5; i++ )
{
Print( "VALUE: " + testSeries[i].ToString() );
Print( "EMA: " + emaTest[i].ToString() );
}
}
return;
Code:
------------------------- VALUE: 500 EMA: 233.333333333333 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 ------------------------- VALUE: 500 EMA: 322.222222222222 VALUE: 100 EMA: 233.333333333333 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 Code:
EMA( testSeries, 5 ).Dispose(); Code:
emaTest = EMA( testSeries, 5 ); |
|
|
|
|
|
#7 | |
|
Senior Member
|
Quote:
|
|
|
|
|
|
|
#8 | |
|
Senior Member
|
Quote:
That looks to me like floating point inaccuracies inherent in trying to use digital equipment to approximate floating point numbers.The clue that it is probably the effects of the optimization that holds the structure in the pipeline, instead of flushing it? The fact, that when you explicitly Dispose() of it, the problem goes away. ![]() It looks like you may have to specify the precision of your calculation results if you want consistency.
|
|
|
|
|
|
|
#9 | |
|
Junior Member
Join Date: Aug 2012
Posts: 15
Thanks: 2
Thanked 0 times in 0 posts
|
Quote:
The EMA / DataSeries appears to be doing some stuff under the hood that does not allow this kind of behavior. I believe I'm going to have to integrate my own EMA with a regular array to get what I need. Thanks so much for looking at it everyone. NT people - it would be good if you could look at this further, this may be a sign that there's a bug on your end somewhere. |
|
|
|
|
|
|
#10 | |
|
Senior Member
|
Quote:
This is what I got: Code:
------------------------- VALUE: 500 EMA: 233.333333333333 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 VALUE: 100 EMA: 100 ------------------------- VALUE: 500 EMA: 233.33333333696 VALUE: 100 EMA: 100.000000005439 VALUE: 100 EMA: 100.000000008159 VALUE: 100 EMA: 100.000000012239 VALUE: 100 EMA: 100.000000018358 |
|
|
|
|
|
|
#11 | |
|
Senior Member
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,204
Thanks: 182
Thanked 305 times in 263 posts
|
Quote:
I guess I need to catch up. My real world job has made me soft.
|
|
|
|
|
|
|
#12 | |
|
Junior Member
Join Date: Aug 2012
Posts: 15
Thanks: 2
Thanked 0 times in 0 posts
|
Quote:
My output is much further off, and again, the coincidence of the 233.333333333 value in both outputs leads me to conclude that even though I'm overwriting the DataSeries, old data continues to live somewhere in NT and continues to be processed by the EMA. I'm bummed that you were not able to reproduce my results, maybe you can if you try running on different bars further apart. Since I believe it's a memory/data issue, no 2 computers are going to get the same results every time however. The reason I created the code snippet was to attempt to narrow down the same issue that I'm seeing in a more complex strategy, and to hopefully either figure out if I'm doing something wrong, or shine some light on the problem. I now have 2 (3 if I count yours) cases where this is happening, and that's more than enough for me to not trust data that I'm filling into a DataSeries and processing with an Indicator. When I use third party code to do the same thing using regular arrays, I'm not seeing any issues. Just want to say once again, I'm very thankful for your time koganam for taking a look at this and your feedback! |
|
|
|
|
|
|
#13 | |||
|
Senior Member
|
Quote:
Quote:
I am actually more surprised that there are FP errors in the first place. I would expect each run of the code to produce the exact same results; not perfectly correct results in the first run, then anything else in the next. After all, the values are being shown to be exactly 100 in both cases, so there should be no difference. The only question now is: "Have we found an error in the way C# handles objects, or is the error in the way that the CPU handles caching and pipelining. To me, our mutual results point to a processing error somewhere. ![]() Quote:
Last edited by koganam; 08-29-2012 at 12:37 AM.
Reason: Corrected spelling
|
|||
|
|
|
|
|
#14 |
|
Senior Member
|
|
|
|
|
|
|
#15 | |
|
Junior Member
Join Date: Aug 2012
Posts: 15
Thanks: 2
Thanked 0 times in 0 posts
|
Quote:
If what you're saying is true, then the EMA doesn't take into account new data entered into the DataSeries, which would make sense given the output. |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| DataSeries Market Analyzer output | iangohye | Market Analyzer | 6 | 03-28-2012 07:04 AM |
| Indicator BarsPeriods perhaps wonky | saltminer | Indicator Development | 5 | 02-21-2012 03:46 AM |
| plotting an EMA from added dataseries | jfw215 | General Programming | 9 | 07-20-2010 07:28 AM |
| Custom DataSeries EMA | eleven | Indicator Development | 4 | 07-14-2010 06:20 AM |
| Need Help- Same Output with different DataSeries | Trendseek | Indicator Development | 9 | 04-30-2010 06:27 AM |