stock_fighter 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3aec0e9fae963679c7a6dc7d578c71a8266bbd0a
4
+ data.tar.gz: fdecba1ef331c5ed7fa538f34e840bbfa1bd62cb
5
+ SHA512:
6
+ metadata.gz: 498eff0bd4d0664dc2d097253b67f7a71fafeee59c46b163ec8c3c7a1f3c7a26c8cc4e9ca0a648685970bfb9936b70c752dfbdc7a79c1dbf887f4c5c2365ed05
7
+ data.tar.gz: a097f519b34b8cd8aab02eeb4151ed2cdba39855fc1bf63f1bc608a03cbcdd68d8d7c3d0725e547b3a609c8303d64ec152e389c309c44fbee51de1cb2abdc938
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stock_fighter.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jing Yang
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,76 @@
1
+ # StockFighter
2
+
3
+ An (unofficial) API for [stockfighter.io](http://stockfighter.io). Designed to be friendly to scripting. Run `bin/console` and start trading!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'stock_fighter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install stock_fighter
20
+
21
+ ## Usage for Scripting
22
+
23
+ See the following script. The code can also be found in `examples/test_exchange.rb`. Detailed API is listed in `lib/stock_fighter/api_mixin`.
24
+
25
+ ```ruby
26
+ require 'stock_fighter/api_mixin'
27
+
28
+ include StockFighter::ApiMixin
29
+
30
+ # Set your API key.
31
+ set_api_key "your_api_key_here"
32
+
33
+ venue = "TESTEX"
34
+ stock = "FOOBAR"
35
+ account = "EXB123456"
36
+
37
+ # Test if the service is up.
38
+ ok = send_heartbeat.parsed_response["ok"] rescue false
39
+
40
+ raise "Can't sent heart beat" unless ok
41
+
42
+ # Get the current orderbook.
43
+ orderbook = get_orderbook venue, stock
44
+
45
+ raise "Can't get orderbook" unless orderbook["ok"]
46
+
47
+ p "bids: ", orderbook["bids"]
48
+ p "asks: ", orderbook["asks"]
49
+
50
+ # Place an order!
51
+ # By default the direction is 'buy' and order type is 'limit'.
52
+ order = place_order venue, stock, account, price: 100, qty: 1, direction: :buy, order_type: :limit
53
+
54
+ # There should be an error unless a valid API key is supplied.
55
+ p order
56
+ ```
57
+
58
+ As a side effect, all objects will have extra methods :-).
59
+
60
+ TODO: add examples for more serious coding style.
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+
66
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stock_fighter.
71
+
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "stock_fighter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ include StockFighter::ApiMixin
14
+
15
+ puts """Welcome to the StockFighter Console!
16
+
17
+ Please start by setting your API key:
18
+
19
+ set_api_key 'your_api_key_here'
20
+
21
+ Supported commands:
22
+
23
+ send_heartbeat
24
+ send_venue_heartbeat venue
25
+
26
+ list_stocks venue
27
+ get_orderbook venue, stock
28
+
29
+ place_order venue, stock, account, price:, qty:, direction: 'buy', order_type: 'limit'
30
+ show_order venue, stock, order
31
+ cancel_order venue, stock, order
32
+
33
+ get_quote venue, stock
34
+
35
+ list_account_orders venue, account
36
+ list_account_stock_orders venue, account, stock
37
+
38
+ See http://stockfighter.io for API details.
39
+
40
+ Other commands
41
+ http : A httparty object to send http request, usage: http.get, http.post etc.
42
+ """
43
+
44
+ require "irb"
45
+ IRB.start
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
@@ -0,0 +1,30 @@
1
+ require 'stock_fighter/api_mixin'
2
+
3
+ include StockFighter::ApiMixin
4
+
5
+ # Set your API key.
6
+ set_api_key "your_api_key_here"
7
+
8
+ venue = "TESTEX"
9
+ stock = "FOOBAR"
10
+ account = "EXB123456"
11
+
12
+ # Test if the service is up.
13
+ ok = send_heartbeat.parsed_response["ok"] rescue false
14
+
15
+ raise "Can't sent heart beat" unless ok
16
+
17
+ # Get the current orderbook.
18
+ orderbook = get_orderbook venue, stock
19
+
20
+ raise "Can't get orderbook" unless orderbook["ok"]
21
+
22
+ p "bids: ", orderbook["bids"]
23
+ p "asks: ", orderbook["asks"]
24
+
25
+ # Place an order!
26
+ # By default the direction is 'buy' and order type is 'limit'.
27
+ order = place_order venue, stock, account, price: 100, qty: 1, direction: :buy, order_type: :limit
28
+
29
+ # There should be an error unless a valid API key is supplied.
30
+ p order
@@ -0,0 +1,13 @@
1
+ module StockFighter
2
+ class Api
3
+ include ApiMixin
4
+
5
+ def initialize api_key
6
+ self.set_api_key api_key
7
+ end
8
+
9
+ def connect_to_level level_name
10
+ Level.new level_name, self
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,86 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ # TODO: better testing.
5
+ module StockFighter
6
+ module ApiMixin
7
+ def self.included base
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ def set_api_key api_key
12
+ http.headers "X-Starfighter-Authorization" => api_key
13
+ end
14
+
15
+ def send_heartbeat
16
+ http.get "/heartbeat"
17
+ end
18
+
19
+ def send_venue_heartbeat venue
20
+ http.get "/venues/#{venue}/heartbeat"
21
+ end
22
+
23
+ def list_stocks venue
24
+ http.get "/venues/#{venue}/stocks"
25
+ end
26
+
27
+ def get_orderbook venue, stock
28
+ http.get "/venues/#{venue}/stocks/#{stock}"
29
+ end
30
+
31
+ def place_order venue, stock, account, price:, qty:, direction: "buy", order_type: "limit"
32
+ params = method(__method__).parameters.map(&:last)
33
+ body = params.map { |p| [p, eval(p.to_s)] }.to_h
34
+
35
+ body[:orderType] = body.delete(:order_type)
36
+
37
+ http.post "/venues/#{venue}/stocks/#{stock}/orders", body: JSON.dump(body)
38
+ end
39
+
40
+ def show_order venue, stock, order
41
+ http.post "/venues/#{venue}/stocks/#{stock}/orders/#{order}"
42
+ end
43
+
44
+ def cancel_order venue, stock, order
45
+ http.delete "/venues/#{venue}/stocks/#{stock}/orders/#{order}"
46
+ end
47
+
48
+ def get_quote venue, stock
49
+ http.get "/venues/#{venue}/stocks/#{stock}/quote"
50
+ end
51
+
52
+ def list_account_orders venue, account
53
+ http.get "/venues/#{venue}/accounts/#{account}/orders"
54
+ end
55
+
56
+ def list_account_stock_orders venue, account, stock
57
+ http.get "get/venues/#{venue}/accounts/#{account}/stocks/#{stock}/orders"
58
+ end
59
+
60
+ private
61
+ def http
62
+ # TODO: how do we create module specific vars?
63
+ @http_delegator ||= ApiMixin.create_partified_module
64
+ end
65
+
66
+ def self.create_partified_module
67
+ Module.new do
68
+ include HTTParty
69
+
70
+ base_uri 'https://api.stockfighter.io/ob/api'
71
+ format :json
72
+ end
73
+ end
74
+
75
+ module ClassMethods
76
+ private
77
+ def share_http_delegator
78
+ http_delegator = ApiMixin.create_partified_module
79
+ define_method :http do
80
+ http_delegator
81
+ end
82
+ private :http
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,30 @@
1
+ require 'stock_fighter/api_mixin'
2
+
3
+ module StockFighter
4
+ class Level
5
+ def initialize level_name, api = nil, api_key: nil, account: nil, venue: nil, stock: nil
6
+ raise "Must provide an API key, or an Api instance." if api.nil? and api_key.nil?
7
+
8
+ @name = level_name
9
+ @delegator = api or StockFighter.create_api api_key
10
+
11
+ @defaults = {}
12
+ @defaults[:account] = account unless account.nil?
13
+ @defaults[:venue] = venue unless venue.nil?
14
+ @defaults[:stock] = stock unless stock.nil?
15
+ end
16
+
17
+ ApiMixin.instance_methods.each do |method|
18
+ required_params = ApiMixin.instance_method(method).parameters.select do |pair|
19
+ pair.first == :req
20
+ end.map(&:last)
21
+
22
+ define_method method do |*args, &block|
23
+ prefilled = required_params.map do |param|
24
+ @defaults[param]
25
+ end
26
+ @delegator.send method, *(prefilled + args), &block
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module StockFighter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "stock_fighter/version"
2
+ require "stock_fighter/level"
3
+ require "stock_fighter/api"
4
+
5
+ module StockFighter
6
+ def self.create_api api_key
7
+ Api.new api_key
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stock_fighter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stock_fighter"
8
+ spec.version = StockFighter::VERSION
9
+ spec.authors = ["Jing Yang"]
10
+ spec.email = ["ditsing@gmail.com"]
11
+
12
+ spec.summary = %q{Better stockfighter.io API, friendly to scripting.}
13
+ spec.description = %q{API for http://stockfighter.io. Run bin/console and start playing!}
14
+ spec.homepage = "http://github.com/ditsing/stock_fighter"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "httparty", "~> 0.13.7"
23
+ spec.add_runtime_dependency "json", "~> 1.8.3"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stock_fighter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jing Yang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: API for http://stockfighter.io. Run bin/console and start playing!
84
+ email:
85
+ - ditsing@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - MIT-LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - examples/test_exchange.rb
99
+ - lib/stock_fighter.rb
100
+ - lib/stock_fighter/api.rb
101
+ - lib/stock_fighter/api_mixin.rb
102
+ - lib/stock_fighter/level.rb
103
+ - lib/stock_fighter/version.rb
104
+ - stock_fighter.gemspec
105
+ homepage: http://github.com/ditsing/stock_fighter
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.5.1
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Better stockfighter.io API, friendly to scripting.
129
+ test_files: []