NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 05-26-2009, 08:12 PM   #1
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default Help - Oscillator Variance Calculation

Hi all,

I am looking for help with the following.

I have an oscillator that has a zero line and can go from 10.000000 to -10.000000.

What I want to do is calculate the amount of the move between bars, or rather how much did the oscillator oscillate.

Say for example I was using Oscillator and I wanted to make a decision based on how much it moved between the value as of the current bar for the osc, to the value of the osc 3 bars ago?

I am basically doing something like:
(this works fine provided my osc values are both above zero)
if (Oscillator(PoFast, PoSlow, PoSmooth)[0] > Oscillator(PoFast, PoSlow, PoSmooth)[3]
&& Oscillator(PoFast, PoSlow, PoSmooth)[
0] - Oscillator(PoFast, PoSlow, PoSmooth)[3] > .1

...then do something...

Now I am pretty sure that this is Freshman math, but I was busy doing more important things, lol. Can someone give me the structure for finding the difference between the values of the osc for given bars?

Will pay in beer.
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 06:51 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,555
Thanks: 261
Thanked 1,013 times in 994 posts
Default

r2kTrader, not sure I follow - if you have the Oscillator as a dataseries you can easily calculate the difference from current bar to x bars back. Or are you looking for some kind of 'pivot detection' algorithm placed on the oscillator? Then I would check into the code of the Swing indicator that would do this on price.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-27-2009, 07:10 AM   #3
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

I thought this through using my scientific calculator, lol, and this is what I came up with. My only kickback was when dealing with negative numbers. I know subtraction is obviously the way to find the difference, but it was the result that was skewing my results.

So I decided that converting the negative number to an absolute would do the trick (I think, looking for some eyes/help).

My logic is as follows, I do the opposite for short side:

1. is osc value from current bar greater than osc value from 3 bars ago, if true, then #2

2. subtract value of osc from 3 bars ago from osc value value of current bar. (I added the Math.abs to convert the result to a whole number so that I can simply base a decision based on the amount of the difference. I already know if its higher, so the result should be the difference)

3. if variance is great than x, then do something.



if (Oscillator(PoFast, PoSlow, PoSmooth)[0] > Oscillator(PoFast, PoSlow, PoSmooth)[3]
&& Math.Abs(Oscillator(PoFast, PoSlow, PoSmooth)[
0] - Oscillator(PoFast, PoSlow, PoSmooth)[3]) > 0
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 07:48 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,555
Thanks: 261
Thanked 1,013 times in 994 posts
Default

Just an idea, maybe simpler: try using the absolute value of the x bar momentum of your oscillator for your decisions.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-27-2009, 07:57 AM   #5
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
Just an idea, maybe simpler: try using the absolute value of the x bar momentum of your oscillator for your decisions.
Bertrand,

Thanks for the reply.

Couldn't that skew my results if say my values were as follows?

1 bar ago (1BA)= -.2
Current bar (CB) = .3

Wouldn't I get a value of 1 if I did something like:
Abs(CB) - Abs(1BA) = .3 - .2 = 1

Whereas:
Abs(CB- 1BA) = .3 - -.2 = 5

I could REALLY use a definitive answer on this and I am increasing the answer bounty to 2 beers, microbrewed!

I want to know the different between the moves of old bars to new bars on my osc and then make a decision based on the deviation or rather the amount of the move.
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 08:24 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,555
Thanks: 261
Thanked 1,013 times in 994 posts
Default

I guess you would need to chart the alternatives and then check the needed requirements visually, AbsValue would just affect the sign, not the rounding.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-27-2009, 08:55 AM   #7
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
I guess you would need to chart the alternatives and then check the needed requirements visually, AbsValue would just affect the sign, not the rounding.
Right, it would take say -5 and make it 5, correct? But isn't that what I want? I want to simply know the diff between the osc value of current bar and 3 bars ago. Just the deviation. To do my compare, it's best to work with a whole value, verses say -5.

So based on my prior post, is my code/logic correct?

Also, sidenote, this is an NT related question. As it stands, I am running this calc on every bar update, but I only want to run this calc on bar close. Is there a code snippet to do this? Bear in mind, I want my strategy NOT to calcOnBarClose, but I don't want to reiterate this snippet every update. Would I setup a bool and then nest the rest of the code inside that test?


Thanks
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 09:04 AM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,555
Thanks: 261
Thanked 1,013 times in 994 posts
Default

r2kTrader, yes I believe this would do it for you - you can use FirstTickOfBar to do this only on the close then, just keep in mind to reference one bar back further to make up for calling it on the open of the next bar...

Code:
 
if (FirstTickOfBar)
{ 
do your on bar close calculations
}
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-27-2009, 09:05 AM   #9
ctrlbrk
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 682
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by r2kTrader View Post
Also, sidenote, this is an NT related question. As it stands, I am running this calc on every bar update, but I only want to run this calc on bar close. Is there a code snippet to do this? Bear in mind, I want my strategy NOT to calcOnBarClose, but I don't want to reiterate this snippet every update. Would I setup a bool and then nest the rest of the code inside that test?


Thanks
if (FirstTickOfBar)
dosomething;

Would allow rest of strategy to update each tick, and 'dosomething' only on bar close.

Mike
ctrlbrk is offline  
Reply With Quote
Old 05-27-2009, 09:06 AM   #10
ctrlbrk
Senior Member
 
Join Date: Oct 2008
Location: Dallas, TX
Posts: 682
Thanks: 0
Thanked 2 times in 2 posts
Default

doh! Bertrand is speedy fast

Mike
ctrlbrk is offline  
Reply With Quote
Old 05-27-2009, 09:44 AM   #11
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Pete's Wicked Ale for the both of you!

Thank you very much guys.

So all things being equal Betrand, my logic of using Abs as per my description is correct? I will be able to calculate the difference between the value of osc from one bar to the other bar and by apply Abs on the result, I will avoid skewing the value of the difference and only convert it to a whole number if it is a negative value?
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 09:59 AM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

r2kTrader,

I am not sure where you got the idea that Math.Abs converts to whole numbers. Math.Abs just does absolute values. It doesn't change it to a whole number in the process.

Math.Abs(-0.2) = 0.2 for example.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-27-2009, 10:20 AM   #13
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
r2kTrader,

I am not sure where you got the idea that Math.Abs converts to whole numbers. Math.Abs just does absolute values. It doesn't change it to a whole number in the process.

Math.Abs(-0.2) = 0.2 for example.
symantics, I'm not a programmer.

Let me rephrase. I meant positive, not whole. :-(
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 10:24 AM   #14
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Then that is correct. You will get appropriate positive values for comparison.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-27-2009, 10:49 AM   #15
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Josh,

Will this work?

private double[] values = Oscillator(PoFast, PoSlow, PoSmooth);
private double currentBar = values[0];
private double oneBarAgo = values[3];

if (currentBar > oneBarAgo && Math.Abs(currentBar - oneBarAgo) > .25)
{
// do something
}
r2kTrader is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
3/10-Oscillator gerryk Indicator Development 10 06-20-2012 11:35 AM
Elliptic Oscillator. Drakmyre Indicator Development 12 01-06-2009 10:21 AM
Forcast oscillator pgabriel Indicator Development 1 12-21-2008 02:37 PM
Request: Bill Williams Awesome Oscillator & Accelerator Oscillator jeremymgp Indicator Development 26 11-04-2008 08:41 PM
Volume Oscillator ohowie Historical NinjaTrader 6.5 Beta Threads 5 03-07-2008 12:15 AM


All times are GMT -6. The time now is 06:21 PM.