Profit and Loss Portal - Tour of Code


App.py

app.py is the starting point for the application

App.py holds all of the routing logic for the application. There are several things to note:

  1. @login_required
    @login_required    
    def home():    
    Adding the login annotation will force a user to log in before they reach a certain page.

  1. @admin_required
    @application.route('/adminpage', methods=['GET', 'POST'])    
    @admin_required    
    Adding the admin annotation will only allow users that are admins to access the specified route.

  1. @application.route
    @application.route('/index')
    Creates a new route, as shown above. This helps modularize and break up the application.

  1. Uploading files - /upload & /maketake-upload
    @application.route('/upload', methods=['GET', 'POST'])    
    def upload():    
    	if request.method == 'GET':    
    	...    
    Uploading files has to be divided into two parts - visiting the uploads page and submitting a file.Visiting the page sends a 'GET' request, which in turn sends the HTML template necessary.Similarly, sending a POST request indicates a file upload. Regular uploads (data) currently (May 20, 2016) use the same name as the upload name while maketake-uploads intelligently rename the files as described below under 'Maketake'

  1. Error Handling
    @application.errorhandler(404)    
    If a page / route is not found, the application serves a not-found page. This page can be modified under the 'templates' directory.

Models.py

Account
  • account_id - unique db identifier for the account
  • name - name on the account
  • initials - account initials (not actively used)
  • commission - commission cost for this account
User
  • user_id - unique db identifier for the user
  • name - name of the user
  • email - email of user (used as login username)
  • password - password used to access account
  • admin - flag indicating if they possess admin privileges
  • accounts - accounts that the user possesses / owns
Transaction
  • transaction_id - unique db identifier for the transaction
  • account_id - unique db identifier for account traded on
  • exchange_id - unique db identifier for exchange traded on
  • price - the sum of money paid for the transaction
  • units - the number of units bought in the transaction
  • sec_sym - the 'security symbol' the option trades under
  • settle - date for transaction completion
  • entry - date for transaction completion (unused)
  • trade - date for transaction completion (unused)
  • ticket_number - the ticket string generated by the exchange
  • buy_sell - flag indicating buying or selling the option
  • commission - amount paid in commission for this transaction
  • isPosition - flag indicating whether this transaction is an opening, closing, or regular transaction (explained in 'Parser')
Exchange - (unused)
  • exchange_id - unique db identifier for the exchange
  • symbol - exchange symbol
StockPosition
  • stock_position_id - unique db identifier for position
  • symbol - symbol the option trades under
  • date - the day this position keeps a record of
  • account_id - account db identifier involved in this position
  • all_transactions - transactions falling under this position
Example:

(January 11, 2015)
Transaction (Alphabet):
    1. Opening Transaction (0 units from previous days)
    2. Regular Transaction (1 unit bought)
    3. Regular Transaction (5 units bought)
    4. Closing Transaction (6 units bought)

(January 11, 2015)
Transaction (AMZN)
    1. Opening Transaction (0 units from previous days)
    2. Regular Transaction (1 unit sold)
    3. Closing Transaction (-1 units total)

(January 12, 2015)
Transaction (Alphabet):
    1. Opening Transaction (6 units bought) (January 11)
    2. Regular Transaction (3 units sold)
    3. Regular Transaction (2 units bought)
    4. Closing Transaction (5 units bought)

Requirements.txt

This file holds all the python dependencies for the app.

Install python dependencies (root directory):
> pip install -r requirements.txt

Testdata

Testdata holds all example data the team was given from Brian. Included are (2) sample data sets and (1) example maketake.


Db Scripts

  • db_create - handles the creation of our backend database
  • db_init - handles the setup of database tables
  • db_insert - inserts file data into the database on upload
    • Takes transaction data and tries to place it under the appropriate StockPosition object in the database. If the transaction impacts others (its transaction date comes before some already in the database) the affected data should self-correct and recalculate.

Parse Transactions

The parser's job is to take example input in FIX 4.2 format and extract relevant information for database insertion. To do this, it does a bunch of string processing and then sets attributes on its own 'Transaction' items. These objects are then returned to the caller (/uploads usually) and inserted into the databse (db_insert).

# Instantiate the transaction parser
my_parsed_data = parse_transactions(<account number>, <file text>)

Parse MakeTakes

The maketake parser's job is to take an example maketake file and build it row by row so that fees can be examined easily. There are typically only two steps needed to work with it:

# Instantiate the parser:
my_parser = MakeTakeParser()

# Parse the maketake
my_maketake = my_parser.parse_maketake(<filetext>)

# Lookup the value
my_maketake.lookup(<Option Symbol>, <buying (true) or selling (false)>

MakeTake Utility

The maketake utility's job is to handle the renaming of the maketake files when a new maketake is added.

Account Naming Scheme

<account_number>_<start_year><start_month><start_day>-<end_year><end_month><end_day>

Examples:

To indicate that a maketake is valid for a certain date range:

User  -> 15
Start -> (January 15, 2015)
End   -> (February 25, 2016)
Name  -> 15_20150115-20160225.txt

To indicate that a maketake doesn't end, simply specify no end date:

User  -> 1
Start -> (January 15, 2015)
End   -> (No ending)
Name  -> 1_20150115-.txt

Useful locations

templates

Holds all the HTML used to render the webpages. Note: because of the Jinja2 templating language that we used, there is a template parent called base.html that does the including of scripts, styles, and other resources for your standard html page

static

Holds all frontend code needed to run the templates. Importantly, this is also where the javascript that controls uploading is located (static/scripts)

uploads

Holds all the uploaded FIX 4.2 data. You can add data to it by way of the upload page in the main application.

maketake_uploads

Holds all the maketake data. Stores all data in the scheme discussed under 'Maketake Utility'