def testSqlDam(self): dam = SqlDAM(echo=False) dam.setup({"db": 'sqlite:////tmp/sqldam.sqlite'}) dam.setSymbol("test") quotes = [ Quote(*[ '1320676200', '32.59', '32.59', '32.58', '32.58', '65213', None ]), Quote(*[ '1320676201', '32.60', '32.60', '32.59', '32.59', '65214', None ]) ] ticks = [ Tick(*['1320676200', '32.59', '32.59', '32.58', '32.58', '65213']), Tick(*['1320676201', '32.60', '32.60', '32.59', '32.59', '65214']) ] dam.writeQuotes(quotes) dam.writeTicks(ticks) dam.commit() print([ str(quotes) for symbol, quotes in dam.readBatchTupleQuotes( ["test"], 0, None).items() ]) print([str(quote) for quote in dam.readQuotes(0, None)]) print([str(tick) for tick in dam.readTicks(0, "1320676201")]) print([str(tick) for tick in dam.readTicks(0, "1320676202")])
def testFundamental(self): dam = SqlDAM(echo=False) dam.setup({"db": 'sqlite:////tmp/sqldam.sqlite'}) dam.setSymbol("test") keyTimeValueDict = { 'Total Debt': { 'As of 2011-12-31': 2089.6500000000001, 'As of 2012-03-31': 2085.0, 'As of 2010-12-31': 1794.23, 'As of 2009-12-31': 0.0, 'As of 2008-12-31': 1000.0, 'As of 2011-09-30': 2543.9899999999998, 'As of 2011-06-30': 2545.6999999999998, 'As of 2011-03-31': 1794.48 }, 'Effect of Special Items on Income Taxes': { '12 months ending 2010-12-31': None, '3 months ending 2011-09-30': None, '12 months ending 2008-12-31': None, '3 months ending 2012-03-31': None, '3 months ending 2011-12-31': None, '12 months ending 2011-12-31': None, '12 months ending 2009-12-31': None, '3 months ending 2011-03-31': None, '3 months ending 2011-06-30': None } } dam.writeFundamental(keyTimeValueDict) dam.commit() ret = dam.readFundamental() print(ret) self.assertEqual(keyTimeValueDict, ret)
def testFundamental(self): dam = SqlDAM(echo = False) dam.setDb('sqlite:////tmp/sqldam.sqlite') dam.setSymbol("test") keyTimeValueDict = {'Total Debt': {'As of 2011-12-31': 2089.6500000000001, 'As of 2012-03-31': 2085.0, 'As of 2010-12-31': 1794.23, 'As of 2009-12-31': 0.0, 'As of 2008-12-31': 1000.0, 'As of 2011-09-30': 2543.9899999999998, 'As of 2011-06-30': 2545.6999999999998, 'As of 2011-03-31': 1794.48}, 'Effect of Special Items on Income Taxes': {'12 months ending 2010-12-31': None, '3 months ending 2011-09-30': None, '12 months ending 2008-12-31': None, '3 months ending 2012-03-31': None, '3 months ending 2011-12-31': None, '12 months ending 2011-12-31': None, '12 months ending 2009-12-31': None, '3 months ending 2011-03-31': None, '3 months ending 2011-06-30': None}} dam.writeFundamental(keyTimeValueDict) dam.commit() ret = dam.readFundamental() self.assertEqual(keyTimeValueDict, ret)
def testSqlDam(self): dam = SqlDAM(echo = False) dam.setDb('sqlite:////tmp/sqldam.sqlite') dam.setSymbol("test") quotes = [Quote(*['1320676200', '32.59', '32.59', '32.58', '32.58', '65213', None]), Quote(*['1320676201', '32.60', '32.60', '32.59', '32.59', '65214', None])] ticks = [Tick(*['1320676200', '32.59', '32.59', '32.58', '32.58', '65213']), Tick(*['1320676201', '32.60', '32.60', '32.59', '32.59', '65214'])] dam.writeQuotes(quotes) dam.writeTicks(ticks) dam.commit()
def testSqlDam(self): dam = SqlDAM(echo = False) dam.setDb('sqlite:////tmp/sqldam.sqlite') dam.setSymbol("test") quotes = [Quote(*['1320676200', '32.59', '32.59', '32.58', '32.58', '65213', None]), Quote(*['1320676201', '32.60', '32.60', '32.59', '32.59', '65214', None])] ticks = [Tick(*['1320676200', '32.59', '32.59', '32.58', '32.58', '65213']), Tick(*['1320676201', '32.60', '32.60', '32.59', '32.59', '65214'])] dam.writeQuotes(quotes) dam.writeTicks(ticks) dam.commit() print([str(quote) for quote in dam.readQuotes(0, None) ]) print([str(tick) for tick in dam.readTicks(0, "1320676201")]) print([str(tick) for tick in dam.readTicks(0, "1320676202")])
def loadMt4CsvToSqlDam(path): """ convert mt4 csv format data to database mt4 csv format: <TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL> EURUSD,20010102,230100,0.9507,0.9507,0.9507,0.9507,4 EURUSD,20010102,230200,0.9506,0.9506,0.9505,0.9505,4 EURUSD,20010102,230300,0.9505,0.9507,0.9505,0.9506,4 """ print path dam = SqlDAM() dam.setup({'db': "sqlite:///../../data/ftest.sqlite"}) dam.setSymbol('EURUSD') skipfirst = True with open(path, "rb") as csvfile: _csvReader = csv.reader(csvfile) count = 0 total_count = 0; for row in _csvReader: if(skipfirst): skipfirst = False continue newrow = [] for col in row: newrow.append(col) # newrow[0] = time.strftime('%Y%m%d', time.strptime(newrow[0], '%Y.%m.%d')) quotes = [] quote = Quote(newrow[1]+newrow[2], newrow[3], newrow[4], newrow[5], newrow[6], newrow[7], newrow[6]) quotes.append(quote) dam.writeQuotes(quotes) count += 1 if(count >= 5000): total_count += count count = 0 dam.commit() print 'dam committed: '+total_count dam.commit() # dam.writeQuotes(quotes) # dam.commit() # if __name__ == "__main__": # loadCsvToSqlDam("../../data/eurusd.csv")
def loadCsvToSqlDam(path): """ convert csv format data to database """ print path dam = SqlDAM() dam.setup({'db': "sqlite:///../../data/ftest.sqlite"}) dam.setSymbol('EURUSD') with open(path, "rb") as csvfile: _csvReader = csv.reader(csvfile) quotes = [] for row in _csvReader: newrow = [] for col in row: newrow.append(col) newrow[0] = time.strftime('%Y%m%d', time.strptime(newrow[0], '%Y.%m.%d')) quote = Quote(newrow[0], newrow[2], newrow[3], newrow[4], newrow[5], newrow[6], newrow[5]) quotes.append(quote) dam.writeQuotes(quotes) dam.commit()
def get(self): """ get quotes """ dam = SqlDAM() print os.path.dirname(__file__) flags={} sqlsave = SqlSaver() sqlsave.setup({'db': 'sqlite:///../zhihui/data/output.sqlite'}, "['EURUSD']_sma") result = sqlsave.getStates(20000101,20131231) for row in result: if row.updateOrders != []: order = row.updateOrders[0] flags[row.time] = order.action dam.setup({'db': 'sqlite:///../zhihui/data/ftest.sqlite'}) dam.setSymbol('EURUSD') _quotes = dam.readQuotes(20000101, 20131231) quotes = [quote.toDict() for quote in _quotes] self.write(json.dumps(quotes))
def getSourceData(): dam = SqlDAM() dam.setup({'db': 'sqlite:///../../../zhihui/data/ftest.sqlite'}) dam.setSymbol('EURUSD') _quotes = dam.readQuotes(20000101, 20131231) return _quotes