如何计算出股价的支撑价?


(pipr) #1

首先获取数据,然后绘制出4条均线。问题是,想寻找出收盘价下方最近的一条均线,这就是我要找的支撑价,以便作为下单参考价格。


(iQuant) #2

仅供参考!

克隆策略
In [5]:
data = D.history_data(['000001.SZA'], '2016-01-01', '2017-10-01',
           fields=['open', 'high','low','close','adjust_factor'])
In [33]:
ma_list = [5, 20, 60,120]
for ma in ma_list:
    data['MA_' + str(ma)] = pd.rolling_mean(data['close'], ma)
T.plot(data[['MA_5','MA_20','MA_60','MA_120','close']],title='000001')
In [26]:
def backup_position(df):
    value = [df.close,df.MA_5,df.MA_20,df.MA_60,df.MA_120]   # 对4个均线值和收盘价 进行排序,返回收盘价下面(最近)的均线的值
    value.sort()
    return value[value.index(df.close)-1]
In [27]:
df = data[['date','instrument','close','MA_5','MA_20','MA_60','MA_120']].dropna()
df['backup_position'] = df.apply(backup_position, axis=1)  # 计算出支撑价位形成新的一列
In [32]:
T.plot(df[['MA_5','MA_20','MA_60','MA_120','close','backup_position']],title='000001')


(大胡子) #3

有个小问题,我修正了下。如果该收盘价比几条均线的值还小,那支撑位就是其本身。

克隆策略
In [4]:
data = D.history_data(['000001.SZA'], '2016-01-01', '2017-10-01',
           fields=['open', 'high','low','close','adjust_factor'])
In [5]:
ma_list = [5, 20, 60,120]
for ma in ma_list:
    data['MA_' + str(ma)] = pd.rolling_mean(data['close'], ma)
T.plot(data[['MA_5','MA_20','MA_60','MA_120','close']],title='000001')
In [6]:
def backup_position(df):
    value = [df.close,df.MA_5,df.MA_20,df.MA_60,df.MA_120]   # 对4个均线值和收盘价 进行排序,返回收盘价下面(最近)的均线的值
    value.sort()
    if value.index(df.close) >=1:
         return value[value.index(df.close)-1]
    elif value.index(df.close) == 0:
        return value[value.index(df.close)]
In [7]:
df = data[['date','instrument','close','MA_5','MA_20','MA_60','MA_120']].dropna()
df['backup_position'] = df.apply(backup_position, axis=1)  # 计算出支撑价位形成新的一列
In [8]:
T.plot(df[['MA_5','MA_20','MA_60','MA_120','close','backup_position']],title='000001')

(189) #4
克隆策略
In [6]:
import pandas as pd 
import numpy as np
import talib as tb
from pandas import Series,DataFrame
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close','low','high','open','amount'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_sse50'])
instruments = list(set(df[df['in_sse50']==1]['instrument']))
stock_raw_data = D.history_data(instruments, start_date, end_date,fields=['open', 'high', 'low', 'close', 'amount'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#合并指数基准及股票池
ma_list = [10,30,60,120]
for ma_len in ma_list:
    df['ma_'+str(ma_len)] = pd.rolling_mean(df['close_x'], ma_len) 
df.drop(['high_y','low_y','close_y', 'instrument_y','amount_y','open_y','amount_x'], axis=1,inplace=True)
df=df.fillna(0)
def backup_position(df):
    value = [df.close_x,df.ma_10,df.ma_30,df.ma_60,df.ma_120]   # 对4个均线值和收盘价 进行排序,返回收盘价下面(最近)的均线的值
    value.sort()
    return value[value.index(df.close_x)-1] if value.index(df.close_x) >=1 else 0      
df['下均线']=df.groupby('instrument_x').apply(backup_position, axis=1)
df.tail()      
Out[6]:
instrument_x high_x low_x date open_x close_x ma_10 ma_30 ma_60 ma_120 下均线
44503 601919.SHA 7.143742 6.966452 2017-11-08 7.039454 7.060311 12.610921 23.961857 261.305977 241.982788 0.000000
44504 601985.SHA 8.134252 7.990465 2017-11-08 8.093170 8.021276 11.108702 23.758143 261.236375 233.661926 0.000000
44505 601988.SHA 6.480832 6.398796 2017-11-08 6.431611 6.480832 10.872927 22.927324 255.082862 195.668672 0.000000
44506 601989.SHA 11.344260 11.092539 2017-11-08 11.344260 11.159664 10.690988 23.081958 251.327980 192.979014 10.690988
44507 601998.SHA 8.433931 8.312093 2017-11-08 8.352705 8.379781 9.734557 21.185619 247.107465 192.007239 0.000000

运行结果是错误的,不知道怎么传上来却是出现了结果,而且最后一列明显错误

backup_position() got an unexpected keyword argument 'axis’错误信息

加上groupby(‘instrument_x’)就变成错误,跟合并数据有关,但是百思不得其解


(189) #5

我已经解决这个问题了[quote=“xiaoshao, post:3, topic:5072”]
def backup_position(df):
value = [df.close,df.MA_5,df.MA_20,df.MA_60,df.MA_120] # 对4个均线值和收盘价 进行排序,返回收盘价下面(最近)的均线的值
value.sort()
if value.index(df.close) >=1:
return value[value.index(df.close)-1]
elif value.index(df.close) == 0:
return value[value.index(df.close)]
[/quote]

新的问题是, 我想定义一个函数,能够多参数输入均线的条数,以求得支撑价;比如我可以输入df.close,df.MA_5,df.MA_20,也可以输入df.close,df.MA_10,df.MA_20,df.MA_120等,就是涉及到自定义函数的多参数问题,用*代替的,


(woody) #6

有支撑价吗?这东西有什么理论支持吗!


(189) #7

可以说有也可以说没有,不知道你有没注意上证指数,每次跌到1%的时候就会有护盘资金勾一下指数,你也可以问问国家队有没有理论支持?呵呵