🏡 index : github.com/captn3m0/mf.captnemo.in.git

author Nemo <commits@captnemo.in> 2023-03-28 18:25:52.0 +05:30:00
committer Nemo <commits@captnemo.in> 2023-03-28 18:25:52.0 +05:30:00
commit
f5bbd5a707868bed4adafea5d385785f50ff42f4 [patch]
tree
deda00f231133db14dbce6c997f1938e646289d6
parent
5fd8303c4c5bacd2f29b3163ea496ce319c4346c
download
f5bbd5a707868bed4adafea5d385785f50ff42f4.tar.gz

Use historical-mf-data repo instead for NAV



Diff

 .gitignore       |  3 ++-
 Gemfile          |  1 +
 Gemfile.lock     |  4 ++++
 Makefile         |  8 --------
 README.md        |  5 +++--
 _config.yml      |  2 ++
 netlify.toml     |  6 ++++++
 _plugins/amfi.rb | 46 ++++++++++++++++++++++++++++++++++++----------
 8 files changed, 47 insertions(+), 28 deletions(-)

diff --git a/.gitignore b/.gitignore
index d462393..10f8515 100644
--- a/.gitignore
+++ a/.gitignore
@@ -1,6 +1,7 @@
_site
.sass-cache
.jekyll-cache
.jekyll-metadata
vendor
nav/IN*
nav/IN*
funds.db*
diff --git a/Gemfile b/Gemfile
index f29d139..95311dd 100644
--- a/Gemfile
+++ a/Gemfile
@@ -1,6 +1,7 @@
source "https://rubygems.org"
gem "jekyll", "~> 4.2.0"
gem "jekyll-theme-modernist", "~> 0.2"
gem "sqlite3", "~> 1.6.2", force_ruby_platform: true

# Windows and JRuby does not include zoneinfo files, so bundle the tzinfo-data gem
# and associated library.
diff --git a/Gemfile.lock b/Gemfile.lock
index 2750e04..ba5f5c1 100644
--- a/Gemfile.lock
+++ a/Gemfile.lock
@@ -47,6 +47,7 @@
      rb-fsevent (~> 0.10, >= 0.10.3)
      rb-inotify (~> 0.9, >= 0.9.10)
    mercenary (0.4.0)
    mini_portile2 (2.8.1)
    pathutil (0.16.2)
      forwardable-extended (~> 2.6)
    public_suffix (5.0.1)
@@ -58,6 +59,8 @@
    safe_yaml (1.0.5)
    sassc (2.4.0)
      ffi (~> 1.9)
    sqlite3 (1.6.2)
      mini_portile2 (~> 2.8.0)
    terminal-table (2.0.0)
      unicode-display_width (~> 1.1, >= 1.1.1)
    unicode-display_width (1.8.0)
@@ -70,6 +73,7 @@
DEPENDENCIES
  jekyll (~> 4.2.0)
  jekyll-theme-modernist (~> 0.2)
  sqlite3 (~> 1.6.2)
  tzinfo (~> 1.2)
  tzinfo-data
  wdm (~> 0.1.1)
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 917d9fd..0000000 100644
--- a/Makefile
+++ /dev/null
@@ -1,8 +1,0 @@
amfi:
	curl --retry 10 \
	--connect-timeout 30 \
	--retry-max-time 100 \
	--silent "https://www.amfiindia.com/spages/NAVAll.txt" | \
	grep ';' > "_data/nav.csv";

all: amfi
diff --git a/README.md b/README.md
index 63f9c65..7a39abe 100644
--- a/README.md
+++ a/README.md
@@ -17,12 +17,13 @@
This service uses data from 2 sources:

- Kuvera
- AMFI
- AMFI, via [Historical Mutual Fund Database](https://github.com/captn3m0/historical-mf-data)

## how

- A mapping between Kuvera Mutual Fund IDs and ISIN data is fetched from [kuvera-mutual-funds-lookup](https://github.com/captn3m0/kuvera-mutual-funds-lookup) as an automatically updating submodule.
- NAV data from AMFI is updated daily and saved in [_data/nav.csv](https://github.com/captn3m0/mf.captnemo.in/blob/main/_data/nav.csv)
- NAV data from AMFI is updated daily and published as a SQLite Database by https://github.com/captn3m0/historical-mf-data.
- This website takes the above, and publishes it via Netlify.

## license

diff --git a/_config.yml b/_config.yml
index f4a7526..068c567 100644
--- a/_config.yml
+++ a/_config.yml
@@ -8,6 +8,8 @@
github:
  repository_url: https://github.com/captn3m0/mf.captnemo.in
exclude:
  - funds.db
  - funds.db.zst
  - .sass-cache/
  - .jekyll-cache/
  - gemfiles/
diff --git a/netlify.toml b/netlify.toml
index 8b1407f..5622d38 100644
--- a/netlify.toml
+++ a/netlify.toml
@@ -1,5 +1,11 @@
[build]
  command = """
  wget https://github.com/captn3m0/historical-mf-data/releases/latest/download/funds.db.zst
  unzstd funds.db.zst
  echo 'CREATE INDEX "date" ON "nav" ("date","scheme_code")' | sqlite3 funds.db
  echo 'CREATE INDEX "nav-scheme" ON "nav" ("scheme_code")' | sqlite3 funds.db
  echo 'CREATE INDEX "securities-scheme" ON "securities" ("scheme_code")' | sqlite3 funds.db
  echo 'CREATE INDEX "securities-isin" ON "securities" ("isin")' | sqlite3 funds.db
  jekyll build
  """
  publish = "_site"
diff --git a/_plugins/amfi.rb b/_plugins/amfi.rb
index cd372ac..c5dc7a1 100644
--- a/_plugins/amfi.rb
+++ a/_plugins/amfi.rb
@@ -1,25 +1,37 @@
require 'csv'
require 'json'
require 'sqlite3'

# Open database for reading only
DB = SQLite3::Database.new('funds.db', :readonly => true)

def get_historical_nav(isin)
  DB.execute("SELECT date, nav from nav_by_isin WHERE isin='#{isin}' ORDER BY date")
end

def get_latest_nav(isin)
  DB.execute("SELECT nav,date from nav_by_isin WHERE isin='#{isin}' ORDER BY date DESC LIMIT 1")[0]
end

def get_mutual_fund_schemes
  DB.execute("SELECT isin,scheme_name as name,S1.scheme_code FROM securities S1
    LEFT JOIN schemes S2 ON S1.scheme_code = S2.scheme_code")
end

Jekyll::Hooks.register :site, :after_init do |site|
  data = CSV.read('_data/nav.csv', col_sep: ';', headers: ['code', 'isin', 'reinvestment_isin', 'name', 'nav', 'date'])[1..]
  data.each do |nav_data|
    date = Date.parse(nav_data['date']).strftime('%Y-%m-%d')
  get_mutual_fund_schemes.each do |isin, scheme_name|
    puts "[+] #{isin}, #{scheme_name}"
    nav = get_latest_nav(isin)

    data = {
      'ISIN'=> nav_data['isin'],
      'name'=> nav_data['name'],
      'nav' => nav_data['nav'].to_f,
      'date'=> date
      'ISIN'=> isin,
      'name'=> scheme_name,
    }
    File.write("nav/#{data['ISIN']}", data.to_json) if data['ISIN'].length == 12
    if nav_data['reinvestment_isin'].length == 12
      data = {
        'ISIN'=> nav_data['reinvestment_isin'],
        'name'=> nav_data['name'] + " (Reinvestment)",
        'nav' => nav_data['nav'],
        'date'=> date
      }
      File.write("nav/#{data['ISIN']}", data.to_json)
    if nav
      data['nav'] = nav[0]
      data['date']= nav[1]
    end

    data['historical_nav'] = get_historical_nav(data['ISIN'])
    File.write("nav/#{data['ISIN']}", data.to_json) 
  end
end