module Tradecow::Spot

Public Class Methods

bids() click to toggle source
# File lib/tradecow/spot.rb, line 14
def self.bids
  begin
    depth.dig("tick", "bids")
  rescue
    retry
  end
end
depth(target) click to toggle source
# File lib/tradecow/spot.rb, line 6
def self.depth target
  begin
    Watchcow::MarketDepth.call(params: {symbol: target}, k: 'spot')
  rescue
    retry
  end
end
place_order(coin, coin_with_unit, type, amount=nil) click to toggle source
# File lib/tradecow/spot.rb, line 30
def self.place_order coin, coin_with_unit, type, amount=nil
  # 市价单,买amount是usdt,卖amount是币数量
  # account-id      string       true  NA       账户 ID,取值参考 GET /v1/account/accounts。现货交易使用 ‘spot’ 账户的 account-id;逐仓杠杆交易,请使用 ‘margin’ 账户的 account-id;全仓杠杆交易,请使用 ‘super-margin’ 账户的 account-id
  # symbol  string   true      NA   交易对,即btcusdt, ethbtc...(取值参考GET /v1/common/symbols)
  # type    string     true        NA     订单类型,包括buy-market, sell-market, buy-limit, sell-limit, buy-ioc, sell-ioc, buy-limit-maker, sell-limit-maker(说明见下文), buy-stop-limit, sell-stop-limit, buy-limit-fok, sell-limit-fok, buy-stop-limit-fok, sell-stop-limit-fok
  # amount  string   true      NA   订单交易量(市价买单为订单交易额)
  #
  # Sell
  # Tradecow::Spot.place_order('btc','btcusdt','sell-market', 0.0001)
  binding.pry
  if amount.nil?
    group = Tradecow::Account.balances('spot').select{|x| x['currency'] == coin}
    trade = group.select{|x| x['type'] == 'trade'}.first['balance'].to_d rescue 0
    frozen = group.select{|x| x['type'] == 'frozen'}.first['balance'].to_d rescue 0
    amount = (trade - frozen).truncate(8).to_s
  end
  path = "/v1/order/orders/place"
  options = {
    "account-id" => Tradecow::Account.ids('spot')['id'],
    "symbol" => coin_with_unit,
    "type" => type,
    "amount" => amount
  }
  req_url = Tradecow::Network.url('POST', path, options)
  begin
    r = HTTParty.post(req_url,
      body: options.to_json,
      headers: {"Content-Type" => "application/json"}).parsed_response
    r
  rescue Exception => e
    puts "#{e}"
  end
end