-
Notifications
You must be signed in to change notification settings - Fork 89
/
FetchOptionsChain.java
195 lines (165 loc) · 5.79 KB
/
FetchOptionsChain.java
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
import fetchOptionsChain.RequestOptionChainWrapper;
import fetchOptionsChain.RequestOptionChain;
import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// Read in TickerList.csv, a list of contracts to generate options chains from
// TickerList.csv fields: Active (T/F) | symbol | secType | exchange | currency
// | expiry | strike | right | multiplier
// Read in PennyPilot.csv, a list of Penny Pilot tickers
// Output option chain to IBAlgoSystem.MasterChainList
public class FetchOptionsChain {
private EClientSocket socket = null;
public String delimiter = ",";
public String delimiter_under = "_";
public String csvFile = "TickerList.csv";
public String PennyPilotFile = "PennyPilot.csv";
public String[] TickerList;
public int counter_iter = 0;
public double price = -1.0;
public Connection sqlConnection = null;
public PreparedStatement preparedStatement = null;
public ResultSet resultSet = null;
public void sqlClose() {
try {
if (resultSet != null) {
resultSet.close();
}
if (sqlConnection != null) {
sqlConnection.close();
}
} catch (Exception e) {
}
}
public FetchOptionsChain() {
Map<String, String> PennyPilotMap = new HashMap<>();
boolean foundPennyTick;
// load Penny Pilot Tickers
List<String> PennyPilotTickers = new ArrayList<String>();
StringBuilder temp_ticker = new StringBuilder("");
try (BufferedReader br1 = new BufferedReader(new FileReader(PennyPilotFile))) {
String input1;
while ((input1 = br1.readLine()) != null) {
if (!input1.split(delimiter)[0].equals(temp_ticker.toString())) {
temp_ticker = new StringBuilder(input1.split(delimiter)[0]);
PennyPilotTickers.add(temp_ticker.toString());
}
}
br1.close();
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(PennyPilotTickers);
// find number of rows in TickerList
int rows = 0;
try (BufferedReader br2 = new BufferedReader(new FileReader(csvFile))) {
String input2;
while ((input2 = br2.readLine()) != null) {
rows++;
}
br2.close();
} catch (IOException e) {
e.printStackTrace();
}
// write values of TickerList
TickerList = new String[rows];
try (BufferedReader br3 = new BufferedReader(new FileReader(csvFile))) {
String input3;
int row_iter = 0;
while ((input3 = br3.readLine()) != null) {
TickerList[row_iter] = input3;
row_iter++;
}
br3.close();
} catch (IOException e) {
e.printStackTrace();
}
// connect to sql, update counter
try {
Class.forName("com.mysql.jdbc.Driver");
sqlConnection = DriverManager.getConnection(
"jdbc:mysql://localhost/IBAlgoSystem?user=user&password=pw");
preparedStatement = sqlConnection.prepareStatement(
"UPDATE IBAlgoSystem.counter SET counter = 0");
preparedStatement.executeUpdate();
preparedStatement = sqlConnection.prepareStatement(
"TRUNCATE TABLE IBAlgoSystem.MasterChainList");
preparedStatement.executeUpdate();
// write PennyPilotMap
for (int i=0; i < TickerList.length; i++) {
foundPennyTick = false;
for (int j=0; j < PennyPilotTickers.size(); j++) {
if (TickerList[i].split(delimiter)[1].equals(PennyPilotTickers.get(j))) {
foundPennyTick = true;
break;
}
}
if (foundPennyTick) {
PennyPilotMap.put(TickerList[i].split(delimiter)[1], "T");
} else {
PennyPilotMap.put(TickerList[i].split(delimiter)[1], "F");
}
}
// connect to socket
EWrapper requestOptionChainWrapper = new RequestOptionChainWrapper(sqlConnection);
EClientSocket socket = new EClientSocket(requestOptionChainWrapper);
socket.eConnect (null, 4002, 100);
try {
while (!(socket.isConnected()));
} catch (Exception e) {
}
// submit a new contract for every request
for (int i = 0; i < rows; i++) {
String line = TickerList[i];
Contract cont = new Contract();
cont.m_symbol = line.split(delimiter)[1];
cont.m_secType = "OPT";
cont.m_exchange = "SMART";
cont.m_currency = line.split(delimiter)[4];
cont.m_multiplier = "100";
RequestOptionChain data = new RequestOptionChain(cont, socket, sqlConnection);
}
// check counter to disconnect socket
preparedStatement = sqlConnection.prepareStatement(
"SELECT counter FROM IBAlgoSystem.counter;");
resultSet = preparedStatement.executeQuery();
counter_iter = WriteInt(resultSet, "counter");
while (counter_iter < rows) {
resultSet = preparedStatement.executeQuery();
counter_iter = WriteInt(resultSet, "counter");
}
socket.eDisconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlClose();
}
}
private int WriteInt(ResultSet resultSet, String column_name) throws SQLException {
int output_counter = 0;
while (resultSet.next()) {
output_counter = resultSet.getInt(column_name);
}
return output_counter;
}
public static void main(String args[]) {
try {
FetchOptionsChain runProcess = new FetchOptionsChain();
} catch (Exception e) {
e.printStackTrace ();
}
}
}