Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Swing High and Swing Low Buying Condition Code

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

    Swing High and Swing Low Buying Condition Code

    Hey,

    I'm coding a strategy and one of the conditions I want to be met is that I have a kind of Swing High before shorting or a kind of Swing Low before buying.

    For example, two red bars required before placing the trade to short, or two green bars required before placing the trade to buy.

    What is the code I should use for this?

    Any help would be greatly appreciated!

    Kind regards,
    Stephen

    #2
    Hello Stephen Trading,

    Thanks for the post.

    I believe in this case you are describing a rising or falling trend, or where the price is greater than the previous for a number of bars or less for a number of bars.

    If that is what you wanted you could do that in two ways. For single or 1 bars ago periods you can use the IsRising or IsFalling
    https://ninjatrader.com/support/help...tsub=IsFalling

    For more than 1 BarsAgo you could just use the BarsAgo and price series. For example

    Code:
    Close[0] > Close[1] && Close[1] > Close[2]
    I look forward to being of further assistance.

    Comment


      #3
      Hey,

      Thank you for getting back to me!

      This is a great idea!

      I previously had just this as my code:

      Close[0] > Close[1]

      Didn't think about adding a separate condition in order to get another.

      Thank you,
      Stephen

      Comment


        #4
        Hey,

        There is a slight problem when I change the code to:

        (Close[0] < Close[1]) ; The strategy then doesn't make any trades

        I previously had it at:

        (Close[0] < Close[2]) ; In which case it does make trades

        I tested and isolated this to be the problem, why is that? And is it possible to fix it?

        Kind regards,
        Stephen

        Comment


          #5
          Hello Stephen Trading,

          From the given samples there is not really a discernible difference there besides the BarsAgo. Can you try Printing the values to see if its just that the condition is never true based on the values seen?

          Code:
          Print(Close[0] + " < " + Close[1]); 
          (Close[0] < Close[1]) ;
          One item I do see here is that you have a semi colon after the condition, if that's how it actually is in your code that's likely part of the problem. if conditions don't have any semicolons used:

          Code:
          if(condition)
          { 
          
          }

          I look forward to being of further assistance.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          62 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          134 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X