Skip to content

Commit

Permalink
Fix bugs and warnings:
Browse files Browse the repository at this point in the history
* Old way of finding a commodity's active month was updated with better criteria matching
* Ability to manually enter a commodity's forward month was added
* Fixed a number of deprecation and other warnings
* Fixed bugs preventing commodities and ^VIX from being displayed
* Fixed bug preventing option position values from being displayed
  • Loading branch information
afazio committed Aug 25, 2015
1 parent 17c145a commit afca59c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
Empty file modified bin/rticker
100644 → 100755
Empty file.
3 changes: 1 addition & 2 deletions lib/rticker/equity.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'rticker/entry'
require 'rticker/net'
require 'cgi'
require 'rational'
require 'date'

module RTicker
Expand Down Expand Up @@ -66,7 +65,7 @@ def Equity.update_yahoo (entries)
price = fields[0]
change = fields[1]
last_date = fields[2]
return if last_date.nil? or Date.strptime(last_date, '"%m/%d/%Y"') != Date.today
return if last_date.nil? or Date.strptime(last_date, '"%m/%d/%Y"') <= Date.today - 3
if price.to_f != entry.curr_value and not entry.curr_value.nil?
# The price has changed
entry.last_changed = Time.now()
Expand Down
21 changes: 16 additions & 5 deletions lib/rticker/future.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class Future < Entry
# ahead of time what the nearest expiration for a contract will be, we'll
# have to figure this out for each contract with web calls. Once we know
# the expiration, we'll calculate the real symbol for the contract and
# store it in @real_symbol.
# store it in @real_symbol. A future may also have an explicit amount of
# months forward to look.
attr_accessor :start_value, :exp_month, :exp_year, :market
attr_accessor :real_symbol
attr_accessor :real_symbol, :forward_months

# Commodities have strange codes for months
COMMODITY_MONTHS = %w[F G H J K M N Q U V X Z]
Expand Down Expand Up @@ -46,6 +47,15 @@ def Future.update (entries)
Thread.new { entry.determine_spot_contract }
end

entries.each do |e|
unless e.forward_months.nil?
symbol, exchange = e.symbol.split(".")
month_symbol = Future::COMMODITY_MONTHS[(Time.now().month + e.forward_months - 1) % 12]
year_symbol = (Time.now().year + ((Time.now().month + e.forward_months - 1) / 12.0).floor) % 100
e.real_symbol = "#{symbol}#{month_symbol}#{year_symbol}.#{exchange}"
end
end

if entries.any? {|e| e.real_symbol.nil?}
#puts "Please wait... determining spot contracts"
threads = []
Expand All @@ -71,7 +81,7 @@ def Future.update (entries)
price = fields[0]
change = fields[1]
last_date = fields[2]
return if last_date.nil? or Date.strptime(last_date, '"%m/%d/%Y"') != Date.today
return if last_date.nil? #or Date.strptime(last_date, '"%m/%d/%Y"') != Date.today
if price.to_f != _entry.curr_value and not _entry.curr_value.nil?
# The price has changed
_entry.last_changed = Time.now()
Expand Down Expand Up @@ -103,12 +113,13 @@ def determine_spot_contract
month_symbol = Future::COMMODITY_MONTHS[curr_month]
year_symbol = curr_year % 100 # Only want last two digits of year.
real_symbol_attempt = "#{symbol}#{month_symbol}#{year_symbol}.#{exchange}"
uri = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7" % CGI::escape(real_symbol_attempt)
uri = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=l1c1d1va2xj1b4j4dyekjm3m4rr5p5p6s7" % CGI::escape(real_symbol_attempt)
response = RTicker::Net.get_response(uri) rescue return

# This contract is only valid if the response doesn't start with
# 0.00. A commodity is never worth nothing!
unless response =~ /^0.00/
last_date = response.split("\n")[0].split(",")[2]
if not response =~ /^0.00/ and last_date and last_date != 'N/A' and Date.strptime(last_date, '"%m/%d/%Y"') >= Date.today - 3
@real_symbol = real_symbol_attempt
@exp_month = curr_month+1 # Convert from 0-based back to 1-based
@exp_year = curr_year
Expand Down
4 changes: 2 additions & 2 deletions lib/rticker/net.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def self.proxy= (proxy)
def self.get_response (url)
begin
return ::Net::HTTP::Proxy(@@proxy_host, @@proxy_port).get(URI.parse url)
rescue Timeout::Error => e
rescue Timeout::Error
return ""
end
end

def self.detect_system_proxy ()
require 'rbconfig'
os = Config::CONFIG['host_os']
os = RbConfig::CONFIG['host_os']
# Only support mac os x right now
return detect_system_proxy_macosx() if os.start_with? "darwin"
return nil
Expand Down
2 changes: 1 addition & 1 deletion lib/rticker/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def Option.run_update (entry)
begin
bid = /id="yfs_b00_[^"]*">([^<]*)</.match(response)[1]
ask = /id="yfs_a00_[^"]*">([^<]*)</.match(response)[1]
rescue Exception => e
rescue Exception
# These results aren't available from about 9am to 9:30am.
# Yahoo's results are often 20-30 minutes behind.
Thread.current.exit
Expand Down
13 changes: 12 additions & 1 deletion lib/rticker/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def RTicker.parse_entry (entry)

sign = match[1]
symbol = match[2]

if sign == '#' # This is a future
submatch = /(.*)\+([0-9]+)$/.match symbol
if submatch
symbol = submatch[1]
forward_months = submatch[2].to_i
end
end

# Because description is optional, let's make it clear with a nil that no
# description was provided.
description = match[3] == "" ? nil : match[3]
Expand All @@ -82,7 +91,9 @@ def RTicker.parse_entry (entry)
when "#"
# An entry starting with a hash is a Future. A useful mnemonic is to
# think of the "pound" sign and think of lbs of a commodity.
return Future.new(*args)
f = Future.new(*args)
f.forward_months = forward_months
return f
when "!"
# An entry starting with an exclamation mark is an option contract.
return Option.new(*args)
Expand Down
2 changes: 1 addition & 1 deletion lib/rticker/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def RTicker.update_screen (entries, no_color=false, once=false)
# So setting no_color effectively turns all of the following variables
# into empty strings, turning off terminal manipulation.
hide_cursor = tput "civis" unless no_color
show_cursor = tput "cnorm" unless no_color
#show_cursor = tput "cnorm" unless no_color
bold = tput "bold" unless no_color
unbold = tput "sgr0" unless no_color
default = tput "sgr0" unless no_color
Expand Down

0 comments on commit afca59c

Please sign in to comment.