刚用QMT不久,回测时发现passorder执行后,重新get_trade_detail_data获取账户可用余额m_dAvailable,和预期的余额不一样。
代码片段如下,初始资金=1000000元, 初始日期20200601以3.959元买入126200股510300.SH, 以141.076元买入3500股511010.SH, 应该剩余现金6608.20元。
而下单完成后, get_trade_detail_data查询结果显示持仓符合预期holdings={'510300.SH': 126200, '511010.SH': 3500},但可用资金=96790.75元。之后每一天的查询结果也都是可用资金=96790.75元。有没有人指点下这个数怎么来的呢?? 佣金印花税也都正确设置了。
def init(C):
C.accountid = "testAccount" # 回测默认账户
C.strategy_state = {
'stock_code': "510300.SH",
'bond_code': "511010.SH",
'initialized': False,
}
print(f"策略初始化完成: 账户={C.accountid} 初始资金={C.capital} 股票={C.strategy_state['stock_code']} 债券={C.strategy_state['bond_code']}")
def handlebar(C):
current_date_str = timetag_to_datetime(C.get_bar_timetag(C.barpos), '%Y%m%d')
current_date_dt = datetime.datetime.strptime(current_date_str, '%Y%m%d')
state = C.strategy_state
account_info = get_trade_detail_data(C.accountid, 'STOCK', 'ACCOUNT')
available_cash = account_info[0].m_dAvailable
balance = account_info[0].m_dBalance
if not state['initialized']:
prices = C.get_market_data_ex(['close'], [state['stock_code'], state['bond_code']], period='1d', count=1, subscribe=False)
stock_price = prices[state['stock_code']].iloc[0, 0]
bond_price = prices[state['bond_code']].iloc[0, 0]
total_cash = available_cash
stock_value = total_cash * 0.5
bond_value = total_cash * 0.5
stock_vol = max(100, int(stock_value / stock_price / 100) * 100)
bond_vol = max(100, int(bond_value / bond_price / 100) * 100)
stock_cash_used = stock_vol * stock_price
bond_cash_used = bond_vol * bond_price
cash_remaining = total_cash - stock_cash_used - bond_cash_used
print(f"{current_date_str} 初始配置: 总资产={balance}元, 可用资金={total_cash}元. 即将以{stock_price}元买入{stock_vol}股{state['stock_code']}, 市值{stock_cash_used:.2f}元; 以{bond_price}元买入{bond_vol}股{state['bond_code']}, 市值{bond_cash_used:.2f}元; 剩余现金{cash_remaining:.2f}元")
if stock_vol > 0:
passorder(23, 1101, C.accountid, state['stock_code'], 5, -1, stock_vol, C)
if bond_vol > 0:
passorder(23, 1101, C.accountid, state['bond_code'], 5, -1, bond_vol, C)
state['initialized'] = True
account_info = get_trade_detail_data(C.accountid, 'STOCK', 'ACCOUNT')
positions = get_trade_detail_data(C.accountid, 'STOCK', 'POSITION')
holdings = {}
if positions is not None:
for pos in positions:
code = pos.m_strInstrumentID + '.' + pos.m_strExchangeID
holdings[code] = pos.m_nVolume
print(f"下单完成后, 最新查新结果为总资产={account_info[0].m_dBalance:.2f}元, 可用资金={account_info[0].m_dAvailable:.2f}元, holdings={holdings}")
return