tradecow 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 76ed1b14d98aead5821e2ffcc14f3dbce066c58fbef5e05b7a630a5c9a5c2528
4
+ data.tar.gz: 26d20d711bd7fbf8c698367547c3ab2670f819120fd1493a5a192e7458958498
5
+ SHA512:
6
+ metadata.gz: b71e94fc987add36262c9cd4f7f6da5c25787124e52e9c580c800922ec01c0505c99eeb794c0ddae1ea889ae808f48d6505259aa800bc50849fa7248d62f0698
7
+ data.tar.gz: f61cc23c7c2d154c7f050cd9a78dc90675675e243cb3e16e369f6ae2d941e8e9b3660f2ac62426aab2455fa21f0ac0830774e4a474c93d6ba287dfb2a1b748a4
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.6.3
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.6
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at drine@client.com.au. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in tradecow.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "httparty", '0.14.0'
13
+ gem 'pry-rails'
14
+ gem 'activesupport', '~> 6.1', '>= 6.1.3.2'
15
+ gem 'watchcow'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 DrinE Liu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Tradecow
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tradecow`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tradecow'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tradecow
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tradecow. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/tradecow/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Tradecow project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/tradecow/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "tradecow"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/run.rb ADDED
@@ -0,0 +1,68 @@
1
+ require './lib/tradecow'
2
+
3
+ Tradecow.configure do |config|
4
+ # l
5
+ # config.access_key = '05592582-f74a34a3-rfhfg2mkl3-d10d1'
6
+ # config.secret_key = '2c5ac248-ebe5c4a0-39239145-efa2c'
7
+
8
+ # q
9
+ # config.access_key = '73a27f61-2eaa9308-mk0lklo0de-d1da6'
10
+ # config.secret_key = '4a76c9c6-6757d921-985ca76d-88815'
11
+ end
12
+
13
+ def get_future_info coin
14
+ data = JSON.parse(Tradecow::Future.contract_position_info(coin))['data']
15
+ if data.nil? || data.empty?
16
+ puts "No #{coin} in your account."
17
+ else
18
+ data.map do |x|
19
+ puts "#{x["contract_code"]}, #{x["available"]}"
20
+ end
21
+ end
22
+ end
23
+
24
+ def close coin, volume
25
+ # Volume 张
26
+ # Tradecow::Future.contract_order 'ltc210924', 100, 'buy', 'close', 'optimal_20'
27
+ r = Tradecow::Future.contract_order coin, volume, 'buy', 'close', 'optimal_20'
28
+ puts r
29
+ end
30
+
31
+ def contract_account_info coin
32
+ JSON.parse(Tradecow::Future.contract_account_info(coin).parsed_response)["data"].first["withdraw_available"]
33
+ end
34
+
35
+ def transfer trans, amount, coin
36
+ # 盈利部分实时结算列表:
37
+ # BTC, ETH, LTC, LINK, TRX, XRP, ADA
38
+ #
39
+ # spot to future: "pro-to-futures"
40
+ # future to spot: "futures-to-pro"
41
+ # 最多8位
42
+ Tradecow::Account.transfer(trans, amount, coin)
43
+ end
44
+
45
+ def sell coin, type, amount=nil
46
+ # 即btcusdt, ethbtc...
47
+ # sell-market
48
+ Tradecow::Spot.place_order coin, type, amount
49
+ end
50
+
51
+ def run
52
+ get_future_info 'eth'
53
+ close 'ETH210625', 743
54
+ # amount = contract_account_info('eth')
55
+ transfer('futures-to-pro', contract_account_info('eth').truncate(8), 'eth')
56
+ sell('eth', 'ethusdt', 'sell-market', nil)
57
+ end
58
+ # Steps
59
+ # 1. 检查合约张数
60
+ # get_future_info 'btc'
61
+ # 2. 合约平仓buy, close
62
+ # close 'btc210924', 100
63
+ # 3. 检查合约账户余额
64
+ # contract_account_info 'btc'
65
+ # 4. 划转到币币账户
66
+ # transfer
67
+ # 5. 币币交易,卖
68
+ # sell
@@ -0,0 +1,76 @@
1
+ require 'HTTParty'
2
+
3
+ module Tradecow
4
+ module Account
5
+ def self.ids target=nil
6
+ path = "/v1/account/accounts"
7
+ req_url = Tradecow::Network.url('GET', path)
8
+ i = 0
9
+ account_list =
10
+ begin
11
+ HTTParty.get(req_url).parsed_response.dig("data")
12
+ rescue Exception => e
13
+ puts "#{e}"
14
+ i += 1
15
+ if i <= 3
16
+ puts 'Retrying'
17
+ retry
18
+ end
19
+ end
20
+ if target.nil?
21
+ account_list
22
+ else
23
+ account_list.select{|x| x["type"] == target}.first
24
+ end
25
+ end
26
+
27
+ def self.balances target
28
+ # spot
29
+ # otc
30
+ # point
31
+ # margin
32
+ account_id = ids.select{|r| r["type"] == target}.first["id"]
33
+ path = "/v1/account/accounts/#{account_id}/balance"
34
+ req_url = Tradecow::Network.url('GET', path)
35
+ i = 0
36
+ begin
37
+ HTTParty.get(req_url).parsed_response.dig("data", "list").sort_by{|x| x["balance"]}.reverse
38
+ rescue Exception => e
39
+ puts "#{e}"
40
+ i += 1
41
+ if i <= 3
42
+ puts 'Retrying'
43
+ retry
44
+ end
45
+ end
46
+ end
47
+
48
+ def self.transfer trans, amount, symbol
49
+ # spot to future: "pro-to-futures"
50
+ # future to spot: "futures-to-pro"
51
+ path = "/v1/futures/transfer"
52
+ options = {
53
+ currency: symbol.upcase,
54
+ amount: amount.to_d.truncate(8).to_s,
55
+ type: trans
56
+ }
57
+ req_url = Tradecow::Network.url('POST', path, options)
58
+ begin
59
+ r = HTTParty.post(req_url,
60
+ body: options.to_json,
61
+ headers: {"Content-Type" => "application/json"}).parsed_response
62
+ if r.dig('status') == 'error'
63
+ puts "Failed, msg: #{r.dig('err-msg')}"
64
+ elsif r.dig('status') == 'ok'
65
+ puts 'Done'
66
+ else
67
+ puts 'Unknown response: '
68
+ puts r
69
+ end
70
+ r
71
+ rescue Exception => e
72
+ puts "#{e}"
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,32 @@
1
+ require 'uri'
2
+
3
+ module Tradecow
4
+ class << self
5
+ attr_accessor :access_key, :secret_key
6
+ end
7
+
8
+ def self.configure
9
+ yield self
10
+ end
11
+
12
+ def self.req_url host, path, params
13
+ "#{host}#{path}?#{params}"
14
+ end
15
+
16
+ def self.spot_url
17
+ "api.huobi.pro"
18
+ end
19
+
20
+ def self.spot_ful_url
21
+ "https://api.huobi.pro"
22
+ end
23
+
24
+ def self.future_url
25
+ "api.hbdm.com"
26
+ end
27
+
28
+ def self.future_ful_url
29
+ "https://api.hbdm.com"
30
+ end
31
+ end
32
+
@@ -0,0 +1,87 @@
1
+
2
+ require 'HTTParty'
3
+
4
+ module Tradecow
5
+ module Future
6
+ def self.contract_order code, volume, direction, offset, order_price_type
7
+ # order_price_type
8
+ # 订单报价类型
9
+ # "limit":限价,"opponent":对手价
10
+ # "post_only":只做maker单,post only下单只受用户持仓数量限制,"optimal_5":最优5档
11
+ # "optimal_10":最优10档
12
+ # "optimal_20":最优20档
13
+ # "ioc":IOC订单
14
+ # "fok":FOK订单, "opponent_ioc": 对手价-IOC下单
15
+ # "optimal_5_ioc":最优5档-IOC下单
16
+ # "optimal_10_ioc":最优10档-IOC下单
17
+ # "optimal_20_ioc":最优20档-IOC下单,"opponent_fok": 对手价-FOK下单
18
+ # "optimal_5_fok":最优5档-FOK下单
19
+ # "optimal_10_fok":最优10档-FOK下单
20
+ # "optimal_20_fok":最优20档-FOK下单
21
+ # 开多:买入开多(direction用buy、offset用open)
22
+ # 平多:卖出平多(direction用sell、offset用close)
23
+ # 开空:卖出开空(direction用sell、offset用open)
24
+ # 平空:买入平空(direction用buy、offset用close)
25
+ # Tradecow::Future.contract_order 'ltc210924', 100, 'buy', 'close', 'optimal_20'
26
+ # Volume 张数
27
+ path = "/api/v1/contract_order"
28
+ options = {
29
+ contract_code: code.upcase,
30
+ volume: volume,
31
+ direction: direction,
32
+ offset: offset,
33
+ order_price_type: order_price_type
34
+ }
35
+ req_url = Tradecow::Network.dm_url('POST', path, options)
36
+ begin
37
+ HTTParty.post(req_url,body: options.to_json,headers: {"Content-Type" => "application/json"}).parsed_response
38
+ rescue Exception => e
39
+ puts "#{e}"
40
+ end
41
+ end
42
+
43
+ def self.contract_position_info symbol=nil
44
+ path = '/api/v1/contract_position_info'
45
+ options = symbol.nil? ? nil : {symbol: symbol.upcase}
46
+ req_url = Tradecow::Network.dm_url('POST', path, options)
47
+ i = 0
48
+ begin
49
+ r = HTTParty.post(req_url, body: options.to_json, headers: {"Content-Type" => "application/json"}).parsed_response
50
+ json_r = JSON.parse r
51
+ if json_r.dig('status') == 'error'
52
+ puts "Failed, msg: #{json_r.dig('err-msg')}"
53
+ else
54
+ puts 'Unknown response: '
55
+ end
56
+ r
57
+ rescue Exception => e
58
+ puts "#{e}"
59
+ i += 1
60
+ if i <= 3
61
+ puts 'Retrying'
62
+ retry
63
+ end
64
+ end
65
+ end
66
+
67
+ def self.contract_account_info symbol=nil
68
+ # btc, eth, ...
69
+ path = "/api/v1/contract_account_info"
70
+ options = symbol.nil? ? nil : {symbol: symbol.upcase}
71
+ req_url = Tradecow::Network.dm_url('POST', path, options)
72
+ i = 0
73
+ begin
74
+ HTTParty.post(req_url,
75
+ body: options.to_json,
76
+ headers: {"Content-Type" => "application/json"})
77
+ rescue Exception => e
78
+ puts "#{e}"
79
+ i += 1
80
+ if i <= 3
81
+ puts 'Retrying'
82
+ retry
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,54 @@
1
+ require 'uri'
2
+ require 'base64'
3
+ require 'openssl'
4
+ require 'json'
5
+ module Tradecow
6
+ module Network
7
+ SPOT_URL = Tradecow.spot_url
8
+ SPOT_FUL_URL = Tradecow.spot_ful_url
9
+ FUTURE_URL = Tradecow.future_url
10
+ FUTURE_FUL_URL = Tradecow.future_ful_url
11
+
12
+ def self.build_query(data)
13
+ data.map do |k, v|
14
+ URI.encode_www_form_component(k) << '=' << URI.encode_www_form_component(v)
15
+ end.join('&')
16
+ end
17
+
18
+ def self.hash_sort(hsh)
19
+ Hash[hsh.sort_by { |k, _| k }]
20
+ end
21
+
22
+ def self.sign(sign_data)
23
+ Base64.encode64(OpenSSL::HMAC.digest('sha256', Tradecow.secret_key, sign_data)).gsub("\n", '')
24
+ end
25
+
26
+ def self.rest_auth_token(method, host, path, req_data = nil)
27
+ params = {
28
+ 'AccessKeyId' => Tradecow.access_key,
29
+ 'SignatureMethod' => 'HmacSHA256',
30
+ 'SignatureVersion' => 2,
31
+ 'Timestamp' => Time.now.getutc.strftime('%Y-%m-%dT%H:%M:%S')
32
+ }
33
+ params = method == 'GET' && !req_data.nil? ? params.merge(req_data.stringify_keys) : params
34
+ sign_data = "#{method}\n#{host}\n#{path}\n#{build_query(hash_sort(params))}"
35
+ params = params.merge({ 'Signature' => sign(sign_data) })
36
+ params
37
+ end
38
+
39
+ def self.url method, path, req_data = nil
40
+ h = Tradecow::Network.rest_auth_token(method, SPOT_URL, path, req_data)
41
+ params = Tradecow::Network.build_query(h)
42
+ Tradecow.req_url(SPOT_FUL_URL, path, params)
43
+ end
44
+
45
+ def self.dm_url method, path, req_data = nil
46
+ h = Tradecow::Network.rest_auth_token(method, FUTURE_URL, path, req_data)
47
+ params = Tradecow::Network.build_query(h)
48
+ Tradecow.req_url(FUTURE_FUL_URL, path, params)
49
+ end
50
+ end
51
+ end
52
+ # Sign
53
+ # params = Tradecow::Network.rest_auth_token('GET', 'api.huobi.pro', '/v1/account/accounts/{account-id}/balance', 'params1' => 'value1)
54
+ # Tradecow::Network.build_query(params)
@@ -0,0 +1,64 @@
1
+ require 'HTTParty'
2
+ require 'watchcow'
3
+
4
+ module Tradecow
5
+ module Spot
6
+ def self.depth target
7
+ begin
8
+ Watchcow::MarketDepth.call(params: {symbol: target}, k: 'spot')
9
+ rescue
10
+ retry
11
+ end
12
+ end
13
+
14
+ def self.bids
15
+ begin
16
+ depth.dig("tick", "bids")
17
+ rescue
18
+ retry
19
+ end
20
+ end
21
+
22
+ def self.bids
23
+ begin
24
+ depth.dig("tick", "asks")
25
+ rescue
26
+ retry
27
+ end
28
+ end
29
+
30
+ def self.place_order coin, coin_with_unit, type, amount=nil
31
+ # 市价单,买amount是usdt,卖amount是币数量
32
+ # account-id string true NA 账户 ID,取值参考 GET /v1/account/accounts。现货交易使用 ‘spot’ 账户的 account-id;逐仓杠杆交易,请使用 ‘margin’ 账户的 account-id;全仓杠杆交易,请使用 ‘super-margin’ 账户的 account-id
33
+ # symbol string true NA 交易对,即btcusdt, ethbtc...(取值参考GET /v1/common/symbols)
34
+ # 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
35
+ # amount string true NA 订单交易量(市价买单为订单交易额)
36
+ #
37
+ # Sell
38
+ # Tradecow::Spot.place_order('btc','btcusdt','sell-market', 0.0001)
39
+ binding.pry
40
+ if amount.nil?
41
+ group = Tradecow::Account.balances('spot').select{|x| x['currency'] == coin}
42
+ trade = group.select{|x| x['type'] == 'trade'}.first['balance'].to_d rescue 0
43
+ frozen = group.select{|x| x['type'] == 'frozen'}.first['balance'].to_d rescue 0
44
+ amount = (trade - frozen).truncate(8).to_s
45
+ end
46
+ path = "/v1/order/orders/place"
47
+ options = {
48
+ "account-id" => Tradecow::Account.ids('spot')['id'],
49
+ "symbol" => coin_with_unit,
50
+ "type" => type,
51
+ "amount" => amount
52
+ }
53
+ req_url = Tradecow::Network.url('POST', path, options)
54
+ begin
55
+ r = HTTParty.post(req_url,
56
+ body: options.to_json,
57
+ headers: {"Content-Type" => "application/json"}).parsed_response
58
+ r
59
+ rescue Exception => e
60
+ puts "#{e}"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tradecow
4
+ VERSION = "0.1.4"
5
+ end
data/lib/tradecow.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'tradecow/base'
3
+ require_relative 'tradecow/network'
4
+ require_relative 'tradecow/account'
5
+ require_relative 'tradecow/spot'
6
+ require_relative 'tradecow/future'
7
+ require_relative "tradecow/version"
8
+
9
+ module Tradecow
10
+ class Error < StandardError; end
11
+ end
12
+
13
+ Tradecow.configure do |config|
14
+ config.access_key = ''
15
+ config.secret_key = ''
16
+ end
data/tradecow.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/tradecow/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tradecow"
7
+ spec.version = Tradecow::VERSION
8
+ spec.authors = ["DrinE Liu"]
9
+ spec.email = ["drine@client.com.au"]
10
+
11
+ spec.summary = "Go big"
12
+ spec.description = "Go big"
13
+ spec.homepage = "https://rubygems.org/"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ # For more information and examples about making a new gem, checkout our
32
+ # guide at: https://bundler.io/guides/creating_gem.html
33
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tradecow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - DrinE Liu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Go big
14
+ email:
15
+ - drine@client.com.au
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/run.rb
31
+ - lib/tradecow.rb
32
+ - lib/tradecow/account.rb
33
+ - lib/tradecow/base.rb
34
+ - lib/tradecow/future.rb
35
+ - lib/tradecow/network.rb
36
+ - lib/tradecow/spot.rb
37
+ - lib/tradecow/version.rb
38
+ - tradecow.gemspec
39
+ homepage: https://rubygems.org/
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://rubygems.org/
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.4.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.0.8
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Go big
63
+ test_files: []