-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
204 lines (172 loc) · 6.65 KB
/
Program.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Jeti Trading System v0
* Date 2017-03
* Author: Daniel Siliski
* */
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using IBApi;
using System.Data;
namespace Jeti_v0{
public class program{
static DataTable p = new DataTable();
static DataTable s = new DataTable();
struct rtbUpdateStruct
{
internal string ticker;
internal long time;
internal double close;
}
static Queue q = new Queue();
static Dictionary<string, int> reqIds = new Dictionary<string, int>();
public static int Main(string[] args){
//Connect to the API
EWrapperImpl ApiWrapper = new EWrapperImpl();
//public EWrapperImpl ApiWrapper;
ApiWrapper.ClientSocket.eConnect("127.0.0.1", 7496, 0, false);
// before proceding, monitor nextValidId
while (ApiWrapper.NextOrderId <= 0) { }
// Load securities
var activecontracts = GetActiveContracts();
// If no tickers found in database, load a default (hard coded) ticker
if (activecontracts.Count == 0)
{ActiveContract defaultcontract = new ActiveContract();
DateTime date1 = new DateTime(1900, 12, 31, 0, 0, 0);
defaultcontract.ActivityDate = date1;
defaultcontract.IBFuturesLocalSymbol = "ZZN6";
activecontracts.Add(defaultcontract);}
// BUILD A CONTRACT FOR EACH TICKER
var IBcontractlist = new List<Contract>();
activecontracts.ForEach(t =>
{if (t.ActivityDate == DateTime.Today)
{Contract nextcontract = BuildNymexFuturesContract(t);
IBcontractlist.Add(nextcontract);}});
// set up dataTable for capture of datafeed price data
p.Columns.Add("ticker", typeof(string));
p.Columns.Add("close", typeof(float));
p.Columns.Add("time", typeof(Int64));
//p.Columns.Add("processed", typeof(DateTime));
// set up dictionary of Tickers:ReqIds
int i = 0;
Parallel.ForEach(IBcontractlist, (t) =>
{
i++;
reqIds.Add(t.LocalSymbol, i);
});
// open an api datafeed for each contract
Parallel.ForEach(IBcontractlist, (t) =>
{
i++;
ApiWrapper.ClientSocket.reqRealTimeBars(t.reqId, t, -1, "BID", false,
GetFakeParameters(4));
Thread.Sleep(5000);
});
// dequeue price updates and write to database
while (1 > 0)
{
// if new data arrived...
if (q.Count > 0)
{
rtbUpdateStruct t = new rtbUpdateStruct();
t = (rtbUpdateStruct)q.Dequeue();
// write to console
Console.WriteLine(t.ticker +
", close: " + t.close +
", time: " + t.time);
// write to database
ApiWrapper.RealTimeBarCapturetoDB(t.close,t.time,t.ticker);
// append to datatable
p.Rows.Add(t.ticker, t.close, t.time);
// update technical indicators
//Studies(t.ticker);
//Scores(t.ticker);
//Weights(t.ticker);
//Trades(t.ticker);
//Orders();
//Reports();
}
}
////Shut down
//Console.WriteLine("Disconnecting... Please press ENTER to close application.");
//Console.ReadLine();
//ApiWrappper.ClientSocket.eDisconnect();
//return 0;
}
public static Contract BuildNymexFuturesContract(ActiveContract t)
{
Contract contract = new Contract();
contract.SecType = "FUT";
if (t.IBFuturesLocalSymbol.Substring(t.IBFuturesLocalSymbol.Length-2) == "ES")
{contract.Exchange = "GLOBEX";}
else {contract.Exchange = "NYMEX";}
contract.Currency = "USD";
contract.LocalSymbol = t.IBFuturesLocalSymbol;
contract.reqId = Contract.GetActiveInstances();
return contract;
}
public static void returnRTBfromAPI(int reqId, long time, double open,
double high, double low, double close, long volume, double WAP, int count)
{
rtbUpdateStruct rtb = new rtbUpdateStruct();
rtb.close = close;
rtb.time = time;
rtb.ticker = reqIds.FirstOrDefault(x => x.Value == reqId).Key;
q.Enqueue(rtb);
}
public static Contract BuildUSStock(ActiveContract t)
{
Contract contract = new Contract();
contract.Symbol = "AMZN";
contract.SecType = "STK";
contract.Currency = "USD";
contract.Exchange = "SMART";
return contract;
}
static public List<ActiveContract> GetActiveContracts()
{
using (var jetiDB = new JETIEntities())
{
return (from ActiveContract in jetiDB.ActiveContracts
select ActiveContract).ToList();
}
}
private static List<TagValue> GetFakeParameters(int numParams)
{
List<TagValue> fakeParams = new List<TagValue>();
for (int i = 0; i < numParams; i++)
fakeParams.Add(new TagValue("tag" + i, i.ToString()));
return fakeParams;
}
private static void Studies(string t)
{
// get data from p for t
// calc 5period EWMA
// calc 10period EWMA
// calc 9period RSI
// write values to a studies DataTable
}
private static void Scores(string t)
{
// calculate the score values of each contract
}
private static void Weights(string t)
{
// decide ideal portfolio weights (alpha forecast + t-cost estimate)
}
private static void Orders(string t)
{
// generate and execute API orders
}
private static void Reports(string t)
{
// persist data on trade price, pnl, positions, etc to database (and in future to reporting layer)
}
}
}