How To Compute Stochastic Oscillator in Erlang?

Member

by hanna , in category: SEO Tools , 5 months ago

How To Compute Stochastic Oscillator in Erlang?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by julio , 5 months ago

@hanna 

To compute the Stochastic Oscillator in Erlang, you can use the following steps:

  1. Calculate the highest high and lowest low prices for the desired period (typically 14 days).
  2. Calculate the current closing price.
  3. Calculate the %K value using the formula: %K = ((current closing price - lowest low price) / (highest high price - lowest low price)) * 100
  4. Calculate the %D value by smoothing the %K value over a specified period (usually 3 days) using a simple moving average: %D = (Sum of %K values over the last 3 days) / 3
  5. You can now use the calculated %K and %D values to analyze the market conditions and make trading decisions.


Here's a simple Erlang function that demonstrates how to compute the Stochastic Oscillator:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-define(PERIOD, 14).
-define(SMA_PERIOD, 3).

stochastic_oscillator(Prices) ->
    High = lists:sublist(Prices, ?PERIOD),
    Low = lists:sublist(Prices, ?PERIOD),
    {HighPrice, _} = lists:max(High),
    {LowPrice, _} = lists:min(Low),
    CurrentPrice = lists:last(Prices),
    K = ((CurrentPrice - LowPrice) / (HighPrice - LowPrice)) * 100,
    D = calculate_sma([K | lists:sublist(Prices, ?SMA_PERIOD)], ?SMA_PERIOD),
    {K, D}.

calculate_sma(Values, Period) ->
    {Sum, _} = lists:foldl(fun(X, {Acc, Count}) -> {X + Acc, Count + 1} end, {0, 0}, Values),
    Sum / Period.


You can call the stochastic_oscillator function with a list of price values for a specific asset to calculate the %K and %D values. Remember to adjust the PERIOD and SMA_PERIOD constants as needed.