Skip to content

Commit

Permalink
Merge pull request #4 from ctubio/master
Browse files Browse the repository at this point in the history
merge latest
  • Loading branch information
serzhiio committed Nov 2, 2017
2 parents 5a25500 + a38683c commit ae4bebc
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -15,7 +15,7 @@ V_UWS := 0.14.4
V_UV := 1.15.0
V_SQL := 3200100
V_QF := v.1.14.4
KZIP = 15e004d5b7125494d0d294438237f4f45367b2db
KZIP = a20ae85a275121c0fddfdd04f4c0ee8afba6b972
KARGS = -Wextra -std=c++11 -O3 -I$(KLOCAL)/include \
src/server/K.cxx -pthread -rdynamic -DUSE_LIBUV \
-DK_STAMP='"$(shell date --rfc-3339=seconds | cut -f1 -d+)"' \
Expand Down
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -16,7 +16,7 @@
[![Dependency Status](https://img.shields.io/david/ctubio/Krypto-trading-bot.svg)](https://david-dm.org/ctubio/Krypto-trading-bot)
[![Open Issues](https://img.shields.io/github/issues/ctubio/Krypto-trading-bot.svg)](https://github.com/ctubio/Krypto-trading-bot/issues)
[![Open Issues](https://img.shields.io/github/issues/ctubio/tribeca.svg)](https://github.com/ctubio/tribeca/issues)
[![Downloads Last 21 Commits](https://img.shields.io/github/downloads/ctubio/Krypto-trading-bot/latest/total.svg?maxAge=2592000&label=downloads%20last%2021%20commits)](https://github.com/ctubio/Krypto-trading-bot)
[![Downloads Last 21 Commits](https://img.shields.io/github/downloads/ctubio/Krypto-trading-bot/total.svg?maxAge=2592000&label=downloads%20last%2021%20commits)](https://github.com/ctubio/Krypto-trading-bot)

Runs on unix-like systems. Persistence is achieved using a built-in server-less SQLite C++ interface. Installation via Docker is supported, but manual installation in a dedicated [Debian](https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/) (or [Raspbian](https://www.raspberrypi.org/downloads/raspbian/)) or [CentOS](https://wiki.centos.org/Download) instance is recommended.

Expand Down Expand Up @@ -108,9 +108,12 @@ To upgrade anytime just run `make reinstall`.

See [configuration](#configuration) section while setting up the configuration options in your new config file `K.sh`.

Once the config file is ready, execute `./K.sh`.
Once the config file is ready, it can be executed simply like:
```
$ ./K.sh
```

Or `make start` to run `K.sh` in the background using [screen](https://www.decf.berkeley.edu/help/unix/screen.html); to see the output, attach the screen with `make screen`.
Alternatively use `make start` to run `K.sh` in the background using [screen](https://www.decf.berkeley.edu/help/unix/screen.html) (to see the output, attach the screen with `make screen` [or run all at once with `make start screen`]).

Feel free to run `make stop` or `make restart` anytime, and don't forget to [read the fucking manual](https://github.com/ctubio/Krypto-trading-bot/blob/master/MANUAL.md).

Expand Down Expand Up @@ -356,7 +359,8 @@ To request new features open a [new issue](https://github.com/ctubio/Krypto-trad

To report errors open a [new issue](https://github.com/ctubio/Krypto-trading-bot/issues/new?title=Error%20report) only after collecting all possible relevant log messages.

Prior make pull requests please understand that your biological and technological distinctiveness will be added to our own. Resistance is futile.
Pull Requests are welcome, but adhere to the Contributor License Agreement:
- Your biological and technological distinctiveness will be added to our own. Resistance is futile.

### Votes

Expand Down
6 changes: 3 additions & 3 deletions src/server/gw.h
Expand Up @@ -95,7 +95,7 @@ namespace K {
mConnectivity autoStart = (mConnectivity)k["state"].get<int>();
if (autoStart != gwAutoStart) {
gwAutoStart = autoStart;
gwUpState();
gwStateUp();
}
};
static void gwConnUp(mGatewayType gwT, mConnectivity gwS) {
Expand All @@ -108,10 +108,10 @@ namespace K {
}
gwConnectExchange = gwConnectMarket == mConnectivity::Connected and gwConnectOrder == mConnectivity::Connected
? mConnectivity::Connected : mConnectivity::Disconnected;
gwUpState();
gwStateUp();
UI::uiSend(uiTXT::ExchangeConnectivity, {{"status", (int)gwConnectExchange}});
};
static void gwUpState() {
static void gwStateUp() {
mConnectivity quotingState = gwConnectExchange;
if (quotingState == mConnectivity::Connected) quotingState = gwAutoStart;
if (quotingState != gwQuotingState) {
Expand Down
7 changes: 7 additions & 0 deletions src/server/km.h
Expand Up @@ -157,6 +157,13 @@ namespace K {
baseValue(b), quoteValue(q), time(t)
{};
};
static void to_json(json& j, const mProfit& k) {
j = {
{"baseValue", k.baseValue},
{"quoteValue", k.quoteValue},
{"time", k.time}
};
};
struct mSafety {
double buy,
sell,
Expand Down
2 changes: 1 addition & 1 deletion src/server/mg.h
Expand Up @@ -31,7 +31,7 @@ namespace K {
void load() {
json k = DB::load(uiTXT::MarketData);
if (k.size()) {
for (json::iterator it = k.begin(); it != k.end(); ++it) {
for (json::reverse_iterator it = k.rbegin(); it != k.rend(); ++it) {
if (it->value("time", (unsigned long)0)+qp.quotingStdevProtectionPeriods*1e+3<FN::T()) continue;
mgStatFV.push_back(it->value("fv", 0.0));
mgStatBid.push_back(it->value("bid", 0.0));
Expand Down
2 changes: 1 addition & 1 deletion src/server/og.h
Expand Up @@ -10,7 +10,7 @@ namespace K {
void load() {
json k = DB::load(uiTXT::Trades);
if (k.size())
for (json::iterator it = k.begin(); it != k.end(); ++it)
for (json::reverse_iterator it = k.rbegin(); it != k.rend(); ++it)
tradesMemory.push_back(mTrade(
(*it)["tradeId"].get<string>(),
(mExchange)(*it)["exchange"].get<int>(),
Expand Down
20 changes: 18 additions & 2 deletions src/server/pg.h
Expand Up @@ -4,6 +4,7 @@
namespace K {
mPosition pgPos;
mSafety pgSafety;
vector<mProfit> pgProfit;
map<double, mTrade> pgBuys;
map<double, mTrade> pgSells;
double pgTargetBasePos = 0;
Expand All @@ -20,6 +21,16 @@ namespace K {
stringstream ss;
ss << setprecision(8) << fixed << pgTargetBasePos;
FN::log("DB", string("loaded TBP = ") + ss.str() + " " + gw->base);
k = DB::load(uiTXT::Position);
if (k.size()) {
for (json::reverse_iterator it = k.rbegin(); it != k.rend(); ++it)
pgProfit.push_back(mProfit(
(*it)["baseValue"].get<double>(),
(*it)["quoteValue"].get<double>(),
(*it)["time"].get<unsigned long>()
));
FN::log("DB", string("loaded ") + to_string(pgProfit.size()) + " historical Profits");
}
};
void waitData() {
ev_gwDataWallet = [](mWallet k) {
Expand Down Expand Up @@ -219,10 +230,10 @@ namespace K {
return sum;
};
static void calcWallet(mWallet k) {
static unsigned long profitT_21s = 0;
static mutex walletMutex,
profitMutex;
static map<string, mWallet> pgWallet;
static vector<mProfit> pgProfit;
walletMutex.lock();
if (k.currency!="") pgWallet[k.currency] = k;
if (!mgFairValue or pgWallet.find(gw->base) == pgWallet.end() or pgWallet.find(gw->quote) == pgWallet.end()) {
Expand All @@ -235,8 +246,13 @@ namespace K {
double baseValue = baseWallet.amount + quoteWallet.amount / mgFairValue + baseWallet.held + quoteWallet.held / mgFairValue;
double quoteValue = baseWallet.amount * mgFairValue + quoteWallet.amount + baseWallet.held * mgFairValue + quoteWallet.held;
unsigned long now = FN::T();
mProfit profit(baseValue, quoteValue, now);
if (profitT_21s+21e+3 < FN::T()) {
profitT_21s = FN::T();
DB::insert(uiTXT::Position, profit, false, "NULL", now - qp.profitHourInterval * 36e+5);
}
profitMutex.lock();
pgProfit.push_back(mProfit(baseValue, quoteValue, now));
pgProfit.push_back(profit);
for (vector<mProfit>::iterator it = pgProfit.begin(); it != pgProfit.end();)
if (it->time + (qp.profitHourInterval * 36e+5) > now) ++it;
else it = pgProfit.erase(it);
Expand Down
5 changes: 3 additions & 2 deletions src/server/qp.h
Expand Up @@ -173,9 +173,10 @@ namespace K {
protected:
void load() {
json k = DB::load(uiTXT::QuotingParametersChange);
if (k.size())
if (k.size()) {
qp = k.at(0);
FN::log("DB", string("loaded Quoting Parameters ") + (k.size() ? "OK" : "OR reading defaults instead"));
FN::log("DB", "loaded Quoting Parameters OK");
} else FN::logWar("QP", "using default values for Quoting Parameters");
};
void waitUser() {
UI::uiSnap(uiTXT::QuotingParametersChange, &onSnap);
Expand Down

0 comments on commit ae4bebc

Please sign in to comment.