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 CaptainJack, 05-29-2026, 05:09 AM
      0 responses
      618 views
      0 likes
      Last Post CaptainJack  
      Started by CaptainJack, 05-29-2026, 12:02 AM
      0 responses
      419 views
      0 likes
      Last Post CaptainJack  
      Started by charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      293 views
      1 like
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      414 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      367 views
      0 likes
      Last Post CarlTrading  
      Working...
      X