<commit_before>module PrivateApi

  class BitXError < StandardError
  end

  #List Orders
  def list_orders(pair, opt={})
    opt.merge!({pair: pair})
    path = '/api/1/listorders'
    r = authed_request(path, {params: opt})
    j = JSON.parse(r.body)

    ol = []
    if j['orders']
      j['orders'].each do |bo|
        ol << {
          completed:    bo['state'] != 'PENDING',
          state:        bo['state'],
          created_at:   Time.at(bo['creation_timestamp'].to_i/1000),
          expires_at:   Time.at(bo['expiration_timestamp'].to_i/1000),
          order_id:     bo['order_id'],
          limit_price:  BigDecimal(bo['limit_price']),
          limit_volume: BigDecimal(bo['limit_volume']),
          base:         BigDecimal(bo['base']),
          fee_base:     BigDecimal(bo['fee_base']),
          counter:      BigDecimal(bo['counter']),
          fee_counter:  BigDecimal(bo['fee_counter']),
          type:   bo['type'].to_sym
        }
      end
    end
    ol
  end

  #Post Order
  #order_type 'BID'/'ASK'
  ORDERTYPE_BID = 'BID'
  ORDERTYPE_ASK = 'ASK'
  def post_order(order_type, volume, price, pair, opt={})
    params = {
      pair: pair,
      type: order_type,
      volume: volume.to_d.round(6).to_s,
      price: price.to_d.round(6).to_s
    }
    opt.merge!({params: params, method: :post})
    path = '/api/1/postorder'
    r = authed_request(path, opt)
    j = JSON.parse(r.body)
    raise BitXError.new("BitX postorder error: #{j['error']}") if j['error']
    j
  end

  #Stop Order
  def stop_order(order_id, opt={})
    options = {params: opt.merge!({order_id: order_id}), method: :post}
    path = '/api/1/stoporder'
    r = authed_request(path, options)
    true
  end

  #Balance
  def balance_for(asset='XBT', opt={})
    opt.merge!({asset: asset})
    path = '/api/1/balance'
    r = authed_request(path, {params: opt})
    j = JSON.parse(r.body)
    raise BitXError.new("BitX balance error: #{j['error']}") if j['error']
    balance = BigDecimal(j['balance'][0]['balance'])<commit_msg>float instead of bd<commit_after>module PrivateApi

  class BitXError < StandardError
  end

  #List Orders
  def list_orders(pair, opt={})
    opt.merge!({pair: pair})
    path = '/api/1/listorders'
    r = authed_request(path, {params: opt})
    j = JSON.parse(r.body)

    ol = []
    if j['orders']
      j['orders'].each do |bo|
        ol << {
          completed:    bo['state'] != 'PENDING',
          state:        bo['state'],
          created_at:   Time.at(bo['creation_timestamp'].to_i/1000),
          expires_at:   Time.at(bo['expiration_timestamp'].to_i/1000),
          order_id:     bo['order_id'],
          limit_price:  BigDecimal(bo['limit_price']),
          limit_volume: BigDecimal(bo['limit_volume']),
          base:         BigDecimal(bo['base']),
          fee_base:     BigDecimal(bo['fee_base']),
          counter:      BigDecimal(bo['counter']),
          fee_counter:  BigDecimal(bo['fee_counter']),
          type:   bo['type'].to_sym
        }
      end
    end
    ol
  end

  #Post Order
  #order_type 'BID'/'ASK'
  ORDERTYPE_BID = 'BID'
  ORDERTYPE_ASK = 'ASK'
  def post_order(order_type, volume, price, pair, opt={})
    params = {
      pair: pair,
      type: order_type,
      volume: volume.to_f.round(6).to_s,
      price: price.to_f.round(6).to_s
    }
    opt.merge!({params: params, method: :post})
    path = '/api/1/postorder'
    r = authed_request(path, opt)
    j = JSON.parse(r.body)
    raise BitXError.new("BitX postorder error: #{j['error']}") if j['error']
    j
  end

  #Stop Order
  def stop_order(order_id, opt={})
    options = {params: opt.merge!({order_id: order_id}), method: :post}
    path = '/api/1/stoporder'
    r = authed_request(path, options)
    true
  end

  #Balance
  def balance_for(asset='XBT', opt={})
    opt.merge!({asset: asset})
    path = '/api/1/balance'
    r = authed_request(path, {params: opt})
    j = JSON.parse(r.body)
    raise BitXError.new("BitX balance error: #{j['error']}") if j['error']
    balance = BigDecimal(j['balance'][0]['balance'])
{'fraction_non_alphanumeric': 0.16290480863591755, 'fraction_numerical': 0.007360157016683023, 'mean_word_length': 2.646690518783542, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '7176494', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1432, 'n_words': 284}