pybinbot 2.1.9__tar.gz → 2.1.12__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pybinbot-2.1.9/pybinbot.egg-info → pybinbot-2.1.12}/PKG-INFO +16 -1
- {pybinbot-2.1.9 → pybinbot-2.1.12}/README.md +15 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/strategy/swing_protection.py +169 -43
- {pybinbot-2.1.9 → pybinbot-2.1.12/pybinbot.egg-info}/PKG-INFO +16 -1
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pyproject.toml +1 -1
- pybinbot-2.1.12/tests/test_swing_protection.py +271 -0
- pybinbot-2.1.9/tests/test_swing_protection.py +0 -153
- {pybinbot-2.1.9 → pybinbot-2.1.12}/LICENSE +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/MANIFEST.in +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/__init__.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/binance/base.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/binance/exceptions.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/binbot/base.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/binbot/exceptions.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/coingecko.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/kucoin/base.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/kucoin/exceptions.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/kucoin/futures.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/kucoin/market.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/kucoin/orders.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/apis/kucoin/rest.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/__init__.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/autotrade_settings.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/bot.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/bot_base.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/cvd.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/deal.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/derivatives.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/fastest_movers.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/grid_ladder.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/order.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/routes.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/signals.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/models/symbol.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/py.typed +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/__init__.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/beta.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/breadth.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/cache.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/candles.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/enums.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/handlers.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/heikin_ashi.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/indicators.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/logging_config.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/maths.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/price_crossings.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/telegram_delivery.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/timestamps.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/types.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/shared/utils.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/strategy/beta.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/strategy/breadth.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/strategy/price_crossings.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/streaming/binance/async_socket_client.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/streaming/binance/socket_client.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/streaming/binance/socket_manager.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot/streaming/kucoin/kucoin_async_client.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot.egg-info/SOURCES.txt +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot.egg-info/dependency_links.txt +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot.egg-info/requires.txt +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/pybinbot.egg-info/top_level.txt +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/setup.cfg +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_beta.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_binbot_api.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_bot_futures_leverage.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_breadth.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_candles.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_closed_bot_status.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_coingecko.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_conditional_entry.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_conditional_futures_orders.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_cvd_api.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_deal_model.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_derivatives_api.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_fastest_movers.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_futures_place_order.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_grid_ladder.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_handlers.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_heikin_ashi.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_indicators.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_kucoin_api.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_logging_config.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_matching_engine.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_maths.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_price_crossings.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_symbol_model.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_telegram_delivery.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_timestamps.py +0 -0
- {pybinbot-2.1.9 → pybinbot-2.1.12}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pybinbot
|
|
3
|
-
Version: 2.1.
|
|
3
|
+
Version: 2.1.12
|
|
4
4
|
Summary: Utility functions for the binbot project.
|
|
5
5
|
Author-email: Carlos Wu <carkodw@gmail.com>
|
|
6
6
|
Requires-Python: >=3.15
|
|
@@ -172,3 +172,18 @@ The selector rejects swing candles wider than 3% and stop distances over 2.5%
|
|
|
172
172
|
after the buffer and outward tick rounding. It never clips a distant swing or
|
|
173
173
|
searches older swings to force a result. Entry callers skip when all timeframes
|
|
174
174
|
fail; live callers retain existing protection and only apply tighter stops.
|
|
175
|
+
|
|
176
|
+
`refine_swing_protection` manages the whole timeframe decision. If 1m history
|
|
177
|
+
is gapped or unreliable, it returns the nearest trustworthy coarser swing.
|
|
178
|
+
Missing timestamps, duplicates, stale bars, zero-volume bars, or frequent price
|
|
179
|
+
gaps make a source unreliable. Price gaps are non-overlapping candle ranges
|
|
180
|
+
separated by more than 0.1% and two price-precision ticks; more than 20% of
|
|
181
|
+
transitions, with at least two gaps, rejects the timeframe.
|
|
182
|
+
|
|
183
|
+
The 3% candle-width threshold prompts refinement. It can be relaxed for a
|
|
184
|
+
coarser fallback after finer data fails, while the buffered stop-distance cap,
|
|
185
|
+
three-hour swing age, confirmation and unbroken-swing requirements still apply.
|
|
186
|
+
If no coarser swing fits, the result has no placement. A missing timeframe in
|
|
187
|
+
the input mapping requests that interval; an empty list means the read finished
|
|
188
|
+
without usable data. Live callers can therefore keep exchange reads off the
|
|
189
|
+
protective exit path while this decision remains shared with entry strategies.
|
|
@@ -143,3 +143,18 @@ The selector rejects swing candles wider than 3% and stop distances over 2.5%
|
|
|
143
143
|
after the buffer and outward tick rounding. It never clips a distant swing or
|
|
144
144
|
searches older swings to force a result. Entry callers skip when all timeframes
|
|
145
145
|
fail; live callers retain existing protection and only apply tighter stops.
|
|
146
|
+
|
|
147
|
+
`refine_swing_protection` manages the whole timeframe decision. If 1m history
|
|
148
|
+
is gapped or unreliable, it returns the nearest trustworthy coarser swing.
|
|
149
|
+
Missing timestamps, duplicates, stale bars, zero-volume bars, or frequent price
|
|
150
|
+
gaps make a source unreliable. Price gaps are non-overlapping candle ranges
|
|
151
|
+
separated by more than 0.1% and two price-precision ticks; more than 20% of
|
|
152
|
+
transitions, with at least two gaps, rejects the timeframe.
|
|
153
|
+
|
|
154
|
+
The 3% candle-width threshold prompts refinement. It can be relaxed for a
|
|
155
|
+
coarser fallback after finer data fails, while the buffered stop-distance cap,
|
|
156
|
+
three-hour swing age, confirmation and unbroken-swing requirements still apply.
|
|
157
|
+
If no coarser swing fits, the result has no placement. A missing timeframe in
|
|
158
|
+
the input mapping requests that interval; an empty list means the read finished
|
|
159
|
+
without usable data. Live callers can therefore keep exchange reads off the
|
|
160
|
+
protective exit path while this decision remains shared with entry strategies.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""Choose recent structural protection without clipping a distant swing."""
|
|
2
2
|
|
|
3
|
-
from collections.abc import Sequence
|
|
3
|
+
from collections.abc import Mapping, Sequence
|
|
4
4
|
from dataclasses import dataclass
|
|
5
5
|
from decimal import ROUND_CEILING, ROUND_FLOOR, Decimal
|
|
6
6
|
from math import isfinite
|
|
@@ -12,6 +12,8 @@ PROTECTION_INTERVALS_MINUTES = (15, 5, 1)
|
|
|
12
12
|
PROTECTION_LOOKBACK_MS = 3 * 60 * 60 * 1000
|
|
13
13
|
MAX_PROTECTION_DISTANCE_PCT = 2.5
|
|
14
14
|
MAX_PROTECTION_CANDLE_RANGE_PCT = 3.0
|
|
15
|
+
MAX_PROTECTION_PRICE_GAP_RATIO = 0.2
|
|
16
|
+
MIN_PROTECTION_PRICE_GAP_PCT = 0.1
|
|
15
17
|
|
|
16
18
|
|
|
17
19
|
@dataclass(frozen=True)
|
|
@@ -24,6 +26,66 @@ class SwingProtection:
|
|
|
24
26
|
candle_range_pct: float
|
|
25
27
|
|
|
26
28
|
|
|
29
|
+
def _completed_protection_window(
|
|
30
|
+
candles: Sequence[Sequence[float]],
|
|
31
|
+
*,
|
|
32
|
+
now_ms: int,
|
|
33
|
+
interval_minutes: int,
|
|
34
|
+
lookback_ms: int,
|
|
35
|
+
fractal_wing: int,
|
|
36
|
+
price_precision: int,
|
|
37
|
+
) -> list[tuple[int, float, float, float, float]] | None:
|
|
38
|
+
interval_ms = interval_minutes * 60 * 1000
|
|
39
|
+
window_end = now_ms // interval_ms * interval_ms
|
|
40
|
+
window_start = now_ms - lookback_ms
|
|
41
|
+
rows: list[tuple[int, float, float, float, float]] = []
|
|
42
|
+
try:
|
|
43
|
+
for candle in candles:
|
|
44
|
+
timestamp = float(candle[0])
|
|
45
|
+
if not isfinite(timestamp):
|
|
46
|
+
return None
|
|
47
|
+
if timestamp < window_start or timestamp >= window_end:
|
|
48
|
+
continue
|
|
49
|
+
if timestamp != int(timestamp) or int(timestamp) % interval_ms:
|
|
50
|
+
return None
|
|
51
|
+
opening, high, low, close = map(float, candle[1:5])
|
|
52
|
+
volume = float(candle[5])
|
|
53
|
+
if (
|
|
54
|
+
not all(
|
|
55
|
+
isfinite(value) and value > 0
|
|
56
|
+
for value in (opening, high, low, close)
|
|
57
|
+
)
|
|
58
|
+
or not low <= min(opening, close) <= max(opening, close) <= high
|
|
59
|
+
or len(candle) < 7
|
|
60
|
+
or not isfinite(volume)
|
|
61
|
+
or volume <= 0
|
|
62
|
+
or not timestamp <= float(candle[6]) < timestamp + interval_ms
|
|
63
|
+
):
|
|
64
|
+
return None
|
|
65
|
+
rows.append((int(timestamp), opening, high, low, close))
|
|
66
|
+
except IndexError, TypeError, ValueError, OverflowError:
|
|
67
|
+
return None
|
|
68
|
+
rows.sort()
|
|
69
|
+
if (
|
|
70
|
+
len(rows) < 2 * fractal_wing + 1
|
|
71
|
+
or rows[-1][0] != window_end - interval_ms
|
|
72
|
+
or any(
|
|
73
|
+
later[0] - earlier[0] != interval_ms
|
|
74
|
+
for earlier, later in zip(rows, rows[1:])
|
|
75
|
+
)
|
|
76
|
+
):
|
|
77
|
+
return None
|
|
78
|
+
tick = 10.0**-price_precision
|
|
79
|
+
price_gaps = sum(
|
|
80
|
+
max(later[3] - earlier[2], earlier[3] - later[2], 0)
|
|
81
|
+
> max(2 * tick, earlier[4] * MIN_PROTECTION_PRICE_GAP_PCT / 100)
|
|
82
|
+
for earlier, later in zip(rows, rows[1:])
|
|
83
|
+
)
|
|
84
|
+
if price_gaps > max(1, MAX_PROTECTION_PRICE_GAP_RATIO * (len(rows) - 1)):
|
|
85
|
+
return None
|
|
86
|
+
return rows
|
|
87
|
+
|
|
88
|
+
|
|
27
89
|
def recent_swing_protection(
|
|
28
90
|
candles: Sequence[Sequence[float]],
|
|
29
91
|
*,
|
|
@@ -35,20 +97,22 @@ def recent_swing_protection(
|
|
|
35
97
|
buffer_pct: float = 0.1,
|
|
36
98
|
max_distance_pct: float = MAX_PROTECTION_DISTANCE_PCT,
|
|
37
99
|
min_distance_pct: float = 0.0,
|
|
38
|
-
max_candle_range_pct: float = MAX_PROTECTION_CANDLE_RANGE_PCT,
|
|
100
|
+
max_candle_range_pct: float | None = MAX_PROTECTION_CANDLE_RANGE_PCT,
|
|
39
101
|
lookback_ms: int = PROTECTION_LOOKBACK_MS,
|
|
40
102
|
not_before_ms: int = 0,
|
|
41
103
|
fractal_wing: int = 2,
|
|
42
104
|
) -> SwingProtection | None:
|
|
43
105
|
"""Evaluate only the latest confirmed swing in a bounded completed window.
|
|
44
106
|
|
|
45
|
-
Input rows use
|
|
107
|
+
Input rows use normalized millisecond OHLCV timestamps. Reject
|
|
46
108
|
incomplete, stale, gapped or invalid history. The swing and all bars used
|
|
47
109
|
to confirm it must be inside the lookback. Never search older swings when
|
|
48
110
|
the latest one is broken, too wide or on the wrong side of the reference.
|
|
49
111
|
Distances include the buffer and outward exchange-tick rounding.
|
|
50
112
|
|
|
51
|
-
|
|
113
|
+
A None candle-range limit permits a coarser fallback after finer history
|
|
114
|
+
fails validation; it never relaxes the buffered stop-distance limit.
|
|
115
|
+
Prefer refine_swing_protection for the full 15m -> 5m -> 1m decision.
|
|
52
116
|
Entry callers use the intended entry as reference; live trailing callers
|
|
53
117
|
use the current favorable price and separately enforce tightening only.
|
|
54
118
|
"""
|
|
@@ -66,52 +130,25 @@ def recent_swing_protection(
|
|
|
66
130
|
buffer_pct,
|
|
67
131
|
max_distance_pct,
|
|
68
132
|
min_distance_pct,
|
|
69
|
-
max_candle_range_pct,
|
|
70
133
|
)
|
|
71
134
|
)
|
|
72
135
|
or reference_price <= 0
|
|
73
136
|
or not 0 <= buffer_pct < 100
|
|
74
137
|
or not 0 <= min_distance_pct < max_distance_pct <= 3
|
|
75
|
-
or not 0 < max_candle_range_pct <= 3
|
|
138
|
+
or (max_candle_range_pct is not None and not 0 < max_candle_range_pct <= 3)
|
|
76
139
|
):
|
|
77
140
|
return None
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if timestamp < window_start or timestamp >= window_end:
|
|
88
|
-
continue
|
|
89
|
-
if timestamp != int(timestamp) or int(timestamp) % interval_ms:
|
|
90
|
-
return None
|
|
91
|
-
opening, high, low, close = map(float, candle[1:5])
|
|
92
|
-
if (
|
|
93
|
-
not all(
|
|
94
|
-
isfinite(value) and value > 0
|
|
95
|
-
for value in (opening, high, low, close)
|
|
96
|
-
)
|
|
97
|
-
or not low <= min(opening, close) <= max(opening, close) <= high
|
|
98
|
-
or len(candle) < 7
|
|
99
|
-
or not timestamp <= float(candle[6]) < timestamp + interval_ms
|
|
100
|
-
):
|
|
101
|
-
return None
|
|
102
|
-
rows.append((int(timestamp), opening, high, low, close))
|
|
103
|
-
except IndexError, TypeError, ValueError, OverflowError:
|
|
104
|
-
return None
|
|
105
|
-
rows.sort()
|
|
106
|
-
if (
|
|
107
|
-
len(rows) < 2 * fractal_wing + 1
|
|
108
|
-
or rows[-1][0] != window_end - interval_ms
|
|
109
|
-
or any(
|
|
110
|
-
later[0] - earlier[0] != interval_ms
|
|
111
|
-
for earlier, later in zip(rows, rows[1:])
|
|
112
|
-
)
|
|
113
|
-
):
|
|
141
|
+
rows = _completed_protection_window(
|
|
142
|
+
candles,
|
|
143
|
+
now_ms=now_ms,
|
|
144
|
+
interval_minutes=interval_minutes,
|
|
145
|
+
lookback_ms=lookback_ms,
|
|
146
|
+
fractal_wing=fractal_wing,
|
|
147
|
+
price_precision=price_precision,
|
|
148
|
+
)
|
|
149
|
+
if rows is None:
|
|
114
150
|
return None
|
|
151
|
+
interval_ms = interval_minutes * 60 * 1000
|
|
115
152
|
|
|
116
153
|
short = position == Position.short
|
|
117
154
|
value_column = 2 if short else 3
|
|
@@ -140,7 +177,9 @@ def recent_swing_protection(
|
|
|
140
177
|
):
|
|
141
178
|
return None
|
|
142
179
|
candle_range = (Decimal(str(high)) / Decimal(str(low)) - 1) * 100
|
|
143
|
-
if candle_range > Decimal(
|
|
180
|
+
if max_candle_range_pct is not None and candle_range > Decimal(
|
|
181
|
+
str(max_candle_range_pct)
|
|
182
|
+
):
|
|
144
183
|
return None
|
|
145
184
|
direction = Decimal(1 if short else -1)
|
|
146
185
|
stop = (
|
|
@@ -165,3 +204,90 @@ def recent_swing_protection(
|
|
|
165
204
|
distance_pct=float(distance),
|
|
166
205
|
candle_range_pct=float(candle_range),
|
|
167
206
|
)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
@dataclass(frozen=True)
|
|
210
|
+
class SwingProtectionSelection:
|
|
211
|
+
protection: SwingProtection | None = None
|
|
212
|
+
required_interval_minutes: int | None = None
|
|
213
|
+
used_coarser_fallback: bool = False
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def refine_swing_protection(
|
|
217
|
+
candles_by_interval: Mapping[int, Sequence[Sequence[float]]],
|
|
218
|
+
*,
|
|
219
|
+
position: Position,
|
|
220
|
+
reference_price: float,
|
|
221
|
+
now_ms: int,
|
|
222
|
+
price_precision: int,
|
|
223
|
+
buffer_pct: float = 0.1,
|
|
224
|
+
max_distance_pct: float = MAX_PROTECTION_DISTANCE_PCT,
|
|
225
|
+
min_distance_pct: float = 0.0,
|
|
226
|
+
lookback_ms: int = PROTECTION_LOOKBACK_MS,
|
|
227
|
+
not_before_ms: int = 0,
|
|
228
|
+
) -> SwingProtectionSelection:
|
|
229
|
+
"""Descend through wide candles, but back up when finer history is unusable.
|
|
230
|
+
|
|
231
|
+
Missing dictionary entries request the next timeframe from the caller;
|
|
232
|
+
an empty list represents a completed read with unavailable data. A gap,
|
|
233
|
+
duplicate or stale bar ends refinement and selects the nearest healthy
|
|
234
|
+
coarser swing that fits the hard distance cap. A wide coarser candle may
|
|
235
|
+
be used in this case; an over-distance or broken swing never qualifies.
|
|
236
|
+
"""
|
|
237
|
+
if (
|
|
238
|
+
position not in (Position.long, Position.short)
|
|
239
|
+
or price_precision < 0
|
|
240
|
+
or not 0 < lookback_ms <= PROTECTION_LOOKBACK_MS
|
|
241
|
+
or not all(
|
|
242
|
+
isfinite(value)
|
|
243
|
+
for value in (
|
|
244
|
+
reference_price,
|
|
245
|
+
buffer_pct,
|
|
246
|
+
max_distance_pct,
|
|
247
|
+
min_distance_pct,
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
or reference_price <= 0
|
|
251
|
+
or not 0 <= buffer_pct < 100
|
|
252
|
+
or not 0 <= min_distance_pct < max_distance_pct <= 3
|
|
253
|
+
):
|
|
254
|
+
return SwingProtectionSelection()
|
|
255
|
+
fallback = None
|
|
256
|
+
for interval in PROTECTION_INTERVALS_MINUTES:
|
|
257
|
+
if interval not in candles_by_interval:
|
|
258
|
+
return SwingProtectionSelection(required_interval_minutes=interval)
|
|
259
|
+
candles = candles_by_interval[interval]
|
|
260
|
+
if (
|
|
261
|
+
_completed_protection_window(
|
|
262
|
+
candles,
|
|
263
|
+
now_ms=now_ms,
|
|
264
|
+
interval_minutes=interval,
|
|
265
|
+
lookback_ms=lookback_ms,
|
|
266
|
+
fractal_wing=2,
|
|
267
|
+
price_precision=price_precision,
|
|
268
|
+
)
|
|
269
|
+
is None
|
|
270
|
+
):
|
|
271
|
+
return SwingProtectionSelection(
|
|
272
|
+
protection=fallback,
|
|
273
|
+
used_coarser_fallback=fallback is not None,
|
|
274
|
+
)
|
|
275
|
+
candidate = recent_swing_protection(
|
|
276
|
+
candles,
|
|
277
|
+
position=position,
|
|
278
|
+
reference_price=reference_price,
|
|
279
|
+
now_ms=now_ms,
|
|
280
|
+
interval_minutes=interval,
|
|
281
|
+
price_precision=price_precision,
|
|
282
|
+
buffer_pct=buffer_pct,
|
|
283
|
+
max_distance_pct=max_distance_pct,
|
|
284
|
+
min_distance_pct=min_distance_pct,
|
|
285
|
+
lookback_ms=lookback_ms,
|
|
286
|
+
not_before_ms=not_before_ms,
|
|
287
|
+
max_candle_range_pct=None,
|
|
288
|
+
)
|
|
289
|
+
if candidate is not None:
|
|
290
|
+
if candidate.candle_range_pct <= MAX_PROTECTION_CANDLE_RANGE_PCT:
|
|
291
|
+
return SwingProtectionSelection(protection=candidate)
|
|
292
|
+
fallback = candidate
|
|
293
|
+
return SwingProtectionSelection()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pybinbot
|
|
3
|
-
Version: 2.1.
|
|
3
|
+
Version: 2.1.12
|
|
4
4
|
Summary: Utility functions for the binbot project.
|
|
5
5
|
Author-email: Carlos Wu <carkodw@gmail.com>
|
|
6
6
|
Requires-Python: >=3.15
|
|
@@ -172,3 +172,18 @@ The selector rejects swing candles wider than 3% and stop distances over 2.5%
|
|
|
172
172
|
after the buffer and outward tick rounding. It never clips a distant swing or
|
|
173
173
|
searches older swings to force a result. Entry callers skip when all timeframes
|
|
174
174
|
fail; live callers retain existing protection and only apply tighter stops.
|
|
175
|
+
|
|
176
|
+
`refine_swing_protection` manages the whole timeframe decision. If 1m history
|
|
177
|
+
is gapped or unreliable, it returns the nearest trustworthy coarser swing.
|
|
178
|
+
Missing timestamps, duplicates, stale bars, zero-volume bars, or frequent price
|
|
179
|
+
gaps make a source unreliable. Price gaps are non-overlapping candle ranges
|
|
180
|
+
separated by more than 0.1% and two price-precision ticks; more than 20% of
|
|
181
|
+
transitions, with at least two gaps, rejects the timeframe.
|
|
182
|
+
|
|
183
|
+
The 3% candle-width threshold prompts refinement. It can be relaxed for a
|
|
184
|
+
coarser fallback after finer data fails, while the buffered stop-distance cap,
|
|
185
|
+
three-hour swing age, confirmation and unbroken-swing requirements still apply.
|
|
186
|
+
If no coarser swing fits, the result has no placement. A missing timeframe in
|
|
187
|
+
the input mapping requests that interval; an empty list means the read finished
|
|
188
|
+
without usable data. Live callers can therefore keep exchange reads off the
|
|
189
|
+
protective exit path while this decision remains shared with entry strategies.
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from pybinbot import Position
|
|
6
|
+
from pybinbot.strategy.swing_protection import (
|
|
7
|
+
recent_swing_protection,
|
|
8
|
+
refine_swing_protection,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
NOW_MS = 1_800_000_000_000
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def candles(interval=15, *, high=102.0, count=12):
|
|
16
|
+
interval_ms = interval * 60_000
|
|
17
|
+
highs = [100.0] * (count - 5) + [100, 101, high, 101, 100]
|
|
18
|
+
rows = []
|
|
19
|
+
previous_close = highs[0] - 0.1
|
|
20
|
+
for index, value in enumerate(highs):
|
|
21
|
+
rows.append(
|
|
22
|
+
[
|
|
23
|
+
NOW_MS - (count - index) * interval_ms,
|
|
24
|
+
previous_close,
|
|
25
|
+
max(value, previous_close),
|
|
26
|
+
min(value - 0.2, previous_close),
|
|
27
|
+
value - 0.1,
|
|
28
|
+
10,
|
|
29
|
+
NOW_MS - (count - index - 1) * interval_ms - 1,
|
|
30
|
+
]
|
|
31
|
+
)
|
|
32
|
+
previous_close = value - 0.1
|
|
33
|
+
return rows
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def resolve(rows, *, interval=15, position=Position.short, **kwargs):
|
|
37
|
+
return recent_swing_protection(
|
|
38
|
+
rows,
|
|
39
|
+
position=position,
|
|
40
|
+
reference_price=100,
|
|
41
|
+
now_ms=NOW_MS,
|
|
42
|
+
interval_minutes=interval,
|
|
43
|
+
price_precision=4,
|
|
44
|
+
**kwargs,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@pytest.mark.parametrize("interval", [15, 5, 1])
|
|
49
|
+
@pytest.mark.parametrize("position", [Position.long, Position.short])
|
|
50
|
+
def test_recent_confirmed_swing_protects_each_direction_at_each_timeframe(
|
|
51
|
+
interval, position
|
|
52
|
+
):
|
|
53
|
+
rows = candles(interval)
|
|
54
|
+
if position == Position.long:
|
|
55
|
+
rows = [
|
|
56
|
+
[t, 200 - o, 200 - low, 200 - high, 200 - c, v, end]
|
|
57
|
+
for t, o, high, low, c, v, end in rows
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
result = resolve(rows, interval=interval, position=position)
|
|
61
|
+
|
|
62
|
+
assert result is not None
|
|
63
|
+
assert result.interval_minutes == interval
|
|
64
|
+
assert result.swing_open_time_ms == NOW_MS - 3 * interval * 60_000
|
|
65
|
+
assert result.confirmation_time_ms == NOW_MS - 1
|
|
66
|
+
assert 2 < result.distance_pct < 2.5
|
|
67
|
+
assert (
|
|
68
|
+
result.stop_price > 100
|
|
69
|
+
if position == Position.short
|
|
70
|
+
else result.stop_price < 100
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_six_percent_candle_requires_a_smaller_timeframe_even_with_a_nearby_peak():
|
|
75
|
+
rows = candles()
|
|
76
|
+
rows[-3][3] = 95
|
|
77
|
+
|
|
78
|
+
assert resolve(rows) is None
|
|
79
|
+
assert resolve(candles(5), interval=5) is not None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_wide_candle_fallback_cannot_hide_a_stop_more_than_two_and_a_half_percent_away():
|
|
83
|
+
assert resolve(candles(high=106.35), max_candle_range_pct=None) is None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_stop_distance_includes_buffer_and_outward_tick_rounding():
|
|
87
|
+
assert resolve(candles(), buffer_pct=0.5) is None
|
|
88
|
+
result = resolve(candles(high=101.99991))
|
|
89
|
+
assert result is not None
|
|
90
|
+
assert result.stop_price == 102.102
|
|
91
|
+
assert result.distance_pct == pytest.approx(2.102)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@pytest.mark.parametrize("high,qualifies", [(102.5, True), (102.5001, False)])
|
|
95
|
+
def test_stop_distance_cap_is_inclusive_without_clipping(high, qualifies):
|
|
96
|
+
assert (resolve(candles(high=high), buffer_pct=0) is not None) is qualifies
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_unclosed_candle_cannot_confirm_a_swing():
|
|
100
|
+
rows = candles()
|
|
101
|
+
rows[-1][0] += 15 * 60_000
|
|
102
|
+
rows[-1][6] += 15 * 60_000
|
|
103
|
+
assert resolve(rows) is None
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@pytest.mark.parametrize("problem", ["stale", "gap", "duplicate", "nan", "ohlc"])
|
|
107
|
+
def test_invalid_or_incomplete_history_cannot_place_protection(problem):
|
|
108
|
+
rows = candles()
|
|
109
|
+
if problem == "stale":
|
|
110
|
+
rows.pop()
|
|
111
|
+
elif problem == "gap":
|
|
112
|
+
rows.pop(-4)
|
|
113
|
+
elif problem == "duplicate":
|
|
114
|
+
rows.append(deepcopy(rows[-1]))
|
|
115
|
+
elif problem == "nan":
|
|
116
|
+
rows[-4][2] = float("nan")
|
|
117
|
+
else:
|
|
118
|
+
rows[-4][3] = rows[-4][2] + 1
|
|
119
|
+
assert resolve(rows) is None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def test_cannot_resurrect_a_swing_older_than_three_hours():
|
|
123
|
+
rows = candles(count=24)
|
|
124
|
+
for row in rows:
|
|
125
|
+
row[0] -= 3 * 60 * 60_000
|
|
126
|
+
row[6] -= 3 * 60 * 60_000
|
|
127
|
+
rows.extend(
|
|
128
|
+
[
|
|
129
|
+
[
|
|
130
|
+
NOW_MS - (12 - i) * 900_000,
|
|
131
|
+
100,
|
|
132
|
+
100.1,
|
|
133
|
+
99.9,
|
|
134
|
+
100,
|
|
135
|
+
10,
|
|
136
|
+
NOW_MS - (11 - i) * 900_000 - 1,
|
|
137
|
+
]
|
|
138
|
+
for i in range(12)
|
|
139
|
+
]
|
|
140
|
+
)
|
|
141
|
+
assert resolve(rows) is None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def test_swing_window_can_be_restricted_to_one_hour_or_the_current_setup():
|
|
145
|
+
rows = candles()
|
|
146
|
+
assert resolve(rows, lookback_ms=60 * 60_000) is None # Only four 15m bars.
|
|
147
|
+
assert resolve(rows, not_before_ms=NOW_MS - 30 * 60_000) is None
|
|
148
|
+
assert resolve(rows, lookback_ms=4 * 60 * 60_000) is None
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_latest_unconfirmed_break_invalidates_the_old_peak():
|
|
152
|
+
rows = candles(count=14)
|
|
153
|
+
rows[-1][2] = 103
|
|
154
|
+
assert resolve(rows) is None
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_latest_wide_swing_does_not_fall_back_to_an_arbitrary_older_peak():
|
|
158
|
+
rows = candles(high=106.35)
|
|
159
|
+
rows[3][2] = 101
|
|
160
|
+
assert resolve(rows) is None
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def wide_candles(interval):
|
|
164
|
+
rows = candles(interval)
|
|
165
|
+
rows[-3][3] = 95.0
|
|
166
|
+
return rows
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def refine(history, *, position=Position.short):
|
|
170
|
+
if position == Position.long:
|
|
171
|
+
history = {
|
|
172
|
+
interval: [
|
|
173
|
+
[t, 200 - o, 200 - low, 200 - high, 200 - c, v, end]
|
|
174
|
+
for t, o, high, low, c, v, end in rows
|
|
175
|
+
]
|
|
176
|
+
for interval, rows in history.items()
|
|
177
|
+
}
|
|
178
|
+
return refine_swing_protection(
|
|
179
|
+
history,
|
|
180
|
+
position=position,
|
|
181
|
+
reference_price=100,
|
|
182
|
+
now_ms=NOW_MS,
|
|
183
|
+
price_precision=4,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def test_wide_candles_request_the_next_finer_timeframe():
|
|
188
|
+
decision = refine({15: wide_candles(15), 5: wide_candles(5)})
|
|
189
|
+
assert decision.protection is None
|
|
190
|
+
assert decision.required_interval_minutes == 1
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def test_continuous_one_minute_data_is_used_when_coarser_candles_are_wide():
|
|
194
|
+
decision = refine({15: wide_candles(15), 5: wide_candles(5), 1: candles(1)})
|
|
195
|
+
assert decision.protection is not None
|
|
196
|
+
assert decision.protection.interval_minutes == 1
|
|
197
|
+
assert not decision.used_coarser_fallback
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
@pytest.mark.parametrize(
|
|
201
|
+
"problem", ["missing", "empty", "stale", "duplicate", "no_trades", "price_gaps"]
|
|
202
|
+
)
|
|
203
|
+
@pytest.mark.parametrize("position", [Position.short, Position.long])
|
|
204
|
+
def test_untrustworthy_one_minute_history_falls_back_to_safe_five_minute_structure(
|
|
205
|
+
problem,
|
|
206
|
+
position,
|
|
207
|
+
):
|
|
208
|
+
fine = candles(1)
|
|
209
|
+
if problem == "missing":
|
|
210
|
+
fine.pop(-4)
|
|
211
|
+
elif problem == "empty":
|
|
212
|
+
fine = []
|
|
213
|
+
elif problem == "stale":
|
|
214
|
+
fine.pop()
|
|
215
|
+
elif problem == "duplicate":
|
|
216
|
+
fine.append(deepcopy(fine[-1]))
|
|
217
|
+
elif problem == "no_trades":
|
|
218
|
+
fine[-4][5] = 0
|
|
219
|
+
else:
|
|
220
|
+
for index, row in enumerate(fine):
|
|
221
|
+
offset = 2 if index % 2 else 0
|
|
222
|
+
row[1:5] = [value + offset for value in row[1:5]]
|
|
223
|
+
decision = refine(
|
|
224
|
+
{15: wide_candles(15), 5: wide_candles(5), 1: fine}, position=position
|
|
225
|
+
)
|
|
226
|
+
assert decision.protection is not None
|
|
227
|
+
assert decision.protection.interval_minutes == 5
|
|
228
|
+
assert decision.protection.candle_range_pct > 3
|
|
229
|
+
assert decision.protection.distance_pct < 2.5
|
|
230
|
+
assert decision.used_coarser_fallback
|
|
231
|
+
assert decision.required_interval_minutes is None
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_untrustworthy_five_minute_history_falls_back_to_fifteen_minutes():
|
|
235
|
+
fine = candles(5)
|
|
236
|
+
fine.pop(-4)
|
|
237
|
+
decision = refine({15: wide_candles(15), 5: fine})
|
|
238
|
+
assert decision.protection is not None
|
|
239
|
+
assert decision.protection.interval_minutes == 15
|
|
240
|
+
assert decision.used_coarser_fallback
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def test_coarser_fallback_cannot_bypass_the_buffered_stop_distance_cap():
|
|
244
|
+
fine = candles(1)
|
|
245
|
+
fine.pop(-4)
|
|
246
|
+
decision = refine(
|
|
247
|
+
{15: candles(15, high=106.35), 5: candles(5, high=106.35), 1: fine}
|
|
248
|
+
)
|
|
249
|
+
assert decision.protection is None
|
|
250
|
+
assert decision.required_interval_minutes is None
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def test_broken_coarser_swing_cannot_be_used_as_a_gap_fallback():
|
|
254
|
+
coarse = wide_candles(5)
|
|
255
|
+
coarse[-1][2] = 103
|
|
256
|
+
fine = candles(1)
|
|
257
|
+
fine.pop(-4)
|
|
258
|
+
# No valid 15m stop; the latest 5m peak was breached by an unconfirmed bar.
|
|
259
|
+
decision = refine({15: candles(15, high=106.35), 5: coarse, 1: fine})
|
|
260
|
+
assert decision.protection is None
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def test_missing_coarse_history_cannot_be_replaced_by_a_trustworthy_finer_frame():
|
|
264
|
+
decision = refine({15: [], 5: candles(5), 1: candles(1)})
|
|
265
|
+
assert decision.protection is None
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def test_one_small_price_gap_does_not_make_an_otherwise_continuous_history_unusable():
|
|
269
|
+
rows = candles(1)
|
|
270
|
+
rows[1][1:5] = [value + 0.31 for value in rows[1][1:5]]
|
|
271
|
+
assert resolve(rows, interval=1) is not None
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
from copy import deepcopy
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
|
|
5
|
-
from pybinbot import Position
|
|
6
|
-
from pybinbot.strategy.swing_protection import recent_swing_protection
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
NOW_MS = 1_800_000_000_000
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def candles(interval=15, *, high=102.0, count=12):
|
|
13
|
-
interval_ms = interval * 60_000
|
|
14
|
-
highs = [100.0] * (count - 5) + [100, 101, high, 101, 100]
|
|
15
|
-
return [
|
|
16
|
-
[
|
|
17
|
-
NOW_MS - (count - index) * interval_ms,
|
|
18
|
-
value - 0.1,
|
|
19
|
-
value,
|
|
20
|
-
value - 0.2,
|
|
21
|
-
value - 0.1,
|
|
22
|
-
10,
|
|
23
|
-
NOW_MS - (count - index - 1) * interval_ms - 1,
|
|
24
|
-
]
|
|
25
|
-
for index, value in enumerate(highs)
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def resolve(rows, *, interval=15, position=Position.short, **kwargs):
|
|
30
|
-
return recent_swing_protection(
|
|
31
|
-
rows,
|
|
32
|
-
position=position,
|
|
33
|
-
reference_price=100,
|
|
34
|
-
now_ms=NOW_MS,
|
|
35
|
-
interval_minutes=interval,
|
|
36
|
-
price_precision=4,
|
|
37
|
-
**kwargs,
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
@pytest.mark.parametrize("interval", [15, 5, 1])
|
|
42
|
-
@pytest.mark.parametrize("position", [Position.long, Position.short])
|
|
43
|
-
def test_recent_confirmed_swing_protects_each_direction_at_each_timeframe(
|
|
44
|
-
interval, position
|
|
45
|
-
):
|
|
46
|
-
rows = candles(interval)
|
|
47
|
-
if position == Position.long:
|
|
48
|
-
rows = [
|
|
49
|
-
[t, 200 - o, 200 - low, 200 - high, 200 - c, v, end]
|
|
50
|
-
for t, o, high, low, c, v, end in rows
|
|
51
|
-
]
|
|
52
|
-
|
|
53
|
-
result = resolve(rows, interval=interval, position=position)
|
|
54
|
-
|
|
55
|
-
assert result is not None
|
|
56
|
-
assert result.interval_minutes == interval
|
|
57
|
-
assert result.swing_open_time_ms == NOW_MS - 3 * interval * 60_000
|
|
58
|
-
assert result.confirmation_time_ms == NOW_MS - 1
|
|
59
|
-
assert 2 < result.distance_pct < 2.5
|
|
60
|
-
assert (
|
|
61
|
-
result.stop_price > 100
|
|
62
|
-
if position == Position.short
|
|
63
|
-
else result.stop_price < 100
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
def test_six_percent_candle_requires_a_smaller_timeframe_even_with_a_nearby_peak():
|
|
68
|
-
rows = candles()
|
|
69
|
-
rows[-3][3] = 95
|
|
70
|
-
|
|
71
|
-
assert resolve(rows) is None
|
|
72
|
-
assert resolve(candles(5), interval=5) is not None
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
def test_tiny_candles_cannot_hide_a_stop_more_than_two_and_a_half_percent_away():
|
|
76
|
-
assert resolve(candles(high=106.35)) is None
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
def test_stop_distance_includes_buffer_and_outward_tick_rounding():
|
|
80
|
-
assert resolve(candles(), buffer_pct=0.5) is None
|
|
81
|
-
result = resolve(candles(high=101.99991))
|
|
82
|
-
assert result is not None
|
|
83
|
-
assert result.stop_price == 102.102
|
|
84
|
-
assert result.distance_pct == pytest.approx(2.102)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
@pytest.mark.parametrize("high,qualifies", [(102.5, True), (102.5001, False)])
|
|
88
|
-
def test_stop_distance_cap_is_inclusive_without_clipping(high, qualifies):
|
|
89
|
-
assert (resolve(candles(high=high), buffer_pct=0) is not None) is qualifies
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def test_unclosed_candle_cannot_confirm_a_swing():
|
|
93
|
-
rows = candles()
|
|
94
|
-
rows[-1][0] += 15 * 60_000
|
|
95
|
-
rows[-1][6] += 15 * 60_000
|
|
96
|
-
assert resolve(rows) is None
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
@pytest.mark.parametrize("problem", ["stale", "gap", "duplicate", "nan", "ohlc"])
|
|
100
|
-
def test_invalid_or_incomplete_history_cannot_place_protection(problem):
|
|
101
|
-
rows = candles()
|
|
102
|
-
if problem == "stale":
|
|
103
|
-
rows.pop()
|
|
104
|
-
elif problem == "gap":
|
|
105
|
-
rows.pop(-4)
|
|
106
|
-
elif problem == "duplicate":
|
|
107
|
-
rows.append(deepcopy(rows[-1]))
|
|
108
|
-
elif problem == "nan":
|
|
109
|
-
rows[-4][2] = float("nan")
|
|
110
|
-
else:
|
|
111
|
-
rows[-4][3] = rows[-4][2] + 1
|
|
112
|
-
assert resolve(rows) is None
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def test_cannot_resurrect_a_swing_older_than_three_hours():
|
|
116
|
-
rows = candles(count=24)
|
|
117
|
-
for row in rows:
|
|
118
|
-
row[0] -= 3 * 60 * 60_000
|
|
119
|
-
row[6] -= 3 * 60 * 60_000
|
|
120
|
-
rows.extend(
|
|
121
|
-
[
|
|
122
|
-
[
|
|
123
|
-
NOW_MS - (12 - i) * 900_000,
|
|
124
|
-
100,
|
|
125
|
-
100.1,
|
|
126
|
-
99.9,
|
|
127
|
-
100,
|
|
128
|
-
10,
|
|
129
|
-
NOW_MS - (11 - i) * 900_000 - 1,
|
|
130
|
-
]
|
|
131
|
-
for i in range(12)
|
|
132
|
-
]
|
|
133
|
-
)
|
|
134
|
-
assert resolve(rows) is None
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def test_swing_window_can_be_restricted_to_one_hour_or_the_current_setup():
|
|
138
|
-
rows = candles()
|
|
139
|
-
assert resolve(rows, lookback_ms=60 * 60_000) is None # Only four 15m bars.
|
|
140
|
-
assert resolve(rows, not_before_ms=NOW_MS - 30 * 60_000) is None
|
|
141
|
-
assert resolve(rows, lookback_ms=4 * 60 * 60_000) is None
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
def test_latest_unconfirmed_break_invalidates_the_old_peak():
|
|
145
|
-
rows = candles(count=14)
|
|
146
|
-
rows[-1][2] = 103
|
|
147
|
-
assert resolve(rows) is None
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
def test_latest_wide_swing_does_not_fall_back_to_an_arbitrary_older_peak():
|
|
151
|
-
rows = candles(high=106.35)
|
|
152
|
-
rows[3][2] = 101
|
|
153
|
-
assert resolve(rows) is None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|