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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      93 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      138 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      123 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      73 views
      0 likes
      Last Post PaulMohn  
      Working...
      X