initial commit

This commit is contained in:
Kacper Donat 2018-03-11 12:48:48 +01:00
commit 62fbec9b44
17 changed files with 5576 additions and 0 deletions

40
.gitignore vendored Normal file
View File

@ -0,0 +1,40 @@
*.csv
*.obj
*.exe
*.lst
*.tmp
# tex
*.aux
*.lof
*.log
*.lot
*.fls
*.out
*.toc
*.fmt
*.fot
*.cb
*.cb2
.*.lb
*.pdf
## Build tool auxiliary files:
*.fdb_latexmk
*.synctex
*.synctex(busy)
*.synctex.gz
*.synctex.gz(busy)
*.pdfsync
# hyperref
*.brf
# listings
*.lol
# makeidx
*.idx
*.ilg
*.ind
*.ist

401
argh.h Normal file
View File

@ -0,0 +1,401 @@
#pragma once
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cassert>
namespace argh
{
// Terminology:
// A command line is composed of 2 types of args:
// 1. Positional args, i.e. free standing values
// 2. Options: args beginning with '-'. We identify two kinds:
// 2.1: Flags: boolean options => (exist ? true : false)
// 2.2: Parameters: a name followed by a non-option value
#if !defined(__GNUC__) || (__GNUC__ >= 5)
using string_stream = std::istringstream;
#else
// Until GCC 5, istringstream did not have a move constructor.
// stringstream_proxy is used instead, as a workaround.
class stringstream_proxy
{
public:
stringstream_proxy() = default;
// Construct with a value.
stringstream_proxy(std::string const& value) :
stream_(value)
{}
// Copy constructor.
stringstream_proxy(const stringstream_proxy& other) :
stream_(other.stream_.str())
{
stream_.setstate(other.stream_.rdstate());
}
void setstate(std::ios_base::iostate state) { stream_.setstate(state); }
// Stream out the value of the parameter.
// If the conversion was not possible, the stream will enter the fail state,
// and operator bool will return false.
template<typename T>
stringstream_proxy& operator >> (T& thing)
{
stream_ >> thing;
return *this;
}
// Get the string value.
std::string str() const { return stream_.str(); }
std::stringbuf* rdbuf() const { return stream_.rdbuf(); }
// Check the state of the stream.
// False when the most recent stream operation failed
operator bool() const { return !!stream_; }
~stringstream_proxy() = default;
private:
std::istringstream stream_;
};
using string_stream = stringstream_proxy;
#endif
class parser
{
public:
enum Mode { PREFER_FLAG_FOR_UNREG_OPTION = 1 << 0,
PREFER_PARAM_FOR_UNREG_OPTION = 1 << 1,
NO_SPLIT_ON_EQUALSIGN = 1 << 2,
SINGLE_DASH_IS_MULTIFLAG = 1 << 3,
};
parser() = default;
parser(std::initializer_list<char const* const> pre_reg_names)
{ add_params(pre_reg_names); }
parser(const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION)
{ parse(argv, mode); }
parser(int argc, const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION)
{ parse(argc, argv, mode); }
void add_param(std::string const& name);
void add_params(std::initializer_list<char const* const> init_list);
void parse(const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION);
void parse(int argc, const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION);
std::multiset<std::string> const& flags() const { return flags_; }
std::map<std::string, std::string> const& params() const { return params_; }
std::vector<std::string> const& pos_args() const { return pos_args_; }
// begin() and end() for using range-for over positional args.
std::vector<std::string>::const_iterator begin() const { return pos_args_.cbegin(); }
std::vector<std::string>::const_iterator end() const { return pos_args_.cend(); }
//////////////////////////////////////////////////////////////////////////
// Accessors
// flag (boolean) accessors: return true if the flag appeared, otherwise false.
bool operator[](std::string const& name) const;
// multiple flag (boolean) accessors: return true if at least one of the flag appeared, otherwise false.
bool operator[](std::initializer_list<char const* const> init_list) const;
// returns positional arg string by order. Like argv[] but without the options
std::string const& operator[](size_t ind) const;
// returns a std::istream that can be used to convert a positional arg to a typed value.
string_stream operator()(size_t ind) const;
// same as above, but with a default value in case the arg is missing (index out of range).
template<typename T>
string_stream operator()(size_t ind, T&& def_val) const;
// parameter accessors, give a name get an std::istream that can be used to convert to a typed value.
// call .str() on result to get as string
string_stream operator()(std::string const& name) const;
// accessor for a parameter with multiple names, give a list of names, get an std::istream that can be used to convert to a typed value.
// call .str() on result to get as string
// returns the first value in the list to be found.
string_stream operator()(std::initializer_list<char const* const> init_list) const;
// same as above, but with a default value in case the param was missing.
// Non-string def_val types must have an operator<<() (output stream operator)
// If T only has an input stream operator, pass the string version of the type as in "3" instead of 3.
template<typename T>
string_stream operator()(std::string const& name, T&& def_val) const;
// same as above but for a list of names. returns the first value to be found.
template<typename T>
string_stream operator()(std::initializer_list<char const* const> init_list, T&& def_val) const;
private:
string_stream bad_stream() const;
std::string trim_leading_dashes(std::string const& name) const;
bool is_number(std::string const& arg) const;
bool is_option(std::string const& arg) const;
bool got_flag(std::string const& name) const;
private:
std::vector<std::string> args_;
std::map<std::string, std::string> params_;
std::vector<std::string> pos_args_;
std::multiset<std::string> flags_;
std::set<std::string> registeredParams_;
std::string empty_;
};
//////////////////////////////////////////////////////////////////////////
inline void parser::parse(const char * const argv[], int mode)
{
int argc = 0;
for (auto argvp = argv; *argvp; ++argc, ++argvp);
parse(argc, argv, mode);
}
//////////////////////////////////////////////////////////////////////////
inline void parser::parse(int argc, const char* const argv[], int mode /*= PREFER_FLAG_FOR_UNREG_OPTION*/)
{
// convert to strings
args_.resize(argc);
std::transform(argv, argv + argc, args_.begin(), [](const char* const arg) { return arg; });
// parse line
for (auto i = 0u; i < args_.size(); ++i)
{
if (!is_option(args_[i]))
{
pos_args_.emplace_back(args_[i]);
continue;
}
auto name = trim_leading_dashes(args_[i]);
if (!(mode & NO_SPLIT_ON_EQUALSIGN))
{
auto equalPos = name.find('=');
if (equalPos != std::string::npos)
{
params_.insert({ name.substr(0, equalPos), name.substr(equalPos + 1) });
continue;
}
}
// if the option is unregistered and should be a multi-flag
if (1 == (args_[i].size() - name.size()) && // single dash
argh::parser::SINGLE_DASH_IS_MULTIFLAG & mode && // multi-flag mode
registeredParams_.find(name) == registeredParams_.end()) // unregistered
{
for (auto const& c : name)
{
flags_.emplace(std::string{ c });
}
}
// any potential option will get as its value the next arg, unless that arg is an option too
// in that case it will be determined a flag.
if (i == args_.size() - 1 || is_option(args_[i + 1]))
{
flags_.emplace(name);
continue;
}
// if 'name' is a pre-registered option, then the next arg cannot be a free parameter to it is skipped
// otherwise we have 2 modes:
// PREFER_FLAG_FOR_UNREG_OPTION: a non-registered 'name' is determined a flag.
// The following value (the next arg) will be a free parameter.
//
// PREFER_PARAM_FOR_UNREG_OPTION: a non-registered 'name' is determined a parameter, the next arg
// will be the value of that option.
if (registeredParams_.find(name) != registeredParams_.end() ||
argh::parser::PREFER_PARAM_FOR_UNREG_OPTION & mode)
{
params_.insert({ name, args_[i + 1] });
++i; // skip next value, it is not a free parameter
continue;
}
if (argh::parser::PREFER_FLAG_FOR_UNREG_OPTION & mode)
flags_.emplace(name);
};
}
//////////////////////////////////////////////////////////////////////////
inline string_stream parser::bad_stream() const
{
string_stream bad;
bad.setstate(std::ios_base::failbit);
return bad;
}
//////////////////////////////////////////////////////////////////////////
inline bool parser::is_number(std::string const& arg) const
{
// inefficient but simple way to determine if a string is a number (which can start with a '-')
std::istringstream istr(arg);
double number;
istr >> number;
return !(istr.fail() || istr.bad());
}
//////////////////////////////////////////////////////////////////////////
inline bool parser::is_option(std::string const& arg) const
{
assert(0 != arg.size());
if (is_number(arg))
return false;
return '-' == arg[0];
}
//////////////////////////////////////////////////////////////////////////
inline std::string parser::trim_leading_dashes(std::string const& name) const
{
auto pos = name.find_first_not_of('-');
return std::string::npos != pos ? name.substr(pos) : name;
}
//////////////////////////////////////////////////////////////////////////
inline bool argh::parser::got_flag(std::string const& name) const
{
return flags_.end() != flags_.find(trim_leading_dashes(name));
}
//////////////////////////////////////////////////////////////////////////
inline bool parser::operator[](std::string const& name) const
{
return got_flag(name);
}
//////////////////////////////////////////////////////////////////////////
inline bool parser::operator[](std::initializer_list<char const* const> init_list) const
{
return std::any_of(init_list.begin(), init_list.end(), [&](char const* const name) { return got_flag(name); });
}
//////////////////////////////////////////////////////////////////////////
inline std::string const& parser::operator[](size_t ind) const
{
if (ind < pos_args_.size())
return pos_args_[ind];
return empty_;
}
//////////////////////////////////////////////////////////////////////////
inline string_stream parser::operator()(std::string const& name) const
{
auto optIt = params_.find(trim_leading_dashes(name));
if (params_.end() != optIt)
return string_stream(optIt->second);
return bad_stream();
}
//////////////////////////////////////////////////////////////////////////
inline string_stream parser::operator()(std::initializer_list<char const* const> init_list) const
{
for (auto& name : init_list)
{
auto optIt = params_.find(trim_leading_dashes(name));
if (params_.end() != optIt)
return string_stream(optIt->second);
}
return bad_stream();
}
//////////////////////////////////////////////////////////////////////////
template<typename T>
string_stream parser::operator()(std::string const& name, T&& def_val) const
{
auto optIt = params_.find(trim_leading_dashes(name));
if (params_.end() != optIt)
return string_stream(optIt->second);
std::ostringstream ostr;
ostr << def_val;
return string_stream(ostr.str()); // use default
}
//////////////////////////////////////////////////////////////////////////
// same as above but for a list of names. returns the first value to be found.
template<typename T>
string_stream parser::operator()(std::initializer_list<char const* const> init_list, T&& def_val) const
{
for (auto& name : init_list)
{
auto optIt = params_.find(trim_leading_dashes(name));
if (params_.end() != optIt)
return string_stream(optIt->second);
}
std::ostringstream ostr;
ostr << def_val;
return string_stream(ostr.str()); // use default
}
//////////////////////////////////////////////////////////////////////////
inline string_stream parser::operator()(size_t ind) const
{
if (pos_args_.size() <= ind)
return bad_stream();
return string_stream(pos_args_[ind]);
}
//////////////////////////////////////////////////////////////////////////
template<typename T>
string_stream parser::operator()(size_t ind, T&& def_val) const
{
if (pos_args_.size() <= ind)
{
std::ostringstream ostr;
ostr << def_val;
return string_stream(ostr.str());
}
return string_stream(pos_args_[ind]);
}
//////////////////////////////////////////////////////////////////////////
inline void parser::add_param(std::string const& name)
{
registeredParams_.insert(trim_leading_dashes(name));
}
//////////////////////////////////////////////////////////////////////////
inline void parser::add_params(std::initializer_list<char const* const> init_list)
{
for (auto& name : init_list)
registeredParams_.insert(trim_leading_dashes(name));
}
}

684
data/bitcoin.dat Normal file
View File

@ -0,0 +1,684 @@
8752.05
8604.21
8552.87
8287.12
8429.06
8460.75
8658.94
8670.89
8936.08
8875.21
8761.93
9053.81
9046.36
9253.07
9363.98
9205.57
9280.09
9185.08
9378.03
9294.35
9259.5
9156.88
9112.25
9215.67
9091.97
8958.45
9056.42
9188.81
9073.29
9206.41
9242.42
9192.92
9083.52
8820.61
8766.82
8740.11
8703.82
8707.45
8646.29
8383.08
8408.7
8452.31
8311.77
8200.97
8089.24
8094.67
8391.64
8168.78
8253.91
8091.14
8040.8
8132.57
8271.91
8185.84
8083.47
7984.02
7809.15
7876.02
7681.9
7542.14
7743.17
7650.2
7351.05
7353
7251.96
7063.49
6914.37
6583.56
7243.79
7151.55
6845.35
6914.92
6996.93
6462.69
6380.43
6350.54
6083.38
6512.61
6254.18
5947.4
6100.89
6677.37
6417.44
6433.73
6720.37
7137.72
7158.31
7288.55
7101.07
6954.57
7163.84
7482.86
7495.56
7755.07
7645.33
7692.8
7634.2
7623.8
7364.76
7275.74
7492.05
7448.41
7466.59
7714.34
7665.49
7971.36
8215.22
8265.65
8402.1
8327.76
8238.81
8190.94
7999.72
8323.91
8255.46
8135.75
8152.92
8096.21
7866.53
7548.1
7738.78
7878.67
8034.39
8001.35
8156.37
8017.2
8040.44
8316.21
8235.25
8341.85
8549.46
8483.74
8588.01
8469.15
8434.02
8244.13
8302.5
8186.21
8229.89
8440.2
8297.66
8208.28
8225.47
8248.8
7931.53
7974.82
8094.5
8078.89
7847.66
7877.85
7963.99
8011.26
8091.78
8254.5
8240.15
8257.6
8214.96
8267.04
8407
8314.32
8370.81
8630.7
8629
8639.75
8555.95
8555.58
8581.48
8694.52
8887.54
8820.21
8934.55
8918.78
8829.27
8874
9058.8
8962.08
8678.04
8633.4
8567.92
8658.55
8638.51
8758.23
8649.54
8352.93
8310.39
8449.7
8335.38
8445.88
8173.74
8269.13
8585.02
8550.97
8410.99
8270.12
8254.73
8054.9
7995.72
8131.24
8075.99
7922.86
7920.85
8105.58
8024.6
8194.66
8276.27
8280.88
8441.18
8421.3
8292.31
8185.63
8341.25
8290.72
8244.16
8312.85
8303.26
8073.08
8221
8332.46
8511.7
8479.71
8488.8
8379.01
8421.21
8471.11
8766.42
8804.78
8827.85
8658.38
8758.65
8678.45
8549.87
8646.98
8699.86
8763.38
8662.27
8780.44
8853.86
8830.13
8877.24
8887.78
8915.26
8753.82
8709.14
8765.63
8578.13
8613.21
8716.28
8666.26
8530.21
8450.37
8505.32
8520.16
8581.28
8597.54
8621.56
8507.98
8552.93
8638.78
8617.23
8614.19
8714.73
8564.5
8539.51
8515.59
8587.5
8648.52
8705.14
8689.98
8804.28
8835.9
8794.46
8888.73
8870.09
8798.3
8767.3
8944.43
9177.53
9241.31
9175.41
9331.88
9278.07
9301.83
9290.59
9230.77
9292.33
9282.69
9437.19
9476.28
9370.49
9397.7
9627.51
9666.09
9736.44
9703.04
9690.34
9828.38
9873.79
9644.96
9674.48
9605.89
9733.25
9677.06
9871.39
9841.61
9909.23
10045.73
10051.35
10109.76
10097.32
9883.3
10074.31
10066.99
10226.63
10274.76
10185.64
10110.56
9997.74
10094.74
9891.11
9792.43
9963.97
9880.83
9812.9
9816.47
9867.26
9844.57
10041.89
10050.03
10092.26
10088.73
9962.58
10038.14
9991.9
10083.78
10211.94
10179.32
10182.47
10328.81
10374.78
10468.59
10575.49
10552.41
10780.68
10740.69
10813.46
10675.14
10664.69
10733.66
10833
10713.73
10634.19
10745.17
10817.43
10804.34
10798.32
10728.17
10780.01
10845.14
10953.88
11083.25
11121.37
11221.73
11166.86
10812.12
10929.64
10494.42
10454.69
10612.05
10325.4
10161.7
10518.07
10613.59
10585.11
10744.08
10781.91
10661.38
10574.05
10715.49
10759.62
10846.77
10788.71
10763.23
10586.93
10395.91
10441.32
10632.99
10588.03
10536.67
10535.45
10462.5
10482.68
10821.99
10813.32
10934.34
10906.89
10941.53
10894.32
10940.84
10943.11
11177.16
11083.52
10952.6
11116.87
11148.11
11208.14
11078.64
11081.1
11161.41
11426.74
11483.32
11363.04
11409.91
11464.18
11451.31
11566.08
11563.86
11362.45
11350.49
11405.37
11424.57
11447.76
11549.22
11523.06
11523.39
11590.57
11686.56
11670
11666.96
11714.64
11711.63
11423.48
11199.65
11074.69
11133.94
11024.15
10896.45
10765.74
10853.54
11090.87
11120.95
11034.03
11180.56
10976.51
10956.55
11048.38
10744.16
10680.16
10588.86
10479.72
10535.39
10548.2
10406.04
10367.51
10291.12
10450.65
10454.51
10491.84
10637.42
10743.94
10722.48
10739.95
10798.28
10859.7
10647.46
10470.63
10339.43
10148.67
10237.34
9878.72
9866.23
9901.33
10026.6
9949.93
9832.9
9915.92
10048.96
10090.35
9912.17
9956.74
9833.12
9688.68
9731.87
9737.82
9811.28
9840.16
9864.36
9827.55
9885.47
9917.99
10120.54
10015.45
10217.36
10205.48
10201.47
10317.24
10329.65
10084.15
10117.78
10207.27
10044.21
9993.87
9925.09
10139.96
10161.28
10325.76
10343.58
10438.23
10413.34
10404.42
10457.13
10189.18
10131.6
10033.41
10057.04
9743.23
9809.57
9823.97
9670.52
9797.07
9757.2
9786.37
9642.93
9586.52
9580.38
9401.32
9499.17
9619.66
9678.38
9633.55
9758.75
9626.79
9597.24
9717.92
9723.05
9777.93
9720.4
9618.85
9592.86
9652.99
9656.93
9523.2
9477.75
9405.39
9415.63
9404.22
9367.41
9442.93
9483.93
9588.2
9588.37
9670.98
9570.14
9579.05
9730.25
9733.22
9628.67
9677.86
9637.93
9432.63
9488.19
9446.81
9475.47
9683.39
9740.81
9837
10118.21
10213.28
10242.22
10201.18
10196.02
10222.71
10242.84
10259.75
10366.35
10352.96
10308.15
10363.58
10193.72
10182.28
10270.72
10311.09
10315.32
10321.23
10627.83
10696.2
10725.32
10645.69
10683.84
10670.66
10840.36
10678.5
10575.87
10623.79
10574.43
10611.58
10678.99
10678.91
10744.57
10738.61
10628.72
10647.22
10692.18
10681.68
10900.49
10899.26
10940.25
10798.44
10743.53
10517.09
10594.36
10575.89
10558.32
10389.15
10480.8
10510.08
10500.77
10407.88
10446.83
10489.03
10531.77
10649.08
10557.65
10425.92
10309.81
10392.13
10370.45
10320.69
10350.66
10428.83
10416.56
10384.52
10522.26
10632.81
10600.36
10715.48
10662.01
10626.55
10766.27
10779.06
10712.27
10737.63
10852.77
10942.8
10904.24
11035.56
10924.01
10964.68
10911.55
11029.42
11141.86
11038.72
11100.17
11000.17
10982.89
11096.05
11081.52
11100.3
10890.96
10916.43
10903.09
11008.12
10966.65
10796.32
10836.1
10852.02
10875.33
10920.94
10901.85
11055.84
11015.57
11099.19
11017.6
11077.99
11209.47
11301.59
11305.12
11359.01
11318.66
11330.5
11297.21
11335.16
11313.99
11306.96
11267.59

1000
data/cdr.dat Normal file

File diff suppressed because it is too large Load Diff

365
data/dogecoin.dat Normal file
View File

@ -0,0 +1,365 @@
0.005301
0.000205
0.000206
0.000210
0.000209
0.000208
0.000211
0.000204
0.000211
0.000229
0.000247
0.000238
0.000243
0.000235
0.000223
0.000219
0.000235
0.000236
0.000246
0.000243
0.000242
0.000264
0.000298
0.000272
0.000254
0.000273
0.000272
0.000304
0.000328
0.000335
0.000489
0.000427
0.000432
0.000442
0.000450
0.000398
0.000432
0.000381
0.000400
0.000390
0.000414
0.000425
0.000426
0.000418
0.000454
0.000459
0.000459
0.000448
0.000443
0.000451
0.000464
0.000522
0.000515
0.000527
0.000544
0.000632
0.000678
0.000734
0.000746
0.000686
0.000610
0.000678
0.000744
0.000840
0.001169
0.001305
0.001521
0.001239
0.001239
0.001212
0.001183
0.001184
0.001274
0.001128
0.001059
0.001318
0.001508
0.001518
0.001900
0.003062
0.003405
0.003562
0.003659
0.002994
0.002424
0.002541
0.002579
0.002732
0.002623
0.002657
0.002867
0.003026
0.003251
0.003179
0.003873
0.003741
0.003302
0.003575
0.003453
0.003313
0.003601
0.003253
0.003313
0.003092
0.003043
0.003163
0.003322
0.003230
0.003141
0.003218
0.003125
0.003104
0.003143
0.003135
0.002961
0.002750
0.002623
0.002793
0.002656
0.002602
0.002645
0.002481
0.002533
0.002518
0.002714
0.002636
0.002397
0.002440
0.002330
0.001952
0.001779
0.001938
0.001823
0.001681
0.001484
0.001307
0.001520
0.001864
0.001689
0.001935
0.001936
0.002095
0.001972
0.001980
0.001773
0.001810
0.001856
0.001782
0.001768
0.001719
0.001718
0.001792
0.001788
0.001795
0.001820
0.002087
0.001909
0.001949
0.001961
0.001937
0.001896
0.001880
0.001856
0.001807
0.001762
0.001738
0.001774
0.001710
0.001678
0.001678
0.001707
0.001712
0.001716
0.001732
0.001816
0.001790
0.001778
0.001760
0.001825
0.001867
0.002105
0.002064
0.002543
0.002226
0.002153
0.001786
0.001908
0.002019
0.001928
0.001596
0.001568
0.001395
0.001463
0.001440
0.001398
0.000854
0.000865
0.000867
0.000853
0.000947
0.000890
0.000862
0.000755
0.000772
0.000954
0.000892
0.001141
0.001151
0.001173
0.001080
0.001082
0.001145
0.001084
0.001085
0.001108
0.001024
0.001001
0.001056
0.001066
0.001056
0.000991
0.001036
0.001046
0.001062
0.001066
0.001110
0.001106
0.001164
0.001067
0.001047
0.001064
0.001050
0.001007
0.001023
0.001016
0.001092
0.001056
0.001051
0.001052
0.001037
0.001173
0.001151
0.001124
0.001138
0.001151
0.001121
0.001189
0.001182
0.001154
0.001177
0.001222
0.001415
0.001163
0.001201
0.001038
0.001211
0.001184
0.001339
0.001390
0.001313
0.001373
0.001376
0.001394
0.001389
0.001823
0.001891
0.001908
0.002072
0.002026
0.001988
0.002363
0.002080
0.002079
0.002148
0.002149
0.002223
0.002463
0.002479
0.002663
0.002720
0.002779
0.002617
0.002547
0.002798
0.003304
0.003461
0.003709
0.003762
0.005933
0.005984
0.006433
0.005666
0.006661
0.007425
0.006054
0.007259
0.008582
0.008931
0.009411
0.008872
0.008486
0.009391
0.007860
0.008972
0.008909
0.009145
0.009320
0.009644
0.012167
0.014863
0.017088
0.015045
0.013420
0.013102
0.011469
0.013056
0.013023
0.011685
0.009857
0.006822
0.007648
0.007969
0.007871
0.008448
0.007131
0.006734
0.006673
0.007083
0.007667
0.007237
0.007373
0.007465
0.006967
0.006024
0.006010
0.005029
0.004475
0.005164
0.004399
0.003533
0.004361
0.004297
0.004566
0.005000
0.004989
0.004956
0.005472
0.005638
0.006730
0.007347
0.007034
0.007049
0.006424
0.006697
0.007094
0.006581
0.006259
0.006648
0.006324
0.006142
0.006339
0.006291
0.006018
0.006215
0.005482

962
data/ltc.dat Normal file
View File

@ -0,0 +1,962 @@
1.7906
1.8019
1.75
1.7791
1.8003
1.8498
2.002
2.01
2.8601
2.8358
3.1018
2.74
3.0333
3
2.9975
2.959
2.7733
2.838
2.876
3.125
3.1194
3.7707
4.092
3.9431
4.056
4.151
4.143
4.938
5.38
5.201
6.464
7.567
4.4454
4.328
5.3081
4.7
4.601
4.122
3.71
3.7501
4.01
3.707
3.9719
3.777
3.8696
3.7869
4.6063
4.6779
4.7
4.69
5.087
4.807
4.6071
4.6402
4.1488
4.187
4.195
4.41
4.37
4.0524
4.2484
3.871
3.94
3.9732
4.161
4.0249
3.9
4.054
3.9412
3.98
4.01
3.363
3.4982
3.6036
3.5697
3.5383
3.3824
3.051
2.96
2.9488
2.89
2.9
2.893
2.8565
2.848
2.84
2.8058
2.6482
2.7098
2.951
3.0789
3.0597
3.05
2.9424
2.955
2.965
2.8351
2.816
2.8498
2.8198
2.8148
2.9337
2.9653
2.884
2.851
2.8096
2.8536
2.86
2.9202
2.902
2.8988
2.8748
3.08
2.9839
2.99
3.0135
2.9929
3.031
3.0082
3.0419
3.169
3.0688
3.062
3.115
3.1141
3.1583
3.1675
3.197
3.1649
3.0907
3.101
3.071
3.0261
3.0402
3.0797
3.0787
3.145
3.1318
3.144
3.1011
3.1186
3.1055
3.0988
3.7993
3.95
3.6891
3.89
4.1342
4.55
4
3.85
3.523
3.532
3.3792
3.4223
3.0999
2.9854
3.3
3.2177
3.2299
3.074
3.1974
3.226
3.182
3.14
3.131
3.156
3.1262
3.154
3.1018
3.376
3.6444
3.605
3.546
3.7
3.6255
3.4312
3.33
3.3798
3.335
3.528
3.4399
3.634
3.8273
3.6906
3.653
3.89
3.6
3.58
3.5879
3.7875
3.6807
3.695
3.7201
3.6997
3.5
3.406
3.4085
3.5601
3.6418
3.6011
3.445
3.4678
3.464
3.489
3.452
3.4401
3.5189
3.499
3.4869
3.507
3.489
3.45
3.62
3.5919
3.58
3.535
3.5517
3.4599
3.4719
3.4334
2.95
3.1
3.001
3.055
3.0099
3.37
3.2057
3.0411
3.1075
3.1847
3.0947
3.4239
3.4742
3.4728
3.384
3.3567
3.3635
3.4334
3.4112
3.4197
3.4489
3.435
3.346
3.304
3.2254
3.1726
3.1778
3.241
3.2179
3.2541
3.264
3.365
3.2569
3.2803
3.3039
3.3114
3.313
3.2924
3.1457
3.1582
3.1694
3.192
3.2132
3.226
3.2187
3.2075
3.2372
3.296
3.27
3.228
3.2096
3.254
3.2416
3.2582
3.2443
3.245
3.2687
3.2467
3.2305
3.2324
3.2302
3.233
3.234
3.2625
3.2693
3.26
3.2952
3.3104
3.2849
3.2511
3.3199
3.3
3.375
3.356
3.3698
3.6711
3.83
4.0837
3.8931
3.8298
3.8207
3.67
3.6969
3.6743
3.734
3.7333
3.7203
3.8452
3.98
3.966
4.08
3.8398
3.8807
3.859
3.9351
4.0181
4.0707
4.007
3.9669
4.0212
3.7989
3.87
3.9353
3.8383
3.958
3.95
4.0456
4.1025
4.4453
4.487
4.7503
4.59
4.76
4.7162
4.8967
4.82
4.8632
4.979
4.7738
4.821
4.767
4.8969
5.0993
5.25
5.2789
5.136
5.2588
5.695
5.6492
5.578
5.6031
5.381
4.8546
4.0007
3.888
4.2775
4.243
4.0731
4.175
4.1299
4.0599
4.2091
4.2663
4.6532
4.2809
4.5046
4.4219
4.4799
4.102
4.25
4.1036
4.138
4.1274
4.1843
4.1734
4.1701
4.19
4.16
4.1928
4.1521
4.19
4.1574
4.114
4.036
4.0596
4.0933
4.0102
3.98
3.9536
3.993
4.0767
4.0551
3.942
3.8534
3.7332
3.56
3.746
3.66
3.69
3.63
3.53
3.6
3.5702
3.5907
3.6226
3.6239
3.6257
3.672
3.95
3.8414
3.8159
3.84
3.7586
3.7645
3.776
3.803
3.8107
3.8262
3.8048
3.9154
4.0101
3.9781
3.97
4.0051
4.0289
3.9789
4.0199
3.8977
3.8569
3.845
3.9017
3.86
3.83
3.811
3.8334
3.837
3.7948
3.8011
3.7506
3.808
3.8234
3.8
3.8582
3.836
3.857
3.8529
3.8486
3.85
3.8491
3.8165
3.8142
3.83
3.8478
3.8649
3.8295
3.7866
3.784
3.8304
3.81
3.9329
3.9228
3.892
3.881
3.8365
3.8272
3.8157
3.789
3.8569
3.918
3.8997
3.881
3.916
3.9885
3.992
3.9947
4.0898
4.03
4
4.11
4.1449
3.8601
3.8979
3.9138
3.9184
3.887
3.8635
3.853
3.8192
3.8057
3.7754
3.883
3.8997
3.9031
3.94
3.8979
3.909
3.9569
3.89
3.9476
3.8991
3.8609
3.8997
3.8908
3.8231
3.873
3.8252
3.8809
3.8777
3.9
3.91
3.9056
3.4783
3.52
3.6601
3.68
3.6987
3.6823
3.633
3.6386
3.653
3.6021
3.65
3.6
3.7195
3.672
3.6493
3.656
3.632
3.6742
4.6161
4.431
4.2991
4.257
4.3633
4.5976
4.59
4.4135
4.3635
4.4997
4.58
4.593
4.59
4.2127
3.84
3.963
4.02
4.34
4.574
3.9443
4.0001
3.9262
3.9466
3.9135
3.8927
3.95
3.8949
3.907
3.9098
3.9208
3.85
3.8246
3.733
3.711
3.7425
4.0382
4.0649
4.0352
4.0283
3.9701
3.9403
3.958
3.9208
3.737
3.8296
3.8153
3.7394
3.7698
3.83
3.8933
3.8831
3.8961
3.8139
3.8
3.8086
3.8566
3.9009
3.9057
3.9488
3.8801
3.8747
3.8728
3.8061
3.851
4.0601
4.1105
4
4.0137
4.1003
4.0063
3.8669
3.8734
3.8109
3.806
3.8701
4.3739
4.1707
4.3061
4.2982
4.1
4.0049
3.9371
4.0584
4.0173
3.9546
4.0076
4.1403
4.0817
4.0862
4.1263
4.1837
4.306
7.78
7.13
7.5874
8.333
8.6
9.095
12.4
10.854
10.014
10.732
9.35
9.9251
9.7771
11.79
11.234
11.915
11.848
12.05
11.66
11.521
10.608
11.592
12.851
15.411
16.689
16.2
16.642
16.242
16.022
15.498
16.988
16.84
17.298
17.2
23.001
24.941
26.213
28.674
30.696
28.706
34
32.335
31
27.801
29.399
29.395
25.791
23.72
26.011
29.134
27.373
27.202
25.61
23.931
30.692
33.579
27.211
24.21
22.095
22.817
24.789
23.667
24.777
26.5
26.967
26.413
27.415
29.999
29.762
28.1
29.847
29.506
28.72
33.95
28
30.08
28.266
28.565
33.248
45.358
43.112
47.197
45.77
43.76
45.85
45.23
41.311
40.158
37.78
39.547
41.192
39.269
38.83
37.01
40.4
45.298
54.59
52.75
50.831
45.686
50.924
48.2
44.315
43.435
46.84
45.165
42.35
38.379
40.935
42.129
43.469
40
44.85
45.45
47.01
45.107
45.1
42.081
42.101
41.8
40.439
41.052
40.001
42.107
43.12
41.812
42.579
43.018
46.255
45.486
46
49
47.56
46.5
46.964
45.879
45.276
45.497
42.967
43.911
43.618
46.52
44.976
45.685
47.971
46.25
53.004
49.663
50.916
51.529
60.5
62.14
63.534
65.448
72.691
87.137
78.494
78.527
66.8
73.28
81.111
80.1
72.91
70.75
67.205
68.121
65.945
61.652
45.001
51.821
51.824
50.718
56.289
52.58
51.502
46.399
47.5
49.28
47.3
52.397
52.203
56.83
54.919
52.876
55.56
54.872
53.261
52.083
51.235
51.665
52.056
52.571
53.31
50.068
50.698
50.804
60.503
59.306
63.939
65.86
64.782
59.172
60.499
59.55
59.996
58.014
56.422
54.533
55.635
56.039
55.5
54.958
54.01
56.838
56.18
55.552
52.849
53.989
55.83
54.636
54.441
54.375
60.34
62.409
64.31
58.626
61.686
58.261
61
61.99
63.03
70.654
67.132
69.591
71.72
72.331
69.99
71.735
72.37
77.886
88.443
85.456
90.761
94.003
83.675
85.768
97.841
98.83
99.865
103.42
99.487
96.503
93.56
120.9
149
144.6
210.11
292.12
295.35
276.31
295.55
295.67
313.5
355.5
340.8
304.31
306.99
248.38
269
261.99
261.14
278.78
263.53
247.45
239.51
206.99
225.4
223.51
250.11
244.21
237.76
242.9
278
271.05
252.77
245
249.24
225.52
234.96
257.7
235.24
231.05
181.8
186.71
187.66
191.68
209.85
189.96
179.54
177.64
180.36
178.8
175.37
181.49
196.08
181.51
167.35
164.5
143.2
132
161.35
147.47
125.32
141.81
137.93
149.86
163.61
154.82
148.92
161.39
159.19
211.97
220.47
228.5
228.9
213.57
221.63
228.98
210.05
192.47
206.21
206.33
218.13
218.36
214.96
202.02
209.26
213.32
209.92
208.32

1260
data/msft.dat Normal file

File diff suppressed because it is too large Load Diff

19
decider.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iterator>
#include "decider.h"
macd_decider::macd_decider(std::size_t limit)
: prices(limit, .0), macd(limit, .0), signal(limit, .0), limit(limit) {}
void macd_decider::process(double price)
{
prices.add(price);
macd.add(ema<double>(prices.begin(), prices.begin() + 12) - ema<double>(prices.begin(), prices.begin() + 26));
signal.add(ema<double>(macd.begin(), macd.begin() + 9));
}
void macd_decider::reset()
{
prices = buffer<double>(limit, .0);
signal = buffer<double>(limit, .0);
macd = buffer<double>(limit, .0);
}

106
decider.h Normal file
View File

@ -0,0 +1,106 @@
#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

48
helpers.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef HELPERS_H_
#define HELPERS_H_
#include <deque>
#include <iterator>
using std::deque;
template <typename T, class iter>
T ema(iter begin, iter end)
{
const std::size_t N = std::distance(begin, end);
const T a = T(1) - T(2) / T(N + 1);
T b = T(1);
T nominator = 0, denominator = 0;
for(iter it = begin; it < end; it++) {
nominator += *it * b;
denominator += b;
b *= a;
}
return nominator / denominator;
}
template <typename T>
class buffer : public deque<T>
{
using deque<T>::deque;
std::size_t limit;
public:
buffer(std::size_t limit) : limit(limit), deque<T>(limit) {};
buffer(std::size_t limit, const T& value) : limit(limit), deque<T>(limit, value) {};
void add(const T& value)
{
if (this->size() >= limit) {
this->pop_back();
}
push_front(value);
}
};
#endif

57
main.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
double expavg(const std::vector<double> &values, int start, int n)
{
double a = 1 - 2./(n + 1);
double nominator = 0., denominator = 0.;
double b = 1.;
for (int i = 0; i < n; i++) {
nominator += b*values[start - i];
denominator += b;
b *= a;
}
return nominator / denominator;
}
int main(int argc, char **argv)
{
int low = 12, high = 26, s = 9;
if (argc >= 2)
low = std::atoi(argv[2]);
if (argc >= 3)
high = std::atoi(argv[4]);
if (argc >= 4)
s = std::atoi(argv[3]);
std::vector<double> prices;
double price;
while (std::cin >> price) {
prices.push_back(price);
}
std::vector<double> macd(prices.size());
std::vector<double> signal(prices.size());
for (int i = 0; i < prices.size(); ++i) {
macd[i] = expavg(prices, i, std::min(i, low)) - expavg(prices, i, std::min(i, high));
signal[i] = expavg(macd, i, std::min(i, s));
}
std::cout << "price,macd,signal,delta" << std::endl;
for (int i = 1; i < prices.size(); ++i) {
std::cout << prices[i] << "," << macd[i] << "," << signal[i] << "," << prices[i] - prices[i-1] << std::endl;
}
}

235
matrix.h Normal file
View File

@ -0,0 +1,235 @@
#ifndef MATRIX_H_
#define MATRIX_H_
#include <iostream>
#include <vector>
#include <functional>
template <typename T, int m, int n>
class matrix
{
T values[m*n];
public:
typedef matrix<T, 1, n> row_t;
typedef matrix<T, m, 1> column_t;
matrix()
{
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; j++)
values[i*n + j] = T();
}
matrix(std::initializer_list<std::initializer_list<T>> list)
{
int i = 0, j = 0;
for (const auto& row : list) {
j = 0;
for (const auto& cell : row) {
set(i, j++, cell);
}
i++;
}
}
T get(int i, int j) const
{
return values[i*n + j];
}
void set(int i, int j, T value)
{
values[i*n + j] = value;
}
template <int p> matrix<T, m, p> operator* (const matrix<T, n, p>& rhs) const
{
matrix<T, m, p> result;
for (int i = 0; i < m; ++i) {
for(int j = 0; j < p; j++) {
T accumulator = 0;
for(int k = 0; k < n; k++)
accumulator += this->get(i, k) * rhs.get(k, j);
result.set(i, j, accumulator);
}
}
return result;
}
matrix<T, m, n> operator* (const T& rhs) const
{
matrix <T, m, n> result;
for (int i = 0; i < m; ++i)
for (int j = 0; j < m; ++j)
result.set(i, j, rhs * get(i, j));
return result;
}
matrix<T, n, m> transpose() const
{
matrix<T, n, m> result;
for (int i = 0; i < m; ++i)
for (int j = 0; j < m; ++j)
result.set(j, i, get(i, j));
return result;
}
matrix<T, m, n> operator- () const
{
matrix<T, m, n> result;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
result.set(i, j, -get(i, j));
return result;
}
matrix<T, m, n> operator- (const matrix<T, m, n>& rhs) const
{
return *this + (-rhs);
}
matrix<T, m, n> operator- (const T& value) const
{
return *this + (-value);
}
matrix<T, m, n> operator+ (const matrix<T, m, n>& rhs) const
{
matrix<T, m, n> result;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
result.set(i, j, this->get(i, j) + rhs.get(i, j));
return result;
}
matrix<T, m, n> operator+ (const T& value) const
{
matrix <T, m, n> result;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
result.set(this->get(i, j) + value);
return result;
}
column_t column(std::size_t j)
{
column_t result;
for (int i = 0; i < m; ++i) {
result.set(i, 0, get(i, j));
}
return result;
}
row_t row(std::size_t i)
{
row_t result;
for (int j = 0; j < n; ++j) {
result.set(0, j, get(i, j));
}
return result;
}
friend std::ostream& operator<< (std::ostream& stream, const matrix<T, m, n>& matrix)
{
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; j++) {
stream << matrix.get(i, j) << " ";
}
stream << std::endl;
}
return stream;
}
};
template <typename T, int m, int n> matrix<T, m, n> operator*(const T& lhs, const matrix<T, m, n>& rhs) {
return rhs * lhs;
}
template <typename T, int n> using vector = matrix<T, n, 1>;
template <typename T, int m, int n> void fill(matrix<T, m, n> &mat, std::function<T(const int&, const int&)> filler)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
mat.set(i, j, filler(i, j));
}
template <typename T, int n, int m>
vector<T, m+n> concat(const vector<T, n> &a, const vector<T, m> &b)
{
vector<T, m+n> result;
for (int i = 0; i < n; i++)
result.set(i, 0, a.get(i, 0));
for (int i = 0; i < m; i++)
result.set(i+n, 0, a.get(i, 0));
return result;
}
template <typename T, int n>
vector<T, n> normalize(const vector<T, n> &vec)
{
T accumulator = T();
for (int i = 0; i < n; i++) {
T temp = vec.get(i, 0);
accumulator += temp * temp;
}
accumulator = sqrt(accumulator);
std::function<T(const T&)> normalizer = [accumulator](const T& item) { return item / accumulator; };
return map(vec, normalizer);
}
template <typename T, int m, int n>
matrix<T, m, n> map(const matrix<T, m, n>& matrix, std::function<T(const T&)> func)
{
::matrix<T, m, n> result;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
result.set(i, j, func(matrix.get(i, j)));
return result;
}
template <typename T, int m, int n>
matrix<T, m, n> combine(const matrix<T, m, n>& a, const matrix<T, m, n>& b, std::function<T(const T&, const T&)> func)
{
::matrix<T, m, n> result;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
result.set(i, j, func(a.get(i, j), b.get(i, j)));
return result;
}
template <typename T, int m, int n>
matrix<T, m, n> combine(const matrix<T, m, n>& a, const matrix<T, m, n>& b, T c, T d)
{
return a*c + d*b;
}
#endif

171
network.h Normal file
View File

@ -0,0 +1,171 @@
#ifndef NETWORK_H_
#define NETWORK_H_
#include "matrix.h"
#include <functional>
template <typename T, int in, int out>
struct layer {
using filler_t = std::function<T(const int&, const int&)>;
using combiner_t = std::function<T(const T&, const T&)>;
matrix<T, out, in> weights;
vector<T, out> bias;
vector<T, out> evaluate(vector<T, in> input)
{
return weights * input - bias;
}
void fill(filler_t weight_filler, filler_t bias_filler)
{
::fill(weights, weight_filler);
::fill(bias, bias_filler);
}
void fill(filler_t filler)
{
// use same filler for both
fill(filler, filler);
}
layer<T, in, out> combine(const layer<T, in, out> &rhs, combiner_t weight_combiner, combiner_t bias_combiner)
{
layer<T, in, out> result;
result.weights = ::combine(weights, rhs.weights, weight_combiner);
result.bias = ::combine(bias, rhs.bias, bias_combiner);
return result;
}
layer<T, in, out> combine(const layer<T, in, out> &rhs, combiner_t combiner)
{
return combine(rhs, combiner, combiner);
}
};
template <std::size_t N, typename T, int ...inputs> struct layer_types;
template <std::size_t N, typename T, int in, int out, int ...rest>
struct layer_types<N, T, in, out, rest...> : layer_types<N-1, T, out, rest...> { };
template<typename T, int in, int out, int ...rest>
struct layer_types<0, T, in, out, rest...> {
using type = layer<T, in, out>;
};
template <typename T, int ...layers> class network;
template <typename T, int in, int out>
class network<T, in, out> {
protected:
layer<T, in, out> current;
public:
static const std::size_t layer_count = 1;
using self = network<T, in, out>;
using output = vector<T, out>;
using input = vector<T, in>;
using filler_t = typename layer<T, in, out>::filler_t;
using combiner_t = typename layer<T, in, out>::combiner_t;
using normalizer_t = std::function<T(const T&)>;
template <std::size_t N> using layer_type = typename layer_types<N, T, in, out>::type;
normalizer_t normalizer;
network(normalizer_t normalizer) : normalizer(normalizer) { }
network() : network([](const T& value) { return value; }) { };
output evaluate(input inp)
{
return map(current.evaluate(inp), normalizer);
}
template <std::size_t N> layer_type<N>& get()
{
return current;
}
void fill(filler_t weights, filler_t bias)
{
current.fill(weights, bias);
}
void fill(filler_t filler)
{
current.fill(filler);
}
self combine(self& rhs, combiner_t weight_combiner, combiner_t bias_combiner) {
self result;
result.template get<0>() = get<0>().combine(rhs.template get<0>(), weight_combiner, bias_combiner);
return result;
}
self combine(self& rhs, combiner_t combiner)
{
return combine(rhs, combiner, combiner);
}
};
template <typename T, int in, int out, int ...layers>
class network<T, in, out, layers...> : public network<T, in, out> {
using base = network<T, in, out>;
using self = network<T, in, out, layers...>;
network<T, out, layers...> subnetwork;
public:
network(typename base::normalizer_t normalizer) : base(normalizer), subnetwork(normalizer) {}
network() : network([](const T& value) { return value; }) { };
using output = typename network<T, out, layers...>::output;
static const std::size_t layer_count = sizeof...(layers) + 1;
template <std::size_t N> using layer_type = typename layer_types<N, T, in, out, layers...>::type;
template <std::size_t N> layer_type<N>& get()
{
return subnetwork.template get<N-1>();
}
template<> layer_type<0>& get<0>()
{
return base::template get<0>();
}
output evaluate(typename base::input inp)
{
auto result = base::evaluate(inp);
return subnetwork.evaluate(result);
}
void fill(typename base::filler_t filler)
{
base::fill(filler);
subnetwork.fill(filler);
}
void fill(typename base::filler_t weights, typename base::filler_t bias)
{
base::fill(weights, bias);
subnetwork.fill(weights, bias);
}
self combine(self& rhs, typename base::combiner_t weight_combiner, typename base::combiner_t bias_combiner) {
self result;
result.template get<0>() = get<0>().combine(rhs.template get<0>(), weight_combiner, bias_combiner);
result.subnetwork = subnetwork.combine(rhs.subnetwork);
return result;
}
};
#endif

16
sprawozdanie/macd.tex Normal file
View File

@ -0,0 +1,16 @@
\begin{axis}[
axis lines=center, no marks,
width=\linewidth, height=.66\linewidth,
]
\addplot table [x expr=\coordindex, y=price, col sep=comma] {\file};
\end{axis}
\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}

View File

@ -0,0 +1,61 @@
\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage{polski}
\usepackage[utf8]{inputenc}
\usepackage[margin=1.25in]{geometry}
\usepackage{alltt}
\usepackage{titling}
\usepackage{pdfpages}
\usepackage{float}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{amstext}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{xspace}
\usepackage{enumerate}
\usepackage{lmodern}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{alphalph}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{wrapfig}
\usepackage[polish]{babel}
\usepackage{braket}
\usepackage{subcaption}
\pgfplotsset{compat=1.15}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\usetikzlibrary{decorations.pathmorphing, arrows.meta, positioning}
\usetikzlibrary{shapes.geometric, arrows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
% opening
\title{Analiza Techniczna - MACD \\ \normalsize Projekt \#1 z \texttt{MN}}
\author{Kacper Donat}
\newenvironment{column}[1]{\noindent\begin{minipage}{#1\linewidth}}{\end{minipage}\vspace{.5\baselineskip}}
\floatname{algorithm}{Program}
\begin{document}
\newcommand{\macd}[1]{\def\file{#1}\input{macd.tex}}
\maketitle
\begin{figure}[H]
\begin{tikzpicture}
\macd{msft.csv}
\end{tikzpicture}
\caption{XD}
\end{figure}
\end{document}

99
trainer.h Normal file
View File

@ -0,0 +1,99 @@
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
template<class T>
struct trained {
unsigned int id;
T decider;
double monies;
unsigned stock;
double result;
};
template <class T>
class trainer {
std::vector< trained<T>* > trainees;
std::function<T()> factory;
unsigned int id;
public:
double money;
unsigned stock;
trainer(double money, unsigned stock, std::size_t n, std::function<T()> factory)
: factory(factory), id(0), money(money), stock(stock)
{
add(n);
}
void add(std::size_t n, std::function<T()> factory)
{
for(std::size_t i = 0; i < n; i++) {
trained<T> *trainee = new trained<T>();
trainee->id = ++id;
trainee->result = 0.0;
trainee->decider = factory();
trainees.push_back(trainee);
}
}
void add(std::size_t n)
{
add(n, factory);
}
void test(std::istream& input)
{
for (auto trainee : trainees) {
trainee->monies = money;
trainee->stock = stock;
trainee->decider.start_money = money;
trainee->decider.start_stock = stock;
}
double price, start;
input >> price;
start = price*stock + money;
do {
for (auto trainee : trainees) {
auto decision = trainee->decider.decide(price, trainee->monies, trainee->stock);
auto current = price*trainee->stock + trainee->monies;
auto max_credit = std::max(current * 0.05, -1e4);
/* std::cout << "D: " << decision << " C: " << current << " P: " << price << " MC: " << max_credit << std::endl; */
if (decision > 0 && trainee->monies - decision*price < -max_credit) {
decision = floor((trainee->monies + max_credit) / price);
}
if (decision < 0 && -decision > trainee->stock) {
decision = -trainee->stock;
}
trainee->stock += decision;
trainee->monies -= price*decision;
}
} while (input >> price);
auto hodl = price * stock + money;
std::cout << "Zakonczono testy " << trainees.size() << " przypadkow." << std::endl;
std::cout << "HODL: " << hodl << " START: " << start << std::endl;
std::cout << "-----------------------" << std::endl;
std::sort(trainees.begin(), trainees.end(), [=](trained<T> *a, trained<T> *b){
return (a->monies + a->stock * price) > (b->monies + b->stock * price);
});
for (auto trainee : trainees) {
auto earned = trainee->monies + trainee->stock * price;
std::cout << "#" << trainee->id << ": " << earned << " [" << earned - hodl << "] " << trainee->stock << " akcji, " << trainee->monies << " gelda w banku. " << std::endl;
}
}
};

52
wtf.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include <functional>
#include <math.h>
#include <typeinfo>
#include <random>
#include <ctime>
#include <algorithm>
#include <vector>
#include "argh.h"
#include "matrix.h"
#include "network.h"
#include "decider.h"
#include "trainer.h"
int main(int argc, char* argv[])
{
argh::parser args;
args.add_params({"-m", "--money"});
args.add_params({"-s", "--stock"});
args.add_params({"-p", "--population"});
args.parse(argc, argv);
double money;
unsigned stock, population;
args({ "m", "money" }, 1000.) >> money;
args({ "s", "stock" }, 1000) >> stock;
args({ "p", "population" }, 25) >> population;
std::uniform_real_distribution<double> distribution(-1.0, 1.0);
std::default_random_engine random_engine;
random_engine.seed(std::time(0));
std::function<double(const int&, const int&)> randomizer = [&](const int& i, const int& j) -> double {
return distribution(random_engine);
};
std::function<double(const double&)> normalizer = [](const double& result) -> double { return erf(result); };
using current_decider = neural_decider<24, 12, 12, 32, 16>;
std::function<current_decider()> factory = [&]() -> current_decider {
current_decider decider(normalizer);
decider.network.fill(randomizer);
return decider;
};
trainer<current_decider> train(money, stock, population, factory);
train.test(std::cin);
}