Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting private variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Setting private variables

    Hi,

    Does it make any difference "where" i set my private variables within a strategy?

    In other words, is there a difference between the two methods below:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public class MissionImpossbleWeeklyBoth : Strategy
    	{
    		private double SundayTradesCumProfit = 0;
    		private double CurrentTradesCumProfit = 0;
    vs

    Code:
    		protected override void OnBarUpdate()
    		{
    		private double SundayTradesCumProfit = 0;
    		private double CurrentTradesCumProfit = 0;
    The reason I am asking is because there are many logics that I have created that I need to be able to "quickly" copy into a new strategy. If I have them under "OBU", then it's easier for me to copy them.

    Thanks

    #2
    Hello,

    Thank you for the post.

    Yes, this is a core fundamental of C#, the scope in which you place your variables is very important. The Modifier used (private,public) is also very important and can only be used in class scope.

    From what you have provided, the first example is valid, the second is not, the private modifier cannot be used inside a method body.


    For variables that should only be used in one iteration of OnBarUpdate, you could form them like the following:


    Code:
    protected override void OnBarUpdate()
    {
    	double SundayTradesCumProfit = 0;
    	double CurrentTradesCumProfit = 0;
    }
    This would be valid for 1 iteration of OnBarUpdate, the next iteration the value would start at 0 again because that is what is defined.

    For variables that accumulate over multiple iterations of OnBarUpdate, those would need to go in the class level:

    Code:
    public class MissionImpossbleWeeklyBoth : Strategy
    {
    	private double SundayTradesCumProfit = 0;
    	private double CurrentTradesCumProfit = 0;
            
            protected override void OnBarUpdate()
            {
    
            }
    }
    Class level variables that are just fields or simple storage would be defined like the above as private. Other class level variables known as properties are defined as public.

    You can find more information on this topic using google and search for keywords like "C# variable scope" or "C# file structure".

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    77 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    53 views
    0 likes
    Last Post CarlTrading  
    Working...
    X