def dashboard(request): tasks = Task.objects.all()[0:5] my_tasks = Task.objects.filter(tester=request.user)[0:5] attr = Results.objects.show_task_id() script_list = [] line = line_time_kf(attr) bar = bar_mp_kf(attr) # myechart = bar.render_embed() # myechart = line.render_embed() script_list.extend(line.get_js_dependencies()) # script_list = bar.get_js_dependencies() script_list.extend(bar.get_js_dependencies()) grid = Grid(width="auto") grid.add(line, grid_bottom="60%") grid.add(bar, grid_top="60%") myechart = grid.render_embed() script_list.extend(grid.get_js_dependencies()) # run_rtv_numbers = Results.objects.order_by('-id').values_list('id').first()[0] # seconds = sum([int(i[0]) for i in Results.objects.values_list('time')]) # m, s = divmod(seconds, 60) # h, m = divmod(m, 60) # d, h = divmod(h, 24) # time_cost = "%02dd:%02dh:%02dm:%02ds" % (d, h, m, s) return render(request, 'dashboard.html', { # 'run_rtv_numbers': run_rtv_numbers, # 'time_cost': time_cost, 'tasks': tasks, 'my_tasks': my_tasks, 'if_dashboard_active': 'active', 'myechart': myechart, 'script_list': script_list })
def echart1(request): template = loader.get_template('ewpyecharts.html') line = Line("压力分析", width=1200, height=700) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高压力", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低压力", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], legend_top="50%", mark_line=["average"], # 设置 dataZoom 控制索引为 0,1 的 x 轴,即第一个和第二个 is_datazoom_show=True, datazoom_xaxis_index=[0, 1]) v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("考核表分析", title_top="50%") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, is_datazoom_show=True) grid = Grid() grid.add(line, grid_top="60%") grid.add(kline, grid_bottom="60%") # grid.render() context = dict( myechart=grid.render_embed(), host=REMOTE_HOST, script_list=grid.get_js_dependencies() ) return HttpResponse(template.render(context, request))
def get_realtime_data(): # 历史数据优先从hdf5中获取 获取不到则实时从接口获取并添加到hdf5中 store = pd.HDFStore(app.config['STOCKS_PRICE_HDF5'], mode='r+') code = request.args.get("code") # sh_index_data = ts.get_k_data('000001', index=True) real_data = ts.get_realtime_quotes(code) if 'sh_index_data' in store: sh_index_data = pd.read_hdf(app.config['STOCKS_PRICE_HDF5'], 'sh_index_data', mode='r+') else: sh_index_data = ts.get_k_data('000001', index=True) sh_index_data.to_hdf(app.config['STOCKS_PRICE_HDF5'], 'sh_index_data', format='table', data_columns=True) if code in store: hist_data = pd.read_hdf(app.config['STOCKS_PRICE_HDF5'], code, mode='r+') else: hist_data = ts.get_k_data(code) hist_data.to_hdf(app.config['STOCKS_PRICE_HDF5'], code, format='table', data_columns=True) store.close() kline = kline_chart(code, hist_data) # 这时还没转dict kline_sh = kline_chart_sh(sh_index_data) grid = Grid(width="100%") grid.add(kline, grid_bottom="60%") grid.add(kline_sh, grid_top="60%") real_data['delta'] = (real_data['price'].astype('float') - real_data['pre_close'].astype('float')) real_data['delta'] = real_data['delta'].apply(lambda x: round(x, 4)) real_data['delta%'] = real_data['delta'] / real_data['pre_close'].astype( 'float') real_data['delta%'] = real_data['delta%'].apply( lambda x: round(x * 100, 2)) real_data = real_data.to_dict('record') hist_data = hist_data.sort_values('date', ascending=False) hist_data = hist_data.to_dict('record') return render_template( "table_with_chart.html", basic_data=url_for('get_stock_basics_json', code=code), basic_data_columns=stock_basics_columns, real_data=url_for('get_realtime_data_json', code=code), real_data_columns=real_data_columns, hist_data_columns=hist_data_columns, hist_data=hist_data, title=('{0}:({1})股票实时价格'.format(real_data[0]['name'], code)), grid=grid.render_embed(), host=app.config['REMOTE_HOST'], grid_script_list=grid.get_js_dependencies(), )
def get(self, request, *args, **kwargs): template_name = 'echart/echart.html' bar = Bar() bar.add('服装', ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90]) line = Line() line.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90]) echart = Grid(page_title='组合图', width=1200, height=250) echart.add(bar, grid_left='50%') echart.add(line, grid_right='55%') context = dict(myechart=echart.render_embed(), host=REMOTE_HOST, script_list=echart.get_js_dependencies()) return render(request, template_name, context)
def get_bars(trade): _id = trade.exec_id[0] _match_trade = [] _match = [] _match_vol = 0 for i, v in trade.iterrows(): _match.append(v) _match_vol += (v.last_quantity if v.side == "BUY" else -v.last_quantity) if _match_vol == 0: _match_trade.append(_match) _match = [] else: if _match_vol != 0: _match_trade.append(_match) for m in _match_trade: for t in m: if t.exec_id == int(_id): trade_points_raw = m break profit = 0 for tpr in trade_points_raw: profit += tpr.last_price * (tpr.last_quantity if tpr.side == 'SELL' else -tpr.last_quantity) trade_lines_raw = [] pos = 0 p_pos = 0 for i in range(len(trade_points_raw)): tpr1 = trade_points_raw[i] pos += (tpr1.last_quantity if tpr1.side == 'BUY' else -tpr1.last_quantity) for j in range(i + 1, len(trade_points_raw)): tpr2 = trade_points_raw[j] if tpr1.side != tpr2.side: p_pos += (tpr2.last_quantity if tpr2.side == 'BUY' else -tpr2.last_quantity) if abs(pos) > abs(p_pos): continue trade_lines_raw.append([tpr1, tpr2]) p_pos = 0 break trade_points = [{ 'coord': [str(tpr.datetime), tpr.last_price, tpr.last_quantity], 'name': 'trade', 'label': { 'show': True, 'formatter': label_tooltip, 'offset': [0, -30], 'fontSize': 15, 'fontFamily': 'monospace', 'color': 'black' }, 'symbol': 'triangle', 'symbolSize': 8 + 3 * tpr.last_quantity, 'symbolKeepAspect': False, 'itemStyle': { 'color': 'blue' if tpr.side == 'BUY' else 'green' }, 'emphasis': { 'label': { 'show': True, 'formatter': label_tooltip, 'offset': [0, -30], 'fontSize': 30, 'fontFamily': 'monospace', 'color': 'black', 'fontWeight': 'bolder' }, 'itemStyle': { 'color': 'black' } }, 'symbolRotate': 0 if tpr.side == 'BUY' else 180 } for tpr in trade_points_raw] trade_lines = [[{ 'coord': [str(tpr1.datetime), tpr1.last_price], 'lineStyle': { 'type': 'dashed', 'color': 'red' if ((tpr2.last_price - tpr1.last_price) if tpr2.side == 'SELL' else (tpr1.last_price - tpr2.last_price)) >= 0 else 'green' }, 'emphasis': { 'lineStyle': { 'type': 'dashed', 'color': 'red' if ((tpr2.last_price - tpr1.last_price) if tpr2.side == 'SELL' else (tpr1.last_price - tpr2.last_price)) >= 0 else 'green', 'width': 2 + 0.1 * abs(tpr2.last_price - tpr1.last_price) * abs(tpr1.last_quantity) }, 'label': { 'show': True, 'formatter': f'{abs(tpr2.last_price - tpr1.last_price)*abs(tpr1.last_quantity)}', 'position': 'middle', 'fontSize': 25, } }, }, { 'coord': [str(tpr2.datetime), tpr2.last_price], 'lineStyle': { 'type': 'dashed', 'color': 'red' if ((tpr2.last_price - tpr1.last_price) if tpr2.side == 'SELL' else (tpr1.last_price - tpr2.last_price)) >= 0 else 'green', }, 'emphasis': { 'lineStyle': { 'type': 'dashed', 'color': 'red' if ((tpr2.last_price - tpr1.last_price) if tpr2.side == 'SELL' else (tpr1.last_price - tpr2.last_price)) >= 0 else 'green', 'width': 2 + 0.1 * abs(tpr2.last_price - tpr1.last_price) * abs(tpr1.last_quantity) }, 'label': { 'show': True, 'formatter': f'{abs(tpr2.last_price - tpr1.last_price)*abs(tpr1.last_quantity)}', 'position': 'middle', 'fontSize': 25, } }, }] for tpr1, tpr2 in trade_lines_raw] _from = trade_points_raw[0].datetime - dt.timedelta(minutes=60) _to = trade_points_raw[-1].datetime + dt.timedelta(minutes=60) code = trade.order_book_id[0] f = Future(user='******', pwd='kairuitouzi') if code[:3] not in [ 'HSI', 'MHI', 'HHI' ] else HKFuture(user='******', pwd='kairuitouzi') _bars = f.get_bars(code, fields=['datetime', 'open', 'close', 'low', 'high'], start=_from, end=_to) x_axis = _bars[0].astype(str) kline = Kline(f'{code}-1min KLine Profit:{profit}') kline.add( f'{code}', x_axis, _bars[1:].T, mark_point_raw=trade_points, mark_line_raw=trade_lines, # label_formatter=label_tooltip, # xaxis_type='category', tooltip_trigger='axis', tooltip_formatter=kline_tooltip, is_datazoom_show=True, datazoom_range=[0, 100], datazoom_type='horizontal', is_more_utils=True) overlap_kline = Overlap('KLine', width='1500px', height='600px') overlap_kline.add(kline) _close = _bars[2].astype(float) MA = {} for w in [5, 10, 30, 60]: ma = talib.MA(_close, timeperiod=w) MA[w] = Line(f'MA{w}') MA[w].add(f'MA{w}', x_axis, np.round(ma, 2), is_symbol_show=False, is_smooth=True) overlap_kline.add(MA[w]) macdDIFF, macdDEA, macd = talib.MACDEXT(_close, fastperiod=12, fastmatype=1, slowperiod=26, slowmatype=1, signalperiod=9, signalmatype=1) diff_line = Line('diff') dea_line = Line('dea') macd_bar = Bar('macd') diff_line.add('diff', x_axis, macdDIFF, line_color='yellow', is_symbol_show=False, is_smooth=True) dea_line.add('diff', x_axis, macdDEA, line_color='blue', is_symbol_show=False, is_smooth=True) macd_bar.add('macd', x_axis, macd, is_visualmap=True, visual_type='color', is_piecewise=True, pieces=[{ max: 0 }, { min: 0 }]) overlap_macd = Overlap('MACD', width='1500px', height='200px') overlap_macd.add(diff_line) overlap_macd.add(dea_line) overlap_macd.add(macd_bar) grid = Grid('Anlysis', width='1500px', height='800px') grid.add(overlap_kline, grid_top='0%') grid.add(overlap_macd, grid_top='80%') return grid.render_embed()
def echart1(request): template = loader.get_template('myfirstvis/pyecharts.html') # # # value = [20, 190, 253, 77, 65] # # attr = ['汕头市', '汕尾市', '揭阳市', '阳江市', '肇庆市'] # # map = Map("广东地图示例", width=1200, height=600) # # map.add("", attr, value, maptype='广东', is_visualmap=True, # # visual_text_color='#000') # #map.render() # # context = dict( # # myechart=map.render_embed(), # # host=REMOTE_HOST, # # script_list=map.get_js_dependencies() # # ) # # # gauge = Gauge("仪表盘示例") # # gauge.add("业务指标", "完成率", 66.66) # # gauge.show_config() # line = Line("折线图示例", width=1200, height=700) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], legend_top="50%", mark_line=["average"], # 设置 dataZoom 控制索引为 0,1 的 x 轴,即第一个和第二个 is_datazoom_show=True, datazoom_xaxis_index=[0, 1]) v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例", title_top="50%") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, is_datazoom_show=True) grid = Grid() grid.add(line, grid_top="60%") grid.add(kline, grid_bottom="60%") # grid.render() context = dict( myechart=grid.render_embed(), host=REMOTE_HOST, script_list=grid.get_js_dependencies() ) # # value = [155, 10, 66, 78] # # attr = ["福建", "山东", "北京", "上海"] # # map = Map("全国地图示例", width=1200, height=600) # # map.add("", attr, value, maptype='china', # # is_visualmap=True, is_piecewise=True, # # visual_text_color="#000", # # visual_range_text=["", ""], # # pieces=[ # # {"max": 160, "min": 70, "label": "高数值"}, # # {"max": 69, "min": 0, "label": "低数值"}, # # ]) # # # # context = dict( # # myechart=map.render_embed(), # # host=REMOTE_HOST, # # script_list=map.get_js_dependencies() # # style = Style( # title_top="#fff", # title_pos="center", # width=1200, # height=600, # background_color="#404a59" # ) # # data_guangzhou = [ # ["广州", "上海"], # ["广州", "北京"], # ["广州", "南京"], # ["广州", "重庆"], # ["广州", "兰州"], # ["广州", "杭州"] # ] # geolines = GeoLines("GeoLines 示例", **style.init_style) # geolines.add("从广州出发", data_guangzhou, is_legend_show=False) # # context = dict( # myechart=geolines.render_embed(), # host=REMOTE_HOST, # script_list=geolines.get_js_dependencies() # ) return HttpResponse(template.render(context, request))