python3.7从零开始学炒股大全集_【干货分享】从零开始学量化:03动量选股策略...

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值