I’ve always had some interest in finance and investment (at least for the sake of offseting the ever growing inflation) and recently realized I need a space where I can easily keep track of my investments, calculate risk and easily monitor a bunch of KPI’s that I care about most. This motivated me to create a simple cli tool tailored to these specific needs. The tech setup for the project consists of SQLite database (easy to handle, lightweight, stored as a file, which is fine since I don’t need to make the DB public anyways), click python package to create the cli interface and SQLAlchemy ORM to manage DB changes easier. I started off with some very basic tables like trades table documenting infos such as name of instrument bought, buy/sell date, amount, price, why bought/why sold etc. but constantly got new ideas for parameters I’d like to have there and other tables I might need so ORM seemed like a very useful tool in this case. That was my first experience with ORM and SQLAlchemy in particular. Here is what my Class for the stock table looks like, for example:

And here is how you add a new entry when you’ve made a trade:
There is also a possibility to change your entry with the cli by using the edit-stock-entry command. You are prompted to specify which entry you want to edit and then you go through the input again and may change the prefilled initial values to the new ones if you want. It will then show you the differences between the initial version of the entry and the new one and ask if you want to update it. This comes especially handy if you make a journal entry before you close the trade, so that you can extend information on the sell price etc. afterwards.
Apart from this main table which is the trading journal itself I also have two supporting tables as of now: brokers and exemption. The stock table has a relationship with broker table through broker used, so broker table backpopulates the stock table. This was needed because I use several brokers and in Germany you have to divide your tax examption amount between your brokers yourself in the beginning of the year (you can also do it separately in your tax report at the end of the year but I find it way more tedious) and it’s important for me to know how much of it I’ve used up per broker. When I want to add a new broker to my list I use the “new-broker-entry” command that allows me to add a new broker to the db, here is a code snippet:
import datetime
from . import Broker
from app.cli_utils import get_number_from_cli, get_optional_string_from_cli, get_date_from_cli, get_required_string_from_cli, get_required_number_from_cli
from app.brokers.get import get_brokers
from app.db import session
def _check_if_broker_exists(name):
brokers = get_brokers(session)
existing_brokers = []
for broker in brokers:
if broker.name.casefold().__eq__(name.casefold()):
existing_brokers.append(broker.name)
return existing_brokers
def create_broker_from_cli():
name = get_required_string_from_cli('Broker you use')
while name.casefold() in _check_if_broker_exists(name):
print('This broker already exists')
name = get_required_string_from_cli('Broker you use')
else:
comment = get_optional_string_from_cli('Comment')
# calculated fields
date = datetime.datetime.today()
new_broker = Broker(
name=name,
date=date,
comment=comment,
)
return new_broker
You can also list your current entries in the cli with the list-entries command. It won’t show all the fields in detail, for that you’d still have to open the DB and check the table. I use DBeaver for that purpose and don’t have any complaints so far. The cli command is still helpful though if you need to know the id of the entry to edit, for example.
As I mentioned before there is also an exemption table that holds the exemption amounts I have with each broker. I can add new exemption and assign it to a broker with new-exemption-entry command. Then, I can check my exemption balance, that is exemption set for the year and the sum not used up yet for the year:

There is also stats command that gives you an overview of your overall profit, profit this year and this month (all without tax; however there is a calculated tax field in the instruments table, but there is no logic yet to incorporate the exemption).

My interest in trading led to taking part in 2019 TRADERS’ Camp organized by comdirect bank here in Germany (here a video), where I’ve learned a lot about risk management. One approach stood out especially to me and I am using it a lot in my trading routines, so I wanted to incorporate it in the app too. The tool helps you define the position size you are allowed to take based on your overall capital, risk capital, risk per position and buy price. The logic relies heavily on the ATR (Average True Range) parameter that helps define a stop loss, so that the accidental stop loss chance is minimal. The tool asks a couple of questions about the parameters of your trade needed to calculate the position size, sum and stop loss and gives it out as a pandas dataframe, e.g.:

Another helpful feature I wanted to have in the app was the abililty to see last 4 reported earnings of a company. As a beginning trader I was tempted to make my trades based on previous earnings reports and analysts’ predictions but I would argue it’s generally a bad idea (which I’ve learned the hard way) and here is even a small analysis I did to support my argument. The feature still comes in handy every once in a while though just to get a generall understanding of the company’s performance. I’ve used the free Alpha Vantage API to get the earnings numbers and here is what the output of the feature looks like:

In case you want to check all the project code, here is the link to GitHub repository. Overall it was a very fun project that definitely helped me learn a lot and is also very extendable - so stay tuned, maybe there will be new features coming in the future!