Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Logic Error

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

    Logic Error

    Hello NT Support,

    The following is from a Ninjascript I recently optimized. The optimized results were entry2_sl = 1 and entry2_slx =1. Logically there should not be any trades resulting since both Close[0]>Close[1] and Close[0]<Close[1] cannot be true at the same time. Yet, I am getting trade results from these conditions. Any idea what is happening?

    [NinjaScriptProperty]
    public int entry2_sl
    { get; set; }
    [NinjaScriptProperty]
    public int entry2_slx
    { get; set; }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;

    // Input Variables
    int entry2_sl = 5, entry2_slx = 20;
    // entry2_sl = short term trend length
    // entry2_slx = longer term trend length

    if ((Close[0] < Close[entry2_sl])
    && (Close[0] > Close[entry2_slx]))
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Entry02Long");


    #2
    Hello harr5754,

    Thanks for your post.

    It looks like you have created 2 class level variables, but then created 2 method level variables with the same name. Since entry2_sl, and entry2_slx are used in the condition, they will use the values from the method level as that is nearest within scope.

    A publicly available resource on variable scope may be found here - https://www.geeksforgeeks.org/scope-...es-in-c-sharp/

    Prints can always give a hint of why a condition evaluated as true/false. If you print out Close[entry2_slx], Close[entry2_sl], and Close[0], just before that condition evaluated, you can see why it becomes true, and you can also see that Close[entry2_slx] and Close[entry2_sl] have different values.

    We look forward to assisting.

    Comment


      #3
      Thanks, Jim. I removed the method level variables and the logic seems to be working now.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      53 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      70 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X