NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 05-15-2007, 02:25 PM   #1
tquinn
Senior Member
 
Join Date: Jan 2005
Location: , ,
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
Default Exposing non dataseries variable

I am calculating a double variable (not a plotted dataseries) which represents a temp level of S/R. Can I make that value public, so it can referenced by strategies? How?
tquinn is offline  
Reply With Quote
Old 05-15-2007, 02:42 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Try creating a property that accesses your internal variable.

Code:
private double tempLevel = 0;

public double TempLevel
{
    get { return tempLevel; }
}
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 09:45 AM   #3
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

I have a follow-up question on this topic. I have a custom indicator that doesn't return any values (i.e., it's a void function), but similar to the OP I would like to implement my indicator in a strategy and have access to several of the values used internally in the function. As you directed below, I created public variables in the properties section (plotAbove is my private variable in the function).

public double TrendAbove
{
get { return plotAbove; }
}

I did this for eight different variables of types int, double and bool. The indicator compiled okay.

My questions are as follows:

1) I understand from a previous question that I should create an instance of my indicator using the Add() function (it's named ATRDots and takes 5 inputs). I assume this should go in the initialize block of the strategy code.

Add(ATRDots(5, 3, 13, 0.75, 0.9));

If I want to analyze multiple timeframes in my strategy, will the ATRDots indicator automatically be created for each bars object I add to strategy? Let's say, for example, I created the following bars objects.

Add(PeriodType.Volume, 4000);
Add(PeriodType.Volume, 1000);
Add(PeriodType.Tick, 444);

If I added my indicator as noted above following the creation of these bars objects would it automatically create a separate instance of my indicator for each of the three bars objects, or I do I need to add the indicator three separate times, one for each bars object?


2) Given that I hopefully have three instances of my ATRDots indicator at this point (one for each bars object), how do I reference the public variables for each one (e.g., for the TrendAbove variable noted above)?

Thank you.
JangoFolly is offline  
Reply With Quote
Old 05-17-2007, 10:34 AM   #4
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

See below.

Quote:
Originally Posted by JangoFolly View Post
I have a follow-up question on this topic. I have a custom indicator that doesn't return any values (i.e., it's a void function), but similar to the OP I would like to implement my indicator in a strategy and have access to several of the values used internally in the function. As you directed below, I created public variables in the properties section (plotAbove is my private variable in the function).

public double TrendAbove
{
get { return plotAbove; }
}

I did this for eight different variables of types int, double and bool. The indicator compiled okay.

My questions are as follows:

1) I understand from a previous question that I should create an instance of my indicator using the Add() function (it's named ATRDots and takes 5 inputs). I assume this should go in the initialize block of the strategy code.

Add(ATRDots(5, 3, 13, 0.75, 0.9));

>>> This Add() method is only used for visualization, nothing more. By calling this method and passing in the indicator as you have done, it will display on the chart when the strategy runs on a chart.

If I want to analyze multiple timeframes in my strategy, will the ATRDots indicator automatically be created for each bars object I add to strategy? Let's say, for example, I created the following bars objects.

Add(PeriodType.Volume, 4000);
Add(PeriodType.Volume, 1000);
Add(PeriodType.Tick, 444);

If I added my indicator as noted above following the creation of these bars objects would it automatically create a separate instance of my indicator for each of the three bars objects, or I do I need to add the indicator three separate times, one for each bars object?

>>> No, an indicator instance is created when you call the method in the OnBarUpdate() method.

// Print the primary series value
Print(ATRDots(5, 3, 13, 0.75, 0.9).TrendAbove);

// Print the 4000 volume bar
Print(ATRDots(BarsArray[1], 5, 3, 13, 0.75, 0.9).TrendAbove);

// Print the 1000 volume bar
Print(ATRDots(BarsArray[2], 5, 3, 13, 0.75, 0.9).TrendAbove);


2) Given that I hopefully have three instances of my ATRDots indicator at this point (one for each bars object), how do I reference the public variables for each one (e.g., for the TrendAbove variable noted above)?

>>> See above.

Thank you.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 11:34 AM   #5
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

Ray,

When I add the following strategy to a chart, the indicator doesn't display on the chart, and the prints to the output window are all zero. I feel like I'm missing part of the big picture about how these things communicate. Thanks.

protected override void Initialize()
{
CalculateOnBarClose = false;

Add(ATRDots(5, 3, 13, 0.75, 0.9));
}

protected override void OnBarUpdate()
{
Print(ATRDots(5, 3, 13, 0.75, 0.9).TrendAbove);
}
JangoFolly is offline  
Reply With Quote
Old 05-17-2007, 12:28 PM   #6
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

My fault, I forgot one critical piece of information. When you have an indicator that does not have a plot and you create a public property that you wish to access from another indicator or strategy, you have to call the Update() method within your property "Getter" to force an update of the OnBarUpdate().

Additional information can be found here on this method - http://www.ninjatrader-support.com/H...V6/Update.html

I have included a Sample.cs (indicator) and SampleStrategy.cs (strategy) that demonstrates everything discussed in this thread.
Attached Files
File Type: cs Sample.cs (4.9 KB, 29 views)
File Type: cs SampleStrategy.cs (1.5 KB, 33 views)
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 01:03 PM   #7
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

It's still not working for me. Can I e-mail you the indicator and strategy to see whether I'm missing something? I would rather not post it publicly. The strategy is just a test print of a single variable.

I really appreciate your patience and help. Thank you.


Quote:
Originally Posted by NinjaTrader_Ray View Post
My fault, I forgot one critical piece of information. When you have an indicator that does not have a plot and you create a public property that you wish to access from another indicator or strategy, you have to call the Update() method within your property "Getter" to force an update of the OnBarUpdate().

Additional information can be found here on this method - http://www.ninjatrader-support.com/H...V6/Update.html

I have included a Sample.cs (indicator) and SampleStrategy.cs (strategy) that demonstrates everything discussed in this thread.
JangoFolly is offline  
Reply With Quote
Old 05-17-2007, 01:08 PM   #8
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

I hope you understand when I say that we don't have the bandwidth to trouble shoot user generated code.

Have you tried the sample code I posted? For sure this works. Its simple and clean. What I would suggest is use my code and build on top of it to achieve the result you are looking for.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 02:27 PM   #9
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

I do understand.

Your sample code worked just fine. I tried changing the indicator reference in your sample strategy to use my indicator rather than your sample indicator, but it still put out only zero.

My indicator works just fine when I add it to a chart, but I did just notice this message in the log. Could it be causing the problem?

Failed to call method 'Initialize' for indicator 'ATRDots': Object reference not set to an instance of an object.


Thank you.

Quote:
Originally Posted by NinjaTrader_Ray View Post
I hope you understand when I say that we don't have the bandwidth to trouble shoot user generated code.

Have you tried the sample code I posted? For sure this works. Its simple and clean. What I would suggest is use my code and build on top of it to achieve the result you are looking for.
JangoFolly is offline  
Reply With Quote
Old 05-17-2007, 02:59 PM   #10
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

For sure that is causing a problem.

- Did you add a call to the Update() method within your indicator for each properties' getter ?
- If yes, apply your indicator to a chart and see if you get the "object" reference error
- If yes, then your indicator is not functioning, somewhere in your indicator code you are accessing a variable that does not hold an object, you will have to add debug print statements to determine where that might be

For example:

private object myObject = null; // Might be an object variable

Where ever you reference this variable, you might add a line of code such as:

if (myObject == null) Print ("Null object at line XXXX");

You then know where in your code is causing the exception and thus can figure out why its null.
NinjaTrader_Ray is offline  
Reply With Quote
Old 05-17-2007, 04:12 PM   #11
JangoFolly
Member
 
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
Default

This was causing the problem...


private string currSymbolRoot; // In the variable section


currSymbolRoot = Instrument.MasterInstrument.Name; // In the initialization block


Everything seems to be working now. The strange thing is that the currSymbolRoot variable was populated correctly, so it wouldn't suggest an error.

Again, thank you for all of your help and patience.


Regards,


Quote:
Originally Posted by NinjaTrader_Ray View Post
For sure that is causing a problem.

- Did you add a call to the Update() method within your indicator for each properties' getter ?
- If yes, apply your indicator to a chart and see if you get the "object" reference error
- If yes, then your indicator is not functioning, somewhere in your indicator code you are accessing a variable that does not hold an object, you will have to add debug print statements to determine where that might be

For example:

private object myObject = null; // Might be an object variable

Where ever you reference this variable, you might add a line of code such as:

if (myObject == null) Print ("Null object at line XXXX");

You then know where in your code is causing the exception and thus can figure out why its null.
JangoFolly 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
User variable scope ct General Programming 1 05-08-2007 07:02 AM
Compile error with MIN and MAX variable assignment lewdfinger General Programming 2 02-10-2007 07:13 AM
How do I get 'barsAgo" info for a variable I create? BradB General Programming 2 01-26-2007 06:54 AM
Hiding a plotted DataSeries in an indicator tquinn Indicator Development 7 01-13-2007 11:42 AM
Exposing a non DataSeries property in an indicator tquinn Indicator Development 4 01-11-2007 02:29 PM


All times are GMT -6. The time now is 06:07 AM.