Class: StockFolio::Runner
- Inherits:
-
Boson::Runner
- Object
- Boson::Runner
- StockFolio::Runner
- Defined in:
- lib/stockfolio/runner.rb
Class Method Summary collapse
Instance Method Summary collapse
- #buy(options = {}) ⇒ Object
- #create(name) ⇒ Object
- #delete(type, alpha, beta = nil) ⇒ Object
- #graph_historical(symbol, start_date, end_date) ⇒ Object
- #historical(symbol, start_date, end_date) ⇒ Object
- #portfolios ⇒ Object
- #positions(name = nil) ⇒ Object
- #quote(symbol) ⇒ Object
- #search(term) ⇒ Object
- #sell(options = {}) ⇒ Object
- #transactions(name) ⇒ Object
- #unwatch(symbol) ⇒ Object
- #version ⇒ Object
- #watch(symbol) ⇒ Object
- #watchlist ⇒ Object
Class Method Details
.common_transaction_options ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/stockfolio/runner.rb', line 6 def self. option :portfolio, :type => :string, :desc => 'Portfolio name', :required => true option :symbol, :type=>:string, :desc => 'Symbol of stock', :required => true option :price, :type => :numeric, :desc => 'Price paid', :required => true option :quantity, :type => :numeric, :desc => 'Quantity', :required => true option :fee, :type => :numeric, :desc => 'Transaction fee' option :date, :type => :string, :desc => 'Transaction date' end |
Instance Method Details
#buy(options = {}) ⇒ Object
187 188 189 |
# File 'lib/stockfolio/runner.rb', line 187 def buy(={}) transaction = () end |
#create(name) ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/stockfolio/runner.rb', line 199 def create(name) p = Portfolio.all(:name => name) if p.size > 0 puts "Portfolio \"#{name}\" already exists" exit end Portfolio.create(:name => name, :created_at => DateTime.now) puts "Created portfolio \"#{name}\"" end |
#delete(type, alpha, beta = nil) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/stockfolio/runner.rb', line 234 def delete(type,alpha,beta = nil) if type == "transaction" name = alpha id = beta puts "Deleting transaction #{id} from #{name}" portfolio = Portfolio.first(:name => name) if portfolio == nil puts "Portfolio #{name} not found" exit end transaction = Transaction.get(id) if transaction != nil && transaction.portfolio == portfolio transaction.destroy! else puts "Could not find transaction #{id} in portfolio #{name}" end elsif type == "portfolio" name = alpha puts "Deleting portfolio #{name}" portfolio = Portfolio.first(:name => name) if portfolio == nil puts "Portfolio #{name} not found" exit end portfolio.transactions.each do |transaction| transaction.destroy! end portfolio.destroy! end end |
#graph_historical(symbol, start_date, end_date) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/stockfolio/runner.rb', line 41 def graph_historical(symbol,start_date,end_date) history = StockFolio::Web.historical(symbol, DateTime.parse(start_date), DateTime.parse(end_date)) values = [] history.each do |row| values << row[:close] end values.reverse! StockFolio::ConsoleGraph.new(values) end |
#historical(symbol, start_date, end_date) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/stockfolio/runner.rb', line 31 def historical(symbol,start_date,end_date) history = StockFolio::Web.historical(symbol, DateTime.parse(start_date), DateTime.parse(end_date)) puts Hirb::Helpers::Table.render(history, :fields => [:date, :open, :close, :low, :high, :volume]) # http://tagaholic.me/hirb/doc/classes/Hirb/Helpers/Table.html#M000008 # :headers => { :date => "Date", ... } # :description => false # :filters => { :date => lambda { |f} }} # https://github.com/change/method_profiler/blob/master/lib/method_profiler/hirb.rb end |
#portfolios ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/stockfolio/runner.rb', line 72 def portfolios q = Portfolio.all portfolios = [] q.each do |p| portfolios << { :name => p.name, :created_at => p.created_at } end puts Hirb::Helpers::Table.render(portfolios, :fields => [:name, :created_at]) end |
#positions(name = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/stockfolio/runner.rb', line 86 def positions(name = nil) portfolios = [] if nil == name portfolios = Portfolio.all if portfolios.size == 0 puts "No portfolio found" exit end else portfolios = Portfolio.all(:name => name) if portfolios.size == 0 puts "Portfolio #{name} not found" exit end end positions = {} portfolios.each do |portfolio| #puts "Positions for #{portfolio.name}" portfolio.transactions.each do |transaction| #puts "#{transaction.order_name} #{transaction.quantity} #{transaction.symbol}" if positions[transaction.symbol] positions[transaction.symbol][:quantity] = positions[transaction.symbol][:quantity] + transaction.quantity #if transaction.quantity > 0 positions[transaction.symbol][:cost] = positions[transaction.symbol][:cost] + transaction.quantity * transaction.price + transaction.real_fee #end positions[transaction.symbol][:balance] = positions[transaction.symbol][:balance] + transaction.quantity * transaction.price + transaction.real_fee else positions[transaction.symbol] = { :symbol => transaction.symbol, :cost => transaction.quantity * transaction.price + transaction.real_fee, :balance => transaction.quantity * transaction.price, :quantity => transaction.quantity } end end end # Get current prices quotes = StockFolio::Web.quote(positions.keys.join(",")) quotes.each do |q| symbol = "#{q["e"]}:#{q["t"]}" positions[symbol][:l] = q["l"] positions[symbol][:c] = q["c"] positions[symbol][:cp] = q["cp"] positions[symbol][:value] = positions[symbol][:quantity] * q["l"].to_f end pos = [] total = {} total[:symbol] = "Total" total[:daygain] = 0 total[:cost] = 0 total[:value] = 0 total[:gain] = 0 positions.each do |symbol,position| p = {} p[:symbol] = symbol.split(":")[1] p[:last_price] = position[:l].to_f p[:change] = "#{position[:c]} (#{position[:cp]}%)" if position[:quantity] > 0 p[:daygain] = position[:quantity] * position[:c].to_f p[:quantity] = position[:quantity] p[:cost] = position[:cost].to_f p[:value] = position[:value].to_f p[:gain] = position[:value] - position[:cost] p[:gain_p] = (position[:value] - position[:cost]) / position[:cost] total[:daygain] = total[:daygain] + p[:daygain] total[:cost] = total[:cost] + position[:cost] total[:value] = total[:value] + position[:value] total[:gain] = total[:gain] + position[:value] - position[:cost] else p[:gain] = (0 - position[:balance]) p[:gain_p] = (0 - position[:balance]) / position[:cost] end pos << p end pos.sort! { |a,b| a[:symbol] <=> b[:symbol] } total[:gain_p] = (total[:value] - total[:cost]) / total[:cost] pos << total puts Hirb::Helpers::Table.render(pos, :fields => [:symbol, :last_price, :change, :daygain, :quantity, :cost, :value, :gain, :gain_p], :headers => {:symbol => "Symbol", :last_price => "Last Price", :change => "Change", :daygain => "Day's Gain", :quantity => "Shares", :cost => "Cost Basis", :value => "Market Value", :gain => "Gain", :gain_p => "Gain %"}, :filters => { :last_price => :to_dollars, :daygain => :to_dollars, :cost => :to_dollars, :value => :to_dollars, :gain => :to_dollars, :gain_p => :to_percent }, :description => false ) puts "Market is #{StockFolio::Web.market_status}" end |
#quote(symbol) ⇒ Object
25 26 27 28 |
# File 'lib/stockfolio/runner.rb', line 25 def quote(symbol) quote = StockFolio::Web.quote(symbol) print_quotes(quote) end |
#search(term) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/stockfolio/runner.rb', line 53 def search(term) matches = StockFolio::Web.search(term) lines = [] matches.each do |match| if match['e'].length > 0 && match['t'].length > 0 line = { :Symbol => "#{match['e']}:#{match['t']}", :Name => match['n'] } lines << line end end puts Hirb::Helpers::Table.render(lines, :fields => [:Symbol, :Name]) end |
#sell(options = {}) ⇒ Object
193 194 195 196 |
# File 'lib/stockfolio/runner.rb', line 193 def sell(={}) [:quantity] = 0 - [:quantity] transaction = () end |
#transactions(name) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/stockfolio/runner.rb', line 211 def transactions(name) p = Portfolio.first(:name => name) if p == nil puts "Portfolio #{name} not found" exit end transactions = [] p.transactions.each do |transaction| transactions << { :id => transaction.id, :symbol => transaction.symbol, :quantity => transaction.quantity, :price => transaction.price, :executed_at => transaction.executed_at, :fee => transaction.fee, :created_at => transaction.created_at } end puts Hirb::Helpers::Table.render(transactions, fields: [:id, :symbol, :quantity, :price, :fee, :executed_at, :created_at]) end |
#unwatch(symbol) ⇒ Object
292 293 294 295 296 297 298 |
# File 'lib/stockfolio/runner.rb', line 292 def unwatch(symbol) item = WatchList.first(:symbol => symbol) if item != nil item.destroy puts "Removed #{symbol} from watchlist" end end |
#version ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/stockfolio/runner.rb', line 15 def version require 'stockfolio/version' require 'stockfolio/check' puts StockFolio::VERSION if StockFolio::Check.has_newer puts "There is a more recent version #{StockFolio::Check.version_available}" end end |
#watch(symbol) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/stockfolio/runner.rb', line 277 def watch(symbol) # Validate symbol quote = StockFolio::Web.quote(symbol) if nil != quote quote.each do |q| item = WatchList.create( :symbol => "#{q["e"]}:#{q["t"]}", :created_at => DateTime.now ) puts "Added #{item.symbol} to watchlist - current price $#{q['l']}" end end end |
#watchlist ⇒ Object
301 302 303 304 305 306 307 308 309 |
# File 'lib/stockfolio/runner.rb', line 301 def watchlist items = WatchList.all(:order => [:symbol.asc]) symbols = [] items.each do |item| symbols << item.symbol end quote = StockFolio::Web.quote(symbols.join(",")) print_quotes(quote) end |