concisely known as Analysis that is super Superb, calculated using Stock and cash Entry Trackers (ASSET)
[API link]: https://asset-calculator.herokuapp.com/
ASSET is an API used for storing, tracking and analyzing personal financial assets. Based off my own personal need to get my messy finances and impulsive stock purchases in order, ASSET stores a user's cash and financial market assets in its database, and can be used to conduct real time analytics both across time periods and at any particular instant in the user's financial history.
ASSET was created in Python using Django REST framework, connected to a MongoDB database and deployed using Heroku. Real time financial data is scraped from yahoo finance using Python's YFinance library. Analytics code can be found in the module nwtracker.
ASSET can currently conduct analytics across time for any two dates - the notable ones include:
- Raw and percent total growth
- Raw and percent portfolio growth
- Raw Profit and Loss
- Raw Non Market Growth (ie raw total growth - raw profit and loss)
ASSET will store any stock transactions and cash entries that a user logs, and use these for analysis. ASSET uses token-based user authentication.
ASSET API works by reading the data field of any HTTP requests, and returning info in the data fields. All data fields are in JSON.
To begin, create a new account with a unique username. This will create a user in the database and return a token which is required for all activities associated with the user.
API Endpoint: /signup HTTP Method: POST Example data:
{
"username": "myusername",
"password": "hopefullystrongpassword"
}
Example response data:
{
"token":"9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b"
}
Now that we have our authentication token, I can begin to populate my profile with all my GME diamong hand longs. Remember to add the token as a string "Token " + <my_token>
with the token obtained from before in the http authorization header, or else the request will fail.
API Endpoint: /stocktrxn/
HTTP Method: POST
HTTP Authorization header = "Token <my_token>"
Example data:
{
"date": "2022-01-01", // yyyy-mm-dd
"ticker": "BABA", // must be valid ticker found in yahoo finance
"quantity": 100,
"price": 101,
"type": "b" // must either be 'b' or 's' for buy or sell
}
Expected response code: 200 OK
We do the same for cash entries. Note that as a design choice, unlike stock transactions, cash entries is simply the total balance of cash the user has at any date, for the currencies indicated.
API Endpoint: /cashentries/
HTTP Method: POST
HTTP Authorization header = "Token <my_token>"
Example data:
{
"date": "2022-01-01",
"single_entries": [
{
"currency": "USD",
"quantity": 1000
},
{
"currency": "SGD",
"quantity": 2000
},
{
"currency": "HKD",
"quantity": 3000
}
]
}
Expected response code: 200 OK
Now that we have populated our profile with transactions and cash, we can ping the API to conduct some analysis. Excitinggg.
API Endpoint: /acrosstime/
HTTP Method: POST
HTTP Authorization header = "Token <my_token>"
Example data:
{
"date_bef":"2022-1-1", // yyyy-mm-dd
"date_aft":"2022-1-2", // yyyy-mm-dd, must be after date_bef
"currency":"HKD" // currency used to calculate raw growth
}
Expected data:
{
"raw_total-growth": 1000,
"percent_total_growth": 80.2,
"raw_portfolio_growth": 800.4,
"raw_profit_and_loss": 67.4,
"raw_non_market_growth": 703
}
Provides a list of all IDs of all stock transactions and cash entries associated with the user
Request provides username and password of previous user, response provides authentication token.
Required data fields:
- Username
- Password
Request provides username and password to create user for the first time, response provides authentication token.
Required data fields:
- Username
- Password
Get a detailed list of all associated stock transactions with the user
Add a new stock transaction. Required data fields:
{
"date" //"yyyy-mm-dd"
"ticker",
"quantity",
"price",
"type", // must either be 'b' or 's' for buy or sell
}
Deletes the transaction with id=<id>
, if it is owned by the user currently logged in
Updates the transaction with id=<id>
using the same format as posting a stock transaction, if it is owned by the user currently logged in
Get a detailed list of all associated cash entries with the user
Add a new cash entry.
Required fields:
{
"date" // "yyyy-mm-dd"
"single_entries": [
{
"currency",
"quantity",
}
]
}
Deletes the cash entry with id=<id>
, if it is owned by the user currently logged in
Updates the cash entry with id=<id>
using the same format as posting a cash entry, if it is owned by the user currently logged in
Get analysis on financial performance between any two dates, and a given currency. Currently, the analytics fields supported are:
- Raw and percent total growth
- Raw and percent portfolio growth
- Raw Profit and Loss
- Raw Non Market Growth (ie raw total growth - raw profit and loss)
Required fields:
{
"date_bef" // yyyy-mm-dd
"date_aft" // yyyy-mm-dd, must be after date_bef
"currency"// Format: "XXX" e.g. HKD, SGD, USD
}
In order of priority
- Streamline yfinance scraping process, as this is currently slowing down analytics since we scrape yfinance every time we conduct analytics. The current plan is to store a local copy of historical stock data and only scrape from yfinance when a user needs real-time data or the historical data needs updating (ie we moved on to the next day).
- Further analytics a. Analytics on portfolios at any instant in time e.g. total stocks at any point in time, breakdown by industry, average P/E values etc
- Create a front-end facing application for users to interact with on browser / mobile
Why Django REST framework? I chose it for its quick pipeline and simplicity, and its large library of in-built support for testing. I was also working with yfinance module in python beforehand and Django was a good choice for creating a back-end app in python.
MIT