Add options index target monitors that auto limit-close on hit.
Position and order forms can arm a target; right-side and hub panels show active monitors; expiry remains the stop with no separate SL. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from lib.options.options_hub_lib import build_options_hub_snapshot
|
||||
|
||||
@@ -10,7 +10,15 @@ class OptionsHubLibTests(TestCase):
|
||||
self.assertFalse(out["enabled"])
|
||||
self.assertTrue(out["ok"])
|
||||
|
||||
def test_build_options_hub_snapshot_positions(self):
|
||||
@patch("lib.options.options_hub_lib._compute_options_stats", return_value={})
|
||||
@patch("lib.options.options_positions_lib.build_display_option_positions")
|
||||
def test_build_options_hub_snapshot_positions(self, mock_positions, _mock_stats):
|
||||
mock_positions.return_value = [
|
||||
{"inst_id": "ETH-USD_UM-260703-1800-C", "pos": 2, "upl": 1.5, "mark_px": 0.1}
|
||||
]
|
||||
conn = MagicMock()
|
||||
conn.__enter__ = MagicMock(return_value=conn)
|
||||
conn.__exit__ = MagicMock(return_value=False)
|
||||
cfg = {
|
||||
"enabled": True,
|
||||
"exchange_options": object(),
|
||||
@@ -18,19 +26,16 @@ class OptionsHubLibTests(TestCase):
|
||||
"fetch_option_positions": lambda ex: [
|
||||
{"instId": "ETH-USD_UM-260703-1800-C", "pos": "2", "upl": "1.5", "markPx": "0.1"}
|
||||
],
|
||||
"format_position_row": lambda p: {
|
||||
"inst_id": p.get("instId"),
|
||||
"pos": 2,
|
||||
"upl": 1.5,
|
||||
"mark_px": 0.1,
|
||||
},
|
||||
"fetch_options_balances": lambda ex: {"trading_usdc": 9.5, "funding_usdc": 12.0},
|
||||
"get_db": MagicMock(),
|
||||
"get_db": MagicMock(return_value=conn),
|
||||
"trade_budget": 10,
|
||||
"account_label": "OKX期权",
|
||||
}
|
||||
out = build_options_hub_snapshot(cfg)
|
||||
self.assertTrue(out["ok"])
|
||||
with patch("lib.options.options_target_lib.list_active_targets", return_value=[]):
|
||||
with patch("lib.options.options_target_lib.targets_by_inst", return_value={}):
|
||||
out = build_options_hub_snapshot(cfg)
|
||||
self.assertTrue(out["ok"], out.get("msg"))
|
||||
self.assertEqual(out["position_count"], 1)
|
||||
self.assertEqual(out["upl_total_usdc"], 1.5)
|
||||
self.assertEqual(out["trading_usdc"], 9.5)
|
||||
self.assertEqual(out.get("target_monitors"), [])
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""期权目标位委托单元测试."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import unittest
|
||||
|
||||
from lib.options.options_target_lib import (
|
||||
ensure_target_tables,
|
||||
list_active_targets,
|
||||
run_options_target_closes,
|
||||
target_hit,
|
||||
upsert_target_monitor,
|
||||
)
|
||||
|
||||
|
||||
class OptionsTargetLibTests(unittest.TestCase):
|
||||
def test_target_hit_call_put(self):
|
||||
self.assertTrue(target_hit(opt_type="C", index_px=2000, target_index=1950))
|
||||
self.assertFalse(target_hit(opt_type="C", index_px=1900, target_index=1950))
|
||||
self.assertTrue(target_hit(opt_type="P", index_px=1800, target_index=1850))
|
||||
self.assertFalse(target_hit(opt_type="P", index_px=1900, target_index=1850))
|
||||
|
||||
def test_upsert_and_trigger_close(self):
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn.row_factory = sqlite3.Row
|
||||
ensure_target_tables(conn)
|
||||
out = upsert_target_monitor(
|
||||
conn,
|
||||
inst_id="ETH-USD_UM-260717-1900-C",
|
||||
target_index=1880,
|
||||
opt_type="C",
|
||||
sheets=1,
|
||||
)
|
||||
self.assertTrue(out["ok"])
|
||||
self.assertEqual(len(list_active_targets(conn)), 1)
|
||||
|
||||
closed = []
|
||||
|
||||
def close_fn(inst_id: str):
|
||||
closed.append(inst_id)
|
||||
return {"ok": True, "submitted_sheets": 1, "premium_received": 1.2, "close_ord_id": "oid1"}
|
||||
|
||||
n = run_options_target_closes(
|
||||
conn,
|
||||
[{"inst_id": "ETH-USD_UM-260717-1900-C", "idx_px": 1885, "opt_type": "C"}],
|
||||
close_fn=close_fn,
|
||||
)
|
||||
self.assertEqual(n, 1)
|
||||
self.assertEqual(closed, ["ETH-USD_UM-260717-1900-C"])
|
||||
self.assertEqual(len(list_active_targets(conn)), 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user