siglab-py 0.3.3__py3-none-any.whl → 0.3.5__py3-none-any.whl

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.

Potentially problematic release.


This version of siglab-py might be problematic. Click here for more details.

@@ -32,6 +32,25 @@ def estimate_fib_retracement(
32
32
 
33
33
  return retracement_price
34
34
 
35
+ def calculate_slope(
36
+ pd_data : pd.DataFrame,
37
+ src_col_name : str,
38
+ slope_col_name : str,
39
+ sliding_window_how_many_candles : int
40
+ ):
41
+ import statsmodels.api as sm # in-compatible with pypy
42
+
43
+ X = sm.add_constant(range(len(pd_data[src_col_name])))
44
+ rolling_slope = pd_data[src_col_name].rolling(window=sliding_window_how_many_candles).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
45
+ pd_data[slope_col_name] = rolling_slope
46
+ max_abs_slope = pd_data[slope_col_name].abs().rolling(window=sliding_window_how_many_candles).max()
47
+ pd_data[f"normalized_{slope_col_name}"] = pd_data[slope_col_name] / max_abs_slope
48
+ normalized_slope_rolling = pd_data[f"normalized_{slope_col_name}"].rolling(window=sliding_window_how_many_candles)
49
+ pd_data[f"normalized_{slope_col_name}_min"] = normalized_slope_rolling.min()
50
+ pd_data[f"normalized_{slope_col_name}_max"] = normalized_slope_rolling.max()
51
+ pd_data[f"normalized_{slope_col_name}_idmin"] = normalized_slope_rolling.apply(lambda x : x.idxmin())
52
+ pd_data[f"normalized_{slope_col_name}_idmax"] = normalized_slope_rolling.apply(lambda x : x.idxmax())
53
+
35
54
  '''
36
55
  compute_candles_stats will calculate typical/basic technical indicators using in many trading strategies:
37
56
  a. Basic SMA/EMAs (And slopes)
@@ -365,40 +384,54 @@ def compute_candles_stats(
365
384
  pd_candles['macd_minus_signal'] = pd_candles['macd'] - pd_candles['signal']
366
385
 
367
386
  if not pypy_compat:
368
- import statsmodels.api as sm # in-compatible with pypy
369
-
370
- # Slopes
371
- X = sm.add_constant(range(len(pd_candles['close'])))
372
- rolling_slope = pd_candles['close'].rolling(window=int(sliding_window_how_many_candles/slow_fast_interval_ratio)).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
373
- pd_candles['close_short_slope'] = rolling_slope
374
- max_abs_slope = pd_candles['close_short_slope'].abs().rolling(window=int(sliding_window_how_many_candles/slow_fast_interval_ratio)).max()
375
- pd_candles['normalized_close_short_slope'] = pd_candles['close_short_slope'] / (2* max_abs_slope)
376
-
377
- X = sm.add_constant(range(len(pd_candles['close'])))
378
- rolling_slope = pd_candles['close'].rolling(window=sliding_window_how_many_candles).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
379
- pd_candles['close_long_slope'] = rolling_slope
380
- max_abs_slope = pd_candles['close_long_slope'].abs().rolling(window=sliding_window_how_many_candles).max()
381
- pd_candles['normalized_close_short_slope'] = pd_candles['close_long_slope'] / (2 * max_abs_slope)
387
+ calculate_slope(
388
+ pd_data=pd_candles,
389
+ src_col_name='close',
390
+ slope_col_name='close_short_slope',
391
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles/slow_fast_interval_ratio)
392
+ )
393
+
394
+ calculate_slope(
395
+ pd_data=pd_candles,
396
+ src_col_name='close',
397
+ slope_col_name='close_long_slope',
398
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles)
399
+ )
382
400
 
383
- X = sm.add_constant(range(len(pd_candles['ema_short_periods'])))
384
- rolling_slope = pd_candles['ema_short_periods'].rolling(window=int(sliding_window_how_many_candles/slow_fast_interval_ratio)).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
385
- pd_candles['ema_short_slope'] = rolling_slope
401
+ calculate_slope(
402
+ pd_data=pd_candles,
403
+ src_col_name='ema_short_periods',
404
+ slope_col_name='ema_short_slope',
405
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles/slow_fast_interval_ratio)
406
+ )
386
407
 
387
- X = sm.add_constant(range(len(pd_candles['ema_long_periods'])))
388
- rolling_slope = pd_candles['ema_long_periods'].rolling(window=sliding_window_how_many_candles).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
389
- pd_candles['ema_long_slope'] = rolling_slope
408
+ calculate_slope(
409
+ pd_data=pd_candles,
410
+ src_col_name='ema_long_periods',
411
+ slope_col_name='ema_long_slope',
412
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles)
413
+ )
390
414
 
391
- X = sm.add_constant(range(len(pd_candles['boillenger_upper'])))
392
- rolling_slope = pd_candles['boillenger_upper'].rolling(window=int(sliding_window_how_many_candles/slow_fast_interval_ratio)).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
393
- pd_candles['boillenger_upper_slope'] = rolling_slope
415
+ calculate_slope(
416
+ pd_data=pd_candles,
417
+ src_col_name='boillenger_upper',
418
+ slope_col_name='boillenger_upper_slope',
419
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles)
420
+ )
394
421
 
395
- X = sm.add_constant(range(len(pd_candles['boillenger_lower'])))
396
- rolling_slope = pd_candles['boillenger_lower'].rolling(window=sliding_window_how_many_candles).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
397
- pd_candles['boillenger_lower_slope'] = rolling_slope
422
+ calculate_slope(
423
+ pd_data=pd_candles,
424
+ src_col_name='boillenger_lower',
425
+ slope_col_name='boillenger_lower_slope',
426
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles)
427
+ )
398
428
 
399
- X = sm.add_constant(range(len(pd_candles['ema_rsi'])))
400
- rolling_slope = pd_candles['ema_rsi'].rolling(window=sliding_window_how_many_candles).apply(lambda x: sm.OLS(x, X[:len(x)]).fit().params[1], raw=False)
401
- pd_candles['ema_rsi_slope'] = rolling_slope
429
+ calculate_slope(
430
+ pd_data=pd_candles,
431
+ src_col_name='ema_rsi',
432
+ slope_col_name='ema_rsi_slope',
433
+ sliding_window_how_many_candles=int(sliding_window_how_many_candles)
434
+ )
402
435
 
403
436
  pd_candles['regular_divergence'] = (
404
437
  (pd_candles['ema_long_slope'] > 0) & (pd_candles['ema_rsi_slope'] < 0) |
@@ -1,8 +1,7 @@
1
1
  from datetime import datetime
2
- from typing import Dict, Union
3
-
2
+ import time
3
+ from typing import Union
4
4
  import pandas as pd
5
- import matplotlib.pyplot as plt
6
5
 
7
6
  from ccxt.okx import okx
8
7
 
@@ -14,6 +13,10 @@ base_ccy : str = "BTC"
14
13
  # ticker = "OL/USDT:USDT"
15
14
  ticker = f"{base_ccy}/USDT:USDT"
16
15
 
16
+ reload_raw_candles : bool = True
17
+ raw_candles_file : str = f"{base_ccy}_raw_candles.csv"
18
+ candles_with_ta_file : str = f"{base_ccy}_candles_ta.csv"
19
+
17
20
  param = {
18
21
  'rateLimit' : 100, # In ms
19
22
  'options' : {
@@ -23,7 +26,7 @@ param = {
23
26
  exchange = okx(param) # type: ignore
24
27
 
25
28
  start_date : datetime = datetime(2024,1,1)
26
- end_date : datetime = datetime(2025,4,20)
29
+ end_date : datetime = datetime(2025,4,22)
27
30
  candle_size : str = '1h'
28
31
  ma_long_intervals : int = 24*30
29
32
  ma_short_intervals : int = 24
@@ -33,15 +36,20 @@ pypy_compatible : bool = False
33
36
  markets = exchange.load_markets()
34
37
  assert(ticker in markets)
35
38
 
36
- pd_candles: Union[pd.DataFrame, None] = fetch_candles(
37
- start_ts=int(start_date.timestamp()),
38
- end_ts=int(end_date.timestamp()),
39
- exchange=exchange,
40
- normalized_symbols=[ ticker ],
41
- candle_size=candle_size
42
- )[ ticker ]
39
+ start = time.time()
43
40
 
44
- pd_candles.to_csv(f"{base_ccy}_raw_candles.csv") # type: ignore
41
+ if reload_raw_candles:
42
+ pd_candles: Union[pd.DataFrame, None] = fetch_candles(
43
+ start_ts=int(start_date.timestamp()),
44
+ end_ts=int(end_date.timestamp()),
45
+ exchange=exchange,
46
+ normalized_symbols=[ ticker ],
47
+ candle_size=candle_size
48
+ )[ ticker ]
49
+
50
+ pd_candles.to_csv(raw_candles_file) # type: ignore
51
+ else:
52
+ pd_candles = pd.read_csv(raw_candles_file) # type: ignore
45
53
 
46
54
  compute_candles_stats(
47
55
  pd_candles=pd_candles, # type: ignore
@@ -49,4 +57,8 @@ compute_candles_stats(
49
57
  sliding_window_how_many_candles=ma_long_intervals,
50
58
  slow_fast_interval_ratio=(ma_long_intervals/ma_short_intervals),
51
59
  pypy_compat=pypy_compatible
52
- )
60
+ )
61
+ pd_candles.to_csv(candles_with_ta_file) # type: ignore
62
+
63
+ compute_candles_stats_elapsed_ms = int((time.time() - start) *1000)
64
+ print(f"elapsed (ms): {compute_candles_stats_elapsed_ms}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: siglab-py
3
- Version: 0.3.3
3
+ Version: 0.3.5
4
4
  Summary: Market data fetches, TA calculations and generic order gateway.
5
5
  Author: r0bbarh00d
6
6
  Author-email: r0bbarh00d <r0bbarh00d@gmail.com>
@@ -25,15 +25,15 @@ siglab_py/tests/unit/analytic_util_tests.py,sha256=eeusM5zkQR2QyVhT7nqF0mwHVg7vl
25
25
  siglab_py/tests/unit/market_data_util_tests.py,sha256=A1y83itISmMJdn6wLpfwcr4tGola8wTf1D1xbelMvgw,2026
26
26
  siglab_py/tests/unit/trading_util_tests.py,sha256=tyefqOTQOoXSlemSDonqmdHp61-1nuXb0_6oeLlaNSM,2689
27
27
  siglab_py/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
- siglab_py/util/analytic_util.py,sha256=KX3IpSYI3-6SpT0kot20olaGxC1Ree4vWdVoMz2RDVc,45380
28
+ siglab_py/util/analytic_util.py,sha256=BI0_nYgs4R9FogyEN3KYaFHigLLsV05IMF7b2-Twemo,45608
29
29
  siglab_py/util/aws_util.py,sha256=KGmjHrr1rpnnxr33nXHNzTul4tvyyxl9p6gpwNv0Ygc,2557
30
30
  siglab_py/util/market_data_util.py,sha256=-IltjTdWsRIWPeXrY-tFbvdadAeL6_Kg3kKVCCGhU6Y,26369
31
31
  siglab_py/util/notification_util.py,sha256=vySgHjpHgwFDLW0tHSi_AGh9JBbPc25IUgvWxmjAeT8,2658
32
32
  siglab_py/util/retry_util.py,sha256=mxYuRFZRZoaQQjENcwPmxhxixtd1TFvbxIdPx4RwfRc,743
33
33
  siglab_py/util/slack_notification_util.py,sha256=G27n-adbT3Q6oaHSMvu_Nom794rrda5PprSF-zvmzkM,1912
34
- siglab_py/util/test_market_data_analytic_util.py,sha256=VUGIefCdR8uW9pwlt0ahE9gZm0zWQNwphTeiTvgKqAY,1706
34
+ siglab_py/util/test_market_data_analytic_util.py,sha256=vWzPbJL8lgdC-oBoiLv2OVgfplFUdyWh95-J2PoUyIg,2152
35
35
  siglab_py/util/trading_util.py,sha256=FmqsamuPhMjZUkz4lCyuE8MHFapXn6yNl8Isy7peQEs,3047
36
- siglab_py-0.3.3.dist-info/METADATA,sha256=uiCdJdpUNUzNpJsKM--FEqIkp6D-odwEYF31Ia_O3Ds,979
37
- siglab_py-0.3.3.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
38
- siglab_py-0.3.3.dist-info/top_level.txt,sha256=AbD4VR9OqmMOGlMJLkAVPGQMtUPIQv0t1BF5xmcLJSk,10
39
- siglab_py-0.3.3.dist-info/RECORD,,
36
+ siglab_py-0.3.5.dist-info/METADATA,sha256=n3OYBS8G2-ukuqlQPH-ZyVqaoDxehLTpAGBSGI9UPDI,979
37
+ siglab_py-0.3.5.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
38
+ siglab_py-0.3.5.dist-info/top_level.txt,sha256=AbD4VR9OqmMOGlMJLkAVPGQMtUPIQv0t1BF5xmcLJSk,10
39
+ siglab_py-0.3.5.dist-info/RECORD,,