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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      576 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      334 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      553 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X