Пример #1
0
    def v_tradecost(self, start=None, end=yesterdayobj(), vopts=None):
        """
        visualization giving the average cost line together with netvalue line

        :param vopts: global option for line in pyecharts
        :returns: pyecharts.line
        """
        funddata = []
        costdata = []
        pprice = self.aim.price[self.aim.price["date"] <= end]
        if start is not None:
            pprice = pprice[pprice["date"] >= start]
        for _, row in pprice.iterrows():
            date = row["date"]
            funddata.append(row["netvalue"])
            cost = 0
            if (date - self.cftable.iloc[0].date).days >= 0:
                cost = self.unitcost(date)
            costdata.append(cost)

        line = Line()
        if vopts is None:
            vopts = line_opts

        line.add_xaxis([d.date() for d in pprice.date])
        line.add_yaxis(series_name="基金净值", y_axis=funddata, is_symbol_show=False)
        line.add_yaxis(series_name="持仓成本", y_axis=costdata, is_symbol_show=False)
        line.set_global_opts(**vopts)
        return line.render_notebook()
Пример #2
0
    def v_netvalue(self, end=yesterdayobj(), benchmark=True, rendered=True, vopts=None):
        """
        visulaization on  netvalue curve

        :param end: dateobject for indicating the end date in the figure, default to yesterday
        :param benchmark: bool, whether include benchmark's netvalue curve, default true
        :param vopts: dict, options for pyecharts instead of builtin settings
        """
        if getattr(self, "bmprice", None) is None:
            benchmark = False
        if benchmark:
            a, b = self.comparison(end)
        else:
            a = self.price
        if vopts is None:
            vopts = line_opts
        line = Line()
        line.add_xaxis([d.date() for d in list(a.date)])
        line.add_yaxis(
            y_axis=list(a.netvalue), series_name=self.name, is_symbol_show=False
        )
        line.set_global_opts(**vopts)
        if benchmark:
            line.add_yaxis(
                series_name=self.benchmark.name,
                y_axis=list(b.netvalue),
                is_symbol_show=False,
            )
        if rendered:
            return line.render_notebook()
        else:
            return line
Пример #3
0
    def v_techindex(self, end=yesterdayobj(), col=None, rendered=True, vopts=None):
        """
        visualization on netvalue curve and specified indicators

        :param end: date string or obj, the end date of the figure
        :param col: list, list of strings for price col name, eg.['MA5','BBI']
            remember generate these indicators before the visualization,
            these cols don't automatically generate for visualization
        :param vopts: dict, options for pyecharts instead of builtin settings
        """
        partprice = self.price[self.price["date"] <= end]
        xdata = [d.date() for d in list(partprice.date)]
        netvaldata = list(partprice.netvalue)
        if vopts is None:
            vopts = line_opts
        line = Line()
        line.add_xaxis(xdata)
        line.add_yaxis(series_name="netvalue", y_axis=netvaldata, is_symbol_show=False)
        line.set_global_opts(**vopts)
        if col is not None:
            for ind in col:
                inddata = list(partprice[ind])
                line.add_yaxis(series_name=ind, y_axis=inddata, is_symbol_show=False)
        if rendered:
            return line.render_notebook()
        else:
            return line
Пример #4
0
    def v_netvalue(self, end=yesterdayobj(), vopts=None, rendered=True):
        """
        起点对齐归一的,各参考基金或指数的净值比较可视化

        :param end: string or object of date, the end date of the line
        :param vkwds: pyechart line.add() options
        :param vopts: dict, options for pyecharts instead of builtin settings
        :returns: pyecharts.charts.Line.render_notebook()
        """
        partprice = self.totprice[self.totprice["date"] <= end]

        line = Line()
        if vopts is None:
            vopts = line_opts
        line.set_global_opts(**vopts)
        line.add_xaxis([d.date() for d in list(partprice.date)])
        for fund in self.fundobjs:
            line.add_yaxis(
                series_name=fund.name,
                y_axis=list(partprice[fund.code]),
                is_symbol_show=False,
            )
        if rendered:
            return line.render_notebook()
        else:
            return line
Пример #5
0
    def v_totvalue(self, end=yesterdayobj(), rendered=True, vopts=None):
        """
        visualization on the total values daily change of the aim
        """
        partp = self.price[self.price["date"] >= self.cftable.iloc[0].date]
        # 多基金账单时起点可能非该基金持有起点
        partp = partp[partp["date"] <= end]

        date = [d.date() for d in partp.date]
        valuedata = [
            self.briefdailyreport(d).get("currentvalue", 0) for d in partp.date
        ]

        line = Line()
        if vopts is None:
            vopts = line_opts

        line.add_xaxis(date)
        line.add_yaxis(series_name="持仓总值",
                       y_axis=valuedata,
                       is_symbol_show=False)
        line.set_global_opts(**vopts)
        if rendered:
            return line.render_notebook()
        else:
            return line
Пример #6
0
def vtradecost(self,
               cftable,
               unitcost=False,
               start=None,
               end=yesterdayobj(),
               rendered=True):
    """
    visualization giving the average cost line together with netvalue line as well as buy and sell points

    :returns: pyecharts.line
    """
    funddata = []
    costdata = []
    cashdata = []  #kahar add in order to observe the sum of the cash
    funddata_test = []
    pprice = self.price[self.price["date"] <= end]
    pcftable = cftable
    if start is not None:
        pprice = pprice[pprice["date"] >= start]
        pcftable = pcftable[pcftable["date"] >= start]
    for _, row in pprice.iterrows():
        date = row["date"]
        funddata.append(row["netvalue"])
        funddata_test.append(3.2)
        if unitcost:
            cost = 0
            cash = 0
            if (date - self.cftable.iloc[0].date).days >= 0:
                cost = self.unitcost(date)
                cash = self.get_datecashvalue(date)
            costdata.append(cost)
            cashdata.append(cash / 1000000.0)
    coords = []
    # pcftable = pcftable[abs(pcftable["cash"]) > threhold]
    for i, r in pcftable.iterrows():
        coords.append(
            [r.date, pprice[pprice["date"] <= r.date].iloc[-1]["netvalue"]])

    upper = pcftable.cash.abs().max()
    lower = pcftable.cash.abs().min()
    if upper == lower:
        upper = 2 * lower + 1  # avoid zero in denominator

    def marker_factory(x, y):
        buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
        if buy < 0:
            color = "#ff7733"
        else:

            color = "#3366ff"
        size = (abs(buy) - lower) / (upper - lower) * 5 + 5
        return opts.MarkPointItem(
            coord=[x.date(), y],
            itemstyle_opts=opts.ItemStyleOpts(color=color),
            # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
            symbol="circle",
            symbol_size=size,
        )

    line = Line()

    line.add_xaxis([d.date() for d in pprice.date])
    print("costdata", costdata)
    if unitcost:
        line.add_yaxis(
            series_name="持仓成本",
            y_axis=costdata,
            is_symbol_show=False,
        )

    line.add_yaxis(
        series_name="持仓总额k6",
        y_axis=cashdata,
        is_symbol_show=False,
    )

    line.add_yaxis(
        series_name="基金净值",
        y_axis=funddata,
        is_symbol_show=False,
        markpoint_opts=opts.MarkPointOpts(
            data=[marker_factory(*c) for c in coords], ),
    )
    line.set_global_opts(
        datazoom_opts=[
            opts.DataZoomOpts(is_show=True,
                              type_="slider",
                              range_start=50,
                              range_end=100),
            opts.DataZoomOpts(
                is_show=True,
                type_="slider",
                orient="vertical",
                range_start=50,
                range_end=100,
            ),
        ],
        tooltip_opts=opts.TooltipOpts(
            is_show=True,
            trigger="axis",
            trigger_on="mousemove",
            axis_pointer_type="cross",
        ),
    )
    if rendered:
        return line.render_notebook()
    else:
        return line
from pyecharts import options as opts
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

# In[13]:

# 折线图
line = Line()  # 折线图对象

# x轴
line.add_xaxis(df.index.to_list())  # x轴数据:列表——日期

# 每个y轴
line.add_yaxis("开盘价", df['open'].round(2).to_list())
line.add_yaxis("收盘价", df['close'].round(2).to_list())

# 图标配置
line.set_global_opts(title_opts=opts.TitleOpts(title='百度股票2019年'),
                     tooltip_opts=opts.TooltipOpts(trigger='axix',
                                                   axis_pointer_type='cross'))

# In[15]:

# 渲染数据
line.render_notebook()
line.render()

# In[ ]:

# In[ ]:
Пример #8
0
# customMap = (
#     Map()
#         .add("商家A",  # 图例
#              [list(z) for z in zip(Faker.provinces, Faker.values())],  # 数据项
#              "china"  # 地图
#              )
#         .set_global_opts(  # 设置全局项
#         title_opts=opts.TitleOpts(  # 设置标题项
#             title="中国地图"  # 设置标题名称
#         )
#     )
# )
# customMap.render("demo11.html")  # 生成名为demo11的本地html文件

line = Line()
line.add_xaxis([
    "201{}年/{}季度".format(y, z) for y in range(4) for z in range(1, 5)
])  #设置x轴数据
line.add_yaxis("电视机销量", [
    4.80, 4.10, 6.00, 6.50, 5.80, 5.20, 6.80, 7.40, 6.00, 5.60, 7.50, 7.80,
    6.30, 5.90, 8.00, 8.40
])  #设置y轴数据
line.set_global_opts(
    xaxis_opts=opts.AxisOpts(
        axislabel_opts=opts.LabelOpts(rotate=-40), ),  #设置x轴标签旋转角度
    yaxis_opts=opts.AxisOpts(name="销量(单位/千台)"),  #设置y轴名称
    title_opts=opts.TitleOpts(title="折线图"))  #设置图表标题

line.render_notebook()  #渲染图表
Пример #9
0
    def v_tradecost_ktest(
        self, unitcost=True, start=None, end=yesterdayobj(), rendered=True
    ):
        """
        visualization giving the average cost line together with netvalue line as well as buy and sell points

        :returns: pyecharts.line
        """
        # funddata = []
        # costdata = []

        pprice = self.price[self.price["date"] <= end]
        pcftable = self.cftable

        if start is not None:
            pprice = pprice[pprice["date"] >= start]
            pcftable = pcftable[pcftable["date"] >= start]
        # for _, row in pprice.iterrows():
        #     date = row["date"]
        #     funddata.append(row["netvalue"])
        #     if unitcost:
        #         cost = 0
        #         if (date - self.cftable.iloc[0].date).days >= 0:
        #             cost = self.unitcost(date)
        #         costdata.append(cost)
        #
        coords = []
        # pcftable = pcftable[abs(pcftable["cash"]) > threhold]
        # for i, r in pcftable.iterrows():
            # coords.append([r.date, pprice[pprice["date"] <= r.date].iloc[-1]["netvalue"]])
        def marker_factory(x, y):
            buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
            print("buy",buy)
            if buy < 0:
                color = "#ff7733"
            else:

                color = "#3366ff"
            size = (abs(buy) - lower) / (upper - lower) * 5 + 5
            print("size",size)
            return opts.MarkPointItem(
                coord=[x.date(), y],
                itemstyle_opts=opts.ItemStyleOpts(color=color),
            # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
                symbol="circle",
                symbol_size=size,
            )

        def marker_factory3v(x, y):
            buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
            print("buy",buy)
            if buy < 0:
                color = "#ff7733"
            else:

                color = "#3366ff"
            # size = (abs(buy) - lower) / (upper - lower) * 5 + 5
            size = len(str(buy)) * 10
            print("size",size)
            return opts.MarkPointItem(
                value = -buy,
                coord=[x.date(), y],
                itemstyle_opts=opts.ItemStyleOpts(color=color),
            # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
                # symbol="circle",
                symbol_size=size,
            )



        coords_cash = []
        # print("pcftable",pcftable)
        # print("pprice",pprice)
           #cash begin
        cashdata = []
        valuedata = []
        # partp = self.price[self.price["date"] >= self.cftable.iloc[0].date]
        # 多基金账单时起点可能非该基金持有起点
        # partp = partp[partp["date"] <= end]

        # valuedata = [
            # self.briefdailyreport(d).get("currentvalue", 0) for d in partp.date
            # self.briefdailyreport(d).get("currentvalue", 0) for d in pprice.date
        # ]
        # print(pprice.date)
        # print("begin to cal ccost")
        for d in pprice.date:
            ucost = self.unitcost(d)
            cshare = self.briefdailyreport(d).get("currentshare")
            if(ucost is None or cshare is None  ):
                valuedata.append(0)
                cashdata.append(0)
            else:
                cashdata.append(ucost*cshare)
                valuedata.append(self.briefdailyreport(d).get("currentvalue", 0))
        #     # self.unitcost(d)*self.briefdailyreport(d).get("currentshare")  for d in partp.date
        #     self.unitcost(d)*self.briefdailyreport(d).get("currentshare")  for d in pprice.date
        # ]
       #cash end


      # date = [d.date() for d in partp.date]
        date = [d.date() for d in pprice.date]
        date_dict = {}
        for i,d in enumerate(date):
            date_dict[convert_date(d)]=i
        # print("date_dict",date_dict)
        # print("date",date)
        # print("cashdata",cashdata)
        # pcftable = pcftable[abs(pcftable["cash"]) > threhold]
        # print("print offset")
        offset = pcftable["date"]
        # print("offset",offset)
        offset = offset[0]
        offsetdate = []
        coords_markvalue = []
        pos = 3
        for i, r in pcftable.iterrows():
            # print("i,r",i,r)
            # print("r.date",type(r.date.date()))
            # print("convert_date(r.date)",type(convert_date(r.date)))
            # print("date_dict[r.date]",date_dict[convert_date(r.date.date())])


            offset = r.date - offset

            valuecoords = valuedata[date_dict[convert_date(r.date.date())]]
            cashcoords = cashdata[date_dict[convert_date(r.date.date())]]
            distance = valuecoords - cashcoords
            if(pos == 3):
                coords_markvalue.append([r.date,cashcoords,offset])
                pos =0
            elif(pos == 0 ):
                coords_markvalue.append([r.date,cashcoords + distance/2,offset])
                pos = 1
            elif(pos == 1):
                coords_markvalue.append([r.date,valuecoords,offset])
                pos =2
            elif(pos ==2 ):
                coords_markvalue.append([r.date,cashcoords,offset])
                pos = 0



            # if(distance>2000):
                # pos = 1
                # coords_markvalue.append([r.date,cashcoords + distance/2,offset])
            # elif(distance<-2000):
                #coords_markvalue.append([r.date,valuecoords - distance/2,offset])
                # coords_markvalue.append([r.date,valuecoords - distance/2,offset])
            # else:
                #coords_markvalue.append([r.date,valuecoords,offset])
                # coords_markvalue.append([r.date,cashcoords,offset])

            coords_cash.append([r.date, cashdata[date_dict[convert_date(r.date.date())]],offset])
            coords.append([r.date,cashdata[date_dict[convert_date(r.date.date())]]])
            offsetdate.append(offset)
            offset = r.date
        # offsetdate[0] = pd.Timedelta(1,unit = "D")

        print("coords_cash",coords_cash)
        print("len(coords_cash)",len(coords_cash))
        # print("offsetdate",offsetdate)
        print("len(offsetdate)",len(offsetdate))


        upper = pcftable.cash.abs().max()
        lower = pcftable.cash.abs().min()
        if upper == lower:
            upper = 2 * lower + 1  # avoid zero in denominator
        global previous
        global direction
        previous = 0
        direction = 1


        def marker_factory1v(x, y,offsetday):
            offset = 0
            buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
            markvalue = 0
            ycoor = y
            global direction
            global previous
            print("previous before:",previous)

            # loc = (abs(buy) - lower) / (upper - lower) * 5000 + 5000
            loc = len(str(buy)) * 1000 + 1000
            if buy < 0:
                color = "#ff7733"
                markvalue = -buy
                if(offsetday < pd.Timedelta(7,unit = "D")):
                    offset = -100
                    ycoor = y + loc*previous*direction
                    previous = 1
                    direction =-direction
                else:
                    previous = 0

            else:
                color = "#3366ff"
                markvalue = -buy
                if(offsetday < pd.Timedelta(7,unit = "D")):
                    ycoor = y + loc*previous*direction
                    direction = -direction
                    previous = 1
                else:
                    previous = 0
            print("previous after",previous)
            # size = (abs(buy) - lower) / (upper - lower) * 50 + 50
            size = len(str(buy)) * 10
            return opts.MarkPointItem(name="test",
                coord=[x.date(), ycoor],
                value = -buy,
                itemstyle_opts=opts.ItemStyleOpts(color=color),
                # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
                # symbol="circle",
                # symbol="rectangle",
                symbol_size=size,
            )
        def marker_factory2v(x, y,offsetday):
            offset = 0
            buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
            markvalue = 0
            ycoor = y
            global direction
            global previous
            print("previous before:",previous)
            print("offsetday",offsetday)
            print("day",x.date())

            # loc = (abs(buy) - lower) / (upper - lower) * 5000 + 5000
            loc = len(str(buy)) * 100
            if buy < 0:
                color = "#ff7733"
                markvalue = -buy
                if(offsetday < pd.Timedelta(5,unit = "D")):
                    offset = -100
                    #ycoor = y + loc*previous*direction
                    ycoor = y +loc*previous*direction
                    previous = 1
                    direction =-direction
                else:
                    previous = 0

            else:
                color = "#3366ff"
                markvalue = -buy
                if(offsetday < pd.Timedelta(5,unit = "D")):
                    #ycoor = y + loc*previous*direction
                    ycoor = y + loc*previous*direction
                    direction = -direction
                    previous = 1
                else:
                    previous = 0
            print("previous after",previous)
            # size = (abs(buy) - lower) / (upper - lower) * 50 + 50
            size = len(str(round(-buy))) * 12
            return opts.MarkPointItem(name="test",
                coord=[x.date(), ycoor],
                value = -round(buy),
                itemstyle_opts=opts.ItemStyleOpts(color=color),
                # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
                # symbol="circle",
                # symbol="rectangle",
                symbol_size=size,
            )


        # print("getting cashdata,pprice",pprice.date)


            # print("2cashdata",cashdata)
        # print("funddata",funddata)
        print("len(2chashdata)",len(cashdata))
        # print("funddata",len(funddata))
        # print("valuedata",valuedata)
        print("len(valuedata)",len(valuedata))

        line = Line()

        #line.add_xaxis([d.date() for d in pprice.date])
        line.add_xaxis(date)

        line.add_yaxis(
                       series_name="持仓总值1",
                       y_axis=valuedata,
                       # is_symbol_show=False,
                       # )

            # y_axis=funddata,
            is_symbol_show=False,
            markpoint_opts=opts.MarkPointOpts(
                data=[marker_factory2v(*c) for c in coords_markvalue],
            ),
        )
        line.extend_axis(
                         yaxis = opts.AxisOpts(
                                axislabel_opts = opts.LabelOpts(formatter="{value}%",)))
        print("xianjinzhi")
        line.add_yaxis(
            series_name="现金值",
            y_axis=cashdata,
            is_symbol_show=False,
            markpoint_opts=opts.MarkPointOpts(
                # data=[marker_factory(*c) for c in coords_cash],
                data=[marker_factory(*c) for c in coords],
                # symbol = "circle",
                # symbol_size =size,
            ),
        )

        def tooltip_factory(coords):
            for x,y in coords:
                return opts.TooltipOpts(
                is_show=True,
                trigger="axis",
                trigger_on="mousemove",
                axis_pointer_type="cross",
                formatter = str(y)
            )

        line.set_global_opts(
            datazoom_opts=[
                opts.DataZoomOpts(
                    is_show=True, type_="slider", range_start=50, range_end=100
                ),
                opts.DataZoomOpts(
                    is_show=True,
                    type_="slider",
                    orient="vertical",
                    range_start=50,
                    range_end=100,
                ),
            ],
            tooltip_opts=opts.TooltipOpts(
                is_show=True,
                trigger="axis",
                # trigger_on="mousemove",
                trigger_on="click",
                axis_pointer_type="cross",
                # formatter = [tooltip_factory(*c) for c in coords]
                formatter = JsCode(
                    """function(params){
                    if(params.dataIndex){
                    return params.dataIndex
                    }
                    else{
                    return params
                    }
                }
                    """
                )
            ),
            # tooltip_opts=tooltip_factory(coords)
            # tooltip_opts=tooltip_factory(coords)
        )

        if rendered:
            return line.render_notebook()
Пример #10
0
    def v_totcash(self, end=yesterdayobj(), rendered=True, vopts=None):
        """
        kahar 20211215 in order to observe the profit
        visualization on the total values daily change of the aim
        """
        partp = self.price[self.price["date"] >= self.cftable.iloc[0].date]
        # 多基金账单时起点可能非该基金持有起点
        partp = partp[partp["date"] <= end]

        date = [d.date() for d in partp.date]
        valuedata = [
            self.briefdailyreport(d).get("currentvalue", 0) for d in partp.date
        ]
        cashdata = [
            self.unitcost(d)*self.briefdailyreport(d).get("currentshare")  for d in partp.date
        ]
        #print(cashdata)

        #mark buy sell begin

        pprice = self.price[self.price["date"] <= end]
        pcftable = self.cftable
        coords = []
        # pcftable = pcftable[abs(pcftable["cash"]) > threhold]
        for i, r in pcftable.iterrows():
            coords.append([r.date, pprice[pprice["date"] <= r.date].iloc[-1]["netvalue"]])
        print("coords:",coords)
        upper = pcftable.cash.abs().max()
        lower = pcftable.cash.abs().min()
        if upper == lower:
            upper = 2 * lower + 1  # avoid zero in denominator


        def marker_factory(x, y):
            buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
            print("buy",buy)
            if buy < 0:
                color = "#ff7733"
            else:

                color = "#3366ff"
            size = (abs(buy) - lower) / (upper - lower) * 5 + 5
            print("size",size)
            return opts.MarkPointItem(
                coord=[x.date(), y],
                itemstyle_opts=opts.ItemStyleOpts(color=color),
            # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
                symbol="circle",
                symbol_size=size,
            )


        #mark buy sell end
        line = Line()

        line.add_xaxis(date)
        line.add_yaxis(series_name="持仓总值1",
                       y_axis=valuedata,
                       is_symbol_show=False,
                        markpoint_opts=opts.MarkPointOpts(
                            data=[marker_factory(*c) for c in coords],
                            ),
                       )
        line.add_yaxis(series_name="现金值", y_axis=cashdata,
                       is_symbol_show=False,
                       markpoint_opts=opts.MarkPointOpts(
                       data=[marker_factory(*c) for c in coords],
                        ),
                    )
        #
        line.set_global_opts(
            datazoom_opts=[
            opts.DataZoomOpts(
                is_show=True, type_="slider", range_start=50, range_end=100
            ),
            opts.DataZoomOpts(
                is_show=True,
                type_="slider",
                orient="vertical",
                range_start=50,
                range_end=100,
                ),
            ],
            tooltip_opts=opts.TooltipOpts(
            is_show=True,
            trigger="axis",
            trigger_on="mousemove",
            axis_pointer_type="cross",
            ),
        )
        if rendered:
            return line.render_notebook()
        else:
            return line