class Watchcow::Rate

Public Class Methods

call(coin=nil) click to toggle source
# File lib/watchcow/rate.rb, line 6
def call coin=nil
  results = []
  coin_list = coin.nil? ? Watchcow::ApiSymbol.contract_list : [coin]
  coin_list.each do |coin|
    Watchcow::ApiSymbol.dm_types.each do |t|
      dm_symbol = {symbol: "#{coin}_#{t}"}
      spot_symbol = {symbol: "#{coin}usdt"}
      begin
        dm_result = Watchcow::MarketDepth.call(params: dm_symbol, k: :future)
        # puts dm_symbol.dig(:symbol)
        spot_result = Watchcow::MarketDepth.call(params: spot_symbol, k: :spot)
        # puts spot_symbol.dig(:symbol)
        dm_result = JSON.parse dm_result
        days_remain = self.days_remain_of(coin, t)
      rescue
        redo
      end

      dm_first_bid = dm_result.dig('tick', 'bids')[0][0].to_f
      spot_first_bid = spot_result.dig('tick', 'bids')[0][0].to_f
      margin_annual = (((dm_first_bid - spot_first_bid) / spot_first_bid) / days_remain * 365 * 100).round(6)
      margin_expect = margin_annual / 365 * days_remain
      base_gap = dm_first_bid - spot_first_bid
      gap_rate = base_gap / dm_first_bid * 100
      real_margin_expect = (1.0 / spot_first_bid - 1.0 / dm_first_bid) * spot_first_bid * 100
      result = {
        coin: "#{coin}_#{t}",
        margin: margin_annual,
        days: days_remain,
        expected_margin: margin_expect,
        real_margin_expected: real_margin_expect,
        dm_first_bid: dm_first_bid,
        spot_first_bid: spot_first_bid,
        base_gap: base_gap,
        gap_rate: gap_rate
      }
      results.push(result)
    end
  end
  results_cq = results.select{|x| x[:coin].include?('cq')}
  results_nq = results.select{|x| x[:coin].include?('nq')}
  results_cw = results.select{|x| x[:coin].include?('cw')}
  results_nw = results.select{|x| x[:coin].include?('nw')}
  {
    cq_list: results_cq.sort{|x,y| y[:expected_margin] <=> x[:expected_margin]},
    nq_list: results_nq.sort{|x,y| y[:expected_margin] <=> x[:expected_margin]},
    cw_list: results_cw.sort{|x,y| y[:expected_margin] <=> x[:expected_margin]},
    nw_list: results_nw.sort{|x,y| y[:expected_margin] <=> x[:expected_margin]}
  }
end
days_remain_of(symbol, t) click to toggle source
# File lib/watchcow/rate.rb, line 57
def days_remain_of symbol, t
  @contract_info = Watchcow::ContractInfo.call
  line = @contract_info.dig('data').select do |h|
    h.dig('symbol') == symbol.upcase && h.dig('contract_type') == Watchcow::ApiSymbol.mapping[t]
  end.first
  contract_time = "#{line.dig("delivery_date").to_date.to_s} 16:00".in_time_zone('Asia/Chongqing').to_time
  ((contract_time - Time.now.to_time) / 1.hours) / 24
end