Class: AnalyzeData

Inherits:
Object
  • Object
show all
Defined in:
lib/sp500_analyzer/analyze_data.rb

Class Method Summary collapse

Class Method Details

.annual_max(year) ⇒ Object



5
6
7
8
9
10
# File 'lib/sp500_analyzer/analyze_data.rb', line 5

def self.annual_max(year)
  array = DataPoint.find_by_year(year)
  array.max_by do |datapoint|
    datapoint.price
  end
end

.annual_min(year) ⇒ Object



12
13
14
15
16
17
# File 'lib/sp500_analyzer/analyze_data.rb', line 12

def self.annual_min(year)
  array = DataPoint.find_by_year(year)
  array.min_by do |datapoint|
    datapoint.price
  end
end

.find_recovery(datapoint) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sp500_analyzer/analyze_data.rb', line 44

def self.find_recovery(datapoint)
  loop_id = datapoint.id + 1
  recovered = false
  until recovered == true || loop_id > DataPoint.all.count
    if DataPoint.all[loop_id].price >= datapoint.price
      recovered = true
      recovery_point = DataPoint.all[loop_id]
    end
    loop_id += 1
  end
  recovery_point
end

.market_crash_mins(fraction, time_in_months) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/sp500_analyzer/analyze_data.rb', line 57

def self.market_crash_mins(fraction, time_in_months)
  peaks = market_peaks(fraction, time_in_months)
  mins = []
  peaks.each do |peak|
    mins << min_within_period(peak, find_recovery(peak))
  end
  mins
end

.market_crashes(fraction, time_in_months) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sp500_analyzer/analyze_data.rb', line 19

def self.market_crashes(fraction, time_in_months)
  crashes = []
  DataPoint.all.each do |datapoint|
    # binding.pry
    loop_id = datapoint.id
    valid_crash = false
    until loop_id + time_in_months < datapoint.id || loop_id < 0
      valid_crash = true if datapoint.price <= DataPoint.all[loop_id].price * fraction
      loop_id -= 1
    end
    crashes << datapoint if valid_crash
  end
  crashes
end

.market_peaks(fraction, time_in_months) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/sp500_analyzer/analyze_data.rb', line 34

def self.market_peaks(fraction, time_in_months)
  peaks = []
  market_crashes(fraction, time_in_months).each do |crash|
    DataPoint.all.each do |datapoint|
      peaks << datapoint if datapoint.price == crash.historical_max.price && datapoint.price == datapoint.historical_max.price && !peaks.include?(datapoint)
    end
  end
  peaks
end

.max_within_period(datapoint_1, datapoint_2) ⇒ Object



81
82
83
84
85
# File 'lib/sp500_analyzer/analyze_data.rb', line 81

def self.max_within_period(datapoint_1, datapoint_2)
  timeframe(datapoint_1,datapoint_2).max_by do |datapoint|
    datapoint.price
  end
end

.min_within_period(datapoint_1, datapoint_2) ⇒ Object



87
88
89
90
91
# File 'lib/sp500_analyzer/analyze_data.rb', line 87

def self.min_within_period(datapoint_1, datapoint_2)
  timeframe(datapoint_1,datapoint_2).min_by do |datapoint|
    datapoint.price
  end
end

.price_difference(datapoint_1, datapoint_2) ⇒ Object



66
67
68
# File 'lib/sp500_analyzer/analyze_data.rb', line 66

def self.price_difference(datapoint_1, datapoint_2)
  datapoint_1.price - datapoint_2.price
end

.timeframe(datapoint_1, datapoint_2) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/sp500_analyzer/analyze_data.rb', line 70

def self.timeframe(datapoint_1, datapoint_2)
  data_spread = []
  range = [datapoint_1.id, datapoint_2.id]
  loop_id = range.min
  while loop_id <= range.max
    data_spread << DataPoint.all[loop_id]
    loop_id += 1
  end
  data_spread
end