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 Haiasi, 04-25-2024, 06:53 PM
      2 responses
      16 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      4 views
      0 likes
      Last Post Creamers  
      Started by Segwin, 05-07-2018, 02:15 PM
      12 responses
      1,785 views
      0 likes
      Last Post Leafcutter  
      Started by poplagelu, Today, 05:00 AM
      0 responses
      3 views
      0 likes
      Last Post poplagelu  
      Started by fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,407 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Working...
      X