import java.util.*;
import com.ib.client.Contract;
import com.ib.client.Order;
import com.ib.client.OrderState;
public class AlgoContract extends Contract {
public String historicalDuration = "1 Y";
public String historicalBarSize = "1 day";
public int historicalUseRTH = 1;
public int historicalDateType = 2;
public String historicalEndTime = AlgoUtils.getCurrentDateTime();
public String historicalWhatToShow = "MIDPOINT";
public List<Bar> bars = new ArrayList<Bar>();
public Bar lastBar = new Bar();
protected transient IB interactiveBrokers;
public static AlgoContract createFx(IB interactiveBrokers, String symbol, String currency) {
AlgoContract contract = new AlgoContract(interactiveBrokers, symbol, currency);
contract.m_secType = "CASH";
contract.m_exchange = "IDEALPRO";
return contract;
}
public static AlgoContract createStk(IB interactiveBrokers, String symbol, String currency) {
AlgoContract contract = new AlgoContract(interactiveBrokers, symbol, currency);
contract.m_secType = "STK";
contract.m_exchange = "SMART";
return contract;
}
protected AlgoContract(IB interactiveBrokers, String symbol, String currency) {
m_symbol = symbol;
m_currency = currency;
this.interactiveBrokers = interactiveBrokers;
this.interactiveBrokers.registerOrders(this);
}
public void allOpenOrders() {
this.interactiveBrokers.client().reqAllOpenOrders();
}
// Call this method to request that newly created TWS orders be implicitly
// associated with the client.
public void bindAutoOpenOrders(boolean autoBind) {
this.interactiveBrokers.client().reqAutoOpenOrders(autoBind);
}
public void open(String action, int quantity, String timeInForce, String type) {
Order order = new Order();
order.m_orderType = type;
order.m_totalQuantity = quantity;
order.m_tif = timeInForce;
order.m_action = action;
this.order(order);
}
public void open(String action, int quantity, String timeInForce, String type, double limit) {
Order order = new Order();
order.m_orderType = type;
order.m_totalQuantity = quantity;
order.m_tif = timeInForce;
order.m_lmtPrice = limit;
order.m_action = action;
this.order(order);
}
public void open(String action, int quantity, String timeInForce, String type, double limit, double aux) {
Order order = new Order();
order.m_orderType = type;
order.m_totalQuantity = quantity;
order.m_tif = timeInForce;
order.m_lmtPrice = limit;
order.m_action = action;
order.m_auxPrice = aux;
this.order(order);
}
protected void order(Order order) {
int id = AlgoUtils.getId();
System.out.println("interactive brokers open order");
this.interactiveBrokers.client().placeOrder(id, this, order);
}
public void handleOpenOrder(int orderId, Contract contract, Order order, OrderState orderState) {
System.out.println("handling open order");
}
public void handleOrderStatus(int orderId, String status, int filled, int remaining, double avgFillPrice, int permId, int parentId, double lastFillPrice,int clientId, String whyHeld) {
System.out.println("handling order status");
System.out.println("status");
System.out.println(status);
System.out.println(filled);
System.out.println(remaining);
System.out.println(avgFillPrice);
System.out.println(whyHeld);
System.out.println(lastFillPrice);
}
@Override
public boolean equals(Object object)
{
boolean sameSame = false;
if (object != null && object instanceof AlgoContract) {
sameSame = (
this.m_secType == ((AlgoContract) object).m_secType &&
this.m_exchange == ((AlgoContract) object).m_exchange &&
this.m_symbol == ((AlgoContract) object).m_symbol &&
this.m_currency == ((AlgoContract) object).m_currency
);
}
return sameSame;
}
protected void printBars() {
for (Bar elem : bars) {
System.out.println(elem.toString());
}
}
protected void printLast() {
System.out.println(bars.get(bars.size() - 2).toString());
System.out.println(bars.get(bars.size() - 1).toString());
}
}