-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMain.java
More file actions
146 lines (122 loc) · 5.59 KB
/
Copy pathMain.java
File metadata and controls
146 lines (122 loc) · 5.59 KB
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
/**
* @author LYNX B.V. Amsterdam
* @version 1.0
* <p>
* Copyright (C) 2019 LYNX B.V. All rights reserved.
* <p>
* A simple Java implementation for (1) placing an order, (2) requesting contract details, (3) requesting market data or
* (4) requesting historical data for a product from the TWS (Trader Workstation) or the LYNX Gateway
*/
import com.ib.client.Contract;
import com.ib.client.Order;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static Contract example_contract;
private static Order example_order;
private static TWSConnection twsConnection;
public static void main(String[] args) {
// Init twsConnection Object, see TWSConnection.java file
twsConnection = new TWSConnection();
twsConnection.makeConnection();
createSampleContractAndOrder();
askUserForInput();
}
/**
* Method that initializes a sample contract & order
*/
private static void createSampleContractAndOrder() {
// Define Contract details
example_contract = new Contract();
example_contract.localSymbol("USD.CAD");
example_contract.secType("CASH");
example_contract.currency("CAD");
example_contract.exchange("IDEALPRO");
// Define Order details
example_order = new Order();
example_order.action("BUY");
example_order.orderType("MKT");
example_order.totalQuantity(1);
}
/**
* Asks and listens for user input, therefore firing different actions
*/
private static void askUserForInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("\nPlease choose an action: \n\n(0) Exit\n(1) Place order \n(2) Request contract details\n" +
"(3) Request market data\n(4) Request historical data\n\nEnter your choice as a number: ");
int response = checkInput(scanner);
switch (response) {
case 0:
System.out.println("[INFO] Program closed.");
System.exit(0);
break;
case 1:
System.out.print("\n(1) You chose to place an order.\nDefault contract: USD.CAD/CASH/IDEALPRO & " +
"Default order: BUY/MKT/1\n[WARNING] The order type is 'Market Order', therefore it will " +
"most likely get filled immediately after placing the order. Do you wish to continue? (y/n)" +
"\nResponse (e.g.: y): ");
if (scanner.nextLine().equals("y")) {
twsConnection.client.placeOrder(twsConnection.getNextValidId(), example_contract, example_order);
} else {
System.out.println("\n[INFO] Make another choice.");
askUserForInput();
}
break;
case 2:
System.out.print("\n(2) You chose to request contract details.\nDefault contract: USD.CAD/CASH/IDEALPRO" +
"\nDo you wish to continue? (y/n)\nResponse (e.g.: y): ");
if (scanner.nextLine().equals("y")) {
twsConnection.client.reqContractDetails(1, example_contract);
} else {
System.out.println("\n[INFO] Make another choice.");
askUserForInput();
}
break;
case 3:
System.out.print("\n(3) You chose to request market data.\nDefault contract: USD.CAD/CASH/IDEALPRO" +
"\nDo you wish to continue? (y/n)\nResponse (e.g.: y): ");
if (scanner.nextLine().equals("y")) {
twsConnection.client.reqMktData(1, example_contract, "", false, false, null);
} else {
System.out.println("\n[INFO] Make another choice.");
askUserForInput();
}
break;
case 4:
System.out.print("\n(4) You chose to request historical data.\nDefault contract: USD.CAD/CASH/IDEALPRO" +
"The result will contain midpoint data as 15 mins bars for one day (20190510)." +
"\nDo you wish to continue? (y/n)\nResponse (e.g.: y): ");
if (scanner.nextLine().equals("y")) {
twsConnection.client.reqHistoricalData(1, example_contract, "20190510 23:59:59 GMT", "1 D",
"15 mins", "MIDPOINT", 1, 1, false, new ArrayList<>());
} else {
System.out.println("\n[INFO] Make another choice.");
askUserForInput();
}
break;
}
}
/**
*
* @param scanner used to get keyboard user input
* @return response expressed as an integer (1 to 4)
*/
private static int checkInput(Scanner scanner) {
String userInput = scanner.nextLine();
int finalResponse;
try {
Integer.parseInt(userInput);
if (Integer.parseInt(userInput) > 4) {
System.out.print("Not an available option. Please enter a number (e.g.: 1): ");
finalResponse = checkInput(scanner);
return finalResponse;
}
return Integer.parseInt(userInput);
} catch (NumberFormatException e) {
System.out.print("Wrong input. Please enter a number (e.g.: 1): ");
finalResponse = checkInput(scanner);
}
return finalResponse;
}
}