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

Using states in ninjascript

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

    Using states in ninjascript

    I need to figure out how to make an indicator that is looking for the same thing to occur in sequence using multiple time frames. When the trigger occurs in the higher time frame I want the state to change and then look for the same trigger on a lower time frame and not restart the sequence until that same trigger occurs again on the higher time frame. I'm assuming using multiple states is the best way to do this but I have very little c# experience and have not been able to find a sample of the syntax. I just need to know how to create multiple states in ninjascript so I can chain triggers together in multiple time frames and ensure they happen in a specific sequence.

    #2
    Hello,

    Thank you for the post.

    In this case, you would likely need to use some boolean variables to control the state, a boolean is just a true/false variable. In the situation where your first condition becomes true, it could set a boolean variable to true. In another condition, you could watch for that variable to become true so the second part of the logic can begin to work.

    You could repeat this process for as many conditions as you need to be true. A very simple example of a boolean switch would be like the following:

    Code:
    private bool myBool1;
    private bool myBool2;
    protected override void OnBarUpdate()
    {
    	if(Close[0] > Open[0])
    	{
    		myBool1 = true;	
    	}
    	
    	if(myBool1 == true && Close[2] > Open[2])
    	{
    		myBool2 = true;
    	}
    	
    	if(myBool1 == true && myBool1 == true)
    	{
    		// both were true
    		myBool1 = false;
    		myBool2 = false;
    	}
    	
    }

    Because we are just using C#, I would suggest searching online for tutorials on C# state machines and conditional if statements. This would give you a more broad understanding of the core language used in NinajScript which in turn will help in writing NinjaScript overall.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thanks for the quick response. This will work but will be problematic for certain types of indicators. I don't want condition A to be checked as a prerequisite for condition B to occur once condition A has already occurred. I need a solution for if A occurs stop checking A then look for B and then once B is found start looking for C , etc. I don't want it to check that the conditions that triggered on time frames A and B are still present when the alert is triggered in time frame C. Each trigger needs to be a separate and unrelated event which does nothing but allow it to look for the next condition in a lower time frame. The reason for this is that the trigger condition may no longer be present on the higher time frame when the lower time frame triggers. This is why using a simple true false condition wont work unless ranges are used. It appears that using states is the best way to accomplish this but I could be wrong as I have very limited c# experience. If you recommend using states for this process could you please provide an example of this syntax using a very basic ma line cross or something equally as simple using 2 or more time frames where the lower time frame (trigger b) will run the method only when trigger A (the higher time frame) has already triggered and wont restart this process until another trigger occurs again on the higher time frame.

      Comment


        #4
        Hello,

        Thank you for the reply.

        Yes, this was just a simple example of usings booleans in response to your question regardings states. For your overall goal, you would need to develop script and its conditions to your liking and test it to make sure it performs the way you intended.

        States may be what you want or not, it would really depend what your end goal is. This would be something you can develop and try using the tools of the platform. I couldnt really say what you should do in this case as that could be construed as trading advice, but I can provide the tools that you can use to educate yourself on this topic.

        In regard to turning off the existing conditions, you could again use another bool for that case. A bool could be used to turn off a specific condition after it becomes true. This is again just a simple example, you can combine its concepts with other parts of NinjaScript to achieve your overall goals:

        Code:
        private bool myBool1;
        private bool myBool2;
        [B]private bool turnOffCondition1;[/B]
        protected override void OnBarUpdate()
        {
        	if(Close[0] > Open[0] [B]&& turnOffCondition1 == false[/B]) 
        	{
        		myBool1 = true;
                        [B]turnOffCondition1 = true;	[/B]
        	}
        	
        	if(myBool1 == true && Close[2] > Open[2])
        	{
        		myBool2 = true;
        	}
        	
        	if(myBool1 == true && myBool1 == true)
        	{
        		// both were true
        		myBool1 = false;
        		myBool2 = false;
                        //some condition to reset
                        [B]turnOffCondition1 = false;[/B]
        	}
        }

        In regard to the multi timeframe aspect of this, if you have not worked with multi timeframe scripts before this can be a complicated subject to start with, I will try to provide the best examples of this that we have below.

        To add a series and also see some example of this, the following page is a good resource:


        For an example of comparing a second series in a simple way, please see the following help guide item:


        This would also bring up another point in multi series scripts, BarsInProgress:


        For a good overview of all of these concepts and how they interlock, please see the following page: https://ninjatrader.com/support/help...nstruments.htm


        Depending on how you want your script to work, you can either check your conditions on all timeframes or just in OnBarUpdate with no other surrounding conditions. You can also separate your logic based on the timeframe that called OnBarUpdate. I would suggest reviewing the following sections for more examples of multi-series scripts:




        For some other examples of if statements and conditional statements in C#, please see the following resources:


        The `if` and `switch` statements provide branching logic in C#. You use `if, `else` and `switch` to choose the path your program follows.

        C# - Nested if Statements - It is always legal in C# to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

        Here you will learn what Conditional Statements are and how to work with them in C#.



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ScottWalsh, 04-16-2024, 04:29 PM
        6 responses
        27 views
        0 likes
        Last Post ScottWalsh  
        Started by frankthearm, Today, 09:08 AM
        10 responses
        35 views
        0 likes
        Last Post frankthearm  
        Started by GwFutures1988, Today, 02:48 PM
        0 responses
        2 views
        0 likes
        Last Post GwFutures1988  
        Started by mmenigma, Today, 02:22 PM
        1 response
        3 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by NRITV, Today, 01:15 PM
        2 responses
        9 views
        0 likes
        Last Post NRITV
        by NRITV
         
        Working...
        X