test_strategy.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import pytest
  2. from okx_codex_trader.strategy import validate_signal
  3. @pytest.mark.parametrize(
  4. ("field_name", "field_value"),
  5. [
  6. ("confidence", True),
  7. ("leverage", True),
  8. ("entry_price", True),
  9. ("take_profit_price", False),
  10. ("stop_loss_price", True),
  11. ],
  12. )
  13. def test_validate_signal_rejects_boolean_numeric_fields(field_name, field_value):
  14. signal = {
  15. "action": "long",
  16. "confidence": 0.9,
  17. "leverage": 2,
  18. "entry_price": None,
  19. "take_profit_price": None,
  20. "stop_loss_price": None,
  21. "reason": "x",
  22. }
  23. signal[field_name] = field_value
  24. with pytest.raises(ValueError):
  25. validate_signal(signal)
  26. def test_validate_signal_rejects_leverage_out_of_range():
  27. with pytest.raises(ValueError):
  28. validate_signal(
  29. {
  30. "action": "long",
  31. "confidence": 0.9,
  32. "leverage": 4,
  33. "entry_price": None,
  34. "take_profit_price": None,
  35. "stop_loss_price": None,
  36. "reason": "x",
  37. }
  38. )
  39. def test_validate_signal_rejects_unknown_action():
  40. with pytest.raises(ValueError):
  41. validate_signal(
  42. {
  43. "action": "hold",
  44. "confidence": 0.9,
  45. "leverage": 2,
  46. "entry_price": None,
  47. "take_profit_price": None,
  48. "stop_loss_price": None,
  49. "reason": "x",
  50. }
  51. )
  52. def test_validate_signal_rejects_confidence_out_of_range():
  53. with pytest.raises(ValueError):
  54. validate_signal(
  55. {
  56. "action": "long",
  57. "confidence": 1.2,
  58. "leverage": 2,
  59. "entry_price": None,
  60. "take_profit_price": None,
  61. "stop_loss_price": None,
  62. "reason": "x",
  63. }
  64. )
  65. def test_validate_signal_requires_full_shape():
  66. with pytest.raises(ValueError):
  67. validate_signal({"action": "long", "confidence": 0.9, "leverage": 2})
  68. def test_validate_signal_returns_trade_signal():
  69. signal = validate_signal(
  70. {
  71. "action": "short",
  72. "confidence": 0.75,
  73. "leverage": 3,
  74. "entry_price": 101.5,
  75. "take_profit_price": 98,
  76. "stop_loss_price": None,
  77. "reason": "trend",
  78. }
  79. )
  80. assert signal.action == "short"
  81. assert signal.confidence == 0.75
  82. assert signal.leverage == 3
  83. assert signal.entry_price == 101.5
  84. assert signal.take_profit_price == 98.0
  85. assert signal.stop_loss_price is None
  86. assert signal.reason == "trend"