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

Skip part of script

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

    Skip part of script

    Hello dear NT support and everybody! So, what if there is some part of script that I want to be skipped if some conditions come true?

    Example:
    if (A=3) do smth
    else continue to read script

    #2
    Hello alexstox,

    You can certainly use an IF statement to accomplish what you are trying to do. In fact the statement you posted would work as a simple switch to turn on and off sections of code using a variable.

    Here is what your statement would look like in NinjaScript
    Code:
    private int A = 3;
    
    protected override void OnBarUpdate()
    {
    if(A == 3)
    {
    // A is equal to 3 so this section of the if statement would be executed
    }
    else
    {
    // If A is not equal to 3 this part would be used instead. 
    }
    }
    For this example the portion of code you would like to skip if A is equal to 3 would be the second half of the if Statement.

    When using an IF statement it is always good to keep in the back of your mind, the only items that will belong in the if statement would be the items that need to be turned on or off using the variable. The rest of your code can remain outside of the IF statement so that it is properly executed.

    I would like to provide this link to an MSDN article on IF statement usages. This is an excellent resource to take a look at as there are quite a few good examples.

    http://msdn.microsoft.com/en-us/library/5011f09h.aspx

    One note on this, Because NinjaScript is essentially C# programming language any C# tutorials you may find online can be helpful when writing NinjaScript.

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by alexstox View Post
      Hello dear NT support and everybody! So, what if there is some part of script that I want to be skipped if some conditions come true?

      Example:
      if (A=3) do smth
      else continue to read script
      Hi alexstox,

      I don't actually understand your question. Could you tell more clearly about what to be skipped? I edit your example a little bit to make it more general and easier for you to express your thought:

      Code:
      if (condition) Do_something_1;
      else Do_something_2;

      So, what do you want to be skipped? Do_something_1 or Do_something_2?

      Thank you.
      Pi
      Last edited by ninZa; 06-06-2014, 06:11 PM.
      ninZa
      NinjaTrader Ecosystem Vendor - ninZa.co

      Comment


        #4
        What if script contains lots of conditions in different places of script. And I need to jump from one condition to another?
        I just need to reverse position. But to do so, I need yo put some rules before long or short entries. That's why I thought to do it like this - check if smth happen before long entries, if yes - jump to short entries rules and do short...

        Maybe you have some examples of reverse position in strategy with lots of rows =)))

        Comment


          #5
          Originally posted by alexstox View Post
          What if script contains lots of conditions in different places of script. And I need to jump from one condition to another?
          I just need to reverse position. But to do so, I need yo put some rules before long or short entries. That's why I thought to do it like this - check if smth happen before long entries, if yes - jump to short entries rules and do short...

          Maybe you have some examples of reverse position in strategy with lots of rows =)))
          If I understand your question correctly, then this may be a solution. You can use the goto function to "jump / fly" to a label of your choice, anywhere in your code.

          Example:
          if (your_condition) goto Your_Label;

          ........ script to be skipped
          ........ script to be skipped
          ........ script to be skipped

          Your_Label:
          Do_your_work_here;
          Learn more about goto statement: http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
          ninZa
          NinjaTrader Ecosystem Vendor - ninZa.co

          Comment


            #6
            Originally posted by ninZa View Post
            If I understand your question correctly, then this may be a solution. You can use the goto function to "jump / fly" to a label of your choice, anywhere in your code.

            Example:


            Learn more about goto statement: http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
            Wow! It's cool! Thank you

            Comment


              #7
              Originally posted by alexstox View Post
              Wow! It's cool! Thank you
              Glad to help you. Cheers
              ninZa
              NinjaTrader Ecosystem Vendor - ninZa.co

              Comment


                #8
                Originally posted by alexstox View Post
                Wow! It's cool! Thank you
                Be very careful, please. There is good reason why the goto statement is frowned upon by development teams. Such unconditional transfer of control makes code VERY hard to read much later, especially with code that has many complex branches. It is very easy to read when writing it: it is very hard to follow when being reviewed, whether by another person in QA, or the original author much later when the ideas are no longer fresh in the mind.

                Every control structure can be implemented using "goto" statements, but this is best left to the compiler, not the high-level programmer. "goto" isn't considered harmful because of its positive effects but because of its negative consequences, and these negativities have been eliminated by structured programming, and its derivatives.

                In the same vein, saying “GOTO is a tool, and as with all tools, it can be used and abused” is completely off the mark. As a parallel example, no modern construction worker would use a rock and claim it “is a tool.” Rocks have been replaced by hammers. Similarly, in high-level programming, "goto" has been replaced by control structures.

                Now, of course, if the construction worker were stranded in the wild without a hammer, sensibly, he would use a rock instead. If a programmer has to use an inferior programming language that doesn't have modern, sensible, control structures, well, of course, one may have to use "goto" instead. But if one uses the goto statement anywhere else, instead of using the appropriate language feature, then clearly the language has not been properly understood, and the "goto" statement is still being wrongly used. It really is as "cut and dried" as that.

                I am off the soapbox.

                Comment


                  #9
                  Originally posted by koganam View Post
                  Be very careful, please. ...

                  I am off the soapbox.
                  Thank you Big Brother

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by tonynt, Today, 11:50 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post tonynt
                  by tonynt
                   
                  Started by Welovethelibrary, Today, 11:44 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post Welovethelibrary  
                  Started by goodknight777, Today, 11:39 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post goodknight777  
                  Started by MohammedAmine, Today, 10:44 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Started by Nolyn, Today, 10:59 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Working...
                  X