Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IF Condition not working?

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

    IF Condition not working?

    Hello,

    I am testing a strategy that uses Keltner Channels. The summary of this strategy is that, when the price is equal to the midline of the Keltner Channel, the action will be to place a buy order around the lower channel. For some reason, I cannot get the IF condition to trigger the action.

    My logic is currently this: "GetCurrentAsk(Default input) = KeltnerChannel(Open, 1, 10).Midline[0]"

    This is stating that when the Current Ask Price is equal to the Midline of the Keltner channel, the action will get triggered (Buy Order by the Lower Channel should be placed)

    The Action statement works by itself, but with the IF condition, it will not place the order ever even though this scenario happens many times a day.

    Any thoughts? Thanks!

    #2
    Hello tradingbobby,
    Try below code:-
    Code:
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    // Set 1
    if (GetCurrentAsk(0) == KeltnerChannel1.Midline[0])
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }
    You can use Strategy Builder & get it working.

    Comment


      #3
      Hello tradingbobby,

      Thanks for your post and welcome to the NinjaTrader forums!

      The test for equality may fail because you are comparing a price level against a calculated value (for the Keltner line) For example, while the price might be 1304.25 the midline might be at 1304.250001 and these would not be equal values. You could add a print statement to print out both the price value and the value of the midline to verify if this is the case.

      You could instead test to see if price has crossed above or crossed below the Midline.

      You could instead test to see if the ask is >= (greater or equal) or <= (less than or equal) to the midline.

      You could instead round the Midline price to match the price using C#..Net Math.Round(). Please see https://docs.microsoft.com/en-us/dot...ew=netcore-3.1

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      67 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      143 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      76 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      47 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      51 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X