#ifndef DECIDER_H_ #define DECIDER_H_ #include "helpers.h" #include "network.h" #include #include class decider { public: double start_money; unsigned start_stock; virtual int decide(double price, double money, unsigned stock) = 0; virtual void reset() = 0; virtual ~decider() { } }; class macd_decider { private: std::size_t limit; protected: buffer prices; buffer macd; buffer signal; public: macd_decider(std::size_t limit = 1000); virtual void process(double price); virtual void reset(); }; template class neural_decider : public decider, macd_decider { public: using network_t = network; using self_t = neural_decider; network_t network; neural_decider() : network(), macd_decider() { } neural_decider(typename network_t::normalizer_t normalizer) : network(normalizer), macd_decider() { } virtual int decide(double price, double money, unsigned stock) { process(price); auto input = prepare(money, stock); auto result = network.evaluate(input); double buy = result.get(0, 0); double sell = result.get(1, 0); double amount = (buy - sell - .5)/1.5; return abs(buy - sell) <= .5 ? 0 : amount * start_stock / 10; } std::ostream& save(std::ostream& stream) { network.save(stream); return stream; } void load(std::istream& stream) { network.load(stream); } virtual void reset() override { macd_decider::reset(); } virtual ~neural_decider() { } self_t combine(self_t smth, typename network_t::combiner_t combiner = [](const double& a, const double& b) { return .35*a + .65*b; }) { self_t result; result.network = network.combine(smth.network, combiner); result.start_money = start_money; result.start_stock = start_stock; return result; } private: typename network_t::input prepare(double money, unsigned stock) { vector state = { { (money - start_money) / start_money }, { (double)stock / this->start_stock } }; vector prices; vector tech; for (int j = 0; j < p; j++) { prices.set(j, 0, this->prices[j]); } int i = 0; for (int j = 0; j < m; j++, i++) { tech.set(i, 0, macd[j]); } for (int j = 0; j < s; j++, i++) { tech.set(i, 0, signal[j]); } // prices are normalized in their domain prices = normalize(prices); // analytic data is normalized in its own domain tech = normalize(tech); auto concated = concat(prices, tech); return concat(concated, state); } }; #endif