【量化笔记】使用python捕捉K线图形态

首先绘制K线图

import pandas as pd
ssec2015=pd.read_csv('ssec2015.csv')

ssec2015=ssec2015.iloc[:,1:]

ssec2015.head()
Date Open High Low Close Volume
0 2015-03-02 3332.720947 3336.760010 3298.668945 3336.284912 346400
1 2015-03-03 3317.695068 3317.695068 3260.428955 3263.052002 382000
2 2015-03-04 3264.181885 3286.587891 3250.483887 3279.532959 293600
3 2015-03-05 3264.084961 3266.637939 3221.666016 3248.476074 320700
4 2015-03-06 3248.035889 3266.933105 3234.532959 3241.187012 282900
ssec2015.tail(n=3)
Date Open High Low Close Volume
19 2015-03-27 3686.134033 3710.477051 3656.831055 3691.095947 408900
20 2015-03-30 3710.612061 3795.935059 3710.612061 3786.568115 564700
21 2015-03-31 3822.987061 3835.566895 3737.042969 3747.898926 561700
from matplotlib.dates import DateFormatter,WeekdayLocator,DayLocator,MONDAY,date2num

from datetime import datetime

ssec2015.Date=[date2num(datetime.strptime(date,"%Y-%m-%d")) for date in ssec2015.Date]

ssec15list
[Date      735659.000000
 Open        3332.720947
 High        3336.760010
 Low         3298.668945
 Close       3336.284912
 Volume    346400.000000
 Name: 0, dtype: float64, Date      735660.000000
 Open        3317.695068
 High        3317.695068
 Low         3260.428955
 Close       3263.052002
 Volume    382000.000000
 Name: 1, dtype: float64, Date      735661.000000
 Open        3264.181885
 High        3286.587891
 Low         3250.483887
 Close       3279.532959
 Volume    293600.000000
 Name: 2, dtype: float64, Date      735662.000000
 Open        3264.084961
 High        3266.637939
 Low         3221.666016
 Close       3248.476074
 Volume    320700.000000
 Name: 3, dtype: float64, Date      735663.000000
 Open        3248.035889
 High        3266.933105
 Low         3234.532959
 Close       3241.187012
 Volume    282900.000000
 Name: 4, dtype: float64, Date      735666.000000
 Open        3224.313965
 High        3307.701904
 Low         3198.370117
 Close       3302.407959
 Volume    321500.000000
 Name: 5, dtype: float64, Date      735667.000000
 Open        3289.084961
 High        3309.915039
 Low         3277.094971
 Close       3286.068115
 Volume    285800.000000
 Name: 6, dtype: float64, Date      735668.000000
 Open        3289.593994
 High        3325.053955
 Low         3278.470947
 Close       3290.899902
 Volume    283000.000000
 Name: 7, dtype: float64, Date      735669.000000
 Open        3314.812988
 High        3360.053955
 Low         3300.488037
 Close       3349.322998
 Volume    357300.000000
 Name: 8, dtype: float64, Date      735670.000000
 Open        3359.488037
 High        3391.254883
 Low         3352.149902
 Close       3372.910889
 Volume    328400.000000
 Name: 9, dtype: float64, Date      735673.000000
 Open        3391.157959
 High        3449.304932
 Low         3377.086914
 Close       3449.304932
 Volume    399100.000000
 Name: 10, dtype: float64, Date      735674.000000
 Open        3469.603027
 High        3504.123047
 Low         3459.694092
 Close       3502.846924
 Volume    520900.000000
 Name: 11, dtype: float64, Date      735675.000000
 Open        3510.500977
 High        3577.662109
 Low         3503.853027
 Close       3577.301025
 Volume    545200.000000
 Name: 12, dtype: float64, Date      735676.000000
 Open        3576.019043
 High        3600.684082
 Low         3546.843994
 Close       3582.270996
 Volume    537300.000000
 Name: 13, dtype: float64, Date      735677.000000
 Open        3587.083984
 High        3632.337891
 Low         3569.379883
 Close       3617.318115
 Volume    516700.000000
 Name: 14, dtype: float64, Date      735680.000000
 Open        3640.104004
 High        3688.249023
 Low         3635.485107
 Close       3687.728027
 Volume    536100.000000
 Name: 15, dtype: float64, Date      735681.000000
 Open        3692.569092
 High        3715.873047
 Low         3600.697998
 Close       3691.409912
 Volume    639600.000000
 Name: 16, dtype: float64, Date      735682.000000
 Open        3680.952881
 High        3693.150879
 Low         3634.559082
 Close       3660.727051
 Volume    521900.000000
 Name: 17, dtype: float64, Date      735683.000000
 Open        3641.941895
 High        3707.318115
 Low         3615.010986
 Close       3682.094971
 Volume    488600.000000
 Name: 18, dtype: float64, Date      735684.000000
 Open        3686.134033
 High        3710.477051
 Low         3656.831055
 Close       3691.095947
 Volume    408900.000000
 Name: 19, dtype: float64, Date      735687.000000
 Open        3710.612061
 High        3795.935059
 Low         3710.612061
 Close       3786.568115
 Volume    564700.000000
 Name: 20, dtype: float64, Date      735688.000000
 Open        3822.987061
 High        3835.566895
 Low         3737.042969
 Close       3747.898926
 Volume    561700.000000
 Name: 21, dtype: float64, Date      735659.000000
 Open        3332.720947
 High        3336.760010
 Low         3298.668945
 Close       3336.284912
 Volume    346400.000000
 Name: 0, dtype: float64, Date      735660.000000
 Open        3317.695068
 High        3317.695068
 Low         3260.428955
 Close       3263.052002
 Volume    382000.000000
 Name: 1, dtype: float64, Date      735661.000000
 Open        3264.181885
 High        3286.587891
 Low         3250.483887
 Close       3279.532959
 Volume    293600.000000
 Name: 2, dtype: float64, Date      735662.000000
 Open        3264.084961
 High        3266.637939
 Low         3221.666016
 Close       3248.476074
 Volume    320700.000000
 Name: 3, dtype: float64, Date      735663.000000
 Open        3248.035889
 High        3266.933105
 Low         3234.532959
 Close       3241.187012
 Volume    282900.000000
 Name: 4, dtype: float64, Date      735666.000000
 Open        3224.313965
 High        3307.701904
 Low         3198.370117
 Close       3302.407959
 Volume    321500.000000
 Name: 5, dtype: float64, Date      735667.000000
 Open        3289.084961
 High        3309.915039
 Low         3277.094971
 Close       3286.068115
 Volume    285800.000000
 Name: 6, dtype: float64, Date      735668.000000
 Open        3289.593994
 High        3325.053955
 Low         3278.470947
 Close       3290.899902
 Volume    283000.000000
 Name: 7, dtype: float64, Date      735669.000000
 Open        3314.812988
 High        3360.053955
 Low         3300.488037
 Close       3349.322998
 Volume    357300.000000
 Name: 8, dtype: float64, Date      735670.000000
 Open        3359.488037
 High        3391.254883
 Low         3352.149902
 Close       3372.910889
 Volume    328400.000000
 Name: 9, dtype: float64, Date      735673.000000
 Open        3391.157959
 High        3449.304932
 Low         3377.086914
 Close       3449.304932
 Volume    399100.000000
 Name: 10, dtype: float64, Date      735674.000000
 Open        3469.603027
 High        3504.123047
 Low         3459.694092
 Close       3502.846924
 Volume    520900.000000
 Name: 11, dtype: float64, Date      735675.000000
 Open        3510.500977
 High        3577.662109
 Low         3503.853027
 Close       3577.301025
 Volume    545200.000000
 Name: 12, dtype: float64, Date      735676.000000
 Open        3576.019043
 High        3600.684082
 Low         3546.843994
 Close       3582.270996
 Volume    537300.000000
 Name: 13, dtype: float64, Date      735677.000000
 Open        3587.083984
 High        3632.337891
 Low         3569.379883
 Close       3617.318115
 Volume    516700.000000
 Name: 14, dtype: float64, Date      735680.000000
 Open        3640.104004
 High        3688.249023
 Low         3635.485107
 Close       3687.728027
 Volume    536100.000000
 Name: 15, dtype: float64, Date      735681.000000
 Open        3692.569092
 High        3715.873047
 Low         3600.697998
 Close       3691.409912
 Volume    639600.000000
 Name: 16, dtype: float64, Date      735682.000000
 Open        3680.952881
 High        3693.150879
 Low         3634.559082
 Close       3660.727051
 Volume    521900.000000
 Name: 17, dtype: float64, Date      735683.000000
 Open        3641.941895
 High        3707.318115
 Low         3615.010986
 Close       3682.094971
 Volume    488600.000000
 Name: 18, dtype: float64, Date      735684.000000
 Open        3686.134033
 High        3710.477051
 Low         3656.831055
 Close       3691.095947
 Volume    408900.000000
 Name: 19, dtype: float64, Date      735687.000000
 Open        3710.612061
 High        3795.935059
 Low         3710.612061
 Close       3786.568115
 Volume    564700.000000
 Name: 20, dtype: float64, Date      735688.000000
 Open        3822.987061
 High        3835.566895
 Low         3737.042969
 Close       3747.898926
 Volume    561700.000000
 Name: 21, dtype: float64]
ssec15list=list()

for i in range(len(ssec2015)):
    ssec15list.append(ssec2015.iloc[i,:])
## 按照包
%pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
Collecting https://github.com/matplotlib/mpl_finance/archive/master.zip
  Downloading https://github.com/matplotlib/mpl_finance/archive/master.zip
[K     \ 471kB 8.3kB/s
Requirement already satisfied: matplotlib in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from mpl-finance==0.10.0) (3.0.3)
Requirement already satisfied: numpy>=1.10.0 in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from matplotlib->mpl-finance==0.10.0) (1.16.2)
Requirement already satisfied: cycler>=0.10 in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from matplotlib->mpl-finance==0.10.0) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from matplotlib->mpl-finance==0.10.0) (1.0.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from matplotlib->mpl-finance==0.10.0) (2.3.1)
Requirement already satisfied: python-dateutil>=2.1 in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from matplotlib->mpl-finance==0.10.0) (2.8.0)
Requirement already satisfied: six in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from cycler>=0.10->matplotlib->mpl-finance==0.10.0) (1.12.0)
Requirement already satisfied: setuptools in /Users/yaochenli/anaconda3/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib->mpl-finance==0.10.0) (40.8.0)
Building wheels for collected packages: mpl-finance
  Building wheel for mpl-finance (setup.py) ... [?25ldone
[?25h  Stored in directory: /private/var/folders/q9/2krqssqd16v76z26x_flv77m0000gn/T/pip-ephem-wheel-cache-rvgseok2/wheels/8f/bf/8a/6c858b2a3be945c7b2d658fbf6ab797b451d686cbee8aa2679
Successfully built mpl-finance
Installing collected packages: mpl-finance
Successfully installed mpl-finance-0.10.0
Note: you may need to restart the kernel to use updated packages.
from mpl_finance import candlestick_ohlc
import matplotlib.pyplot as plt
ax=plt.subplot()
mondays=WeekdayLocator(MONDAY)
weekFormatter = DateFormatter('%y %m %d'
  • 7
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值