def create_stock_figure_column(code, start_date, end_date): obj = CStock(code) delta_days = (end_date - start_date).days if delta_days <= 0: return None start_date = start_date.strftime('%Y-%m-%d') end_date = end_date.strftime('%Y-%m-%d') df = obj.get_k_data_in_range(start_date, end_date) if df is None: return None df['date'] = df['date'].apply( lambda x: str_to_datetime(x, dformat="%Y-%m-%d")) source = ColumnDataSource(df) mapper = linear_cmap(field_name='pchange', palette=['red', 'green'], low=0, high=0, low_color='green', high_color='red') p = figure(plot_height=500, plot_width=1300, tools="", toolbar_location=None, sizing_mode="scale_both", x_range=(0, len(df))) p.xaxis.axis_label = "时间" p.yaxis.axis_label = "点数" p.segment(x0='index', y0='low', x1='index', y1='high', line_width=2, color='black', source=source) p.vbar(x='index', bottom='open', top='close', width=50 / delta_days, color=mapper, source=source) p.xaxis.major_label_overrides = { i: mdate.strftime('%Y-%m-%d') for i, mdate in enumerate(df["date"]) } volume_p = figure(plot_height=150, plot_width=1300, tools="", toolbar_location=None, sizing_mode="scale_both") volume_p.x_range = p.x_range volume_p.vbar(x='index', top='volume', width=50 / delta_days, color=mapper, source=source) volume_p.xaxis.major_label_overrides = { i: mdate.strftime('%Y-%m-%d') for i, mdate in enumerate(df["date"]) } return column(p, volume_p)
def choose_stock(code_list, start_date, end_date): state_dict = dict() state_dict[ct.FL] = list() state_dict[ct.JL] = list() state_dict[ct.QL] = list() state_dict[ct.KL] = list() good_code_list = list() #stock_info = DataFrame(columns={'code', 'ppercent', 'npercent', 'sai', 'sri', 'pchange'}) stock_info = DataFrame() for code in code_list: cstock_obj = CStock(code, redis_host = '127.0.0.1') df_profit = cstock_obj.get_base_floating_profit_in_range(start_date, end_date) if df_profit.profit.mean() > 2: state_dict[ct.FL].append(code) elif df_profit.profit.mean() < -2: state_dict[ct.KL].append(code) elif df_profit.profit.mean() >= -2 and df_profit.profit.mean() <= 0: state_dict[ct.QL].append(code) else: state_dict[ct.JL].append(code) df = cstock_obj.get_k_data_in_range(start_date, end_date) if df is None or df.empty or len(df) < 55: continue if average_amount_volume(df) and large_down_time(df) and max_turnover(df): good_code_list.append(code) return state_dict, good_code_list
from os.path import abspath, dirname sys.path.insert(0, dirname(dirname(abspath(__file__)))) import const as ct import numpy as np import pandas as pd from cstock import CStock import matplotlib.pyplot as plt start_date = '2016-01-01' end_date = '2019-01-01' selected = ['601318', '600547', '002153', '600584', '601933', '600600', '000063', '000837', '000543', '000423'] df = pd.DataFrame() for code in selected: obj = CStock(code, dbinfo = ct.OUT_DB_INFO, redis_host = '127.0.0.1') data = obj.get_k_data_in_range(start_date, end_date) data['code'] = code data = data[['code', 'date', 'close']] df = df.append(data) df = df.set_index('date') table = df.pivot(columns='code') table = table[~table.index.isin(table[table.isnull().any(axis=1)].index.tolist())] returns_daily = table.pct_change() returns_annual = returns_daily.mean() * 250 cov_daily = returns_daily.cov() cov_annual = cov_daily * 250 port_returns = []