public BitfinexOrderStatusResponse placeBitfinexMarketOrder(
      MarketOrder marketOrder, BitfinexOrderType bitfinexOrderType) throws IOException {

    String pair = BitfinexUtils.toPairString(marketOrder.getCurrencyPair());
    String type = marketOrder.getType().equals(Order.OrderType.BID) ? "buy" : "sell";
    String orderType = bitfinexOrderType.toString();

    try {
      BitfinexOrderStatusResponse newOrder =
          bitfinex.newOrder(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexNewOrderRequest(
                  String.valueOf(exchange.getNonceFactory().createValue()),
                  pair,
                  marketOrder.getTradableAmount(),
                  BigDecimal.ONE,
                  "bitfinex",
                  type,
                  orderType));
      return newOrder;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public BitfinexCreditResponse[] getBitfinexActiveCredits() throws IOException {

    try {
      BitfinexCreditResponse[] credits =
          bitfinex.activeCredits(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexActiveCreditsRequest(
                  String.valueOf(exchange.getNonceFactory().createValue())));
      return credits;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public BitfinexOfferStatusResponse[] getBitfinexOpenOffers() throws IOException {

    try {
      BitfinexOfferStatusResponse[] activeOffers =
          bitfinex.activeOffers(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexNonceOnlyRequest(
                  "/v1/offers", String.valueOf(exchange.getNonceFactory().createValue())));
      return activeOffers;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public BitfinexOrderStatusResponse getBitfinexOrderStatus(String orderId) throws IOException {

    try {
      BitfinexOrderStatusResponse orderStatus =
          bitfinex.orderStatus(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexOrderStatusRequest(
                  String.valueOf(exchange.getNonceFactory().createValue()),
                  Integer.valueOf(orderId)));
      return orderStatus;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public BitfinexOfferStatusResponse cancelBitfinexOffer(String offerId) throws IOException {

    try {
      BitfinexOfferStatusResponse cancelResponse =
          bitfinex.cancelOffer(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexCancelOfferRequest(
                  String.valueOf(exchange.getNonceFactory().createValue()),
                  Integer.valueOf(offerId)));
      return cancelResponse;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public boolean cancelBitfinexOrderMulti(List<String> orderIds) throws IOException {

    int[] cancelOrderIds = new int[orderIds.size()];

    for (int i = 0; i < cancelOrderIds.length; i++) {
      cancelOrderIds[i] = Integer.valueOf(orderIds.get(i));
    }

    try {
      //     bitfinex.cancelOrderMulti(apiKey, payloadCreator, signatureCreator, new
      // BitfinexCancelOrderMultiRequest(String.valueOf(exchange.getNonceFactory().createValue()),
      // cancelOrderIds));
      return true;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public boolean cancelBitfinexOrder(String orderId) throws IOException {

    try {
      bitfinex.cancelOrders(
          apiKey,
          payloadCreator,
          signatureCreator,
          new BitfinexCancelOrderRequest(
              String.valueOf(exchange.getNonceFactory().createValue()), Integer.valueOf(orderId)));
      return true;
    } catch (BitfinexException e) {
      if (e.getMessage().equals("Order could not be cancelled.")) {
        return false;
      } else {
        throw new ExchangeException(e.getMessage());
      }
    }
  }
  public BitfinexTradeResponse[] getBitfinexTradeHistory(String symbol, long timestamp, int limit)
      throws IOException {

    try {
      BitfinexTradeResponse[] trades =
          bitfinex.pastTrades(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexPastTradesRequest(
                  String.valueOf(exchange.getNonceFactory().createValue()),
                  symbol,
                  timestamp,
                  limit));
      return trades;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public BitfinexOrderStatusResponse placeBitfinexLimitOrder(
      LimitOrder limitOrder, BitfinexOrderType bitfinexOrderType, boolean hidden)
      throws IOException {

    String pair = BitfinexUtils.toPairString(limitOrder.getCurrencyPair());
    String type = limitOrder.getType().equals(Order.OrderType.BID) ? "buy" : "sell";
    String orderType = bitfinexOrderType.toString();

    BitfinexNewOrderRequest request;
    if (hidden) {
      request =
          new BitfinexNewHiddenOrderRequest(
              String.valueOf(exchange.getNonceFactory().createValue()),
              pair,
              limitOrder.getTradableAmount(),
              limitOrder.getLimitPrice(),
              "bitfinex",
              type,
              orderType);
    } else {
      request =
          new BitfinexNewOrderRequest(
              String.valueOf(exchange.getNonceFactory().createValue()),
              pair,
              limitOrder.getTradableAmount(),
              limitOrder.getLimitPrice(),
              "bitfinex",
              type,
              orderType);
    }

    try {
      BitfinexOrderStatusResponse newOrder =
          bitfinex.newOrder(apiKey, payloadCreator, signatureCreator, request);
      return newOrder;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }
  public BitfinexOfferStatusResponse placeBitfinexFloatingRateLoanOrder(
      FloatingRateLoanOrder loanOrder, BitfinexOrderType orderType) throws IOException {

    String direction = loanOrder.getType() == OrderType.BID ? "loan" : "lend";

    try {
      BitfinexOfferStatusResponse newOrderResponse =
          bitfinex.newOffer(
              apiKey,
              payloadCreator,
              signatureCreator,
              new BitfinexNewOfferRequest(
                  String.valueOf(exchange.getNonceFactory().createValue()),
                  loanOrder.getCurrency(),
                  loanOrder.getTradableAmount(),
                  new BigDecimal("0.0"),
                  loanOrder.getDayPeriod(),
                  direction));
      return newOrderResponse;
    } catch (BitfinexException e) {
      throw new ExchangeException(e.getMessage());
    }
  }