feat: добавить общий виртуальный счёт, справку и восстановление позиций
This commit is contained in:
1 parent
6aa76064dc
commit
c5aed5b970
29 files changed
+3600
-224
No files matched your search
@@ -0,0 +1,83 @@
|
||||
"""Independent adversarial account checks: an outage must not erase a known loss."""
|
||||
import tempfile
|
||||
import unittest
|
||||
import json
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import cross
|
||||
from virtual_account import AccountError, VirtualAccount
|
||||
|
||||
|
||||
class MissingQuoteAccountingTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
journal = mock.patch.object(cross.CrossEngine, "_journal")
|
||||
journal.start()
|
||||
self.addCleanup(journal.stop)
|
||||
|
||||
def test_later_legacy_import_is_accounted_before_engine_starts(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
account = VirtualAccount(root / "wallet.json", 300)
|
||||
cross.CrossEngine(root, start=False, account=account)
|
||||
legacy = {"positions": [{"base": "XYZ", "opened": 1000, "qty": 100,
|
||||
"spot_entry": 1, "fut_entry": 1.02, "entry_gap": 2,
|
||||
"fees": .15, "funding": .02}]}
|
||||
(root / "basis_bybit.json").write_text(json.dumps(legacy), encoding="utf-8")
|
||||
again = cross.CrossEngine(root, start=False, account=account)
|
||||
self.assertEqual(len(again.positions), 1)
|
||||
self.assertEqual(len(account.restore("cross")["positions"]), 1)
|
||||
self.assertAlmostEqual(account.view()["balance"], 300 - .15 + .02)
|
||||
self.assertGreater(account.view()["reserved"], 200)
|
||||
|
||||
def test_unaffordable_later_import_is_not_adopted_without_reserved_cash(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
account = VirtualAccount(root / "wallet.json", 10)
|
||||
cross.CrossEngine(root, start=False, account=account)
|
||||
legacy = {"positions": [{"base": "XYZ", "opened": 1000, "qty": 100,
|
||||
"spot_entry": 1, "fut_entry": 1.02, "entry_gap": 2, "fees": .15}]}
|
||||
path = root / "basis_bybit.json"
|
||||
path.write_text(json.dumps(legacy), encoding="utf-8")
|
||||
again = cross.CrossEngine(root, start=False, account=account)
|
||||
self.assertTrue(again.account_error)
|
||||
self.assertFalse(again.settings.running)
|
||||
self.assertEqual(again.positions, [])
|
||||
self.assertEqual(account.view()["balance"], 10)
|
||||
self.assertEqual(account.view()["reserved"], 0)
|
||||
self.assertEqual(json.loads(path.read_text(encoding="utf-8")), legacy)
|
||||
|
||||
def test_missing_quote_and_settings_save_cannot_make_known_loss_withdrawable(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
account = VirtualAccount(root / "wallet.json", 300)
|
||||
engine = cross.CrossEngine(root, start=False, account=account)
|
||||
quote = cross.Link("XYZ", "Gate", "Binance", "XYZUSDT", "XYZUSDT",
|
||||
.999, 1, 1.02, 1.021, 1e5, 1e5, 1e7, 1e7, .0001, 8)
|
||||
engine.settings.amount = 100
|
||||
engine.links[quote.key] = quote
|
||||
engine._entries(1000, [{"link": quote, "fits": True, "net": 1, "usual": .5}])
|
||||
engine.links[quote.key] = replace(quote, spot_bid=.5)
|
||||
engine._mark_account(snapshot_fresh=True)
|
||||
loss, free = account.view()["unrealized"], account.view()["free"]
|
||||
self.assertLess(loss, -50)
|
||||
self.assertLess(free, 50)
|
||||
engine.links.clear() # one exchange vanished from the fresh market snapshot
|
||||
self.assertEqual(engine.set_settings({"running": False}), "")
|
||||
self.assertAlmostEqual(account.view()["unrealized"], loss)
|
||||
self.assertAlmostEqual(account.view()["free"], free)
|
||||
with self.assertRaises(AccountError):
|
||||
account.adjust(-80, "must-not-withdraw-lost-money")
|
||||
|
||||
# The loss is in the authoritative position snapshot, not just an in-memory aggregate.
|
||||
restored_account = VirtualAccount(root / "wallet.json")
|
||||
restored = cross.CrossEngine(root, start=False, account=restored_account)
|
||||
self.assertAlmostEqual(restored.positions[0].paper_unrealized, loss)
|
||||
restored.save()
|
||||
self.assertAlmostEqual(restored_account.view()["unrealized"], loss)
|
||||
self.assertIn("cross", restored_account.view()["stale"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user