def test1(): t1 = nowJst() t0 = t1 - deltaMinute(30) server = MT5Bind('US30Cash') data = server.scrapeRange('M5', t0, t1) if len(data) > 0: print(data)
def updateTicks(stock, repeat=100000): server = MT5Bind(stock) tbegin, tend = handler.rangeOfTicks(stock) if tend is None: t = TimeUtility.jstTime(2018, 1, 1, 0, 0) else: t = tend nothing = 0 for i in range(repeat): data = server.acquireTicks(t, size=20000) if len(data) > 1: handler.updateTicks(stock, data) print(stock, str(TimeUtility.nowJst()), 'Tick Download done ', i, len(data), data[0], data[-1]) logger.debug('updateTicks() ... ' + stock + ': ' + str(i) + ' Length:' + str(len(data)) + '...' + str(data[0]) + '-' + str(data[-1])) tbegin, tend = handler.rangeOfTicks(stock) t = tend nothing = 0 else: t += TimeUtility.deltaHour(1) nothing += 1 if nothing > 10 * 24: break
def start(): stocks = Setting.xm_index() + Setting.xm_fx() schedular = Schedular() for stock in stocks: for timeframe in Timeframe.timeframes(): schedular.addTask(keyOfData(stock, timeframe), timeframe) is_initial = True while loop: for stock in stocks: server = MT5Bind(stock) for timeframe in Timeframe.timeframes(): if is_initial or schedular.shouldDoNow( keyOfData(stock, timeframe)): (tbegin, tend) = handler.rangeOfTime(stock, timeframe) data = server.acquireRange(timeframe, tend, TimeUtility.nowJst()) logger.debug(stock + ' ' + timeframe.symbol + 'Download Length: ' + str(len(data))) if len(data) > 1: handler.update(stock, timeframe, data) print(stock, timeframe.symbol, 'Download done ', len(data)) is_initial = False
def test(): stock = 'EURCHFmicro' server = MT5Bind(stock) fig = plt.figure(figsize=(20, 8)) ax = fig.add_subplot(1, 1, 1) view = DynamicChartViewer(fig, ax, server, 'H8', 300) plt.show()
def save(stock, timeframe): server = MT5Bind(stock) dic = server.scrapeWithDic(timeframe) values = dic['data'] d = [] for value in values: d.append([value['time'], value['open'], value['high'], value['low'], value['close']]) df = pd.DataFrame(data=d, columns=['Time', 'Open', 'High', 'Low', 'Close']) df.to_csv('./' + stock + '_' + timeframe + '.csv', index=False)
def buildDb(self, timeframe): server = MT5Bind(self.stock) data = server.scrape(timeframe) if len(data) > 0: self.uploadFirst(timeframe, data) begin = data[0][0] end = data[-1][0] print(timeframe, 'length:', len(data), 'begin:', begin, 'end:', end) return
def test(): stock = 'JP225Cash' server = MT5Bind(stock) data = server.scrape('M5', 300) d = server.toTimeSeries(data) df = d.toDataFrame() print(df) fig = plt.figure(figsize=(15, 8)) ax = fig.add_subplot(1, 1, 1) view = ChartViewer(fig, ax, stock + '-M5') view.drawChart(d)
def test4(): stock = 'US30Cash' (tbegin, tend) = handler.rangeOfTicks(stock) now = TimeUtility.nowJst() server = MT5Bind(stock) data = server.acquireTicks(now, size=10) if len(data) <= 1: return len(data) - 1 handler.updateTicks(stock, data) begin, end = handler.rangeOfTicks(stock) print('Done...', stock, begin, end)
def test1(): stock = 'US30Cash' timeframe = Timeframe('M1') (begin, end) = handler.rangeOfTime(stock, timeframe) #t0 = end + timeframe.deltaTime #t1 = TimeUtility.nowJst() - TimeUtility.deltaMinute(1) server = MT5Bind(stock) data = server.acquire(timeframe, size=500) if len(data) <= 1: return len(data) - 1 handler.update(stock, timeframe, data) begin, end = handler.rangeOfTime(stock, timeframe) print('Done...', stock, timeframe, begin, end)
def downloadTickData(save_dir, stock, year, month, day): filepath = save_dir + stock + '_Tick_' + str(year).zfill(4) + '-' + str( month).zfill(2) + '-' + str(day).zfill(2) + '.csv' if os.path.isfile(filepath): return server = MT5Bind(stock) t_from = TimeUtility.jstTime(year, month, day, 0, 0) t_to = t_from + TimeUtility.deltaDay( 1) #TimeUtility.jstTime(year, month, day, 23, 59) data = server.acquireTicksRange(t_from, t_to) if len(data) > 0: df = pd.DataFrame(data=data, columns=['Time', 'Bid', 'Ask', 'Mid', 'Volume']) df.to_csv(filepath, index=False)
def test2(): stock = 'US30Cash' timeframe = Timeframe('M1') (tbegin, tend) = handler.rangeOfTime(stock, timeframe) server = MT5Bind(stock) now = TimeUtility.nowJst() data = server.acquireRange(timeframe, tend, now) if len(data) == 0: return -1 if len(data) == 1: return 0 handler.update(stock, timeframe, data) begin, end = handler.rangeOfTime(stock, timeframe) print('Done...', stock, timeframe, begin, end)
def firstUpdate(stocks, size=99999): for stock in stocks: server = MT5Bind(stock) for timeframe in Timeframe.timeframes(): (begin, end) = handler.rangeOfTime(stock, timeframe) data = server.acquire(timeframe, size=size) if len(data) <= 1: print('Error No Data', stock, timeframe.symbol) continue handler.update(stock, timeframe, data) begin, end = handler.rangeOfTime(stock, timeframe) print('Done... legth: ', len(data), stock, timeframe.symbol, begin, end) logger.debug('firstUpdate() ... ' + stock + '-' + timeframe.symbol + ' begin: ' + str(begin) + ' end: ' + str(end))
def update(self, timeframe): begin, end = self.rangeOfTime(timeframe) if end is None: return t0 = end + setting.deltaTimeFrame(timeframe) t1 = nowJst() - deltaMinute(1) server = MT5Bind(self.stock) data = server.scrapeRange(timeframe, t0, t1) if len(data) == 0: return -1 if len(data) == 1: return 0 # remove last data data = data[:len(data) - 1] table = PriceTable(self.stock, timeframe) db = XMDb() ret = db.insert(table, data) if ret == False: print('DB Insert error1') return -1 stored = db.fetchAllItem(table, 'time') times = stored['time'] tbegin = times[0] tend = times[-1] manage = ManageTable() db = XMDb() ret = db.create(manage) if ret == False: print('Management DB create Error!') return -1 ret = db.update(manage, [self.stock, timeframe, None, tend]) if ret == False: print('Management DB update Error!') return -1 return len(data)
def downloadData(market, timeframe, length): server = MT5Bind(market) dic = server.scrapeWithDic(timeframe, length) print(dic) return dic