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 06-17-2012, 02:25 PM   #1
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default how to put some methods into a shared file, and let my indicator and strategy refer

Hi,

I have finished programming an indicator. The indicator basically contains a bunch of conditions and outputs some bool type variables to indicate entry/exit.

Now I'm programming a strategy. I wonder if you can briefly teach me how to put some functions that output bool type in a separate file, and let my indicator refer to it. That way if I need to change my method, I don't need to change both indicator and strategy files.
leontancfa is offline  
Reply With Quote
Old 06-17-2012, 02:32 PM   #2
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

You will need to expose these bools publicly which can then be read directly from the strategy. You would not need to write to a separate file with this method.

Please see our Reference Sample on Exposing indicator values that are not plots

http://www.ninjatrader.com/support/f...ead.php?t=4991
NinjaTrader_Matthew is offline  
Reply With Quote
Old 06-17-2012, 09:44 PM   #3
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Thanks for reply.

I tried the same as the example below.

Code:
            Add(MACD(12, 26, 9));
            Add(SampleBoolSeries());
But the problem is that my strategy is a multi-timeframe strategy.

I can only add indicator to the primary data series, but can't add to my secondary data series.

Is there a way to work around this?
leontancfa is offline  
Reply With Quote
Old 06-18-2012, 03:42 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,568
Thanks: 261
Thanked 1,017 times in 998 posts
Default

Hi Leon, what you experience is expected - if you wish to Add() an indicator working on another series, please do those calcs in the added indicator itself.
NinjaTrader_Bertrand is online now  
Reply With Quote
Old 06-18-2012, 07:10 AM   #5
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
if you wish to Add() an indicator working on another series, please do those calcs in the added indicator itself.
Sorry, what does this mean?

So say if I firstly added a secondary series by:
Add(PeriodType.Ticks, 500);

Then how do I run the indicator SampleBoolSeries() on that secondary series?
leontancfa is offline  
Reply With Quote
Old 06-18-2012, 07:42 AM   #6
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

Hello,

The Add() method would only be used for visual charting purposes. If you wanted to visualize this indicator on the chart from a secondary data series, you would need to manually add the indicator to your chart.

However you can still reference the indicator methods in your strategy and specify which data series it is being calculated from

Taking the SampleBoolSeries for example, the conditional logic would read:

Code:
if(SampleBoolSeries(BarsArray[1]).BullIndication[0])
				EnterLong();
By using BarsArray[1], we are specifying that this indicator is calculated from the first Add() data series you have in your strategy.

More information on BarsArray can be found below:

http://www.ninjatrader.com/support/h.../barsarray.htm
NinjaTrader_Matthew is offline  
Reply With Quote
Old 06-19-2012, 09:44 AM   #7
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi I think my question would really be: how to build a strategy and indicator referencing a external library? Is there an example for it?
leontancfa is offline  
Reply With Quote
Old 06-19-2012, 09:59 AM   #8
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

Hello,

It is possible to have your indicator or strategy read from an external file.

Please see our Reference Sample on Using StreamReader to read from a text file


http://www.ninjatrader.com/support/f...ead.php?t=3476
NinjaTrader_Matthew is offline  
Reply With Quote
Old 06-19-2012, 07:44 PM   #9
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Matthew View Post
Hello,

The Add() method would only be used for visual charting purposes. If you wanted to visualize this indicator on the chart from a secondary data series, you would need to manually add the indicator to your chart.

However you can still reference the indicator methods in your strategy and specify which data series it is being calculated from

Taking the SampleBoolSeries for example, the conditional logic would read:

Code:
if(SampleBoolSeries(BarsArray[1]).BullIndication[0])
                EnterLong();
By using BarsArray[1], we are specifying that this indicator is calculated from the first Add() data series you have in your strategy.

More information on BarsArray can be found below:

http://www.ninjatrader.com/support/h.../barsarray.htm
Thank you. So does it mean that it does not matter whether "Add(SampleBoolSeries());" is added into Initialize(), the strategy will work the same?


Code:
        protected override void Initialize()
        {
            // Add our indicators to the strategy for charting purposes
            Add(MACD(12, 26, 9));
            Add(SampleBoolSeries());
            
            CalculateOnBarClose = true;
        }
leontancfa is offline  
Reply With Quote
Old 06-20-2012, 05:23 AM   #10
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello leontancfa,
Thanks for your note and I am replying for Matthew.

Yes, the strategy will work fine even if you do not Add the indicator in the initialize section of the strategy.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 06-23-2012, 06:44 AM   #11
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default

what is the benefit of having a private variable,
Code:
        #region Variables
        private BoolSeries aBC;
        #endregion
and then have a public property?
Code:
        public BoolSeries ABC
        {
            get { return aBC; }
        }
what could go wrong if I just make the variable public, and get the bool value by MyIndicator.aBC[0]?
leontancfa is offline  
Reply With Quote
Old 06-23-2012, 11:39 AM   #12
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,356
Thanks: 24
Thanked 1,304 times in 1,067 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by leontancfa View Post
what is the benefit of having a private variable,
Code:
        #region Variables
        private BoolSeries aBC;
        #endregion
and then have a public property?
Code:
        public BoolSeries ABC
        {
            get { return aBC; }
        }
what could go wrong if I just make the variable public, and get the bool value by MyIndicator.aBC[0]?
Mainly because it is the best practice in most cases.

ref: http://msdn.microsoft.com/en-us/library/w86s7x04.aspx
koganam is online now  
Reply With Quote
The following user says thank you to koganam for this post:
Old 06-23-2012, 08:02 PM   #13
leontancfa
Member
 
Join Date: Nov 2008
Posts: 95
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you. so I read that reference. I saw one reason is to let outside the indicator only read, but not set the value of the inner variable.

Did I miss other reasons?
leontancfa is offline  
Reply With Quote
Old 06-23-2012, 11:13 PM   #14
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,356
Thanks: 24
Thanked 1,304 times in 1,067 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by leontancfa View Post
Thank you. so I read that reference. I saw one reason is to let outside the indicator only read, but not set the value of the inner variable.

Did I miss other reasons?
That main import of separating Properties from the value of the backing store is in this paragraph:

"Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields."

The funny thing about programming is that you may think at this time that you do not need any of those abilities, so you can dispense with them and just use a public variable. In my experience, however, sooner or later, if one continues development, one will find that he/she wants to do something which would be very easy to do, if only Properties had been used in the first place.

Best practices get dubbed as such, because the collective wisdom of those who have come before us, have found out the hard way, that not doing things in that manner, often causes regrets down the road.

Just my $0.02.
koganam is online now  
Reply With Quote
The following user says thank you to koganam for this post:
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
Shared Indicator ScottB Automated Trading 2 08-04-2011 08:10 AM
Trying to update shared file zacharydw00 NinjaScript File Sharing Discussion 7 11-02-2009 07:17 AM
Can I refer an indicator variables in a strategy ? blarouche General Programming 3 07-13-2009 09:09 AM
Create methods that are shared by other indicators xewoox General Programming 5 02-25-2009 02:09 PM
Shared Draww methods only worl in Panel 1 scjohn General Programming 1 05-27-2007 12:51 PM


All times are GMT -6. The time now is 10:19 AM.