#include "../misc_framework/StandardPlaceholders.h" #include "../misc_framework/GenericDataNodes.h" #include "StandardCandles.h" #include "IntradaySma.h" //////////////////////////////////////////////////////////////////// // IntradaySma //////////////////////////////////////////////////////////////////// class IntradaySma : GenericDataNode { private: // These are only set in the constructor. StandardCandles *_candleData; int _candles; // These are used to cache the result. mutable StandardCandles::Epoch _lastEpoch; mutable bool _valid; mutable double _value; void checkForUpdate() const; IntradaySma(DataNodeArgument const &args); friend class GenericDataNodeFactory; public: void getDouble(bool &valid, double &value) const; std::string getDebugInfo() const; }; std::string IntradaySma::getDebugInfo() const { checkForUpdate(); TclList result; result<historicalCandleCount() <<"epoch" <<_candleData->getEpoch() <<"_lastEpoch" <<_lastEpoch <<"_value"; if (_valid) result<<_value; else result<<"invalid"; return result; } void IntradaySma::checkForUpdate() const { if (_candleData->getEpoch() == _lastEpoch) return; _lastEpoch = _candleData->getEpoch(); if (_candleData->historicalCandleCount() < _candles) { _valid = false; _value = 0.0; } else { double total = 0.0; StandardCandles::CandleArray const &data = _candleData->getHistory(); for (int i = _candleData->historicalCandleCount() - _candles; i < _candleData->historicalCandleCount(); i++) total += data[i].close; _value = total / _candles; _valid = true; } } IntradaySma::IntradaySma(DataNodeArgument const &args) : _lastEpoch(0), _valid(false) { DataNodeArgumentVector const &argList = args.getListValue(); assert(argList.size() == 4); // (Symbol, Minutes/Bar, Bars, Strict) std::string const &symbol = argList[0].getStringValue(); int minutesPerBar = argList[1].getIntValue(); _candles = argList[2].getIntValue(); const bool strict = argList[3].getBooleanValue(); assert(_candles > 0); addAutoLink(StandardCandles::find(this, 0, _candleData, symbol, minutesPerBar, strict)); } void IntradaySma::getDouble(bool &valid, double &value) const { checkForUpdate(); valid = _valid; value = _value; } //////////////////////////////////////////////////////////////////// // Global //////////////////////////////////////////////////////////////////// DataNodeArgument createIntradaySmaFactory(DataNode::Integer minutes, DataNode::Integer bars, bool strict) { return GenericDataNodeFactory::create (argList(symbolPlaceholderObject, minutes, bars, strict)); }