Class: DisplayData
- Inherits:
-
Object
- Object
- DisplayData
- Defined in:
- lib/sp500_analyzer/display_data.rb
Class Method Summary collapse
- .display_by_year(year) ⇒ Object
- .display_comparison_info(date_1, date_2) ⇒ Object
- .display_datapoint(datapoint) ⇒ Object
- .display_market_crash_info ⇒ Object
- .display_max_by_year ⇒ Object
- .display_points(array) ⇒ Object
- .display_price(price) ⇒ Object
- .display_yearly ⇒ Object
- .extended_info(date) ⇒ Object
- .validate_date(date) ⇒ Object
- .validate_year(year) ⇒ Object
Class Method Details
.display_by_year(year) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/sp500_analyzer/display_data.rb', line 56 def self.display_by_year(year) array = DataPoint.all.select do |point| point.date.year == year end self.display_points(array) end |
.display_comparison_info(date_1, date_2) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sp500_analyzer/display_data.rb', line 109 def self.display_comparison_info(date_1, date_2) datapoint1 = DataPoint.find_by_date(date_1) datapoint2 = DataPoint.find_by_date(date_2) if datapoint1.date > datapoint2.date datapoint1 = DataPoint.find_by_date(date_2) datapoint2 = DataPoint.find_by_date(date_1) end difference = datapoint2.price - datapoint1.price string_difference = "#{'+' if difference > 0}#{display_price(difference)}" high = AnalyzeData.max_within_period(datapoint1,datapoint2) low = AnalyzeData.min_within_period(datapoint1,datapoint2) puts "-"*50 puts "1. " + display_datapoint(datapoint1) puts "2. " + display_datapoint(datapoint2) puts "\nChange in price between #{datapoint1.date.strftime('%m/%d/%Y')} and #{datapoint2.date.strftime('%m/%d/%Y')}:" puts "#{string_difference}" puts "\nHighest point between #{datapoint1.date.strftime('%m/%d/%Y')} and #{datapoint2.date.strftime('%m/%d/%Y')}:" puts display_datapoint(high) puts "\nLowest point between #{datapoint1.date.strftime('%m/%d/%Y')} and #{datapoint2.date.strftime('%m/%d/%Y')}:" puts display_datapoint(low) puts "-"*50 end |
.display_datapoint(datapoint) ⇒ Object
26 27 28 |
# File 'lib/sp500_analyzer/display_data.rb', line 26 def self.display_datapoint(datapoint) "Date: #{datapoint.date.strftime('%b-%d-%Y')} Price: #{display_price(datapoint.price)}" end |
.display_market_crash_info ⇒ Object
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 |
# File 'lib/sp500_analyzer/display_data.rb', line 82 def self.display_market_crash_info puts "A stock market crash may be defined as \"a rapid and often unanticipated decrease in stock prices.\" In order to distinguish crash periods within our data, we must specify the variables of \"rapidness\" (timespan) and \"decrease in stock price\". The decrease in stock price will be set using a percentage value and the timespan will be set using a number of months.\n\n" puts "Example: Setting a decline percentage of '20' and a timespan of '12' will provide information for all stock market crashes where the price of the S&P 500 decreased by 20% within 1 year (12 months).\n\n" decline = 0 timespan = 0 until decline >=1 && decline < 100 print "Set decrease percentage(1-99): " decline = gets.strip.to_i end until timespan >= 1 print "Set timespan in months: " timespan = gets.strip.to_i end decline_float = 1.0 - (decline.to_f/100.0) market_peaks = AnalyzeData.market_peaks(decline_float, timespan) market_bottoms = AnalyzeData.market_crash_mins(decline_float, timespan) market_peaks.each_with_index do |peak, index| puts "-"*50 puts "CRASH #{index+1} INFO\n\n" crash = Crash.new(peak, market_bottoms[index]) puts "Market Peak: " + crash.peak.date.strftime("%B %d, %Y") + " - " + display_price(crash.peak.price) puts "Market Trough: " + crash.bottom.date.strftime("%B %d, %Y") + " - " + display_price(crash.bottom.price) puts "Market Recovery: "+ crash.recovery_point.date.strftime("%B %d, %Y") + " - " + display_price(crash.recovery_point.price) end puts "-"*50 end |
.display_max_by_year ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sp500_analyzer/display_data.rb', line 63 def self.display_max_by_year year = "" until year == "all" || (year.to_i >= 1871 && year.to_i <= DataPoint.all.last.date.year) print "Enter year (or \"all\" to see maximums for all years): " year = gets.strip end if year == "all" all_years = (DataPoint.all.first.date.year..DataPoint.all.last.date.year).to_a maximums = all_years.map do |year| AnalyzeData.annual_max(year) end display_points(maximums) else puts "-"*40 puts display_datapoint(AnalyzeData.annual_max(year.to_i)) puts "-"*40 end end |
.display_points(array) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/sp500_analyzer/display_data.rb', line 41 def self.display_points(array) puts "-"*40 array.each do |datapoint| puts display_datapoint(datapoint) puts "-"*40 end end |
.display_price(price) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/sp500_analyzer/display_data.rb', line 4 def self.display_price(price) if price > 0 price_string = "$" + sprintf('%.2f', price) else price_string = "-$" + sprintf('%.2f', price.abs) end price_string end |
.display_yearly ⇒ Object
49 50 51 52 53 54 |
# File 'lib/sp500_analyzer/display_data.rb', line 49 def self.display_yearly array = DataPoint.all.select do |point| point.date.month == 1 end self.display_points(array) end |
.extended_info(date) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sp500_analyzer/display_data.rb', line 30 def self.extended_info(date) datapoint = DataPoint.find_by_date(date) puts "-"*40 puts "Date: #{datapoint.date.strftime('%B %d, %Y')}\n\n" puts "Price: #{display_price(datapoint.price)}" puts "Change over previous month: #{'+' if datapoint.monthly_change > 0}#{display_price(datapoint.monthly_change)}" puts "Change over previous year: #{'+' if datapoint.yearly_change > 0}#{display_price(datapoint.yearly_change)}" puts "Historical maximum (#{datapoint.historical_max.date.strftime('%m/%d/%Y')}): #{display_price(datapoint.historical_max.price)}" puts "-"*40 end |
.validate_date(date) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/sp500_analyzer/display_data.rb', line 13 def self.validate_date(date) valid = false date_array = date.split("/") valid = true if date_array[0].to_i >= 1 && date_array[0].to_i <= 12 && date_array[1].to_i >= 1871 && date_array[1].to_i <= DataPoint.all.last.date.year valid end |