Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Update Series after specific price break, not OnBarUpdate

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

    Update Series after specific price break, not OnBarUpdate

    Hey,

    I want to code pivot breakout indicator, and store previous pivot values inside series. How can I store data in Series, that it would only update when certain price point is broken, like:
    if (High[0] > PivotHigh[0])
    {
    PivotHigh[1] = PivotHigh[0]; // Optional, unless it automatically stores new value into the series, so this step may not be necessary.
    PivotHigh[0] = High[0];
    }

    And until next time, when High[0] > PivotHigh[0], PivotHigh[0] would remain the same, no matter how many bars passed.

    #2
    Hello UltraNIX,

    Series work by storing 1 value per bar, if you wanted to keep the previous value you need to persist it on each bar. This would need to happen before your conditions :

    PivotHigh[0] = PivotHigh[1];

    That carries over the last value and sets the plot. If your logic becomes true then it would reset it.

    Comment


      #3
      That is ok, but my main question is quite opposite - how to avoid that "storing 1 value per bar" and only store value, when there's a "need' (i.e. change of PivotHigh)? So that PIvotHigh[1] would be previous PivotHigh, and not PivotHigh value of previous bar (which would in most cases be the same).

      Idea is like this:
      PivotHigh[0] - current pivot high
      PivotHigh[1] - previous pivot high
      PivotHigh[2] - 3rd last pivot high

      and so on.

      Comment


        #4
        Hello UltraNIX,

        - how to avoid that "storing 1 value per bar" and only store value, when there's a "need'
        You can technically do that with a series, you would just use a condition to set the value. On bars where the condition is false no value is set. If you check the series you need to know how many BarsAgo each instance was, you can see the Swing or Pivot indicators and the methods they use in their code as a few examples of looping to find valid data points in a series. That would be what is required to do that idea using a series.

        The alternative to looping and finding valid datapoints is to keep a continuous value until changed by starting each OnBarUpdate by setting the previous value and then if the conditions happen it updates the plot for that bar.
        An alternative would be to use variables:
        Code:
        private double PivotHigh;
        That would work like this:

        Code:
        PivotHigh =  current pivot high
        If you wanted more values ago, you would need more variables.

        The easiest C# way to do what you are asking would be a List<double> or an array: double[], those would index like your idea.






        Comment


          #5
          if I had to create as many values, as how much more bars ago I want to go, like (PivotHigh1, PivotHigh2, PivotHigh3), it's a tedious process.

          I heard that it is possible to create a dictionary and store like 2 values (for example, Time and PivotHigh), would that work? I don't know how to create dictionaries and would it work in this case, so I ask here for a help

          Comment


            #6
            Hello UltraNIX,

            A dictionary does store 2 values, if there was a second value that you needed to store as a way to look up the data that would work. For what you specifically asked for a List<double> matches that.

            These are both C# types so you can find many examples online of how to use each.

            Comment


              #7
              Again, does dictionary update on every bar, or, can it be set up to update only when needed?

              Comment


                #8
                Hello UltraNIX,

                That would depend on what you program, a dictionary is a C# type and has no specific NinjaScript/bar relation. The same would be for a List, that depends where you define the variable and where you use it/update it.

                I would suggest to review some external C# List and Dictionary examples to better understand how to use those types. If you feel that a dictionary would work for your use case you can use any C# types in NinjaScript.

                The specific example you provided matches a List<double> directly and not a dictionary. A dictionary would be used similar like the following MyObject[Key] where Key is not specifically an incremented index but could be any type, a List would be an incremented index like you asked for which contains instances of a value.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                50 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                126 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                69 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X