Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 md4866, 05-01-2024, 08:15 PM
    2 responses
    18 views
    0 likes
    Last Post md4866
    by md4866
     
    Started by samish18, Today, 12:20 PM
    0 responses
    7 views
    0 likes
    Last Post samish18  
    Started by thread, 04-15-2024, 11:58 PM
    3 responses
    48 views
    0 likes
    Last Post Georg1o
    by Georg1o
     
    Started by leojimenezp, 04-20-2024, 05:49 PM
    4 responses
    49 views
    0 likes
    Last Post leojimenezp  
    Started by nicthe, Today, 09:24 AM
    1 response
    7 views
    0 likes
    Last Post nicthe
    by nicthe
     
    Working...
    X