用python使用baostock接口编写一个函数:通过输入股票代码和起止日期获取指定股票的日K线数据,并通过比较每一天股票的当日最高价和前一日最低价,以及当日最低价和前一日最高价来判断是否出现跳空现象,如果出现跳空现象,则记录下跳空现象发生的日期。
时间: 2023-08-31 19:44:01 浏览: 165
### 回答1:
可以使用以下Python代码来实现:import baostock as bsdef get_jump_data(code, startdate, enddate):
# 登陆系统
lg = bs.login()
# 获取指定股票的日K线数据
rs = bs.query_history_k_data_plus(code,
"date,code,open,high,low,close",
start_date=startdate, end_date=enddate)
data_list = []
while (rs.error_code == '0') & rs.next():
# 获取一条记录,将记录合并在一起
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
# 对result进行处理,找出开盘价高于昨日收盘价的记录,或者低于昨日收盘价的记录,即跳空现象
result['jump_flag'] = 0
for i in range(len(result)-1):
if result.iloc[i,2]>result.iloc[i+1,5]:
result.iloc[i+1,7] = 1
elif result.iloc[i,3]<result.iloc[i+1,6]:
result.iloc[i+1,7] = 1
jump_data = result[result['jump_flag'] == 1]
# 登出系统
bs.logout()
return jump_data
### 回答2:
import baostock as bs
import pandas as pd
def get_gap_dates(stock_code, start_date, end_date):
# 登录baostock系统
lg = bs.login()
# 获取指定股票的日K线数据
rs = bs.query_history_k_data(stock_code, "date,high,low", start_date=start_date, end_date=end_date, frequency="d", adjustflag="3")
data_list = []
while (rs.error_code == '0') & rs.next():
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
# 计算每一天股票的当日最高价和前一日最低价,以及当日最低价和前一日最高价的差值
result['pre_low'] = result['low'].shift(1)
result['pre_high'] = result['high'].shift(1)
result['gap1'] = result['high'].astype(float) - result['pre_low'].astype(float)
result['gap2'] = result['low'].astype(float) - result['pre_high'].astype(float)
# 获取跳空现象发生的日期
gap_dates = result[(result['gap1'] > 0) | (result['gap2'] < 0)]['date'].tolist()
# 登出系统
bs.logout()
return gap_dates
# 调用函数,获取跳空现象发生的日期
stock_code = "sh.600000" # 以浦发银行为例
start_date = "2022-01-01"
end_date = "2022-12-31"
gap_dates = get_gap_dates(stock_code, start_date, end_date)
print("跳空现象发生的日期:", gap_dates)
### 回答3:
import baostock
import pandas as pd
def detect_gap(stock_code, start_date, end_date):
# 登录baostock
lg = baostock.login()
if lg.error_code != "0":
return "登录失败,请检查用户名和密码"
# 获取股票的日K线数据
rs = baostock.query_history_k_data(stock_code, "date,open,high,low,close", start_date=start_date, end_date=end_date, frequency="d", adjustflag="3")
if rs.error_code != "0":
return "获取数据失败,请检查股票代码和日期范围"
# 将数据转换成DataFrame格式
data = rs.get_data()
df = pd.DataFrame(data, columns=rs.fields)
# 增加两列用于记录跳空现象的日期
df["gap_up"] = ""
df["gap_down"] = ""
# 检测跳空现象
for i in range(1, len(df)):
high_prev = float(df.iloc[i-1]["high"])
low_prev = float(df.iloc[i-1]["low"])
high_today = float(df.iloc[i]["high"])
low_today = float(df.iloc[i]["low"])
if high_today > high_prev and low_today > high_prev:
df.at[i, "gap_up"] = df.iloc[i]["date"]
elif low_today < low_prev and high_today < low_prev:
df.at[i, "gap_down"] = df.iloc[i]["date"]
# 仅保留带有跳空现象的行
df = df[df["gap_up"] != "" or df["gap_down"] != ""]
# 登出baostock
baostock.logout()
return df
# 使用示例
stock_code = "sh.600000" # 股票代码,这里以中国工商银行为例
start_date = "2020-01-01" # 起始日期
end_date = "2021-01-01" # 终止日期
result = detect_gap(stock_code, start_date, end_date)
print(result)
阅读全文