Skip to content

Commit

Permalink
added a script to grab symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhky committed Jan 18, 2024
1 parent fe438b1 commit a318f6c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ publish/

# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
# in these script will be unencrypted
PublishScripts/

# NuGet Packages
Expand Down
49 changes: 49 additions & 0 deletions script/retrieve_stock_symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python

from argparse import ArgumentParser
import os

import pandas as pd
from finsim.data.finnhub import FinnHubStockReader


def get_argparser():
argparser = ArgumentParser(description='Retrieve stock symbols from Finnhub')
argparser.add_argument('outputjson', help='path of the stock symbols (*.json)')
argparser.add_argument('--finhubbtokenpath', help='path of Finnhub tokens')
argparser.add_argument('--useenvtoken', help='Use the environment variable FINNHUBTOKEN as the tokens')
argparser.add_argument('--shorten', default=False, action='store_true', help='shorten list of symbols')
return argparser


if __name__ == '__main__':
# parsing argument
args = get_argparser().parse_args()

# check if the output directory exists
dirname = os.path.dirname(args.outputjson)
if not os.path.isdir(dirname):
raise FileNotFoundError('Directory {} does not exist!'.format(dirname))

# get Finnhub tokens
if args.useenvtoken:
finnhub_token = os.getenv('FINNHUBTOKEN')
if finnhub_token is None:
raise ValueError('Finnhub tokens not found in the environment variable $FINNHUBTOKEN.')
else:
finnhub_token = open('finnhub.tokens', 'r').read().strip()

# initialize FinnHub reader
finnreader = FinnHubStockReader(finnhub_token)

# grab symbols
allsym = finnreader.get_all_US_symbols()
allsymdf = pd.DataFrame(allsym)

if args.shorten:
filtered_symdf = allsymdf[allsymdf['mic'].isin(['XNAS', 'XNYS', 'ARCX'])]
filtered_symdf = filtered_symdf[~filtered_symdf['type'].isin(['PUBLIC'])]
filtered_symdf = filtered_symdf[~filtered_symdf['symbol'].str.contains('\.')]
filtered_symdf.to_json(args.outputjson, orient='records')
else:
allsymdf.to_json(args.outputjson, orient='records')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def package_description():
tests_require=[
'unittest2'
],
# scripts=[],
script=['script/retrieve_stock_symbols'],
include_package_data=True,
ext_modules=ext_modules,
test_suite="test",
Expand Down

0 comments on commit a318f6c

Please sign in to comment.