为什么实际已没有资金了,但是程序仍发送买入订单?
由franklili创建,最终由franklili 被浏览 28 用户
问题
我参考以下知识库文章写了买入程序。https://bigquant.com/wiki/doc/dingyi-luoji-OCkaNT91EC
# 1. 资金分配
cash_avg = context.portfolio.portfolio_value / trading_days
cash = context.portfolio.cash
cash_for_buy = min(context.portfolio.cash, cash_avg)
amount_for_buy = int(cash_for_buy / price)
positions_amount = context.portfolio.positions[sid].amount
amount_avg = cash_avg / price
amount_for_sell = int(min(positions_amount, amount_avg))
if (zone == 'undervalue' or (zone == 'reasonable' and context.trade_director == 'buy')) and amount_for_buy >= 1:
print('date: ' + data.current_dt.strftime('%Y-%m-%d'))
print(' daily_residual: ' + str(daily_residual))
print(' zone: ' + str(zone))
print(' positions_amount: '+ str(positions_amount))
print(' cash:' + str(cash))
print(' cash_for_buy:' + str(cash_for_buy))
print(' price: ' + str(price))
print(' amount_for_buy:' + str(amount_for_buy))
print(' buy signal')
context.trade_director = 'buy'
context.order_value(sid, cash_for_buy)
打印结果如下:
date: 2021-03-09 daily_residual: 0.8778283962324132 zone: reasonable positions_amount: 82 cash:4056051.1746489997 cash_for_buy:898845.0749297999 price: 54729.0 amount_for_buy:16 buy signal date: 2021-03-10 daily_residual: 0.931082019472111 zone: reasonable positions_amount: 82 cash:4178887.1746489997 cash_for_buy:923412.2749297998 price: 56227.0 amount_for_buy:16 buy signal date: 2021-03-11 daily_residual: 0.9275399892576957 zone: reasonable positions_amount: 82 cash:4314843.174648999 cash_for_buy:950603.4749297999 price: 57885.0 amount_for_buy:16 buy signal date: 2021-03-12 daily_residual: 0.92788406191438 zone: reasonable positions_amount: 82 cash:4272695.174648999 cash_for_buy:942173.8749297999 price: 57371.0 amount_for_buy:16 buy signal
\
平台日志结果为:
2021-03-13 23:59:00 WARN 2021-03-13 cancel order Future(0 [BTC_USDT.HBI]) since no cash 2021-03-13 23:59:00 INFO order[23:59:00][id:021366,BTC_USDT.HBI -16@MARKET, offset:平昨] 2021-03-12 23:59:00 WARN 2021-03-12 cancel order Future(0 [BTC_USDT.HBI]) since no cash 2021-03-12 23:59:00 INFO order[00:00:00][id:5251f5,BTC_USDT.HBI 16@MARKET, offset:开仓] 2021-03-11 23:59:00 WARN 2021-03-11 cancel order Future(0 [BTC_USDT.HBI]) since no cash 2021-03-11 23:59:00 INFO order[00:00:00][id:1f2156,BTC_USDT.HBI 16@MARKET, offset:开仓] 2021-03-10 23:59:00 WARN 2021-03-10 cancel order Future(0 [BTC_USDT.HBI]) since no cash 2021-03-10 23:59:00 INFO order[00:00:00][id:08eec8,BTC_USDT.HBI 16@MARKET, offset:开仓] 2021-03-09 23:59:00 WARN 2021-03-09 cancel order Future(0 [BTC_USDT.HBI]) since no cash 2021-03-09 23:59:00 INFO order[00:00:00][id:0096d1,BTC_USDT.HBI 16@MARKET, offset:开仓]
为什么打印显示有资金,但是平台日志显示没有资金?而且在没有成交的情况下,每天的资金数在变化?