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

2 Strategies into 1 Script Structure

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

    2 Strategies into 1 Script Structure

    I need help with developing a structure for a strategy. It’s actually 2 strategies with different rules combined into 1. I would like the first set of rules to be the primary and second set to only run if first set is not running. The way that I have it right now is below. Would this be the best way for it or could you please provide recommendations?

    OnBarUpdate()
    {
    if (set1)
    {
    if (conditions for set1 long)
    EnterLong()
    if (conditions for set1 short)
    EnterShort()
    }

    else if (set2 - conditions for set2 long)
    {
    if (conditions for set2 long)
    EnterLong()
    }

    else if (set2 - conditions for set2short)
    {
    if (conditions for set2 short)
    EnterShort()
    }
    }

    #2
    Hello AgriTrdr,

    Yes that would be the correct use of else if. You made a primary set to contain the code and if that is not true it would move to set 2 etc until one of those top level if statements is true. All the other else statements are not used once one of the conditions is true.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. I'm curious would there be another creative way to write it?

      Comment


        #4
        Hello AgriTrdr,

        Yes however that would require more code, if else statements are generally the most simple and correct way to make branching logic. One thought that comes to mind as a creative way would be to use variables and just allow all of the conditions to be checked always. Later in the code you could check which variables got set to know which of the conditions were true. That would require a lot more logic for the same general task and that would also require using multiple if conditions to check which of the variable sets paired together make each condition. That would be more confusing as well.

        NinjaScript is C# language so you can use any of the mechanics that C# offers like if else/ switch/ variables and conditions to make branching logic.

        You will most always need an if statement at some point to make sure a condition is true to know what action to do, the if else serves that purpose the best out of the various options because it provides a way to isolate sections of code (logically and visually) and also check conditions in the same line of code.


        JesseNinjaTrader Customer Service

        Comment


          #5
          I see what you're saying. Yes it does become more complicated with variables and multiple if statements. The primary thing that I'm trying to achieve is if "set1" strategy is active even though "set2" strategy has the signal it should prevent "set2" from triggering because "set1" supersedes "set2".

          I just tested if and else if method. Elseif does trigger when the signal is given.

          Would something like this prevent set2 from triggering:

          bool strategy1 = true;
          bool strategy2 = true;

          if (strategy1 == true && strategy2 == true)


          if(strategy1 == false && strategy2 == true)

          Comment


            #6
            Hello AgriTrdr,

            Your original code had already achieved that, if the set1 was true it would fall into that if statement and the others wont be executed. Set 2 is only checked as the else when the set 1 is false.



            JesseNinjaTrader Customer Service

            Comment


              #7
              Ok thanks. I'll try running the strategy again.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by fx.practic, 10-15-2013, 12:53 AM
              5 responses
              5,403 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Started by Shai Samuel, 07-02-2022, 02:46 PM
              4 responses
              94 views
              0 likes
              Last Post Bidder
              by Bidder
               
              Started by DJ888, Yesterday, 10:57 PM
              0 responses
              6 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by MacDad, 02-25-2024, 11:48 PM
              7 responses
              158 views
              0 likes
              Last Post loganjarosz123  
              Started by Belfortbucks, Yesterday, 09:29 PM
              0 responses
              8 views
              0 likes
              Last Post Belfortbucks  
              Working...
              X