Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Did Price Trade At This Level X Bars Ago?

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

    Did Price Trade At This Level X Bars Ago?

    Does anyone have an idea of how to write up code to be true if:

    "At any time, price traded at 1.4320 over the last 20 bars"

    IOW, I want to return true if price has ever hit 1.4320 in the last X amount of bars.

    It would be easy to do this just based on Close... but I want to do this based on Bid or Ask... so basically (I assume) I'd have to do this at the tick level...

    Thoughts?

    #2
    Hi trend747,

    There's a few built in methods that check conditions over a lookback to return a useful value.

    CountIf() method.


    MRO:


    LRO:


    When looking at historical bars, you only have four price points to work from: OHLC. These methods will not work in a multiseries framework which you may need to work at the tick level. Similar functionality is possible through custom coding by iterating through previous bars.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Yes, there's not exactly one way to do it, and the best technique varies depending on whether you need this historically/real time or within multiseries framework.

      For single series, CountIf() works well to check occurrence of a condition. I'd use something like this to check bars over a lookback to see if they contain the specified price:

      if (CountIf(delegate {return Low[0] <= 1.4320 && High[0] >= 1.4320 ;}, 20) > 0)
      Print("There's been at least one occurence of my test condition");
      Last edited by NinjaTrader_RyanM1; 07-27-2011, 08:32 AM.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by trend747 View Post
        Does anyone have an idea of how to write up code to be true if:

        "At any time, price traded at 1.4320 over the last 20 bars"

        IOW, I want to return true if price has ever hit 1.4320 in the last X amount of bars.

        It would be easy to do this just based on Close... but I want to do this based on Bid or Ask... so basically (I assume) I'd have to do this at the tick level...

        Thoughts?
        Check for Highest High and Lowest Low. If the reference price is between them, then the reference price must have been traded through at least once.

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          Yes, there's not exactly one way to do it, and the best technique varies depending on whether you need this historically/real time or within multiseries framework.

          For single series, CountIf() works well to check occurrence of a condition. I'd use something like this to check bars over a lookback to see if they contain the specified price:

          if (CountIf(delegate {return Low[0] >= 1.4320 && High[0] <= 1.4320 ;}, 20) > 0)
          Print("There's been at least one occurence of my test condition");
          Ryan, should that not be the other way around?

          if (CountIf(delegate {return Low[0] <= 1.4320 && High[0] >= 1.4320 ;}, 20) > 0)

          Comment


            #6
            Hi koganam,

            Yes, you're right. Thanks for the spot and post corrected above.
            Last edited by NinjaTrader_RyanM1; 07-27-2011, 08:32 AM.
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            571 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            549 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            549 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X