-
Notifications
You must be signed in to change notification settings - Fork 89
/
WritePriceData.java
109 lines (96 loc) · 3.6 KB
/
WritePriceData.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
package simplePriceFeed;
import com.ib.client.EClientSocket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Date;
public class WritePriceData {
public static final int NULL=0, LOOK=1<<0, LONG=1<<1, SHORT=1<<2, WAIT_FILL=1<<3, WAIT_CANCEL=1<<4;
public int sysState = NULL;
private final RequestPriceData data;
private final EClientSocket socket;
private final Connection sqlConnection;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private double prevLastPrice;
private int bugCounter;
private static int nextOrderId = 1;
WritePriceData(RequestPriceData data, EClientSocket socket,
Connection sqlConnection) {
this.data = data;
this.socket = socket;
this.sqlConnection = sqlConnection;
sysState = LOOK;
}
void check() {
try {
if (data.cont.m_secType.equals("STK")) {
// check that lastPrice isn't a bug
statement = sqlConnection.createStatement();
resultSet = statement.executeQuery(
"SELECT last FROM IBAlgoSystem.price WHERE symbol='"
+ data.cont.m_symbol + "' AND secType='STK';");
prevLastPrice = outputLastPrice(resultSet);
resultSet = statement.executeQuery(
"SELECT bugCounter FROM IBAlgoSystem.price WHERE symbol='"
+ data.cont.m_symbol + "' AND secType='STK';");
bugCounter = outputBugCounter(resultSet);
if ((prevLastPrice > 0.0) &&
(Math.abs(data.lastPrice/prevLastPrice - 1) > 0.1) &&
(bugCounter < 3)) {
bugCounter++;
preparedStatement = sqlConnection.prepareStatement(
"UPDATE IBAlgoSystem.price SET bugCounter="
+ Integer.toString(bugCounter) + ";");
} else {
preparedStatement = sqlConnection.prepareStatement(
"UPDATE IBAlgoSystem.price SET bid ="
+ Double.toString(data.bidPrice) + ", ask =" +
Double.toString(data.askPrice) + ", last ="
+ Double.toString(data.lastPrice) + ", close ="
+ Double.toString(data.closePrice) +
", bugCounter = 0, updateTime = "
+ Long.toString((new Date()).getTime())
+ " WHERE symbol = '" + data.cont.m_symbol +
"' AND secType ='STK' AND currency = '"
+ data.cont.m_currency + "';");
}
} else if (data.cont.m_secType.equals("OPT")) {
preparedStatement = sqlConnection.prepareStatement(
"UPDATE IBAlgoSystem.price SET bid ="
+ Double.toString(data.bidPrice) + ", ask =" +
Double.toString(data.askPrice) + ", last ="
+ Double.toString(data.lastPrice) + ", close ="
+ Double.toString(data.closePrice) + ", updateTime ="
+ Long.toString((new Date()).getTime())
+ " WHERE symbol = '" + data.cont.m_symbol +
"' AND secType = 'OPT' AND currency = '"
+ data.cont.m_currency + "' AND expiry='"
+ data.cont.m_expiry + "' AND strike =" +
Double.toString(data.cont.m_strike) + " AND callorput = '"
+ data.cont.m_right + "' AND multiplier = '"
+ data.cont.m_multiplier + "';");
}
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
public double outputLastPrice(ResultSet resultSet) throws SQLException {
Double lastPrice = -1.0;
while (resultSet.next()) {
lastPrice = resultSet.getDouble("last");
}
return lastPrice;
}
public int outputBugCounter(ResultSet resultSet) throws SQLException {
int bugCounter = 0;
while (resultSet.next()) {
bugCounter = resultSet.getInt("bugCounter");
}
return bugCounter;
}
}