#!/usr/bin/python
# Python version 2.7.6
import datetime
import time
def timer(label):
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
timer_out = open("timer.txt",'a')
timer_out.write(label + ": " + st + "\n")
timer_out.close()
timer("Start")
def format_currency(value):
if value < 0:
results = '${:.2f}'.format(value)
value = "(" + results + ")"
return value
else:
return '${:.2f}'.format(value)
csv_in = "TransactionHistory_22523594.csv"
csv_out = "transaction_history.csv"
header = "Row,Date,Buy/Sell,QTY,Security,Price,Debit,"\
"Credit,Commission,Total Amount,Currency" + '\n'
footer_commission = 0
footer_debit = 0
footer_credit = 0
footer_total_amount = 0
counter = 1
split = ","
join = ","
row_out = ""
def write_file(row,write_to):
f_out = open(write_to,'a')
f_out.write(row + "\n")
f_out.close()
write_file(header,csv_out)
for line in reversed(list(open(csv_in))):
if len(line.strip()) != 0 :
line = line.strip()
column = line.split(split)
if column[2] == "Buy" or column[2] == "Sell" or column[2] == "Expired":
row_counter = '{0:03d}'.format(counter)
transaction_date = column[0]
buy_sell = column[2]
qty = column[5]
security = column[4]
price = column[6]
if price == "":
price = "0"
total_amount = column[8]
currency = column[9]
transaction_date = (datetime.datetime.strptime\
(transaction_date, "%Y-%m-%d").strftime("%a %b %d"))
qty = abs(int(qty))
price = float(price)
total_amount = float(total_amount)
amount = qty * price * 100
amount = int(amount)
abs_total_amount = abs(float(total_amount))
if column[8] >= "0" and column[8] <= "1":
commission = 0
else:
commission = abs(abs_total_amount - amount)
if total_amount <= 1:
debit = amount
credit = 0
else:
debit = 0
credit = amount
if debit > abs_total_amount:
credit = debit
commission = debit
debit = 0
footer_debit = (footer_debit + debit)
footer_credit = (footer_credit + credit)
footer_commission = (footer_commission + commission)
footer_total_amount = (footer_total_amount + total_amount)
qty = str(abs(qty))
price = format_currency(price)
debit = format_currency(debit)
credit = format_currency(credit)
if debit == "$0.00":
debit = ""
if credit == "$0.00":
credit = ""
commission = format_currency(commission)
if commission == "0":
commission = ""
total_amount = format_currency(total_amount)
counter = counter + 1
row = (row_counter + join + transaction_date + join + buy_sell + join \
+ qty + join + security + join + price + join + debit + join + credit + join \
+ commission + join + total_amount + join + currency + "\n")
print row_counter
row_out = row_out + row
pl = (footer_debit + footer_commission)
pl_percent = ((footer_credit - (pl)) / pl * 100)
pl_debit = format_currency(pl)
footer_debit = format_currency(footer_debit)
footer_credit = format_currency(footer_credit)
footer_commission = format_currency(footer_commission)
footer_total_amount = format_currency(footer_total_amount)
pl_percent = str(pl_percent)
join2x = (join + join)
join5x = (join + join + join + join + join)
footer = (join5x + "Subtotal" + join + footer_debit + join + footer_credit + join \
+ footer_commission + join + footer_total_amount + join + currency + "\n \n" \
+ join5x + join2x + "Total Debit" + join2x + pl_debit + join + currency + "\n" \
+ join5x + join2x + "Total Credit" + join2x + footer_credit + join + currency + "\n" \
+ join5x + join2x + "P/L" + join + pl_percent + " %" + join + footer_total_amount \
+ join + currency)
write_file(row_out,csv_out)
write_file(footer,csv_out)
timer("Finish")
input("\n\nPress the Enter key to exit.")