How To Compute Stochastic Oscillator using C#?

Member

by darion , in category: Third Party Scripts , 2 months ago

How To Compute Stochastic Oscillator using C#?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , a month ago

@darion 

Here is an example of how you can compute the Stochastic Oscillator using C#:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;

class StochasticOscillator
{
    public static List<double> ComputeStochasticOscillator(List<double> closingPrices, int period)
    {
        List<double> stochasticOscillator = new List<double>();

        for (int i = period; i < closingPrices.Count; i++)
        {
            double minPrice = closingPrices.GetRange(i - period, period).Min();
            double maxPrice = closingPrices.GetRange(i - period, period).Max();

            double currentPrice = closingPrices[i];
            
            double stochasticValue = ((currentPrice - minPrice) / (maxPrice - minPrice)) * 100;
            
            stochasticOscillator.Add(stochasticValue);
        }

        return stochasticOscillator;
    }

    static void Main()
    {
        List<double> closingPrices = new List<double> { 10, 12, 15, 14, 16, 18, 20, 22, 24, 25, 28, 30 };
        int period = 5;
        
        List<double> stochasticOscillator = ComputeStochasticOscillator(closingPrices, period);

        foreach (var value in stochasticOscillator)
        {
            Console.WriteLine(value);
        }
    }
}


In this example, we have a ComputeStochasticOscillator method that takes a list of closing prices and a period as parameters. It then calculates the Stochastic Oscillator values for each closing price based on the period provided. The main method demonstrates how to use this method with sample closing prices and a period of 5.