Skip to content

Commit

Permalink
Added IBJoinHistoricalSeries.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobust committed Mar 29, 2015
1 parent f44b81f commit 53dd652
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 5 additions & 3 deletions IBCombineHistoricalData.py
Expand Up @@ -27,8 +27,8 @@ def parseLine(line):
return dateStr.strip(), priceStr.strip(), closePriceStr.strip()

if __name__ == "__main__":
dataPath = "C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/CAD/BID/"
outFile = "C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/CAD_BID.txt"
dataPath = "C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/CAD/ASK/"
outFile = "C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/CAD_ASK.txt"

files = os.listdir(dataPath)
files.sort(key=tokenize) #this allows files to be sorted in natural order eg 1.txt, 2.txt ... 10.txt instead of 1.txt 10.txt 2.txt etc
Expand All @@ -43,6 +43,8 @@ def parseLine(line):
lines = f.readlines()
for line in lines:
date, time, closePrice = parseLine(line)
out.write(date + ", " + time + ", " + closePrice + "\n")
time = time.replace(":", "")
datetime = str(long(date + time))
out.write(datetime + ", " + closePrice + "\n")

out.close()
4 changes: 2 additions & 2 deletions IBDownloadHistoricalData.java
Expand Up @@ -54,11 +54,11 @@ class IBDownloadHistoricalData implements EWrapper {
private FileLog mServerErrorsLog = new FileLog("ServerErrors.txt");

//CHANGE this to the desired path
private String mDataPath = "Data/CAD/BID/";
private String mDataPath = "Data/JPY/BID/";
//CHANGE this to the desired contract
private Contract mContract = new Contract(0, "USD", "CASH", "",
0, "", "",
"IDEALPRO", "CAD", "", "",
"IDEALPRO", "JPY", "", "",
new Vector<ComboLeg>(), "IDEALPRO", false,
"", "");
//CHANGE this between BID and ASK to get different fields
Expand Down
28 changes: 28 additions & 0 deletions IBJoinHistoricalSeries.py
@@ -0,0 +1,28 @@
import numpy as np
import numpy.lib.recfunctions as rfn
import pandas as pd

#The logic in this file takes many the data files generated by IBCombineHistoricalData
#"joins" them by datetime to create a file with one date-time column and numerous data columns
if __name__ == "__main__":
#MANUALLY SET THESE FILES FOR JOINING
filesToJoin = [
"C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/CAD_BID.txt",
"C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/CAD_ASK.txt"
]
#MANUALLY SET THIS FOR OUTPUT FILE
outFile = "C:/Dropbox/CninSrc/JTS/TWS API/samples/Java/Data/JOINED.txt"
#MANUALLY SET THIS HEADER SO EASIER TO REMEMBER COLUMNS
headerTxt = "Time,CAD_BID,CAD_ASK"

joined = []
for file in filesToJoin:
print "Handing: " + file
data = np.genfromtxt(file, delimiter = ',', dtype=np.dtype([('time',np.long), ('price', np.float)]))
print "Joining"
if len(joined) == 0:
joined = data
else:
joined = rfn.join_by('time', joined, data, jointype='inner', usemask=False)

np.savetxt(outFile, joined, delimiter=',', fmt="%s", header=headerTxt, comments="")

0 comments on commit 53dd652

Please sign in to comment.