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 DanielTynera, Today, 01:14 AM
      0 responses
      2 views
      0 likes
      Last Post DanielTynera  
      Started by yertle, 04-18-2024, 08:38 AM
      9 responses
      40 views
      0 likes
      Last Post yertle
      by yertle
       
      Started by techgetgame, Yesterday, 11:42 PM
      0 responses
      12 views
      0 likes
      Last Post techgetgame  
      Started by sephichapdson, Yesterday, 11:36 PM
      0 responses
      2 views
      0 likes
      Last Post sephichapdson  
      Started by bortz, 11-06-2023, 08:04 AM
      47 responses
      1,615 views
      0 likes
      Last Post aligator  
      Working...
      X