老帅李华杰
管理员
管理员
  • UID2
  • 粉丝23
  • 关注0
  • 发帖数323
  • 社区居民
  • 忠实会员
  • 喜欢达人
  • 原创写手
阅读:381回复:0

限定时间平仓

楼主#
更多 发布于:2025-03-11 23:36
from datetime import datetime

# 参数设置
CLOSE_TIME = "14:50"  # 设置平仓时间(根据具体品种调整)

async def on_bar(quote):
    # 转换行情时间为可读格式
    current_time = datetime.fromtimestamp(quote.datetime // 1000000000).strftime('%H:%M')
    
    # 收盘平仓逻辑
    if current_time >= CLOSE_TIME:
        if api.get_position(symbol).pos != 0:
            print(f"[{current_time}] 执行收盘平仓")
            await api.target_position(symbol=symbol, volume=0)
            
# 在main函数中启动策略
async def main():
    symbol = "SHFE.au1912"
    api = TqApi()
    quote = api.get_quote(symbol)
    api.create_task(on_bar(quote))
好的指标等于至高的阵地,明察秋毫自然马到成功; 微信手机同号:15907742318 老帅李华杰
游客

返回顶部