-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
404 lines (304 loc) · 11.7 KB
/
Copy pathutils.py
File metadata and controls
404 lines (304 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Utility functions for Bank Nifty Scalping Bot.
Includes expiry calculation, throttling, logging, and helper functions.
"""
import asyncio
import functools
import logging
import os
import time
from datetime import datetime, timedelta
from typing import Any, Callable, Optional
from config import LOG_FILE, LOG_LEVEL
# =============================================================================
# LOGGING SETUP
# =============================================================================
class SafeStreamHandler(logging.StreamHandler):
"""Stream handler that safely encodes Unicode for Windows console."""
def emit(self, record):
try:
msg = self.format(record)
# Replace Unicode characters that Windows console can't handle
safe_msg = msg.encode("ascii", "replace").decode("ascii")
stream = self.stream
stream.write(safe_msg + self.terminator)
self.flush()
except Exception:
self.handleError(record)
def setup_logging() -> logging.Logger:
"""Configure and return the main logger."""
# Create logs directory if it doesn't exist
log_dir = os.path.dirname(LOG_FILE)
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir)
# Configure logger
logger = logging.getLogger("scalping_bot")
logger.setLevel(getattr(logging, LOG_LEVEL))
# File handler (with UTF-8 encoding for full emoji support in logs)
file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8")
file_handler.setLevel(logging.DEBUG)
# Console handler (safe for Windows)
console_handler = SafeStreamHandler()
console_handler.setLevel(getattr(logging, LOG_LEVEL))
# Formatter
formatter = logging.Formatter(
"%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add handlers
if not logger.handlers:
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
# Global logger instance
logger = setup_logging()
# =============================================================================
# EXPIRY DATE CALCULATION
# =============================================================================
def get_next_weekly_expiry(reference_date: Optional[datetime] = None) -> datetime:
"""
Calculate the next weekly expiry date (Thursday) for Bank Nifty.
Args:
reference_date: Date to calculate from (default: today)
Returns:
datetime: Next Thursday expiry date
"""
if reference_date is None:
reference_date = datetime.now()
# Thursday is weekday 3 (Monday=0, Thursday=3)
days_until_thursday = (3 - reference_date.weekday()) % 7
# If today is Thursday after market hours, get next week's expiry
if days_until_thursday == 0 and reference_date.hour >= 15:
days_until_thursday = 7
expiry_date = reference_date + timedelta(days=days_until_thursday)
# Return date at market close time
return expiry_date.replace(hour=15, minute=30, second=0, microsecond=0)
def get_expiry_string(expiry_date: Optional[datetime] = None) -> str:
"""
Get expiry date in API format (YYYY-MM-DD).
Args:
expiry_date: Expiry datetime (default: next weekly expiry)
Returns:
str: Formatted expiry string
"""
if expiry_date is None:
expiry_date = get_next_weekly_expiry()
return expiry_date.strftime("%Y-%m-%d")
def get_monthly_expiry(reference_date: Optional[datetime] = None) -> datetime:
"""
Calculate the monthly expiry (last Thursday of the month).
Args:
reference_date: Date to calculate from (default: today)
Returns:
datetime: Monthly expiry date
"""
if reference_date is None:
reference_date = datetime.now()
# Get last day of current month
if reference_date.month == 12:
next_month = reference_date.replace(
year=reference_date.year + 1, month=1, day=1
)
else:
next_month = reference_date.replace(month=reference_date.month + 1, day=1)
last_day = next_month - timedelta(days=1)
# Find last Thursday
days_since_thursday = (last_day.weekday() - 3) % 7
last_thursday = last_day - timedelta(days=days_since_thursday)
return last_thursday.replace(hour=15, minute=30, second=0, microsecond=0)
# =============================================================================
# THROTTLING & RATE LIMITING
# =============================================================================
class Throttle:
"""
Rate limiter to prevent exceeding API limits.
Thread-safe implementation using asyncio locks.
"""
def __init__(self, max_calls: int, period_seconds: float):
"""
Initialize throttle.
Args:
max_calls: Maximum number of calls allowed
period_seconds: Time period for the limit
"""
self.max_calls = max_calls
self.period = period_seconds
self.calls: list[float] = []
self._lock = asyncio.Lock()
async def acquire(self) -> bool:
"""
Acquire permission to make a call.
Blocks if rate limit would be exceeded.
Returns:
bool: True if call is allowed
"""
async with self._lock:
now = time.time()
# Remove calls outside the window
self.calls = [t for t in self.calls if now - t < self.period]
if len(self.calls) >= self.max_calls:
# Calculate wait time
oldest_call = min(self.calls)
wait_time = self.period - (now - oldest_call)
if wait_time > 0:
logger.debug(f"Throttle: waiting {wait_time:.3f}s")
await asyncio.sleep(wait_time)
self.calls.append(time.time())
return True
def reset(self):
"""Reset the throttle."""
self.calls = []
class SLUpdateThrottle:
"""
Special throttle for Stop Loss updates to prevent excessive modifications.
Only allows update if price moved enough AND enough time has passed.
"""
def __init__(self, min_points: float, min_interval: float):
"""
Args:
min_points: Minimum price movement required
min_interval: Minimum seconds between updates
"""
self.min_points = min_points
self.min_interval = min_interval
self.last_update_time: float = 0
self.last_update_price: float = 0
def should_update(self, current_price: float, new_sl_price: float) -> bool:
"""
Check if SL update should be allowed.
Args:
current_price: Current market price
new_sl_price: Proposed new stop loss price
Returns:
bool: True if update should proceed
"""
now = time.time()
# Check time interval
if now - self.last_update_time < self.min_interval:
return False
# Check price movement
price_diff = abs(new_sl_price - self.last_update_price)
if self.last_update_price > 0 and price_diff < self.min_points:
return False
return True
def mark_updated(self, sl_price: float):
"""Mark that an update was made."""
self.last_update_time = time.time()
self.last_update_price = sl_price
# =============================================================================
# RETRY DECORATOR
# =============================================================================
def async_retry(max_retries: int = 3, delay: float = 1.0, backoff: float = 2.0):
"""
Decorator for async functions with exponential backoff retry.
Args:
max_retries: Maximum number of retry attempts
delay: Initial delay between retries in seconds
backoff: Multiplier for delay after each retry
"""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> Any:
current_delay = delay
last_exception = None
for attempt in range(max_retries + 1):
try:
return await func(*args, **kwargs)
except Exception as e:
last_exception = e
if attempt < max_retries:
logger.warning(
f"{func.__name__} attempt {attempt + 1} failed: {e}. "
f"Retrying in {current_delay:.1f}s..."
)
await asyncio.sleep(current_delay)
current_delay *= backoff
else:
logger.error(
f"{func.__name__} failed after {max_retries + 1} attempts: {e}"
)
raise last_exception
return wrapper
return decorator
# =============================================================================
# MARKET HOURS UTILITIES
# =============================================================================
def is_market_hours(check_time: Optional[datetime] = None) -> bool:
"""
Check if given time is within market hours.
Args:
check_time: Time to check (default: now)
Returns:
bool: True if within market hours
"""
if check_time is None:
check_time = datetime.now()
# Check if weekday (Monday=0, Friday=4)
if check_time.weekday() > 4:
return False
# Import here to avoid circular import
from config import (
MARKET_CLOSE_HOUR,
MARKET_CLOSE_MINUTE,
MARKET_OPEN_HOUR,
MARKET_OPEN_MINUTE,
)
market_open = check_time.replace(
hour=MARKET_OPEN_HOUR, minute=MARKET_OPEN_MINUTE, second=0, microsecond=0
)
market_close = check_time.replace(
hour=MARKET_CLOSE_HOUR, minute=MARKET_CLOSE_MINUTE, second=0, microsecond=0
)
return market_open <= check_time <= market_close
def time_to_market_open() -> Optional[timedelta]:
"""
Calculate time remaining until market opens.
Returns:
timedelta or None if market is open
"""
now = datetime.now()
if is_market_hours(now):
return None
from config import MARKET_OPEN_HOUR, MARKET_OPEN_MINUTE
# Calculate next market open
next_open = now.replace(
hour=MARKET_OPEN_HOUR, minute=MARKET_OPEN_MINUTE, second=0, microsecond=0
)
# If past today's open, move to next trading day
if now >= next_open:
next_open += timedelta(days=1)
# Skip weekends
while next_open.weekday() > 4:
next_open += timedelta(days=1)
return next_open - now
# =============================================================================
# ATM STRIKE CALCULATION
# =============================================================================
def calculate_atm_strike(spot_price: float, strike_interval: int = 100) -> int:
"""
Calculate At-The-Money strike price.
Args:
spot_price: Current spot price of the index
strike_interval: Strike price interval (100 for Bank Nifty)
Returns:
int: ATM strike price
"""
return round(spot_price / strike_interval) * strike_interval
def get_strike_range(
atm_strike: int, num_strikes: int = 5, interval: int = 100
) -> list[int]:
"""
Get a range of strikes around ATM.
Args:
atm_strike: ATM strike price
num_strikes: Number of strikes on each side
interval: Strike interval
Returns:
list: List of strike prices
"""
strikes = []
for i in range(-num_strikes, num_strikes + 1):
strikes.append(atm_strike + (i * interval))
return strikes