![]() |
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jan 2005
Location: , ,
Posts: 109
Thanks: 0
Thanked 0 times in 0 posts
|
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?
|
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Try creating a property that accesses your internal variable.
Code:
private double tempLevel = 0;
public double TempLevel
{
get { return tempLevel; }
}
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#4 | |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
See below.
Quote:
Ray
NinjaTrader Customer Service |
|
|
|
|
|
|
#5 |
|
Member
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
|
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); } |
|
|
|
|
|
#6 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#7 | |
|
Member
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
|
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:
|
|
|
|
|
|
|
#8 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#9 | |
|
Member
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
|
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:
|
|
|
|
|
|
|
#10 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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.
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#11 | |
|
Member
Join Date: Nov 2006
Location: , ,
Posts: 73
Thanks: 0
Thanked 0 times in 0 posts
|
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:
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |