def updateQuotesFromDukascopy(instrument, period, depth = 5000000): intervalSize = 3000 intervals = int(depth/intervalSize) import postgresql as db tableName = db.getTableName(instrument, period, depth) if db.isTableExist(tableName) is False: db.createQuotesTable(instrument, period, depth) lastQuote = db.getLastQuote(tableName) if lastQuote is not None: lastQuoteTime = lastQuote[0] else: lastQuoteTime = datetime(2000,1,1) lastQuoteReachedFlag = False engine = DukascopyEngine() for i in range(0, intervals): print 'collecting %d of %d' %(i+1, intervals) if lastQuoteReachedFlag is True: continue res = [] newData = engine.getHistoryBars(instrument, intervalSize, i*intervalSize, 0, period, trimInstrument=True, filterWeekends=False) for row in newData: if row[0] > lastQuoteTime: res.append(row) else: lastQuoteReachedFlag = True db.insertQuotes(tableName, res)
def getHistoryDayByDay(instrument, period, dateFromS, dateToS): try: dateFrom = datetime.strptime(dateFromS, "%d/%m/%Y %H:%M:%S") except Exception as e: dateFrom = datetime.strptime(dateFromS, "%d/%m/%Y") try: dateTo = datetime.strptime(dateToS, "%d/%m/%Y %H:%M:%S") except Exception as e: dateTo = datetime.strptime(dateToS, "%d/%m/%Y") engine = DukascopyEngine() quotes = None currentDate = dateFrom nextDay = dateFrom + relativedelta(days=+1) while nextDay < dateTo: print "collecting: " + instrument + " " + period + " " + currentDate.strftime('%d/%m/%Y %H:%M:%S') + '-' + nextDay.strftime('%d/%m/%Y %H:%M:%S') r = engine.getHistoryFromDateToDate(instrument, period, currentDate.strftime('%d/%m/%Y %H:%M:%S'), nextDay.strftime('%d/%m/%Y %H:%M:%S')) r = r[:len(r)-1] #obrezaem posledniy chtobi ne bilo gemora na sticah dnia if quotes is None: quotes = r else: quotes = np.append(quotes, r, axis=0) currentDate = currentDate + relativedelta(days=+1) nextDay = nextDay + relativedelta(days=+1) if currentDate < dateTo: print "collecting: " + instrument + " " + period + " " + currentDate.strftime('%d/%m/%Y %H:%M:%S') + '-' + nextDay.strftime('%d/%m/%Y %H:%M:%S') r = engine.getHistoryFromDateToDate(instrument, period, currentDate.strftime('%d/%m/%Y %H:%M:%S'), nextDay.strftime('%d/%m/%Y %H:%M:%S')) if quotes is None: quotes = r else: quotes = np.append(quotes, r, axis=0) return quotes
def updateQuotes(maxQuotes = 100): intervalSize = 3000 intervals = 2100 import postgresql as db #tableName = "eurusd_1min" tableName = "eurusd_10sec" lastQuote = db.getLastQuote(tableName) if lastQuote is not None: lastQuoteTime = lastQuote[0] else: lastQuoteTime = datetime(2000,1,1) lastQuoteReachedFlag = False engine = DukascopyEngine() for i in range(0, intervals): print 'collecting %d of %d' %(i+1, intervals) if lastQuoteReachedFlag is True: continue res = [] newData = engine.getHistoryBars('EUR/USD', intervalSize, i*intervalSize, 0, 'TEN_SECS', trimInstrument=True, filterWeekends=False) for row in newData: if row[0] > lastQuoteTime: res.append(row) else: lastQuoteReachedFlag = True db.insertQuotes(tableName, res) exit() print lastQuote data = getHistoryIntervalsToDB(10, 10, instrument='EUR/USD', period='ONE_MIN') db.insertQuotes("eurusd_1min", data) print 'done'
def getHistoryIntervalsToDB(intervals, intervalSize, instrument = 'EUR/USD', period = 'ONE_MIN'): engine = DukascopyEngine() res = engine.getHistoryBars(instrument, intervalSize, 0, 0, period, trimInstrument=True, filterWeekends=False) for i in range(1,intervals): print 'collecting %d of %d' %(i, intervals) newData = engine.getHistoryBars(instrument, intervalSize, i*intervalSize, 0, period, trimInstrument=True, filterWeekends=False) res = np.append(newData, res, axis=0) return res
if f[len(f)-1] < m - 2*s: print 'opening long' if coef[0] < 0: self.engine.sendOrder(Order('EUR/USD', -1, 0, 0, 0, -1*np.round(coef[0],2), 0, 0, market=True), bar) else: self.engine.sendOrder(Order('EUR/USD', 1, 0, 0, 0, np.round(coef[0],2), 0, 0, market=True), bar) if coef[1] < 0: self.engine.sendOrder(Order('GBP/USD', -1, 0, 0, 0, -1*np.round(coef[1],2), 0, 0, market=True), bar) else: self.engine.sendOrder(Order('GBP/USD', 1, 0, 0, 0, np.round(coef[1],2), 0, 0, market=True), bar) if coef[2] < 0: self.engine.sendOrder(Order('AUD/USD', -1, 0, 0, 0, -1*np.round(coef[2],2), 0, 0, market=True), bar) else: self.engine.sendOrder(Order('AUD/USD', 1, 0, 0, 0, np.round(coef[2],2), 0, 0, market=True), bar) if coef[3] < 0: self.engine.sendOrder(Order('USD/CAD', -1, 0, 0, 0, -1*np.round(coef[3],2), 0, 0, market=True), bar) else: self.engine.sendOrder(Order('USD/CAD', 1, 0, 0, 0, np.round(coef[3],2), 0, 0, market=True), bar) def onStop(self): print 'firstMio stopped..' engine = DukascopyEngine() engine.connect(['USD/CAD', 'EUR/USD', 'GBP/USD', 'AUD/USD'], '1 Min', firstMio)