Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
SamaelChen committed Sep 15, 2020
1 parent 4bf8e69 commit 9bafaaf
Showing 1 changed file with 107 additions and 7 deletions.
114 changes: 107 additions & 7 deletions quant/nlinebreak.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import pandas as pd
import baostock as bs
from tqdm import tqdm
import datetime
import matplotlib.pyplot as plt
# %%
dt = datetime.date.today().strftime('%Y-%m-%d')
# %%
lg = bs.login()

print('login respond error_code: ' + lg.error_code)
print('login respond error_msg: ' + lg.error_msg)

rs = bs.query_all_stock(day='2020-09-11')
rs = bs.query_all_stock(day=dt)
print('query_all_stock respond error_code: ' + rs.error_code)
print('query_all_stock respond error_msg: ' + rs.error_msg)
# %%
Expand Down Expand Up @@ -61,7 +64,7 @@ def download_data(start_date, end_date, stocks_list):


# %%
data = download_data('2020-01-01', '2020-09-14', all_stocks['code'])
data = download_data('2020-01-01', dt, all_stocks['code'])
# %%
data.head()
# %%
Expand Down Expand Up @@ -132,12 +135,11 @@ def find_PositiveStar_NegativeStar(df, stockcode='sh.600000'):
datelist = hisdata['date']
for i in range(len(datelist)):
if i > 2 and i < len(datelist) - 2:
# 启明星 前一日是中阳线,或者连续两天阳线
# 启明星 前一日是长阴实体,或者连续n天阴线
if zdflist[i-1] < -5 or (zdflist[i-1] < -4 and zdflist[i-1] + zdflist[i-2] < -6):
# 当日高开或者平开,但是有上下影线,且上影线长度超过了K线实体的长度
# or zdflist[i] < 0
if (min(openlist[i], closelist[i]) - lowlist[i] > (abs(openlist[i] - closelist[i]))):
# 次日下跌,且跌到之前上涨阳线的一半以下
# 当日是个纺锤线
if ((min(openlist[i], closelist[i]) - lowlist[i] + highlist[i] - max(openlist[i], closelist[i])) > (abs(openlist[i] - closelist[i]))):
# 次日上涨,且涨幅大于阴线一半
if zdflist[i + 1] > 0 and closelist[i + 1] > 0.5*(openlist[i - 1] + closelist[i - 1]):
print("at %s,%s出现了启明星,前日涨幅%f,当日涨幅%f,次日涨幅 %f,形态出现后涨幅%f" % (
datelist[i], stockcode, zdflist[i-1], zdflist[i], zdflist[i+1], zdflist[i+2]))
Expand All @@ -162,3 +164,101 @@ def find_PositiveStar_NegativeStar(df, stockcode='sh.600000'):
# %%
sum(res['pos']) / res.shape[0]
# %%
res['zdf'].sum()
# %%
res.loc[res['date'] == '2020-09-10']
# %%


def find_PositiveStar(df, stockcode='sh.600000'):
"""找出底部十字星"""
# 获取历史K线数据
hisdata = df.loc[df['code'] == stockcode]
hisdata = hisdata.reset_index()
highlist = hisdata['high'].astype('float')
lowlist = hisdata['low'].astype('float')
openlist = hisdata['open'].astype('float')
closelist = hisdata['close'].astype('float')
zdflist = hisdata['pctChg'].astype('float')
datelist = hisdata['date']
res = pd.DataFrame()
for i in range(len(datelist)):
if i > 2 and i < len(datelist) - 1:
# 启明星 前一日是长阴实体,或者连续n天阴线
if zdflist[i-1] < -5 or (zdflist[i-1] < -4 and zdflist[i-1] + zdflist[i-2] < -6):
# 当日是个纺锤线
if ((min(openlist[i], closelist[i]) - lowlist[i] + highlist[i] - max(openlist[i], closelist[i])) > (abs(openlist[i] - closelist[i]))):
print("at %s,%s 出现了十字星,前日涨幅 %f,当日涨幅 %f,次日涨幅 %f" % (
datelist[i], stockcode, zdflist[i-1], zdflist[i], zdflist[i+1]))
tmp = pd.DataFrame({'code': [stockcode], 'date': [
datelist[i]], 'nextday_chg': [zdflist[i+1]]})
res = res.append(tmp, ignore_index=True)
# return stockcode, datelist[i], zdflist[i+1]
# elif ((min(openlist[i], closelist[i]) - lowlist[i]) > 2 * (abs(openlist[i] - closelist[i]))):
# print("at %s,%s 出现了锤子线,前日涨幅 %f,当日涨幅 %f,次日涨幅 %f" % (
# datelist[i], stockcode, zdflist[i-1], zdflist[i], zdflist[i+1]))
# return stockcode, datelist[i], zdflist[i+1]
return res


# %%
find_PositiveStar(data, 'sz.000913')
# %%
res = pd.DataFrame()
for code in tqdm(all_stocks['code']):
tmp = find_PositiveStar(data, code)
if len(tmp) > 0:
res = res.append(tmp, ignore_index=True)
# %%
res.head()
# %%
res['pos'] = res['nextday_chg'].apply(lambda x: 1 if x > 0 else 0)
print(res['pos'].sum() / res.shape[0])
# %%


def find_hammer(df, stockcode='sh.600000'):
"""找出底部锤子线"""
# 获取历史K线数据
hisdata = df.loc[df['code'] == stockcode]
hisdata = hisdata.reset_index()
highlist = hisdata['high'].astype('float')
lowlist = hisdata['low'].astype('float')
openlist = hisdata['open'].astype('float')
closelist = hisdata['close'].astype('float')
zdflist = hisdata['pctChg'].astype('float')
datelist = hisdata['date']
res = pd.DataFrame()
for i in range(len(datelist)):
if i > 2 and i < len(datelist) - 1:
# 启明星 前一日是长阴实体,或者连续n天阴线
if zdflist[i-1] < -5 or (zdflist[i-1] < -4 and zdflist[i-1] + zdflist[i-2] < -6):
# 当日是个纺锤线
if ((min(openlist[i], closelist[i]) - lowlist[i]) > 2 * (abs(openlist[i] - closelist[i]))) and ((max(openlist[i], closelist[i]) / highlist[i]) > 0.999):
print("at %s,%s 出现了锤子线,前日涨幅 %f,当日涨幅 %f,次日涨幅 %f" % (
datelist[i], stockcode, zdflist[i-1], zdflist[i], zdflist[i+1]))
tmp = pd.DataFrame({'code': [stockcode], 'date': [
datelist[i]], 'nextday_chg': [zdflist[i+1]]})
res = res.append(tmp, ignore_index=True)
# return stockcode, datelist[i], zdflist[i+1]
# elif ((min(openlist[i], closelist[i]) - lowlist[i]) > 2 * (abs(openlist[i] - closelist[i]))):
# print("at %s,%s 出现了锤子线,前日涨幅 %f,当日涨幅 %f,次日涨幅 %f" % (
# datelist[i], stockcode, zdflist[i-1], zdflist[i], zdflist[i+1]))
# return stockcode, datelist[i], zdflist[i+1]
return res


# %%
find_hammer(data, 'sz.000913')
# %%
res = pd.DataFrame()
for code in tqdm(all_stocks['code']):
tmp = find_hammer(data, code)
if len(tmp) > 0:
res = res.append(tmp, ignore_index=True)
# %%
res.head()
# %%
res['pos'] = res['nextday_chg'].apply(lambda x: 1 if x > 0 else 0)
print(res['pos'].sum() / res.shape[0])
# %%

0 comments on commit 9bafaaf

Please sign in to comment.