def testVWAP(self): taqBaseDir = "/Users/leonmaclin/Documents/sampleTAQ/" filePathName = "trades/20070620/IBM_trades.binRT" start930 = 19 * 60 * 60 * 1000 / 2 end4 = 16 * 60 * 60 * 1000 vwap = VWAP(TAQTradesReader(taqBaseDir + filePathName), start930, end4) print("There were %d trades and a VWAP price of %f" % (vwap.getN(), vwap.getVWAP()))
def testName(self): startTS = 18 * 60 * 60 * 1000 / 2 # 930AM endTS = 16 * 60 * 60 * 1000 # 4PM numBuckets = 2 baseDir = '/Users/leonmaclin/Documents/sampleTAQ/' fileName = 'trades/20070620/IBM_trades.binRT' data = TAQTradesReader(baseDir + fileName) returnBuckets = ReturnBuckets(data, startTS, endTS, numBuckets) self.assertTrue(returnBuckets.getN() == 2)
def getTradesFile( self, dateString, # eg "20070620", a string ticker): filePathName = self._baseDir + "trades/" + dateString + "/" + ticker + "_trades.binRT" if (not (os.path.exists(filePathName))): raise Exception("File %s doesn't exist" % filePathName) if (not (access(filePathName, R_OK))): raise Exception("You don't have access to %s" % filePathName) return TAQTradesReader(filePathName)
def test_classifyAll(self): filePathName = "/Users/leonmaclin/Documents/sampleTAQ/trades/20070620/IBM_trades.binRT" data = TAQTradesReader(filePathName) tickTest = TickTest() startOfDay = 18 * 60 * 60 * 1000 / 2 endOfDay = startOfDay + ( 13 * 60 * 60 * 1000 / 2 ) classifications = tickTest.classifyAll( data, startOfDay, endOfDay ) # To see the first 10 prices, enable Window > Show view > Expressions # In the Expressions window, paste: list(map(lambda i: data.getPrice(i), range( 0, 10 ))) self.assertTrue( data.getPrice(1) < data.getPrice(0) ) # Each classification is a tuple containing timestamp, price, and classification self.assertTrue( classifications[ 0 ][ 2 ] == 0 ) # 1st good price has a classification of 0 self.assertTrue( classifications[ 1 ][ 2 ] == -1 ) # 2nd good price has a classification of -1 # We now look at the back of the queue self.assertTrue( classifications[-4][1] < classifications[-3][1] ) # 4th last price is less than 3rd last price self.assertAlmostEqual( classifications[-3][1], classifications[-2][1], 0.00001 ) # 3rd last price and 2nd last price are the same self.assertAlmostEqual( classifications[-2][1], classifications[-1][1], 0.00001 ) # 2nd last price and last price are the same self.assertTrue( classifications[-3][2] == 1 ) self.assertTrue( classifications[-2][2] == 1 ) self.assertTrue( classifications[-1][2] == 1 )
def testConstructor(self): startTS = None endTS = None numBuckets = 2 baseDir = "/Users/leonmaclin/Documents/sampleTAQ/" fileName = "trades/20070620/IBM_trades.binRT" data = TAQTradesReader( baseDir + fileName ) fpb = FirstPriceBuckets( data, numBuckets, startTS, endTS ) self.assertTrue( fpb.getN() == 2 ) self.assertTrue( fpb.getTimestamp( 0 ) == 34241000 and fpb.getTimestamp( 1 ) == 45901000 ) self.assertTrue( fpb.getPrice( 0 ) > ( 106.5 - 0.0001 ) and fpb.getPrice( 0 ) < ( 106.5 + 0.0001 ) ) self.assertTrue( fpb.getPrice( 1 ) > ( 106.73999786376953 - 0.0001 ) and fpb.getPrice( 1 ) < ( 106.73999786376953 + 0.0001 ) )
def testConstructor(self): startTS = None endTS = None numBuckets = 2 baseDir = '/Users/leonmaclin/Documents/sampleTAQ/' fileName = 'trades/20070620/IBM_trades.binRT' data = TAQTradesReader(baseDir + fileName) fpb = LastPriceBuckets(data, numBuckets, startTS, endTS) self.assertTrue(fpb.getN() == 2) self.assertTrue( fpb.getTimestamp(0) == 45896000 and fpb.getTimestamp(1) == 57599000) self.assertTrue( fpb.getPrice(0) > (106.72000122070312 - 0.0001) and fpb.getPrice(0) < (106.72000122070312 + 0.0001)) self.assertTrue( fpb.getPrice(1) > (106.0 - 0.0001) and fpb.getPrice(1) < (106.0 + 0.0001)) print("Finished")
def test1(self): dirc = '/Users/Zibin/NYU/twriter.gz' w = TradeWriter(dirc, 1) # Create a .gz file on directory r = TAQTradesReader('/Users/Zibin/Documents/R/trades/20070620/NVDA_trades.binRT') # Read a file # Using tested readers, test for expected values baseTS = r.getSecsFromEpocToMidn() * 1000 ts = baseTS + r.getMillisFromMidn(0) s = r.getSize(0) p = r.getPrice(0) w.writer(r,1,1) # write to the file b = BinReader(dirc, '>QIIf', 100) # use binreader to read ts0, _, size0, p0 = b.next() b.close() self.assertEquals( ts0, ts ) # Check if time stamps are equal self.assertEquals( size0, s ) # Check if numbers of shares are equal self.assertAlmostEquals( p0, p ) # Check if prices are equal
def testXMinuteReturns(self): taqBaseDir = "/Users/canelbiryol/Documents/sampleTAQ/" filePathName = "trades/20070620/IBM_trades.binRT" data = TAQTradesReader(taqBaseDir + filePathName)
def ori_vs_adj_trade_NVDA(): dayStartNVDA = '20070910' # Start day dayEndNVDA = '20070911' # End day parentddir1 = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) parentddir2 = os.path.abspath(os.path.join(parentddir1, os.path.pardir)) oriTradeStartNVDA = TAQTradesReader(os.path.join(parentddir2, 'R/trades/'+dayStartNVDA+'/NVDA_trades.binRT')) oriTradeEndNVDA = TAQTradesReader(os.path.join(parentddir2, 'R/trades/'+dayEndNVDA+'/NVDA_trades.binRT')) oriPriceNVDA = [] # List to store price oriShareNVDA = [] # List to store number of shares timeList = [] # List to store time stamp # Append data to each of the lists for i in range(oriTradeStartNVDA.getN()): oriPriceNVDA.append(oriTradeStartNVDA.getPrice(i)) oriShareNVDA.append(oriTradeStartNVDA.getSize(i)) timeList.append(1189396800000 + oriTradeStartNVDA.getTimestamp(i)) for i in range(oriTradeEndNVDA.getN()): oriPriceNVDA.append(oriTradeEndNVDA.getPrice(i)) oriShareNVDA.append(oriTradeEndNVDA.getSize(i)) timeList.append(1189483200000 + oriTradeEndNVDA.getTimestamp(i)) # convert data format to datetime dateTimeList = [] for timeStamp in timeList: dateTimeList.append(datetime.fromtimestamp(timeStamp/1000)) adjTradeStartNVDA = BinReader(os.path.join(os.getcwd(), 'adjusted_trades/NVDA_trades.gz'), '>QIIf', 100) ts0, _, size0, p0 = adjTradeStartNVDA.next() ts = [] ts.append(ts0) share = [] share.append(size0) price = [] price.append(p0) while adjTradeStartNVDA.hasNext(): t, i, s, p = adjTradeStartNVDA.next() ts.append(t) share.append(s) price.append(p) sIdx = binarySearch(ts, 0, len(ts)-1, timeList[0]) # find the start index eIdx = binarySearch(ts, 0, len(ts)-1, timeList[-1]) # find the end index pList = price[sIdx:eIdx+1] sList = share[sIdx:eIdx+1] adjTradeStartNVDA.close() # close bin reader plt.figure(figsize=(9,6)) ax = plt.gca() xfmt = mdates.DateFormatter('%m-%d %H:%M') ax.xaxis.set_major_formatter(xfmt) plt.plot(dateTimeList[:-5], oriPriceNVDA[:-5], ls = 'None', marker = 'o', ms = 5, color = 'y', label = 'original') plt.plot(dateTimeList[:-5], pList, ls = 'None', marker = 'x', ms = 2 , color = 'black', label = 'adjusted') plt.xticks([datetime.strptime('2007-09-10 10:00:00', '%Y-%m-%d %H:%M:%S'), datetime.strptime('2007-09-10 15:00:00', '%Y-%m-%d %H:%M:%S'), datetime.strptime('2007-09-11 09:30:00', '%Y-%m-%d %H:%M:%S'), datetime.strptime('2007-09-11 15:00:00', '%Y-%m-%d %H:%M:%S')]) plt.title('Original NVDA Trade Price VS Adjusted NVDA Trade Price') plt.xlabel('Time') plt.ylabel('Price') plt.legend() plt.show() plt.figure(figsize=(9,6)) ax = plt.gca() xfmt = mdates.DateFormatter('%m-%d %H:%M') ax.xaxis.set_major_formatter(xfmt) plt.plot(dateTimeList[:-5], oriShareNVDA[:-5], ls = 'None', marker = 'o', ms = 5, color = 'y', label = 'original') plt.plot(dateTimeList[:-5], sList, ls = 'None', marker = 'x', ms = 2 , color = 'black', label = 'adjusted') plt.xticks([datetime.strptime('2007-09-10 10:00:00', '%Y-%m-%d %H:%M:%S'), datetime.strptime('2007-09-10 15:00:00', '%Y-%m-%d %H:%M:%S'), datetime.strptime('2007-09-11 09:30:00', '%Y-%m-%d %H:%M:%S'), datetime.strptime('2007-09-11 15:00:00', '%Y-%m-%d %H:%M:%S')]) plt.ylim(0,10000) plt.title('Original NVDA Trade Share VS Adjusted NVDA Trade Share') plt.xlabel('Time') plt.ylabel('Number of Shares') plt.legend() plt.show()
def test1(self): fileName = '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/data/sampleTAQ/sampleTAQ/trades/20070620/IBM_trades.binRT' reader = TAQTradesReader(fileName) print(reader.getN())
def test1(self): # Stocks and trades s_p500 = '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/s_p500.xlsx' stackedTrades = np.array( [['20070620', 34241000, 106.5, 85200.0], ['20070621', 57596000, 106.61000061035156, 500.0], ['20070621', 57596000, 106.61000061035156, 200.0], ['20070621', 57597000, 106.5999984741211, 200.0], ['20070621', 57597000, 106.5999984741211, 200.0], ['20070621', 57597000, 106.5999984741211, 200.0]]) stackedQuotes = np.array( [['20070620', 34241000, 106.5, 85200.0, 106.1, 8200.0], ['20070621', 57597000, 106.5, 85200.0, 106.1, 800.0], ['20070621', 57597000, 106.5, 85200.0, 106.1, 800.0], ['20070621', 57597000, 106.5, 85200.0, 106.1, 800.0], ['20070621', 57597000, 106.5, 85200.0, 106.1, 800.0]]) # Directories where to store filepathadj = '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/adj/' filepathcln = '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/cln/' # Multipliers map multmap = AdjustingHashmap(s_p500) # Write after reading and adjusting adjuster = TAQAdjust(stackedQuotes, stackedTrades, 'IBM', multmap) adjuster.setPriceMult("20070621", 2.0) adjuster.setVolMult("20070621", 4.0) adjuster.adjustQuote() adjuster.adjustTrade() adjuster.storeAdjustedQuotes(filepathadj) adjuster.storeAdjustedTrades(filepathadj) # Write after reading and cleaning cleaner = TAQCleaner(stackedQuotes, stackedTrades, 'IBM') stackedQuotes = stackedQuotes[cleaner.cleanQuotesIndices() == True, :] stackedTrades = stackedTrades[cleaner.cleanTradesIndices() == True, :] cleaner.storeCleanedQuotes(filepathcln) cleaner.storeCleanedTrades(filepathcln) # Read results readerclnQ = TAQQuotesReader( '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/cln/quotes/20070620/IBM_quotes.binRQ' ) readerclnT = TAQTradesReader( '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/cln/trades/20070620/IBM_trades.binRT' ) readeradjQ = TAQQuotesReader( '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/adj/quotes/20070620/IBM_quotes.binRQ' ) readeradjT = TAQTradesReader( '/media/louis/DATA/documents/cours/NYU/SPRING_18/ATQS/HK1/adj/trades/20070620/IBM_trades.binRT' ) # Using previously tested readers, test for expected values self.assertEquals(readerclnQ.getN(), 5) self.assertEquals(readerclnQ.getSecsFromEpocToMidn(), 0) self.assertEquals(readerclnQ.getMillisFromMidn(readerclnQ.getN() - 1), 57597000) self.assertEquals(readerclnQ.getBidSize(readerclnQ.getN() - 1), 21300) self.assertEquals(readerclnQ.getAskSize(readerclnQ.getN() - 1), 200) self.assertAlmostEquals(readerclnQ.getAskPrice(readerclnQ.getN() - 1), 53.0499, 3) self.assertAlmostEquals(readerclnQ.getBidPrice(readerclnQ.getN() - 1), 53.25, 3) # Using previously tested readers, test for expected values self.assertEquals(readeradjQ.getN(), 5) self.assertEquals(readeradjQ.getSecsFromEpocToMidn(), 0) self.assertEquals(readeradjQ.getMillisFromMidn(readeradjQ.getN() - 1), 57597000) self.assertEquals(readeradjQ.getBidSize(readeradjQ.getN() - 1), 21300) self.assertEquals(readeradjQ.getAskSize(readeradjQ.getN() - 1), 200) self.assertAlmostEquals(readeradjQ.getAskPrice(readeradjQ.getN() - 1), 53.0499, 3) self.assertAlmostEquals(readeradjQ.getBidPrice(readeradjQ.getN() - 1), 53.25, 3) # Using previously tested readers, test for expected values self.assertEquals(readerclnT.getN(), 6) self.assertEquals(readerclnT.getSecsFromEpocToMidn(), 0) self.assertEquals(readerclnT.getMillisFromMidn(readerclnT.getN() - 1), 57597000) self.assertEquals(readerclnT.getSize(readerclnT.getN() - 1), 50) self.assertAlmostEquals(readerclnT.getPrice(readerclnT.getN() - 1), 53.29999, 3) # Using previously tested readers, test for expected values self.assertEquals(readeradjT.getN(), 6) self.assertEquals(readeradjT.getSecsFromEpocToMidn(), 0) self.assertEquals(readeradjT.getMillisFromMidn(readeradjT.getN() - 1), 57597000) self.assertEquals(readeradjT.getSize(readeradjT.getN() - 1), 50) self.assertAlmostEquals(readerclnT.getPrice(readeradjT.getN() - 1), 53.29999, 3)
def test1(self): fileName = 'D:/TAQClean/trades/20070620/IBM_trades.binRT' reader = TAQTradesReader( fileName ) print( reader.getN() ) print( reader.getTimestamp(1) )