- package Arb;
- import com.ib.client.EClientSocket;
- import com.ib.client.Contract;
- import com.ib.client.TickType;
- import java.lang.*;
- import java.io.*;
- import java.util.*;
- public class OrderMgr {
- public enum OrderState {
- NOT_PLACED
- }
- public class MgrOrder {
- OrderState mState = OrderState.NOT_PLACED;
- int mPrice=-1;
- public void Buy(int price)
- {
- //TODO: send req to IB
- mPrice = price;
- }
- public void Sell(int price)
- {
- //TODO: send req to IB
- mPrice = price;
- }
- }
- MgrOrder mBuys[];
- MgrOrder mSells[];
- int mDepth;
- int mMaxPosition;
- MyOrder mProto;
- int mCurrentBid;
- int mCurrentAsk;
- public OrderMgr(int depth, MyOrder prototype, int maxPosition) {
- mDepth = depth;
- mProto = prototype;
- mMaxPosition = maxPosition;
- mCurrentBid = 0;
- mCurrentAsk = 1000000000;
- mBuys = new MgrOrder[depth];
- mSells = new MgrOrder[depth];
- for(int i=0; i < depth; i++)
- {
- mBuys[i] = new MgrOrder();
- mSells[i] = new MgrOrder();
- }
- }
- //Prices in ticks
- public void Update(int bid, int ask)
- {
- mCurrentBid=bid;
- mCurrentAsk=ask;
- Update();
- }
- public void UpdateOrderState()
- {
- //TODO: update the order states
- Update();
- }
- public int[] GetStates()
- {
- int ret[] = new int[mDepth*2];
- for(int i=0; i < mDepth; i++)
- {
- ret[i] = mSells[i].mPrice;
- ret[mDepth+i] = mBuys[i].mPrice;
- }
- return ret;
- }
- //TODO: mPrice isn't good enough! need to check all possible prices the order could have
- boolean checkBuy(int price)
- {
- for(int i=0; i < mDepth; i++)
- if(mSells[i].mPrice <= price && mSells[i].mPrice!=-1)
- return false;
- return true;
- }
- //TODO: mPrice isn't good enough! need to check all possible prices the order could have
- boolean checkSell(int price)
- {
- for(int i=0; i < mDepth; i++)
- if(mBuys[i].mPrice >= price && mBuys[i].mPrice!=-1)
- return false;
- return true;
- }
- public void Update()
- {
- //TODO: we can't issue bid/ask movements that would cause self ordering
- //so one half of this will have to wait until the orders are acked on the other side
- //Any current bids above bid must be moved to bid or below if possible
- ArrayList<MgrOrder> canMove = new ArrayList<MgrOrder>();
- for(int i=0; i < mDepth; i++)
- {
- MgrOrder order = mBuys[i];
- if(order.mPrice>mCurrentBid||order.mPrice<mCurrentBid-(mDepth-1))
- canMove.add(order);
- }
- for(int i=0; i < canMove.size(); i++)
- {
- MgrOrder order = canMove.get(i);
- int bid = 0;
- for(int j=0; j < mDepth; j++)
- {
- bid = mCurrentBid-j;
- boolean found=false;
- for(int k=0; k < mDepth; k++)
- {
- if(mBuys[k].mPrice==bid)
- {
- found=true;
- break;
- }
- }
- if(!found)
- break;
- }
- //Bid has the new target price
- if(checkBuy(bid))
- order.Buy(bid);
- }
- //Any current asks must be moved above or to the current ask
- canMove = new ArrayList<MgrOrder>();
- for(int i=0; i < mDepth; i++)
- {
- MgrOrder order = mSells[i];
- if(order.mPrice<mCurrentAsk||order.mPrice>mCurrentAsk+(mDepth-1))
- canMove.add(order);
- }
- for(int i=0; i < canMove.size(); i++)
- {
- MgrOrder order = canMove.get(i);
- int ask = 0;
- for(int j=0; j < mDepth; j++)
- {
- ask = mCurrentAsk+j;
- boolean found=false;
- for(int k=0; k < mDepth; k++)
- {
- if(mSells[k].mPrice==ask)
- {
- found=true;
- break;
- }
- }
- if(!found)
- break;
- }
- //Ask has the new target price
- if(checkSell(ask))
- order.Sell(ask);
- }
- }
- };