def update_day(): def f(iterrow): try: code = iterrow[0] info = iterrow[1] print(code) filename = 'd:/analyze_data/k/{}.csv'.format(code) if os.path.exists(filename): text = open(filename, encoding='GBK').read() text = text.replace('--', '') hist = pd.read_csv(StringIO(text), dtype={'date': 'object'}) hist = hist.set_index('date') hist_close = hist['close'] for i in range(len(hist)): k = hist.iloc[i] date = k.name d = {'code': code, 'date': date, 'outstanding': info['outstanding'], 'totals': info['totals'], 'totalAssets': info['totals'] * k['close']} d['bbi'] = (hist_close.iloc[i:i + 3].mean() + hist_close.iloc[i:i + 6].mean() + hist_close.iloc[i:i + 12].mean() + hist_close.iloc[i:i + 24].mean()) / 4 d['量比'] = k['volume'] / (hist['volume'].iloc[i:i + 6].tail(5).mean()) d['turnover'] = k['turnover'] mu.acquire() df = all_days[date] if date in all_days else pd.DataFrame(columns=columns) df = df.append(d, ignore_index=True) all_days[date] = df mu.release() except Exception as e: print(e) print(iterrow) all_days = {} columns = ['code', 'date', 'outstanding', 'totals', 'totalAssets', 'bbi', '量比', 'turnover'] basics = get_stock_basics() # basics = basics.head(50) mu = threading.Lock() tp = ThreadPool() tp.map(f, basics.iterrows()) # for iterrow in basics.iterrows(): # f(iterrow) path = 'd:/analyze_data/day' if not os.path.exists(path): os.makedirs(path) for (k, v) in all_days.items(): v = v.set_index('code') v.to_csv('{}/{}.csv'.format(path, k))
def get_bbi_match(date): ignore_list = json.load(open('ignore_list.json', encoding='utf8')) ignore_list = [] pool = pd.DataFrame( columns=['code', 'bbi', '量比', 'turnover', 'totalAssets']) mu = threading.Lock() # basics = get_stock_basics() basics = get_stock_basics() for index, row in basics.iterrows(): if index in ignore_list: # print(index) continue filename = 'd:/analyze_data/k/{}.csv'.format(index) if os.path.exists(filename): text = open(filename, encoding='GBK').read() text = text.replace('--', '') hist = pd.read_csv(StringIO(text), dtype={'date': 'object'}) hist = hist.set_index('date') hist = hist[hist.index <= date] if len(hist) == 0: continue if hist.index[0] == date: row['bbi'] = (hist['close'].head(3).mean() + hist['close'].head(6).mean() + hist['close'].head(12).mean() + hist['close'].head(24).mean()) / 4 row['量比'] = hist['volume'][0] / ( hist['volume'].head(6).tail(5).mean()) row['turnover'] = hist['turnover'][0] pool = pool.append( { 'code': index, 'bbi': row['bbi'], '量比': row['量比'], 'turnover': row['turnover'], 'totalAssets': row['totals'] * hist['close'][0] }, ignore_index=True) # print(pool) pool = pool[pool['turnover'].between(2, 7)] pool = pool[pool['量比'].between(0.5, 3)] pool = pool[pool['bbi'].between(5, 10)] pool = pool.sort_values('totalAssets') # print(pool) if len(pool) > 0: return pool.iloc[0]['code']
def update(self): t = datetime.datetime.now() today = str(t.date()) # redis_pool = redis.ConnectionPool(host='127.0.0.1', port='6379') basics = get_stock_basics() index_pool = list(basics.iterrows()) self.fail_pool = [] tp = ThreadPool() while len(index_pool) > 0: tp.map(self.update_tick_and_dd, index_pool) index_pool = self.fail_pool self.fail_pool = [] sleep(5) print(datetime.datetime.now() - t)
def update(code): hist = get_hist_data(code, start='2016-02-13') if hist is None or len(hist) < 10: return settlement = get_settlement(hist) amplitude = ((hist['high'] - hist['low']) / settlement * 100).round(2) hist['amplitude'] = amplitude hist = hist[amplitude > 12] hist = hist[hist['p_change'] > 0] hist = hist[[ 'open', 'high', 'low', 'close', 'p_change', 'turnover', 'amplitude' ]] if len(hist) > 0: print(code) print(hist['p_change'].mean()) print(hist) if __name__ == '__main__': t = datetime.datetime.now() basics = get_stock_basics() tp = ThreadPool() tp.map(update, basics.index) print(datetime.datetime.now() - t)