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

Evaluate Only Once

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

    Evaluate Only Once

    Hello,

    So, I have a bunch of lines on my chart with equally spaced y values, imagine a line at every $10 for the sake of simplicity, and right now I have them structured into an array in order to do certain things with them.

    One of the things I am doing is determining which line was last hit by using
    Code:
    for (i = 0; i < 13; i++)  {if (Closes[1][0] == array[i]) {lastHit = i; }}
    That will give me a number which corresponds to a line. My question is regarding my goal of being able to count how many times price touched each line over the course of the day. The issue that I am currently having is that I only want the count to increase the first time it is touched, which is to say that if price touches any given line, the count for that line should be 1, and it should not increase to 2 until the price has touched the line above or below it and returned to the original line, making count = 2 and whichever other line price touched count = 1.

    How can I somehow store the last value that this calculation yielded and ensure it is only counting when the value has changed?

    And tangentially, how can I ascribe the different values of i their own count value using the array. I know I could do this easily outside the context of the array, but elegant > inelegant, right?

    I hope I have provided enough information and I am looking forward to your responses.

    Thank you.

    #2
    Hi lunardiplomacy, thanks for your note.

    You would need a counter in the data structure and increment it when the CrossAbove/CrossBelow methods are true. Evaluate if the price crossed above or below on each bar and if one of those are true add a point to the counter.

    https://ninjatrader.com/support/helpGuides/nt8/?crossabove.htm - CrossAbove()
    https://ninjatrader.com/support/help...crossbelow.htm - CrossBelow()

    Your array would need to hold two values for each element in this case. so a price and a counter. You can make a struct for that and make an array of the struct type :



    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris, thanks for getting back to me.

      Right, two values for each array element is exactly what I was looking for. I am reading this microsoft documentation, and if I am understanding correctly then this is pretty interesting and I didn't know it was possible.

      What still isn't clear to me exactly however, is how to ensure that a point is only added to the count the first time the condition is true (whether it is closes[1][0] == line or CrossAbove/Below (line)), because of course, if I am counting every time the line is hit and the price happens to be hovering around that particular line, my count is going to be a ridiculously high number and meaningless.

      The above issue also relates to another query I have, which is generally how to store and refer to the last value of a variable. In order to store the values of a variable I am calculating I think I am supposed to be using a series<T> object? But even once I create a series for the variable I am calculating, I am not sure how to refer to the last value it calculated, as in not the one it JUST calculated, but the one PRIOR to that.

      For example in much the same way that if I add a Print statement to my script, I can see the values print in the output window. If I am calculating on each tick (which I am, most of the time) then the output will print the same value repeatedly until it changes. Meaning I can't use Series[1] to refer to the last value of the series, because that will just give me the value of the last instance of OnBarUpdate, which is likely to be the same value and not the one I want (the previous value).

      I think that is everything. I am sorry if I didn't explain anything well or if I repeated myself.

      Thanks for you help.

      Comment


        #4
        Maybe a more apt way to ask this question is, how do I drop consecutive duplicate values from a series?

        Comment


          #5
          Hello lunardiplomacy,

          This is Jim, responding on behalf of Chris who is out of the office at this time.

          If you are looking to track when something happens once, I suggest to use a bool. For example, initialize your bool "doOnce" with a value of true. If "doOnce" is true and your condition to check something is true, take your action and assign "doOnce" to a value of false. Upon some other condition, I.E on IsFirstTickOfBar, set the bool back to true so your "doOnce" condition can be checked again for that bar.

          Series objects are meant to hold a collection of values that are synchronized with each bar that the Series object is synched to. This does not necessarily mean the MySeries[1] will always represent the last value you gave it. For example, when using Calculate.OnEachTick or Calculate.OnPriceChange, the incoming realtime data will constantly update MySeries[0]. This does not mean MySeries[1] will be the last value you assigned.

          Once a new bar forms, then MySeries[0] will shift to MySeries[1].

          For storing the last value of a variable, I suggest creating a new variable called "lastValue" whenever you update your variable to a new value, assign "lastValue" the value of your variable, before you update your variable.

          We look forward to assisting.

          JimNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by kevinenergy, 02-17-2023, 12:42 PM
          118 responses
          2,777 views
          1 like
          Last Post kevinenergy  
          Started by briansaul, Today, 05:31 AM
          0 responses
          4 views
          0 likes
          Last Post briansaul  
          Started by fwendolynlpxz, Today, 05:19 AM
          0 responses
          4 views
          0 likes
          Last Post fwendolynlpxz  
          Started by traderqz, Yesterday, 12:06 AM
          11 responses
          28 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by PaulMohn, Today, 03:49 AM
          0 responses
          8 views
          0 likes
          Last Post PaulMohn  
          Working...
          X