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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Peppo, 04-13-2025, 12:59 PM
    11 responses
    88 views
    0 likes
    Last Post Peppo
    by Peppo
     
    Started by michelz, 02-18-2025, 08:30 AM
    31 responses
    1,018 views
    0 likes
    Last Post rockmanx00  
    Started by TraderYoda, 05-24-2019, 03:33 AM
    43 responses
    2,119 views
    1 like
    Last Post pwmmmm
    by pwmmmm
     
    Started by jb805, 02-04-2025, 12:34 PM
    5 responses
    168 views
    0 likes
    Last Post Swagpapifrio  
    Started by Newtrader101, Today, 11:27 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Working...
    X