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 XXtrader, Yesterday, 11:30 PM
      2 responses
      11 views
      0 likes
      Last Post XXtrader  
      Started by Waxavi, Today, 02:10 AM
      0 responses
      6 views
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Started by TradeForge, Today, 02:09 AM
      0 responses
      11 views
      0 likes
      Last Post TradeForge  
      Started by Waxavi, Today, 02:00 AM
      0 responses
      2 views
      0 likes
      Last Post Waxavi
      by Waxavi
       
      Started by elirion, Today, 01:36 AM
      0 responses
      7 views
      0 likes
      Last Post elirion
      by elirion
       
      Working...
      X