<commit_before>package org.knowm.xchange.livecoin.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.livecoin.Livecoin;
import org.knowm.xchange.livecoin.LivecoinAdapters;
import org.knowm.xchange.livecoin.LivecoinExchange;
import org.knowm.xchange.livecoin.dto.LivecoinPaginatedResponse;
import org.knowm.xchange.livecoin.dto.trade.LivecoinCancelResponse;
import org.knowm.xchange.livecoin.dto.trade.LivecoinOrderResponse;
import org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair;
import org.knowm.xchange.service.trade.params.CancelOrderByIdParams;
import org.knowm.xchange.service.trade.params.CancelOrderParams;
import org.knowm.xchange.utils.DateUtils;

public class LivecoinTradeServiceRaw extends LivecoinBaseService<Livecoin> {

  public LivecoinTradeServiceRaw(LivecoinExchange exchange) {
    super(Livecoin.class, exchange);
  }

  public List<LimitOrder> getAllOpenOrders() throws IOException {
    LivecoinPaginatedResponse response = service.allClientOrders(apiKey, signatureCreator, "OPEN");

    List<LimitOrder> resp = new ArrayList<>();
    if (response.getData() == null) return resp;

    for (Map map : response.getData()) {
      Object statusRaw = map.get("orderStatus");
      if (statusRaw != null
          && (statusRaw.toString().equals("OPEN")
              || statusRaw.toString().equals("PARTIALLY_FILLED"))) {
        resp.add(LivecoinAdapters.adaptOpenOrder(map));
      }
    }
    return resp;
  }

  public List<UserTrade> tradeHistory(Date start, Date end, Integer limit, Long offset)
      throws IOException {
    List<Map> response =
        service.transactions(
            apiKey,
            signatureCreator,
            String.valueOf(DateUtils.toMillisNullSafe(start)),
            String.valueOf(DateUtils.toMillisNullSafe(end)),
            "BUY,SELL",
            limit,
            offset);

    List<UserTrade> resp = new ArrayList<>();
    for (Map map : response) {
      UserTrade fundingRecord = LivecoinAdapters.adaptUserTrade(map);

      resp.add(fundingRecord);
    }

    return resp;
  }

  public String makeMarketOrder(MarketOrder order) throws IOException {
    LivecoinOrderResponse response;
    if (order.getType().equals(Order.OrderType.BID)) {
      response =
          service.buyWithMarketOrder(
              apiKey,
              signatureCreator,
              order.getCurrencyPair().toString(),
              order.getOriginalAmount());
    } else {
      response =<commit_msg>Livecoin allows a maximum of 30 days history<commit_after>package org.knowm.xchange.livecoin.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.UserTrade;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.livecoin.Livecoin;
import org.knowm.xchange.livecoin.LivecoinAdapters;
import org.knowm.xchange.livecoin.LivecoinExchange;
import org.knowm.xchange.livecoin.dto.LivecoinPaginatedResponse;
import org.knowm.xchange.livecoin.dto.trade.LivecoinCancelResponse;
import org.knowm.xchange.livecoin.dto.trade.LivecoinOrderResponse;
import org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair;
import org.knowm.xchange.service.trade.params.CancelOrderByIdParams;
import org.knowm.xchange.service.trade.params.CancelOrderParams;
import org.knowm.xchange.utils.DateUtils;

public class LivecoinTradeServiceRaw extends LivecoinBaseService<Livecoin> {

  private static final long THIRTY_DAYS_MILLISECONDS = 2_592_000_000L;

  public LivecoinTradeServiceRaw(LivecoinExchange exchange) {
    super(Livecoin.class, exchange);
  }

  public List<LimitOrder> getAllOpenOrders() throws IOException {
    LivecoinPaginatedResponse response = service.allClientOrders(apiKey, signatureCreator, "OPEN");

    List<LimitOrder> resp = new ArrayList<>();
    if (response.getData() == null) return resp;

    for (Map map : response.getData()) {
      Object statusRaw = map.get("orderStatus");
      if (statusRaw != null
          && (statusRaw.toString().equals("OPEN")
          || statusRaw.toString().equals("PARTIALLY_FILLED"))) {
        resp.add(LivecoinAdapters.adaptOpenOrder(map));
      }
    }
    return resp;
  }

  public List<UserTrade> tradeHistory(Date startTime, Date endTime, Integer limit, Long offset)
      throws IOException {
    long end = DateUtils.toMillisNullSafe(endTime);
    // Livecoin API limitation: 30 days max period
    long start = Math.max(DateUtils.toMillisNullSafe(startTime), end - THIRTY_DAYS_MILLISECONDS);

    List<Map> response =
        service.transactions(
            apiKey,
            signatureCreator,
            String.valueOf(start),
            String.valueOf(end),
            "BUY,SELL",
            limit,
            offset);

    List<UserTrade> resp = new ArrayList<>();
    for (Map map : response) {
      UserTrade fundingRecord = LivecoinAdapters.adaptUserTrade(map);

      resp.add(fundingRecord);
    }

    return resp;
  }

  public String makeMarketOrder(MarketOrder order) throws IOException {
    LivecoinOrderResponse response;
    if (order.getType().equals(Order.OrderType.BID)) {
      response =
          service.buyWithMarketOrder(
              apiKey,
              signatureCreator,
              order.getCurrencyPair().toString(),
              order.getOriginalAmount());
    } else {
      response =
{'fraction_non_alphanumeric': 0.10484006029140848, 'fraction_numerical': 0.0023446658851113715, 'mean_word_length': 3.6802507836990594, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3528470', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1783, 'n_words': 312}