sb-alpaca 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +7 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sb/alpaca.rb +132 -0
- data/lib/sb/alpaca/version.rb +5 -0
- data/sb-alpaca.gemspec +34 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 97a58fefaa4264b0596d55a11c8071efcec648f52a36245e2372f5fa53016e26
|
4
|
+
data.tar.gz: 0fc07d8210f8f1dd0ded350e17c9d6eb6c865c103f92b38c6d170a25e51ce6ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6858742bc5d392e050823ffe4753dbd556ddcc3e14ba07f3a00ad004864954d1304d872feb70b2fd7e00d5d317aaa536f451a5575a7d4dce16a2affe9fe3c22
|
7
|
+
data.tar.gz: c0ecb25aa85e1bc0104a614375b3185405cd0f3f7dbfb6fc3de48d73c91a15bdeeea828fe6ef4c0925498ba8e0fd4e92c6f0e36fc749d16f904348df0811e20d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Sb::Alpaca
|
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/sb/alpaca`. 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 'sb-alpaca'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sb-alpaca
|
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. 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 tags, 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]/sb-alpaca.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sb/alpaca"
|
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
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/sb/alpaca.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'sb/config'
|
2
|
+
require 'faye/websocket'
|
3
|
+
require 'eventmachine'
|
4
|
+
require 'json'
|
5
|
+
require "sb/alpaca/version"
|
6
|
+
|
7
|
+
module Sb
|
8
|
+
module Alpaca
|
9
|
+
ACTIONS = {
|
10
|
+
auth: ->(key_id,secret_key) {
|
11
|
+
{
|
12
|
+
"action": "authenticate",
|
13
|
+
"data": {
|
14
|
+
"key_id": key_id,
|
15
|
+
"secret_key": secret_key
|
16
|
+
}
|
17
|
+
}.to_json
|
18
|
+
},
|
19
|
+
listen: ->(listen_list) {
|
20
|
+
{
|
21
|
+
"action": "listen",
|
22
|
+
"data": {
|
23
|
+
"streams": listen_list
|
24
|
+
}
|
25
|
+
}.to_json
|
26
|
+
},
|
27
|
+
unlisten: ->(unlisten_list) {
|
28
|
+
{
|
29
|
+
"action": "unlisten",
|
30
|
+
"data": {
|
31
|
+
"streams": unlisten_list
|
32
|
+
}
|
33
|
+
}.to_json
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
class Error < StandardError; end
|
38
|
+
# Your code goes here...
|
39
|
+
|
40
|
+
class Stream
|
41
|
+
EP = {
|
42
|
+
paper_api: 'wss://paper-api.alpaca.markets/stream',
|
43
|
+
api: 'wss://api.alpaca.markets/stream',
|
44
|
+
data: 'wss://data.alpaca.markets/stream'
|
45
|
+
}
|
46
|
+
|
47
|
+
TYPES = [:api, :paper_api, :data]
|
48
|
+
def initialize type=:data, paper=true
|
49
|
+
if TYPES.include? type
|
50
|
+
@ready = true
|
51
|
+
@type = type
|
52
|
+
@state = nil
|
53
|
+
|
54
|
+
@stream_url = EP[type]
|
55
|
+
@subscriptions = {@type => []}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def subscribe &blk
|
60
|
+
@subscriptions[@type] << blk
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def listen list
|
65
|
+
puts "listening to #{list}"
|
66
|
+
@ws.send ACTIONS[:listen].call(list)
|
67
|
+
end
|
68
|
+
|
69
|
+
def unlisten list
|
70
|
+
puts "unlistening to #{list}"
|
71
|
+
@ws.send ACTIONS[:unlisten].call(list)
|
72
|
+
end
|
73
|
+
|
74
|
+
def open
|
75
|
+
return unless @ready
|
76
|
+
|
77
|
+
start_listening
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
def start_listening
|
82
|
+
@th = Thread.new do
|
83
|
+
EM.run {
|
84
|
+
@ws = Faye::WebSocket::Client.new(@stream_url)
|
85
|
+
|
86
|
+
@ws.on :open do |event|
|
87
|
+
p [:open]
|
88
|
+
auth_action = ACTIONS[:auth].call(Sb::Config['ALPACA']['KEY_ID'], Sb::Config['ALPACA']['SECRET_KEY'])
|
89
|
+
@ws.send(auth_action)
|
90
|
+
end
|
91
|
+
|
92
|
+
@ws.on :message do |event|
|
93
|
+
#p [:message, event.data]
|
94
|
+
|
95
|
+
if event.data.is_a? Array
|
96
|
+
p_data = event.data.inject('') {|l,e| l+ e.chr}
|
97
|
+
else
|
98
|
+
p_data = event.data
|
99
|
+
end
|
100
|
+
|
101
|
+
e_data = JSON.parse p_data
|
102
|
+
data = e_data['data']
|
103
|
+
|
104
|
+
if data['status'] == 'authorized'
|
105
|
+
@state = 'authorized'
|
106
|
+
elsif e_data['stream'] == 'trade_updates' || e_data['stream'] == 'account_updates'
|
107
|
+
@subscriptions[@type].each {|s| s.call(data) }
|
108
|
+
elsif data['ev']
|
109
|
+
@subscriptions[@type].each {|s| s.call(data) }
|
110
|
+
end
|
111
|
+
|
112
|
+
#p [:message, p_data]
|
113
|
+
end
|
114
|
+
|
115
|
+
@ws.on :close do |event|
|
116
|
+
p [:close, event.data]
|
117
|
+
@ws = nil
|
118
|
+
end
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
until @state == 'authorized'
|
124
|
+
puts 'alpaca websocket: waiting for authorization'
|
125
|
+
sleep 1
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/sb-alpaca.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "sb/alpaca/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "sb-alpaca"
|
7
|
+
spec.version = Sb::Alpaca::VERSION
|
8
|
+
spec.authors = ["iodevel"]
|
9
|
+
spec.email = ["cbrandt92@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = '...'
|
12
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
13
|
+
# spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
14
|
+
|
15
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
19
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_runtime_dependency "sb-config", "~> 0.4.0"
|
33
|
+
spec.add_runtime_dependency "faye-websocket"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sb-alpaca
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- iodevel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sb-config
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.4.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.4.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faye-websocket
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- cbrandt92@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- bin/console
|
81
|
+
- bin/setup
|
82
|
+
- lib/sb/alpaca.rb
|
83
|
+
- lib/sb/alpaca/version.rb
|
84
|
+
- sb-alpaca.gemspec
|
85
|
+
homepage:
|
86
|
+
licenses: []
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubygems_version: 3.0.3
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: "..."
|
107
|
+
test_files: []
|