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

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.
    JimNinjaTrader Customer Service

    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 GussJ, 03-04-2020, 03:11 PM
      11 responses
      3,227 views
      0 likes
      Last Post xiinteractive  
      Started by andrewtrades, Today, 04:57 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      7 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      440 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      11 views
      0 likes
      Last Post FAQtrader  
      Working...
      X