请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3364762 新帖:0

#输入参数,获取N日创新低、新高百分比

我就是英雄发表于:5 月 9 日 20:57回复(1)

输入参数,获取N日创新低、新高百分比

import pandas as pd
import numpy as np
import datetime
import matplotlib.mlab as mlab  
import matplotlib.pyplot as plt
#输入参数,获取N日创新低、新高百分比
day_num=20
stock_list=get_all_securities(['stock']) #获取所有股票代码
stock_list = list(get_all_securities(['stock']).index)
#stock_list[1]
t2='09:30'
def get_count(t2):
    t1=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    tt=t1[:11]+t2
    d1 = datetime.datetime.strptime(t1[:16], '%Y-%m-%d %H:%M')
    d2 = datetime.datetime.strptime(tt, '%Y-%m-%d %H:%M')
    delta = (d1 - d2).seconds/60
    count = round(delta)
    if count>120 and count<210:
        count=120
    elif count>210 and count<330:
        count=count-90
    elif count>330:
         count=329
    return count
count=get_count(t2)

#获取当日最高价
df_today_h=history(count=count, unit='1m', field='high', security_list=stock_list, df=True, skip_paused=True, fq='pre').max()
#获取当日最低价
df_today_l=history(count=count, unit='1m', field='high', security_list=stock_list, df=True, skip_paused=True, fq='pre').min()

#当日开盘股票总数
to_num=len(df_today_h.index)

#当日开盘股票列表
stock_list_today=df_today_h.index.values

#获取前N日的最高价(不包括当前交易日)
df20_h=history(count=day_num, unit='1d', field='high', security_list=stock_list_today, df=True, skip_paused=False, fq='pre').max()
#获取前N日的最高价(不包括当前交易日)
df20_l=history(count=day_num, unit='1d', field='high', security_list=stock_list_today, df=True, skip_paused=False, fq='pre').min()
#
#创新高的股票 
br_h_list=df_today_h[find(df_today_h>df20_h)]
br_h_num=len(br_h_list)
#创新低的股票
br_l_list=df_today_l[find(df_today_l<df20_l)]
br_l_num=len(br_l_list)
#br_h_num
#百分比  
ratio_h=br_h_num/to_num*100
ratio_l=br_l_num/to_num*100
#ratio
labels_h=['总开盘股票数','创新高股票数']
labels_l=['总开盘股票数','创新低股票数']
X_h=[100-ratio_h,ratio_h]
X_l=[100-ratio_l,ratio_l]

fig = plt.figure()
plt.pie(X_h,labels=labels_h,autopct='%1.2f%%') #画饼图(数据,数据对应的标签,百分数保留两位小数点)
plt.title("创"+str(day_num)+"日新高百分比图")
plt.show()  
fig = plt.figure()
plt.pie(X_l,labels=labels_l,autopct='%1.2f%%') #画饼图(数据,数据对应的标签,百分数保留两位小数点)
plt.title("创"+str(day_num)+"日新低百分比图")
plt.show()  
 

全部回复

0/140

量化课程

    移动端课程