首页 > 其他 > 详细

用matplotlib绘制每次交易的盈亏三角形

时间:2018-02-05 00:04:07      阅读:546      评论:0      收藏:0      [点我收藏+]

用matplotlib绘制每次交易的盈亏三角形

结果:

技术分享图片

代码:

python

def plot_trade_triangle(self):        
    # plot each trade as a trade-triangle, and annotate pnl
    trade = self.trade
    equity = self.equity.equity
    fig,ax=plt.subplots()
    for dt, row in trade.iterrows():
        bars = row.buybar, row[‘sellbar‘]
        pnl = 0 , row[‘pnl‘]
        colors = ‘r‘ if row.pnl>0 else ‘g‘
        
        ax.plot( bars, pnl, color=colors)
        arrowprops = dict(facecolor=‘black‘, 
                          edgecolor=colors,
                          #shrink=0.05,
                          arrowstyle=‘->‘,)
        y_factor = 1.2 if abs(row.pnl) > 10. else 1.6
        colors = ‘yellow‘ if row.pnl>0 else ‘cyan‘
        valignment = ‘bottom‘ if row[‘pnl‘]>0 else ‘top‘
        note= ‘hld:%s\npnl:%s‘% (bars[1]-bars[0], str(row[‘pnl‘]))
        ax.annotate(note, xy=(row.sellbar, row.pnl), 
                    xytext=(row.sellbar, row.pnl*y_factor), 
                    va=valignment,
                    arrowprops=arrowprops,)   
        ax.fill_between(bars, pnl, color=colors)
        
        # 画: 持股期间的浮动盈亏:
        float_pnl=(roc1[row.buybar+1:row.sellbar+1]+1).cumprod()
        iloc =  [equity.index.get_loc(key) for key in float_pnl.index]
        float_pnl = pd.Series( (float_pnl.values-1)*100., index=iloc)
        ax.plot(float_pnl, color=‘b‘)
        ax.set_ylabel(‘float_pnl(%)‘)

用matplotlib绘制每次交易的盈亏三角形

原文:https://www.cnblogs.com/duan-qs/p/8414610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!