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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      129 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      74 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      116 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      111 views
      1 like
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      89 views
      0 likes
      Last Post CarlTrading  
      Working...
      X