一.数据导入

安装tushare模块包

pip install tushare

http://tushare.org/

tushare是一个财经数据接口包

import numpy as np
import matplotlib as plt
import pandas as pd
import tushare as ts #获取股票号为601318的股票信息
df=ts.get_k_data("") #将数据保存到本地,方便处理
df.to_csv("601318.csv",index=False) #获取数据内的有用列,并将date列作为index
df = pd.read_csv("601318.csv",index_col="date")[["open","close","high","low","volume"]]

二.分析股票策略

使用双均线金叉点和死叉点

首先是添加5日均线点,和30日均线点

#获取5日均线,10日均线
df["ma5"]=np.nan
df["ma30"]=np.nan # 方式一
# for i in range(4, len(df)):
# df.loc[df.index[i], 'ma5'] = df['close'][i-4:i+1].mean() # for i in range(29, len(df)):
# df.loc[df.index[i], 'ma30'] = df['close'][i-29:i+1].mean() # 方式二
df["ma5"]=df["close"].rolling(5).mean()
df["ma30"]=df["close"].rolling(30).mean()

三.查看数据图形

df[["close","ma5","ma30"]].plot()

四.获取金叉和死叉点

# 获取金叉点,死叉点
golden_cross = []
death_cross = [] for i in range(1, len(df)):
if df['ma5'][i] >= df['ma30'][i] and df['ma5'][i-1] < df['ma30'][i-1]:
golden_cross.append(df.index[i])
if df['ma5'][i] < df['ma30'][i] and df['ma5'][i-1] >= df['ma30'][i-1]:
death_cross.append(df.index[i])

五.计算收益

first_money = 100000
money = first_money
hold = 0 # 持有的股票数 sr1 = pd.Series(1, index=golden_cross)
sr2 = pd.Series(0, index=death_cross)
sr = sr2.append(sr1).sort_index() for i in range(0, len(sr)):
p = df['close'][sr.index[i]]
if sr.iloc[i] == 1:
#金叉
buy = (money // p*100)
hold += buy*100
money -= buy*100*p
else:
money += hold*p
hold = 0 p = df['close'][-1]
now_money = hold*p + money now_money - first_money

numpy+pandas+matplotlib+tushare股票分析的更多相关文章

  1. python 数据分析工具之 numpy pandas matplotlib

    作为一个网络技术人员,机器学习是一种很有必要学习的技术,在这个数据爆炸的时代更是如此. python做数据分析,最常用以下几个库 numpy pandas matplotlib 一.Numpy库 为了 ...

  2. 第一章:AI人工智能 の 数据预处理编程实战 Numpy, Pandas, Matplotlib, Scikit-Learn

    本课主题 数据中 Independent 变量和 Dependent 变量 Python 数据预处理的三大神器:Numpy.Pandas.Matplotlib Scikit-Learn 的机器学习实战 ...

  3. 常用统计分析python包开源学习代码 numpy pandas matplotlib

    常用统计分析python包开源学习代码 numpy pandas matplotlib 待办 https://github.com/zmzhouXJTU/Python-Data-Analysis

  4. Python模块简介及安装 [numpy,pandas,matplotlib,scipy,statsmodels,Gensim,sklearn,keras]

    https://pan.baidu.com/s/1bpVv3Ef  67bd          模块安装文件下载地址 pip install "numpy-1.12.0b+mkl-cp35- ...

  5. numpy, pandas, matplotlib等常用库的学习手册

    pandas介绍: 待续 参考资料: 中文:https://www.cnblogs.com/skying555/p/5914391.html 英文:http://www.datadependence. ...

  6. Pandas应用案例-股票分析:使用tushare包获取股票的历史行情数据进行数据分析

    目标: 使用tushare包获取股票的历史行情数据 输出该股票所有收盘比开盘上涨3%以上的日期 输出该股票所有开盘比前日收盘跌幅超过2%以上的日期 假如为我们从2010年1月1日开始,每月第一个交易日 ...

  7. numpy pandas matplotlib

    import numpy as np import pandas as pd import matplotlib.pyplot as plt ---------------numpy--------- ...

  8. Numpy, Pandas, Matplotlib, Scipy 初步

    Numpy: 计算基础,  以类似于matlab的矩阵计算为基础.  底层以C实现, 速度快. Pandas: 以numpy为基础, 扩充了很多统计工具. 重点是数据统计分析. Matplotlib: ...

  9. python 安装anaconda, numpy, pandas, matplotlib 等

    如果没安装anaconda,则这样安装这些库: pip install numpy pip install pandas pip install matplotlib sudo apt-get ins ...

随机推荐

  1. ckeditor(在线文本编辑器)使用教程

    ckeditor是一款由javascript编写的富文本网页编辑器,它可以填写文字.插入图片.视频.Excel等富媒体信息,也可以在源码方式下填写内容,在各个网站中应用非常广泛. 下面就来说说cked ...

  2. Thread 1 cannot allocate new log, sequence 187398

    报错信息: Thread 1 cannot allocate new log, sequence 187398Checkpoint not complete 处理方法: 查看REDO日志组 selec ...

  3. python 获取当前时间及前一天时间

    import datetime from pandas.tseries.offsets import Day now_time =datetime.datetime.now()#获取当前时间 yes_ ...

  4. [转]FireFox与IE 下js兼容触发click事件的代码

    本文转自:http://www.jb51.net/article/16549.htm FireFox与IE 下js兼容触发click事件 ,对于需要兼容这两者的朋友,就需要参考下下面的代码了<a ...

  5. 在rails 中使用mysql 出现Mysql::Error: Incorrect string value: 的问题

    这是因为你在做数据库的操作中有非英文的问题,之后gem mysql2 处理中文必须要数据库也指定是utf-8 才比较好处理 解决的方法很简单,将数据库每张表都转化成utf-8即可,如果数据库没有什么重 ...

  6. 带你认识spark安装包的目录结构

    福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟   ...

  7. win8中如何设定editplus为txt默认打开程序

    设定EditPlus为TXT默认打开方式吧. 首选,打开我们的EditPlus 接着,点击[工具]菜单,点击[参数设置]这个菜单项 来到设定界面 找到[设置&语法]这个选项,然后可以看到里面有 ...

  8. react-router + redux + react-redux 的例子与分析

    一个 react-router + redux  + react-redux 的例子与分析 index.js  import React from 'react' import ReactDom fr ...

  9. web worker技术-js新线程

    web worker的小例子,用来入门很合适,建议启动服务来开发.可以使用node的anywhere. <!DOCTYPE html> <html lang="en&quo ...

  10. 零基础逆向工程33_Win32_07_创建线程

    1 什么是线程(Threads)? 什么是多线程? 怎么在windows中观察多线程? 线程可以简单理解为主程序为解决一个问题而选择的其中一条路线. 同理,多线程就是同时选择不同的路线来解决此问题. ...