从状态转换概率与股指涨落的散点图看股指涨落规律

原始数据

1.股票指数


在代码中会对收盘价做对数处理:yi=log2(X_{i+1}/X_i)

2.股票状态切换概率(股票状态参考:从时间序列到复杂网络,概率为一阶马尔科夫计算的状态转移矩阵概率)


计算代码:

# coding: utf-8
import matplotlib.pyplot as plt
import pandas as pd
import math
from datetime import datetime
import matplotlib.dates as mdates
from dateutil.parser import parse
'''
Function:probFluct VS.LogIndex Scatter
Parametter:2input file name 3output picture name 4output picture title
'''

#char time transfer to datetime type
def strTime2DateTime(time1):
    time2=[]
    for i in time1:
        time2.append(parse(str(i)))
    return time2

def loadProbFluctSquenceData():
    #load stock data
    timeAndProbFluctSquence=pd.read_csv('timeAndProbFluctData/timeAndProbFluctSquence_slideVisibilityGraph_NASDAQk5.csv',parse_dates=True)
    probFluct=timeAndProbFluctSquence['probFluct']#y轴值
    closePriceTime=timeAndProbFluctSquence['time']
    closePriceTime=strTime2DateTime(closePriceTime)#x轴值
    return closePriceTime,list(probFluct)

def loadLogStockData():
    #load score data
    stockData=pd.read_csv('data/data__NASDAQ.csv',parse_dates=True)#,index_col=0
    index=stockData['close_price']#y轴值
    logIndexTime=stockData['time']
    logIndex=[]    #y_i=log2(X_{i+1}/X_i)
    for i in range(len(index)-1):
          medium=math.log(float(index[i+1]/index[i]),2)
          logIndex.append(medium)
    logIndexTime=logIndexTime[0:-1]#delete the last time
    logIndexTime=strTime2DateTime(logIndexTime)#x轴值
    return logIndexTime,logIndex

def BindingData(closePriceTime,probFluct,logIndexTime,logIndex):
    closePriceTimeprobFluctDict={}
    count=0
    for i in closePriceTime:
        closePriceTimeprobFluctDict[i]=probFluct[count]
        count+=1

    count=0
    logIndexTimelogIndexDict={}
    for i in logIndexTime:
        logIndexTimelogIndexDict[i]=logIndex[count]
        count+=1
    #get the time intersection
    X_probFluct=[]
    Y_logIndex=[]
    timeIntersection=[val for val in closePriceTime if val in logIndexTime]

    for i in timeIntersection:
        X_probFluct.append(closePriceTimeprobFluctDict[i])
        Y_logIndex.append(logIndexTimelogIndexDict[i])
    return X_probFluct,Y_logIndex

def DrawScatter(X_probFluct,Y_logIndex):
     fig = plt.figure(figsize=(18,9))
     ax=fig.add_subplot(111)
     ax.set_title("Scatter of ProbFluct VS. LogIndex__NASDAQk5")
     ax = plt.gca()#grid setting
     ax.yaxis.grid(True)#delete y axis grid
     ax.xaxis.grid(True)#save x axis grid
     ax.set_xlabel('ProbFluct')
     ax.set_ylabel('LogIndex')
     ax.scatter(X_probFluct,Y_logIndex,alpha=0.5)
     #plt.savefig("outputResult/Scatter_slideVisibilityGraph_probFluctVS.LogIndex__NASDAQk5.png")  #save the graph
     plt.show()

if __name__=="__main__":
    closePriceTime,probFluct=loadProbFluctSquenceData()
    logIndexTime,logIndex=loadLogStockData()
    X_probFluct,Y_logIndex=BindingData(closePriceTime,probFluct,logIndexTime,logIndex)
    DrawScatter(X_probFluct,Y_logIndex)



结果图:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值