1.10K 浏览
0

前天晚上估计是连不上,所以昨天升级了VIP。运行程序,打印“程序还没开始……”,没有任何错误信息,就结束了。

import pandas as pd
import numpy as np
import datetime
import time
import os
from copy import copy
from tqsdk import TqApi, TqAuth, TqAccount, TqKq
 start_time = '21:00'
has_night = True
 year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
#'''
print('程序还没开始......')
startTime = datetime.datetime(year, month, day, 20, 49, 0)
while datetime.datetime.now() < startTime:
    time.sleep(600)

print('程序还有10分钟开始……')
startTime = datetime.datetime(year, month, day, 20, 59, 0)
while datetime.datetime.now() < startTime:
    time.sleep(10)
#'''
  attempts = 0
success = False
while attempts < 200 and not success:
    try:
        api = TqApi(account=TqAccount(……), auth=TqAuth(……)
        success = True
    except:
        attempts += 1
 account = api.get_account()
cash = account.available
#资金有十几万元,应该连原油都够开一手的。
df_margin = pd.read_csv('margin.csv')
df_symbol = pd.DataFrame()
 for index,row in df_margin.iterrows():
     product = row['product']
    margin_rate = row['margin']
     try:
        #future = api.query_quotes(ins_class="FUTURE", product_id=product)
        future = api.query_cont_quotes(product_id=product,has_night=has_night)
    except:
        #future = api.query_quotes(ins_class="FUTURE", product_id=product.upper())
        future = api.query_cont_quotes(product_id=product.upper(),has_night=has_night)
    #'''
    try:
        symbol = future[0]
    except:
        continue
     q = api.get_quote(symbol)
     if q.exchange_id in ['SHFE','INE']:
        #上期所的合约,有五档行情,平仓要平今仓。
        ask_volume = q.ask_volume1 + q.ask_volume2 + q.ask_volume3 + q.ask_volume4 + q.ask_volume5
        bid_volume = q.bid_volume1 + q.bid_volume2 + q.bid_volume3 + q.bid_volume4 + q.bid_volume5
        close_offset = 'CLOSETODAY'
    else:
        #非上期所的合约,只有一档行情,平仓不分今仓和老仓。
        ask_volume = q.ask_volume1
        bid_volume = q.bid_volume1
        close_offset = 'CLOSE'
         #过滤掉不活跃的合约。
    min_vol = min(ask_volume, bid_volume)
    if min_vol == 0:
        continue
     margin = q.upper_limit * q.volume_multiple * margin_rate
    #过滤不够钱开仓的合约。
    volume = int(cash*0.9 / margin)
    if volume < 1:
        continue
     #判断开仓的方向。
    open_direction, close_direction = 'SELL', 'BUY'
    if bid_volume > ask_volume:
        open_direction, close_direction = 'BUY', 'SELL'
     #计算方向的强度。
    max_vol = max(ask_volume, bid_volume)
    rate = max_vol / min_vol
     #计算开平仓的价格。
    market_price = {'BUY':q.upper_limit, 'SELL':q.lower_limit}
    open_price = market_price[open_direction]
    close_price = market_price[close_direction]
    #将结果写入表格。
    row = pd.DataFrame({'symbol':[symbol], 'rate':[rate], 'open_direction':[open_direction], 'close_direction':[close_direction], 'open_price':[open_price], 'close_price':[close_price], 'close_offset':[close_offset], 'volume':[volume]})
    df_symbol = df_symbol.append(row)
 df_symbol = df_symbol.sort_values('rate',ascending=False)
symbol = list(df_symbol['symbol'])[0]
print(symbol)

万一表格是空的,倒数第二行应该报错。但程序并没有打印出选合约的结果,也就是说最后一行没有执行。

这个程序,注释掉和开盘时间有关的几行,昨天白天用模拟交易运行了两次,都没问题的。

dadaowu 已回答的问题 2022年3月16日
0

14 -5应该没有问题,

dadaowu 已回答的问题 2022年3月16日