commit | author | age
|
16b655
|
1 |
#include "strat.hpp" |
JG |
2 |
|
0b7aa0
|
3 |
#include "bom.hpp" |
JG |
4 |
#include "book.hpp" |
|
5 |
#include "json.hpp" |
|
6 |
#include "secrets.hpp" |
|
7 |
|
|
8 |
#include "protocol.hpp" |
|
9 |
#include <deque> |
|
10 |
#include <unordered_map> |
|
11 |
|
2c515f
|
12 |
namespace strat |
JG |
13 |
{ |
0b7aa0
|
14 |
std::unordered_map<std::string, json::Message*> orders; |
16b655
|
15 |
|
0b7aa0
|
16 |
void deleteOrder(json::AddedMessage* m); |
JG |
17 |
void freeMessages(std::deque<json::Message*>& m); |
|
18 |
void printFeed(std::deque<json::Message*>& m); |
|
19 |
void dumbHit(std::deque<json::Message*>& m); |
|
20 |
|
|
21 |
void initialise() |
|
22 |
{ |
|
23 |
bom::initialise(); |
|
24 |
protocol::createWebSocket(); |
16b655
|
25 |
} |
0b7aa0
|
26 |
|
JG |
27 |
void destroy() |
|
28 |
{ |
|
29 |
bom::destroy(); |
|
30 |
for (auto& i : orders) { |
|
31 |
switch (i.second->type) { |
|
32 |
case json::FUTURE_TYPE: |
|
33 |
case json::SPREAD_TYPE: |
|
34 |
case json::CALL_TYPE: |
|
35 |
case json::PUT_TYPE: |
|
36 |
deleteOrder((json::AddedMessage*)i.second); |
|
37 |
default:; |
|
38 |
} |
|
39 |
delete i.second; |
|
40 |
} |
|
41 |
} |
|
42 |
|
|
43 |
void mainLoop() |
|
44 |
{ |
|
45 |
auto bs = protocol::recoverBook(); |
|
46 |
while (true) { |
|
47 |
auto messages = protocol::catchUp(bs); |
|
48 |
// bom::updateBom(bs); |
|
49 |
printFeed(messages); |
|
50 |
freeMessages(messages); |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
void deleteOrder(json::AddedMessage* m) |
|
55 |
{ |
|
56 |
json::DeleteMessage a(json::DELETE, m->product, m->id); |
|
57 |
delete protocol::deleteOrder(a); |
|
58 |
} |
|
59 |
|
|
60 |
void printFeed(std::deque<json::Message*>& m) |
|
61 |
{ |
|
62 |
for (auto& i : m) { |
|
63 |
switch (i->type) { |
|
64 |
case json::FUTURE_TYPE: |
|
65 |
case json::SPREAD_TYPE: |
|
66 |
case json::CALL_TYPE: |
|
67 |
case json::PUT_TYPE: |
|
68 |
std::cout << ((json::AnnounceMessage*)i)->as_string() << std::endl; |
|
69 |
break; |
|
70 |
case json::SETTLEMENT: |
|
71 |
std::cout << ((json::SettleMessage*)i)->as_string() << std::endl; |
|
72 |
break; |
|
73 |
case json::ADDED: |
|
74 |
std::cout << ((json::AddedMessage*)i)->as_string() << std::endl; |
|
75 |
break; |
|
76 |
case json::DELETED: |
|
77 |
std::cout << ((json::DeletedMessage*)i)->as_string() << std::endl; |
|
78 |
break; |
|
79 |
case json::TRADE: |
|
80 |
std::cout << ((json::TradeMessage*)i)->as_string() << std::endl; |
|
81 |
break; |
|
82 |
case json::BROKER_REQUEST: |
|
83 |
std::cout << ((json::BrokerRequest*)i)->as_string() << std::endl; |
|
84 |
break; |
|
85 |
case json::BROKER_ACK: |
|
86 |
std::cout << ((json::BrokerAck*)i)->as_string() << std::endl; |
|
87 |
break; |
|
88 |
case json::BROKER_CONFIRM: |
|
89 |
std::cout << ((json::BrokerConfirm*)i)->as_string() << std::endl; |
|
90 |
break; |
|
91 |
default:; |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
void freeMessages(std::deque<json::Message*>& m) |
|
97 |
{ |
|
98 |
for (auto i : m) { |
|
99 |
delete i; |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
void dumbHit(std::deque<json::Message*>& m) |
|
104 |
{ |
|
105 |
for (auto& i : m) { |
|
106 |
if (i->type == json::ADDED) { |
|
107 |
json::AddedMessage* j = (json::AddedMessage*) i; |
|
108 |
if (j->owner == "nseow") { |
|
109 |
book::OrderSideEnum s = j->side == book::Buy ? book::Sell : book::Buy; |
|
110 |
json::AddMessage a(json::ADD, j->product, j->price, s, j->resting); |
|
111 |
auto b = protocol::addOrder(a); |
|
112 |
if (b->type == json::ADDED) { |
|
113 |
json::DeleteMessage d(json::DELETE, j->product, ((json::AddedMessage*) b)->id); |
|
114 |
delete protocol::deleteOrder(d); |
|
115 |
} |
|
116 |
delete b; |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
} // namespace strat |