option_max_pain 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e36fb689ae9b20e12246677b5e3bf9b332c9b66e326bc94103c74a19e2b7928
4
+ data.tar.gz: ff30fc5284580a0ef2bff86c3dd221fbe1fa115fb41569c45900e06c42106d85
5
+ SHA512:
6
+ metadata.gz: '04915fbafc086a7e75320a320f1560dee1641720b5a984b9fca4e0e732f4b658a0db8f42c94deb5107a86f9747ea90201d834438184f470041c374664150743c'
7
+ data.tar.gz: 8f36746c44020198d0e307f6bb968dd28b63c41aa10b429f5792e32ed41760333deeb3f53ab5e2ade41719c868c9cdc43784023e5b991dbea70bd1b4985f4097
@@ -0,0 +1,2 @@
1
+
2
+ pkg/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in option_max_pain.gemspec
6
+ gemspec
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ option_max_pain (0.1.1)
5
+ nokogiri
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ansi (1.5.0)
11
+ builder (3.2.3)
12
+ coderay (1.1.2)
13
+ ffi (1.9.25)
14
+ formatador (0.2.5)
15
+ guard (2.14.2)
16
+ formatador (>= 0.2.4)
17
+ listen (>= 2.7, < 4.0)
18
+ lumberjack (>= 1.0.12, < 2.0)
19
+ nenv (~> 0.1)
20
+ notiffany (~> 0.0)
21
+ pry (>= 0.9.12)
22
+ shellany (~> 0.0)
23
+ thor (>= 0.18.1)
24
+ guard-compat (1.2.1)
25
+ guard-minitest (2.4.6)
26
+ guard-compat (~> 1.2)
27
+ minitest (>= 3.0)
28
+ listen (3.1.5)
29
+ rb-fsevent (~> 0.9, >= 0.9.4)
30
+ rb-inotify (~> 0.9, >= 0.9.7)
31
+ ruby_dep (~> 1.2)
32
+ lumberjack (1.0.13)
33
+ method_source (0.9.0)
34
+ mini_portile2 (2.3.0)
35
+ minitest (5.11.3)
36
+ minitest-reporters (1.3.0)
37
+ ansi
38
+ builder
39
+ minitest (>= 5.0)
40
+ ruby-progressbar
41
+ nenv (0.3.0)
42
+ nokogiri (1.8.4)
43
+ mini_portile2 (~> 2.3.0)
44
+ notiffany (0.1.1)
45
+ nenv (~> 0.1)
46
+ shellany (~> 0.0)
47
+ pry (0.11.3)
48
+ coderay (~> 1.1.0)
49
+ method_source (~> 0.9.0)
50
+ rake (10.5.0)
51
+ rb-fsevent (0.10.3)
52
+ rb-inotify (0.9.10)
53
+ ffi (>= 0.5.0, < 2)
54
+ ruby-progressbar (1.9.0)
55
+ ruby_dep (1.5.0)
56
+ shellany (0.0.1)
57
+ thor (0.20.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler (~> 1.16)
64
+ guard
65
+ guard-minitest
66
+ minitest (~> 5.0)
67
+ minitest-reporters
68
+ option_max_pain!
69
+ rake (~> 10.0)
70
+
71
+ BUNDLED WITH
72
+ 1.16.2
@@ -0,0 +1,42 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ guard :minitest do
19
+ # with Minitest::Unit
20
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
21
+ watch(%r{^lib/(.*)?/([^/]+)\.rb$}) { |m| "test/test_#{m[1]}_#{m[2]}.rb" }
22
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
23
+
24
+ # with Minitest::Spec
25
+ # watch(%r{^spec/(.*)_spec\.rb$})
26
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
27
+ # watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
28
+
29
+ # Rails 4
30
+ # watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
31
+ # watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
32
+ # watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
33
+ # watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
34
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
35
+ # watch(%r{^test/.+_test\.rb$})
36
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
37
+
38
+ # Rails < 4
39
+ # watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
40
+ # watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
41
+ # watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
42
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Owais
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # OptionMaxPain
2
+
3
+ Automatically calculate max pain of options listed on the National Stock Exchange of India, or through custom data
4
+
5
+ ## What is Option Pain?
6
+
7
+ > The theory of options pain stems as a corollary to the belief – “90% of the options expire worthless, hence option writers/sellers tend to make money more often, more consistently than the option buyers”.<br><br>
8
+ Now if this statement is true, then we can make a bunch of logical deductions –<br>
9
+ >1) At any point only one party can make money i.e either the option buyers or option sellers, but not both.
10
+ >2) From the above statement, it is clear that the sellers are the ones making money.
11
+ >3) If option sellers tend to make maximum money, then it also means that the price of the option on expiry day should be driven to a point where it would cause least amount of loss to option writers.<br><br>
12
+
13
+ >If point 2 is true, then it further implies that option prices can be manipulated, at least on the day of expiry.<br>
14
+ If point 3 is true, then it further implies that there exists a group of traders who can manipulate the option prices, at least on the day of expiry.<br><br>
15
+ If such a group exists then it must be the option writers/sellers since it is believed that they are the ones who make maximum money/consistently make money trading options.<br><br>
16
+ Now considering all the above points, there must exist a single price point at which, if the market expires, then it would cause least amount of pain to the option writers (or cause maximum amount of pain to option buyers).<br><br>
17
+ If one can identify this price point, then it’s most likely that this is the point at which markets will expire. The ‘Option Pain’ theory does just this – identify the price at which the market is likely to expire considering least amount of pain is caused to option writers.If one can identify this price point, then it’s most likely that this is the point at which markets will expire. The ‘Option Pain’ theory does just this – identify the price at which the market is likely to expire considering least amount of pain is caused to option writers.
18
+
19
+ [Read more about Option Max Pain](https://zerodha.com/varsity/chapter/max-pain-pcr-ratio/ "Read more about Option Max Pain")
20
+
21
+ ## How to use this calculator?
22
+ WIP
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "option_max_pain"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "option_max_pain/version"
2
+ require "option_max_pain/calculator"
3
+ require "option_max_pain/nse_calculator"
4
+
5
+
6
+ module OptionMaxPain
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,26 @@
1
+ module OptionMaxPain
2
+ class Calculator
3
+ def self.calculate_max_pain(options)
4
+ # options should be a hash with available strikes as keys that contains
5
+ # a hash of its OI of call and put options
6
+ # Eg =>
7
+ # options = {7700 => {call: 2131, put: 1234}, 7800 => {call: 890, put: 8209183}}
8
+
9
+ options_pain = {}
10
+ sorted_strikes = options.keys.sort
11
+
12
+ sorted_strikes.each_with_index do |strike, index|
13
+ loss_due_to_call = sorted_strikes[0..index].collect do |key|
14
+ options[key][:call] * (strike - key)
15
+ end.sum
16
+
17
+ loss_due_to_put = sorted_strikes[index..-1].collect do |key|
18
+ options[key][:put] * (key - strike)
19
+ end.sum
20
+
21
+ options_pain[strike] = loss_due_to_call + loss_due_to_put
22
+ end
23
+ options_pain.min_by {|k,v| v}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ module OptionMaxPain
5
+ class NseCalculator < Calculator
6
+ def self.calculate_max_pain(stock)
7
+ page = Nokogiri::HTML open "https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=#{stock}"
8
+
9
+ strike_col_index = self.get_col_index page.css("th[title='Strike Price']").first
10
+ put_oi_col_index = self.get_col_index page.css("th[title='Open Interest']").last
11
+ call_oi_col_index = self.get_col_index page.css("th[title='Open Interest']").first
12
+
13
+ options = page.css('table#octable tr')[2..-2].collect do |row|
14
+ {
15
+ row.elements[strike_col_index].text.gsub(',','').to_i => {
16
+ call: row.elements[call_oi_col_index].text.gsub(',','').to_i,
17
+ put: row.elements[put_oi_col_index].text.gsub(',','').to_i
18
+ }
19
+ }
20
+ end
21
+ options
22
+
23
+ super(options.reduce({}, :merge))
24
+ end
25
+
26
+ def self.get_col_index(element)
27
+ element.parent.elements.index(element)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module OptionMaxPain
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "option_max_pain/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "option_max_pain"
8
+ spec.version = OptionMaxPain::VERSION
9
+ spec.authors = ["Owais"]
10
+ spec.email = ["owaiswiz@gmail.com"]
11
+
12
+ spec.summary = %q{Automatically calculate max pain of options listed on the National Stock Exchange of India, or through custom data}
13
+ spec.homepage = "https://github.com/owaiswiz/OptionMaxPain"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "nokogiri"
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "minitest-reporters"
30
+ spec.add_development_dependency "guard"
31
+ spec.add_development_dependency "guard-minitest"
32
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: option_max_pain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Owais
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - owaiswiz@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - Guardfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/option_max_pain.rb
128
+ - lib/option_max_pain/calculator.rb
129
+ - lib/option_max_pain/nse_calculator.rb
130
+ - lib/option_max_pain/version.rb
131
+ - option_max_pain.gemspec
132
+ homepage: https://github.com/owaiswiz/OptionMaxPain
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.7.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Automatically calculate max pain of options listed on the National Stock
156
+ Exchange of India, or through custom data
157
+ test_files: []