3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/option_max_pain/calculator.rb', line 3
def self.calculate_max_pain(options)
options_pain = {}
sorted_strikes = options.keys.sort
sorted_strikes.each_with_index do |strike, index|
loss_due_to_call = sorted_strikes[0..index].collect do |key|
options[key][:call] * (strike - key)
end.sum
loss_due_to_put = sorted_strikes[index..-1].collect do |key|
options[key][:put] * (key - strike)
end.sum
options_pain[strike] = loss_due_to_call + loss_due_to_put
end
options_pain.min_by {|k,v| v}
end
|