144 lines
6.8 KiB
Python
144 lines
6.8 KiB
Python
|
|
"""限价交给QMT等待,数量、资金与用户命令仍在PMS检查。"""
|
|||
|
|
import unit_test_guard
|
|||
|
|
import copy
|
|||
|
|
import unittest
|
|||
|
|
from datetime import datetime
|
|||
|
|
from unittest.mock import patch
|
|||
|
|
from test_wiring import install_fakes
|
|||
|
|
from app.services import exec_advisor as ea, executor, market
|
|||
|
|
|
|||
|
|
CODE = '300693.SZ'
|
|||
|
|
NOW = datetime(2026, 10, 28, 11, 10)
|
|||
|
|
|
|||
|
|
|
|||
|
|
class QmtPriceTests(unittest.TestCase):
|
|||
|
|
def setUp(self):
|
|||
|
|
self.fake = install_fakes(prices={CODE: 31.10}, params={
|
|||
|
|
'PMS_EXEC_QMT_PRICE_MONITOR': True, 'PMS_EXEC_IMPL': 'A',
|
|||
|
|
'PMS_JUDGE_API_BASE': 'http://fake', 'PMS_TOTAL_SCALE': 2000000,
|
|||
|
|
'PMS_DISPATCH_MODE': 'ws'})
|
|||
|
|
self.fake.qmt.set_channel()
|
|||
|
|
self.response = {'verdict': 'WAIT', 'reason': '价格未进区间', 'observed': {
|
|||
|
|
'support': 29.1, 'pressure': 32.15, 'buy_band': [28.81, 30.02],
|
|||
|
|
'sell_band': [31.24, 32.47], 'y_signal': 'WATCH', 'today_exit': None}}
|
|||
|
|
self.post = patch.object(ea, '_post', side_effect=lambda *a: copy.deepcopy(self.response)).start()
|
|||
|
|
patch.object(ea.td, 'ymd', return_value=20261028).start()
|
|||
|
|
patch.object(executor.td, 'trade_days_left', return_value=3).start()
|
|||
|
|
self.addCleanup(patch.stopall)
|
|||
|
|
|
|||
|
|
def decide(self, **kw):
|
|||
|
|
args = dict(side='buy', action='OPEN', ts_code=CODE, now=NOW,
|
|||
|
|
day={'price': 31.1, 'day_chg_from_open': .2, 'limit_up': True},
|
|||
|
|
params={}, is_last_day=False, quota=200, fired_today=0, prog={})
|
|||
|
|
args.update(kw)
|
|||
|
|
return ea.decide(**args)
|
|||
|
|
|
|||
|
|
def parent(self):
|
|||
|
|
self.fake.insert_instruction(instruction_id='P', origin_type='plan', origin_id='PLAN',
|
|||
|
|
ts_code=CODE, action='OPEN', side='buy', qty=600, status='PROPOSED',
|
|||
|
|
progress={'deadline': '2026-10-30', 'is_command': True, 'children': []})
|
|||
|
|
|
|||
|
|
def test_outside_band_submits_limit_without_chasing(self):
|
|||
|
|
for price in (31.10, 100, 20):
|
|||
|
|
r = self.decide(day={'price': price, 'day_chg_from_open': .2})
|
|||
|
|
self.assertEqual((r['action'], r['limit_price'], r['qty_hint']), ('FIRE', 30.02, 200))
|
|||
|
|
|
|||
|
|
def test_sell_below_band_uses_sell_floor(self):
|
|||
|
|
r = self.decide(side='sell', action='TRIM', now='09:35')
|
|||
|
|
self.assertEqual((r['action'], r['limit_price']), ('FIRE', 31.24))
|
|||
|
|
|
|||
|
|
def test_missing_invalid_prices_and_service_error_never_fallback_to_market(self):
|
|||
|
|
for band in (None, [], [30, 20], [0, 20], [20, float('nan')], [20, float('inf')]):
|
|||
|
|
self.response['observed']['buy_band'] = band
|
|||
|
|
self.assertEqual(self.decide()['action'], 'WAIT')
|
|||
|
|
self.post.side_effect = RuntimeError('unavailable')
|
|||
|
|
self.assertEqual(self.decide()['action'], 'WAIT')
|
|||
|
|
|
|||
|
|
def test_unavailable_and_missing_source_wait(self):
|
|||
|
|
self.response['verdict'] = 'UNAVAILABLE'
|
|||
|
|
self.assertEqual(self.decide()['action'], 'WAIT')
|
|||
|
|
self.fake.params['PMS_EXEC_IMPL'] = 'B'
|
|||
|
|
self.assertEqual(self.decide()['action'], 'WAIT')
|
|||
|
|
|
|||
|
|
def test_today_exit_bad_signal_and_reference_drift_still_block(self):
|
|||
|
|
self.response['observed']['today_exit'] = 'EXIT'
|
|||
|
|
self.assertEqual(self.decide()['action'], 'WAIT')
|
|||
|
|
self.response['observed'].update(today_exit=None, y_signal='AVOID')
|
|||
|
|
self.assertEqual(self.decide()['action'], 'WAIT')
|
|||
|
|
self.response['observed']['y_signal'] = 'WATCH'
|
|||
|
|
r = self.decide(prog={'ref_lock': {'ymd': 20261028, 'support': 25, 'pressure': 32.15}})
|
|||
|
|
self.assertEqual(r['action'], 'WAIT')
|
|||
|
|
self.assertTrue(r['ref_drift'])
|
|||
|
|
|
|||
|
|
def test_old_cache_must_refresh_exit_facts(self):
|
|||
|
|
old = {'exec_advice': {'ymd': 20261028, 'verdict': 'WAIT', 'buy_band': [28, 30],
|
|||
|
|
'valid_until_min': 900}}
|
|||
|
|
self.response['observed']['today_exit'] = 'EXIT'
|
|||
|
|
self.assertEqual(self.decide(prog=old)['action'], 'WAIT')
|
|||
|
|
self.assertEqual(self.post.call_count, 1)
|
|||
|
|
|
|||
|
|
def test_valid_cache_does_not_poll_price_again(self):
|
|||
|
|
prog = {}
|
|||
|
|
self.decide(prog=prog)
|
|||
|
|
self.decide(prog=prog)
|
|||
|
|
self.assertEqual(self.post.call_count, 1)
|
|||
|
|
|
|||
|
|
def test_quota_session_halt_and_missing_quote_remain(self):
|
|||
|
|
for kw in ({'fired_today': 200}, {'now': '12:00'}, {'day': {'price': 0}},
|
|||
|
|
{'day': {'price': float('nan')}}, {'day': {'price': 31, 'halted': True}}):
|
|||
|
|
self.assertNotEqual(self.decide(**kw)['action'], 'FIRE')
|
|||
|
|
self.assertEqual(self.post.call_count, 0)
|
|||
|
|
|
|||
|
|
def test_late_day_does_not_reprice_buy_to_market(self):
|
|||
|
|
r = self.decide(now='14:50', is_last_day=True)
|
|||
|
|
self.assertEqual(r['limit_price'], 30.02)
|
|||
|
|
self.assertFalse(r['forced'])
|
|||
|
|
|
|||
|
|
def test_disabled_mode_retains_old_wait(self):
|
|||
|
|
self.fake.params['PMS_EXEC_QMT_PRICE_MONITOR'] = False
|
|||
|
|
self.assertEqual(self.decide(day={'price': 31.1})['action'], 'WAIT')
|
|||
|
|
|
|||
|
|
def test_grid_and_urgent_keep_own_limit(self):
|
|||
|
|
grid = self.decide(prog={'origin': 'strategy', 'stype': 'GRID'}, instruction_limit=29.8)
|
|||
|
|
urgent = self.decide(side='sell', urgent=True)
|
|||
|
|
self.assertEqual(grid['limit_price'], 29.8)
|
|||
|
|
self.assertEqual(urgent['action'], 'FIRE')
|
|||
|
|
self.assertEqual(self.post.call_count, 0)
|
|||
|
|
|
|||
|
|
def test_run_tick_reserves_one_daily_slice_and_does_not_repeat(self):
|
|||
|
|
self.parent()
|
|||
|
|
with patch.object(market, 'get_ma5', side_effect=AssertionError('should not monitor MA5')), \
|
|||
|
|
patch.object(market, 'day_snapshot', return_value={'price': 31.10, 'day_chg_from_open': .2}):
|
|||
|
|
r = executor.run_tick(now=NOW)
|
|||
|
|
self.assertTrue(r['ok'], r)
|
|||
|
|
self.assertEqual([(x['qty'], x['limit']) for x in r['fired']], [(200, 30.02)])
|
|||
|
|
self.assertEqual(executor.run_tick(now=NOW)['fired'], [])
|
|||
|
|
self.assertEqual(len(self.fake.qmt.orders), 1)
|
|||
|
|
self.assertEqual(self.fake.instructions['P']['exec_qty'], 0)
|
|||
|
|
|
|||
|
|
def test_pause_and_cash_limits_still_reject(self):
|
|||
|
|
self.parent()
|
|||
|
|
self.fake.params['PMS_GLOBAL_BUY_HALT'] = True
|
|||
|
|
r = executor.run_tick(now=NOW)
|
|||
|
|
self.assertEqual(r['fired'], [])
|
|||
|
|
self.assertIn('BUY_HALT', str(r))
|
|||
|
|
self.fake.params['PMS_GLOBAL_BUY_HALT'] = False
|
|||
|
|
self.fake.params['PMS_TOTAL_SCALE'] = 1
|
|||
|
|
r = executor.run_tick(now=NOW)
|
|||
|
|
self.assertEqual(r['fired'], [])
|
|||
|
|
self.assertTrue(r['rejected'], r)
|
|||
|
|
|
|||
|
|
def test_dry_run_preserves_reference_lock_and_writes_nothing(self):
|
|||
|
|
self.parent()
|
|||
|
|
self.fake.instructions['P']['progress']['ref_lock'] = {
|
|||
|
|
'ymd': 20261028, 'support': 25, 'pressure': 32.15}
|
|||
|
|
before = copy.deepcopy(self.fake.instructions)
|
|||
|
|
r = executor.run_tick(now=NOW, dry_run=True)
|
|||
|
|
self.assertEqual(r['fired'], [])
|
|||
|
|
self.assertEqual(self.fake.instructions, before)
|
|||
|
|
self.assertEqual(len(self.fake.qmt.orders), 0)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
unittest.main()
|