Files
Crypto_Scam/tests/test_minorder.py
2026-10-22 20:19:52 +03:00

86 lines
4.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""The smallest trade amount both legs accept (spot minimum order, perp minimum lot and notional): paper and real
trades of the spot–futures tabs and «between exchanges» are made only above it. No network: rules are set by hand."""
from __future__ import annotations
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parent))
import basis # noqa: E402
import cross # noqa: E402
import minorder # noqa: E402
from test_basis import pair # noqa: E402
class MinEntryTests(unittest.TestCase):
def setUp(self):
self.rules = minorder.Rules()
self.patch = mock.patch.object(minorder, "RULES", self.rules)
self.patch.start()
self.dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.patch.stop()
self.dir.cleanup()
def test_both_legs_decide(self):
# MEXC CATE: spot 1 $ minimum, perp 1 contract = 100 coins → 100 coins × 0.0754 / (1 − 0.05 %) ≈ 7.54 $
self.rules.set("MEXC", "spot", {"CATE": {"usd": 1.0, "coins": 1.0}})
self.rules.set("MEXC", "fut", {"CATE_USDT": {"coins": 100.0, "usd": 0.0}})
m = self.rules.min_entry("MEXC", "CATE", 0.0754, "MEXC", "CATE_USDT", 0.0786, 0.05)
self.assertAlmostEqual(m["fut"], 100 * 0.0754 / 0.9995)
self.assertEqual(m["usd"], 7.7) # × 1.02 margin, rounded up to a cent
# the spot leg can be the bigger one (Bybit spot 5 $), and the perp's minimum notional counts too
self.rules.set("Bybit", "spot", {"DOGE": {"usd": 5.0, "coins": 0.1}})
self.rules.set("Bybit", "fut", {"DOGEUSDT": {"coins": 1.0, "usd": 5.0}})
m = self.rules.min_entry("Bybit", "DOGE", 0.2, "Bybit", "DOGEUSDT", 0.2, 0.1)
self.assertAlmostEqual(m["spot"], 5.0)
self.assertAlmostEqual(m["fut"], 25 * 0.2 / 0.999) # 5 $ notional = 25 coins
self.assertIn("меньше мин. входа", minorder.too_small(5.0, m, "Bybit", "Bybit"))
self.assertEqual(minorder.too_small(6.0, m, "Bybit", "Bybit"), "")
def test_unknown_rules_do_not_block(self):
self.assertIsNone(self.rules.min_entry("Gate", "XYZ", 1.0, "Gate", "XYZ_USDT", 1.0, 0.1))
self.assertEqual(minorder.too_small(1.0, None, "Gate", "Gate"), "")
self.rules.set("Gate", "fut", {"XYZ_USDT": {"coins": 10.0, "usd": 0.0}}) # one leg known: it counts
self.assertIsNone(self.rules.min_entry("Gate", "XYZ", 1.0, "Gate", "XYZ_USDT", 1.0, 0.1)["spot"])
def test_basis_paper_not_entered_below_minimum(self):
e = basis.BasisEngine("mexc", Path(self.dir.name), start=False)
e.settings.running, e.settings.all_coins, e.settings.amount = True, True, 5.0
e.pairs = {"BIG": pair("BIG"), "OK": pair("OK")}
self.rules.set("MEXC", "fut", {"BIGUSDT": {"coins": 100.0, "usd": 0.0}, "OKUSDT": {"coins": 1.0, "usd": 0.0}})
e.step(1000.0)
self.assertEqual([p.base for p in e.positions], ["OK"])
row = {r["base"]: r for r in e.view()["rows"]}["BIG"]
self.assertFalse(row["fits"])
self.assertIn("меньше мин. входа 102.16 $ (фьюч MEXC ≥ 100.15 $)", row["why"]) # 100 × 1.001 / 0.9995 × 1.02
self.assertAlmostEqual(row["min_entry"], 102.16)
def test_cross_paper_not_entered_below_minimum(self):
e = cross.CrossEngine(Path(self.dir.name), start=False)
e.settings.running, e.settings.history_min, e.settings.amount = True, 0.0, 5.0
key = "XYZ|Gate|Binance"
e.links = {key: cross.Link("XYZ", "Gate", "Binance", "XYZUSDT", "XYZUSDT", 0.999, 1.0, 1.02, 1.021,
1e5, 1e5, 1e7, 1e7, 0.0001, 8.0)}
e.usual, e.seen_since = {key: 0.5}, {key: 0.0}
self.rules.set("Gate", "spot", {"XYZ": {"usd": 3.0, "coins": 0.01}})
self.rules.set("Binance", "fut", {"XYZUSDT": {"coins": 1.0, "usd": 5.0}}) # 5 $ notional at 1.02
e.step(1000.0)
self.assertEqual(e.positions, [])
row = e.view()["rows"][0]
self.assertIn("фьюч Binance ≥", row["why"])
e.settings.amount = 6.0
e.step(1001.0)
self.assertEqual(len(e.positions), 1)
if __name__ == "__main__":
unittest.main()