zoukankan      html  css  js  c++  java
  • 金融分析之基本ma5 及金叉、死叉

     1 import pandas as pd
     2 import numpy as np
     3 import matplotlib.pylab as plt
     4 
     5 res=pd.read_csv("601318.csv",parse_dates=True,index_col="date")
     6 
     7 
     8 #画图
     9 # plt.plot(res["open"],"r-",res["close"],"b-")
    10 # plt.xlabel(u"time")
    11 # plt.ylabel(u"price")
    12 # plt.show()
    13 
    14 #计算ma5和ma10
    15 
    16 #----------方案1
    17 # res["ma5"]=np.NaN
    18 # res["ma10"]=np.NaN
    19 #
    20 # for i in range(4,len(res)):       #从4开始
    21 #     res.loc[res.index[i],"ma5"]=res["close"][i-4:i+1].mean()  #按下表取到时间对象,然后loc拿到该位置的ma5值,然后取到平均值
    22 #
    23 # for i in range(9,len(res)):
    24 #     res.loc[res.index[i],"ma5"]=res["close"][i-9:i+1].mean()
    25 #
    26 # print(res)
    27 
    28 #----------方案2
    29 # res["ma5"]=(res["close"].cumsum()-res["close"].cumsum().shift(1).fillna(0).shift(4))/5      #如果一次性移动5个的话,第五个
    30 #位置会成为NaN,这样,第五个位置上相减后就会没有值,所以先移动一个,让其填充上0,这样保证前四个是NaN,但是第五个有值
    31 # res["ma10"]=(res["close"].cumsum()-res["close"].cumsum().shift(1).fillna(0).shift(9))/10
    32 
    33 
    34 # 1   2   3   4   5   6   7
    35 # 1   3   6   10  15  21  28
    36 #                 1   3   6   10
    37 
    38 #----------方案3
    39 res["ma5"]=res["close"].rolling(5).mean()     #默认rolling的光标行是最后一行
    40 res["ma10"]=res["close"].rolling(10).mean()
    41 
    42 # plt.plot(res["ma5"],"r-",res["ma10"],"b-")
    43 # plt.xlabel(u"time")
    44 # plt.ylabel(u"price")
    45 # plt.show()
    46 
    47 
    48 #找出金叉和死叉
    49 
    50 res=res.dropna()
    51 
    52 #----------------方案1
    53 # golden=[]
    54 # black=[]
    55 # sr=res["ma5"]>=res["ma10"]
    56 #
    57 # for i in range(1,len(sr)):
    58 #     if sr.iloc[i]==True and sr.iloc[i-1]==False:
    59 #         golden.append(sr.index[i])
    60 #     if sr.iloc[i]==False and sr.iloc[i-1]==True:
    61 #         black.append(sr.index[i])
    62 
    63 #----------------方案2
    64 golden=res[list((res["ma5"]>=res["ma10"]) & (res["ma5"]<res["ma10"]).shift(1))].index
    65 
    66 black=res[(res["ma5"]<=res["ma10"]) & (res["ma5"]>res["ma10"]).shift(1)].index
    67 
    68 # print(type((res["ma5"]>=res["ma10"]) & (res["ma5"]<res["ma10"]).shift(1)))
    69 # print(list((res["ma5"]>=res["ma10"]) & (res["ma5"]<res["ma10"]).shift(1)))
    70 
    71 #1、先取到5日大于10日的True,在取到5日小于10日的True,让后者往后挪一天则按照布尔索引取到所有值,然后再转化为索引
    72 
    73 #                    1     2   3   4   5   6   7   8   9   10  11  12  13  14  15
    74 #例如:5日>10日:#     T     T   T   F   T   T   T   T   F   F   F   F   T   T   T
    75 #例如:5日<10日:#     F     F   F   T   F   F   F   F   T   T   T   T   F   F   F
    76 #例如:5日<10日,后挪1:#     F   F   F   T   F   F   F   F   T   T   T   T   F   F   F
    77 
    78 #死叉是4日和9日,则都是T的时候是金叉,如5日
  • 相关阅读:
    eclipse——插件报错:Could not find node.js
    常用css设置
    前端跨域常见的处理方法
    刷新建设批次为空的sql
    PostgreSQL 添加字段语句
    查询在A表有记录数据,B表没有记录数据的SQL
    postgreSql 备份复制表结构和数据 SQL语句
    删除site_planning_id 和version 重复的,如果有多个版本,留下版本号最高的
    leetcode 精选top面试题
    leetcode 精选top面试题
  • 原文地址:https://www.cnblogs.com/chedanlangren/p/7413001.html
Copyright © 2011-2022 走看看