diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml index 4b78435..97450e8 100644 --- a/.gitea/workflows/docker-build.yml +++ b/.gitea/workflows/docker-build.yml @@ -21,4 +21,4 @@ jobs: - name: 直接启动服务 run: | docker rm -vf ak13 || true - docker run -d --name ak13 --workdir /app -v ak13-logs:/app/logs ak13:latest \ No newline at end of file + docker run -d --name ak13 --workdir /app -e TZ=Asia/Shanghai -v ak13-logs:/app/logs ak13:latest \ No newline at end of file diff --git a/cronjob b/cronjob index 7cab833..d56e066 100644 --- a/cronjob +++ b/cronjob @@ -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 diff --git a/day.py b/day.py new file mode 100644 index 0000000..00503f6 --- /dev/null +++ b/day.py @@ -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}" + ) + diff --git a/hour.py b/hour.py new file mode 100644 index 0000000..8881e1b --- /dev/null +++ b/hour.py @@ -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) diff --git a/main.py b/main.py index e975e02..c1c4e46 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,5 @@ import akshare as ak -import sh000300 + if __name__ == '__main__': print(ak.__version__) - sh000300.day() diff --git a/sz159745.py b/sz159745.py new file mode 100644 index 0000000..fb0ba50 --- /dev/null +++ b/sz159745.py @@ -0,0 +1,15 @@ +import akshare as ak + +def fund(): + # 获取建材ETF(159745)的最新行情 + df = ak.fund_etf_hist_sina(symbol="sz159745") + latest_price = df.iloc[-1]["close"] # 最新收盘价 + print(f"建材ETF(159745)最新报价:{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() \ No newline at end of file diff --git a/util.py b/util.py new file mode 100644 index 0000000..bb73d50 --- /dev/null +++ b/util.py @@ -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}") \ No newline at end of file