-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurity.cs
115 lines (104 loc) · 2.73 KB
/
Security.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace etc
{
class Security
{
private object thisLock = new object();
public string symbol;
public SortedDictionary<int, int> buys;
public SortedDictionary<int, int> sells;
public int bid; // -1 = invalid
public int ask; // -1 = invalid
public int mid; // -1 = invalid
public int lastTradePrice; // -1 = invalid
public int lastTradeSize; // -1 = invalid
public DateTime lastTradeTime; // DateTime.MinValue to start with
public int rspWeight;
public int xlyWeight;
public int xlpWeight;
public int xluWeight;
public Security(string symbol_)
{
symbol = symbol_;
buys = new SortedDictionary<int, int>();
sells = new SortedDictionary<int, int>();
//if (symbol == "BOND") { fair = 1000.0; }
bid = ask = mid = lastTradePrice = -1;
rspWeight = xlyWeight = xlpWeight = xluWeight = 0;
switch (symbol)
{
case "AMZN": rspWeight = 3; xlyWeight = 6; break;
case "HD": rspWeight = 6; xlyWeight = 6; break;
case "DIS": rspWeight = 8; xlyWeight = 8; break;
case "PG": rspWeight = 6; xlpWeight = 12; break;
case "KO": rspWeight = 12; xlpWeight = 12; break;
case "PM": rspWeight = 6; xlpWeight = 6; break;
case "NEE": rspWeight = 4; xluWeight = 8; break;
case "DUK": rspWeight = 6; xluWeight = 6; break;
case "SO": rspWeight = 8; xluWeight = 8; break;
case "XLY": break;
case "XLP": break;
case "XLU": break;
case "RSP": break;
default: Console.Error.WriteLine(string.Format("----- UNKNOWN SYMBOL: {0} -----", symbol)); break;
}
}
public object GetLock()
{
return thisLock;
}
public void OnBook(BookEventArgs args)
{
if (args.symbol != symbol) return;
lock (thisLock)
{
buys = args.buys;
sells = args.sells;
bid = Bid();
ask = Ask();
mid = Mid();
}
}
public void OnTrade(TradeEventArgs args)
{
if (args.symbol != symbol) return;
var now = DateTime.Now;
lock (thisLock)
{
lastTradePrice = args.price;
lastTradeSize = args.size;
lastTradeTime = now;
}
}
private int Bid()
{
if (buys.Count == 0) return -1;
return buys.Last().Key;
}
private int Ask()
{
if (sells.Count == 0) return -1;
return sells.First().Key;
}
private int Mid()
{
if (bid < 0 || ask < 0) return -1;
return (bid + ask) / 2;
}
public void UpdateFrom(Security source)
{
buys = new SortedDictionary<int, int>(source.buys);
sells = new SortedDictionary<int, int>(source.sells);
bid = source.bid;
ask = source.ask;
mid = source.mid;
lastTradePrice = source.lastTradePrice;
lastTradeSize = source.lastTradeSize;
lastTradeTime = source.lastTradeTime;
}
}
}