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 > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 07-05-2011, 03:35 PM   #1
joesandyego
Member
 
Join Date: Jun 2011
Posts: 53
Thanks: 7
Thanked 2 times in 2 posts
Default Accessing Indicators by reference within a Strategy

Hi,
I am newish to C# and new to NT and so forgive the question...
From within a strategy I would like to access an Indicator by reference. The reason for doing this is so that I do not have to search and replace the name of the indicator in multiple spots within the strategy when I want to use a new indicator (with a different name but same input and output parameters).

When using the indicator within the strategy this works fine and passes me back the value of "NewStop1"....

exitLongStopT1Price = MyIndicator(orderPrice, BarsSinceEntry(), exitLongStopT1Price, "Long").NewStop1[0];

this does not work....

Indicator.MyIndicator refindicator= new Indicator.MyIndicator();

exitLongStopT1Price = refindicator(orderPrice, BarsSinceEntry(), exitLongStopT1Price, "Long").NewStop1[0];


I get an error saying "refindicator is a Variable but is used like a Method". I have tried several combinations of () and [] to no avail. In the end, I'm stumped.

Also, in the MyIndicator, I have the parameters defined as follows.
[Description("")]
[GridCategory("Parameters")]
public double AveragePrice1
{
get { return averagePrice1; }
set { averagePrice1 = value; }
}


The Indicator works fine and, again, if I call it directly from within the strategy it works fine. I have not made any out of the ordinary customizations to the Indicator. Any help or sample code would be greatly appreciated.
joesandyego is offline  
Reply With Quote
Old 07-05-2011, 05:02 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

joesandyego,

You are accessing a singleton double value from your indicator like that. If you want to do that you need to store it into a double variable. That double value will be the value whatever value the indicator was at the point in time you assigned it its value. If the indicator changes, the variable will not change along with it.

If you are trying to just simplify the naming you need to create it as an indicator.

private MACD someInd;
someInd = MACD(12, 26, 9);
Print(someInd.Avg[0]);
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-06-2011, 09:32 AM   #3
joesandyego
Member
 
Join Date: Jun 2011
Posts: 53
Thanks: 7
Thanked 2 times in 2 posts
Default

OK. Thanks for the response. So if I understand you correctly then using your example of MACD, if I need to call MACD multiple times within my strategy using different Fast,Slow and Signal parameters each time it is called then I will need to call MACD directly with each new set of parameters each time. Because even in your example of ' someInd = MACD(12, 26, 9); ' MACD is effectively used directly when assigning someInd.

Users must get around this somehow. Because I think that, for example, many Stop Loss and Exit strategies may need real time parameters related to a specific position (if multiple positions are active) such as AvgPrice. So the Stop Loss/Exit indicator may get called multiple times within a strategy (OnBarUpdate) with different parameters passed each time. If I wanted to change to a new Stop Loss/Exit indicator then I would need to find and replace the name of the Indicator in my strategy which could be multiple instances and then recompile. If I could call the Indicator via reference then I could just have a switch/control that switches easily between Indicators.

Again, thanks for your response and I hope that you could just confirm my understanding.
joesandyego is offline  
Reply With Quote
Old 07-06-2011, 11:08 AM   #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

If you are trying to have dynamic parameters then you have no other option but to call it with the parameters you want. This is not really an issue.

Print(MACD(variable1, variable2, variable3).Avg[0]);

Just change the variables to whatever you need them to be and it will be dynamic. There really is not much benefit to creating "references". The only reason people do it is so they can type less in code. If you have to have it dynamic, then you need to do it the way shown above.
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-06-2011, 11:55 AM   #5
joesandyego
Member
 
Join Date: Jun 2011
Posts: 53
Thanks: 7
Thanked 2 times in 2 posts
Default

Thanks for your responses and time. I understand.
joesandyego 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
accessing other indicators or instruments Stephan123 Indicator Development 5 05-03-2011 04:53 PM
Accessing Properties of other Indicators WolliWilli Version 7 Beta General Questions & Bug Reports 1 11-20-2009 06:29 AM
Accessing Real-Time Indicators within a Strategy Prospectus Strategy Development 1 10-22-2009 06:02 AM
accessing indicators velocity General Programming 7 02-22-2009 11:17 PM
Reference variables for indicators cls71 General Programming 4 03-12-2008 01:56 AM


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