Skip to content

Instantly share code, notes, and snippets.

@maronavenue
Last active January 19, 2024 13:08
Show Gist options
  • Save maronavenue/91cd721f2dccda1da4206c1756cc76f2 to your computer and use it in GitHub Desktop.
Save maronavenue/91cd721f2dccda1da4206c1756cc76f2 to your computer and use it in GitHub Desktop.
YBLL: FactSet Workshop #1
import argparse
def main():
# Instantiate the parser
parser = argparse.ArgumentParser(description='Retrieves a given datapoint from input PSE:TICKER. Supports timeseries data.')
# This is an example of configuring positional input arguments to your script
parser.add_argument('ticker', type=str, help='PSE Ticker')
parser.add_argument('field', type=str, help='Fieldname')
# This makes it so that this is an optional argument
parser.add_argument('date', type=str, nargs='?', help='YYYYMMDD date input for timeseries datapoints.')
# We start parsing the user input arguments and store them in a dict-like structure
args = parser.parse_args()
print("Ticker: {}".format(args.ticker))
print(f"Fieldname: {args.field}")
if args.date:
print(f"Date: {args.date}")
else:
print("No date supplied.")
# We are just printing stuff for now...
# You may add extra validations to ensure inputs are correct such as enforcing the date format, etc.
# The next logical problem to solve is parsing the CSV file. Let's try to solve this problem first. :-)
# CODE GOES HERE...
if __name__ == "__main__":
main()
import argparse
import csv
def extract_sector_and_subsector(input_string: str) -> (str, str):
"""Extracts the sector and subsector from the input string."""
sector, subsector = input_string.split("|")
return sector, subsector
def main():
# Instantiate the parser
parser = argparse.ArgumentParser(description='Retrieves a given datapoint from input PSE:TICKER. Supports timeseries data.')
# This is an example of configuring positional input arguments to your script
parser.add_argument('ticker', type=str, help='PSE Ticker')
parser.add_argument('field', type=str, help='Fieldname')
# This makes it so that this is an optional argument
parser.add_argument('date', type=str, nargs='?', help='YYYYMMDD date input for timeseries datapoints.')
# We start parsing the user input arguments and store them in a dict-like structure
args = parser.parse_args()
# Let's use a dict data structure to store our data in-memory
# Remember though that in a real-world scenario, you would want to use a database
# to store your data. This is just a simple example.
data = {}
# Now, let's open the CSV file and read the data into our data structure
# Assume the filename is hardcoded for this exercise
with open('pse_data_01052024.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
for index, row in enumerate(reader):
# Condition # 1. Skip header row
if index == 0:
continue
# Condition # 2. Skip rows that start with "<" because we don't need them
if row[0].startswith("<"):
continue
# Now we start extracting the elements for every data row
key = row[0]
raw_date = row[1]
company_name = row[2]
company_price = row[3]
company_shares = float(row[4])
net_foreign = row[5]
sector_subsector = row[6]
if key not in data:
data[key] = {}
data[key]["NAME"] = company_name
# This is an example of unpacking a Tuple with 2 elements, we directly store them in our dict
data[key]["SECTOR"], data[key]["SUBSECTOR"] = extract_sector_and_subsector(sector_subsector)
# At this point, we're able to store facts or non-timeseries data into our dict.
# Now, try storing timeseries data:
# You would need a second key to hold the date, and then a third key to hold the fieldname.
# Example:
# data[key][date]["PRICE"] = *insert variable or value here*
# data[key][date]["SHARES"] = *insert variable or value here*
# Make sure date is in YYYYMMDD format!
# You would also need to convert net foreign from USD to PHP.
# This is where you can use the API I shared to get the exchange rate.
# Try creating a function to convert it for you. :)
result = ''
if args.date:
result = data[args.ticker][args.date][args.field]
else:
result = data[args.ticker][args.field]
print(result)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment