def connect_min_data(today_min, tomorrow_min): price_array = np.array([]) date_array = [] volume_array = [] current_volume = 0 for tm in today_min: hlc = (tm['highest_price'] + tm['lowest_price'] + tm['close_price']) / 3 price_array = np.append(price_array, np.array([hlc])) record_time = time_converter.intdate_to_datetime(tm['0']) record_time = record_time.replace(hour=int(tm['time'] / 100), minute=int(tm['time'] % 100)) date_array.append(record_time) current_volume += tm['volume'] volume_array.append(current_volume) for tomm in tomorrow_min: hlc = (tomm['highest_price'] + tomm['lowest_price'] + tomm['close_price']) / 3 price_array = np.append(price_array, np.array([hlc])) record_time = time_converter.intdate_to_datetime(tomm['0']) record_time = record_time.replace(hour=int(tomm['time'] / 100), minute=int(tomm['time'] % 100)) date_array.append(record_time) current_volume += tomm['volume'] volume_array.append(current_volume) return price_array, date_array, volume_array
def negative_individual_investor(reader, today, code_dict): #check 5 days candidates = [] for progress, (code, v) in enumerate(code_dict.items()): print('investor', today, f'{progress+1}/{len(code_dict)}', end='\r') today_int = v['past_data'][-1]['0'] until_date = time_converter.intdate_to_datetime(today_int).date() from_date = until_date - timedelta(days=10) investor_data = stock_api.request_investor_data( reader, code, from_date, until_date) if len(investor_data) < 5 or today_int != investor_data[-1]['0']: print('INVESTOR DATA WRONG', code, from_date, until_date) continue investor_data = investor_data[-5:] #check 5 days #print(investor_data[-5:]) volume_array = [] current_volume = 0 for v in investor_data: data = dt.cybos_stock_investor_convert(v) current_volume += data['individual'] volume_array.append(current_volume) #print('volume', volume_array) X = np.arange(len(volume_array)).reshape((-1, 1)) reg = LinearRegression().fit(X, np.array(volume_array)) #print('slope', reg.coef_[0]) if reg.coef_[0] < 0: candidates.append(code) print('') return candidates
def _convert_data_readable(code, past_data): converted_data = [] avg_prices = np.array([]) avg_volumes = np.array([]) yesterday_close = 0 for p in past_data: converted = dt.cybos_stock_day_tick_convert(p) converted['code'] = code converted['date'] = time_converter.intdate_to_datetime(converted['0']) avg_prices = np.append(avg_prices, np.array([converted['close_price']])) avg_volumes = np.append(avg_volumes, np.array([converted['volume']])) if yesterday_close == 0: yesterday_close = converted['close_price'] converted['yesterday_close'] = yesterday_close else: converted['yesterday_close'] = yesterday_close yesterday_close = converted['close_price'] if len(avg_prices) == MAVG: converted['moving_average'] = avg_prices.mean() avg_prices = avg_prices[1:] converted['volume_average'] = avg_volumes.mean() avg_volumes = avg_volumes[1:] else: converted['moving_average'] = 0 converted['avg_volumes'] = 0 converted_data.append(converted) return converted_data
def __init__(self, code, d, min_data, write_graph=False): self.code = code self.today = d self.minute_data = min_data self.write_graph = write_graph if write_graph: self.shapes = [] self.moving_average = np.array([]) self.price_array = np.array([]) self.date_array = np.array([]) for data in min_data: dt = time_converter.intdate_to_datetime(data['0']) dt = dt.replace(hour=int(data['time'] / 100), minute=int(data['time'] % 100)) data['date'] = dt self.price_array = np.append(self.price_array, [data['close_price']]) self.date_array = np.append(self.date_array, [dt]) if len(self.price_array) < 10: self.moving_average = np.append(self.moving_average, [self.price_array.mean()]) else: self.moving_average = np.append( self.moving_average, [self.price_array[-10:].mean()])
def __init__(self, data): self.df = pd.DataFrame(list(map(lambda x: {'date': time_converter.intdate_to_datetime(x['0']), 'close': x['5'], 'high' : x['3'], 'low' : x['4'], 'end' : x['5'], 'volume' : x['6']}, data))) self.df = self.df.set_index('date')
def __init__(self, data, period): self.period = period self.df = pd.DataFrame( list( map( lambda x: { 'date': time_converter.intdate_to_datetime(x['0']), 'close': x['5'] }, data))) self.df = self.df.set_index('date')
def _get_data_from_db(code, from_date, until_date, db_suffix): from_date = from_date if from_date.__class__.__name__ == 'date' else from_date.date() until_date = until_date if until_date.__class__.__name__ == 'date' else until_date.date() now = datetime.now() from_date = _correct_date(from_date, now) until_date = _correct_date(until_date, now) stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] db_data = list(stock_db[code + db_suffix].find({'0': {'$gte':time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date)}})) print(code + db_suffix, time_converter.datetime_to_intdate(from_date), time_converter.datetime_to_intdate(until_date)) db_data = sort_db_data(db_data, db_suffix) days = [time_converter.intdate_to_datetime(d['0']).date() for d in db_data] days = list(dict.fromkeys(days)) working_days = get_working_days(from_date, until_date) vacancy_data = list(stock_db[code + '_V'].find({'0': {'$gte':time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date)}})) vacancy_days = [time_converter.intdate_to_datetime(d['0']).date() for d in vacancy_data] empties = _check_empty_date(days, vacancy_days, working_days) #print('DB DATA', len(db_data), 'EMPTIES', empties) return db_data, empties
def __init__(self, data, short_ma, long_ma): self.short_ma = short_ma self.long_ma = long_ma self.df = pd.DataFrame( list( map( lambda x: { 'date': time_converter.intdate_to_datetime(x['0']), 'close': x['5'] }, data))) self.df = self.df.set_index('date')
def __init__(self, data): # Collect data and divide into 2 group, first 90 days data for mu and sigma and last days for checking speculation df = pd.DataFrame(list(map(lambda x: {'date': time_converter.intdate_to_datetime(x['0']), 'close': x['5']}, data))) if len(df) < 100: print('Data is less than 100 days') sys.exit(1) df = df.set_index('date') self.calculating = df[:90] self.simulation = {} self.simulation['Actual'] = list(df.close.iloc[90:].values) for i in range(RandomWalk.SIMULATION_COUNT): self.simulation['Simulation' + str(i)] = [df.close.iloc[90]]
def __init__(self, data): self.i = 0 def inc(): self.i += 1 return self.i self.price_list = list( map( lambda x: { 'day': time_converter.intdate_to_datetime(x['0']), 'close': x['5'], 'xaxis': inc(), 'volume': x['6'] }, data)) self.df = pd.DataFrame(self.price_list) self.df = self.df.set_index('day')
def get_day_period_data(code, from_date: date, until_date: date, db_suffix='_D'): from_date = from_date if from_date.__class__.__name__ == 'date' else from_date.date( ) until_date = until_date if until_date.__class__.__name__ == 'date' else until_date.date( ) if from_date > datetime.now().date(): from_date = datetime.now().date() if until_date > datetime.now().date(): # Cannot exceed today until_date = datetime.now().date() stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] db_data = list(stock_db[code + db_suffix].find({ '0': { '$gte': time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date) } })) #print('DB LEN', len(db_data)) days = [time_converter.intdate_to_datetime(d['0']).date() for d in db_data] days = list(dict.fromkeys(days)) #print('DAYS:', days) working_days = get_working_days(from_date, until_date) #print('WORKING DAYS:', working_days) empties = _insert_day_data(stock_db, code, days, working_days, db_suffix) if len(empties) > 0: db_data = list(stock_db[code + db_suffix].find({ '0': { '$gte': time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date) } })) #print(db_data) return db_data
def check_investor_trend(code, start_date, end_date): conn = connection.Connection() obj = win32com.client.gencache.EnsureDispatch('CpSysDib.CpSvr7254') obj.SetInputValue(0, code) obj.SetInputValue(1, 0) obj.SetInputValue(2, time_converter.datetime_to_intdate(start_date)) obj.SetInputValue(3, time_converter.datetime_to_intdate(end_date)) obj.SetInputValue(4, ord('0')) obj.SetInputValue(5, 0) obj.SetInputValue(6, ord('1')) now = datetime.now() continue_request = True datas = [] prev = None while continue_request: conn.wait_until_available() obj.BlockRequest() count = obj.GetHeaderValue(1) if count == 0: break for i in range(count): d = {} for j in range(19): d[str(j)] = obj.GetDataValue(j, i) if prev != None and prev['0'] <= d['0']: continue_request = False break if now - time_converter.intdate_to_datetime(d['0']) > timedelta(days=365*5): continue_request = False prev = d datas.append(d) datas = sorted(datas, key=lambda x: x['0']) return datas
def get_year_high_date(code, intdate): if code in _uni_data: if intdate in _uni_data[code]: return time_converter.intdate_to_datetime( _uni_data[code][intdate]['year_highest_date']) return datetime.now()
def save(message_reader, peak_data, data, start_time, until_time, matched_data): price_array, date_array, volume_array_old, price_average = data code = peak_data['code'] volume_array = [] matched_data_price_array = [] matched_data_date_array = [] matched_data_volume_array = [] titles = ['comparision', peak_data['code']] current_volume = 0 for v in volume_array_old: volume_array.append(v - current_volume) current_volume = v for m in matched_data: current_volume = 0 min_data = stock_api.request_stock_minute_data(message_reader, m['code'], m['from_date'].date(), m['until_date'].date()) if len(min_data) == 0: print('SAVE NO MIN DATA', code, m['from_date'], m['until_date']) continue min_data_c = [] for md in min_data: min_data_c.append(dt.cybos_stock_day_tick_convert(md)) min_data_price_array = [ int((mdata['lowest_price'] + mdata['close_price'] + mdata['highest_price']) / 3) for mdata in min_data_c ] min_data_date_array = [] min_data_volume_array = [] for mdata in min_data_c: min_data_volume_array.append(mdata['volume']) tc = time_converter.intdate_to_datetime(mdata['0']).date() if tc == m['from_date'].date(): min_data_date_array.append(date_array[0].replace( hour=int(mdata['time'] / 100), minute=int(mdata['time'] % 100))) else: min_data_date_array.append(date_array[-1].replace( hour=int(mdata['time'] / 100), minute=int(mdata['time'] % 100))) matched_data_price_array.append(min_data_price_array) matched_data_date_array.append(min_data_date_array) matched_data_volume_array.append(min_data_volume_array) titles.append(m['code'] + '_' + m['from_date'].strftime('%Y%m%d-') + m['until_date'].strftime('%Y%m%d')) fig = make_subplots(rows=len(matched_data_price_array) + 2, cols=1, shared_xaxes=True, specs=[[{ "secondary_y": True }]] * (len(matched_data_price_array) + 2), subplot_titles=titles) fig.add_trace(go.Scatter(x=date_array, y=profit_price(price_array), line=dict(color='black', width=2)), row=1, col=1) for i, data in enumerate(matched_data_price_array): fig.add_trace(go.Scatter(x=matched_data_date_array[i], y=profit_price(matched_data_price_array[i]), line=dict(width=1), opacity=0.8), row=1, col=1) fig.update_xaxes(type='category', tickformat='%H%M', row=1, col=1) fig.add_trace(go.Scatter(x=date_array, y=price_array, name=code, line=dict(color='black', width=1)), secondary_y=False, row=2, col=1) fig.add_trace(go.Scatter(x=date_array, y=price_average, line=dict(color='green')), secondary_y=False, row=2, col=1) fig.add_trace(go.Bar(x=date_array, y=volume_array), row=2, col=1, secondary_y=True) fig.update_xaxes(type='category', tickformat='%H%M', row=2, col=1) fig.update_yaxes(title_text='price', secondary_y=False, row=2, col=1) fig.update_yaxes(title_text='volume', secondary_y=True, row=2, col=1) for i, data in enumerate(matched_data_price_array): fig.add_trace(go.Scatter(x=matched_data_date_array[i], y=data), row=i + 3, col=1, secondary_y=False) fig.add_trace(go.Bar(x=matched_data_date_array[i], y=matched_data_volume_array[i]), row=i + 3, col=1, secondary_y=True) fig.update_xaxes(type='category', tickformat='%H%M', row=i + 3, col=1) shapes = [ dict(x0=until_time, x1=until_time, y0=0, y1=1, xref='x', yref='paper', line_width=2) ] annotations = [] for p in peak_data['peak'][1:]: annotations.append( go.layout.Annotation(x=p['time'], y=p['price'], text=('t' if p['type'] == 1 else 'b'), xref='x2', yref='y3', showarrow=True, arrowhead=7)) fig.update_layout(title=code + '_' + start_time.strftime('%Y%m%d'), shapes=shapes, annotations=annotations, yaxis_tickformat='d', autosize=True, width=1920, height=(len(matched_data_price_array) + 2) * 300) fig.write_html(code + '_' + until_time.strftime('%Y%m%d%H%M_') + str(len(peak_data['peak'])) + '.html', auto_open=False) print( 'save done', code + '_' + until_time.strftime('%Y%m%d%H%M_') + str(len(peak_data['peak'])) + '.html')
def convert_to_datetime(self, record_date, record_time): hour_min = time_converter.intdate_to_datetime(record_date) hour_min = hour_min.replace(hour=int(record_time / 100), minute=int(record_time % 100)) return hour_min