Class: OptionMaxPain::NseCalculator

Inherits:
Calculator show all
Defined in:
lib/option_max_pain/nse_calculator.rb

Class Method Summary collapse

Class Method Details

.calculate_max_pain(stock) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/option_max_pain/nse_calculator.rb', line 6

def self.calculate_max_pain(stock)
  page = Nokogiri::HTML open "https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=#{stock}"

  strike_col_index  = self.get_col_index page.css("th[title='Strike Price']").first
  put_oi_col_index  = self.get_col_index page.css("th[title='Open Interest']").last
  call_oi_col_index = self.get_col_index page.css("th[title='Open Interest']").first

  options = page.css('table#octable tr')[2..-2].collect do |row|
    { 
      row.elements[strike_col_index].text.gsub(',','').to_i => {
        call: row.elements[call_oi_col_index].text.gsub(',','').to_i,
        put:  row.elements[put_oi_col_index].text.gsub(',','').to_i
      }
    }
  end
  options

  super(options.reduce({}, :merge))
end

.get_col_index(element) ⇒ Object



26
27
28
# File 'lib/option_max_pain/nse_calculator.rb', line 26

def self.get_col_index(element)
  element.parent.elements.index(element)
end