Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help on Stochastics(param).K[n]

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

    Need help on Stochastics(param).K[n]

    I try to code a stochastics divergence indicator and seem to have a problem with the Stochastics() indicator. The following works fine:

    public class StochDivergence : Indicator
    {
    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.DarkGreen), PlotStyle.Line, "Plot0"));
    CalculateOnBarClose = false;
    Overlay = false;
    PriceTypeSupported = false;
    }
    protected override void OnBarUpdate()
    {
    // +++++++++++++++++
    Values[0].Set(Stochastics(3,5,2).K[0]); //<=== This line
    // -------------------------------
    }
    }

    Above code works fine, stochastics %K is displayed on the chart..
    But if I change the K[0] to K[1] or other non-zero positive integer value, nothing is displayed on the chart. Stochastics().D[] has the same problem. It seems I can only access current bar's %D abd %K. (According to the manual, D[1] or K[3] should work.)

    What do I do wrong? Please help.

    Thanks

    #2
    Welcome to our forums - you just miss a simple check at the OnBarUpdate() start to make sure only valid bars are accesssed. Your indicator would likely throw an error to the log if you tried accessing prior bars -

    Comment


      #3
      Thank you very much for the help. It makes perfect sense.

      Comment

      Latest Posts

      Collapse

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