拆分 day 和 hour 逻辑
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m42s

This commit is contained in:
cheney 2025-07-22 15:32:00 +08:00
parent bcf209b37f
commit fcf9b977fb
7 changed files with 121 additions and 5 deletions

View File

@ -21,4 +21,4 @@ jobs:
- name: 直接启动服务 - name: 直接启动服务
run: | run: |
docker rm -vf ak13 || true docker rm -vf ak13 || true
docker run -d --name ak13 --workdir /app -v ak13-logs:/app/logs ak13:latest docker run -d --name ak13 --workdir /app -e TZ=Asia/Shanghai -v ak13-logs:/app/logs ak13:latest

View File

@ -1,2 +1,4 @@
# 设置每天 14:45 执行 # 开市每小时执行
45 14 * * * root /usr/local/bin/python /app/main.py >> /var/log/cron.log 2>&1 0 * * * * root /usr/local/bin/python /app/hour.py 2>&1 | tee -a /var/log/cron.log /app/log/cron.log
# 每天 14:45 执行
45 14 * * * root /usr/local/bin/python /app/day.py 2>&1 | tee -a /var/log/cron.log /app/log/cron.log

23
day.py Normal file
View File

@ -0,0 +1,23 @@
import datetime
import akshare as ak
import pushplus
import sz159745
import util
if __name__ == '__main__':
print(ak.__version__)
now = datetime.datetime.now()
print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
# flag = util.is_trading_time_akshare()
# print(f"是否为开市时间: {'是' if flag else '否'}")
# if flag:
cost = "0.66"
price = sz159745.price()
print("sz159745 price:", price)
pushplus.send_msg(now.strftime('%Y-%m-%d %H:%M:%S'),
f"sz159745 cost {cost} price: {price}"
)

16
hour.py Normal file
View File

@ -0,0 +1,16 @@
import datetime
import akshare as ak
import sz159745
import util
if __name__ == '__main__':
print(ak.__version__)
now = datetime.datetime.now()
print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
flag = util.is_trading_time_akshare()
print(f"是否为开市时间: {'' if flag else ''}")
price = sz159745.price()
print("sz159745 price:", price)

View File

@ -1,6 +1,5 @@
import akshare as ak import akshare as ak
import sh000300
if __name__ == '__main__': if __name__ == '__main__':
print(ak.__version__) print(ak.__version__)
sh000300.day()

15
sz159745.py Normal file
View File

@ -0,0 +1,15 @@
import akshare as ak
def fund():
# 获取建材ETF159745的最新行情
df = ak.fund_etf_hist_sina(symbol="sz159745")
latest_price = df.iloc[-1]["close"] # 最新收盘价
print(f"建材ETF159745最新报价{latest_price}")
def price():
df = ak.stock_individual_spot_xq(symbol="sz159745")
latest_price = df.query("item == '现价'")["value"].values[0]
return latest_price
if __name__ == '__main__':
price()

61
util.py Normal file
View File

@ -0,0 +1,61 @@
import datetime
import akshare as ak
def is_trading_time_akshare(current_time=None):
"""
使用akshare判断当前是否为A股开市时间包括交易日和交易时段
参数:
current_time: 待判断的时间默认为当前时间
返回:
bool: 是否为开市状态
"""
if current_time is None:
current_time = datetime.datetime.now()
# 获取当前日期字符串YYYY-MM-DD
current_date_str = current_time.strftime("%Y-%m-%d")
# 获取A股历史交易日数据包含所有交易日
trade_dates = ak.tool_trade_date_hist_sina()
# 1. 判断是否为交易日
is_trading_day = current_date_str in trade_dates["trade_date"].values
if not is_trading_day:
return False
# 2. 判断是否在交易时间段内
# 上午9:30-11:30下午13:00-15:00
current_time_of_day = current_time.time()
morning_start = datetime.time(9, 30)
morning_end = datetime.time(11, 30)
afternoon_start = datetime.time(13, 0)
afternoon_end = datetime.time(15, 0)
in_morning = morning_start <= current_time_of_day <= morning_end
in_afternoon = afternoon_start <= current_time_of_day <= afternoon_end
return in_morning or in_afternoon
if __name__ == "__main__":
now = datetime.datetime.now()
print(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"是否为开市时间: {'' if is_trading_time_akshare() else ''}")
# 测试特定时间
test_times = [
datetime.datetime(2023, 10, 9, 10, 0), # 交易日上午
datetime.datetime(2023, 10, 9, 14, 0), # 交易日下午
datetime.datetime(2023, 10, 9, 8, 0), # 交易日前早
datetime.datetime(2023, 10, 7, 10, 0), # 周六
datetime.datetime(2023, 10, 1, 10, 0), # 国庆假期
]
print("\n测试特定时间:")
for test_time in test_times:
status = "" if is_trading_time_akshare(test_time) else ""
print(f"{test_time.strftime('%Y-%m-%d %H:%M')}: {status}")