NinjaTrader Support Forum  
X

Attention!

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


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-27-2009, 02:15 PM   #1
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default Using Structs with Indicator within Strategy

Hi all,

I am trying to implement the following from within the variables section and it is throwing an error.

private double[] test = SMA(fast)[0];
private double testCurrentBar = test[1];

Then in my code I want to do something like:

if (testCurrentBar > 0)

---Note---
I am not really interested in SMA, rather any indicator and getting the value of the indicator for the current bar, last bar, bars back, etc.

I'm just tired and it's not clicking, if anyone could lend a hand it would be appreciated.


Last edited by r2kTrader; 05-27-2009 at 02:34 PM.
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 02:39 PM   #2
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 really don't understand what you are trying to do. If you want the SMA's value of the current bar just go SMA(Fast)[0]. If you want the previous bar just go SMA(Fast)[1]. Not following your reason for needing double[].
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-27-2009, 02:57 PM   #3
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 really don't understand what you are trying to do. If you want the SMA's value of the current bar just go SMA(Fast)[0]. If you want the previous bar just go SMA(Fast)[1]. Not following your reason for needing double[].
I'm trying to setup a Array that can hold the values so I can then just access the variable. I don't want to have to type in SMA(Fast)[0] for every instance I compare, rather I just want to say currentBar or whatever.

Would I need a DataSeries for this?

Easier to do

if(testCurrentBar - testLastBar > 0)

{
//do something
}

Than

if(SMA(Fast)[0] - SMA(Fast)[1] > 0)

so this is what I am trying to do. I want my code cleaner and more readable.

Thanks
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 03:09 PM   #4
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

Doing such an operation will be inefficient for the code. If you only want to compare current and last just create two double variables to store those values. Otherwise you would need to make a whole DataSeries which is essentially doubling the resources of just calling SMA directly.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-27-2009, 04:17 PM   #5
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Red face

Josh,

That is what I am asking how to do

Can you spit me a snippet?
r2kTrader is offline  
Reply With Quote
Old 05-27-2009, 06:32 PM   #6
junkone
Senior Member
 
Join Date: Sep 2008
Posts: 356
Thanks: 1
Thanked 1 time in 1 post
Default

i think what Josh is suggesting is
currentVal=SMA(Fast)[0];
prevValue=SMA(Fast)[1];

bool result =currentVal>prevValue?true:false;

.
not wanting to hijack this thread but to extend it.
would calling the dataseries everytime make it recalculate the data
for eg, SMA(Fast)[0] calculate the values for alteast <fast> periods and get the current value

SMA(Fast)[1] calculate the values for alteast <fast> periods and get the prev value
junkone is offline  
Reply With Quote
Old 05-28-2009, 06:16 AM   #7
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Junkone,

Thanks for jumping in.

This is what I did:
double poCurrentBar = Oscillator(PoFast, PoSlow, PoSmooth)[1]; //not zero because we calc on bar close for this osc - FirstTickOfBar
double poOneBarsAgo = Oscillator(PoFast, PoSlow, PoSmooth)[2];
double poTwoBarsAgo= Oscillator(PoFast, PoSlow, PoSmooth)[3];
double poThreeBarsAgo= Oscillator(PoFast, PoSlow, PoSmooth)[4];

But is this efficient? Wouldn't it be better to call it once than 4 times?

How come it won't accept

double[] poCurrentBar = Oscillator(PoFast, PoSlow, PoSmooth)[1];

It says can't convert type double to type double[]

I'm a student of C#, so any help would be appreciated.

PS,

I like the idea of the bool, would be more efficient.
Last edited by r2kTrader; 05-28-2009 at 06:19 AM.
r2kTrader is offline  
Reply With Quote
Old 05-28-2009, 07:16 AM   #8
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default bump

Josh,

Could you look at my code and let me know if I am calling the Oscillator more than is necessary? I wanted to call it once per bar update and then just load the array and reference the array, but it won't let me do a double[] from a double.
r2kTrader is offline  
Reply With Quote
Old 05-28-2009, 07:17 AM   #9
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

junkone,

When called from within the same script there already is an instance of the indicator in memory so it doesn't recalculate twice.


r2kTrader,
double[] is an array while Oscillator(...)[1] is a double. That is why you get the cannot convert message.

Try this untested code. Unfortunately I cannot provide more assistance than this. You can try searching google for examples.
double[] someVar = new double[4];
someVar[1] = Oscillator(..)[1];

Calling Oscillator() four times in your code is no more or less efficient than calling it once since it is already in memory.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-28-2009, 07:39 AM   #10
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
junkone,

When called from within the same script there already is an instance of the indicator in memory so it doesn't recalculate twice.


r2kTrader,
double[] is an array while Oscillator(...)[1] is a double. That is why you get the cannot convert message.

Try this untested code. Unfortunately I cannot provide more assistance than this. You can try searching google for examples.
double[] someVar = new double[4];
someVar[1] = Oscillator(..)[1];

Calling Oscillator() four times in your code is no more or less efficient than calling it once since it is already in memory.
Then an array would just be redundant. Thank you for this update, very helpful.
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
Indicator in strategy? igorvlassov Strategy Development 5 12-20-2008 03:19 PM
Indicator or strategy? 5iver Indicator Development 3 11-12-2008 08:15 AM
Strategy into an indicator paco99 Indicator Development 2 08-16-2008 11:06 AM
Strategy or an Indicator? ShimonD General Programming 1 01-24-2008 07:13 AM
Using Strategy Wizard to develope Indicator on Indicator mrlucky1x Indicator Development 3 08-25-2007 04:09 PM


All times are GMT -6. The time now is 09:38 PM.