Skip to content

Commit

Permalink
Merge pull request knowm#1390 from ww3456/develop
Browse files Browse the repository at this point in the history
[Bitfinex] order replace added to raw trade service
  • Loading branch information
timmolter authored Feb 23, 2017
2 parents c05b3e4 + c7c65b8 commit 015971d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusRequest;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexPastTradesRequest;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexReplaceOrderRequest;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexTradeResponse;

import si.mazi.rescu.ParamsDigest;
Expand Down Expand Up @@ -78,6 +79,11 @@ BitfinexCancelOrderMultiResponse cancelOrderMulti(@HeaderParam("X-BFX-APIKEY") S
@HeaderParam("X-BFX-SIGNATURE") ParamsDigest signature, BitfinexCancelOrderMultiRequest cancelOrderRequest)
throws IOException, BitfinexException;

@POST
@Path("order/cancel/replace")
BitfinexOrderStatusResponse replaceOrder(@HeaderParam("X-BFX-APIKEY") String apiKey, @HeaderParam("X-BFX-PAYLOAD") ParamsDigest payload,
@HeaderParam("X-BFX-SIGNATURE") ParamsDigest signature, BitfinexReplaceOrderRequest newOrderRequest) throws IOException, BitfinexException;

@POST
@Path("offer/cancel")
BitfinexOfferStatusResponse cancelOffer(@HeaderParam("X-BFX-APIKEY") String apiKey, @HeaderParam("X-BFX-PAYLOAD") ParamsDigest payload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public enum BitfinexOrderFlags implements IOrderFlags {
/**
* These are orders that allow you to be sure to always pay the maker fee.
*/
POST_ONLY
POST_ONLY,

/**
* For order amends indicates that the new order should use the remaining amount of the original order.
*/
USE_REMAINING
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.knowm.xchange.bitfinex.v1.dto.trade;

import java.math.BigDecimal;

import com.fasterxml.jackson.annotation.JsonProperty;

public class BitfinexReplaceOrderRequest extends BitfinexNewOrderRequest {

@JsonProperty("order_id")
protected long replaceOrderId;

@JsonProperty("use_remaining")
protected boolean useRemaining = false;

public BitfinexReplaceOrderRequest(String nonce, long replaceOrderId, String symbol, BigDecimal amount, BigDecimal price, String exchange, String side,
String type, boolean isHidden, boolean isPostOnly, boolean useRemaining) {

super(nonce, symbol, amount, price, exchange, side, type, isHidden, isPostOnly);

request = "/v1/order/cancel/replace";
this.replaceOrderId = replaceOrderId;
this.useRemaining = useRemaining;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusRequest;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexPastTradesRequest;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexReplaceOrderRequest;
import org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexTradeResponse;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.Order.OrderType;
Expand Down Expand Up @@ -88,7 +89,18 @@ public BitfinexOrderStatusResponse placeBitfinexMarketOrder(MarketOrder marketOr
}
}

public BitfinexOrderStatusResponse placeBitfinexLimitOrder(LimitOrder limitOrder, BitfinexOrderType bitfinexOrderType)
public BitfinexOrderStatusResponse placeBitfinexLimitOrder(LimitOrder limitOrder, BitfinexOrderType orderType) throws IOException {

return sendLimitOrder(limitOrder, orderType, Long.MIN_VALUE);
}

public BitfinexOrderStatusResponse replaceBitfinexLimitOrder(LimitOrder limitOrder, BitfinexOrderType orderType, long replaceOrderId)
throws IOException {

return sendLimitOrder(limitOrder, orderType, replaceOrderId);
}

private BitfinexOrderStatusResponse sendLimitOrder(LimitOrder limitOrder, BitfinexOrderType bitfinexOrderType, long replaceOrderId)
throws IOException {

String pair = BitfinexUtils.toPairString(limitOrder.getCurrencyPair());
Expand All @@ -108,19 +120,38 @@ public BitfinexOrderStatusResponse placeBitfinexLimitOrder(LimitOrder limitOrder
isPostOnly = false;
}

BitfinexNewOrderRequest request = new BitfinexNewOrderRequest(String.valueOf(exchange.getNonceFactory().createValue()), pair,
limitOrder.getTradableAmount(), limitOrder.getLimitPrice(), "bitfinex", type, orderType, isHidden, isPostOnly);
BitfinexOrderStatusResponse response;
if (replaceOrderId == Long.MIN_VALUE) { // order entry
BitfinexNewOrderRequest request = new BitfinexNewOrderRequest(String.valueOf(exchange.getNonceFactory().createValue()), pair,
limitOrder.getTradableAmount(), limitOrder.getLimitPrice(), "bitfinex", type, orderType, isHidden, isPostOnly);
try {
response = bitfinex.newOrder(apiKey, payloadCreator, signatureCreator, request);
} catch (BitfinexException e) {
throw new ExchangeException(e);
}

try {
BitfinexOrderStatusResponse response = bitfinex.newOrder(apiKey, payloadCreator, signatureCreator, request);
if(limitOrder instanceof BitfinexLimitOrder) {
BitfinexLimitOrder bitfinexOrder = (BitfinexLimitOrder) limitOrder;
bitfinexOrder.setResponse(response);
} else { // order amend
boolean useRemaining;
if (limitOrder.hasFlag(BitfinexOrderFlags.USE_REMAINING)) {
useRemaining = true;
} else {
useRemaining = false;
}
return response;
} catch (BitfinexException e) {
throw new ExchangeException(e);

BitfinexReplaceOrderRequest request = new BitfinexReplaceOrderRequest(String.valueOf(exchange.getNonceFactory().createValue()), replaceOrderId,
pair, limitOrder.getTradableAmount(), limitOrder.getLimitPrice(), "bitfinex", type, orderType, isHidden, isPostOnly, useRemaining);
try {
response = bitfinex.replaceOrder(apiKey, payloadCreator, signatureCreator, request);
} catch (BitfinexException e) {
throw new ExchangeException(e);
}
}

if (limitOrder instanceof BitfinexLimitOrder) {
BitfinexLimitOrder bitfinexOrder = (BitfinexLimitOrder) limitOrder;
bitfinexOrder.setResponse(response);
}
return response;
}

public BitfinexNewOrderMultiResponse placeBitfinexOrderMulti(List<? extends Order> orders, BitfinexOrderType bitfinexOrderType) throws IOException {
Expand Down

0 comments on commit 015971d

Please sign in to comment.