Given a list of information about fills for different stocks, calculate the total profit or loss accrued for each symbol.
The attached text file contains the following columns:
- time: Date and time of fill
- symbol: Symbol of product
- price: Price of fill
- buy_sell: Side of fill
- qty: Qty of fill
Write a python script that calculates total PNL per symbol. You may ignore any trading costs (just calculate Gross PNL). The output should be a csv with just two columns: symbol, total_pnl, and one row per symbol.
Important: You may assume/confirm that positions are flat after totaling all buys and sells (both on a per-symbol basis, and subsequently overall). In other words, you only need to worry about realized PNL – there should be no open positions with unrealized pnl left to mark to any particular price.
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.# open the data file to read
datafile = open("teza_fills.csv", "r")
# skip first line with headers
# read the data in datafile and calculate PNL for each symbol of product
pnl = dict() # a dictionary to store PNLs
for line in datafile:
# split the line by commas
tokens = line.strip().split(",")
# obtain details of the entry
symbol = tokens
price = float(tokens)
buy_sell = tokens
qty = int(tokens...