Class: StockFolio::Runner

Inherits:
Boson::Runner
  • Object
show all
Defined in:
lib/stockfolio/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.common_transaction_optionsObject



6
7
8
9
10
11
12
13
# File 'lib/stockfolio/runner.rb', line 6

def self.common_transaction_options
    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



137
138
139
# File 'lib/stockfolio/runner.rb', line 137

def buy(options={})
    transaction = transaction_from_options(options)
end

#create(name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/stockfolio/runner.rb', line 149

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/stockfolio/runner.rb', line 184

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

#historical(symbol, start_date, end_date) ⇒ Object



31
32
33
34
# 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])
end

#portfoliosObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stockfolio/runner.rb', line 56

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/stockfolio/runner.rb', line 70

def positions(name = nil)
    portfolios = []
    if nil == name
        portfolios = Portfolio.all
    else
        portfolios = Portfolio.all(:name => name)
    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 = []
    positions.each do |symbol,position|
        p = {}
        p["Symbol"] = symbol.split(":")[1]
        p["Last Price"] = "$#{position[:l].to_f.round(2)}"
        p["Change"] = "#{position[:c]} (#{position[:cp]}%)"

        if position[:quantity] > 0
            p["Day's Gain"] = (position[:quantity] * position[:c].to_f).round(2)
            p["Shares"] = position[:quantity]
            p["Cost Basis"] = "$#{position[:cost].round(2)}"
            p["Market Value"] = "$#{position[:value].round(2)}"
            p["Gain"] = "$#{(position[:value] - position[:cost]).round(2)}"
            p["Gain %"] = "#{(100.0 * (position[:value] - position[:cost]) / position[:cost]).round(1)}%"
        else
            p["Gain"] = "$#{(0 - position[:balance]).round(2)}"
            p["Gain %"] = "#{(100.0 * (0 - position[:balance]) / position[:cost]).round(1)}%"
        end
        pos << p
    end

    puts Hirb::Helpers::Table.render(pos, :fields => ["Symbol", "Last Price", "Change", "Day's Gain", "Shares", "Cost Basis", "Market Value", "Gain", "Gain %"])
    
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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stockfolio/runner.rb', line 37

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



143
144
145
146
# File 'lib/stockfolio/runner.rb', line 143

def sell(options={})
    options[:quantity] = 0 - options[:quantity]
    transaction = transaction_from_options(options)
end

#transactions(name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/stockfolio/runner.rb', line 161

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



242
243
244
245
246
247
248
# File 'lib/stockfolio/runner.rb', line 242

def unwatch(symbol)
    item = WatchList.first(:symbol => symbol)
    if item != nil
        item.destroy
        puts "Removed #{symbol} from watchlist"
    end
end

#versionObject



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



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/stockfolio/runner.rb', line 227

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

#watchlistObject



251
252
253
254
255
256
257
258
259
# File 'lib/stockfolio/runner.rb', line 251

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