Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to HOLD a value in "OnBarUpdate"..

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

    How to HOLD a value in "OnBarUpdate"..

    I have a dot plot on the upper line of a Donchian Channel when it slopes down.
    I need it to maintain the plot level until it slopes down again.

    protected override void OnBarUpdate()
    {
    Value.Set((MAX(High, Period)[0] + MIN(Low, Period)[0]) / 2);
    Upper.Set(MAX(High, Period)[0]);
    Lower.Set(MIN(Low, Period)[0]);

    if((MAX(High, Period)[0]) > (MAX(High, Period)[-1]))
    {
    Dip.Set(MAX(High, Period)[-1]);
    }
    else
    {
    ** Not sure what needs to go here to maintain last value ???
    It simply doesn't plot anything if the above "IF" statement is not utilized.
    {
    }

    Thx in advance for any guidance...

    #2
    Hello Randwulf,

    Thank you for the post.

    One problem I can see is that you are using Negative BarsAgo, the BarsAgo system works with Positive numbers. You should change the [-1] to [1] if you meant 1 BarsAgo or the previous bar.

    To retain the value you could likely use the prior plot value for this purpose:

    Code:
    Dip[0] = Dip[1];
    You would also need to check that you have at least 1 bar before doing a BarsAgo check of 1 bar:

    Code:
    if(CurrentBar < 1) 
    {
    Dip[0] = some value for the first bar;
    } else {
     // logic you have shown in the sample 
    }

    I look forward to being of further assistance.


    Comment


      #3
      Thank you for the timely reply Jesse.
      I knew it should be positive numbers for "bars back" but couldn't seem to get it to work.
      I will continue to correct this.
      In the meantime, I simply created a "double" variable and assign the change to it and on each bar update it assigns the plot value to it.
      Great idea about insuring I have at least 1+ bars first... It's been a long time since I've dabbled in NinjaScript and had forgotten that.
      Thanks again for your ever appreciated support...

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      559 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 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
      546 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X