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): # 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 = '/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): fileName = 'D:/TAQClean/trades/20070620/IBM_trades.binRT' reader = TAQTradesReader( fileName ) print( reader.getN() ) print( reader.getTimestamp(1) )