wiele poprawek

This commit is contained in:
Kacper Donat 2018-03-18 21:31:48 +01:00
parent 9bc85bc995
commit 35c3d4f936
36 changed files with 19629 additions and 149 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
*.exe
*.lst
*.tmp
*.pdb
# tex
*.aux

View File

@ -12,10 +12,10 @@ all: macd.exe tester.exe trainer.exe
macd.exe: macd.obj
$(link) $(ldebug) $(conflags) /out:macd.exe macd.obj $(conlibs)
tester.exe: tester.obj decider.obj
tester.exe: tester.obj decider.obj simulator.obj
$(link) $(ldebug) $(conflags) /out:tester.exe $** $(conlibs)
trainer.exe: trainer.obj decider.obj
trainer.exe: trainer.obj decider.obj simulator.obj
$(link) $(ldebug) $(conflags) /out:trainer.exe $** $(conlibs)
clean:

View File

@ -1,4 +1,5 @@
#include "network.h"
#include "decider.h"
using current_decider = neural_decider<24, 12, 12, 32, 16>;
// 4 prices, 4 macd, 4 signal, 4 feedback loop
using current_decider = neural_decider<6, 8, 8, 4, 8, 8>;

1260
data/11bit.dat Normal file

File diff suppressed because it is too large Load Diff

1260
data/amazon.dat Normal file

File diff suppressed because it is too large Load Diff

1260
data/apple.dat Normal file

File diff suppressed because it is too large Load Diff

1260
data/dow30.dat Normal file

File diff suppressed because it is too large Load Diff

1261
data/fb.dat Normal file

File diff suppressed because it is too large Load Diff

1001
data/google.dat Normal file

File diff suppressed because it is too large Load Diff

1260
data/nasdaq.dat Normal file

File diff suppressed because it is too large Load Diff

1290
data/plnchf.dat Normal file

File diff suppressed because it is too large Load Diff

1290
data/plneur.dat Normal file

File diff suppressed because it is too large Load Diff

1290
data/plngbp.dat Normal file

File diff suppressed because it is too large Load Diff

1287
data/plngld.dat Normal file

File diff suppressed because it is too large Load Diff

1293
data/plnjpy.dat Normal file

File diff suppressed because it is too large Load Diff

1281
data/plnrub.dat Normal file

File diff suppressed because it is too large Load Diff

1290
data/plnusd.dat Normal file

File diff suppressed because it is too large Load Diff

1260
data/tesla.dat Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,11 +6,6 @@
#include <iterator>
#include <iostream>
template <typename T>
T sign(const T& value) {
return value < 0 ? -1 : (value > 0 ? value : 0);
}
class decider {
public:
double start_money;
@ -38,14 +33,16 @@ public:
virtual void reset(double price);
};
template <int p, int m, int s, int ...layers>
template <int p, int m, int s, int r, int ...layers>
class neural_decider : public decider, macd_decider {
public:
using network_t = network<double, p+m+s+2, layers..., 2>;
using self_t = neural_decider<p, m, s, layers...>;
using network_t = network<double, p+m+s+r, layers..., 2+r>;
using self_t = neural_decider<p, m, s, r, layers...>;
network_t network;
vector<double, r> feedback;
neural_decider() : network(), macd_decider() { }
neural_decider(typename network_t::normalizer_t normalizer)
: network(normalizer), macd_decider() { }
@ -63,6 +60,10 @@ public:
double amount = buy - sell;
amount -= sign(amount) * .5;
for (int i = 0; i < r; i++) {
feedback.set(i, 0, result.get(i + 2, 0));
}
return abs(buy - sell) <= .5 ? 0 : amount * start_stock;
}
@ -86,7 +87,6 @@ public:
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;
@ -133,8 +133,7 @@ private:
// analytic data is normalized in its own domain
tech = normalize(tech);
auto concated = concat(prices, tech);
return concat(concated, state);
return concat(concat(prices, tech), feedback);
}
};

View File

@ -6,6 +6,11 @@
using std::deque;
template <typename T>
int sign(const T& value) {
return value < 0 ? -1 : (value > 0 ? 1 : 0);
}
template <typename T, class iter>
T ema(iter begin, iter end)
{
@ -28,7 +33,6 @@ template <typename T>
class buffer : public deque<T>
{
using deque<T>::deque;
std::size_t limit;
public:

View File

@ -1,5 +1,7 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include "helpers.h"
#include "argh.h"
@ -10,30 +12,49 @@ int main(int argc, const char* argv[])
args.add_params({ "l", "low" });
args.add_params({ "h", "high" });
args.add_params({ "s", "signal" });
args.add_params({ "o", "output" });
args.parse(argc, argv);
unsigned low, high, s;
std::string output;
args({"l", "low"}, 12) >> low;
args({"h", "high"}, 26) >> high;
args({"s", "signal"}, 9) >> s;
args({"o", "output"}, "./intersections.csv") >> output;
double price;
std::cin >> price;
unsigned max = std::max({ low, high, s });
buffer<double> prices(max);
buffer<double> prices(max, price);
buffer<double> macd(max);
buffer<double> signal(max);
double price;
std::cout << "no,price,macd,signal,delta" << std::endl;
for (int i = 0; std::cin >> price; i++) {
std::ofstream intersections(output);
intersections << "no,x,action" << std::endl;
int i = 0, j = 0;
do {
prices.add(price);
double value = ema<double>(prices.begin(), prices.begin() + low) - ema<double>(prices.begin(), prices.begin() + high);
macd.add(value);
signal.add(ema<double>(macd.begin(), macd.begin() + s));
std::cout << i << "," << prices[0] << "," << macd[0] << "," << signal[0] << "," << prices[1] - prices[0] << std::endl;
}
if (sign(macd[0] - signal[0]) != sign(macd[1] - signal[1])) {
// zamiana
double x1 = i+1, x2 = i;
double y1 = macd[1], y2 = macd[0];
double y3 = signal[1], y4 = signal[0];
double a = abs(y2 - y1), b = abs(y4 - y3);
double x = (a * x2 + b * x1) / (a + b);
intersections << ++j << "," << x << "," << (macd[0] - signal[0] > 0 ? "buy" : "sell") << std::endl;
}
std::cout << ++i << "," << prices[0] << "," << macd[0] << "," << signal[0] << "," << prices[0] - prices[1] << std::endl;
} while(std::cin >> price);
}

134
macd/msft-intersect-a.tex Normal file
View File

@ -0,0 +1,134 @@
\draw [sell] (action-1|-G c1r1.north) -- (action-1|-G c1r3.south);
\draw [buy] (action-2|-G c1r1.north) -- (action-2|-G c1r3.south);
\draw [sell] (action-3|-G c1r1.north) -- (action-3|-G c1r3.south);
\draw [buy] (action-4|-G c1r1.north) -- (action-4|-G c1r3.south);
\draw [sell] (action-5|-G c1r1.north) -- (action-5|-G c1r3.south);
\draw [buy] (action-6|-G c1r1.north) -- (action-6|-G c1r3.south);
\draw [sell] (action-7|-G c1r1.north) -- (action-7|-G c1r3.south);
\draw [buy] (action-8|-G c1r1.north) -- (action-8|-G c1r3.south);
\draw [sell] (action-9|-G c1r1.north) -- (action-9|-G c1r3.south);
\draw [buy] (action-10|-G c1r1.north) -- (action-10|-G c1r3.south);
\draw [sell] (action-11|-G c1r1.north) -- (action-11|-G c1r3.south);
\draw [buy] (action-12|-G c1r1.north) -- (action-12|-G c1r3.south);
\draw [sell] (action-13|-G c1r1.north) -- (action-13|-G c1r3.south);
\draw [buy] (action-14|-G c1r1.north) -- (action-14|-G c1r3.south);
\draw [sell] (action-15|-G c1r1.north) -- (action-15|-G c1r3.south);
\draw [buy] (action-16|-G c1r1.north) -- (action-16|-G c1r3.south);
\draw [sell] (action-17|-G c1r1.north) -- (action-17|-G c1r3.south);
\draw [buy] (action-18|-G c1r1.north) -- (action-18|-G c1r3.south);
\draw [sell] (action-19|-G c1r1.north) -- (action-19|-G c1r3.south);
\draw [buy] (action-20|-G c1r1.north) -- (action-20|-G c1r3.south);
\draw [sell] (action-21|-G c1r1.north) -- (action-21|-G c1r3.south);
\draw [buy] (action-22|-G c1r1.north) -- (action-22|-G c1r3.south);
\draw [sell] (action-23|-G c1r1.north) -- (action-23|-G c1r3.south);
\draw [buy] (action-24|-G c1r1.north) -- (action-24|-G c1r3.south);
\draw [sell] (action-25|-G c1r1.north) -- (action-25|-G c1r3.south);
\draw [buy] (action-26|-G c1r1.north) -- (action-26|-G c1r3.south);
\draw [sell] (action-27|-G c1r1.north) -- (action-27|-G c1r3.south);
\draw [buy] (action-28|-G c1r1.north) -- (action-28|-G c1r3.south);
\draw [sell] (action-29|-G c1r1.north) -- (action-29|-G c1r3.south);
\draw [buy] (action-30|-G c1r1.north) -- (action-30|-G c1r3.south);
\draw [sell] (action-31|-G c1r1.north) -- (action-31|-G c1r3.south);
\draw [buy] (action-32|-G c1r1.north) -- (action-32|-G c1r3.south);
\draw [sell] (action-33|-G c1r1.north) -- (action-33|-G c1r3.south);
\draw [buy] (action-34|-G c1r1.north) -- (action-34|-G c1r3.south);
\draw [sell] (action-35|-G c1r1.north) -- (action-35|-G c1r3.south);
\draw [buy] (action-36|-G c1r1.north) -- (action-36|-G c1r3.south);
\draw [sell] (action-37|-G c1r1.north) -- (action-37|-G c1r3.south);
\draw [buy] (action-38|-G c1r1.north) -- (action-38|-G c1r3.south);
\draw [sell] (action-39|-G c1r1.north) -- (action-39|-G c1r3.south);
\draw [buy] (action-40|-G c1r1.north) -- (action-40|-G c1r3.south);
\draw [sell] (action-41|-G c1r1.north) -- (action-41|-G c1r3.south);
\draw [buy] (action-42|-G c1r1.north) -- (action-42|-G c1r3.south);
\draw [sell] (action-43|-G c1r1.north) -- (action-43|-G c1r3.south);
\draw [buy] (action-44|-G c1r1.north) -- (action-44|-G c1r3.south);
\draw [sell] (action-45|-G c1r1.north) -- (action-45|-G c1r3.south);
\draw [buy] (action-46|-G c1r1.north) -- (action-46|-G c1r3.south);
\draw [sell] (action-47|-G c1r1.north) -- (action-47|-G c1r3.south);
\draw [buy] (action-48|-G c1r1.north) -- (action-48|-G c1r3.south);
\draw [sell] (action-49|-G c1r1.north) -- (action-49|-G c1r3.south);
\draw [buy] (action-50|-G c1r1.north) -- (action-50|-G c1r3.south);
\draw [sell] (action-51|-G c1r1.north) -- (action-51|-G c1r3.south);
\draw [buy] (action-52|-G c1r1.north) -- (action-52|-G c1r3.south);
\draw [sell] (action-53|-G c1r1.north) -- (action-53|-G c1r3.south);
\draw [buy] (action-54|-G c1r1.north) -- (action-54|-G c1r3.south);
\draw [sell] (action-55|-G c1r1.north) -- (action-55|-G c1r3.south);
\draw [buy] (action-56|-G c1r1.north) -- (action-56|-G c1r3.south);
\draw [sell] (action-57|-G c1r1.north) -- (action-57|-G c1r3.south);
\draw [buy] (action-58|-G c1r1.north) -- (action-58|-G c1r3.south);
\draw [sell] (action-59|-G c1r1.north) -- (action-59|-G c1r3.south);
\draw [buy] (action-60|-G c1r1.north) -- (action-60|-G c1r3.south);
\draw [sell] (action-61|-G c1r1.north) -- (action-61|-G c1r3.south);
\draw [buy] (action-62|-G c1r1.north) -- (action-62|-G c1r3.south);
\draw [sell] (action-63|-G c1r1.north) -- (action-63|-G c1r3.south);
\draw [buy] (action-64|-G c1r1.north) -- (action-64|-G c1r3.south);
\draw [sell] (action-65|-G c1r1.north) -- (action-65|-G c1r3.south);
\draw [buy] (action-66|-G c1r1.north) -- (action-66|-G c1r3.south);
\draw [sell] (action-67|-G c1r1.north) -- (action-67|-G c1r3.south);
\draw [buy] (action-68|-G c1r1.north) -- (action-68|-G c1r3.south);
\draw [sell] (action-69|-G c1r1.north) -- (action-69|-G c1r3.south);
\draw [buy] (action-70|-G c1r1.north) -- (action-70|-G c1r3.south);
\draw [sell] (action-71|-G c1r1.north) -- (action-71|-G c1r3.south);
\draw [buy] (action-72|-G c1r1.north) -- (action-72|-G c1r3.south);
\draw [sell] (action-73|-G c1r1.north) -- (action-73|-G c1r3.south);
\draw [buy] (action-74|-G c1r1.north) -- (action-74|-G c1r3.south);
\draw [sell] (action-75|-G c1r1.north) -- (action-75|-G c1r3.south);
\draw [buy] (action-76|-G c1r1.north) -- (action-76|-G c1r3.south);
\draw [sell] (action-77|-G c1r1.north) -- (action-77|-G c1r3.south);
\draw [buy] (action-78|-G c1r1.north) -- (action-78|-G c1r3.south);
\draw [sell] (action-79|-G c1r1.north) -- (action-79|-G c1r3.south);
\draw [buy] (action-80|-G c1r1.north) -- (action-80|-G c1r3.south);
\draw [sell] (action-81|-G c1r1.north) -- (action-81|-G c1r3.south);
\draw [buy] (action-82|-G c1r1.north) -- (action-82|-G c1r3.south);
\draw [sell] (action-83|-G c1r1.north) -- (action-83|-G c1r3.south);
\draw [buy] (action-84|-G c1r1.north) -- (action-84|-G c1r3.south);
\draw [sell] (action-85|-G c1r1.north) -- (action-85|-G c1r3.south);
\draw [buy] (action-86|-G c1r1.north) -- (action-86|-G c1r3.south);
\draw [sell] (action-87|-G c1r1.north) -- (action-87|-G c1r3.south);
\draw [buy] (action-88|-G c1r1.north) -- (action-88|-G c1r3.south);
\draw [sell] (action-89|-G c1r1.north) -- (action-89|-G c1r3.south);
\draw [buy] (action-90|-G c1r1.north) -- (action-90|-G c1r3.south);
\draw [sell] (action-91|-G c1r1.north) -- (action-91|-G c1r3.south);
\draw [buy] (action-92|-G c1r1.north) -- (action-92|-G c1r3.south);
\draw [sell] (action-93|-G c1r1.north) -- (action-93|-G c1r3.south);
\draw [buy] (action-94|-G c1r1.north) -- (action-94|-G c1r3.south);
\draw [sell] (action-95|-G c1r1.north) -- (action-95|-G c1r3.south);
\draw [buy] (action-96|-G c1r1.north) -- (action-96|-G c1r3.south);
\draw [sell] (action-97|-G c1r1.north) -- (action-97|-G c1r3.south);
\draw [buy] (action-98|-G c1r1.north) -- (action-98|-G c1r3.south);
\draw [sell] (action-99|-G c1r1.north) -- (action-99|-G c1r3.south);
\draw [buy] (action-100|-G c1r1.north) -- (action-100|-G c1r3.south);
\draw [sell] (action-101|-G c1r1.north) -- (action-101|-G c1r3.south);
\draw [buy] (action-102|-G c1r1.north) -- (action-102|-G c1r3.south);
\draw [sell] (action-103|-G c1r1.north) -- (action-103|-G c1r3.south);
\draw [buy] (action-104|-G c1r1.north) -- (action-104|-G c1r3.south);
\draw [sell] (action-105|-G c1r1.north) -- (action-105|-G c1r3.south);
\draw [buy] (action-106|-G c1r1.north) -- (action-106|-G c1r3.south);
\draw [sell] (action-107|-G c1r1.north) -- (action-107|-G c1r3.south);
\draw [buy] (action-108|-G c1r1.north) -- (action-108|-G c1r3.south);
\draw [sell] (action-109|-G c1r1.north) -- (action-109|-G c1r3.south);
\draw [buy] (action-110|-G c1r1.north) -- (action-110|-G c1r3.south);
\draw [sell] (action-111|-G c1r1.north) -- (action-111|-G c1r3.south);
\draw [buy] (action-112|-G c1r1.north) -- (action-112|-G c1r3.south);
\draw [sell] (action-113|-G c1r1.north) -- (action-113|-G c1r3.south);
\draw [buy] (action-114|-G c1r1.north) -- (action-114|-G c1r3.south);
\draw [sell] (action-115|-G c1r1.north) -- (action-115|-G c1r3.south);
\draw [buy] (action-116|-G c1r1.north) -- (action-116|-G c1r3.south);
\draw [sell] (action-117|-G c1r1.north) -- (action-117|-G c1r3.south);
\draw [buy] (action-118|-G c1r1.north) -- (action-118|-G c1r3.south);
\draw [sell] (action-119|-G c1r1.north) -- (action-119|-G c1r3.south);
\draw [buy] (action-120|-G c1r1.north) -- (action-120|-G c1r3.south);
\draw [sell] (action-121|-G c1r1.north) -- (action-121|-G c1r3.south);
\draw [buy] (action-122|-G c1r1.north) -- (action-122|-G c1r3.south);
\draw [sell] (action-123|-G c1r1.north) -- (action-123|-G c1r3.south);
\draw [buy] (action-124|-G c1r1.north) -- (action-124|-G c1r3.south);
\draw [sell] (action-125|-G c1r1.north) -- (action-125|-G c1r3.south);
\draw [buy] (action-126|-G c1r1.north) -- (action-126|-G c1r3.south);
\draw [sell] (action-127|-G c1r1.north) -- (action-127|-G c1r3.south);
\draw [buy] (action-128|-G c1r1.north) -- (action-128|-G c1r3.south);
\draw [sell] (action-129|-G c1r1.north) -- (action-129|-G c1r3.south);
\draw [buy] (action-130|-G c1r1.north) -- (action-130|-G c1r3.south);
\draw [sell] (action-131|-G c1r1.north) -- (action-131|-G c1r3.south);
\draw [buy] (action-132|-G c1r1.north) -- (action-132|-G c1r3.south);
\draw [sell] (action-133|-G c1r1.north) -- (action-133|-G c1r3.south);
\draw [buy] (action-134|-G c1r1.north) -- (action-134|-G c1r3.south);

134
macd/msft-intersect-c.tex Normal file
View File

@ -0,0 +1,134 @@
\coordinate (action-1) at (0,0);
\coordinate (action-2) at (1,0);
\coordinate (action-3) at (3,0);
\coordinate (action-4) at (10,0);
\coordinate (action-5) at (29,0);
\coordinate (action-6) at (34,0);
\coordinate (action-7) at (47,0);
\coordinate (action-8) at (82,0);
\coordinate (action-9) at (96,0);
\coordinate (action-10) at (108,0);
\coordinate (action-11) at (127,0);
\coordinate (action-12) at (135,0);
\coordinate (action-13) at (144,0);
\coordinate (action-14) at (145,0);
\coordinate (action-15) at (152,0);
\coordinate (action-16) at (155,0);
\coordinate (action-17) at (163,0);
\coordinate (action-18) at (165,0);
\coordinate (action-19) at (181,0);
\coordinate (action-20) at (192,0);
\coordinate (action-21) at (193,0);
\coordinate (action-22) at (195,0);
\coordinate (action-23) at (196,0);
\coordinate (action-24) at (208,0);
\coordinate (action-25) at (214,0);
\coordinate (action-26) at (220,0);
\coordinate (action-27) at (234,0);
\coordinate (action-28) at (237,0);
\coordinate (action-29) at (251,0);
\coordinate (action-30) at (252,0);
\coordinate (action-31) at (253,0);
\coordinate (action-32) at (262,0);
\coordinate (action-33) at (275,0);
\coordinate (action-34) at (290,0);
\coordinate (action-35) at (295,0);
\coordinate (action-36) at (300,0);
\coordinate (action-37) at (305,0);
\coordinate (action-38) at (307,0);
\coordinate (action-39) at (322,0);
\coordinate (action-40) at (324,0);
\coordinate (action-41) at (331,0);
\coordinate (action-42) at (333,0);
\coordinate (action-43) at (334,0);
\coordinate (action-44) at (344,0);
\coordinate (action-45) at (354,0);
\coordinate (action-46) at (366,0);
\coordinate (action-47) at (379,0);
\coordinate (action-48) at (382,0);
\coordinate (action-49) at (389,0);
\coordinate (action-50) at (391,0);
\coordinate (action-51) at (393,0);
\coordinate (action-52) at (413,0);
\coordinate (action-53) at (433,0);
\coordinate (action-54) at (455,0);
\coordinate (action-55) at (463,0);
\coordinate (action-56) at (468,0);
\coordinate (action-57) at (469,0);
\coordinate (action-58) at (476,0);
\coordinate (action-59) at (479,0);
\coordinate (action-60) at (488,0);
\coordinate (action-61) at (505,0);
\coordinate (action-62) at (516,0);
\coordinate (action-63) at (521,0);
\coordinate (action-64) at (526,0);
\coordinate (action-65) at (548,0);
\coordinate (action-66) at (573,0);
\coordinate (action-67) at (574,0);
\coordinate (action-68) at (578,0);
\coordinate (action-69) at (585,0);
\coordinate (action-70) at (593,0);
\coordinate (action-71) at (605,0);
\coordinate (action-72) at (607,0);
\coordinate (action-73) at (612,0);
\coordinate (action-74) at (633,0);
\coordinate (action-75) at (664,0);
\coordinate (action-76) at (667,0);
\coordinate (action-77) at (678,0);
\coordinate (action-78) at (694,0);
\coordinate (action-79) at (701,0);
\coordinate (action-80) at (704,0);
\coordinate (action-81) at (706,0);
\coordinate (action-82) at (712,0);
\coordinate (action-83) at (715,0);
\coordinate (action-84) at (729,0);
\coordinate (action-85) at (739,0);
\coordinate (action-86) at (745,0);
\coordinate (action-87) at (758,0);
\coordinate (action-88) at (760,0);
\coordinate (action-89) at (772,0);
\coordinate (action-90) at (773,0);
\coordinate (action-91) at (778,0);
\coordinate (action-92) at (787,0);
\coordinate (action-93) at (791,0);
\coordinate (action-94) at (803,0);
\coordinate (action-95) at (823,0);
\coordinate (action-96) at (834,0);
\coordinate (action-97) at (835,0);
\coordinate (action-98) at (839,0);
\coordinate (action-99) at (860,0);
\coordinate (action-100) at (895,0);
\coordinate (action-101) at (910,0);
\coordinate (action-102) at (918,0);
\coordinate (action-103) at (927,0);
\coordinate (action-104) at (939,0);
\coordinate (action-105) at (946,0);
\coordinate (action-106) at (950,0);
\coordinate (action-107) at (964,0);
\coordinate (action-108) at (980,0);
\coordinate (action-109) at (989,0);
\coordinate (action-110) at (1000,0);
\coordinate (action-111) at (1001,0);
\coordinate (action-112) at (1011,0);
\coordinate (action-113) at (1015,0);
\coordinate (action-114) at (1019,0);
\coordinate (action-115) at (1020,0);
\coordinate (action-116) at (1021,0);
\coordinate (action-117) at (1034,0);
\coordinate (action-118) at (1042,0);
\coordinate (action-119) at (1055,0);
\coordinate (action-120) at (1067,0);
\coordinate (action-121) at (1077,0);
\coordinate (action-122) at (1096,0);
\coordinate (action-123) at (1110,0);
\coordinate (action-124) at (1123,0);
\coordinate (action-125) at (1124,0);
\coordinate (action-126) at (1127,0);
\coordinate (action-127) at (1147,0);
\coordinate (action-128) at (1158,0);
\coordinate (action-129) at (1183,0);
\coordinate (action-130) at (1204,0);
\coordinate (action-131) at (1215,0);
\coordinate (action-132) at (1221,0);
\coordinate (action-133) at (1240,0);
\coordinate (action-134) at (1250,0);

18
macd/sin-intersect-a.tex Normal file
View File

@ -0,0 +1,18 @@
\draw [sell] (action-1|-G c1r1.north) -- (action-1|-G c1r3.south);
\draw [buy] (action-2|-G c1r1.north) -- (action-2|-G c1r3.south);
\draw [sell] (action-3|-G c1r1.north) -- (action-3|-G c1r3.south);
\draw [buy] (action-4|-G c1r1.north) -- (action-4|-G c1r3.south);
\draw [sell] (action-5|-G c1r1.north) -- (action-5|-G c1r3.south);
\draw [buy] (action-6|-G c1r1.north) -- (action-6|-G c1r3.south);
\draw [sell] (action-7|-G c1r1.north) -- (action-7|-G c1r3.south);
\draw [buy] (action-8|-G c1r1.north) -- (action-8|-G c1r3.south);
\draw [sell] (action-9|-G c1r1.north) -- (action-9|-G c1r3.south);
\draw [buy] (action-10|-G c1r1.north) -- (action-10|-G c1r3.south);
\draw [sell] (action-11|-G c1r1.north) -- (action-11|-G c1r3.south);
\draw [buy] (action-12|-G c1r1.north) -- (action-12|-G c1r3.south);
\draw [sell] (action-13|-G c1r1.north) -- (action-13|-G c1r3.south);
\draw [buy] (action-14|-G c1r1.north) -- (action-14|-G c1r3.south);
\draw [sell] (action-15|-G c1r1.north) -- (action-15|-G c1r3.south);
\draw [buy] (action-16|-G c1r1.north) -- (action-16|-G c1r3.south);
\draw [sell] (action-17|-G c1r1.north) -- (action-17|-G c1r3.south);
\draw [buy] (action-18|-G c1r1.north) -- (action-18|-G c1r3.south);

18
macd/sin-intersect-c.tex Normal file
View File

@ -0,0 +1,18 @@
\coordinate (action-1) at (1,0);
\coordinate (action-2) at (2,0);
\coordinate (action-3) at (23,0);
\coordinate (action-4) at (76,0);
\coordinate (action-5) at (138,0);
\coordinate (action-6) at (201,0);
\coordinate (action-7) at (264,0);
\coordinate (action-8) at (327,0);
\coordinate (action-9) at (390,0);
\coordinate (action-10) at (453,0);
\coordinate (action-11) at (515,0);
\coordinate (action-12) at (578,0);
\coordinate (action-13) at (641,0);
\coordinate (action-14) at (704,0);
\coordinate (action-15) at (767,0);
\coordinate (action-16) at (829,0);
\coordinate (action-17) at (892,0);
\coordinate (action-18) at (955,0);

103
macd/wig-intersect-a.tex Normal file
View File

@ -0,0 +1,103 @@
\draw [buy] (action-1|-G c1r1.north) -- (action-1|-G c1r3.south);
\draw [sell] (action-2|-G c1r1.north) -- (action-2|-G c1r3.south);
\draw [buy] (action-3|-G c1r1.north) -- (action-3|-G c1r3.south);
\draw [sell] (action-4|-G c1r1.north) -- (action-4|-G c1r3.south);
\draw [buy] (action-5|-G c1r1.north) -- (action-5|-G c1r3.south);
\draw [sell] (action-6|-G c1r1.north) -- (action-6|-G c1r3.south);
\draw [buy] (action-7|-G c1r1.north) -- (action-7|-G c1r3.south);
\draw [sell] (action-8|-G c1r1.north) -- (action-8|-G c1r3.south);
\draw [buy] (action-9|-G c1r1.north) -- (action-9|-G c1r3.south);
\draw [sell] (action-10|-G c1r1.north) -- (action-10|-G c1r3.south);
\draw [buy] (action-11|-G c1r1.north) -- (action-11|-G c1r3.south);
\draw [sell] (action-12|-G c1r1.north) -- (action-12|-G c1r3.south);
\draw [buy] (action-13|-G c1r1.north) -- (action-13|-G c1r3.south);
\draw [sell] (action-14|-G c1r1.north) -- (action-14|-G c1r3.south);
\draw [buy] (action-15|-G c1r1.north) -- (action-15|-G c1r3.south);
\draw [sell] (action-16|-G c1r1.north) -- (action-16|-G c1r3.south);
\draw [buy] (action-17|-G c1r1.north) -- (action-17|-G c1r3.south);
\draw [sell] (action-18|-G c1r1.north) -- (action-18|-G c1r3.south);
\draw [buy] (action-19|-G c1r1.north) -- (action-19|-G c1r3.south);
\draw [sell] (action-20|-G c1r1.north) -- (action-20|-G c1r3.south);
\draw [buy] (action-21|-G c1r1.north) -- (action-21|-G c1r3.south);
\draw [sell] (action-22|-G c1r1.north) -- (action-22|-G c1r3.south);
\draw [buy] (action-23|-G c1r1.north) -- (action-23|-G c1r3.south);
\draw [sell] (action-24|-G c1r1.north) -- (action-24|-G c1r3.south);
\draw [buy] (action-25|-G c1r1.north) -- (action-25|-G c1r3.south);
\draw [sell] (action-26|-G c1r1.north) -- (action-26|-G c1r3.south);
\draw [buy] (action-27|-G c1r1.north) -- (action-27|-G c1r3.south);
\draw [sell] (action-28|-G c1r1.north) -- (action-28|-G c1r3.south);
\draw [buy] (action-29|-G c1r1.north) -- (action-29|-G c1r3.south);
\draw [sell] (action-30|-G c1r1.north) -- (action-30|-G c1r3.south);
\draw [buy] (action-31|-G c1r1.north) -- (action-31|-G c1r3.south);
\draw [sell] (action-32|-G c1r1.north) -- (action-32|-G c1r3.south);
\draw [buy] (action-33|-G c1r1.north) -- (action-33|-G c1r3.south);
\draw [sell] (action-34|-G c1r1.north) -- (action-34|-G c1r3.south);
\draw [buy] (action-35|-G c1r1.north) -- (action-35|-G c1r3.south);
\draw [sell] (action-36|-G c1r1.north) -- (action-36|-G c1r3.south);
\draw [buy] (action-37|-G c1r1.north) -- (action-37|-G c1r3.south);
\draw [sell] (action-38|-G c1r1.north) -- (action-38|-G c1r3.south);
\draw [buy] (action-39|-G c1r1.north) -- (action-39|-G c1r3.south);
\draw [sell] (action-40|-G c1r1.north) -- (action-40|-G c1r3.south);
\draw [buy] (action-41|-G c1r1.north) -- (action-41|-G c1r3.south);
\draw [sell] (action-42|-G c1r1.north) -- (action-42|-G c1r3.south);
\draw [buy] (action-43|-G c1r1.north) -- (action-43|-G c1r3.south);
\draw [sell] (action-44|-G c1r1.north) -- (action-44|-G c1r3.south);
\draw [buy] (action-45|-G c1r1.north) -- (action-45|-G c1r3.south);
\draw [sell] (action-46|-G c1r1.north) -- (action-46|-G c1r3.south);
\draw [buy] (action-47|-G c1r1.north) -- (action-47|-G c1r3.south);
\draw [sell] (action-48|-G c1r1.north) -- (action-48|-G c1r3.south);
\draw [buy] (action-49|-G c1r1.north) -- (action-49|-G c1r3.south);
\draw [sell] (action-50|-G c1r1.north) -- (action-50|-G c1r3.south);
\draw [buy] (action-51|-G c1r1.north) -- (action-51|-G c1r3.south);
\draw [sell] (action-52|-G c1r1.north) -- (action-52|-G c1r3.south);
\draw [buy] (action-53|-G c1r1.north) -- (action-53|-G c1r3.south);
\draw [sell] (action-54|-G c1r1.north) -- (action-54|-G c1r3.south);
\draw [buy] (action-55|-G c1r1.north) -- (action-55|-G c1r3.south);
\draw [sell] (action-56|-G c1r1.north) -- (action-56|-G c1r3.south);
\draw [buy] (action-57|-G c1r1.north) -- (action-57|-G c1r3.south);
\draw [sell] (action-58|-G c1r1.north) -- (action-58|-G c1r3.south);
\draw [buy] (action-59|-G c1r1.north) -- (action-59|-G c1r3.south);
\draw [sell] (action-60|-G c1r1.north) -- (action-60|-G c1r3.south);
\draw [buy] (action-61|-G c1r1.north) -- (action-61|-G c1r3.south);
\draw [sell] (action-62|-G c1r1.north) -- (action-62|-G c1r3.south);
\draw [buy] (action-63|-G c1r1.north) -- (action-63|-G c1r3.south);
\draw [sell] (action-64|-G c1r1.north) -- (action-64|-G c1r3.south);
\draw [buy] (action-65|-G c1r1.north) -- (action-65|-G c1r3.south);
\draw [sell] (action-66|-G c1r1.north) -- (action-66|-G c1r3.south);
\draw [buy] (action-67|-G c1r1.north) -- (action-67|-G c1r3.south);
\draw [sell] (action-68|-G c1r1.north) -- (action-68|-G c1r3.south);
\draw [buy] (action-69|-G c1r1.north) -- (action-69|-G c1r3.south);
\draw [sell] (action-70|-G c1r1.north) -- (action-70|-G c1r3.south);
\draw [buy] (action-71|-G c1r1.north) -- (action-71|-G c1r3.south);
\draw [sell] (action-72|-G c1r1.north) -- (action-72|-G c1r3.south);
\draw [buy] (action-73|-G c1r1.north) -- (action-73|-G c1r3.south);
\draw [sell] (action-74|-G c1r1.north) -- (action-74|-G c1r3.south);
\draw [buy] (action-75|-G c1r1.north) -- (action-75|-G c1r3.south);
\draw [sell] (action-76|-G c1r1.north) -- (action-76|-G c1r3.south);
\draw [buy] (action-77|-G c1r1.north) -- (action-77|-G c1r3.south);
\draw [sell] (action-78|-G c1r1.north) -- (action-78|-G c1r3.south);
\draw [buy] (action-79|-G c1r1.north) -- (action-79|-G c1r3.south);
\draw [sell] (action-80|-G c1r1.north) -- (action-80|-G c1r3.south);
\draw [buy] (action-81|-G c1r1.north) -- (action-81|-G c1r3.south);
\draw [sell] (action-82|-G c1r1.north) -- (action-82|-G c1r3.south);
\draw [buy] (action-83|-G c1r1.north) -- (action-83|-G c1r3.south);
\draw [sell] (action-84|-G c1r1.north) -- (action-84|-G c1r3.south);
\draw [buy] (action-85|-G c1r1.north) -- (action-85|-G c1r3.south);
\draw [sell] (action-86|-G c1r1.north) -- (action-86|-G c1r3.south);
\draw [buy] (action-87|-G c1r1.north) -- (action-87|-G c1r3.south);
\draw [sell] (action-88|-G c1r1.north) -- (action-88|-G c1r3.south);
\draw [buy] (action-89|-G c1r1.north) -- (action-89|-G c1r3.south);
\draw [sell] (action-90|-G c1r1.north) -- (action-90|-G c1r3.south);
\draw [buy] (action-91|-G c1r1.north) -- (action-91|-G c1r3.south);
\draw [sell] (action-92|-G c1r1.north) -- (action-92|-G c1r3.south);
\draw [buy] (action-93|-G c1r1.north) -- (action-93|-G c1r3.south);
\draw [sell] (action-94|-G c1r1.north) -- (action-94|-G c1r3.south);
\draw [buy] (action-95|-G c1r1.north) -- (action-95|-G c1r3.south);
\draw [sell] (action-96|-G c1r1.north) -- (action-96|-G c1r3.south);
\draw [buy] (action-97|-G c1r1.north) -- (action-97|-G c1r3.south);
\draw [sell] (action-98|-G c1r1.north) -- (action-98|-G c1r3.south);
\draw [buy] (action-99|-G c1r1.north) -- (action-99|-G c1r3.south);
\draw [sell] (action-100|-G c1r1.north) -- (action-100|-G c1r3.south);
\draw [buy] (action-101|-G c1r1.north) -- (action-101|-G c1r3.south);
\draw [sell] (action-102|-G c1r1.north) -- (action-102|-G c1r3.south);
\draw [buy] (action-103|-G c1r1.north) -- (action-103|-G c1r3.south);

103
macd/wig-intersect-c.tex Normal file
View File

@ -0,0 +1,103 @@
\coordinate (action-1) at (1,0);
\coordinate (action-2) at (3,0);
\coordinate (action-3) at (14,0);
\coordinate (action-4) at (28,0);
\coordinate (action-5) at (48,0);
\coordinate (action-6) at (49,0);
\coordinate (action-7) at (52,0);
\coordinate (action-8) at (69,0);
\coordinate (action-9) at (90,0);
\coordinate (action-10) at (102,0);
\coordinate (action-11) at (112,0);
\coordinate (action-12) at (125,0);
\coordinate (action-13) at (126,0);
\coordinate (action-14) at (132,0);
\coordinate (action-15) at (158,0);
\coordinate (action-16) at (173,0);
\coordinate (action-17) at (186,0);
\coordinate (action-18) at (195,0);
\coordinate (action-19) at (207,0);
\coordinate (action-20) at (208,0);
\coordinate (action-21) at (209,0);
\coordinate (action-22) at (216,0);
\coordinate (action-23) at (220,0);
\coordinate (action-24) at (234,0);
\coordinate (action-25) at (236,0);
\coordinate (action-26) at (240,0);
\coordinate (action-27) at (260,0);
\coordinate (action-28) at (287,0);
\coordinate (action-29) at (319,0);
\coordinate (action-30) at (334,0);
\coordinate (action-31) at (338,0);
\coordinate (action-32) at (345,0);
\coordinate (action-33) at (352,0);
\coordinate (action-34) at (363,0);
\coordinate (action-35) at (374,0);
\coordinate (action-36) at (389,0);
\coordinate (action-37) at (400,0);
\coordinate (action-38) at (412,0);
\coordinate (action-39) at (429,0);
\coordinate (action-40) at (434,0);
\coordinate (action-41) at (450,0);
\coordinate (action-42) at (460,0);
\coordinate (action-43) at (472,0);
\coordinate (action-44) at (496,0);
\coordinate (action-45) at (502,0);
\coordinate (action-46) at (503,0);
\coordinate (action-47) at (506,0);
\coordinate (action-48) at (514,0);
\coordinate (action-49) at (517,0);
\coordinate (action-50) at (519,0);
\coordinate (action-51) at (532,0);
\coordinate (action-52) at (535,0);
\coordinate (action-53) at (549,0);
\coordinate (action-54) at (561,0);
\coordinate (action-55) at (564,0);
\coordinate (action-56) at (568,0);
\coordinate (action-57) at (574,0);
\coordinate (action-58) at (576,0);
\coordinate (action-59) at (580,0);
\coordinate (action-60) at (584,0);
\coordinate (action-61) at (588,0);
\coordinate (action-62) at (602,0);
\coordinate (action-63) at (607,0);
\coordinate (action-64) at (614,0);
\coordinate (action-65) at (629,0);
\coordinate (action-66) at (631,0);
\coordinate (action-67) at (637,0);
\coordinate (action-68) at (645,0);
\coordinate (action-69) at (648,0);
\coordinate (action-70) at (656,0);
\coordinate (action-71) at (660,0);
\coordinate (action-72) at (671,0);
\coordinate (action-73) at (682,0);
\coordinate (action-74) at (702,0);
\coordinate (action-75) at (713,0);
\coordinate (action-76) at (719,0);
\coordinate (action-77) at (726,0);
\coordinate (action-78) at (731,0);
\coordinate (action-79) at (733,0);
\coordinate (action-80) at (748,0);
\coordinate (action-81) at (761,0);
\coordinate (action-82) at (765,0);
\coordinate (action-83) at (776,0);
\coordinate (action-84) at (796,0);
\coordinate (action-85) at (816,0);
\coordinate (action-86) at (834,0);
\coordinate (action-87) at (841,0);
\coordinate (action-88) at (851,0);
\coordinate (action-89) at (853,0);
\coordinate (action-90) at (856,0);
\coordinate (action-91) at (858,0);
\coordinate (action-92) at (862,0);
\coordinate (action-93) at (870,0);
\coordinate (action-94) at (880,0);
\coordinate (action-95) at (900,0);
\coordinate (action-96) at (910,0);
\coordinate (action-97) at (918,0);
\coordinate (action-98) at (925,0);
\coordinate (action-99) at (933,0);
\coordinate (action-100) at (939,0);
\coordinate (action-101) at (949,0);
\coordinate (action-102) at (975,0);
\coordinate (action-103) at (993,0);

View File

@ -77,7 +77,7 @@ class matrix
matrix<T, n, m> result;
for (int i = 0; i < m; ++i)
for (int j = 0; j < m; ++j)
for (int j = 0; j < n; ++j)
result.set(j, i, get(i, j));
@ -243,6 +243,8 @@ vector<T, n> normalize(const vector<T, n> &vec)
accumulator /= n;
if (!accumulator) return vec;
std::function<T(const T&)> normalizer = [accumulator](const T& item) { return item / accumulator; };
return map(vec, normalizer);
}

44
simulator.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "simulator.h"
simulator::simulator(class decider* decider, double money, unsigned stock)
: decider(decider), money(money), start_money(money), stock(stock), start_stock(stock)
{
decider->start_money = money;
decider->start_stock = stock;
initialized = false;
}
double simulator::proceed(double price)
{
if (!initialized) {
initialized = true;
start_price = price;
decider->reset(price);
}
auto decision = decider->decide(price, money, stock);
auto current = price * stock + money;
auto max_credit = std::max(current * this->credit_ratio, -this->max_credit);
if (decision < 0) {
decision = std::max<int>(decision, -stock); // cannot sell more than we actually have
} else if (decision > 0) {
decision = std::min<int>(floor((money + max_credit) / price), decision);
}
money -= price * decision;
stock += decision;
return decision;
}
std::vector<double> simulator::proceed(std::vector<double> prices)
{
std::vector<double> result;
for(auto price : prices) {
result.push_back(proceed(price));
}
return result;
}

23
simulator.h Normal file
View File

@ -0,0 +1,23 @@
#include <algorithm>
#include "decider.h"
class simulator {
decider* decider;
double start_money;
unsigned start_stock;
bool initialized;
public:
double credit_ratio = 0.05;
double max_credit = 1e5;
double money, start_price;
unsigned stock;
simulator(class decider* decider, double money, unsigned stock);
double proceed(double price);
std::vector<double> proceed(std::vector<double> prices);
};

View File

@ -1,16 +1,26 @@
\begin{axis}[
axis lines=center, no marks,
width=\linewidth, height=.66\linewidth,
\begin{groupplot}[width=\linewidth, height=4cm, group style={
group size=1 by 4,
vertical sep=.2cm,
group name=G
},
axis lines=center,
no marks,
xtick=\empty, clip=false
]
\addplot table [x expr=\coordindex, y=price, col sep=comma] {\file};
\end{axis}
\nextgroupplot
\addplot +[thick] table [x=no, y=price, col sep=comma] {\file};
\begin{axis}[
axis lines=center, no marks,
width=\linewidth, height=3cm,
hide y axis,
]
\addplot table [x expr=\coordindex, y=macd, col sep=comma] {\file};
\addplot table [x expr=\coordindex, y=signal, col sep=comma] {\file};
\addplot table [x expr=\coordindex, y=delta, col sep=comma] {\file};
\end{axis}
\nextgroupplot[height=2cm, ytick=\empty, y axis line style={draw=none}]
\addplot +[thick, black] table [x=no, y=delta, col sep=comma] {\file};
\nextgroupplot[height=3cm]
\addplot +[name path global=macd, red] table [x=no, y=macd, col sep=comma] {\file};
\addplot +[name path global=signal, blue] table [x=no, y=signal, col sep=comma] {\file};
\coordinate (safe) at (35,0);
\input{\action-c.tex}
\nextgroupplot[height=3cm]
\addplot +[thick, orange] table [x=x, y=decsion, col sep=comma] {\decision};
\end{groupplot}
\input{\action-a.tex}

View File

@ -25,14 +25,16 @@
\usepackage[polish]{babel}
\usepackage{braket}
\usepackage{subcaption}
\usepackage{csvsimple} % kurwa
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{groupplots}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\usetikzlibrary{decorations.pathmorphing, arrows.meta, positioning}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{shapes.geometric, arrows, intersections}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
@ -45,15 +47,16 @@
\floatname{algorithm}{Program}
\newcommand{\macd}[1]{\def\file{../macd/#1.dat}\def\action{../macd/#2-intersect.dat}\input{macd.tex}}
\tikzstyle{sell}=[red, -latex, densely dotted]
\tikzstyle{buy}=[green, latex-, densely dotted]
\begin{document}
\newcommand{\macd}[1]{\def\file{#1}\input{macd.tex}}
\maketitle
\begin{figure}[H]
\begin{tikzpicture}
\macd{msft.csv}
\macd{sin}
\end{tikzpicture}
\caption{XD}
\end{figure}

View File

@ -6,73 +6,83 @@
#include "helpers.h"
#include "argh.h"
#include "common.h"
#include "simulator.h"
int main(int argc, const char* argv[])
{
argh::parser args;
args.add_params({ "s", "stock" });
args.add_params({ "m", "money" });
args.add_params({ "o", "output" });
args.parse(argc, argv);
std::string network, input;
std::string network, input, output;
args(1) >> network;
args(2) >> input;
args(2, "") >> input;
args({"o", "output"}, "") >> output;
double money, start_money;
unsigned stock, start_stock;
bool quiet = args[{"q", "quiet"}] && output.empty();
args({"s", "stock"}, 1000) >> stock;
args({"m", "money"}, 1000.) >> money;
double start_money;
unsigned start_stock;
start_money = money;
start_stock = stock;
args({"s", "stock"}, 1000) >> start_stock;
args({"m", "money"}, 1000.) >> start_money;
std::function<double(const double&)> normalizer = [](const double& result) -> double { return erf(result); };
current_decider decider(normalizer);
std::ifstream network_file(network, std::ios::in | std::ios::binary);
std::ifstream input_file(input);
std::ifstream input_file;
std::ofstream output_file;
decider.load(network_file);
std::istream *in;
std::ostream *out;
double price;
decider.start_money = money;
decider.start_stock = stock;
std::cout << "x,price,decsion,money,stock" << std::endl;
input_file >> price;
decider.reset(price);
int i = 0;
do {
auto decision = decider.decide(price, money, stock);
auto current = price * stock + money;
auto max_credit = std::max(current * 0.05, -1e4);
if (decision < 0) {
decision = std::max<int>(decision, -stock); // cannot sell more than we actually have
} else if (decision > 0) {
decision = std::min<int>(floor((money + max_credit) / price), decision);
if (!output.empty()) {
output_file.open(output);
out = &output_file;
} else {
out = &std::cout;
}
money -= price * decision;
stock += decision;
if (!input.empty()) {
input_file.open(input);
in = &input_file;
} else {
in = &std::cin;
}
std::cout
<< ++i << ","
current_decider decider(normalizer);
decider.load(network_file);
simulator sim(&decider, start_money, start_stock);
double price;
if(!quiet) *out << "x,price,decsion,money,stock" << std::endl;
for(int i = 0; *in >> price; i++) {
auto decision = sim.proceed(price);
if (!quiet) {
*out << i + 1 << ","
<< price << ","
<< decision << ","
<< money << ","
<< stock
<< std::endl;
<< sim.money << ","
<< sim.stock << std::endl;
}
}
auto wealth = sim.money + sim.stock * price;
auto start = start_money + sim.start_price * start_stock;
auto hodl = start_money + price * start_stock;
} while(input_file >> price);
std::cout << "Koniec: "
<< money + stock*price << std::showpos << "("
<< (money + stock*price) / (start_money + start_stock*price)
<< ")";
<< wealth << " " << std::showpos
<< "S: "<< (wealth - start)*100 / (start) << "%, "
<< "H: "<< (wealth - hodl)*100 / (hodl) << "%"
<< std::noshowpos
<< std::endl;
/* decider.network.save(std::cout); */
}

15
tex.php Normal file
View File

@ -0,0 +1,15 @@
<?php
$filename = $argv[1];
$input = fopen($argv[1], "r");
$coordinates = fopen(str_replace(".csv", "-c.tex", $filename), "w");
$actions = fopen(str_replace(".csv", "-a.tex", $filename), "w");
fgetcsv($input); // ommit header
while (list($no, $x, $action) = fgetcsv($input)) {
fwrite($coordinates, sprintf("\\coordinate (action-%d) at (%d,0);\n", $no, $x));
fwrite($actions, sprintf("\\draw [%s] (action-%d|-G c1r1.north) -- (action-%d|-G c1r3.south);\n", $action, $no, $no));
}
fclose($input);
fclose($coordinates);
fclose($actions);

View File

@ -24,17 +24,19 @@ int main(int argc, char* argv[])
args.add_params({"-p", "--population"});
args.add_params({"-n", "--iterations"});
args.add_params({"-o", "--output-dir"});
args.add_params({"-s", "--samples"});
args.parse(argc, argv);
double money;
unsigned stock, population, iterations;
unsigned stock, population, iterations, sampling;
std::string input_file, output_dir;
args({ "m", "money" }, 1000.) >> money;
args({ "s", "stock" }, 1000) >> stock;
args({ "p", "population" }, 25) >> population;
args({ "n", "iterations" }, 4) >> iterations;
args({ "s", "samples" }, 0) >> sampling;
args({ "o", "output-dir" }, "") >> output_dir;
std::uniform_real_distribution<double> distribution(-2.0, 2.0);
@ -75,6 +77,24 @@ int main(int argc, char* argv[])
datasets[filename] = set;
if (sampling) {
trainer<current_decider>::dataset sample;
int i = 1;
for(double price : set) {
sample.push_back(price);
if (i % sampling == 0) {
std::stringstream stream;
stream << filename << "-" << i;
datasets[stream.str()] = sample;
sample = trainer<current_decider>::dataset();
std::cout << "Probka " << stream.str() << std::endl;
}
i++;
}
}
file.close();
std::cout << "Zaladowano zestaw testowy " << filename << " z " << set.size() << " wartosciami." << std::endl;
}
@ -84,17 +104,28 @@ int main(int argc, char* argv[])
return -1;
}
std::cout << "generation,score_med,score_high,profit_med,profit_high,best" << std::endl;
while (train.generation <= iterations) {
train.evolve();
for (auto pair : datasets) {
train.train(pair.second, pair.first);
}
train.evolve();
train.sort();
std::cout
<< train.generation << ","
<< train.population()[population / 2]->score << ","
<< train.population()[0]->score << ","
<< train.population()[population / 2]->profit << ","
<< train.population()[0]->profit << ","
<< train.population()[0]->id
<< std::endl;
}
if (!output_dir.empty()) {
int i = 0;
for (auto trained : train.population()) {
std::stringstream stream;
stream << output_dir << trained->id << ".net";
stream << output_dir << ++i << ".net";
std::string filename = stream.str();
std::cout << "Zapisuje siec #" << trained->id << " do pliku " << filename << std::endl;

View File

@ -7,12 +7,14 @@
#include <memory>
#include <string>
#include "simulator.h"
template<class T>
struct trained {
unsigned id;
unsigned position;
double score;
double score, profit;
T decider;
};
@ -36,11 +38,16 @@ public:
std::function<double(std::shared_ptr<trained<T>>, const dataset&, double, unsigned)> q;
trainer(double money, unsigned stock, std::size_t n, std::function<T()> factory)
: factory(factory), id(0), generation(1), money(money), stock(stock), n(n), random_engine(std::time(0))
: factory(factory), id(0), generation(0), money(money), stock(stock), n(n), random_engine(std::time(0))
{
this->q = [=](std::shared_ptr<trained<T>> x, const dataset& input, double money, unsigned stock) {
auto result = (money + stock * input.back()) / (this->money + this->stock * input.front());
auto current = input.back() * stock + money;
auto start = input.front() * this->stock + this->money;
auto hodl = input.back() * this->stock + this->money;
auto result = std::min((current - hodl)/hodl, (current - start)/start);
if (result < 0) result *= 5;
return result / (1 + abs(result));
};
add(n);
@ -69,70 +76,38 @@ public:
void evolve()
{
if (generation) {
sort();
filter();
breed();
}
// cleanup before next training sessions
for (auto t : trainees) {
t->score = 0;
t->score = t->profit = 0;
}
generation++;
}
void train(const dataset& input, std::shared_ptr<trained<T>> trainee)
{
trainee->decider.start_money = money;
trainee->decider.start_stock = stock;
trainee->decider.reset(input.front());
simulator sim(&(trainee->decider), this->money, this->stock);
sim.proceed(input);
double money = this->money;
unsigned stock = this->stock;
for (double price : input) {
auto decision = trainee->decider.decide(price, money, stock);
auto current = price * stock + money;
auto max_credit = std::max(current * 0.05, -1e4);
if (decision < 0) {
decision = std::max<int>(decision, -stock); // cannot sell more than we actually have
} else if (decision > 0) {
decision = std::min<int>(floor((money + max_credit) / price), decision);
}
money -= price * decision;
stock += decision;
}
trainee->score += q(trainee, input, money, stock);
trainee->score += q(trainee, input, sim.money, sim.stock);
auto last = input.back();
auto first = input.front();
auto wealth = money + stock * last;
auto wealth = sim.money + sim.stock * last;
auto hodl = this->money + this->stock * last;
auto start = this->money + this->stock * first;
std::cout
<< "#" << trainee->id << ": " << wealth
<< std::showpos
<< " H: " << wealth - hodl << " (" << (wealth - hodl) / hodl * 100 << "%)"
<< " S: " << wealth - start << " (" << (wealth - start) / start * 100 << "%) "
<< std::noshowpos
<< stock << " akcji, "
<< money << " gelda w banku. "
<< std::endl;
trainee->profit += (wealth - start) / start;
}
void train(const dataset& input, const std::string& name)
{
std::cout << "Zestaw " << name
<< " GEN #" << this->generation
<< " start: " << money + input.front() * stock
<< " HODL: " << money + input.back() * stock
<< std::endl;
for (auto trainee : trainees) {
train(input, trainee);
}
@ -160,21 +135,15 @@ public:
});
trainees.erase(iterator, std::end(trainees));
std::cout << "Przy życiu pozostają: ";
for (auto trainee : trainees) {
std::cout << "#" << trainee->id << " (" << trainee->position << ") ";
}
}
void breed()
{
std::size_t diff = n - trainees.size();
std::cout << "W populacji brakuje " << diff << " sieci, aktualnie " << trainees.size() << "." << std::endl;
std::vector<double> probability;
for (auto t : trainees) {
probability.push_back(t->score);
probability.push_back(t->position);
}
std::discrete_distribution<unsigned> distribution(probability.begin(), probability.end());
@ -202,21 +171,15 @@ public:
first = distribution(random_engine);
do { second = distribution(random_engine); } while (first == second);
auto combined = trainees[first]->decider.combine(trainees[second]->decider, combiner).mutate(mutator);
std::cout << "Łączenie #" << trainees[first]->id << " z #" << trainees[second]->id << " dało #" << (id+1) << std::endl;
add(combined);
add(trainees[first]->decider.combine(trainees[second]->decider, combiner));
}
for (int i = 0; i < to_mutate; i++) {
first = distribution(random_engine);
std::cout << "Mutowanie #" << trainees[first]->id << " w #" << id+1 << std::endl;
add(trainees[first]->decider.mutate(mutator));
}
add(diff - to_combine - to_mutate); // some random things
generation++;
}
std::vector<std::shared_ptr<trained<T>>> population() {