繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

交易列表分析小工具

作者/sdeewew 2019-05-10 06:01 0 来源: FX168财经网人物频道

实现功能:
列表交易次数统计;
交易次数折线图;
交易k线图买入卖出标注;

如果想要实现放大功能,聚宽没有对应的包,本地可以:
法一:
在前面加 %matlabplotlib notebook ,可惜聚宽报错,可能是装包的版本问题

法二:
Now thanks to mpld3 it's super easy to enable zooming in inline plots!

All you have to do is install mpld3 (pip install mpld3), and then add this to your notebook:

%matplotlib inline
import mpld3
mpld3.enable_notebook()
Now your plots will get a toolbar menu at the bottom left, in which you can enable mouse zooming :)

import tushare as ts
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
from matplotlib.pylab import date2num
import datetime
import pandas as pd

class tradelist_analysis(object):
    
    def __init__(self, path=None,symbol=None):
        self.path = path
        self.symbol = symbol
    
    # 交易次数统计
    def symbol_counts(self):
        syms = self.extract_symbols()
        return syms.value_counts()
    
    def plot_symbol_counts(self):
        syms = self.extract_symbols()
        syms.value_counts().plot()

    # 读取交易列表
    def get_all_df(self):
        df = pd.read_csv(self.path) # 导出的时候设置转为utf8无bom格式
        df = df[~(df['成交价']=='--')]
        return df
    
    # 提取股票代码
    def extract_symbols(self):
        df = self.get_all_df()
        pattern = r'[0-9]{6}.[A-Z]{4}'
        syms = df['标的'].str.findall(pattern)
        syms = syms.apply(lambda x : x[0])
        return syms

    # 股票代码添加后缀
    def trans_symbol(self):
        if len(self.symbol)==11:
            return self.symbol
      
        if int(self.symbol[0])==6:
            self.symbol = self.symbol+'.XSHG'
        else:
            self.symbol = self.symbol+'.XSHE'
        return self.symbol
            
    # 获取个股买入卖出信息
    def get_symbol_df(self):
        self.symbol = self.trans_symbol()
        df = self.get_all_df()
        df['日期']=pd.to_datetime(df['日期'])
        bs = df['标的'].str.contains(self.symbol)
        dfs = df[bs]
        return dfs

    # 获取行情数据
    def get_hist_data(self):
        self.symbol = self.trans_symbol()
        dfs = self.get_symbol_df()
        start = dfs['日期'].iloc[0]
        end = dfs['日期'].iloc[-1]
        
        hist_data = get_price(self.symbol,start_date = start-datetime.timedelta(5), end_date=end+datetime.timedelta(5), frequency='daily', fields=['open', 'high','low','close'])
        hist_data.reset_index(inplace=True)
        hist_data['index']=hist_data['index'].apply(lambda s:date2num(s))
        hist_data=hist_data.as_matrix()
        return hist_data
  
    # 创建子图
    def plot_candle(self):
        hist_data=self.get_hist_data()
        dfs = self.get_symbol_df()
        
        fig, ax = plt.subplots()
        fig.subplots_adjust(bottom=0.1)

        # 设置X轴刻度为日期时间
        ax.xaxis_date()
        ax.autoscale_view()
        plt.xticks(rotation=45)
        plt.yticks()
        plt.title("股票代码:xx")
        plt.xlabel("时间")
        plt.ylabel("股价(元)")
        mpf.candlestick_ohlc(ax,hist_data,width=1,colorup='r',colordown='green')
        plt.grid()

            
        for i in range(len(dfs)):
            dt = dfs['日期'].iloc[i]
            price = dfs['成交价'].iloc[i]
            #print dt,price
            if dfs['交易类型'].iloc[i]=='买':
                ax.annotate('buy%s'%price, xy=(dt, float(price)), xytext=(dt, float(price)-0.2),arrowprops=dict(facecolor='black', shrink=0.1),)
            else:
                ax.annotate('sell%s'%price, xy=(dt, float(price)), xytext=(dt, float(price)+0.2),arrowprops=dict(facecolor='yellow', shrink=0.1),)
ta = tradelist_analysis('transaction_1.csv','000498')
ta.plot_candle()
ta.get_symbol_df()
日期 时间 品种 标的 交易类型 下单类型 数量 成交价 成交额 平仓盈亏 手续费 成交状态
2708 2015-04-29 09:30 股票 山东路桥(000498.XSHE) 市价单 6400股 7.71 49344 0 14.80 全部成交
2841 2015-05-08 09:30 股票 山东路桥(000498.XSHE) 市价单 -6400股 7.11 (45504) -3840 59.16 全部成交
2856 2015-05-08 09:30 股票 山东路桥(000498.XSHE) 市价单 7000股 7.13 49910 0 14.97 全部成交
2882 2015-05-20 09:30 股票 山东路桥(000498.XSHE) 市价单 -7000股 9.13 (63910) 14000 83.08 全部成交
2885 2015-05-20 09:30 股票 山东路桥(000498.XSHE) 市价单 5400股 9.15 49410 0 14.82 全部成交
2999 2015-06-01 09:30 股票 山东路桥(000498.XSHE) 市价单 -5400股 8.69 (46926) -2484 61.00 全部成交
3299 2015-06-30 09:30 股票 山东路桥(000498.XSHE) 市价单 5900股 8.41 49619 0 14.89 全部成交
3335 2015-07-03 09:30 股票 山东路桥(000498.XSHE) 市价单 -5900股 7.39 (43601) -6018 56.68 全部成交
3358 2015-07-03 09:30 股票 山东路桥(000498.XSHE) 市价单 6700股 7.41 49647 0 14.89 全部成交
3392 2015-07-06 09:30 股票 山东路桥(000498.XSHE) 市价单 -6700股 7.64 (51188) 1541 66.54 全部成交
3472 2015-07-07 09:30 股票 山东路桥(000498.XSHE) 市价单 7900股 6.29 49691 0 14.91 全部成交
3612 2015-07-10 09:30 股票 山东路桥(000498.XSHE) 市价单 -7900股 6.36 (50244) 553 65.32 全部成交
3619 2015-07-10 09:30 股票 山东路桥(000498.XSHE) 市价单 7800股 6.38 49764 0 14.93 全部成交
3849 2015-07-21 09:30 股票 山东路桥(000498.XSHE) 市价单 -7800股 7.78 (60684) 10920 78.89 全部成交
3852 2015-07-21 09:30 股票 山东路桥(000498.XSHE) 市价单 6400股 7.8 49920 0 14.98 全部成交
3949 2015-07-29 09:30 股票 山东路桥(000498.XSHE) 市价单 -6400股 7.22 (46208) -3712 60.07 全部成交
3952 2015-07-29 09:30 股票 山东路桥(000498.XSHE) 市价单 6900股 7.24 49956 0 14.99 全部成交
4070 2015-08-18 09:30 股票 山东路桥(000498.XSHE) 市价单 -6900股 9.77 (67413) 17457 87.64 全部成交
4091 2015-08-18 09:30 股票 山东路桥(000498.XSHE) 市价单 5100股 9.79 49929 0 14.98 全部成交
4118 2015-08-19 09:30 股票 山东路桥(000498.XSHE) 市价单 -5100股 7.98 (40698) -9231 52.91 全部成交
4155 2015-08-19 09:30 股票 山东路桥(000498.XSHE) 市价单 6200股 8 49600 0 14.88 全部成交
4228 2015-08-24 09:30 股票 山东路桥(000498.XSHE) 市价单 -6200股 6.99 (43338) -6262 56.34 全部成交
4280 2015-08-24 09:30 股票 山东路桥(000498.XSHE) 市价单 7100股 7.01 49771 0 14.93 全部成交
4414 2015-08-26 09:30 股票 山东路桥(000498.XSHE) 市价单 -7100股 6.19 (43949) -5822 57.13 全部成交
4533 2015-08-26 09:30 股票 山东路桥(000498.XSHE) 市价单 8000股 6.21 49680 0 14.90 全部成交
4833 2015-09-07 09:30 股票 山东路桥(000498.XSHE) 市价单 -8000股 5.71 (45680) -4000 59.38 全部成交
5482 2015-10-30 09:30 股票 山东路桥(000498.XSHE) 市价单 7400股 6.76 50024 0 15.01 全部成交
5831 2015-12-24 09:30 股票 山东路桥(000498.XSHE) 市价单 -7400股 8.47 (62678) 12654 81.48 全部成交
5833 2015-12-24 09:30 股票 山东路桥(000498.XSHE) 市价单 5800股 8.49 49242 0 14.77 全部成交
6004 2016-01-08 09:30 股票 山东路桥(000498.XSHE) 市价单 -5800股 8.05 (46690) -2552 60.70 全部成交
6037 2016-01-08 09:30 股票 山东路桥(000498.XSHE) 市价单 6200股 8.07 50034 0 15.01 全部成交
6210 2016-01-14 09:30 股票 山东路桥(000498.XSHE) 市价单 -6200股 7.01 (43462) -6572 56.50 全部成交
6280 2016-01-20 09:30 股票 山东路桥(000498.XSHE) 市价单 6000股 8.33 49980 0 14.99 全部成交
6281 2016-01-21 09:30 股票 山东路桥(000498.XSHE) 市价单 -6000股 7.58 (45480) -4500 59.12 全部成交
6282 2016-01-21 09:30 股票 山东路桥(000498.XSHE) 市价单 6500股 7.6 49400 0 14.82 全部成交
6286 2016-01-22 09:30 股票 山东路桥(000498.XSHE) 市价单 -6500股 7.22 (46930) -2470 61.01 全部成交
6296 2016-01-22 09:30 股票 山东路桥(000498.XSHE) 市价单 6900股 7.24 49956 0 14.99 全部成交
6368 2016-01-27 09:30 股票 山东路桥(000498.XSHE) 市价单 -6900股 6.48 (44712) -5244 58.13 全部成交
6408 2016-01-27 09:30 股票 山东路桥(000498.XSHE) 市价单 7700股 6.5 50050 0 15.01 全部成交
6461 2016-01-28 09:30 股票 山东路桥(000498.XSHE) 市价单 -7700股 5.99 (46123) -3927 59.96 全部成交
6467 2016-01-28 09:30 股票 山东路桥(000498.XSHE) 市价单 8300股 6.01 49883 0 14.96 全部成交
6501 2016-01-29 09:30 股票 山东路桥(000498.XSHE) 市价单 -8300股 5.5 (45650) -4233 59.35 全部成交
6571 2016-01-29 09:30 股票 山东路桥(000498.XSHE) 市价单 9000股 5.52 49680 0 14.90 全部成交
7210 2016-05-05 09:30 股票 山东路桥(000498.XSHE) 市价单 -9000股 7.07 (63630) 13950 82.72 全部成交
7691 2016-08-30 09:30 股票 山东路桥(000498.XSHE) 市价单 6800股 7.36 50048 0 15.01 全部成交
7747 2016-09-13 09:30 股票 山东路桥(000498.XSHE) 市价单 -6800股 6.82 (46376) -3672 60.29 全部成交
7805 2016-10-25 09:30 股票 山东路桥(000498.XSHE) 市价单 6700股 7.45 49915 0 14.97 全部成交
8841 2017-05-23 09:30 股票 山东路桥(000498.XSHE) 市价单 -6700股 6.88 (46096) -3819 59.92 全部成交
8876 2017-05-24 09:30 股票 山东路桥(000498.XSHE) 市价单 7400股 6.69 49506 0 14.85 全部成交
9291 2017-07-20 09:30 股票 山东路桥(000498.XSHE) 市价单 -7400股 8.34 (61716) 12210 80.23 全部成交
9302 2017-07-20 09:30 股票 山东路桥(000498.XSHE) 市价单 5900股 8.36 49324 0 14.80 全部成交
9433 2017-08-04 09:30 股票 山东路桥(000498.XSHE) 市价单 -5900股 7.76 (45784) -3540 59.52 全部成交
9435 2017-08-04 09:30 股票 山东路桥(000498.XSHE) 市价单 6400股 7.78 49792 0 14.94 全部成交
 
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2208文章4145粉丝45

    暂无个人简介信息

  • 张迎妤

    人气1904文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 指导老师

    人气1864文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2320文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 梁孟梵

    人气2184文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 王启蒙现货黄金

    人气320文章3490粉丝8

    本人做分析师以来,并专注于贵金属投资市场,尤其是在现货黄金...

  • 金泰铬J

    人气2328文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4760文章8329粉丝119

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...

FX168财经

FX168财经学院

FX168财经

FX168北美