我不知道这对所有500人都有效,但你可以试试.数据来自
https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents.csv
SP
package sp;
import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class SP {
//just a sample, like this so you can just use Files.lines instead.
private static List lines = Arrays.asList(new String[]{
"Symbol,Name,Sector",
"MMM,3M Company,Industrials",
"ABT,Abbott Laboratories,Health Care",
"ABBV,AbbVie,Health Care",
"ACN,Accenture plc,Information Technology",
"ATVI,Activision Blizzard,Information Technology",
"AYI,Acuity Brands Inc,Industrials",
"ADBE,Adobe Systems Inc,Information Technology",
"AAP,Advance Auto Parts,Consumer Discretionary",
"AES,AES Corp,Utilities",
"AET,Aetna Inc,Health Care",
"AMG,Affiliated Managers Group Inc,Financials",
"AFL,AFLAC Inc,Financials",
"A,Agilent Technologies Inc,Health Care",
"APD,Air Products & Chemicals Inc,Materials",
"AKAM,Akamai Technologies Inc,Information Technology",
});
public static void main(String[] args) throws InterruptedException{
EWrapper wrapper = new Wrapper();
EClientSocket socket = new EClientSocket(wrapper);
socket.eConnect("", 4001, 123);
//supposedly gives frozen last recorded value, not working!
socket.reqMarketDataType(2);
AtomicInteger tickerId = new AtomicInteger(0);
lines.stream().skip(1).forEach(line -> {
//new cont for every request
Contract cont = new Contract();
cont.m_currency = "usd";
cont.m_exchange = "smart";
cont.m_secType = "stk";
cont.m_symbol = line.split(",")[0];
Data data = new Data(cont, socket);
});
//need you own logic for when to end program
//Thread.sleep(5000);//this thread, Socket starts a reader thread
//socket.eDisconnect();
}
}
包装纸
package sp;
import com.ib.client.CommissionReport;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
import com.ib.client.TickType;
import com.ib.client.UnderComp;
import java.util.HashMap;
import java.util.Map;
public class Wrapper implements EWrapper{
public Map dataMap = new HashMap<>();
public Map orderMap = new HashMap<>();
//reqMktData snapshots are received here
@Override
public void tickPrice(int tickerId, int field, double price, int canAutoExecute) {
if (field == TickType.LAST) {
//if you just want the last price
dataMap.get(tickerId).dataRecd(price);
}
}
@Override
public void execDetails(int reqId, Contract contract, Execution execution) {
orderMap.get(execution.m_orderId).exec(execution);
}
//snip
}
数据
package sp;
import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class Data {
final Contract cont;
private final EClientSocket socket;
private final Strat strat;
private static int nextId = 1; //auto increment for each request
private final int myId;
List prices = new ArrayList<>();
double lastPrice = -1;
public Data(Contract cont, EClientSocket socket) {
this.cont = cont;
this.socket = socket;
strat = new Strat(this, socket);
myId = nextId++;
((Wrapper) socket.wrapper()).dataMap.put(myId, this);
reqData();
// //call every 10 min
// Timer timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// reqData();
// }
// }, 10 * 60 * 1000);
}
private void reqData(){
socket.reqMktData(myId, cont, "", false /* true */, null);
}
public void dataRecd(double last){
lastPrice = last;
prices.add(last);
strat.check();
}
}
斯特拉特
package sp;
import com.ib.client.EClientSocket;
import com.ib.client.Execution;
public class Strat {
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 Data data;
private final EClientSocket socket;
private static int nextOrderId = 1;
Strat(Data data, EClientSocket socket) {
this.data = data;
this.socket = socket;
sysState = LOOK;
}
void check() {
System.out.println("should I buy? "+ data.cont.m_symbol + " @ " + data.lastPrice);
/*if (false && sysState & LOOK == LOOK) {
((Wrapper) socket.wrapper()).orderMap.put(nextOrderId, this);
socket.placeOrder(nextOrderId++, data.cont, new Order());
sysState = WAIT_FILL;
nextOrderId++;
}*/
}
public void exec(Execution exec){
//will be called by wrapper after an exec.
//sysState = LONG; //or whatever
}
}