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 11-21-2008, 07:58 AM   #1
spinalwiz
Member
 
Join Date: Oct 2008
Posts: 60
Thanks: 0
Thanked 0 times in 0 posts
Default Setting an Indicator Variable from a Strategy

Hi,

I was just wondering why this code doesn't work - I have the strategy and the indicator added to a chart. From the Print() statement I can tell the variable is getting set in the indicator, but the plot is just plotting 1's.

Code:
public class MyCustomStrategy : Strategy {
        #region Variables
            int myValue = 0;
        #endregion

        protected override void Initialize(){
            CalculateOnBarClose = true;
        }

        protected override void OnBarUpdate(){
            myValue++;
            MyCustomIndicator().TestValue = myValue;
            if(myValue == 10)
                myValue = 0;    
        }
    }
}
Code:
public class MyCustomIndicator : Indicator
    {
        #region Variables
            private int testValue = 0;
        #endregion

        protected override void Initialize() {
            Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
            CalculateOnBarClose    = true;
        }

        protected override void OnBarUpdate() {
              Print(testValue);    
            Values[0].Set(testValue);
        }

        #region Properties
        public int TestValue {
            get { return testValue; }
            set { testValue = Math.Max(1, value); }
        }
spinalwiz is offline  
Reply With Quote
Old 11-21-2008, 11:13 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Hi spinalwiz,

Please take a look at your indicator region properties and use this snippet:

Code:
public int TestValue
{
get { return testValue; }
set { testValue = Math.Max(0, value); }
}
Tested this briefly on my end and it correctly plots a '0' on the chart.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 12-05-2008, 05:12 AM   #3
spinalwiz
Member
 
Join Date: Oct 2008
Posts: 60
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi Bertrand,

Good spot, although I dont think I explained myself very well. Ignoring the rest of the code, this is the part I can't get to work:

In the Strategy:
Code:
MyCustomIndicator().TestValue = 5;
And in my Indicator:

Code:
Values[0].Set(testValue);
But it just plots 0s for some reason. The strange part is that if I add to the indicator:

Code:
Print(testValue);
Then in the output window it prints:

0
5
0
5
0
5
0
5

This is the code I have in Properties:
Code:
        public int TestValue {
            get { return testValue; }
            set { testValue = Math.Max(0, value); }
        }
spinalwiz is offline  
Reply With Quote
Old 12-05-2008, 07:46 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Hi spinalwiz,

Thanks, please take a look then at the following reference sample - http://www.ninjatrader-support2.com/...ead.php?t=4991

You want to use a DataSeries class to have up-to-date values...

Have a great day!
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 12-16-2008, 11:23 AM   #5
spinalwiz
Member
 
Join Date: Oct 2008
Posts: 60
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi,

Thank you for your reply. I am now using a DataSeries although it unfortunately still does not work. The example you gave was for getting an indicator variable from a strategy. I need to set an indicator variable from a strategy.

The code I have in my strategy is:
Code:
MyCustomIndicator().MyData.Set(5);    
MyCustomIndicator().Update();
And in my Indicator I have:
Code:
Values[0].Set(myData[0]);
My Properties section looks like this:
Code:
public DataSeries MyData
        {
            set { myData = value; }
            get { return myData; }
        }
spinalwiz is offline  
Reply With Quote
Old 12-16-2008, 11:40 AM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,377
Thanks: 252
Thanked 966 times in 949 posts
Default

Hi spinalwiz, unfortunately this is not supported.
NinjaTrader_Bertrand 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
Color variable not being initialized if Indicator is included in a Template GaryAlbers Suggestions And Feedback 3 04-15-2008 07:10 AM
Strategy with a variable that is not plotted ssierra Strategy Analyzer 1 03-08-2008 07:17 PM
externally accessible custom indicator variable? Lost Trader General Programming 5 01-12-2008 02:27 AM
Setting Swing to a variable? Burga1 Strategy Development 15 12-24-2007 03:25 AM
Using a formula or variable as an input to an Indicator nolantx Indicator Development 5 05-24-2007 03:58 PM


All times are GMT -6. The time now is 11:09 AM.