请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3365867 新帖:0

上证综指跳空高开回补统计

外汇交易达人发表于:5 月 10 日 07:53回复(1)

导入需要的程序包

import pandas as pd

获取上证综指的2005年01月至今的上证综指日行情数据

df = get_price('000001.XSHG', start_date='2005-01-01', end_date='2018-11-15', frequency='daily', fields=['open','high','low','close','pre_close'])

高开幅度

df['hike']=df['close']-df['pre_close']
df['markup']=df['hike']*100/df['pre_close']
df['pre_high']=0

df['date_loop']='2000-1-1'
df['low_loop']=''
high_pre=0

前一天最高价

for index, row  in df.iterrows():
  if high_pre>0:
    df.loc[index,'pre_high']=high_pre
  high_pre=row['high']

判断是否属于跳空高开,且当日收盘涨幅需要大约4%

df['selecflag']=((df.markup>4)&(df.open>df.pre_high))
df_select=df[df['selecflag']]

针对符合高开的交易日逐个处理
最低价低于跳空高开前一天的最高价才算回补

for index, row  in df_select.iterrows():
  highindex=row['pre_high']
  df_tmp=df[df.index>=index]
  df_tmp2=df_tmp[df_tmp['low']'2000-1-1':
    df_select.loc[index,'date_loop']=index_loop[0]
    df_select.loc[index,'low_loop']=df_tmp2.ix[0,'high']

看跳空高开后哪一天回补

df_select
#导入需要的程序包
import pandas as pd
# 获取上证综指的2005年01月的至今日行情数据
df = get_price('000001.XSHG', start_date='2005-01-01', end_date='2018-11-15', frequency='daily', fields=['open','high','low','close','pre_close']) 
#高开幅度
df['hike']=df['close']-df['pre_close']
df['markup']=df['hike']*100/df['pre_close']
df['pre_high']=0

#回补日期
df['date_loop']='2000-1-1'
df['low_loop']=''
high_pre=0

# 前一天最高价
for index, row  in df.iterrows():
  if high_pre>0:
    df.loc[index,'pre_high']=high_pre
  high_pre=row['high']
    #print(df_pre)

#判断是否属于跳空高开
df['selecflag']=((df.markup>4)&(df.open>df.pre_high))
df_select=df[df['selecflag']]
# 针对符合高开的交易日逐个处理
# 最低价低于跳空高开前一天的最高价才算回补
for index, row  in df_select.iterrows():
  highindex=row['pre_high']
  df_tmp=df[df.index>=index]
  df_tmp2=df_tmp[df_tmp['low']<highindex]
  df_tmp2=df_tmp2.head(1)
  index_loop=df_tmp2.index
  if index_loop>'2000-1-1':
    #print(index_loop)
    df_select.loc[index,'date_loop']=index_loop[0]
    df_select.loc[index,'low_loop']=df_tmp2.ix[0,'high']
  #print(date_loop[0])

#df_select['date_span']=df_select.date_loop-df_select.index
df_select
/opt/conda/lib/python3.5/site-packages/pandas/core/indexing.py:517: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s
/opt/conda/lib/python3.5/site-packages/ipykernel_launcher.py:13: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  del sys.path[0]
/opt/conda/lib/python3.5/site-packages/ipykernel_launcher.py:10: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.
  # Remove the CWD from sys.path while we load stuff.
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
open high low close pre_close hike markup pre_high date_loop low_loop selecflag
2008-02-04 4415.023 4672.214 4415.023 4672.170 4320.767 351.403 8.132885 4411.704 2008-02-22 00:00:00 4500.39 True
2008-04-24 3539.868 3593.197 3461.643 3583.028 3278.330 304.698 9.294305 3296.717 2008-06-10 00:00:00 3215.5 True
2008-04-30 3545.573 3705.093 3543.019 3693.106 3523.405 169.701 4.816392 3544.229 2008-04-30 00:00:00 3705.09 True
2008-09-19 2067.643 2075.091 2043.316 2075.091 1895.837 179.254 9.455138 1942.846 2008-10-13 00:00:00 2074.47 True
2008-09-22 2241.723 2269.733 2164.798 2236.410 2075.091 161.319 7.774069 2075.091 2008-10-07 00:00:00 2183 True
2008-11-10 1782.305 1876.162 1782.305 1874.801 1747.713 127.088 7.271674 1762.233 2000-1-1 True
2009-10-09 2840.131 2912.550 2834.618 2911.715 2779.426 132.289 4.759580 2803.858 2010-05-05 00:00:00 2857.29 True
2015-08-10 3786.033 3943.624 3775.854 3928.415 3744.205 184.210 4.919870 3756.740 2015-08-18 00:00:00 4006.34 True
2015-08-28 3125.264 3235.839 3102.945 3232.350 3083.591 148.759 4.824213 3085.422 2015-09-01 00:00:00 3180.33 True
2018-10-22 2565.640 2675.410 2565.640 2654.880 2550.470 104.410 4.093755 2553.390 2018-10-25 00:00:00 2606.1 True
 

全部回复

0/140

量化课程

    移动端课程