#ifndef __StochasticFormula_h_ #define __StochasticFormula_h_ /* This is the formula for stochastics. This should be easy to insert into a * data node, or into another environmnet without data nodes. This formula * makes very few assumptions or includes. * * We have two basic operations. You can add a day. (A.k.a. a period or a * bar.) Or you can delete some number of days off of the end. That way we * can do a decent amount of caching. We don't recompute the entire thing * each time you add a single day. If the last day is constantly changing * (because you are computing the formula before the day ends) just delete * the last day from this object, and add a new value for it. * * This was originally designed to work with IntradayStochastic.C. However, * this could easily be adjusted to work with daily stochastics or the * stochastic RSI formula. */ #include class StochasticFormula { private: int _rangeCount, _pKSmaCount, _pDSmaCount; bool _inputsAreValid; // List of partial results. These can contain NAN for missing values. struct Row { double high; double low; double positionInRange; double firstSma; }; std::vector< Row > _data; // Cache the last instance of the second SMA. We could compute and store it // for every row, but that would be wasteful. mutable double _secondSma; // This can be NAN, like most items in a row. mutable bool _secondSmaValid; // This means that the cache is valid and does not need to be recomputed. The value might or might not be NAN. public: StochasticFormula(); StochasticFormula(int rangeCount, int pKSmaCount, int pDSmaCount); // Ideally these would be set in the constructor. However that's not always // possible. setParams() automatically calls reset(). void setParams(int rangeCount, int pKSmaCount, int pDSmaCount); bool inputsAreValid() const { return _inputsAreValid; } int getDayCount() const; // How much history do we have. void limitDayCount(int days); // Possibly cut days, but never add them. void reset() { limitDayCount(0); } // Return to start. void addDay(double high, double low, double close); void getValues(bool &valid, double &pK, double &pD) const; }; #endif