watchcow 0.1.12 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9278d3d386832cfad1db4406ee6c5b0343c2dd096135aaedd9a4cf98c6ff59cd
4
- data.tar.gz: c098df95b942ed5ba925f5af509f3845378d79aca20103d6d11d7830711f3439
3
+ metadata.gz: 1df0dc2cf2e5b29f6a7823afbfb19bf5f5d457cf9b81f06d289130cdf15e4eb2
4
+ data.tar.gz: 4041b2e5e30678b67db33958b6c86c7399ba01018cef6f5c364899976a9f1789
5
5
  SHA512:
6
- metadata.gz: fd615fc5b8559d39e6f05d5f3f29da5742b0b6e5e25398b2446557e7e8bfc8f37708fafde6cc6ec743b885a27171eebbff077386db05ff0183dec45e8203fa89
7
- data.tar.gz: 6d93a1703a8bc79de7e84ba126de8e39ddb435780b5a9d4f937b755dd17ed91d707de013d6ca5e1eb2d55ecb18136f02e3534b28166a6c9758b12a7d49620580
6
+ metadata.gz: 6e59700b75480c9abb161c0a495f51461918d0e574b531e77f48c3c31bb480836b9356cc08dd7b2c494ee4dcfeb4788c1887a1fcb496bdb42da4176fbfd2c31b
7
+ data.tar.gz: bcf6f472b21b27f9ea62a029cad0e38a4d6a9953ccb31a5837686ace18b974bb4da19823a1fb7f36857a9dea738ea25216e276fdfe9e53d3d6322a556d14dae1
@@ -1,20 +1,6 @@
1
1
  module Watchcow
2
2
  module ApiSymbol
3
3
  class << self
4
- def contract_list
5
- %w{btc eth link dot eos trx ada ltc bch xrp bsv etc fil}
6
- end
7
-
8
- def dm_types
9
- data = mapping.select do |k, v|
10
- v.in? current_types
11
- end.keys
12
- end
13
-
14
- def current_types
15
- Watchcow::ContractInfo.current_contracts
16
- end
17
-
18
4
  def mapping
19
5
  {
20
6
  'cq' => 'quarter',
@@ -9,7 +9,10 @@ module Watchcow
9
9
  end
10
10
 
11
11
  def current_contracts
12
- call.dig('data').map{|x| x["contract_type"]}.uniq
12
+ types = call.dig('data').group_by{|x| x["symbol"]}
13
+ types.map do |k, v|
14
+ { k=> v.map{|x| Watchcow::ApiSymbol.mapping.invert[x["contract_type"]]}}
15
+ end.inject(&:merge)
13
16
  end
14
17
  end
15
18
  end
data/lib/watchcow/rate.rb CHANGED
@@ -5,40 +5,22 @@ module Watchcow
5
5
  class << self
6
6
  def call coin=nil
7
7
  results = []
8
- coin_list = coin.nil? ? Watchcow::ApiSymbol.contract_list : [coin]
9
- coin_list.each do |coin|
10
- Watchcow::ApiSymbol.dm_types.each do |t|
8
+ coin_list = coin.nil? ? Watchcow::ContractInfo.current_contracts : Watchcow::ContractInfo.current_contracts.select{|k, v| k == coin.upcase}
9
+ coin_list.each do |coin, contract_types|
10
+ contract_types.each do |t|
11
11
  dm_symbol = {symbol: "#{coin}_#{t}"}
12
- spot_symbol = {symbol: "#{coin}usdt"}
12
+ spot_symbol = {symbol: "#{coin.downcase}usdt"}
13
13
  begin
14
14
  dm_result = Watchcow::MarketDepth.call(params: dm_symbol, k: :future)
15
- # puts dm_symbol.dig(:symbol)
16
15
  spot_result = Watchcow::MarketDepth.call(params: spot_symbol, k: :spot)
17
- # puts spot_symbol.dig(:symbol)
18
16
  dm_result = JSON.parse dm_result
19
- days_remain = self.days_remain_of(coin, t)
17
+ days_remain =
18
+ n = 1
20
19
  rescue
21
- redo
20
+ n += 1
21
+ redo if n <= 3
22
22
  end
23
-
24
- dm_first_bid = dm_result.dig('tick', 'bids')[0][0].to_f
25
- spot_first_bid = spot_result.dig('tick', 'bids')[0][0].to_f
26
- margin_annual = (((dm_first_bid - spot_first_bid) / spot_first_bid) / days_remain * 365 * 100).round(6)
27
- margin_expect = margin_annual / 365 * days_remain
28
- base_gap = dm_first_bid - spot_first_bid
29
- gap_rate = base_gap / dm_first_bid * 100
30
- real_margin_expect = (1.0 / spot_first_bid - 1.0 / dm_first_bid) * spot_first_bid * 100
31
- result = {
32
- coin: "#{coin}_#{t}",
33
- margin: margin_annual,
34
- days: days_remain,
35
- expected_margin: margin_expect,
36
- real_margin_expected: real_margin_expect,
37
- dm_first_bid: dm_first_bid,
38
- spot_first_bid: spot_first_bid,
39
- base_gap: base_gap,
40
- gap_rate: gap_rate
41
- }
23
+ result = cals dm_result, spot_result, coin, t
42
24
  results.push(result)
43
25
  end
44
26
  end
@@ -54,14 +36,38 @@ module Watchcow
54
36
  }
55
37
  end
56
38
 
39
+ private
40
+
57
41
  def days_remain_of symbol, t
58
- @contract_info = Watchcow::ContractInfo.call
59
- line = @contract_info.dig('data').select do |h|
42
+ contract_info = Watchcow::ContractInfo.call
43
+ line = contract_info.dig('data').select do |h|
60
44
  h.dig('symbol') == symbol.upcase && h.dig('contract_type') == Watchcow::ApiSymbol.mapping[t]
61
45
  end.first
62
46
  contract_time = "#{line.dig("delivery_date").to_date.to_s} 16:00".in_time_zone('Asia/Chongqing').to_time
63
47
  ((contract_time - Time.now.to_time) / 1.hours) / 24
64
48
  end
49
+
50
+ def cals dm_result, spot_result, coin, t
51
+ days_remain = days_remain_of(coin, t)
52
+ dm_first_bid = dm_result.dig('tick', 'bids')[0][0].to_f
53
+ spot_first_bid = spot_result.dig('tick', 'bids')[0][0].to_f
54
+ margin_annual = (((dm_first_bid - spot_first_bid) / spot_first_bid) / days_remain * 365 * 100).round(6)
55
+ margin_expect = margin_annual / 365 * days_remain
56
+ base_gap = dm_first_bid - spot_first_bid
57
+ gap_rate = base_gap / dm_first_bid * 100
58
+ real_margin_expect = (1.0 / spot_first_bid - 1.0 / dm_first_bid) * spot_first_bid * 100
59
+ result = {
60
+ coin: "#{coin}_#{t}",
61
+ margin: margin_annual,
62
+ days: days_remain,
63
+ expected_margin: margin_expect,
64
+ real_margin_expected: real_margin_expect,
65
+ dm_first_bid: dm_first_bid,
66
+ spot_first_bid: spot_first_bid,
67
+ base_gap: base_gap,
68
+ gap_rate: gap_rate
69
+ }
70
+ end
65
71
  end
66
72
  end
67
73
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Watchcow
4
- VERSION = "0.1.12"
4
+ VERSION = "0.2.12"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watchcow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - DrinE Liu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-11 00:00:00.000000000 Z
11
+ date: 2021-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty