def min60(time): min60_times = [1030, 1130, 1400, 1500] if time in min60_times: temp = time // 100 * 60 + time % 100 - 60 temp = temp // 60 * 100 + temp % 60 print(temp) sql = {'time': {'$gt': temp, '$lte': time}, 'date': Calendar.today()} curror = BaseModel('real_kline_min5').query(sql=sql) if curror.count(): data = pd.DataFrame(list(curror)) data = data.sort_values(by=['time'], ascending=True) data = data.groupby(by=['stock_code'], as_index=False).agg({ 'volume': 'sum', 'amount': 'sum', 'open': 'first', 'close': 'last', 'high': 'max', 'low': 'min' }) data['time'] = time data['date'] = Calendar.today() BaseModel('real_kline_min60').insert_batch( data.to_dict(orient='records')) print('min60 ok') curror.close()
def check(self): app = Dealer('127.0.0.1', 7497, 1) # time.sleep(1) while True: try: time.sleep(1) accounting_values = app.get_accounting_values('DU1162631') if accounting_values is not None: BaseModel('jts_log').insert_batch([{ 'date': dt.datetime.now(), 'event': 'login-ok', 'accounting_values': accounting_values }]) break else: BaseModel('jts_log').insert_batch([{ 'date': dt.datetime.now(), 'event': 'login-ok', 'accounting_values': accounting_values }]) print('accounting_values None') except Exception as e: print('av failed')
def get_all_stock_code_info(cls, date=None, time=None, kline='kline_min5'): sql = analyzer('date = {} and time = {}'.format(date, time)) data = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data) == 0: return pd.DataFrame() data['pro'] = data.close / data.open - 1 #以上是把数据从数据库中取出 #并且求出各个时刻的收益 #注意,如果要求收益的计算方式close/pre_close-1,则close需要通过shift()调整 data2 = None if time == 1500: #如果time是1500,需要把明天的第一根取出来计算 x = Calendar.calc(analyzer('date = {}'.format(date))['date'], 1)['date'] sql = analyzer('date = {} and time = {}'.format( str(x.year * 10000 + x.month * 100 + x.day), 935)) data2 = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data2) == 0: return pd.DataFrame() data = pd.merge(data, data2, on='stock_code') data['profit'] = (data.close_y / data.close_x - 1) return data.loc[:, [ 'stock_code', 'date_x', 'time_x', 'profit', 'pro' ]] else: time_index = KlineBase.get_time_index(time) sql = analyzer('date = {} and time = {}'.format(date, time_index)) data2 = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data2) == 0: return pd.DataFrame() data = pd.merge(data, data2, on='stock_code') data['profit'] = (data.close_y / data.close_x - 1) return data.loc[:, [ 'stock_code', 'date_x', 'time_x', 'profit', 'pro' ]]
def draw_time_trends(cls, data, profits, date, code, pixel, table_name, detal): # 'dif', 'dea', 'macd' # print("x") data = data.tail(detal) data['volume'] = pixel / 2 - data['volume'] + 0.5 + pixel / 2 - 1 data['up'] += 0.5 volume = data['volume'].astype('int').tolist() close = data['up'].astype('int').tolist() matrix = np.zeros((pixel, pixel), dtype=np.bool) val = int(pixel - 1) for i in range(len(close)): # matrix[volume[i], i] = True if close[i] > pixel / 4: matrix[int(pixel / 4):close[i], i] = True else: matrix[close[i]:int(pixel / 4), i] = True matrix[volume[i]:val, i] = True # BaseModel(table_name).insert_batch( # {'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist()}) BaseModel(table_name).remove(date=date, stock_code=code) BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist() })
def exe(parm): time = parm['time'] datetime = parm['datetime'] i = parm['sc'] temp = datetime.hour * 60 + datetime.minute - 5 temp = temp // 60 * 100 + temp % 60 # print(temp) curror = BaseModel('real_buy_sell_all_stock_code').query( sql={ 'stock_code': { '$in': i }, 'datetime': { '$gte': dt.datetime(datetime.year, datetime.month, datetime.day, temp // 100, temp % 100), '$lt': dt.datetime(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, 0) } }) if curror.count() > 0: data = pd.DataFrame(list(curror)) data['volume2'] = data.volume data['amount2'] = data.amount data['high'] = data.price data['low'] = data.price data['close'] = data.price data['open'] = data.price data = data.groupby(by=['stock_code'], as_index=False).agg({ 'volume': 'first', 'volume2': 'last', 'amount': 'first', 'amount2': 'last', 'open': 'first', 'close': 'last', 'high': 'max', 'low': 'min' }) data['volume'] = data.volume2 - data.volume data['amount'] = data.amount2 - data.amount data = data.drop(['volume2', 'amount2'], axis=1) data['time'] = time data['date'] = dt.datetime(datetime.year, datetime.month, datetime.day) BaseModel('real_kline_min5').insert_batch( data.to_dict(orient='records'))
def drow_boll_line(cls, data, profits, date, code, pixel, table_name): # 'mid', 'top', 'bottom' if len(data) != pixel: return data['mid'] += 0.5 data['top'] += 0.5 data['bottom'] += 0.5 data['low'] += 0.5 data['high'] += 0.5 mid = data['mid'].astype('int').tolist() top = data['top'].astype('int').tolist() bottom = data['bottom'].astype('int').tolist() low = data['low'].astype('int').tolist() high = data['high'].astype('int').tolist() matrix = np.zeros((pixel, pixel), dtype=np.bool) num = pixel - 1 for i in range(len(mid)): matrix[(num - mid[i]), i] = True matrix[(num - top[i]), i] = True matrix[(num - bottom[i]), i] = True matrix[(num - high[i]):(num - low[i]), i] = True BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist() })
def draw_macd_line(cls, data, profits, date, code, pixel, table_name): # 'dif', 'dea', 'macd' if len(data) != pixel: return data['dif'] += 0.5 data['dea'] += 0.5 data['macd'] += 0.5 dif = data['dif'].astype('int').tolist() dea = data['dea'].astype('int').tolist() macd = data['macd'].astype('int').tolist() matrix = np.zeros((pixel, pixel), dtype=np.bool) val = int(pixel / 2 - 1) for i in range(len(dif)): matrix[(dif[i]), i] = True matrix[(dea[i]), i] = True if (macd[i]) > val: matrix[val:(macd[i]), i] = True else: matrix[(macd[i]):val, i] = True BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist() })
def hot_min(self, date, time, kline): data = ModelData.read_data(table_name=kline, field=['stock_code', 'close', 'open'], date=date, time=time) if len(data) <= 0 and time == 1130: data = ModelData.read_data(table_name=kline, field=['stock_code', 'close', 'open'], date=date, time=1300) data = data.drop(['_id'], axis=1) data['profit'] = data.close / data.open - 1 data['profit'] = data[abs(data.profit) < 0.11].profit data['pro'] = data.profit # data['pro'] = data.profit.map(lambda x: 1 if x > 0 else 0) data = data.dropna() data = pd.merge(stock_codes, data, on=['stock_code']) data = data.fillna(0) # print(data.stock_code.tolist()) # data=data[0:56*56] arr = np.array(data.pro.tolist() + [0] * (64 * 64 - len(data))).reshape(64, 64) # ax = sns.heatmap(arr, vmin=-0.03, vmax=0.03,cbar=False) # plt.matshow(arr, cmap='hot') # plt.show() BaseModel('hot_min5_version4').insert_batch( dict(date=date, time=time, value=arr.tolist()))
def get_all_info(cls, start_date, end_date, kline='kline_day'): #不限定股票,条件为时间范围、kline sql = {'date': {'$gte': start_date, '$lte': end_date}} data = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data) == 0: return pd.DataFrame() data.to_csv('f://hhh.csv')
def get_stock_code_list(cls): t = Calendar.today() t = Calendar.calc(t, -1)['date'] obj = BaseModel('kline_day') curror = obj.query({'date': t}) data = pd.DataFrame(list(curror)) data = data.sort_values(by=['stock_code'], ascending=True) return data
def get_all_min5_info(cls, start_date, end_date, kline='index_min5'): sql = {'date': {'$gte': start_date, '$lte': end_date}} data = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data) == 0: return pd.DataFrame() data = signaldata.merge_time(data) # data=pd.DataFrame() data = data.sort_values(by=['date'], ascending=True) return data
def start(self, file): print('strat') status = subprocess.call('taskkill /F /IM tws.exe') status = subprocess.call(file) BaseModel('jts_log').insert_batch([{ 'date': dt.datetime.now(), 'event': 'login-pre', 'accounting_values': status }])
def is_trade_day(cls, date): date = dt.datetime.now() if BaseModel('calendar').query( sql={ 'date': dt.datetime(date.year, date.month, date.day) }).count(): return True else: print('no bussiness day') return False
def check(self): app = Dealer('127.0.0.1', 4002, 4002) # x = app.get_accounting_values('DU1142167') x = app.get_accounting_values(no) BaseModel('jts_log').insert_batch([{ 'date': dt.datetime.now(), 'event': 'login-ok', 'accounting_values': x, 'user': no }]) app.disconnect()
def draw_ma_line(cls, data, profits, date, pixel, code, table_name): if len(data) != pixel: return data['ma5'] += 0.5 data['ma10'] += 0.5 data['ma20'] += 0.5 data['ma60'] += 0.5 data['ma120'] += 0.5 data['high'] += 0.5 data['low'] += 0.5 ma5 = data['ma5'].astype('int').tolist() ma20 = data['ma20'].astype('int').tolist() ma10 = data['ma10'].astype('int').tolist() ma60 = data['ma60'].astype('int').tolist() ma120 = data['ma120'].astype('int').tolist() high = data.high.astype('int').tolist() low = data.low.astype('int').tolist() matrix = np.zeros((pixel, pixel), dtype=np.bool) num = pixel - 1 for i in range(len(ma5)): matrix[(num - ma5[i]), i] = True matrix[(num - ma10[i]), i] = True matrix[(num - ma20[i]), i] = True matrix[(num - ma60[i]), i] = True matrix[(num - ma120[i]), i] = True matrix[(num - high[i]):(num - low[i]), i] = True # BaseModel(table_name).insert_batch( # {'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist()}) BaseModel(table_name).remove(date=date, stock_code=code) BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist() })
def start_exe(kline='kline_min30'): a=time.clock() data = Stock.get_all_stock() stocks = data pool = multiprocessing.Pool(processes=4) m = 10 li = [{'stocks':stocks[i:i + m],'kline':kline} for i in range(0, len(stocks), m)] result = pool.map(fun, li) spend=time.clock()-a BaseModel('kline_data_update_mark').insert_batch({'status':200,'kline':'ma_'+kline,'other':'speed '+str(spend),'date':dt.datetime.now()}) # if __name__ == '__main__': # start_exe(kline='kline_day')
def draw_ma_line2(cls, data, profits, date, pixel, code, table_name, n): if len(data) != pixel: return data['ma5'] += 0.5 data['ma10'] += 0.5 data['ma20'] += 0.5 data['ma60'] += 0.5 data['ma120'] += 0.5 data['high'] += 0.5 data['low'] += 0.5 ma5 = data['ma5'].astype('int').tolist() ma20 = data['ma20'].astype('int').tolist() ma10 = data['ma10'].astype('int').tolist() ma60 = data['ma60'].astype('int').tolist() ma120 = data['ma120'].astype('int').tolist() high = data.high.astype('int').tolist() low = data.low.astype('int').tolist() matrix = np.zeros((pixel, pixel), dtype=np.bool) num = int(pixel / 2 - 1) mark = 0 num2 = int(pixel / 2) for i in range(len(ma5)): matrix[(num - ma5[i]), i] = True matrix[(num - ma10[i]), i] = True matrix[(num - ma20[i]), i] = True matrix[(num - ma60[i]), i] = True matrix[(num - ma120[i]), i] = True matrix[(num - high[i]):(num - low[i]), i] = True if (i >= len(ma5) - n): matrix[(num - ma5[i]) + num2, mark * n:(mark + 1) * n] = True matrix[(num - ma10[i]) + num2, mark * n:(mark + 1) * n] = True matrix[(num - ma20[i]) + num2, mark * n:(mark + 1) * n] = True matrix[(num - ma60[i]) + num2, mark * n:(mark + 1) * n] = True matrix[(num - ma120[i]) + num2, mark * n:(mark + 1) * n] = True matrix[(num - high[i] + num2):(num - low[i]) + num2, mark * n:(mark + 1) * n] = True mark += 1 BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist() })
def draw_macd_line2(cls, data, profits, profit_relative, date, code, pixel, table_name, n): # 'dif', 'dea', 'macd' if len(data) != pixel: return data['dif'] += 0.5 data['dea'] += 0.5 data['macd'] += 0.5 dif = data['dif'].astype('int').tolist() dea = data['dea'].astype('int').tolist() macd = data['macd'].astype('int').tolist() # pixel= matrix = np.zeros((pixel, pixel), dtype=np.bool) val = int(pixel / 2 / 2 - 1) val2 = int(pixel * 3 / 4 - 1) num = int(pixel / 2) mark = 0 detal = int(pixel / n) for i in range(len(dif)): matrix[(dif[i]), i] = True matrix[(dea[i]), i] = True if (macd[i]) > val: matrix[val:(macd[i]), i] = True else: matrix[(macd[i]):val, i] = True if (i >= len(dif) - n): # matrix[(dif[i])+num, mark*n:(mark+1)*n] = True # matrix[(dea[i])+num, mark*n:(mark+1)*n] = True if (macd[i] + num) > val2: matrix[val2:(macd[i] + num), mark * detal:(mark + 1) * detal] = True else: matrix[(macd[i] + num):val2, mark * detal:(mark + 1) * detal] = True mark += 1 BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'profit_relative': profit_relative, 'date': date, 'value': matrix.tolist() })
def draw_macd_line4(cls, data, profits, profit_relative, date, code, pixel, table_name, n, count): # 'dif', 'dea', 'macd' if len(data) != pixel: return data['dif'] += 0.5 data['dea'] += 0.5 data['macd'] += 0.5 dif = data['dif'].astype('int').tolist() dea = data['dea'].astype('int').tolist() macd = data['macd'].astype('int').tolist() matrix = np.zeros((pixel + n * count, pixel + n * count), dtype=np.bool) val = int(pixel / 2 + 2) mark = 0 for i in range(pixel + n): if i < len(dif): matrix[(dif[i]), i] = True matrix[(dea[i]), i] = True if (macd[i]) > val: matrix[val:(macd[i]) + 1, i] = True else: matrix[(macd[i]):val + 1, i] = True else: matrix[(dif[i - n]), (pixel) + mark * count:(pixel) + (mark + 1) * count] = True matrix[(dea[i - n]), (pixel) + mark * count:(pixel) + (mark + 1) * count] = True if (macd[i - n]) > val: matrix[val:(macd[i - n]) + 1, (pixel) + mark * count:(pixel) + (mark + 1) * count] = True else: matrix[(macd[i - n]):val + 1, (pixel) + mark * count:(pixel) + (mark + 1) * count] = True mark += 1 # plt.matshow(matrix,cmap='hot') # plt.show() BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'profit_relative': profit_relative, 'date': date, 'value': matrix.tolist() })
def get_all_stock_code_info_for_day(cls, start_date, end_date, kline='kline_day'): #day数据不同于日内数据 sql = {'date': {'$gte': start_date, '$lte': end_date}} data = pd.DataFrame(list(BaseModel(kline).query(sql))) #以上操作是将数据从数据库中获取 if len(data) == 0: return pd.DataFrame() after_data = data[data.date == end_date] before_data = data[data.date == start_date] data = pd.merge(before_data, after_data, on=['stock_code']) data['pro'] = data.close_x / data.open_x - 1 data['profit'] = data.close_y / data.close_x - 1 # print(data.head()) # print(data.head()) return data.loc[:, ['stock_code', 'date_x', 'profit', 'pro']]
def draw_ma_stub_line2(cls, data, profits, date, code, table_name, pixel, length, profit_relative, count): if len(data) != length: return data['high'] += 0.5 data['low'] += 0.5 data['open'] += 0.5 data['close'] += 0.5 high = data.high.astype('int').tolist() low = data.low.astype('int').tolist() close = data.close.astype('int').tolist() open = data.open.astype('int').tolist() matrix = np.zeros((pixel, pixel), dtype=np.bool) # val = int((pixel + n * count) / 2 - 1) num = pixel mark = 0 for i in range(length): # matrix[(num - high[i ]):(num - low[i])+1, mark * count+count//3+1: (mark + 1) * count-count//3-1] = True if open[i] >= close[i]: matrix[(num - open[i]):(num - close[i]) + 1, mark * count:(mark + 1) * count] = True matrix[(num - high[i]):(num - low[i]) + 1, mark * count + count // 3:(mark + 1) * count - count // 3] = True else: matrix[(num - close[i]), mark * count:(mark + 1) * count] = True #横收 matrix[(num - open[i]), mark * count:(mark + 1) * count] = True #横开 matrix[(num - open[i]):(num - low[i]) + 1, mark * count + count // 3:(mark + 1) * count - count // 3] = True matrix[(num - high[i]):(num - close[i]) + 1, mark * count + count // 3:(mark + 1) * count - count // 3] = True matrix[(num - close[i]):(num - open[i]) + 1, mark * count] = True matrix[(num - close[i]):(num - open[i]) + 1, (mark + 1) * count - 1] = True mark += 1 BaseModel(table_name).insert_batch({ 'stock_code': code, 'profit': profits, 'profit_relative': profit_relative, 'date': date, 'value': matrix.tolist() })
def macd_exe(kline='kline_min30'): a = time.clock() stocks = Stock.get_all_stock() pool = multiprocessing.Pool(processes=4) m = 10 li = [{ 'stocks': stocks[i:i + m], 'kline': kline } for i in range(0, len(stocks), m)] result = pool.map(fun, li) BaseModel('kline_data_update_mark').insert_batch({ 'status': 200, 'kline': 'macd_' + kline, 'other': 'speed ' + str(time.clock() - a), 'date': dt.datetime.now() })
def draw_volume(cls, data, profits, date, code, pixel, table_name, n, count): if len(data) != pixel: return data['volume'] += 0.5 volume = data['volume'].astype('int').tolist() # pixel= matrix = np.zeros((pixel + n * count, pixel + n * count), dtype=np.bool) # val = int((pixel + n * count) / 2 - 1) num = pixel + n * count num2 = num // 2 mark = 0 for i in range(pixel + n): if i < len(volume): if (num - volume[i]) < num2: matrix[(num - volume[i]):num2 + 1, i] = True else: matrix[num2:(num - volume[i]) + 1, i] = True else: if (num - volume[i - n]) < num2: matrix[(num - volume[i - n]):num2 + 1, (pixel) + mark * count:(pixel) + (mark + 1) * count] = True else: matrix[num2:(num - volume[i - n]) + 1, (pixel) + mark * count:(pixel) + (mark + 1) * count] = True mark += 1 obj = BaseModel(table_name) # obj.remove(dict(date=date,stock_code=code)) obj.insert_batch({ 'stock_code': code, 'profit': profits, 'date': date, 'value': matrix.tolist() })
def login(self): BaseModel('jts_log').insert_batch([{ 'date': dt.datetime.now(), 'event': 'login-start' }]) pyautogui.PAUSE = 1 pyautogui.FAILSAFE = True # 启用自动防故障功能f # pyautogui.click(100, 100) pyautogui.typewrite('zjf', 0.1) pyautogui.typewrite(['shiftleft'], 0.1) pyautogui.typewrite('520520', 0.1) pyautogui.typewrite(['shiftleft', '\t'], 0.1) pyautogui.typewrite('zjf', 0.1) pyautogui.typewrite(['shiftleft'], 0.1) pyautogui.typewrite('804533125', 0.1) pyautogui.typewrite(['shiftleft', '\t'], 0.1) pyautogui.typewrite(['down', 'down', '\n'], 0.1) pyautogui.typewrite( ['\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\n'], 0.1) time.sleep(30) self.check()
def exe(): obj = JtsStart() app = Dealer('127.0.0.1', 4002, 4002) # x = app.get_accounting_values('DU1142167') x = app.get_accounting_values(no) try: app.disconnect() except Exception as e: print(e) if x is None: t1 = threading.Thread(target=obj.start, args=(u'C:\Jts\ibgateway\972/ibgateway.exe', )) t2 = threading.Thread(target=obj.login) t1.start() time.sleep(10) t2.start() else: BaseModel('jts_log').insert_batch([{ 'date': dt.datetime.now(), 'event': '不用重启', 'accounting_values': x, 'user': no }]) print('状态不错不用重启')
'low': resell, 'high': reselh, 'close': resel, 'open': resel }) ModelData.insert_data(table_name=kline, data=dd) import datetime as dt start_date = dt.datetime(2016, 1, 1) end_date = dt.datetime(2018, 6, 20) data = pd.DataFrame( list( BaseModel('calendar').query( {'date': { '$gte': start_date, '$lte': end_date }}))) sampleNo = abs(data.num.iloc[0] - data.num.iloc[len(data) - 1]) + 1 index_profit = (Stochastic.get_random(sigma=0.03, sampleNo=sampleNo)) data2 = Stock.get_stock_code_list() stocks = data2.stock_code.tolist() for sc in stocks: Stochastic.get_k(stock_code=sc, kline="kline_day", date=data.date.tolist(), sampleNo=sampleNo, index_profit=index_profit)
def is_trade_day(cls, date): curror = BaseModel('calendar').query(dict(date=Calendar.today())) return True if curror.count() else False
class PlateName: index_code=[] name=[] hangye=[] def get_plate(self): f=open('plate_name.txt','r') for line in f: line=line.split('|') self.index_code.append(line[1]) self.name.append(line[0]) self.hangye.append(line[5].replace('\n','')) data=pd.DataFrame(dict(index_code=self.index_code,name=self.name,hangye=self.hangye)) return data # obj=PlateName() # data1=obj.get_plate() # obj2=PlateStock() # data2=obj2.get_plate() # data=pd.merge(data1,data2,on=['hangye']) # BaseModel('stock_code_plate').remove({}) # BaseModel('stock_code_plate').insert_batch(data.to_dict(orient='records')) #=========================================================================== data=pd.DataFrame(list(BaseModel('stock_code_plate').query({}))) # print(data) data=data.loc[:,['stock_code','index_code']].sort_values(by=['stock_code']) index_dict={} for i,r in data.iterrows(): index_dict[r.stock_code]=r.index_code print(index_dict)
def get_stock_code_plate(cls): data = pd.DataFrame(list(BaseModel('stock_code_plate').query())) data.drop_duplicates(['stock_code'], inplace=True) return data.loc[:, ['code', 'stock_code']]
def get_all_info(cls): data = pd.DataFrame(list(BaseModel('platestock').query())) # print(data) if len(data) == 0: return pd.DataFrame() return data.loc[:, ['hangye', 'stock_code']]