-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBondMarketMaker.cs
178 lines (158 loc) · 4.52 KB
/
BondMarketMaker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace etc
{
class BondMarketMaker
{
private object thisLock = new object();
public bool canTradeBond;
public int buyOrder;
public int sellOrder;
public Dictionary<int, Direction> orderDir;
public Dictionary<int, int> orderPrice;
public Dictionary<int, int> orderSize;
private Market market;
public static string bondTicker = "BOND";
public BondMarketMaker(Market market_)
{
market = market_;
canTradeBond = false;
buyOrder = 0;
sellOrder = 0;
orderDir = new Dictionary<int, Direction>();
orderPrice = new Dictionary<int, int>();
orderSize = new Dictionary<int, int>();
}
public int Main()
{
lock (thisLock)
{
market.GotHello += market_GotHello;
market.Open += market_Open;
market.Close += market_Close;
market.Fill += market_Fill;
market.Ack += market_Ack;
while (true)
{
if (canTradeBond)
{
int bondPosition = market.GetPosition(bondTicker);
if (bondPosition > 80 && sellOrder < 90)
{
AddOrder(bondTicker, Direction.SELL, 1000, 5);
}
if (bondPosition < -80 && buyOrder < 90)
{
AddOrder(bondTicker, Direction.BUY, 1000, 5);
}
if (bondPosition + buyOrder < 85 && buyOrder < 80)
{
AddOrder(bondTicker, Direction.BUY, 999, 97 - (bondPosition + buyOrder));
}
if (bondPosition - sellOrder > -85 && sellOrder < 80)
{
AddOrder(bondTicker, Direction.SELL, 1001, 97 + (bondPosition - sellOrder));
}
Task.Delay(10).Wait();
}
}
}
}
void AddOrder(string symbol, Direction dir, int price, int size)
{
lock (thisLock)
{
int orderID = market.Add(symbol, dir, price, size);
orderDir.Add(orderID, dir);
orderPrice.Add(orderID, price);
orderSize.Add(orderID, size);
if (dir == Direction.BUY)
{
buyOrder += size;
}
else
{
sellOrder += size;
}
Task.Delay(10).Wait();
}
}
void market_Ack(object sender, AckEventArgs e)
{
lock (thisLock)
{
int id = e.id;
if (orderDir.ContainsKey(id))
{
if (orderDir[id] == Direction.BUY)
{
//buyOrder += orderSize[id];
}
else
{
//sellOrder += orderSize[id];
}
}
}
}
void market_Fill(object sender, FillEventArgs e)
{
lock (thisLock)
{
int id = e.id;
if (e.symbol == bondTicker)
{
if (e.dir == Direction.BUY)
{
buyOrder -= e.size;
}
else
{
sellOrder -= e.size;
}
}
if (orderDir.ContainsKey(id))
{
Direction dir = orderDir[id];
int price = orderPrice[id];
int size = orderSize[id];
size -= e.size;
if (size >= 0)
{
orderSize.Remove(id);
orderSize.Add(id, size);
}
else
{
orderDir.Remove(id);
orderPrice.Remove(id);
orderSize.Remove(id);
}
}
}
}
void market_Close(object sender, CloseEventArgs e)
{
lock (thisLock)
{
canTradeBond = !e.symbols.Contains(bondTicker);
}
}
void market_Open(object sender, OpenEventArgs e)
{
lock (thisLock)
{
canTradeBond = e.symbols.Contains(bondTicker);
}
}
void market_GotHello(object sender, HelloEventArgs e)
{
lock (thisLock)
{
}
}
}
}