def run_tarder_func(c):
'''
运行下单函数
'''
if check_is_trader_date_1():
# 新增:第一步先撤销所有未成交买入委托
cancel_result = cancel_all_orders(c)
if not cancel_result:
print("警告:撤销未完成买入委托失败,将继续执行后续操作")
# 获取当前日期
today_1 = datetime.now().strftime('%Y%m%d')
# 检查上证指数T3条件
sh_diff, sh_t3 = get_shangzheng_diff(c, today_1)
print(f"大盘指数DIFF值: {sh_diff}, T3条件: {sh_t3}")
if sh_t3:
print("大盘指数满足T3条件,策略正常运行")
# 获取初始账户信息
initial_account = get_account(c, c.account, c.account_type)
print(f"初始可用资金: {initial_account['可用金额']}")
# 计算初始买入金额
if text['买入的金额设置'] == '百分比':
initial_total = initial_account['总资产']
initial_ratio = text['百分比']
initial_value = initial_total * initial_ratio
initial_av_value = initial_account['可用金额']
# 确保初始买入金额不超过可用资金
buy_value = min(initial_value, initial_av_value)
else:
buy_value = text['买入金额']
buy_df, sell_df = get_buy_sell_data(c)
# 初始化 sold_stocks 变量
sold_stocks = []
# 先卖出
if sell_df.shape[0] > 0:
for stock, hold_amount, amount in zip(sell_df['证券代码'].tolist(),
sell_df['持仓量'].tolist(), sell_df['可用数量'].tolist()):
try:
print('{} {}持有数量{} 卖出{}'.format(datetime.now(), stock, hold_amount, amount))
passorder(24, 1101, c.account, stock, 6, 0, amount, '', 1, '', c)
sold_stocks.append(stock)
# 卖出后从跟踪止损字典中移除该股票
if stock in stock_trailing_stop:
del stock_trailing_stop[stock]
print(f"移除跟踪止损: {stock}")
except Exception as e:
print(e)
print('{} 卖出有问题{}持有数量{} 卖出{}'.format(datetime.now(), stock, hold_amount, amount))
else:
print('{}卖出没有股票池'.format(datetime.now()))
# 等待卖出委托成交
wait_for_orders(c)
# 获取卖出后的账户信息
post_sell_account = get_account(c, c.account, c.account_type)
post_sell_av_value = post_sell_account['可用金额']
print(f"卖出后可用资金: {post_sell_av_value}")
# 更新当前持股数量,过滤掉已卖出的股票
hold_stock = get_position(c, c.account, c.account_type)
if hold_stock.shape[0] > 0:
hold_stock = hold_stock[~hold_stock['证券代码'].isin(sold_stocks)]
current_hold_count = hold_stock.shape[0]
# 重新计算买入金额,考虑卖出后的可用资金
if text['买入的金额设置'] == '百分比':
post_sell_total = post_sell_account['总资产']
final_ratio = text['百分比']
final_value = post_sell_total * final_ratio
# 确保最终买入金额不超过卖出后的可用资金
final_buy_value = min(final_value, post_sell_av_value)
else:
final_buy_value = text['买入金额']
print(f"最终买入金额: {final_buy_value}")
# 买入
available_buy_count = text['持股限制'] - current_hold_count
if available_buy_count > 0 and buy_df.shape[0] > 0:
# 执行买入前再次检查是否涨停,过滤掉涨停股票
filtered_buy_stocks = []
for stock in buy_df['证券代码'].tolist()[:available_buy_count]:
try:
if check_is_up_limit(c, stock):
print(f"{stock} 当前涨停,不进行买入")
else:
filtered_buy_stocks.append(stock)
except Exception as e:
print(f"检查{stock}涨停状态出错: {e}")
print(f"默认{stock}未涨停,继续执行买入")
filtered_buy_stocks.append(stock)
# 使用过滤后的股票列表执行买入
for stock in filtered_buy_stocks:
try:
trader_type, amount, price = order_stock_value(c, c.account, c.account_type, stock, final_buy_value, trader_type='buy')
if trader_type == 'buy' and amount >= 10:
passorder(23, 1101, c.account, str(stock), 3, 0, amount, '', 1, '', c)
print('{} 买入{} 金额{} 数量{} 价格{}'.format(datetime.now(), stock, final_buy_value, amount, price))
# 买入后初始化跟踪止损信息
stock_trailing_stop[stock] = {'peak_price': price, 'stop_price': price * (1 - TRAILING_STOP_PERCENT)}
print(f"初始化跟踪止损: {stock}, 最高价: {price}, 止损价: {stock_trailing_stop[stock]['stop_price']}")
else:
print('{} 买入不了{} 金额{} 数量{} 价格{}'.format(datetime.now(), stock, final_buy_value, amount, price))
except Exception as e:
print(e)
print('{} {}买入有问题'.format(datetime.now(), stock))
else:
if available_buy_count == 0:
print(f"持股数量已达到限制,不进行买入操作。当前持股数量: {current_hold_count}")
if buy_df.shape[0] == 0:
print('{}买入没有股票池'.format(datetime.now()))
else:
print("大盘指数不满足T3条件,开始清仓并等待")
clear_all_positions(c)
else:
print('{}调整仓不是交易时间'.format(datetime.now()))
def run_log(c):
print('{}程序运行正常等待程序定时调仓'.format(datetime.now()))