<commit_before>#include "gdax.h"
#include "parameters.h"
#include "utils/restapi.h"
#include "unique_json.hpp"
#include "utils/base64.h"
#include "openssl/sha.h"
#include "openssl/hmac.h"
#include <iomanip>
#include <vector>
#include <array>
#include <ctime>

namespace GDAX {

static RestApi& queryHandle(Parameters &params)
{
  static RestApi query ("https://api.gdax.com",
                        params.cacert.c_str(), *params.logFile);
  return query;
}

quote_t getQuote(Parameters &params)
{
  auto &exchange = queryHandle(params);
  std::string pair;
  pair = "/products/";
  pair += params.leg1.c_str();
  pair += "-";
  pair += params.leg2.c_str();
  pair += "/ticker";
  unique_json root { exchange.getRequest(pair) };
  const char *bid, *ask;
  int unpack_fail = json_unpack(root.get(), "{s:s, s:s}", "bid", &bid, "ask", &ask);
  if (unpack_fail)
  {
    bid = "0";
    ask = "0";
  }

  return std::make_pair(std::stod(bid), std::stod(ask));
}

double getAvail(Parameters &params, std::string currency)
{
  unique_json root { authRequest(params, "GET", "/accounts", "") };
  size_t arraySize = json_array_size(root.get());
  double available = 0.0;
  const char* currstr;
  for (size_t i = 0; i < arraySize; i++) {
    std::string tmpCurrency = json_string_value(json_object_get(json_array_get(root.get(), i), "currency"));
    if (tmpCurrency.compare(currency.c_str()) == 0) {
      currstr = json_string_value(json_object_get(json_array_get(root.get(), i), "available"));
      if (currstr != NULL){
        available = atof(currstr);
      } else {
        *params.logFile << "<GDAX> Error with currency string" << std::endl;
        available = 0.0;
      }
    }
  }
  return available;
}

double getActivePos(Parameters &params)
{
  // TODO: this is not really a good way to get active positions
  getAvail(params,"BTC");
}

double getLimitPrice(Parameters &params, double volume, bool isBid)
{
  auto &exchange  = queryHandle(params);
  // TODO: Build a real URL with leg1 leg2 and auth post it
  // FIXME: using level 2 order book - has aggregated data but should be sufficient for now.
  unique_json root { exchange.getRequest("/products/BTC-USD/book?level=2") };
  auto bidask  = json_object_get(root.get(),isBid ? "bids" : "asks");
  *params.logFile << "<GDAX> Looking for a limit price to fill "
                  << std::setprecision(8) << fabs(volume) << " Legx...\n";
  double tmpVol = 0.0;<commit_msg>missed a return in gdax.cpp<commit_after>#include "gdax.h"
#include "parameters.h"
#include "utils/restapi.h"
#include "unique_json.hpp"
#include "utils/base64.h"
#include "openssl/sha.h"
#include "openssl/hmac.h"
#include <iomanip>
#include <vector>
#include <array>
#include <ctime>

namespace GDAX {

static RestApi& queryHandle(Parameters &params)
{
  static RestApi query ("https://api.gdax.com",
                        params.cacert.c_str(), *params.logFile);
  return query;
}

quote_t getQuote(Parameters &params)
{
  auto &exchange = queryHandle(params);
  std::string pair;
  pair = "/products/";
  pair += params.leg1.c_str();
  pair += "-";
  pair += params.leg2.c_str();
  pair += "/ticker";
  unique_json root { exchange.getRequest(pair) };
  const char *bid, *ask;
  int unpack_fail = json_unpack(root.get(), "{s:s, s:s}", "bid", &bid, "ask", &ask);
  if (unpack_fail)
  {
    bid = "0";
    ask = "0";
  }

  return std::make_pair(std::stod(bid), std::stod(ask));
}

double getAvail(Parameters &params, std::string currency)
{
  unique_json root { authRequest(params, "GET", "/accounts", "") };
  size_t arraySize = json_array_size(root.get());
  double available = 0.0;
  const char* currstr;
  for (size_t i = 0; i < arraySize; i++) {
    std::string tmpCurrency = json_string_value(json_object_get(json_array_get(root.get(), i), "currency"));
    if (tmpCurrency.compare(currency.c_str()) == 0) {
      currstr = json_string_value(json_object_get(json_array_get(root.get(), i), "available"));
      if (currstr != NULL){
        available = atof(currstr);
      } else {
        *params.logFile << "<GDAX> Error with currency string" << std::endl;
        available = 0.0;
      }
    }
  }
  return available;
}

double getActivePos(Parameters &params)
{
  // TODO: this is not really a good way to get active positions
  return getAvail(params,"BTC");
}

double getLimitPrice(Parameters &params, double volume, bool isBid)
{
  auto &exchange  = queryHandle(params);
  // TODO: Build a real URL with leg1 leg2 and auth post it
  // FIXME: using level 2 order book - has aggregated data but should be sufficient for now.
  unique_json root { exchange.getRequest("/products/BTC-USD/book?level=2") };
  auto bidask  = json_object_get(root.get(),isBid ? "bids" : "asks");
  *params.logFile << "<GDAX> Looking for a limit price to fill "
                  << std::setprecision(8) << fabs(volume) << " Legx...\n";
  double tmpVol = 0.0;
{'fraction_non_alphanumeric': 0.15918869084204057, 'fraction_numerical': 0.007785289899610736, 'mean_word_length': 3.7444120505344993, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '6163999', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1689, 'n_words': 422}