107 lines
2.3 KiB
C++
107 lines
2.3 KiB
C++
#ifndef DECIDER_H_
|
|
#define DECIDER_H_
|
|
|
|
#include "helpers.h"
|
|
#include "network.h"
|
|
#include <iterator>
|
|
#include <iostream>
|
|
|
|
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<double> prices;
|
|
buffer<double> macd;
|
|
buffer<double> signal;
|
|
|
|
public:
|
|
macd_decider(std::size_t limit = 1000);
|
|
|
|
virtual void process(double price);
|
|
virtual void reset();
|
|
};
|
|
|
|
template <int p, int m, int s, int ...layers>
|
|
class neural_decider : public decider, macd_decider {
|
|
public:
|
|
using network_t = network<double, p+m+s+2, layers..., 2>;
|
|
network_t network;
|
|
|
|
double start_money;
|
|
unsigned start_stock;
|
|
|
|
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;
|
|
}
|
|
|
|
virtual void reset()
|
|
{
|
|
this->reset();
|
|
}
|
|
|
|
virtual ~neural_decider() { }
|
|
|
|
private:
|
|
typename network_t::input prepare(double money, unsigned stock)
|
|
{
|
|
vector<double, 2> state = {
|
|
{ (money - start_money) / start_money },
|
|
{ (double)stock / this->start_stock }
|
|
};
|
|
|
|
vector<double, p> prices;
|
|
vector<double, m+s> 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
|