Package: pydsxkline
pydsxklinepydsxkline是一个有趣的python包，一行代码即可显示K线图，主要应用于股票金融量化领域，当您想要把股票数据图形化的时候，可以试试这个小工具，希望能帮到有需要的朋友。pydsxkline基于dsxkline进行封装,具体的接口可以查看官网http://www.dsxkline.com预览Getting startedInstallpipinstallpydsxklineCurrently only python version 3.8 or older supportedHello worldfrompydsxkline.dsxklineimportDsxKlineDsxKline.show("sh000001","上证指数")分时图DsxKline.show("sh000001","上证指数",CycleType.t)五日图# 五日图DsxKline.show("sh000001","上证指数",CycleType.t5)日KDsxKline.show("sh000001","上证指数",CycleType.day)周KDsxKline.show("sh000001","上证指数",CycleType.week)月KDsxKline.show("sh000001","上证指数",CycleType.month)年KDsxKline.show("sh000001","上证指数",CycleType.year)1分钟k线DsxKline.show("sh000001","上证指数",CycleType.m1)自定义DsxKline.show("sh000001","上证指数",CycleType.day,FqType.qfq,theme=DsxThemeName.white,sides["VOL","MACD","KDJ","RSI","WR","CCI","PSY","BIAS"],height=1600)加载本地数据# 日线数据datas=["20210915,3651.16,3677.53,3638.32,3656.22,474970001.00,60423154.41","20210916,3664.84,3677.92,3606.73,3607.09,546741474.00,67395534.04","20210917,3595.27,3620.96,3569.27,3613.97,516850210.00,62883439.43","20210922,3563.21,3629.45,3560.50,3628.49,472296053.00,55987472.96","20210923,3651.27,3670.95,3632.28,3642.22,534995486.00,63321892.91","20210924,3637.87,3651.43,3607.79,3613.07,507304772.00,60678860.91",...]DsxKline.show("sh000001","上证指数",datas=datas,enable_data_api=False,cycle=CycleType.day)画买卖点defdraw_event():return[DsxKline.draw_cycle_with_date("20230313","买","red","#ffffff"),DsxKline.draw_cycle_with_date("20221129","卖","green","#ffffff",12.99),DsxKline.draw_cycle_with_date("202303241104","买","red","#ffffff")]DsxKline.show("sh000001","上证指数",CycleType.day,draw_event=draw_event(),main=["SAR"])自定义头部HTMLheader="""<h1 style="color:#fff;text-align:center;font-size:20px;line-height:50px;border-bottom:1px solid #191b28">这是一个大师兄线图 pydsxkline </h1><ul class="mycss"><li><span>累计收益率：</span><b>30.6%</b></li><li><span>年化收益率：</span><b>80.6%</b></li><li><span>夏普比率：</span><b>0.35</b></li><li><span>盈亏比：</span><b>1.2</b></li><li><span>胜率：</span><b>67.6%</b></li><li><span>最大回撤：</span><b>30.6%</b></li><li><span>最大收益率：</span><b>10.6%</b></li><li><span>最小收益率：</span><b>-8.6%</b></li><li><span>总资产：</span><b>240098.9 元</b></li><li><span>盈利：</span><b>20366 元</b></li><li><span>亏损：</span><b>-90880 元</b></li></ul><style>.mycss{list-style:none;padding:10px 20px;color:#c5cbc0;font-size:14px;}.mycss li{float:left;width:25%;padding:5px 0;}.mycss li span{color:#c5cbce;}</style>"""DsxKline.show("sh000001","上证指数",CycleType.day,header_html=header,header_height=160)自定义指标数据# 自定义指标数据# 这个要准备跟K线数据一一对应的指标数据# 指标名称name1="MYMA"def_install_js():# 指标值名称 MA10，line 画折线，颜色 #ff0000_model=[DrawModel("MA10","line","#ff0000")]# 支持蜡烛图_chartType=[ChartType.candle]# 副图 sides 或者主图 main 或者两者_location=["sides"]# 指标数据_datas=[]# 这里模拟计算一个均线指标N=10# 获取K线数据klines=get_datas()foriinrange(len(klines)):ifi>=N:amount=0forjinrange(max(0,i-N+1),i+1):subitem=klines[j]subobj=subitem.split(",")close=float(subobj[4])amount+=closeMA10=amount/N_datas.append({"MA10":MA10})else:_datas.append({"MA10":None})# 生成jsinstall_js=DsxKline.get_install_index_js(name1,_model,_chartType,_location,_datas)returninstall_jsDsxKline.show("sh000001","上证指数",CycleType.day,main=["SAR"],sides=[name1,"VOL","MACD","KDJ"],datas=get_datas(),enable_data_api=False,install_index_js=_install_js())自定义指标算法自定义指标算法比较麻烦，因为js引擎只能执行js代码，所以指标算法需要用js代码来写# 自定义指标算法，这个比较麻烦，算法需要用js来写name2="MYMAMA"def_create_index_js():# 指标值名称 MA10，line 画折线，颜色 #ff0000_model=[DrawModel("MA10","line","#ff0000"),DrawModel("MA30","line","#ffff00")]# 支持蜡烛图_chartType=[ChartType.candle]# 副图 sides 或者主图 main 或者两者_location=["sides"]# 指标算法帧函数_func="""// 这里写js函数代码var _name2 = "%s";var _func = function(){// 取得当前坐标数据var item = this.datas[this.day];// 初始化指标if (item.ZHIBIAO[_name2] == null)item.ZHIBIAO[_name2] = {};// 开始计算指标值，这里我们计算个均线指标吧var N = 10;if(this.day<N) {item.ZHIBIAO[_name2]["MA10"] = null;item.ZHIBIAO[_name2]["MA30"] = null;return;}var amount = 0;for(var i=Math.max(this.day-N+1,0);i<=this.day;i++){var subitem = this.datas[i];amount += subitem.CLOSE;}var MA10 = amount / N;item.ZHIBIAO[_name2]["MA10"] = MA10;N = 30;if(this.day<N) {item.ZHIBIAO[_name2]["MA30"] = null;return;}amount = 0;for(var i=Math.max(this.day-N+1,0);i<=this.day;i++){var subitem = this.datas[i];// 这里模型数据里有K线所有的字段值可以引用amount += subitem.CLOSE;}var MA30 = amount / N;// 保存指标值item.ZHIBIAO[_name2]["MA30"] = MA30;}"""%name2# 生成jscreate_js=DsxKline.get_create_index_js(name2,_model,_chartType,_location,_func)returncreate_jsDsxKline.show("sh000001","上证指数",CycleType.day,main=["SAR"],sides=[name2,"VOL","MACD","KDJ"],create_index_js=_create_index_js())参数属性详细的文档可参考：http://www.dsxkline.com# 名称name=""# 证券代码symbol=""# 昨日收盘价last_close=0# 宽width=800# 高height=600# 图表类型chartType=ChartType.timeSharing# 周期类型cycle=CycleType.t# 复权类型fq=FqType.default# 主题theme=DsxThemeName.dark# 主图指标main=["MA"]# 副图指标sides=["VOL","MACD","RSI"]# 数据datas=[]# 每页请求数据大小page_size=320# 页码page=1# 十字线回调on_crossing=None# 更新完成update_complate=None# 副图高度side_height=80# 底部间距padding_bottom=10# 蜡烛图类型 默认空心candel_type=CandleType.hollow# 缩放类型 默认固定左边进行缩放zoom_lock_type=ZoomLockType.right# 是否显示提示面板，十字线移动的时候显示的详情面板is_show_kline_tip_pannel=True# 自动适配，k线图根据父窗口的大小自动调整autosize=False# 是否启用内置行情数据接口，当使用本地数据的时候请关闭设置为 falseenable_data_api=True# debugdebug=False# 画图事件集合draw_event=None# 头部htmlheader_html=None# 头部高度header_height=0# 安装自定义指标数据install_index_js=install_index_js# 创建自定义指标算法js代码create_index_js=create_index_js