Class: StockPivot::Trading
- Inherits:
-
Object
- Object
- StockPivot::Trading
- Defined in:
- lib/stock_pivot/trading.rb
Instance Attribute Summary collapse
-
#commission ⇒ Object
Returns the value of attribute commission.
-
#long_all_in ⇒ Object
Returns the value of attribute long_all_in.
-
#margin_balance ⇒ Object
Returns the value of attribute margin_balance.
-
#margin_ratio ⇒ Object
Returns the value of attribute margin_ratio.
-
#quotes ⇒ Object
Returns the value of attribute quotes.
-
#risk_ratio ⇒ Object
Returns the value of attribute risk_ratio.
-
#short_all_in ⇒ Object
Returns the value of attribute short_all_in.
Instance Method Summary collapse
- #buying_power ⇒ Object
- #defaults ⇒ Object
-
#initialize(args) ⇒ Trading
constructor
A new instance of Trading.
- #last_eod ⇒ Object
- #long_risk_amount ⇒ Object
- #long_share_size ⇒ Object
- #risk_amount ⇒ Object
- #share_size ⇒ Object
- #short_risk_amount ⇒ Object
- #short_share_size ⇒ Object
Constructor Details
#initialize(args) ⇒ Trading
Returns a new instance of Trading.
8 9 10 11 12 13 14 15 |
# File 'lib/stock_pivot/trading.rb', line 8 def initialize(args) args = defaults.merge(args) @quotes = args.fetch(:quotes) @margin_balance = args.fetch(:margin_balance).to_d @margin_ratio = args.fetch(:margin_ratio).to_d @risk_ratio = args.fetch(:risk_ratio).to_d @commission = args.fetch(:commission).to_d end |
Instance Attribute Details
#commission ⇒ Object
Returns the value of attribute commission.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def commission @commission end |
#long_all_in ⇒ Object
Returns the value of attribute long_all_in.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def long_all_in @long_all_in end |
#margin_balance ⇒ Object
Returns the value of attribute margin_balance.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def margin_balance @margin_balance end |
#margin_ratio ⇒ Object
Returns the value of attribute margin_ratio.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def margin_ratio @margin_ratio end |
#quotes ⇒ Object
Returns the value of attribute quotes.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def quotes @quotes end |
#risk_ratio ⇒ Object
Returns the value of attribute risk_ratio.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def risk_ratio @risk_ratio end |
#short_all_in ⇒ Object
Returns the value of attribute short_all_in.
4 5 6 |
# File 'lib/stock_pivot/trading.rb', line 4 def short_all_in @short_all_in end |
Instance Method Details
#buying_power ⇒ Object
21 22 23 |
# File 'lib/stock_pivot/trading.rb', line 21 def @buying_power ||= ((@margin_balance / @margin_ratio) - (commission * 2)).to_d.floor end |
#defaults ⇒ Object
17 18 19 |
# File 'lib/stock_pivot/trading.rb', line 17 def defaults {margin_ratio: 0.50, risk_ratio: 0.01, commission: 9.99} end |
#last_eod ⇒ Object
29 30 31 |
# File 'lib/stock_pivot/trading.rb', line 29 def last_eod @last_eod ||= StockPivot::EodQuote.new(@quotes.first.to_h) end |
#long_risk_amount ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/stock_pivot/trading.rb', line 46 def long_risk_amount if @long_all_in (long_share_size * last_eod.range).to_d.floor else risk_amount end end |
#long_share_size ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/stock_pivot/trading.rb', line 37 def long_share_size if (share_size * last_eod.high) > @long_all_in = true ( / last_eod.high).floor else share_size end end |
#risk_amount ⇒ Object
25 26 27 |
# File 'lib/stock_pivot/trading.rb', line 25 def risk_amount @risk_amount ||= ( * @risk_ratio).to_d.floor end |
#share_size ⇒ Object
33 34 35 |
# File 'lib/stock_pivot/trading.rb', line 33 def share_size @share_size ||= (risk_amount / last_eod.range).to_d.floor end |
#short_risk_amount ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/stock_pivot/trading.rb', line 63 def short_risk_amount if @short_all_in (short_share_size * last_eod.range).to_d.floor else risk_amount end end |
#short_share_size ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/stock_pivot/trading.rb', line 54 def short_share_size if (share_size * last_eod.low) > @short_all_in = true ( / last_eod.low).floor else share_size end end |