Feb-02-2018, 09:06 PM
headline ... How do i run this code
I read the thread
[Basic] How to execute python code
although helpful.. im still not sure how i run this?
I have python 2.7 installed and here is the script
I have some instructions from the creator but i don't want to be rude and pester the guy...
I have opened the terminal and tried "python ibtos.py tradelog.txt tostrades"
I have tried renameing the files to the names in the script..
Im sorry if this is painfully obvious of what i should do....
I am planning on learning Python but obviously i havent gotten there yet....
Thanks for any help in advance
I read the thread
[Basic] How to execute python code
although helpful.. im still not sure how i run this?
I have python 2.7 installed and here is the script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
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" |
I have opened the terminal and tried "python ibtos.py tradelog.txt tostrades"
I have tried renameing the files to the names in the script..
Im sorry if this is painfully obvious of what i should do....
I am planning on learning Python but obviously i havent gotten there yet....
Thanks for any help in advance