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

two bits of code with different results - why?

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

    two bits of code with different results - why?

    I have rewritten the same entry code in two different ways expecting the entries to be identical but the backtest results are different. Can anyone suggest why this might be?

    First entry code:
    Code:
                if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                {                    
                    if(direction == "down")
                    {                    
                        if(Stochastics(BarsArray[0],3,5,3).K[0] > 80)
                        {    
                            EnterShort(shares, "E1");
                        }
                    }
                }
                if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                {        
                    if(direction == "up")
                    {                    
                        if(Stochastics(BarsArray[0],3,5,3).K[0] < 20)
                        {    
                            EnterLong(shares,"E1");
                        }
                    }
                }
    Second entry code:

    Code:
                if (ToTime(Time[0]) > start && ToTime(Time[0]) < (start + end))
                {
                    //if(Position.MarketPosition == MarketPosition.Flat)
                    //{
                        if(direction == "down")
                        {    
                            if(Stochastics(BarsArray[0],3,5,3).K[0] > 80)
                            {    
                                EnterShort(shares, "E1");
                            }
                        }
                        else if(direction == "up")
                        {                    
                            if(Stochastics(BarsArray[0],3,5,3).K[0] < 20)
                            {    
                                EnterLong(shares,"E1");
                            }
                        }                    
                        
                    //}
                }

    #2
    Hello,

    Thank you for the question.

    It looks like logically these look similar but I do see these are different.

    In the first case you are executing One and then the Other, the second sample you are executing One or the Other

    The change is with your if statement for direction, the second sample demonstrates the use of else which means that only 1 of these conditions can happen. In the first example you are not using an else which means both could potentially be executed.

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

    Comment


      #3
      Brilliant! I never would have figured that out. Thanks a lot!!!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DJ888, Today, 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, Today, 09:29 PM
      0 responses
      7 views
      0 likes
      Last Post Belfortbucks  
      Started by zstheorist, Today, 07:52 PM
      0 responses
      7 views
      0 likes
      Last Post zstheorist  
      Started by pmachiraju, 11-01-2023, 04:46 AM
      8 responses
      151 views
      0 likes
      Last Post rehmans
      by rehmans
       
      Working...
      X