@@ -226,8 +226,8 @@ private void enterPosition(Spread spread) {
226226 try {
227227 longLimitPrice = getLimitPrice (spread .getLongExchange (), spread .getCurrencyPair (), tradeVolume .getLongVolume (), Order .OrderType .ASK );
228228 shortLimitPrice = getLimitPrice (spread .getShortExchange (), spread .getCurrencyPair (), tradeVolume .getShortVolume (), Order .OrderType .BID );
229- } catch (ExchangeException e ) {
230- LOGGER .warn ("Failed to fetch order books for {}/{} and currency {}/{} to compute entry prices: {}" ,
229+ } catch (IOException | ExchangeException e ) {
230+ LOGGER .error ("Failed to fetch order books for {}/{} and currency {}/{} to compute entry prices: {}" ,
231231 longExchangeName ,
232232 spread .getShortExchange ().getDefaultExchangeSpecification ().getExchangeName (),
233233 spread .getCurrencyPair ().base ,
@@ -456,7 +456,7 @@ private void exitPosition(Spread spread) {
456456 try {
457457 longLimitPrice = getLimitPrice (spread .getLongExchange (), spread .getCurrencyPair (), tradeVolume .getLongVolume (), Order .OrderType .BID );
458458 shortLimitPrice = getLimitPrice (spread .getShortExchange (), spread .getCurrencyPair (), tradeVolume .getShortVolume (), Order .OrderType .ASK );
459- } catch (ExchangeException e ) {
459+ } catch (IOException | ExchangeException e ) {
460460 LOGGER .warn ("Failed to fetch order books (on active position) for {}/{} and currency {}/{} to compute entry prices: {}" ,
461461 longExchangeName ,
462462 spread .getShortExchange ().getDefaultExchangeSpecification ().getExchangeName (),
@@ -941,35 +941,35 @@ BigDecimal getVolumeForOrder(Exchange exchange, CurrencyPair currencyPair, Strin
941941 * @param orderType Are we buying or selling? Use the bid or ask price?
942942 * @return The more accurate price for this order.
943943 */
944- BigDecimal getLimitPrice (Exchange exchange , CurrencyPair rawCurrencyPair , BigDecimal allowedVolume , Order .OrderType orderType ) {
944+ BigDecimal getLimitPrice (Exchange exchange , CurrencyPair rawCurrencyPair , BigDecimal allowedVolume , Order .OrderType orderType ) throws IOException {
945945 CurrencyPair currencyPair = exchangeService .convertExchangePair (exchange , rawCurrencyPair );
946+ OrderBook orderBook = exchange .getMarketDataService ().getOrderBook (currencyPair );
947+ List <LimitOrder > orders = orderType .equals (Order .OrderType .ASK ) ? orderBook .getAsks () : orderBook .getBids ();
948+ BigDecimal price ;
949+ BigDecimal volume = BigDecimal .ZERO ;
946950
947- try {
948- OrderBook orderBook = exchange .getMarketDataService ().getOrderBook (currencyPair );
949- List <LimitOrder > orders = orderType .equals (Order .OrderType .ASK ) ? orderBook .getAsks () : orderBook .getBids ();
950- BigDecimal price ;
951- BigDecimal volume = BigDecimal .ZERO ;
952-
953- // Walk through orders, ordered by price, until we satisfy all the volume we need.
954- // Return the price of the last order we see.
955- //
956- // If we set our limit order at this price (without waiting too long) it is very likely to fill
957- // because we know the exchange has enough currency available to fill it at this or a better price.
958- for (LimitOrder order : orders ) {
959- price = order .getLimitPrice ();
951+ // Walk through orders, ordered by price, until we satisfy all the volume we need.
952+ // Return the price of the last order we see.
953+ //
954+ // If we set our limit order at this price (without waiting too long) it is very likely to fill
955+ // because we know the exchange has enough currency available to fill it at this or a better price.
956+ for (LimitOrder order : orders ) {
957+ price = order .getLimitPrice ();
958+
959+ if (order .getRemainingAmount () == null || BigDecimal .ZERO .compareTo (order .getRemainingAmount ()) == 0 ) {
960+ volume = volume .add (order .getOriginalAmount ());
961+ } else {
960962 volume = volume .add (order .getRemainingAmount ());
963+ }
961964
962- if (volume .compareTo (allowedVolume ) > 0 ) {
963- int scale = computePriceScale (exchange , currencyPair );
965+ if (volume .compareTo (allowedVolume ) > 0 ) {
966+ int scale = computePriceScale (exchange , currencyPair );
964967
965- return price .setScale (scale , RoundingMode .HALF_EVEN );
966- }
968+ return price .setScale (scale , RoundingMode .HALF_EVEN );
967969 }
968- } catch (IOException e ) {
969- LOGGER .error ("IOE fetching {} {} order volume" , exchange .getExchangeSpecification ().getExchangeName (), currencyPair , e );
970970 }
971971
972- throw new RuntimeException ("Not enough liquidity on exchange to fulfill required volume!" );
972+ throw new LiquidityException ("Not enough liquidity on exchange to fulfill required volume!" );
973973 }
974974
975975 /**
0 commit comments