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

Question about strategy development

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

    Question about strategy development

    Hello,

    this is a basic question and I should know the answer but I need to make sure:

    what is the difference between the following codes:

    1. If( SMA1 > Close && CrossAbove( SMA2, SMA1, 1))

    2. if( SMA1 > Close)
    If( CrossAbove( SMA2, SMA1, 1)

    I am not sure when to use the “&&” and when to use different “if”

    thank you

    #2
    Hello Jorge.andres.o,

    These two examples would essentially equate the same with how it is coded.

    The first sample is saying if condition 1 and condition 2 are true then do the action. The second sample would be doing the same except you have two individual if conditions. Its only the same because you did not use a condition body or { } the first if statement would only affect the second condition.

    To determine which to use you need to know what actions you need to do. Here are a few examples of how you can structure the conditions.

    Code:
    if(condition1)
        if(Condition 2)
            Print("condition 1 and 2 were true");
    Print("This comes after the conditions and does not apply to either condition");
    If you look at this example the condition 2 will only be checked if condition 1 was true, The Print will always happen because the condition 1 only applies to the next line following it. There were no { } used.

    Code:
    if(condition1)
    {
        Print("1");
        if(Condition 2)
       {
          Print("2");
       }
    }
    If you use condition bodys like this then the Print 1 will only happen if condition 1 happens, Print 2 will only happen if condition 1 and 2 are true.

    It is generally important to use { } to make sure your conditions are specific and apply to all of the lines you need which reside within the { }. This is very important if you need to execute multiple lines of code based on the condition being true.

    You can form conditions like your first sample if you need to make sure multiple conditions are all true at once, if condition 1 and 2 are true do the action:


    Code:
    if(condition1 && condition2)
    {
      Print("action 1");
      Print("action 2");
    }

    Please let me know if I may be of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you Jesse

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by bortz, 11-06-2023, 08:04 AM
      47 responses
      1,611 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      9 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      19 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      6 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      22 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X