Guest User

Untitled

a guest
Aug 12th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. package Arb;
  2.  
  3. import com.ib.client.EClientSocket;
  4. import com.ib.client.Contract;
  5. import com.ib.client.TickType;
  6. import java.lang.*;
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. public class OrderMgr {
  11. public enum OrderState {
  12. NOT_PLACED
  13. }
  14.  
  15.  
  16.  
  17. public class MgrOrder {
  18. OrderState mState = OrderState.NOT_PLACED;
  19. int mPrice=-1;
  20.  
  21. public void Buy(int price)
  22. {
  23. //TODO: send req to IB
  24. mPrice = price;
  25. }
  26.  
  27. public void Sell(int price)
  28. {
  29. //TODO: send req to IB
  30. mPrice = price;
  31. }
  32. }
  33.  
  34. MgrOrder mBuys[];
  35. MgrOrder mSells[];
  36.  
  37. int mDepth;
  38. int mMaxPosition;
  39. MyOrder mProto;
  40. int mCurrentBid;
  41. int mCurrentAsk;
  42.  
  43. public OrderMgr(int depth, MyOrder prototype, int maxPosition) {
  44. mDepth = depth;
  45. mProto = prototype;
  46. mMaxPosition = maxPosition;
  47. mCurrentBid = 0;
  48. mCurrentAsk = 1000000000;
  49.  
  50. mBuys = new MgrOrder[depth];
  51. mSells = new MgrOrder[depth];
  52.  
  53. for(int i=0; i < depth; i++)
  54. {
  55. mBuys[i] = new MgrOrder();
  56. mSells[i] = new MgrOrder();
  57. }
  58. }
  59.  
  60. //Prices in ticks
  61. public void Update(int bid, int ask)
  62. {
  63. mCurrentBid=bid;
  64. mCurrentAsk=ask;
  65.  
  66. Update();
  67. }
  68.  
  69. public void UpdateOrderState()
  70. {
  71. //TODO: update the order states
  72. Update();
  73. }
  74.  
  75. public int[] GetStates()
  76. {
  77. int ret[] = new int[mDepth*2];
  78. for(int i=0; i < mDepth; i++)
  79. {
  80. ret[i] = mSells[i].mPrice;
  81. ret[mDepth+i] = mBuys[i].mPrice;
  82. }
  83. return ret;
  84. }
  85.  
  86. //TODO: mPrice isn't good enough! need to check all possible prices the order could have
  87. boolean checkBuy(int price)
  88. {
  89. for(int i=0; i < mDepth; i++)
  90. if(mSells[i].mPrice <= price && mSells[i].mPrice!=-1)
  91. return false;
  92. return true;
  93. }
  94.  
  95. //TODO: mPrice isn't good enough! need to check all possible prices the order could have
  96. boolean checkSell(int price)
  97. {
  98. for(int i=0; i < mDepth; i++)
  99. if(mBuys[i].mPrice >= price && mBuys[i].mPrice!=-1)
  100. return false;
  101. return true;
  102. }
  103.  
  104. public void Update()
  105. {
  106. //TODO: we can't issue bid/ask movements that would cause self ordering
  107. //so one half of this will have to wait until the orders are acked on the other side
  108.  
  109. //Any current bids above bid must be moved to bid or below if possible
  110. ArrayList<MgrOrder> canMove = new ArrayList<MgrOrder>();
  111. for(int i=0; i < mDepth; i++)
  112. {
  113. MgrOrder order = mBuys[i];
  114. if(order.mPrice>mCurrentBid||order.mPrice<mCurrentBid-(mDepth-1))
  115. canMove.add(order);
  116. }
  117.  
  118. for(int i=0; i < canMove.size(); i++)
  119. {
  120. MgrOrder order = canMove.get(i);
  121. int bid = 0;
  122. for(int j=0; j < mDepth; j++)
  123. {
  124. bid = mCurrentBid-j;
  125. boolean found=false;
  126. for(int k=0; k < mDepth; k++)
  127. {
  128. if(mBuys[k].mPrice==bid)
  129. {
  130. found=true;
  131. break;
  132. }
  133. }
  134. if(!found)
  135. break;
  136. }
  137. //Bid has the new target price
  138. if(checkBuy(bid))
  139. order.Buy(bid);
  140. }
  141.  
  142. //Any current asks must be moved above or to the current ask
  143. canMove = new ArrayList<MgrOrder>();
  144. for(int i=0; i < mDepth; i++)
  145. {
  146. MgrOrder order = mSells[i];
  147. if(order.mPrice<mCurrentAsk||order.mPrice>mCurrentAsk+(mDepth-1))
  148. canMove.add(order);
  149. }
  150.  
  151. for(int i=0; i < canMove.size(); i++)
  152. {
  153. MgrOrder order = canMove.get(i);
  154. int ask = 0;
  155. for(int j=0; j < mDepth; j++)
  156. {
  157. ask = mCurrentAsk+j;
  158. boolean found=false;
  159. for(int k=0; k < mDepth; k++)
  160. {
  161. if(mSells[k].mPrice==ask)
  162. {
  163. found=true;
  164. break;
  165. }
  166. }
  167. if(!found)
  168. break;
  169. }
  170.  
  171. //Ask has the new target price
  172. if(checkSell(ask))
  173. order.Sell(ask);
  174. }
  175.  
  176.  
  177. }
  178.  
  179. };
Advertisement
Add Comment
Please, Sign In to add comment