import com.ib.client.Contract;
import com.ib.client.ExecutionFilter;
public class AlgoAccount {
private IB ib;
private String accountId;
private AlgoWebSocket socket;
public AlgoAccount(IB ib, AlgoWebSocket socket, String accountId) {
this.ib = ib;
this.accountId = accountId;
this.socket = socket;
this.ib.registerAccount(this);
}
public void requestExecutions() {
ExecutionFilter filter = new ExecutionFilter();
int reqId = AlgoUtils.getId();
this.ib.client().reqExecutions(reqId, filter);
}
public void requestUpdates() {
System.out.println("request updates");
this.ib.client().reqAccountUpdates(false, this.accountId);
this.ib.client().reqAccountUpdates(true, this.accountId);
}
public void stopUpdates() {
this.ib.client().reqAccountUpdates(false, null);
}
public void handleAccountTime(String timeStamp) {
AlgoAccountTime data = new AlgoAccountTime();
data.time = timeStamp;
String accountJson = AlgoUtils.getJson("account-time", data);
this.socket.send(accountJson);
}
public void handlePortfolio(Contract contract, int position, double marketPrice, double marketValue, double averageCost, double unrealizedPNL, double realizedPNL, String accountName) {
System.out.println("portfolio");
AlgoAccountPortfolio portfolio = new AlgoAccountPortfolio();
portfolio.contract = contract;
portfolio.position = position;
portfolio.marketPrice = marketPrice;
portfolio.marketValue = marketValue;
portfolio.averageCost = averageCost;
portfolio.unrealizedPNL = unrealizedPNL;
portfolio.realizedPNL = realizedPNL;
String portfolioJson = AlgoUtils.getJson("positions-data", portfolio);
this.socket.send(portfolioJson);
}
public void handleAccountValue(String key, String value, String currency, String accountName) {
if (key == "FullMaintMarginReq" ||
key == "NetLiquidation" ||
key == "AvailableFunds") {
AlgoAccountValue data = new AlgoAccountValue();
data.key = key;
data.value = value;
data.currency = currency;
data.accountName = accountName;
String accountJson = AlgoUtils.getJson("account-data", data);
this.socket.send(accountJson);
}
}
}
class AlgoAccountPortfolio {
Contract contract;
int position;
double marketPrice;
double marketValue;
double averageCost;
double unrealizedPNL;
double realizedPNL;
}
class AlgoAccountTime {
String time;
}
class AlgoAccountValue {
public String key;
public String value;
public String currency;
public String accountName;
}