stock_pivot 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 +7 -0
- data/.gitignore +24 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +10 -0
- data/bin/stockpivot +5 -0
- data/lib/stock_pivot/cli.rb +89 -0
- data/lib/stock_pivot/eod_quote.rb +21 -0
- data/lib/stock_pivot/feed_wrapper.rb +9 -0
- data/lib/stock_pivot/quotes.rb +33 -0
- data/lib/stock_pivot/trading.rb +71 -0
- data/lib/stock_pivot/version.rb +3 -0
- data/lib/stock_pivot.rb +21 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/stock_pivot/quotes_spec.rb +20 -0
- data/spec/stock_pivot/trading_spec.rb +113 -0
- data/stock_pivot.gemspec +37 -0
- metadata +250 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ae2e71493fe5fa0c0595f6b83a35511f10715675
|
4
|
+
data.tar.gz: 1086639c4f7585cb19408604132b20e4e8204aa1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 21f83e1eedaa9de17d9dba969e394de27fd6ca7f40a5790baf7543727e755c6a9631935b508e44805e734fc0f0cc0e6efd00a946f1de50105b51774be652c191
|
7
|
+
data.tar.gz: 0a75a95d180a271ce984de846f02329b60c561b82d53a23ae6ae8631d8874bda4f22fcea6d153407afde9f9850a811790c186d1e4d9e13ca15ab0864d1d45eae
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
.DS_Store
|
2
|
+
.idea
|
3
|
+
*.gem
|
4
|
+
*.rbc
|
5
|
+
.bundle
|
6
|
+
.config
|
7
|
+
.yardoc
|
8
|
+
Gemfile.lock
|
9
|
+
InstalledFiles
|
10
|
+
_yardoc
|
11
|
+
coverage
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/reports
|
17
|
+
test/tmp
|
18
|
+
test/version_tmp
|
19
|
+
tmp
|
20
|
+
*.bundle
|
21
|
+
*.so
|
22
|
+
*.o
|
23
|
+
*.a
|
24
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Scott V. Rosenthal
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# StockPivot
|
2
|
+
|
3
|
+
Given a stock symbol and margin balance, `stockpivot` will calculate tomorrow's possible long/short trades based on the following:
|
4
|
+
|
5
|
+
- stock's end of day closing
|
6
|
+
- buying power
|
7
|
+
- risk ratio
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'stock_pivot'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install stock_pivot
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
$ stockpivot trade AAPL 10000
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( https://github.com/[my-github-username]/stock_pivot/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/stockpivot
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'rainbow'
|
3
|
+
require 'active_support/number_helper'
|
4
|
+
|
5
|
+
module StockPivot
|
6
|
+
class Cli < Thor
|
7
|
+
include Thor::Actions
|
8
|
+
include ActiveSupport::NumberHelper
|
9
|
+
|
10
|
+
I18n.enforce_available_locales = false
|
11
|
+
|
12
|
+
desc 'trade SYMBOL MARGIN_BALANCE', 'calculate shares to go long/short based on last eod quote'
|
13
|
+
option :risk_ratio, type: :numeric, aliases: :r
|
14
|
+
def trade(symbol, margin_balance)
|
15
|
+
args = {}
|
16
|
+
args[:symbol] = symbol.upcase
|
17
|
+
args[:margin_balance] = margin_balance.to_d
|
18
|
+
|
19
|
+
args[:risk_ratio] = options[:risk_ratio] if options[:risk_ratio]
|
20
|
+
|
21
|
+
quotes = StockPivot::Quotes.feed(symbol)
|
22
|
+
|
23
|
+
if quotes.size > 0
|
24
|
+
args[:quotes] = quotes
|
25
|
+
trading = StockPivot::Trading.new(args)
|
26
|
+
|
27
|
+
say "\n"
|
28
|
+
say '-' * 80
|
29
|
+
say "\n"
|
30
|
+
|
31
|
+
say Rainbow("Margin balance: #{number_to_currency(trading.margin_balance)}").yellow
|
32
|
+
say Rainbow("Margin ratio: #{number_to_percentage(trading.margin_ratio)}").yellow
|
33
|
+
say Rainbow("Buying power: #{number_to_currency(trading.buying_power)}").cyan
|
34
|
+
say Rainbow("Buying power risk ratio: #{number_to_percentage(trading.risk_ratio)}").yellow
|
35
|
+
say Rainbow("Margin risk amount: #{number_to_currency(trading.risk_amount)}").red
|
36
|
+
say Rainbow("Trade commission: #{number_to_currency(trading.commission * 2)}").red
|
37
|
+
|
38
|
+
say "\n"
|
39
|
+
say '-' * 80
|
40
|
+
say "\n"
|
41
|
+
|
42
|
+
say Rainbow("Symbol: #{trading.last_eod.symbol}").yellow
|
43
|
+
say Rainbow("EOD date: #{trading.last_eod.trade_date}").yellow
|
44
|
+
say Rainbow("Risk range: #{number_to_currency(trading.last_eod.range)}").yellow
|
45
|
+
|
46
|
+
say "\n"
|
47
|
+
say '-' * 80
|
48
|
+
say "\n"
|
49
|
+
|
50
|
+
say Rainbow("Long trade:").cyan.underline
|
51
|
+
say Rainbow("#1 - BUY: #{trading.long_share_size}s @ #{trading.last_eod.high} EXP DAY - TO OPEN").green
|
52
|
+
say Rainbow("#2 - SELL: #{trading.long_share_size}s @ #{trading.last_eod.low} EXP GTC - TO CLOSE").red
|
53
|
+
say Rainbow("** ALL IN").yellow.underline if trading.long_all_in
|
54
|
+
say Rainbow("** Long Risk amount: #{number_to_currency(trading.long_risk_amount)}").red.underline
|
55
|
+
say "\n"
|
56
|
+
say Rainbow("Long 3X Profit target - OCO:").cyan.underline
|
57
|
+
say Rainbow("#3 - Profit target - SELL: #{trading.long_share_size}s @ #{trading.last_eod.high + (trading.last_eod.range * 3.00)} EXP GTC - TO CLOSE").green
|
58
|
+
say Rainbow("Profit amount: #{number_to_currency(trading.long_risk_amount * 3.00)}").green.underline
|
59
|
+
|
60
|
+
say "\n"
|
61
|
+
say '-' * 80
|
62
|
+
say "\n"
|
63
|
+
|
64
|
+
say Rainbow("Short trade:").cyan.underline
|
65
|
+
say Rainbow("#1 - SELL: #{trading.short_share_size}s @ #{trading.last_eod.low} EXP DAY - TO OPEN").red
|
66
|
+
say Rainbow("#2 - BUY: #{trading.short_share_size}s @ #{trading.last_eod.high} EXP GTC - TO CLOSE").green
|
67
|
+
say Rainbow("** ALL IN").yellow.underline if trading.short_all_in
|
68
|
+
say Rainbow("** Short Risk amount: #{number_to_currency(trading.short_risk_amount)}").red.underline
|
69
|
+
say "\n"
|
70
|
+
say Rainbow("Short 3X Profit target - OCO:").cyan.underline
|
71
|
+
say Rainbow("#3 - Profit target - BUY: #{trading.short_share_size}s @ #{trading.last_eod.low - (trading.last_eod.range * 3.00)} EXP GTC - TO CLOSE").green
|
72
|
+
say Rainbow("Profit amount: #{number_to_currency(trading.short_risk_amount * 3.00)}").green.underline
|
73
|
+
|
74
|
+
say "\n"
|
75
|
+
say '-' * 80
|
76
|
+
say "\n"
|
77
|
+
|
78
|
+
else
|
79
|
+
say "\n"
|
80
|
+
say Rainbow("No quotes found for symbol: #{symbol}").yellow
|
81
|
+
say "\n\n"
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
#puts "Margin ratio"
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module StockPivot
|
2
|
+
class EodQuote
|
3
|
+
|
4
|
+
attr_reader :symbol, :trade_date, :open, :high, :low, :close, :volume
|
5
|
+
|
6
|
+
def initialize(args)
|
7
|
+
@symbol = args.fetch(:symbol).upcase
|
8
|
+
@trade_date = args.fetch(:trade_date)
|
9
|
+
@open = args.fetch(:open, 0.0).to_d
|
10
|
+
@high = args.fetch(:high, 0.0).to_d
|
11
|
+
@low = args.fetch(:low, 0.0).to_d
|
12
|
+
@close = args.fetch(:close, 0.0).to_d
|
13
|
+
@volume = args.fetch(:volume, 0).to_i
|
14
|
+
end
|
15
|
+
|
16
|
+
def range
|
17
|
+
@range ||= (@high - @low).round(2)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module StockPivot
|
2
|
+
module Quotes
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def feed(symbol, opts = {})
|
7
|
+
opts = options.merge(opts)
|
8
|
+
days_ago = opts.fetch(:days_ago)
|
9
|
+
exclude_holidays!(days_ago)
|
10
|
+
end_date = 1.business_day.ago
|
11
|
+
start_date = days_ago.business_days.ago
|
12
|
+
StockPivot::FeedWrapper.get(
|
13
|
+
symbol: symbol,
|
14
|
+
start_date: start_date,
|
15
|
+
end_date: end_date
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
def options
|
20
|
+
{days_ago: 10}
|
21
|
+
end
|
22
|
+
|
23
|
+
def exclude_holidays!(days_ago)
|
24
|
+
beginning_of_year = days_ago.days.ago.beginning_of_year
|
25
|
+
Holidays.between(beginning_of_year, Time.now, :us, :observed).map do |holiday|
|
26
|
+
BusinessTime::Config.holidays << holiday[:date]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module StockPivot
|
2
|
+
class Trading
|
3
|
+
|
4
|
+
attr_accessor :quotes, :margin_balance,
|
5
|
+
:margin_ratio, :risk_ratio, :commission,
|
6
|
+
:long_all_in, :short_all_in
|
7
|
+
|
8
|
+
def initialize(args)
|
9
|
+
args = defaults.merge(args)
|
10
|
+
@quotes = args.fetch(:quotes)
|
11
|
+
@margin_balance = args.fetch(:margin_balance).to_d
|
12
|
+
@margin_ratio = args.fetch(:margin_ratio).to_d
|
13
|
+
@risk_ratio = args.fetch(:risk_ratio).to_d
|
14
|
+
@commission = args.fetch(:commission).to_d
|
15
|
+
end
|
16
|
+
|
17
|
+
def defaults
|
18
|
+
{margin_ratio: 0.50, risk_ratio: 0.01, commission: 9.99}
|
19
|
+
end
|
20
|
+
|
21
|
+
def buying_power
|
22
|
+
@buying_power ||= ((@margin_balance / @margin_ratio) - (commission * 2)).to_d.floor
|
23
|
+
end
|
24
|
+
|
25
|
+
def risk_amount
|
26
|
+
@risk_amount ||= (buying_power * @risk_ratio).to_d.floor
|
27
|
+
end
|
28
|
+
|
29
|
+
def last_eod
|
30
|
+
@last_eod ||= StockPivot::EodQuote.new(@quotes.first.to_h)
|
31
|
+
end
|
32
|
+
|
33
|
+
def share_size
|
34
|
+
@share_size ||= (risk_amount / last_eod.range).to_d.floor
|
35
|
+
end
|
36
|
+
|
37
|
+
def long_share_size
|
38
|
+
if (share_size * last_eod.high) > buying_power
|
39
|
+
@long_all_in = true
|
40
|
+
(buying_power / last_eod.high).floor
|
41
|
+
else
|
42
|
+
share_size
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def long_risk_amount
|
47
|
+
if @long_all_in
|
48
|
+
(long_share_size * last_eod.range).to_d.floor
|
49
|
+
else
|
50
|
+
risk_amount
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def short_share_size
|
55
|
+
if (share_size * last_eod.low) > buying_power
|
56
|
+
@short_all_in = true
|
57
|
+
(buying_power / last_eod.low).floor
|
58
|
+
else
|
59
|
+
share_size
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def short_risk_amount
|
64
|
+
if @short_all_in
|
65
|
+
(short_share_size * last_eod.range).to_d.floor
|
66
|
+
else
|
67
|
+
risk_amount
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/stock_pivot.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'user_config'
|
3
|
+
require 'yahoo_finance'
|
4
|
+
require 'business_time'
|
5
|
+
require 'holidays'
|
6
|
+
|
7
|
+
require 'stock_pivot/version'
|
8
|
+
require 'stock_pivot/feed_wrapper'
|
9
|
+
require 'stock_pivot/eod_quote'
|
10
|
+
require 'stock_pivot/quotes'
|
11
|
+
require 'stock_pivot/trading'
|
12
|
+
require 'stock_pivot/cli'
|
13
|
+
|
14
|
+
begin
|
15
|
+
require 'pry'
|
16
|
+
rescue LoadError
|
17
|
+
end
|
18
|
+
|
19
|
+
module StockPivot
|
20
|
+
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'stock_pivot'
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
6
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
7
|
+
#
|
8
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
9
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
10
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
11
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
12
|
+
# a separate helper file that requires the additional dependencies and performs
|
13
|
+
# the additional setup, and require it from the spec files that actually need it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# rspec-expectations config goes here. You can use an alternate
|
22
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
23
|
+
# assertions if you prefer.
|
24
|
+
config.expect_with :rspec do |expectations|
|
25
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
26
|
+
# and `failure_message` of custom matchers include text for helper methods
|
27
|
+
# defined using `chain`, e.g.:
|
28
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
29
|
+
# # => "be bigger than 2 and smaller than 4"
|
30
|
+
# ...rather than:
|
31
|
+
# # => "be bigger than 2"
|
32
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
36
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
37
|
+
config.mock_with :rspec do |mocks|
|
38
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
39
|
+
# a real object. This is generally recommended, and will default to
|
40
|
+
# `true` in RSpec 4.
|
41
|
+
mocks.verify_partial_doubles = true
|
42
|
+
end
|
43
|
+
|
44
|
+
# The settings below are suggested to provide a good initial experience
|
45
|
+
# with RSpec, but feel free to customize to your heart's content.
|
46
|
+
=begin
|
47
|
+
# These two settings work together to allow you to limit a spec run
|
48
|
+
# to individual examples or groups you care about by tagging them with
|
49
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
50
|
+
# get run.
|
51
|
+
config.filter_run :focus
|
52
|
+
config.run_all_when_everything_filtered = true
|
53
|
+
|
54
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
55
|
+
# For more details, see:
|
56
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
57
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
58
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
59
|
+
config.disable_monkey_patching!
|
60
|
+
|
61
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
62
|
+
# be too noisy due to issues in dependencies.
|
63
|
+
config.warnings = true
|
64
|
+
|
65
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
66
|
+
# file, and it's useful to allow more verbose output when running an
|
67
|
+
# individual spec file.
|
68
|
+
if config.files_to_run.one?
|
69
|
+
# Use the documentation formatter for detailed output,
|
70
|
+
# unless a formatter has already been configured
|
71
|
+
# (e.g. via a command-line flag).
|
72
|
+
config.default_formatter = 'doc'
|
73
|
+
end
|
74
|
+
|
75
|
+
# Print the 10 slowest examples and example groups at the
|
76
|
+
# end of the spec run, to help surface which specs are running
|
77
|
+
# particularly slow.
|
78
|
+
config.profile_examples = 10
|
79
|
+
|
80
|
+
# Run specs in random order to surface order dependencies. If you find an
|
81
|
+
# order dependency and want to debug it, you can fix the order by providing
|
82
|
+
# the seed, which is printed after each run.
|
83
|
+
# --seed 1234
|
84
|
+
config.order = :random
|
85
|
+
|
86
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
87
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
88
|
+
# test failures related to randomization by passing the same `--seed` value
|
89
|
+
# as the one that triggered the failure.
|
90
|
+
Kernel.srand config.seed
|
91
|
+
=end
|
92
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module StockPivot
|
4
|
+
describe Quotes do
|
5
|
+
describe '.feed' do
|
6
|
+
let(:feed) {
|
7
|
+
StockPivot::Quotes.feed('GPRO')
|
8
|
+
}
|
9
|
+
|
10
|
+
it 'returns an array' do
|
11
|
+
expect(feed).to be_a(Array)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'defaults to ten days' do
|
15
|
+
expect(feed.size).to eq(10)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
module StockPivot
|
6
|
+
|
7
|
+
describe Trading do
|
8
|
+
|
9
|
+
let(:quotes) do
|
10
|
+
[
|
11
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-09-05', open: '54.25', high: '59.24', low: '53.51', close: '58.75', volume: '7628400', adjusted_close: '58.75'),
|
12
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-09-04', open: '54.30', high: '55.88', low: '52.30', close: '53.08', volume: '8183900', adjusted_close: '53.08'),
|
13
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-09-03', open: '55.95', high: '57.45', low: '55.43', close: '56.86', volume: '9204800', adjusted_close: '56.86'),
|
14
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-09-02', open: '53.00', high: '55.35', low: '52.52', close: '53.94', volume: '9045100', adjusted_close: '53.94'),
|
15
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-29', open: '49.47', high: '52.13', low: '49.45', close: '51.80', volume: '1024420', adjusted_close: '51.80'),
|
16
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-28', open: '45.50', high: '49.86', low: '44.63', close: '48.90', volume: '1058910', adjusted_close: '48.90'),
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:trading) do
|
21
|
+
StockPivot::Trading.new(
|
22
|
+
quotes: quotes,
|
23
|
+
margin_balance: 10000,
|
24
|
+
margin_ratio: 0.5,
|
25
|
+
risk_ratio: 0.01,
|
26
|
+
commission: 9.99)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#buying_power' do
|
30
|
+
it 'should calculate buying power minus commission' do
|
31
|
+
expect(trading.buying_power).to eq((20000 - (9.99 * 2)).floor)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#risk_amount' do
|
36
|
+
it 'should calculate risk amount using risk ratio' do
|
37
|
+
expect(trading.risk_amount).to eq(((20000 - (9.99 * 2)) * 0.01).floor)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#last_eod' do
|
42
|
+
it 'should return the last eod open' do
|
43
|
+
expect(trading.last_eod.high).to eq((59.24).to_d)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should return the last eod high' do
|
47
|
+
expect(trading.last_eod.low).to eq((53.51).to_d)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should return the last eod range' do
|
51
|
+
expect(trading.last_eod.range).to eq((59.24 - 53.51).to_d)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#share_size' do
|
56
|
+
it 'should return the # of shares to trade' do
|
57
|
+
share_size = (trading.risk_amount / trading.last_eod.range).floor
|
58
|
+
expect(trading.share_size).to eq(share_size)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#short_share_size' do
|
63
|
+
it 'should return the # of shares to sell if bearish' do
|
64
|
+
share_size = (trading.risk_amount / trading.last_eod.range).floor
|
65
|
+
expect(trading.short_share_size).to eq(share_size)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#long_share_size' do
|
70
|
+
it 'should return the # of shares to buy if bullish' do
|
71
|
+
share_size = (trading.risk_amount / trading.last_eod.range).floor
|
72
|
+
expect(trading.long_share_size).to eq(share_size)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when trading a small range" do
|
77
|
+
|
78
|
+
let(:quotes_small_range) do
|
79
|
+
[
|
80
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-27', open: '45.87', high: '46.00', low: '45.90', close: '45.50', volume: '1906300', adjusted_close: '45.50'),
|
81
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-26', open: '44.80', high: '45.80', low: '44.57', close: '45.29', volume: '3774100', adjusted_close: '45.29'),
|
82
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-25', open: '43.85', high: '44.35', low: '43.30', close: '43.85', volume: '1847800', adjusted_close: '43.85'),
|
83
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-22', open: '43.00', high: '43.83', low: '42.70', close: '43.29', volume: '1683000', adjusted_close: '43.29'),
|
84
|
+
OpenStruct.new(symbol: 'GPRO', trade_date: '2014-08-21', open: '44.60', high: '44.86', low: '43.03', close: '43.17', volume: '2688700', adjusted_close: '43.17')
|
85
|
+
]
|
86
|
+
end
|
87
|
+
|
88
|
+
let(:trading_small_range) do
|
89
|
+
StockPivot::Trading.new(
|
90
|
+
quotes: quotes_small_range,
|
91
|
+
margin_balance: 10000,
|
92
|
+
margin_ratio: 0.5,
|
93
|
+
risk_ratio: 0.01,
|
94
|
+
commission: 9.99)
|
95
|
+
end
|
96
|
+
|
97
|
+
describe '#short_share_size' do
|
98
|
+
it 'should return the # of shares to short if bearish based on buying power' do
|
99
|
+
share_size = (trading_small_range.buying_power / trading_small_range.last_eod.low).floor
|
100
|
+
expect(trading_small_range.short_share_size).to eq(share_size)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#long_share_size' do
|
105
|
+
it 'should return the # of shares to go long if bullish based on buying power' do
|
106
|
+
share_size = (trading_small_range.buying_power / trading_small_range.last_eod.high).floor
|
107
|
+
expect(trading_small_range.long_share_size).to eq(share_size)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
data/stock_pivot.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stock_pivot/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'stock_pivot'
|
8
|
+
spec.version = StockPivot::VERSION
|
9
|
+
spec.authors = ['Scott V. Rosenthal']
|
10
|
+
spec.email = ['sr7575@gmail.com']
|
11
|
+
spec.summary = %q{Given a stock symbol & margin balance, stockpivot will calculate tomorrow's possible long/short trades.}
|
12
|
+
spec.description = %q{Given a stock symbol & margin balance, stockpivot will calculate tomorrow's possible long/short trades.}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.required_ruby_version = '~> 2.1'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'fakefs'
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
spec.add_development_dependency 'pry'
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'activesupport'
|
31
|
+
spec.add_runtime_dependency 'user_config'
|
32
|
+
spec.add_runtime_dependency 'thor'
|
33
|
+
spec.add_runtime_dependency 'rainbow'
|
34
|
+
spec.add_runtime_dependency 'yahoo-finance'
|
35
|
+
spec.add_runtime_dependency 'business_time'
|
36
|
+
spec.add_runtime_dependency 'holidays'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,250 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stock_pivot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott V. Rosenthal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-08 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: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: fakefs
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: user_config
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: thor
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rainbow
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: yahoo-finance
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: business_time
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: holidays
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
type: :runtime
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
description: Given a stock symbol & margin balance, stockpivot will calculate tomorrow's
|
196
|
+
possible long/short trades.
|
197
|
+
email:
|
198
|
+
- sr7575@gmail.com
|
199
|
+
executables:
|
200
|
+
- stockpivot
|
201
|
+
extensions: []
|
202
|
+
extra_rdoc_files: []
|
203
|
+
files:
|
204
|
+
- ".gitignore"
|
205
|
+
- ".rspec"
|
206
|
+
- Gemfile
|
207
|
+
- LICENSE.txt
|
208
|
+
- README.md
|
209
|
+
- Rakefile
|
210
|
+
- bin/stockpivot
|
211
|
+
- lib/stock_pivot.rb
|
212
|
+
- lib/stock_pivot/cli.rb
|
213
|
+
- lib/stock_pivot/eod_quote.rb
|
214
|
+
- lib/stock_pivot/feed_wrapper.rb
|
215
|
+
- lib/stock_pivot/quotes.rb
|
216
|
+
- lib/stock_pivot/trading.rb
|
217
|
+
- lib/stock_pivot/version.rb
|
218
|
+
- spec/spec_helper.rb
|
219
|
+
- spec/stock_pivot/quotes_spec.rb
|
220
|
+
- spec/stock_pivot/trading_spec.rb
|
221
|
+
- stock_pivot.gemspec
|
222
|
+
homepage: ''
|
223
|
+
licenses:
|
224
|
+
- MIT
|
225
|
+
metadata: {}
|
226
|
+
post_install_message:
|
227
|
+
rdoc_options: []
|
228
|
+
require_paths:
|
229
|
+
- lib
|
230
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
231
|
+
requirements:
|
232
|
+
- - "~>"
|
233
|
+
- !ruby/object:Gem::Version
|
234
|
+
version: '2.1'
|
235
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
236
|
+
requirements:
|
237
|
+
- - ">="
|
238
|
+
- !ruby/object:Gem::Version
|
239
|
+
version: '0'
|
240
|
+
requirements: []
|
241
|
+
rubyforge_project:
|
242
|
+
rubygems_version: 2.4.1
|
243
|
+
signing_key:
|
244
|
+
specification_version: 4
|
245
|
+
summary: Given a stock symbol & margin balance, stockpivot will calculate tomorrow's
|
246
|
+
possible long/short trades.
|
247
|
+
test_files:
|
248
|
+
- spec/spec_helper.rb
|
249
|
+
- spec/stock_pivot/quotes_spec.rb
|
250
|
+
- spec/stock_pivot/trading_spec.rb
|