|
1 | 1 | package codingblackfemales.gettingstarted; |
2 | 2 |
|
3 | 3 | import codingblackfemales.action.Action; |
| 4 | +import codingblackfemales.action.CancelChildOrder; |
| 5 | +import codingblackfemales.action.CreateChildOrder; |
4 | 6 | import codingblackfemales.action.NoAction; |
5 | 7 | import codingblackfemales.algo.AlgoLogic; |
| 8 | +import codingblackfemales.sotw.ChildOrder; |
6 | 9 | import codingblackfemales.sotw.SimpleAlgoState; |
| 10 | +import codingblackfemales.sotw.marketdata.AskLevel; |
| 11 | +import codingblackfemales.sotw.marketdata.BidLevel; |
7 | 12 | import codingblackfemales.util.Util; |
| 13 | +import messages.order.Side; |
8 | 14 | import org.slf4j.Logger; |
9 | 15 | import org.slf4j.LoggerFactory; |
10 | 16 |
|
| 17 | +import java.util.Iterator; |
| 18 | + |
11 | 19 | public class MyAlgoLogic implements AlgoLogic { |
12 | 20 |
|
13 | 21 | private static final Logger logger = LoggerFactory.getLogger(MyAlgoLogic.class); |
| 22 | + // Thresholds for buy and sell actions |
| 23 | + private static long buyThreshold = 95; // Minimum acceptable bid price for a buy order |
| 24 | + private static long sellThreshold = 115; // Minimum acceptable ask price for a sell order |
| 25 | + private static long spreadThreshold = -3; // Minimum spread threshold for action |
| 26 | + |
| 27 | + |
14 | 28 |
|
15 | 29 | @Override |
16 | 30 | public Action evaluate(SimpleAlgoState state) { |
17 | 31 |
|
18 | 32 | var orderBookAsString = Util.orderBookToString(state); |
19 | 33 |
|
| 34 | +//shows current state of order book and current state of active orders |
20 | 35 | logger.info("[MYALGO] The state of the order book is:\n" + orderBookAsString); |
| 36 | + logger.info("Active Orders:" + state.getActiveChildOrders().toString()); |
| 37 | + |
| 38 | + var totalOrderCount = state.getChildOrders().size(); |
| 39 | + |
| 40 | + //make sure we have an exit condition... |
| 41 | + if (totalOrderCount > 10) { |
| 42 | + return NoAction.NoAction; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + final BidLevel bidlevel = state.getBidAt(0); |
| 48 | + final long bestBidPrice = bidlevel.price; |
| 49 | + final long bidQuantity = bidlevel.quantity; |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + final AskLevel asklevel = state.getAskAt(0); |
| 54 | + final long bestAskPrice = asklevel.price; |
| 55 | + final long askQuantity = asklevel.quantity; |
| 56 | + |
| 57 | + |
| 58 | + //calculate the spread |
| 59 | + final long spread = bestAskPrice - bestBidPrice; |
| 60 | + logger.info("[MYALGO] Spread between ask and bid " + spread); |
| 61 | + |
| 62 | + //calculate the midPrice |
| 63 | + final long midPrice =(bestBidPrice + bestBidPrice)/2; |
| 64 | + logger.info("[MYALGO] Mid-price calculated:" + midPrice); |
| 65 | + |
| 66 | + //Calculate the spread as a percentage of the mid-price |
| 67 | + final double spreadPercentage = (double) spread / midPrice * 100; |
| 68 | + logger.info("[MYALGO] Spread percentage: " + spreadPercentage + "%"); |
| 69 | + |
| 70 | + // Check for matching orders |
| 71 | + matchOrders(state, bestBidPrice, bestAskPrice); |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + // not to create or cancel orders because spread is small |
| 76 | + if (spread < spreadThreshold) { |
| 77 | + logger.info("[MYALGO] The spread is small " + spread + " No Action"); |
| 78 | + return NoAction.NoAction; |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +//count the buy and sell orders |
| 84 | + long buyOrdersCount = state.getChildOrders().stream().filter(ChildOrder -> ChildOrder.getSide() == Side.BUY).count(); |
| 85 | + long sellOrdersCount = state.getChildOrders().stream().filter(ChildOrder -> ChildOrder.getSide() == Side.SELL).count(); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + //create new buy orders if there are less than 5 orders |
| 90 | + if (buyOrdersCount < 5 ) { |
| 91 | + return createBuyOrder(state, buyOrdersCount, bidQuantity, bestBidPrice); |
| 92 | + } |
21 | 93 |
|
22 | | - /******** |
23 | | - * |
24 | | - * Add your logic here.... |
25 | | - * |
26 | | - */ |
| 94 | + //create new sell orders if there are less than 5 order |
| 95 | + if (sellOrdersCount < 5 ) { |
| 96 | + return createSellOrder(state, sellOrdersCount, askQuantity, bestAskPrice); |
| 97 | + } |
| 98 | + |
| 99 | + //cancel orders that don't match the best price |
| 100 | + |
| 101 | + return cancelOrders(state, bestBidPrice, bestAskPrice); |
| 102 | + } |
| 103 | + |
| 104 | + private void matchOrders(SimpleAlgoState state, long bestBidPrice, long bestAskPrice) { |
| 105 | + Iterator<ChildOrder> iterator = state.getActiveChildOrders().iterator(); |
| 106 | + |
| 107 | + while (iterator.hasNext()) { |
| 108 | + ChildOrder order = iterator.next(); |
| 109 | + if (order.getSide() == Side.BUY && order.getPrice() >= bestAskPrice) { |
| 110 | + logger.info("[MYALGO] Matched BUY order: Order ID: #" + order.getOrderId() +", Side: " + order.getSide() + ", Price: " + order.getPrice() + ", Quantity: " + order.getQuantity()); |
| 111 | + iterator.remove(); // Remove matched order |
| 112 | + } else if (order.getSide() == Side.SELL && order.getPrice() <= bestBidPrice) { |
| 113 | + logger.info("[MYALGO] Matched SELL order: Order ID: #" + order.getOrderId() +", Side: " + order.getSide() + ", Price: " + order.getPrice() + ", Quantity: " + order.getQuantity()); |
| 114 | + iterator.remove(); // Remove matched order |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + //create method to create new buy order |
| 124 | + public Action createBuyOrder(SimpleAlgoState state, long buyOrdersCount, long bidQuantity, long bestBidPrice) { |
| 125 | + logger.info("[MYALGO] Creating new buy order: " + state.getChildOrders().size() + " orders and add to new buy order " + bidQuantity + " @ " + bestBidPrice); |
| 126 | + logger.info(state.getActiveChildOrders().toString()); |
| 127 | + logger.info("Buy order count is:" + buyOrdersCount); |
| 128 | + //creates a new child order |
| 129 | + return new CreateChildOrder(Side.BUY, bidQuantity, bestBidPrice); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + // create a method to create a new sell order |
| 134 | + public Action createSellOrder(SimpleAlgoState state, long sellOrdersCount, long askQuantity, long bestAskPrice) { |
| 135 | + logger.info("[MYALGO] Creating new sell order:" + state.getChildOrders().size() + " orders and add to new sell order " + askQuantity + " @ " + bestAskPrice); |
| 136 | + logger.info("Sell order count is:" + sellOrdersCount); |
| 137 | + //creates a new child order |
| 138 | + return new CreateChildOrder(Side.SELL, askQuantity, bestAskPrice); |
| 139 | + } |
| 140 | + |
| 141 | + //create a method to cancel orders that don't match best price or fall below thresholds |
| 142 | + public Action cancelOrders(SimpleAlgoState state, long bestBidPrice, long bestAskPrice){ |
| 143 | + for (ChildOrder order : state.getActiveChildOrders()){ |
| 144 | + logger.info("Order ID: #" + order.getOrderId() + ", Side: " + order.getSide() + ", Price: " + order.getPrice() + ", Quantity: " + order.getQuantity()); |
| 145 | + |
| 146 | + boolean buyOrder = order.getSide() ==Side.BUY; |
| 147 | + boolean notBuyThreshold = order.getPrice() != buyThreshold; |
| 148 | + boolean lessThanBuyThreshold = order.getPrice() < buyThreshold; |
| 149 | + |
| 150 | + // Cancel buy orders not matching the best price or below the buy threshold |
| 151 | + if (buyOrder && (notBuyThreshold || lessThanBuyThreshold)) { |
| 152 | + logger.info(String.format("The buy Threshold is %d", + buyThreshold )); |
| 153 | + logger.info(String.format("[MYALGO] Cancel BUY order %d with price %d and quantity %d. The current best bid price is %d." ,+ order.getOrderId(), + order.getPrice(), + order.getQuantity(), + bestBidPrice)); |
| 154 | + return new CancelChildOrder(order); |
| 155 | + } |
| 156 | + boolean sellTheOrder = order.getSide() ==Side.SELL; |
| 157 | + boolean notSellThreshold = order.getPrice() != sellThreshold; |
| 158 | + boolean lessThanSellThreshold = order.getPrice() < sellThreshold; |
| 159 | + |
| 160 | + |
| 161 | + // Cancel sell orders not matching the best price or below the sell threshold |
| 162 | + if (sellTheOrder && (notSellThreshold || lessThanSellThreshold)) { |
| 163 | + logger.info(String.format("The sell Threshold is %d", + sellThreshold )); |
| 164 | + logger.info(String.format("[MYALGO] Cancel SELL order %d with price %d and quantity %d. The current best ask price is %d." ,+ order.getOrderId(), + order.getPrice(), + order.getQuantity(), + bestAskPrice)); |
| 165 | + return new CancelChildOrder(order); |
| 166 | + } |
| 167 | + } |
27 | 168 |
|
28 | 169 | return NoAction.NoAction; |
29 | 170 | } |
30 | 171 | } |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
0 commit comments