Skip to content

Commit

Permalink
Merge pull request #16 from b-seite/master
Browse files Browse the repository at this point in the history
Update from ctubio
  • Loading branch information
b-seite committed Nov 21, 2017
2 parents 49ba14b + 1d18373 commit 24b49a0
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 94 deletions.
21 changes: 6 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ V_CURL := 7.55.1
V_NCUR := 6.0
V_JSON := v2.1.1
V_UWS := 0.14.4
V_UV := 1.15.0
V_SQL := 3200100
V_QF := v.1.14.4
V_PVS := 6.19.23789.1731
KZIP = 3916a5af67afc112c8c273c24c7b5842af2b40ed
KZIP = 0b57ac8dc50c7fe025e6a8418e6e300d2677e668
KARGS = -Wextra -std=c++11 -O3 -I$(KLOCAL)/include \
src/server/K.cxx -pthread -rdynamic -DUSE_LIBUV \
src/server/K.cxx -pthread -rdynamic -DUWS_THREADSAFE \
-DK_STAMP='"$(shell date --rfc-3339=seconds | cut -f1 -d+)"' \
-DK_BUILD='"$(CHOST)"' $(KLOCAL)/include/uWS/*.cpp \
$(KLOCAL)/lib/K-$(CHOST).a $(KLOCAL)/lib/libquickfix.a \
$(KLOCAL)/lib/libsqlite3.a $(KLOCAL)/lib/libz.a \
$(KLOCAL)/lib/libcurl.a $(KLOCAL)/lib/libssl.a \
$(KLOCAL)/lib/libcrypto.a $(KLOCAL)/lib/libncurses.a \
$(KLOCAL)/lib/libuv.a -ldl
$(KLOCAL)/lib/libcrypto.a $(KLOCAL)/lib/libncurses.a -ldl

all: K

Expand Down Expand Up @@ -70,7 +68,6 @@ help:
# make pvs - download pvs src files #
# make zlib - download zlib src files #
# make curl - download curl src files #
# make libuv - download libuv src files #
# make ncurses - download ncurses src files #
# make sqlite - download sqlite src files #
# make openssl - download openssl src files #
Expand Down Expand Up @@ -98,7 +95,7 @@ ifdef KALL
unset KALL && echo -n $(CARCH) | xargs -I % -d ' ' $(MAKE) CHOST=% $@
else
mkdir -p build-$(CHOST)
CHOST=$(CHOST) $(MAKE) zlib openssl curl sqlite ncurses libuv json uws quickfix
CHOST=$(CHOST) $(MAKE) zlib openssl curl sqlite ncurses json uws quickfix
test -f /sbin/ldconfig && sudo ldconfig || :
endif

Expand Down Expand Up @@ -150,12 +147,6 @@ ncurses: build-$(CHOST)
--prefix=$(PWD)/$(KLOCAL) --with-fallbacks=linux,screen,vt100,xterm,xterm-256color,putty-256color \
&& make && make install )

libuv: build-$(CHOST)
test -d build-$(CHOST)/libuv-$(V_UV) || ( \
curl -L https://github.com/libuv/libuv/archive/v$(V_UV).tar.gz | tar xz -C build-$(CHOST) \
&& cd build-$(CHOST)/libuv-$(V_UV) && sh autogen.sh && CC=$(CC) ./configure --host=$(CHOST) \
--prefix=$(PWD)/$(KLOCAL) && make && make install )

json: build-$(CHOST)
test -f $(KLOCAL)/include/json.h || (mkdir -p $(KLOCAL)/include \
&& curl -L https://github.com/nlohmann/json/releases/download/$(V_JSON)/json.hpp \
Expand Down Expand Up @@ -195,7 +186,7 @@ cleandb: /data/db/K*
packages:
test -n "`command -v apt-get`" && sudo apt-get -y install g++ build-essential automake autoconf libtool libxml2 libxml2-dev zlib1g-dev openssl stunnel python curl gzip imagemagick screen \
|| (test -n "`command -v yum`" && sudo yum -y install gcc-c++ automake autoconf libtool libxml2 libxml2-devel openssl stunnel python curl gzip ImageMagick screen) \
|| (test -n "`command -v brew`" && (xcode-select --install || :) && (brew install automake autoconf libxml2 sqlite openssl zlib libuv stunnel python curl gzip imagemagick || brew upgrade || :)) \
|| (test -n "`command -v brew`" && (xcode-select --install || :) && (brew install automake autoconf libxml2 sqlite openssl zlib stunnel python curl gzip imagemagick || brew upgrade || :)) \
|| (test -n "`command -v pacman`" && sudo pacman --noconfirm -S --needed base-devel libxml2 zlib sqlite curl libcurl-compat openssl stunnel python gzip imagemagick screen)
sudo mkdir -p /data/db/
sudo chown $(shell id -u) /data/db
Expand Down Expand Up @@ -362,4 +353,4 @@ md5: src
asandwich:
@test `whoami` = 'root' && echo OK || echo make it yourself!

.PHONY: K dist link Linux Darwin build zlib openssl curl ncurses libuv quickfix uws json pvs clean cleandb list screen start stop restart startall stopall restartall gdax packages install docker travis reinstall client www bundle diff latest changelog test test-cov send-cov png png-check release md5 asandwich
.PHONY: K dist link Linux Darwin build zlib openssl curl ncurses quickfix uws json pvs clean cleandb list screen start stop restart startall stopall restartall gdax packages install docker travis reinstall client www bundle diff latest changelog test test-cov send-cov png png-check release md5 asandwich
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,6 @@ or donate your time with programming or financial suggestions in the topical IRC
- https://github.com/michaelgrosner/tribeca (https://github.com/michaelgrosner)
- https://www.sqlite.org
- https://github.com/michaelgrosner/tribeca (https://github.com/michaelgrosner)
- http://libuv.org
- https://github.com/michaelgrosner/tribeca (https://github.com/michaelgrosner)

### Help

Expand Down
8 changes: 4 additions & 4 deletions src/client/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ export enum SOP { Off, Trades, Size, TradesSize }
export enum STDEV { Off, OnFV, OnFVAPROff, OnTops, OnTopsAPROff, OnTop, OnTopAPROff }

export interface QuotingParameters {
widthPing?: number;
widthPing?: any;
widthPingPercentage?: number;
widthPong?: number;
widthPong?: any;
widthPongPercentage?: number;
widthPercentage?: boolean;
bestWidth?: boolean;
buySize?: number;
buySize?: any;
buySizePercentage?: number;
buySizeMax?: boolean;
sellSize?: number;
sellSize?: any;
sellSizePercentage?: number;
sellSizeMax?: boolean;
pingAt?: PingAt;
Expand Down
21 changes: 20 additions & 1 deletion src/client/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ class FormViewModel<T> {
this.display = JSON.parse(JSON.stringify(defaultParameter));
}

public cleanDecimal = (p: T) => {
return p;
}

public reset = () => {
this.display = JSON.parse(JSON.stringify(this.master));
};

public update = (p: T) => {
p = this.cleanDecimal(p);
this.master = JSON.parse(JSON.stringify(p));
this.display = JSON.parse(JSON.stringify(p));
this.pending = false;
Expand Down Expand Up @@ -70,7 +75,13 @@ class DisplayQuotingParameters extends FormViewModel<Models.QuotingParameters> {

constructor(sub: Subscribe.ISubscribe<Models.QuotingParameters>,
fire: Subscribe.IFire<Models.QuotingParameters>) {
super(<Models.QuotingParameters>{}, sub, fire);
super(<Models.QuotingParameters>{}, sub, fire, (d: any) => {
d.widthPing = parseFloat(d.widthPing);
d.widthPong = parseFloat(d.widthPong);
d.buySize = parseFloat(d.buySize);
d.sellSize = parseFloat(d.sellSize);
return d;
});

this.availableQuotingModes = DisplayQuotingParameters.getMapping(Models.QuotingMode);
this.availableQuotingSafeties = DisplayQuotingParameters.getMapping(Models.QuotingSafety);
Expand All @@ -84,6 +95,14 @@ class DisplayQuotingParameters extends FormViewModel<Models.QuotingParameters> {
this.availableSTDEV = DisplayQuotingParameters.getMapping(Models.STDEV);
}

public cleanDecimal = (d: any) => {
d.widthPing = parseFloat(d.widthPing).toFixed(13).replace(/0+$/,'').replace(/\.$/,'');
d.widthPong = parseFloat(d.widthPong).toFixed(13).replace(/0+$/,'').replace(/\.$/,'');
d.buySize = parseFloat(d.buySize).toFixed(13).replace(/0+$/,'').replace(/\.$/,'');
d.sellSize = parseFloat(d.sellSize).toFixed(13).replace(/0+$/,'').replace(/\.$/,'');
return d;
}

private static getMapping<T>(enumObject: T) {
let names = [];
for (let mem in enumObject) {
Expand Down
1 change: 0 additions & 1 deletion src/server/K.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

using namespace std;

#include "uv.h"
#include "json.h"
#include "sqlite3.h"
#include "uWS/uWS.h"
Expand Down
51 changes: 21 additions & 30 deletions src/server/ev.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ namespace K {
class EV: public Klass {
private:
uWS::Hub *hub = nullptr;
int eCode = EXIT_FAILURE;
public:
mutex hubMutex;
uWS::Group<uWS::SERVER> *uiGroup = nullptr;
uv_timer_t *tCalcs = nullptr,
*tStart = nullptr,
*tDelay = nullptr,
*tWallet = nullptr,
*tCancel = nullptr;
Timer *tCalcs = nullptr,
*tStart = nullptr,
*tDelay = nullptr,
*tWallet = nullptr,
*tCancel = nullptr;
function<void(mOrder)> ogOrder;
function<void(mTrade)> ogTrade;
function<void()> mgLevels,
Expand All @@ -32,14 +30,13 @@ namespace K {
gw->hub = hub = new uWS::Hub(0, true);
};
void waitTime() {
uv_timer_init(hub->getLoop(), tCalcs = new uv_timer_t());
uv_timer_init(hub->getLoop(), tStart = new uv_timer_t());
uv_timer_init(hub->getLoop(), tDelay = new uv_timer_t());
uv_timer_init(hub->getLoop(), tWallet = new uv_timer_t());
uv_timer_init(hub->getLoop(), tCancel = new uv_timer_t());
tCalcs = new Timer(hub->getLoop());
tStart = new Timer(hub->getLoop());
tDelay = new Timer(hub->getLoop());
tWallet = new Timer(hub->getLoop());
tCancel = new Timer(hub->getLoop());
};
void waitData() {
gw->hubMutex = &hubMutex;
gw->gwGroup = hub->createGroup<uWS::CLIENT>();
};
void waitUser() {
Expand All @@ -57,23 +54,17 @@ namespace K {
public:
void start() {
hub->run();
halt(eCode);
};
void stop(int code, function<void()> gwCancelAll) {
eCode = code;
if (uv_loop_alive(hub->getLoop())) {
uv_timer_stop(tCancel);
uv_timer_stop(tWallet);
uv_timer_stop(tCalcs);
uv_timer_stop(tStart);
uv_timer_stop(tDelay);
gw->close();
gw->gwGroup->close();
gwCancelAll();
uiGroup->close();
FN::close(hub->getLoop());
hub->getLoop()->destroy();
}
tCancel->stop();
tWallet->stop();
tCalcs->stop();
tStart->stop();
tDelay->stop();
gw->close();
gw->gwGroup->close();
gwCancelAll();
uiGroup->close();
halt(code);
};
void listen(int port) {
Expand All @@ -97,15 +88,15 @@ namespace K {
cout << FN::uiT();
for(unsigned int i = 0; i < 21; ++i)
cout << "THE END IS NEVER ";
cout << "THE END" << '\n';
cout << "THE END." << '\n';
halt(code);
};
static void quit(int sig) {
FN::screen_quit();
cout << '\n';
json k = FN::wJet("https://api.icndb.com/jokes/random?escape=javascript&limitTo=[nerdy]");
cout << FN::uiT() << "Excellent decision! "
<< ((k.is_null() || !k["/value/joke"_json_pointer].is_string())
<< ((k.is_null() or !k["/value/joke"_json_pointer].is_string())
? "let's plant a tree instead.." : k["/value/joke"_json_pointer].get<string>()
) << '\n';
(*evExit)(EXIT_SUCCESS);
Expand Down
10 changes: 1 addition & 9 deletions src/server/fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ namespace K {
signal(SIGWINCH, screen_resize);
thread([&]() {
int ch;
while ((ch = wgetch(wBorder)) != 'q') {
while ((ch = wgetch(wBorder)) != 'q' and ch != 'Q') {
switch (ch) {
case ERR: continue;
// case KEY_PPAGE: wscrl(wLog, -3); wrefresh(wLog); break;
Expand Down Expand Up @@ -768,14 +768,6 @@ namespace K {
redrawwin(wLog);
wrefresh(wLog);
};
static void close(uv_loop_t* loop) {
uv_walk(loop, close_walk_cb, NULL);
uv_run(loop, UV_RUN_DEFAULT);
};
static void close_walk_cb(uv_handle_t* handle, void* arg) {
if (!uv_is_closing(handle))
uv_close(handle, NULL);
};
};
class B64 {
public:
Expand Down
10 changes: 5 additions & 5 deletions src/server/gw.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace K {
handshake(gw->exchange);
};
void waitTime() {
((EV*)events)->tWallet->data = (void*)this;
uv_timer_start(((EV*)events)->tWallet, [](uv_timer_t *handle) {
((EV*)events)->tWallet->setData(this);
((EV*)events)->tWallet->start([](Timer *handle) {
GW *k = (GW*)handle->data;
if (((CF*)k->config)->argDebugEvents) FN::log("DEBUG", "EV GW tWallet timer");
k->gw->wallet();
}, 0, 15e+3);
((EV*)events)->tCancel->data = (void*)this;
uv_timer_start(((EV*)events)->tCancel, [](uv_timer_t *handle) {
((EV*)events)->tCancel->setData(this);
((EV*)events)->tCancel->start([](Timer *handle) {
GW *k = (GW*)handle->data;
if (((CF*)k->config)->argDebugEvents) FN::log("DEBUG", "EV GW tCancel timer");
if (k->qp->cancelOrdersAuto)
Expand Down Expand Up @@ -135,7 +135,7 @@ namespace K {
string _price_ = price_.str();
for (string::iterator it=_price_.begin(); it!=_price_.end();)
if (*it == '+' or *it == '-') break; else it = _price_.erase(it);
stringstream os(string("1e").append(to_string(stod(_price_)-4)));
stringstream os(string("1e").append(to_string(fmax(stod(_price_),-4)-4)));
os >> gw->minTick;
}
k = FN::wJet(string(gw->http).append("/symbols_details"));
Expand Down
18 changes: 16 additions & 2 deletions src/server/km.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,21 @@ namespace K {
mOrder(string o, mExchange e, mPair P, mSide S, double q, mOrderType t, bool i, double p, mTimeInForce F, mORS s, bool O):
orderId(o), exchangeId(""), exchange(e), pair(P), side(S), quantity(q), type(t), isPong(i), price(p), timeInForce(F), orderStatus(s), preferPostOnly(O), lastQuantity(0), time(0), computationalLatency(0)
{};
string quantity2str() {
stringstream ss;
ss << setprecision(8) << fixed << quantity;
return ss.str();
};
string lastQuantity2str() {
stringstream ss;
ss << setprecision(8) << fixed << lastQuantity;
return ss.str();
};
string price2str() {
stringstream ss;
ss << setprecision(8) << fixed << price;
return ss.str();
};
};
static void to_json(json& j, const mOrder& k) {
j = {
Expand Down Expand Up @@ -508,7 +523,6 @@ namespace K {
function<void(mLevels)> evDataLevels;
function<void(mConnectivity)> evConnectOrder,
evConnectMarket;
mutex *hubMutex = nullptr;
uWS::Hub *hub = nullptr;
uWS::Group<uWS::CLIENT> *gwGroup = nullptr;
mExchange exchange = mExchange::Null;
Expand All @@ -523,7 +537,7 @@ namespace K {
virtual string A() = 0;
virtual void wallet() = 0,
levels() = 0,
send(string oI, mSide oS, double oP, double oQ, mOrderType oLM, mTimeInForce oTIF, bool oPO, unsigned long oT) = 0,
send(string oI, mSide oS, string oP, string oQ, mOrderType oLM, mTimeInForce oTIF, bool oPO, unsigned long oT) = 0,
cancel(string oI, string oE, mSide oS, unsigned long oT) = 0,
cancelAll() = 0,
close() = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/server/og.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace K {
void waitData() {
gw->evDataOrder = [&](mOrder k) {
if (((CF*)config)->argDebugEvents) FN::log("DEBUG", "EV OG evDataOrder");
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG reply ") + k.orderId + "::" + k.exchangeId + " [" + to_string((int)k.orderStatus) + "]: " + to_string(k.quantity) + "/" + to_string(k.lastQuantity) + " at price " + to_string(k.price));
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG reply ") + k.orderId + "::" + k.exchangeId + " [" + to_string((int)k.orderStatus) + "]: " + k.quantity2str() + "/" + k.lastQuantity2str() + " at price " + k.price2str());
updateOrderState(k);
};
};
Expand All @@ -53,8 +53,8 @@ namespace K {
public:
void sendOrder(mSide oS, double oP, double oQ, mOrderType oLM, mTimeInForce oTIF, bool oIP, bool oPO) {
mOrder o = updateOrderState(mOrder(gw->randId(), gw->exchange, mPair(gw->base, gw->quote), oS, oQ, oLM, oIP, FN::roundSide(oP, gw->minTick, oS), oTIF, mORS::New, oPO));
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG send ") + (o.side == mSide::Bid ? "BID id " : "ASK id ") + o.orderId + ": " + to_string(o.quantity) + " " + o.pair.base + " at price " + to_string(o.price) + " " + o.pair.quote);
gw->send(o.orderId, o.side, o.price, o.quantity, o.type, o.timeInForce, o.preferPostOnly, o.time);
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG send ") + (o.side == mSide::Bid ? "BID id " : "ASK id ") + o.orderId + ": " + o.quantity2str() + " " + o.pair.base + " at price " + o.price2str() + " " + o.pair.quote);
gw->send(o.orderId, o.side, o.price2str(), o.quantity2str(), o.type, o.timeInForce, o.preferPostOnly, o.time);
++((UI*)client)->orders60sec;
};
void cancelOrder(string k) {
Expand Down Expand Up @@ -334,7 +334,7 @@ namespace K {
allOrdersIds[k.exchangeId] = k.orderId;
allOrders[k.orderId] = k;
ogMutex.unlock();
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG save ") + (k.side == mSide::Bid ? "BID id " : "ASK id ") + k.orderId + "::" + k.exchangeId + " [" + to_string((int)k.orderStatus) + "]: " + to_string(k.quantity) + " " + k.pair.base + " at price " + to_string(k.price) + " " + k.pair.quote);
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG save ") + (k.side == mSide::Bid ? "BID id " : "ASK id ") + k.orderId + "::" + k.exchangeId + " [" + to_string((int)k.orderStatus) + "]: " + k.quantity2str() + " " + k.pair.base + " at price " + k.price2str() + " " + k.pair.quote);
} else cleanOrder(k.orderId, k.exchangeId);
if (((CF*)config)->argDebugOrders) FN::log("DEBUG", string("OG memory ") + to_string(allOrders.size()) + "/" + to_string(allOrdersIds.size()));
};
Expand Down
10 changes: 5 additions & 5 deletions src/server/qe.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace K {
quotingMode[mQuotingMode::Depth] = &calcDepthOfMarket;
};
void waitTime() {
((EV*)events)->tStart->data = (void*)this;
((EV*)events)->tCalcs->data = (void*)this;
uv_timer_start(((EV*)events)->tCalcs, [](uv_timer_t *handle) {
((EV*)events)->tStart->setData(this);
((EV*)events)->tCalcs->setData(this);
((EV*)events)->tCalcs->start([](Timer *handle) {
QE *k = (QE*)handle->data;
if (((CF*)k->config)->argDebugEvents) FN::log("DEBUG", "EV GW tCalcs timer");
if (((MG*)k->market)->fairValue) {
Expand Down Expand Up @@ -545,10 +545,10 @@ namespace K {
nextStart[side] = q;
nextIsPong = isPong;
if (tStarted) {
uv_timer_stop(((EV*)events)->tStart);
((EV*)events)->tStart->stop();
tStarted = 0;
}
uv_timer_start(((EV*)events)->tStart, [](uv_timer_t *handle) {
((EV*)events)->tStart->start([](Timer *handle) {
QE *k = (QE*)handle->data;
if (((CF*)k->config)->argDebugEvents) FN::log("DEBUG", "EV GW tStart timer");
k->start(k->nextStart.begin()->first, k->nextStart.begin()->second, k->nextIsPong);
Expand Down

0 comments on commit 24b49a0

Please sign in to comment.