Announcement

Collapse
No announcement yet.

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.

    Comment


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

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      149 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      85 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      129 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      125 views
      1 like
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      102 views
      0 likes
      Last Post CarlTrading  
      Working...
      X