def __init__(self, d): self.result = cf.NONE_SENSE_DATA_TYPE self.data_list = [] self.x, self.y, self.time, self.instrumentID = d self.button_dict = { 'time': self.time, 'instrumentID': self.instrumentID } flag = 0 for data in self.x: if flag == 0: mongo_engine = MongoEngine() right_data = mongo_engine.find_selective({ 'instrumentID': self.instrumentID, 'time': self.time }) if right_data[0]['opend'] != data[0] and right_data[0][ 'high'] != data[1] and right_data[0]['low'] != data[ 2] and right_data[0]['close'] != data[3]: print(right_data[0]) wrong_data = mongo_engine.find_selective({ 'opend': data[0], 'high': data[1], 'low': data[2], 'close': data[3] }) print(wrong_data[0]) raise NameError date_time = datetime.datetime.strptime(self.time, '%Y-%m-%d') t = date2num(date_time) flag = 1 open, high, low, close = (data[0], data[1], data[2], data[3]) datas = (t, open, high, low, close) self.data_list.append(datas) t -= 1
# # model.add(SimpleRNN(32,return_sequences=True)) # model.add(SimpleRNN(32,return_sequences=True)) # model.add(SimpleRNN(32)) # model.add(Dense(4, activation='softmax')) # # model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) if os.path.exists(cf.GENERAL_MODEL_NAME): model = load_model(cf.GENERAL_MODEL_NAME) return model else: return None mongo_engine = MongoEngine() list = mongo_engine.getAllData() x_l, y_l, t_l, i_l = dd.list_data2train_data(list) x_list = np.array(x_l) y_list = np.array(y_l) t_list = np.array(t_l) i_list = np.array(i_l) n_initial = 1000 idx = [] up_and_down_count = 0 not_order_count = 0 for i in range(len(y_list)): if y_list[i] == 1 or y_list[i] == -1 or y_list[i] == 2: up_and_down_count += 1 idx.append(i)
def db_lookup(db, coll, key, last_updated=720): ''' This is essentially a memoize function for database lookups that sets the value if it doesn't \ already exist or has surpassed the allotted last_updated time IMPORTANT: In order to use this decorator, one must specify a selection_filter kwarg \ in the function that is wrapped by this decorator. The selection_filter value is a standard mongo filter selection object. This is necessary because there is no way to consistently pass dynamic values prior to the decorator being called So we find the document based on this and then update the desired <key> to the return value of our function EXAMPLE: @db_lookup('scraped_data', 'full_html', 'html') function get_html(self, selection_filter={'url': 'example_url'}): # return the html based on a get from <selection_filter[url]> That example function will first check the database for where url matches the example_url \ and then if it doesnt exist or if its been greater than 24 hours since the field was last updated then it will return the html and db_lookup will automatically handle changing the last_updated field and the returned html to the html field IF THE VALUE DOES NOT EXIST IN THE DB THEN WE AUTOMATICALLY INSERT ONE; \ ONLY THE VALUES PRESENT IN THE KEY AND THE SELECTION FILTER WILL BE DEFINED IN THE NEWLY CREATED DOCUMENT; OH AND last_updated will be included Parameters ---------- db: str The MongoDB database you wish to connect to coll: str The MongoDB database collection you wish to check data from key: str The key value you wish to return last_updated: int or float How long (in hours) before a cached value is re-updated ''' from mongo_engine import MongoEngine if last_updated: last_updated_seconds = last_updated * 3600 db_ = MongoEngine(db=db, collection=coll) def outer(func): def inner(*args, **kwargs): document = db_.collection.find_one(kwargs['selection_filter']) if document: if last_updated_seconds: now_ts = datetime.utcnow().timestamp() document_ts = document['last_updated'].timestamp() if now_ts - document_ts > last_updated_seconds: value = func(*args, **kwargs) db_.update_value(kwargs['selection_filter'], key, value, 'one') return value print('Returning cached version') return document[key] else: value = func(*args, **kwargs) # instantiate new entry as the selection args new_entry = kwargs['selection_filter'] # update that entry with our newly generated value and current time as last_updated new_entry.update({ 'last_updated': datetime.utcnow(), key: value }) # add to the db db_.add_entry(new_entry) # return the value we initially were looking for return value return inner return outer
from mongo_engine import MongoEngine import numpy as np from histogram import Histogram import config as cf if __name__ == '__main__': mongo = MongoEngine() a = mongo.find_selective({'chanceType':1}) b = mongo.find_selective({'chanceType':-1}) c = mongo.find_selective({'chanceType':2}) d = np.concatenate((a,b,c)) print(d) for dd in d: list = mongo.find_pos_pre_list(dd['instrumentID'],dd['time'],cf.ROW_LENGTH) x = [] for l in list: xx =[] xx.append(l['opend']) xx.append(l['high']) xx.append(l['low']) xx.append(l['close']) x.append(xx) x = np.array(x) y = list[0]['chanceType'] time = list[0]['time'] instrumentID = list[0]['instrumentID'] ha = (x,y,time,instrumentID) histo = Histogram(ha) histo.createImage() # e = np.delete(d,[])
class Histogram(object): up_buttion = [0.91, 0.8, 0.08, 0.03] down_buttion = [0.91, 0.7, 0.08, 0.03] damped_buttion = [0.91, 0.6, 0.08, 0.03] not_order_buttion = [0.91, 0.5, 0.08, 0.03] mongo_engine = MongoEngine() x = [] y = 0 time = 0 instrumentID = '' button_dict = {} def __init__(self, d): self.result = cf.NONE_SENSE_DATA_TYPE self.data_list = [] self.x, self.y, self.time, self.instrumentID = d self.button_dict = { 'time': self.time, 'instrumentID': self.instrumentID } flag = 0 for data in self.x: if flag == 0: mongo_engine = MongoEngine() right_data = mongo_engine.find_selective({ 'instrumentID': self.instrumentID, 'time': self.time }) if right_data[0]['opend'] != data[0] and right_data[0][ 'high'] != data[1] and right_data[0]['low'] != data[ 2] and right_data[0]['close'] != data[3]: print(right_data[0]) wrong_data = mongo_engine.find_selective({ 'opend': data[0], 'high': data[1], 'low': data[2], 'close': data[3] }) print(wrong_data[0]) raise NameError date_time = datetime.datetime.strptime(self.time, '%Y-%m-%d') t = date2num(date_time) flag = 1 open, high, low, close = (data[0], data[1], data[2], data[3]) datas = (t, open, high, low, close) self.data_list.append(datas) t -= 1 def draw_button_up(self, axis): global button_up #must global point = plt.axes(axis) button_up = Button(point, 'up') button_up.on_clicked(self.button_press_up) def draw_button_down(self, axis): global button_down #must global point = plt.axes(axis) button_down = Button(point, 'down') button_down.on_clicked(self.button_press_down) def draw_button_damped(self, axis): global button_damped #must global point = plt.axes(axis) button_damped = Button(point, 'damped') button_damped.on_clicked(self.button_press_damped) def draw_button_not_order(self, axis): global button_not_order #must global point = plt.axes(axis) button_not_order = Button(point, 'not_order') button_not_order.on_clicked(self.button_press_not_order) def button_press_up(self, event): print('button up is pressed!') d = {'instrumentID': self.instrumentID, 'time': self.time} o = self.mongo_engine.find_one(d) m = self.mongo_engine.find_post_one(self.instrumentID, self.time) if m is not None and o['close'] - m['close'] < 0: self.mongo_engine.update_chance_type(self.button_dict, 1) self.result = 1 else: self.mongo_engine.update_chance_type(self.button_dict, 0) self.result = 0 def button_press_down(self, event): print('button down is pressed!') d = {'instrumentID': self.instrumentID, 'time': self.time} o = self.mongo_engine.find_one(d) m = self.mongo_engine.find_post_one(self.instrumentID, self.time) print(m) if m is not None and o['close'] - m['close'] > 0: self.mongo_engine.update_chance_type(self.button_dict, -1) self.result = -1 else: self.mongo_engine.update_chance_type(self.button_dict, 0) self.result = 0 def button_press_damped(self, event): print('button damped is pressed!') d = {'instrumentID': self.instrumentID, 'time': self.time} o = self.mongo_engine.find_one(d) m = self.mongo_engine.find_post_one(self.instrumentID, self.time) if m is not None and o['close'] - m['close'] < 0: self.mongo_engine.update_chance_type(self.button_dict, 2) self.result = 2 else: self.mongo_engine.update_chance_type(self.button_dict, 0) self.result = 0 def button_press_not_order(self, event): print('button not_order is pressed!') self.mongo_engine.update_chance_type(self.button_dict, cf.NOT_ORDER_DATA_TYPE) self.result = cf.NOT_ORDER_DATA_TYPE def createImage(self): # 创建子图 fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) fig.set_figheight(7) fig.set_figwidth(14) # 设置X轴刻度为日期时间 ax.xaxis_date() plt.xticks(rotation=1500) plt.yticks() plt.title(self.instrumentID) plt.xlabel("time") plt.ylabel("price") self.draw_button_up(self.up_buttion) self.draw_button_down(self.down_buttion) self.draw_button_damped(self.damped_buttion) self.draw_button_not_order(self.not_order_buttion) # fig.set_facecolor('green') # mpf.index_bar(ax,data_list) mpf.candlestick_ohlc(ax, self.data_list, width=0.8, colorup='r', colordown='b') plt.grid() plt.show()
plt.yticks() plt.title(self.instrumentID) plt.xlabel("time") plt.ylabel("price") self.draw_button_up(self.up_buttion) self.draw_button_down(self.down_buttion) self.draw_button_damped(self.damped_buttion) self.draw_button_not_order(self.not_order_buttion) # fig.set_facecolor('green') # mpf.index_bar(ax,data_list) mpf.candlestick_ohlc(ax, self.data_list, width=0.8, colorup='r', colordown='b') plt.grid() plt.show() if __name__ == '__main__': a = {'instrumentID': 'ag1812'} mongo_engine = MongoEngine() list = mongo_engine.findLast(a, cf.ROW_LENGTH) x_list, y_list, t_list, i_list = dd.list_data2train_data(list) data = (x_list[0], y_list[0], t_list[0], i_list[0]) histogram = Histogram(data) histogram.createImage()