选取某一个时间段内,前三天涨幅超过某一数值的股票。
用法:
输入三个参数,起始日期,结束日期,r, 最后一个参数表示三天涨幅r%。2.代码解读
- from __future__ import division
- from gmsdk import *
- import sys
- import arrow
- md.init("", "") # 输入用户名、密码
- def TGP(start_time, end_time, r):
- import arrow
- import pandas as pd
- import numpy as np
- #获取全市场股票代码
- e = ['SHSE', 'SZSE']
- for f in e:
- if f == 'SHSE':
- all_stocks = md.get_instruments(f, 1, 1)
- all_stocks_symbol1 = [b.symbol for b in all_stocks] # 获取沪市全部股票代码
- if f == 'SZSE':
- all_stocks = md.get_instruments(f, 1, 1)
- all_stocks_symbol2 = [b.symbol for b in all_stocks] # 获取深市全部股票代码
- all_stocks_symbol = all_stocks_symbol1 + all_stocks_symbol2
- code = []
- time1 = []
- for stock in all_stocks_symbol:
- a = md.get_dailybars(str(stock), start_time + ' 00:00:00', end_time + ' 00:00:00') #获取指定时间日线数据
- close_daily = [bar.close for bar in a] #获取日线收盘价
- time = [bar.utc_time for bar in a] #获取日线时间戳
- AU1 = pd.Series(close_daily) #将日线收盘价存为Series
- AU2 = pd.Series(close_daily).shift(3) #向前移动三格,得到对应时间点三天前的收盘价
- AU = np.array(AU1) / np.array(AU2) #相除得到三天的涨幅序列,使用numpy加速运算
- rr = ((r / 100) + 1) #参数处理,例如输入筛选涨幅为20%, rr = 20/100 + 1 = 1.2
- AU[0:3] = 0 #数据处理,将NaN赋值为0
- if len(AU[AU >= rr]) > 0: # 如果三天的涨幅序列中有大于等于要求涨幅的情况
- index2 = time[np.where(AU >= rr)[0][0]] #得到第一次出现满足条件的时间戳
- time_happen = arrow.get(index2).to('local') #时间戳格式转换
- time_happen = time_happen.format('YYYY-MM-DD') #保留 YYYY-MM-DD 部分
- code.append(stock) #记录股票代码
- time1.append(time_happen) #记录第一次满足条件时间
- time1 = pd.DataFrame(time1,columns=['time']) #转换为DataFrame
- code = pd.DataFrame(code, columns=['code']) #转换为DataFrame
- result = pd.concat([code, time1], axis=1) #合并两列
- result.to_csv('growth.csv') #存为 csv 文件
- TGP('2017-03-01', '2017-03-28', 15)
3. Python相关函数 3.1 Python标准函数:
动量选股策略:选取前期强势的股票,判断其将继续强势后买入持有。 |