-
-
Save arthurpham/97af2ebb8b50fbc90052b9a2ca7f236e to your computer and use it in GitHub Desktop.
Interactive Brokers to Thinkorswim Trade Log Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import operator | |
if (len(sys.argv) == 3): | |
inputFileName = sys.argv[1] | |
outputFolder = sys.argv[2] | |
# open symbols file | |
tradelogFile = open(inputFileName,"rU") | |
month_names = {'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12} | |
expiry_dict = {} | |
# for each line in the log file | |
for line in tradelogFile: | |
# remove the line terminator if any | |
line = line.rstrip('\n') | |
tokens = line.split('|') | |
if tokens[0] == 'OPT_TRD': | |
transaction_id = tokens[1] | |
subtokens = tokens[3].split(' ') | |
underlying = subtokens[0] | |
expiry = subtokens[1] | |
# Reformat date for sorting expiry_key | |
year = expiry[5:7] | |
full_year = '20' + year | |
month = expiry[2:5] | |
month_num = str(month_names[expiry[2:5]]).zfill(2) | |
day = expiry[0:2] | |
key_expiry = full_year + month_num + day | |
strike = int(float(subtokens[2])) | |
if subtokens[3] == 'P': | |
callPut = 'PUT' | |
else: | |
callPut = 'CALL' | |
openClose = tokens[6] | |
transDate = tokens[7] | |
transTime = tokens[8] | |
lots = int(float(tokens[10])) | |
contract_size = int(float(tokens[11])) | |
fillPrice = tokens[12] | |
proceeds = tokens[13] | |
commission = tokens[14] | |
#print underlying,expiry,strike,callPut,openClose,transDate,transTime,lots,fillPrice,proceeds,commission | |
# Trades are grouped by a combination of underlying symbol and expiration date. | |
expiry_key = key_expiry + '_' + underlying | |
record = '' | |
if lots > 0: | |
record = record + 'BUY ' | |
else: | |
record = record + 'SELL ' | |
record = record + str(lots) + ' ' + underlying + ' ' + str(contract_size) + ' ' + day + ' ' + month + ' ' + year + ' ' + str(strike) + ' ' + callPut + \ | |
' @' + fillPrice + ' LMT FIXED' | |
# If this expiry entry isn't in the map yet then create it as a blank map | |
if expiry_key not in expiry_dict: | |
expiry_dict[expiry_key] = {} | |
# transaction key needs to be unique and date/time is insufficient so append transaction_id | |
# transaction_id is sequential in time *except* for expired options which have their own separate starting point | |
transactionTimestamp = transDate + transTime + transaction_id | |
expiry_dict[expiry_key][transactionTimestamp] = record | |
for key1,value1 in expiry_dict.iteritems(): | |
outFileName = outputFolder + "/" + key1 + ".txt" | |
outputFile = open(outFileName,"w") | |
sortedRecords = sorted(value1.items(), key=operator.itemgetter(0), reverse=False) | |
for trade in sortedRecords: | |
outputFile.write(trade[1] + '\r') | |
outputFile.close() | |
tradelogFile.close() | |
else: | |
print "wrong number of aruments" | |
print "example:" | |
print "python tradelog-analyzer.py input_file.txt output_folder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment