Skip to content

Commit c8039c9

Browse files
authored
Add files via upload
1 parent ef0383f commit c8039c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+28786
-1
lines changed

AlterGUI/algo.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <Python.h>
5+
#include "matplotlibcpp.h"
6+
#include <math.h>
7+
#include <map>
8+
9+
namespace plt = matplotlibcpp;
10+
11+
double fx(double x, double y)
12+
{
13+
return -(pow(x, 2) + pow(y, 2));
14+
}
15+
16+
double dfx(double x, double y)
17+
{
18+
return -2.0*x;
19+
}
20+
21+
double dfy(double x, double y)
22+
{
23+
return -2.0*y;
24+
}
25+
26+
double plane(double x, double y, double x0, double y0)
27+
{
28+
return dfx(x0, y0)*(x - x0) + dfy(x0, y0)*(y - y0) + fx(x0, y0);
29+
}
30+
31+
std::map<std::string, std::vector<std::vector<double>>> Tangent(double c, double x0, double y0)
32+
{
33+
double m0 = x0 - c, m1 = x0 + c;
34+
double n0 = y0 - c, n1 = y0 + c;
35+
int o = 60;
36+
37+
double dM = (m1 - m0)/(o - 1);
38+
double dN = (n1 - n0)/(o - 1);
39+
40+
std::map<std::string, std::vector<std::vector<double>>> result;
41+
std::vector<double> tx, ty, tz;
42+
43+
double rx, ry;
44+
45+
for(int i = 0; i < o; ++i){
46+
tx.clear();
47+
ty.clear();
48+
tz.clear();
49+
rx = m0 + i*dM;
50+
for(int j = 0; j < o; ++j){
51+
ry = n0 + j*dM;
52+
tx.push_back(rx);
53+
ty.push_back(ry);
54+
tz.push_back(plane(rx, ry, x0, y0));
55+
}
56+
result["x"].push_back(tx);
57+
result["y"].push_back(ty);
58+
result["z"].push_back(tz);
59+
}
60+
return result;
61+
}
62+
63+
int main()
64+
{
65+
PyObject * ax = plt::chart(111);
66+
plt::Clear3DChart(ax);
67+
68+
int n = 60;
69+
double t0 = -4.0, t1 = 4.0;
70+
double dT = (t1 - t0)/(n - 1);
71+
72+
std::vector<std::vector<double>> x, y, z;
73+
std::vector<double> tx, ty, tz;
74+
75+
double rx, ry;
76+
77+
for(int i = 0; i < n; ++i){
78+
tx.clear();
79+
ty.clear();
80+
tz.clear();
81+
rx = t0 + i*dT;
82+
for(int j = 0; j < n; ++j){
83+
ry = t0 + j*dT;
84+
tx.push_back(rx);
85+
ty.push_back(ry);
86+
tz.push_back(fx(rx, ry));
87+
}
88+
x.push_back(tx);
89+
y.push_back(ty);
90+
z.push_back(tz);
91+
}
92+
93+
double Px = -3.5, Py = -3.5;
94+
double learning = 0.15;
95+
96+
std::map<std::string, std::vector<std::vector<double>>> gd;
97+
98+
for(int i = 0; i < 100; ++i){
99+
plt::Clear3DChart(ax);
100+
gd = Tangent(2, Px, Py);
101+
102+
plt::surface3D(ax, x, y, z, "red", 0.9);
103+
plt::surface3D(ax, gd["x"], gd["y"], gd["z"], "green", 0.9);
104+
105+
Px = Px + learning*dfx(Px, Py);
106+
Py = Py + learning*dfy(Px, Py);
107+
108+
plt::pause(0.5);
109+
}
110+
111+
112+
plt::show();
113+
114+
return 0;
115+
}

AlterGUI/altergui.png

1.13 MB
Loading

BookViz/book.cpp

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <sstream>
4+
#include <vector>
5+
#include <map>
6+
#include <cpprest/ws_client.h>
7+
#include <boost/property_tree/ptree.hpp>
8+
#include <boost/property_tree/json_parser.hpp>
9+
#include <thread>
10+
#include <algorithm>
11+
#include <chrono>
12+
#include "matplotlibcpp.h"
13+
#include <Python.h>
14+
15+
using namespace web;
16+
using namespace web::websockets::client;
17+
using namespace boost::property_tree;
18+
namespace plt = matplotlibcpp;
19+
20+
class datafeed {
21+
private:
22+
ptree JSON(std::string message){
23+
std::stringstream ss(message);
24+
ptree result;
25+
read_json(ss, result);
26+
return result;
27+
}
28+
29+
void CYCLONE(ptree df, std::map<double, double> & bids, std::map<double, double> & asks){
30+
bool snapshot = false;
31+
bool l2update = false;
32+
for(ptree::const_iterator it = df.begin(); it != df.end(); ++it){
33+
if(l2update == true && it->first == "changes"){
34+
for(ptree::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt){
35+
std::vector<std::string> hold;
36+
for(ptree::const_iterator kt = jt->second.begin(); kt != jt->second.end(); ++kt){
37+
hold.push_back(kt->second.get_value<std::string>().c_str());
38+
}
39+
40+
double price = atof(hold[1].c_str());
41+
double volume = atof(hold[2].c_str());
42+
43+
if(hold[0] == "buy"){
44+
if(volume == 0){
45+
bids.erase(price);
46+
} else {
47+
bids[price] = volume;
48+
}
49+
} else {
50+
if(volume == 0){
51+
asks.erase(price);
52+
} else {
53+
asks[price] = volume;
54+
}
55+
}
56+
}
57+
}
58+
if(snapshot == true && it->first == "bids"){
59+
for(ptree::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt){
60+
std::vector<double> hold;
61+
for(ptree::const_iterator kt = jt->second.begin(); kt != jt->second.end(); ++kt){
62+
hold.push_back(atof(kt->second.get_value<std::string>().c_str()));
63+
}
64+
bids[hold[0]] = hold[1];
65+
}
66+
}
67+
if(snapshot == true && it->first == "asks"){
68+
for(ptree::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt){
69+
std::vector<double> hold;
70+
for(ptree::const_iterator kt = jt->second.begin(); kt != jt->second.end(); ++kt){
71+
hold.push_back(atof(kt->second.get_value<std::string>().c_str()));
72+
}
73+
asks[hold[0]] = hold[1];
74+
}
75+
}
76+
77+
if(it->first == "type"){
78+
if(it->second.get_value<std::string>() == "l2update"){
79+
l2update = true;
80+
}
81+
if(it->second.get_value<std::string>() == "snapshot"){
82+
snapshot = true;
83+
}
84+
}
85+
}
86+
}
87+
88+
89+
90+
public:
91+
92+
static void Socket(datafeed dx, std::map<double, double> & bids, std::map<double, double> & asks){
93+
std::string url = "wss://ws-feed.exchange.coinbase.com";
94+
std::string msg = "{\"type\":\"subscribe\",\"product_ids\":[\"BTC-USD\"],\"channels\":[\"level2_batch\"]}";
95+
96+
websocket_client client;
97+
client.connect(url).wait();
98+
websocket_outgoing_message outmsg;
99+
outmsg.set_utf8_message(msg);
100+
client.send(outmsg);
101+
102+
while(true){
103+
client.receive().then([](websocket_incoming_message inmsg){
104+
return inmsg.extract_string();
105+
}).then([&](std::string message){
106+
dx.CYCLONE(dx.JSON(message), std::ref(bids), std::ref(asks));
107+
}).wait();
108+
}
109+
110+
client.close().wait();
111+
112+
}
113+
};
114+
115+
std::map<std::string, std::vector<double>> Extract(std::map<double, double> bids, std::map<double, double> asks)
116+
{
117+
int depth = 80;
118+
std::map<std::string, std::vector<double>> result;
119+
120+
int count = 0;
121+
double bidvol = 0;
122+
for(auto it = bids.rbegin(); it != bids.rend(); ++it){
123+
bidvol += it->second;
124+
result["bidPrice"].push_back(it->first);
125+
result["bidSize"].push_back(bidvol);
126+
count += 1;
127+
if(count >= depth){
128+
break;
129+
}
130+
}
131+
132+
count = 0;
133+
double askvol = 0;
134+
for(auto it = asks.begin(); it != asks.end(); ++it){
135+
askvol += it->second;
136+
result["askPrice"].push_back(it->first);
137+
result["askSize"].push_back(askvol);
138+
count += 1;
139+
if(count >= depth){
140+
break;
141+
}
142+
}
143+
144+
std::reverse(result["bidPrice"].begin(), result["bidPrice"].end());
145+
std::reverse(result["bidSize"].begin(), result["bidSize"].end());
146+
147+
148+
return result;
149+
}
150+
151+
std::vector<double> push_into(double ii, int n){
152+
std::vector<double> result;
153+
for(int i = 0; i < n; ++i){
154+
result.push_back(ii);
155+
}
156+
return result;
157+
}
158+
159+
int main()
160+
{
161+
PyObject * ax = plt::chart(121);
162+
PyObject * ay = plt::chart(122);
163+
164+
std::map<double, double> bids, asks;
165+
std::map<std::string, std::vector<double>> preprice;
166+
167+
datafeed wsfeed;
168+
std::thread feed(wsfeed.Socket, wsfeed, std::ref(bids), std::ref(asks));
169+
170+
std::this_thread::sleep_for(std::chrono::seconds(10));
171+
172+
std::vector<std::vector<double>> bX, bY, bZ, aX, aY, aZ;
173+
174+
int ii = 0;
175+
int jj = 0;
176+
int limit = 80;
177+
178+
while(true){
179+
preprice = Extract(bids, asks);
180+
bX.clear();
181+
bY.clear();
182+
bZ.push_back(preprice["bidSize"]);
183+
for(int u = 0; u < bZ.size(); ++u){
184+
bY.push_back(preprice["bidPrice"]);
185+
bX.push_back(push_into(ii, preprice["bidPrice"].size()));
186+
ii += 1;
187+
}
188+
189+
aX.clear();
190+
aY.clear();
191+
aZ.push_back(preprice["askSize"]);
192+
for(int u = 0; u < aZ.size(); ++u){
193+
aY.push_back(preprice["askPrice"]);
194+
aX.push_back(push_into(jj, preprice["askPrice"].size()));
195+
jj += 1;
196+
}
197+
198+
199+
if(bZ.size() >= limit){
200+
bX.erase(bX.begin());
201+
bY.erase(bY.begin());
202+
bZ.erase(bZ.begin());
203+
}
204+
205+
if(aZ.size() >= limit){
206+
aX.erase(aX.begin());
207+
aY.erase(aY.begin());
208+
aZ.erase(aZ.begin());
209+
}
210+
211+
plt::Clear3DChart(ax);
212+
plt::Clear3DChart(ay);
213+
214+
plt::surface3D(ax, bX, bY, bZ, "red", 1.0);
215+
plt::surface3D(ay, aX, aY, aZ, "limegreen", 1.0);
216+
217+
plt::pause(0.1);
218+
219+
//std::cout << bX.size() << " " << bX[0].size() << "\t" << bY.size() << " " << bY[0].size() << "\t" << bZ.size() << " " << bZ[0].size() << std::endl;
220+
}
221+
222+
plt::show();
223+
feed.join();
224+
return 0;
225+
}

BookViz/book.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
echo "Compiling"
3+
g++ book.cpp -std=c++17 -lcpprest -lcrypto -lssl -lpthread -I/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 -L/Library/Frameworks/Python.framework/Versions/3.11/lib -lpython3.11 -I/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/core/include
4+
echo "Compiled"
5+
exit 0

BookViz/bookviz.png

1.55 MB
Loading

0 commit comments

Comments
 (0)