← Back to Documentations

Signature Description Parameters
#include <DataFrame/DataFrameFinancialVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct StochasticOscillatorVisitor;

// -------------------------------------

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using stoch_v = StochasticOscillatorVisitor<T, I, A>;
This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This visitor calculates the Stochastic Oscillator, which consists of two lines, %K and %D. It requires 3 input columns in the order of low, high, close. %K shows where the current close sits within the range between the lowest low and the highest high of the last k_period bars. It is scaled from 0 to 100. A value of 100 means the close is at the top of the recent range, and 0 means it is at the bottom. %D is a simple moving average of %K over d_period bars. It is a smoother, slower version of %K and is used as its signal line.

get_result() and get_pct_k() return %K.
get_pct_d() returns %D.
Both results are vectors with the same number of items as the given columns. The first k_period - 1 items of %K are NaN. The first k_period + d_period - 2 items of %D are NaN. If the highest high equals the lowest low over a window (a perfectly flat range), %K for that bar is 0.
explicit
StochasticOscillatorVisitor(size_type k_period = 14, size_type d_period = 3);

k_period: Look-back window for the highest high and lowest low
d_period: Rolling window of the moving average applied to %K to get %D
        
T: Column data type
I: Index type
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_StochasticOscillatorVisitor()  {

    std::cout << "\nTesting StochasticOscillatorVisitor{ } ..." << std::endl;

    StrDataFrame    df;

    try  {
        df.read("IBM.csv", io_format::csv2);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
        ::exit(-1);
    }

    stoch_v<double, std::string>    stoch;

    df.single_act_visit<double, double, double>("IBM_Low", "IBM_High", "IBM_Close", stoch);

    // Result is %K, which is the same as get_pct_k()
    //
    assert(stoch.get_result().size() == 5031);
    assert(std::isnan(stoch.get_result()[0]));
    assert(std::isnan(stoch.get_result()[12]));
    assert(std::abs(stoch.get_result()[13] - 95.8763) < 0.0001);
    assert(std::abs(stoch.get_result()[16] - 65.1961) < 0.0001);
    assert(std::abs(stoch.get_result()[23] - 89.0) < 0.001);
    assert(std::abs(stoch.get_result()[25] - 7.0) < 0.001);
    assert(std::abs(stoch.get_result()[30] - 12.4444) < 0.0001);
    assert(std::abs(stoch.get_result()[5022] - 2.84034) < 0.0001);
    assert(std::abs(stoch.get_result()[5029] - 13.3901) < 0.0001);
    assert(std::abs(stoch.get_result()[5030] - 26.7849) < 0.0001);

    assert(stoch.get_pct_k().size() == 5031);
    assert(std::isnan(stoch.get_pct_k()[0]));
    assert(std::isnan(stoch.get_pct_k()[12]));
    assert(std::abs(stoch.get_pct_k()[13] - 95.8763) < 0.0001);
    assert(std::abs(stoch.get_pct_k()[16] - 65.1961) < 0.0001);
    assert(std::abs(stoch.get_pct_k()[23] - 89.0) < 0.001);
    assert(std::abs(stoch.get_pct_k()[25] - 7.0) < 0.001);
    assert(std::abs(stoch.get_pct_k()[30] - 12.4444) < 0.0001);
    assert(std::abs(stoch.get_pct_k()[5022] - 2.84034) < 0.0001);
    assert(std::abs(stoch.get_pct_k()[5029] - 13.3901) < 0.0001);
    assert(std::abs(stoch.get_pct_k()[5030] - 26.7849) < 0.0001);

    assert(stoch.get_pct_d().size() == 5031);
    assert(std::isnan(stoch.get_pct_d()[0]));
    assert(std::isnan(stoch.get_pct_d()[14]));
    assert(std::abs(stoch.get_pct_d()[15] - 67.9065) < 0.0001);
    assert(std::abs(stoch.get_pct_d()[16] - 57.6797) < 0.0001);
    assert(std::abs(stoch.get_pct_d()[23] - 56.9627) < 0.0001);
    assert(std::abs(stoch.get_pct_d()[25] - 44.0) < 0.001);
    assert(std::abs(stoch.get_pct_d()[30] - 9.81481) < 0.00001);
    assert(std::abs(stoch.get_pct_d()[5022] - 28.5447) < 0.0001);
    assert(std::abs(stoch.get_pct_d()[5029] - 6.09002) < 0.00001);
    assert(std::abs(stoch.get_pct_d()[5030] - 14.3247) < 0.0001);
}

C++ DataFrame