# coding=utf-8
|
from __future__ import print_function, absolute_import, unicode_literals
|
|
import decimal
|
from gm.api import *
|
|
|
# 将纯数字代码转化为=》掘金格式股票代码
|
def format_stock_symbol(code):
|
if code.startswith('00'):
|
return "SZSE." + code
|
elif code.startswith('60'):
|
return "SHSE." + code
|
else:
|
print(f"这个代码有异常,无法处理{code}")
|
return code
|
|
|
# 将掘金格式股票代码转化=》纯数字代码
|
def format_stock_code(symbol):
|
stock_code = symbol.split('.')[1] # 将symbol转为纯数字编号股票代码
|
return stock_code
|
|
|
# 查询前N个交易日的具体交易日期函数:在调用时date_of_the_day应该传入当天日期,num是前跨天数。循环取值后 返回的date_of_the_day值就是目标具体的交易日期,
|
def pre_num_trading_day(date_of_the_day, num):
|
for i in range(num):
|
Pre_date = get_previous_trading_date(exchange='SZSE', date=date_of_the_day) # 获取前一个交易日API
|
date_of_the_day = Pre_date
|
return date_of_the_day
|
|
|
# 计算当日涨幅公式
|
def intraday_growth(price, pre_close):
|
today_growth = (price - pre_close) / pre_close * 100 # 计算涨幅百分比
|
today_growth = round(today_growth, 2)
|
return today_growth
|
|
|
# 计算实时涨幅公式
|
# 初始化历史价格
|
price_history = {}
|
|
|
def calculate_growth(symbol, price):
|
try:
|
if symbol not in price_history:
|
return 0 # 不足两个历史价格,无法计算涨幅
|
last_price = price_history[symbol]
|
# print(f"price_history[symbol]~~~~~~~~~{price_history[symbol]}")
|
|
growth = (price - last_price) / last_price * 100 # 计算涨幅百分比
|
return growth
|
finally:
|
price_history[symbol] = price
|
# print(f"price======={price}")
|
# print(f"price_history=={price_history}")
|
# print(f"price_history[symbol]=={price_history[symbol]}")
|
|
|
# 计算当日涨停价函数 形参pre_close 实参应传入 pre_close 【return的结果需要小数点后两位,在第三位进行四舍五入】
|
def limit_up_price(pre_close):
|
limit_up_price = decimal.Decimal(str(pre_close)) * decimal.Decimal("1.1")
|
limit_up_price = limit_up_price.quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP)
|
return float(limit_up_price)
|
|
|
# 计算当日跌停价函数 形参pre_close 实参应传入 pre_close 【return的结果需要小数点后两位round取整】
|
def limit_down_price(pre_close):
|
limit_down_price = decimal.Decimal(str(pre_close)) * decimal.Decimal("0.9")
|
limit_down_price = limit_down_price.quantize(decimal.Decimal("0.00"), decimal.ROUND_HALF_UP)
|
return limit_down_price
|
|
|
# print(limit_up_price(24.95))
|
# print(calculate_growth("000333",54.3))
|
|
# 历史K累计涨停天数函数
|
def count_limit_up_day(K_line_data, symbol):
|
limit_up_day = 0 # 初始化涨停天数
|
if K_line_data is not None:
|
# 计算90天内涨停天数
|
for i in K_line_data:
|
if 'attribute' in i and (i['attribute'] == 'one_line_limit_up' or i['attribute'] == 'limit_down_then_limit_up' or i['attribute'] == 'limit_up_then_limit_down_then_limit_up' or i['attribute'] == 'limit_up'):
|
limit_up_day += 1
|
# print(f"涨停提日期:{i['bob']}")
|
# print(f"涨停天数----limit_up_day======{limit_up_day}")
|
else:
|
# 处理找不到K线属性的情况
|
print(f"未找到 {symbol} 的K线属性,或为新股、次新股第一天的时候确实没有历史K线")
|
return limit_up_day
|
|
|
# 安全交易量公式 用于计算不同时间段理论的安全交易量值
|
def secure_volume(now):
|
hour = now.hour
|
minute = now.minute
|
# 给今昨交易量比赋初值,然后每个时间段给具体的值
|
ratios = 0
|
if (hour >= 9 and 30 <= minute < 40) and hour < 10:
|
ratios = 0.2
|
elif (hour >= 9 and minute >= 40) and hour < 10:
|
ratios = 0.4
|
elif 10 <= hour < 11:
|
ratios = 0.6
|
elif 11 <= hour < 14:
|
ratios = 0.8
|
elif 14 <= hour < 15:
|
ratios = 1.2
|
return ratios
|