dragon_bridge_predictive_break.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. from datetime import date
  3. from typing import Optional
  4. from dragon_strategy_config import StrategyConfig
  5. def allow_predictive_error_reentry(
  6. *,
  7. enabled: bool,
  8. last_exit_predictive_break: bool,
  9. last_real_sell_date: Optional[date],
  10. row_date: date,
  11. a1: float,
  12. b1: float,
  13. c1: float,
  14. ) -> bool:
  15. if not enabled:
  16. return False
  17. if not last_exit_predictive_break:
  18. return False
  19. if last_real_sell_date is None:
  20. return False
  21. return (
  22. (row_date - last_real_sell_date).days <= 3
  23. and -0.02 < a1 < 0.01
  24. and b1 > -0.16
  25. and c1 > 50
  26. )
  27. def allow_predictive_b1_break_short_exit(
  28. *,
  29. enabled: bool,
  30. has_sell_signal: bool,
  31. entry_is_glued: bool,
  32. holding_days: int,
  33. a1: float,
  34. b1: float,
  35. c1: float,
  36. config: StrategyConfig,
  37. ) -> bool:
  38. if not enabled or has_sell_signal or not entry_is_glued:
  39. return False
  40. return (
  41. holding_days <= config.predictive_b1_break_short_holding_days_max
  42. and config.predictive_b1_break_short_a1_min < a1 < config.predictive_b1_break_short_a1_max
  43. and b1 < config.predictive_b1_break_short_b1_max
  44. and config.predictive_b1_break_short_c1_low < c1 < config.predictive_b1_break_short_c1_high
  45. )
  46. def allow_predictive_b1_break_long_exit(
  47. *,
  48. enabled: bool,
  49. has_sell_signal: bool,
  50. entry_is_glued: bool,
  51. holding_days: int,
  52. max_c1_since_entry: float,
  53. max_a1_since_entry: float,
  54. max_b1_since_entry: float,
  55. last_ql_sell_date: Optional[date],
  56. row_date: date,
  57. a1: float,
  58. b1: float,
  59. c1: float,
  60. config: StrategyConfig,
  61. ) -> bool:
  62. if not enabled or has_sell_signal or not entry_is_glued:
  63. return False
  64. if last_ql_sell_date is None:
  65. return False
  66. ql_days = (row_date - last_ql_sell_date).days
  67. return (
  68. holding_days >= config.predictive_b1_break_long_holding_days_min
  69. and max_c1_since_entry < config.predictive_b1_break_long_max_c1
  70. and max_a1_since_entry > config.predictive_b1_break_long_max_a1
  71. and max_b1_since_entry > config.predictive_b1_break_long_max_b1
  72. and 0 < ql_days <= config.predictive_b1_break_long_ql_days_max
  73. and config.predictive_b1_break_long_a1_min < a1 < config.predictive_b1_break_long_a1_max
  74. and b1 < config.predictive_b1_break_long_b1_max
  75. and config.predictive_b1_break_long_c1_low < c1 < config.predictive_b1_break_long_c1_high
  76. )