finviz_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +31 -0
  4. data/Rakefile +34 -0
  5. data/lib/finviz_rails.rb +10 -0
  6. data/lib/finviz_rails/filter_mapper.rb +41 -0
  7. data/lib/finviz_rails/finviz.rb +34 -0
  8. data/lib/finviz_rails/indices.rb +35 -0
  9. data/lib/finviz_rails/login.rb +25 -0
  10. data/lib/finviz_rails/screener.rb +74 -0
  11. data/lib/finviz_rails/url_formatter.rb +42 -0
  12. data/lib/finviz_rails/version.rb +3 -0
  13. data/lib/tasks/finviz_rails_tasks.rake +4 -0
  14. data/test/dummy/README.rdoc +28 -0
  15. data/test/dummy/Rakefile +6 -0
  16. data/test/dummy/app/assets/javascripts/application.js +13 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  18. data/test/dummy/app/controllers/application_controller.rb +5 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/test/dummy/bin/bundle +3 -0
  22. data/test/dummy/bin/rails +4 -0
  23. data/test/dummy/bin/rake +4 -0
  24. data/test/dummy/bin/setup +29 -0
  25. data/test/dummy/config.ru +4 -0
  26. data/test/dummy/config/application.rb +26 -0
  27. data/test/dummy/config/boot.rb +5 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +41 -0
  31. data/test/dummy/config/environments/production.rb +79 -0
  32. data/test/dummy/config/environments/test.rb +42 -0
  33. data/test/dummy/config/initializers/assets.rb +11 -0
  34. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  36. data/test/dummy/config/initializers/env_vars.rb +2 -0
  37. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/test/dummy/config/initializers/inflections.rb +16 -0
  39. data/test/dummy/config/initializers/mime_types.rb +4 -0
  40. data/test/dummy/config/initializers/session_store.rb +3 -0
  41. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/test/dummy/config/locales/en.yml +23 -0
  43. data/test/dummy/config/routes.rb +56 -0
  44. data/test/dummy/config/secrets.yml +22 -0
  45. data/test/dummy/db/test.sqlite3 +0 -0
  46. data/test/dummy/log/test.log +423 -0
  47. data/test/dummy/public/404.html +67 -0
  48. data/test/dummy/public/422.html +67 -0
  49. data/test/dummy/public/500.html +66 -0
  50. data/test/dummy/public/favicon.ico +0 -0
  51. data/test/finviz_rails_test.rb +15 -0
  52. data/test/test_helper.rb +20 -0
  53. metadata +190 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0874b31646a0e5a6c49aa9bbeb9686a26d9fa511
4
+ data.tar.gz: 63ccebdadb48d0a0c5b53fc91d648aa9642e3abc
5
+ SHA512:
6
+ metadata.gz: 5013eeab0c5f1331857876c60a93c0ad3ed5d568fc0df060884beb7d33067e1d1689ecebb4e148d7945b9ccd28e8365a3d8d63540ccdefa99ae921d96ddb0b23
7
+ data.tar.gz: 11bf44aeb1d002518a05d14b615deebdbc5614f64e0e135fbb05cff9a113456d39a5252561ea4a38d03a82bfe5a43ce936218eebc05ee0c8320dbe4b73e8e9d1
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Nemrow
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ = FinvizRails
2
+
3
+ gem 'finviz_rails', git: 'https://github.com/nemrow/finviz_rails.git'
4
+
5
+
6
+ == To Use
7
+
8
+ without finviz authentication (not finviz elite)
9
+
10
+ finviz = Finviz.new
11
+
12
+ with authentication (using finviz elite)
13
+
14
+ finviz = Finviz.new(finviz_username, finviz_password)
15
+
16
+ // You can also set FINVIZ_USERNAME and FINVIZ_PASSWORD
17
+ // environment variables instead
18
+
19
+ to screen stocks
20
+
21
+ finviz.screener(
22
+ pattern: "wedge",
23
+ order: "-change",
24
+ signal: "toplosers",
25
+ max_stock_count: 20
26
+ )
27
+
28
+ avialable options:
29
+
30
+ price_range, analyst_recommendation, volatility,
31
+ average_volume, pattern, country, current_volume
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'FinvizRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,10 @@
1
+ require "finviz_rails/finviz"
2
+ require "finviz_rails/login"
3
+ require "finviz_rails/screener"
4
+ require "finviz_rails/url_formatter"
5
+ require "finviz_rails/filter_mapper"
6
+ require "finviz_rails/indices"
7
+
8
+ module FinvizRails
9
+ end
10
+
@@ -0,0 +1,41 @@
1
+ class FilterMapper
2
+ def initialize(*params)
3
+ @params = params.first
4
+ @param_components = []
5
+ end
6
+
7
+ def run
8
+ mapper.each do |key, value|
9
+ @param_components << "#{value}_#{@params[key]}" if @params[key]
10
+ end
11
+ @param_components.join(",")
12
+ end
13
+
14
+ private
15
+
16
+ def mapper
17
+ {
18
+ price_range: "sh_price",
19
+ analyst_recommendation: "an_recom",
20
+ volatility: "ta_volatility",
21
+ average_volume: "sh_avgvol",
22
+ pattern: "ta_pattern",
23
+ country: "geo",
24
+ current_volume: "sh_curvol",
25
+ simple_moving_average_20_day: "ta_sma20",
26
+ simple_moving_average_50_day: "ta_sma50",
27
+ simple_moving_average_200_day: "ta_sma200",
28
+ current_ratio: "fa_curratio",
29
+ performance: "ta_perf",
30
+ sector: "sec",
31
+ insider_ownership: "sh_insiderown",
32
+ industry: "ind",
33
+ insider_transactions: "sh_insidertrans",
34
+ change: "ta_change",
35
+ institutional_ownership: "sh_instown",
36
+ gap: "ta_gap",
37
+ change_from_open: "ta_changeopen",
38
+ candlestick: "ta_candlestick"
39
+ }
40
+ end
41
+ end
@@ -0,0 +1,34 @@
1
+ require 'httparty'
2
+ require 'mechanize'
3
+
4
+ class Finviz
5
+ include HTTParty
6
+
7
+ attr_accessor :authenticated
8
+
9
+ def initialize(username=nil, password=nil)
10
+ @username = ENV['FINVIZ_USERNAME'] || username
11
+ @password = ENV['FINVIZ_PASSWORD'] || password
12
+ login if @username && @password
13
+ end
14
+
15
+ def screener(params)
16
+ Screener.new(agent, @authenticated, params).run
17
+ end
18
+
19
+ def indices
20
+ Indices.new(agent, @authenticated).run
21
+ end
22
+
23
+ private
24
+
25
+
26
+ def login
27
+ @authenticated = true
28
+ Login.new(@username, @password, agent).run
29
+ end
30
+
31
+ def agent
32
+ @agent ||= Mechanize.new
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ class Indices
2
+ def initialize(agent, auth, *params)
3
+ @agent = agent
4
+ @auth = auth
5
+ @results = {}
6
+ end
7
+
8
+ def run
9
+ @agent.get(url)
10
+ matches.map do |match|
11
+ indice = get_indice(match.first)
12
+ percent_change = get_percent(match.first)
13
+ @results[indice] = percent_change
14
+ end
15
+ @results
16
+ end
17
+
18
+ private
19
+
20
+ def get_indice(string)
21
+ string.match(/^"(.+?)"/)[1]
22
+ end
23
+
24
+ def get_percent(string)
25
+ string.match(/change":(\d*\.?\d*)/)[1].to_f
26
+ end
27
+
28
+ def url
29
+ "http://#{'elite.' if @auth}finviz.com/futures_charts.ashx?t=INDICES&p=d1"
30
+ end
31
+
32
+ def matches
33
+ @agent.page.content.scan /{"label":(.+?),"sparkline"/
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ class Login
2
+ def initialize(username, password, agent)
3
+ @username = username
4
+ @password = password
5
+ @agent = agent
6
+ end
7
+
8
+ def run
9
+ go_to_login_page
10
+ form['email'] = @username
11
+ form['password'] = @password
12
+ form.submit
13
+ @agent
14
+ end
15
+
16
+ private
17
+
18
+ def go_to_login_page
19
+ @agent.get("http://finviz.com/login.ashx")
20
+ end
21
+
22
+ def form
23
+ @form ||= @agent.page.forms[1]
24
+ end
25
+ end
@@ -0,0 +1,74 @@
1
+ class Screener
2
+ def initialize(agent, auth, *params)
3
+ @agent = agent
4
+ @auth = auth
5
+ @params = params.first
6
+ @stocks = []
7
+ end
8
+
9
+ def run
10
+ @agent.get(url)
11
+ set_pagination_count
12
+ set_found_stock_count
13
+ add_stocks_from_page(@agent.page.parser)
14
+ if @pagination_count
15
+ (2..@pagination_count).each do |page_num|
16
+ break if max_stock_count && @stocks.count >= max_stock_count
17
+ @agent.get url(page_num)
18
+ add_stocks_from_page(@agent.page.parser)
19
+ end
20
+ end
21
+ results
22
+ end
23
+
24
+ private
25
+
26
+ def results
27
+ {
28
+ found_stock_count: @found_stock_count,
29
+ returned_stock_count: @stocks.count,
30
+ stocks: @stocks
31
+ }
32
+ end
33
+
34
+ def set_found_stock_count
35
+ @found_stock_count = begin
36
+ text = @agent.page.parser.css('.count-text').text
37
+ match = text.match /Total: (\d+)/
38
+ match[1]
39
+ end
40
+ end
41
+
42
+ def max_stock_count
43
+ @params[:max_stock_count]
44
+ end
45
+
46
+ def set_pagination_count
47
+ @pagination_count ||= begin
48
+ pages = @agent.page.parser.css('a.screener-pages')
49
+ pages.any? ? pages.last.text.to_i : nil
50
+ end
51
+ end
52
+
53
+ def add_stocks_from_page(html)
54
+ rows = html.css('#screener-content > table > tr:nth-child(4) tr')
55
+ headers = rows.shift
56
+ stocks = rows.map do |row|
57
+ stock = {}
58
+ row.css('td').each_with_index do |td, index|
59
+ header = format_header(headers.css('td')[index].text)
60
+ stock[header] = td.text
61
+ end
62
+ stock
63
+ end
64
+ @stocks.concat stocks
65
+ end
66
+
67
+ def format_header(header)
68
+ header.gsub('.','').gsub('/','_').gsub(' ','').downcase
69
+ end
70
+
71
+ def url(page=nil)
72
+ UrlFormatter.new(@auth, page, @params).run
73
+ end
74
+ end
@@ -0,0 +1,42 @@
1
+ class UrlFormatter
2
+ def initialize(auth, page, params)
3
+ @auth = auth
4
+ @page = page
5
+ @params = params
6
+ end
7
+
8
+ def run
9
+ if @page
10
+ stock_num = ((@page * 20) + 1) - 20
11
+ else
12
+ stock_num = 0
13
+ end
14
+ "#{url}&r=#{stock_num}"
15
+ end
16
+
17
+ private
18
+
19
+ def url
20
+ @url ||= [base_url, signal, filters, order, tickers].join("&")
21
+ end
22
+
23
+ def signal
24
+ "s=#{@params[:signal]}"
25
+ end
26
+
27
+ def filters
28
+ "f=#{FilterMapper.new(@params).run}"
29
+ end
30
+
31
+ def order
32
+ "o=#{@params[:order]}"
33
+ end
34
+
35
+ def tickers
36
+ "t=#{@params[:tickers].join(',')}"
37
+ end
38
+
39
+ def base_url
40
+ "http://#{'elite.' if @auth}finviz.com/screener.ashx?v=111"
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module FinvizRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :finviz_rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */