策略定义:
获取所有上市股票,剔除停牌股、百日内新股、退市股;
获取时间范围内股票信息(百日内最高价,最低价,均价,涨跌幅,最新价,市值)
计算5日,10日,20日均线,及MACD
提取价值股(几个低于最高价*0.75,均线金叉,MACD金叉,前一天成交量大于5.5亿)
# 策略中必须有init方法
# coding=utf-8
from __future__ import print_function, absolute_import, unicode_literals
from gm.api import *
import numpy as np
import datetime
import talib
import time
start = time.perf_counter()
set_token('2c705a9dd13617610384e13505253dbe93d698eb')
date = datetime.datetime.now()
date1 = date.strftime("%Y-%m-%d %H:%M:%S")
date2 = (date + datetime.timedelta(days=-100)).strftime("%Y-%m-%d %H:%M:%S")
# date1 = context.now.strftime("%Y-%m-%d %H:%M:%S")
# date2 = (context.now - timedelta(days=100)).strftime("%Y-%m-%d %H:%M:%S")
# 通过get_instruments获取所有的上市股票代码
all_stock = get_instruments(exchanges='SHSE, SZSE', sec_types=[1], fields='symbol, listed_date, delisted_date', df=True)
# 剔除停牌和st股和上市不足100日的新股和退市股和B股
code = all_stock[(all_stock['listed_date'] < date2) & (all_stock['delisted_date'] > date1) & (all_stock['symbol'].str[5] != '9') & (all_stock['symbol'].str[5] != '2')]
fundamenta_1 = get_fundamentals('trading_derivative_indicator', code['symbol'].to_list(),
start_date=date2, end_date=date1,
fields='TOTMKTCAP', df=True,
filter='TOTMKTCAP>5000000000')
taget_symbol_1 = []
taget_symbol_2 = []
taget_symbol_3 = []
for sym in fundamenta_1['symbol']:
# 获取股票基础信息(开盘、最高、最低、现价、买卖5档、成交总手、成交总额)
# data1 = current(symbols='SHSE.603589', fields='open,high,low,close,price,cum_volume,cum_amount,quotes')
# 获取历史数据用于计算(价格、成交量)
# history_data = history(symbol='sym', frequency='1d', start_time=date2, end_time=date1, count=100, df=True)
history_data = history_n(symbol=sym, frequency='1d', end_time=date1, count=100,
fill_missing='last', df=True)
# symbol_data = history_n(symbol='SHSE.603589', frequency='1d', end_time=date1, count=3, fill_missing='last', df=True)
# 获取其他数据(股票衍生表、资产负债表、现价流量表、利润表)
# 股票衍生表:trading_derivative_indicator
# (NEGOTIABLEMV/流通市值,PB/市净率(PB),PELFY/市盈率LFY,PELFYNPAAEI/扣非市盈率,TOTMKTCAP/总市值,NEGOTIABLEMV/流通市值,TURNRATE/当日换手率)
# get_fund = get_fundamentals(table='trading_derivative_indicator', symbols='SHSE.603589', start_date=date2,
# end_date=date1,
# fields='TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI')
close_price = history_data['close'].values
max_x = history_data['high'].values # 最高价
min_n = history_data['low'].values # 最低价
max_high = np.max(max_x) # 最高收盘价
min_colse = np.min(min_n) # 最低收盘价
amount = history_data['amount'].values # 成交量
# 获取均线的数据,通过timeperiod参数来分别获取 5,10,20 日均线的数据。
ma5 = talib.SMA(close_price, timeperiod=5)
ma10 = talib.SMA(close_price, timeperiod=10)
ma20 = talib.SMA(close_price, timeperiod=20)
# print(ma5[-1], ma10[-1], ma20[-1])
# 获取MACD的数据
macd, signal, hist = talib.MACD(close_price, fastperiod=12, slowperiod=26, signalperiod=9)
# print(macd[-1]) # 快线
# print(signal[-1]) # 慢线
# print(hist[-1]) # 快线-慢线
if amount[-1] > 3000000000:
taget_symbol_3.append(sym)
if amount[-1] > 3000000000 and close_price[-1] <= ma10[-1]:
taget_symbol_2.append(sym)
if close_price[-1] < max_high*0.75 and close_price[-1] < min_colse*1.15 and hist[-1] >= 0 and ma5[-1] >= ma10[-1] \
and amount[-1] > 550000000:
taget_symbol_1.append(sym)
KCB = list(filter(lambda x: x[6] == '8', taget_symbol_1)) # 科创板
target_symbol = list(set(taget_symbol_1) - set(KCB)) # 主板
end = time.perf_counter()
print('开始时间:{},结束时间:{},运行时间 : {} 秒'.format(start, end, end - start))
print(target_symbol)
print(taget_symbol_2)
print(taget_symbol_3)