def preload_chart(chart, file_path, allow_time_gap_in_sec=90): loaded_bar = {} for line in open(file_path): line = line.strip() if len(line) == 0: continue # DATE,BEGIN,END,LOW,HIGH data = line.split(",") start_date = dateutil.parser.parse(data[0]) begin = float(data[1]) end = float(data[2]) low = float(data[3]) high = float(data[4]) timestamp = TimeUtil.timestamp_utc(start_date) loaded_bar[timestamp] = (begin, end, low, high) keys = sorted(loaded_bar.keys()) latest_date = keys[-1] if (TimeUtil.now_timestamp_utc() - latest_date) > allow_time_gap_in_sec: print("Cannot load preloaded chart due to timegap: ", (TimeUtil.now_timestamp_utc() - latest_date)) return one_sec = 1 for timestamp in keys: begin, end, low, high = loaded_bar[timestamp] chart.add_new_data(timestamp + one_sec, begin) chart.add_new_data(timestamp + one_sec, low) chart.add_new_data(timestamp + one_sec, high) chart.add_new_data(timestamp + one_sec, end) print("Load preloaded chart!!")
def on_message(self, ws, msg): now = TimeUtil.now_timestamp_utc() received_data = json.loads(msg) #print("msg:"+str(msg)) if not isinstance(received_data, list): print("error: recieved data is not array: " + str(msg)) return if len(received_data) < 5: print("error: recieved array length is less than 5: " + str(received_data)) return msg_id = received_data[0] pair = received_data[1] price = float(received_data[2]) amount = float(received_data[3]) trade_type = received_data[4] update_only_high_or_low = False if self.last_received_id is not None and msg_id < self.last_received_id: # this is not clever solution!!! # because old message possibly update (high&low) # but I ignore it. my algorithm cannot handle high & low value #print("old message recieved (latest:%d, received:%d)" % (self.last_received_id, msg_id,)) update_only_high_or_low = True return if pair != "btc_jpy": print("received data is not btc_jpy data: " + str(pair)) return self.last_received_id = max(msg_id, self.last_received_id) # update chart self.chart.add_new_data(now, price, update_only_high_or_low) # callback after update if self.callback_on_update is not None: self.callback_on_update(0)