-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADRArbitrager.cs
186 lines (166 loc) · 5.67 KB
/
ADRArbitrager.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
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace etc
{
class ADRArbitrager
{
private object thisLock = new object();
public bool canTrade;
public static string adrTicker = "VALE";
public static string ordTicker = "VALBZ";
public int adrBuyOrderID;
public int adrSellOrderID;
public int adrAsk;
public int adrBid;
public int ordAsk;
public int ordBid;
private Market market;
public ADRArbitrager(Market market_)
{
market = market_;
adrBuyOrderID = -1;
adrSellOrderID = -1;
adrAsk = adrBid = ordAsk = ordBid = -1;
canTrade = false;
}
public int Main()
{
lock (thisLock)
{
market.GotHello += market_GotHello;
market.Open += market_Open;
market.Close += market_Close;
market.Ack += market_Ack;
market.Fill += market_Fill;
market.Book += market_Book;
while (true)
{
if (canTrade)
{
int adr = market.GetPosition(adrTicker);
int ord = market.GetPosition(ordTicker);
if (Math.Abs(adr) >= 10)
{
CancelAllOrder();
Direction dir = Direction.BUY;
if (adr > 0)
dir = Direction.SELL;
market.Convert(adrTicker, dir, Math.Abs(adr));
ord += adr;
adr = 0;
}
if (ord != 0)
{
Direction dir = Direction.BUY;
int price;
if (ord > 0)
{
dir = Direction.SELL;
price = ordBid;
}
else
{
dir = Direction.BUY;
price = ordAsk;
}
int orderId = market.Add(ordTicker, dir, price, 1);
Task.Delay(200).Wait();
market.Cancel(orderId);
}
Task.Delay(200).Wait();
}
}
}
}
public void CancelAllOrder()
{
lock (thisLock)
{
if (adrBuyOrderID >= 0)
{
market.Cancel(adrBuyOrderID);
adrBuyOrderID = -1;
}
if (adrSellOrderID >= 0)
{
market.Cancel(adrSellOrderID);
adrSellOrderID = -1;
}
//Task.Delay(100).Wait();
}
}
void market_Book(object sender, BookEventArgs e)
{
lock (thisLock)
{
string ticker = e.symbol;
if (ticker == ordTicker)
{
if ((e.buys.Count > 0) && (e.sells.Count > 0))
{
CancelAllOrder();
ordBid = e.buys.Last().Key;
ordAsk = e.sells.First().Key;
int adr = market.GetPosition(adrTicker);
//Console.WriteLine("I am adding buy order. " + "adr = " + adr + "I am going to buy " + Math.Min(10 - adr, 10) + "shares.");
adrBuyOrderID = market.Add(adrTicker, Direction.BUY, ordBid - 2, Math.Max(0, Math.Min(10 - adr, 3)));
//Console.WriteLine("I am adding sell order. " + "adr = " + adr + "I am going to sell " + Math.Min(10 + adr, 10) + "shares.");
adrSellOrderID = market.Add(adrTicker, Direction.SELL, ordAsk + 2, Math.Max(0, Math.Min(10 + adr, 3)));
//Task.Delay(200).Wait();
}
}
if (ticker == adrTicker)
{
if ((e.buys.Count > 0) && (e.sells.Count > 0))
{
adrBid = e.buys.Last().Key;
adrAsk = e.sells.First().Key;
}
}
}
}
void market_Fill(object sender, FillEventArgs e)
{
lock (thisLock)
{
int id = e.id;
if (e.dir == Direction.BUY)
{
}
else
{
}
}
}
void market_Ack(object sender, AckEventArgs e)
{
lock (thisLock)
{
int id = e.id;
}
}
void market_Close(object sender, CloseEventArgs e)
{
lock (thisLock)
{
canTrade = (!e.symbols.Contains(adrTicker)) && (!e.symbols.Contains(ordTicker));
}
}
void market_Open(object sender, OpenEventArgs e)
{
lock (thisLock)
{
canTrade = e.symbols.Contains(adrTicker) && e.symbols.Contains(ordTicker);
}
}
void market_GotHello(object sender, HelloEventArgs e)
{
lock (thisLock)
{
}
}
}
}