选取某一个时间段内,前三天涨幅超过某一数值的股票。
用法:
输入三个参数,起始日期,结束日期,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标准函数:
功能 | 函数原型 | 参数 | 返回值 | ||
参数名 | 含义 | ||||
sys | 提供了一系列有关Python运行环境的变量和函数。 | ||||
sys.argv[0] | 当前程序名 | ||||
sys.argv | 获取当前正在执行的命令行参数的参数列表(list)。 | sys.argv | sys.argv[1] | 第一个参数 | |
sys.argv[2] | 第二个参数 | ||||
arrow | 标准的时间日期库。 | ||||
series | Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。Series的字符串表现形式为:索引在左边,值在右边。dataframe中的数据是以一个或者多个二位块存放的(而不是列表、字典或者别的一维数据结构)。 | ||||
time | 返回当前时间的时间戳 | time.time() | 返回当前时间的时间戳 | ||
DataFrame | DataFrame是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔值的)。 | ||||
len | 返回对象(字符、列表、元组等)长度或项目个数。 | len(s) | s | 对象 | 返回对象长度。 |
append | 用于在列表末尾添加新的对象。 | list.append(obj) | obj | 添加到列表末尾的对象。 | 该方法无返回值,但是会修改原来的列表。 |
功能 | 函数原型 | 参数 | 返回值 | |||
参数名 | 类型 | 说明 | ||||
get_instruments | 提取交易代码。策略类和行情服务类都提供该接口。 | get_instruments(exchange, sec_type, is_active) | exchange | string | 交易所代码 | Instrument对象列表 |
sec_type | int | 代码类型:1.股票,2.基金,3.指数,4.期货,5.ETF | ||||
is_active | int | 当天是否交易:1.是,0.否 | ||||
get_dailybars | 提取指定时间段的历史日周期Bar数据,支持单个代码提取或多个代码组合提取。DailyBar比Bar多了部分静态数据,如结算价,涨跌停等。策略类和行情服务类都提供该接口。 | get_dailybars(symbol_list, begin_time, end_time) | symbol_list | string | 证券代码, 带交易所代码以确保唯一,如SHSE.600000,同时支持多只代码 | DailyBar列表 |
begin_time | string | 开始日期, 如2015-10-19 | ||||
end_time | string | 结束日期, 如2015-10-30 | ||||
bar | 各种周期的Bar数据 | class Bar(object): def __init__(self): self.exchange = '' ## 交易所代码 self.sec_id = '' ## 证券ID self.bar_type = 0 ## bar类型,以秒为单位,比如1分钟bar, bar_type=60 self.strtime = '' ## Bar开始时间 self.utc_time = 0.0 ## Bar开始时间 self.strendtime = '' ## Bar结束时间 self.utc_endtime = 0.0 ## Bar结束时间 self.open = 0.0 ## 开盘价 self.high = 0.0 ## 最高价 self.low = 0.0 ## 最低价 self.close = 0.0 ## 收盘价 self.volume = 0.0 ## 成交量 self.amount = 0.0 ## 成交额 self.pre_close ## 前收盘价 self.position; ## 持仓量 self.adj_factor ## 复权因子 self.flag ## 除权出息标记 |
4. 金融术语:
动量选股策略:选取前期强势的股票,判断其将继续强势后买入持有。