openfund-maker 1.0.16__py3-none-any.whl → 1.0.17__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.
@@ -167,29 +167,29 @@ class ThreeLineOrdergBot:
167
167
  atr = sum(trs[-period:]) / period
168
168
  return atr
169
169
 
170
- def calculate_sma_pandas(self,symbol, kLines, period) -> pd.Series:
170
+ def calculate_sma_pandas(self,symbol, klines, period) -> pd.Series:
171
171
  """
172
172
  使用 pandas 计算 SMA
173
173
  :param KLines K线
174
174
  :param period: SMA 周期
175
175
  :return: SMA 值
176
176
  """
177
- precision= self.get_precision_length(symbol)
178
- df = pd.DataFrame(kLines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
179
- sma = df['close'].rolling(window=period).mean().round(precision)
177
+
178
+ df = pd.DataFrame(klines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
179
+ sma = df['close'].rolling(window=period).mean()
180
180
  return sma
181
181
 
182
- def calculate_ema_pandas(self,symbol, kLines, period) -> pd.Series:
182
+ def calculate_ema_pandas(self,symbol, klines, period) -> pd.Series:
183
183
  """
184
184
  使用 pandas 计算 EMA
185
185
  :param KLines K线
186
186
  :param period: EMA 周期
187
187
  :return: EMA 值
188
188
  """
189
- precision= self.get_precision_length(symbol)
190
- df = pd.DataFrame(kLines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
189
+
190
+ df = pd.DataFrame(klines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
191
191
  # 计算EMA
192
- ema = df['close'].ewm(span=period, adjust=False).mean().round(precision)
192
+ ema = df['close'].ewm(span=period, adjust=False).mean()
193
193
  return ema
194
194
 
195
195
  def calculate_average_amplitude(self,klines, period=60):
@@ -315,7 +315,7 @@ class ThreeLineOrdergBot:
315
315
  self.logger.info(f"--------- ++ {symbol} Order placed done! --------")
316
316
 
317
317
  # 定义根据均线斜率判断 K 线方向的函数: 0 空 1 多 -1 平
318
- def judge_k_line_direction(self,symbol, pair_config, ema:pd.Series) -> int:
318
+ def judge_k_line_direction(self, symbol, pair_config, ema: pd.Series, klines) -> int:
319
319
  """
320
320
  判断K线方向
321
321
  Args:
@@ -325,37 +325,55 @@ class ThreeLineOrdergBot:
325
325
  Returns:
326
326
  int: -1:平, 0:空, 1:多
327
327
  """
328
- def check_ema_range(ema_data: pd.Series, period: int, limit: float, tick_size: float) -> bool:
329
- """检查EMA是否在指定范围内震荡"""
330
- ema_window = ema_data[-period:]
331
- price_range = ema_window.max() - ema_window.min()
332
- return abs(price_range) <= limit * tick_size
333
-
334
- def get_trend_direction(slope: float) -> int:
335
- """根据斜率判断趋势方向"""
336
- if slope > 0:
337
- return 1 # 上升趋势
338
- elif slope < 0:
339
- return 0 # 下降趋势
340
- return -1 # 震荡趋势
341
-
342
328
  # 获取配置参数
343
- tick_size = self.get_tick_size(symbol)
344
- ema_range_period = int(pair_config.get('ema_range_period', 3))
345
- ema_range_limit = float(pair_config.get('ema_range_limit', 1))
329
+ period = int(pair_config.get('ema_range_period', 3))
346
330
 
347
- # 判断是否在震荡区间
348
- if check_ema_range(ema, ema_range_period, ema_range_limit, tick_size):
349
- direction = -1
331
+ # precision= self.get_precision_length(symbol)
332
+
333
+ ema_4_diff = ema.diff().tail(period)
334
+
335
+ direction = None
336
+ if all(ema_4_diff <= 0) :
337
+ # 下降趋势
338
+ direction = 0
339
+ elif all(ema_4_diff >= 0) :
340
+ # 上升趋势
341
+ direction = 1
350
342
  else:
351
- # 计算最新斜率并判断方向
352
- latest_slope = ema.diff().iloc[-1]
353
- direction = get_trend_direction(latest_slope)
354
-
355
- self.logger.debug(f"{symbol}: 极差={abs(ema[-ema_range_period:].max() - ema[-ema_range_period:].min()):.9f} "
356
- f"斜率={ema.diff().iloc[-1]:.9f}, K线方向 {direction}")
357
-
343
+ # 震荡趋势
344
+ direction = -1
345
+ self.logger.debug(f"{symbol}: K线极差={ema_4_diff.map('{:.9f}'.format).values} ,K线方向={direction}")
358
346
  return direction
347
+
348
+ def judge_ema_direction(self, symbol, pair_config, ema: pd.Series) -> int:
349
+ """
350
+ 判断EMA方向
351
+ Args:
352
+ symbol: 交易对
353
+ pair_config: 配置参数
354
+ ema: EMA数据
355
+ Returns:
356
+ int: -1:平, 0:空, 1:多
357
+ """
358
+ # 获取配置参数
359
+ period = int(pair_config.get('ema_range_period', 3))
360
+
361
+ precision= self.get_precision_length(symbol)
362
+
363
+ ema_4_diff = ema.round(precision).diff().tail(period)
364
+
365
+ direction = None
366
+ if all(ema_4_diff <= 0) and any(ema_4_diff < 0) :
367
+ # 下降趋势
368
+ direction = 0
369
+ elif all(ema_4_diff >= 0) and any(ema_4_diff > 0) :
370
+ # 上升趋势
371
+ direction = 1
372
+ else:
373
+ # 震荡趋势
374
+ direction = -1
375
+ self.logger.debug(f"{symbol}: EMA极差={ema_4_diff.map('{:.4f}'.format).values} ,EMA方向={direction}")
376
+ return direction
359
377
 
360
378
  def judge_cross_direction(self,fastklines,slowklines) :
361
379
  # 创建DataFrame
@@ -392,8 +410,10 @@ class ThreeLineOrdergBot:
392
410
  'index': last_death
393
411
  }
394
412
 
395
- def judge_ma_apex(self,symbol, fastklines,slowklines) -> bool:
396
-
413
+ def judge_ma_apex(self,symbol,pair_config, fastklines,slowklines) -> bool:
414
+ period = int(pair_config.get('ema_range_period', 3))
415
+ precision= self.get_precision_length(symbol)
416
+
397
417
  df = pd.DataFrame({
398
418
  'ema': fastklines,
399
419
  'sma': slowklines
@@ -401,16 +421,18 @@ class ThreeLineOrdergBot:
401
421
  # 快线和慢线的差值
402
422
  # 将ema和sma转换为tick_size精度
403
423
  # df['diff'] = df['ema'].apply(lambda x: float(self.round_price_to_tick(x, tick_size))) - df['sma'].apply(lambda x: float(self.round_price_to_tick(x, tick_size)))
404
- df['diff'] = df['ema']-df['sma']
424
+ df['diff'] = df['ema'].round(precision)-df['sma'].round(precision)
425
+ df['ema_diff'] = df['ema'] - df['ema'].shift(1)
426
+ df['sma_diff'] = df['sma'] - df['sma'].shift(1)
405
427
  # 计算斜率,【正】表示两线距离扩张,【负】表示两线距离收缩
406
428
  df['slope'] = df['diff'].abs().diff().round(4)
407
- df['flag'] = df['slope'] <= 0.0
408
429
 
409
- self.logger.debug(f"{symbol}: slopes = \n{df[['ema','sma','diff','slope','flag']].iloc[-6:-1]} ")
410
- # 检查最后两个斜率是否都为负
411
- # 取slopes最新的第2个和第3个值进行判断
430
+ self.logger.debug(f"{symbol}: slopes = \n{df[['ema','ema_diff','sma','sma_diff','diff','slope']].iloc[-6:-1]} ")
412
431
 
413
- return all(slope <= 0.0 for slope in df['slope'].iloc[-3:-1])
432
+ # 两条线的距离是扩张状态还是收缩状态 true 是收缩 flase 是扩张
433
+ is_expanding_or_contracting = all(df['slope'].tail(period) <= 0 ) and any(df['slope'].tail(period) < 0)
434
+
435
+ return is_expanding_or_contracting
414
436
 
415
437
 
416
438
  def calculate_range_diff(self,prices:pd.Series) -> float:
@@ -474,8 +496,8 @@ class ThreeLineOrdergBot:
474
496
  sma_length = pair_config.get('sma', 50)
475
497
 
476
498
  # 增加 金叉死叉 方向确认的 20250209
477
- fastk = self.calculate_ema_pandas(symbol, klines, period=ema_length)
478
- slowk = self.calculate_sma_pandas(symbol, klines, period=sma_length)
499
+ fastk = self.calculate_ema_pandas(symbol=symbol, klines=klines, period=ema_length)
500
+ slowk = self.calculate_sma_pandas(symbol=symbol, klines=klines, period=sma_length)
479
501
 
480
502
  cross_direction = self.judge_cross_direction(fastklines=fastk,slowklines=slowk)
481
503
  # 更新交叉状态
@@ -487,27 +509,28 @@ class ThreeLineOrdergBot:
487
509
 
488
510
 
489
511
  # 判断趋势:多头趋势或空头趋势
490
- direction = self.judge_k_line_direction(symbol=symbol, pair_config=pair_config,ema=fastk)
512
+ direction = self.judge_k_line_direction(symbol=symbol, pair_config=pair_config,ema=fastk,klines=klines)
491
513
  if direction == 1:
492
514
  is_bullish_trend = True
493
515
  elif direction == 0:
494
516
  is_bearish_trend = True
495
517
 
496
518
  # 结合金叉死叉判断是否是周期顶部和底部
497
- is_apex = self.judge_ma_apex(symbol=symbol,fastklines=fastk,slowklines=slowk)
519
+ is_apex = self.judge_ma_apex(symbol=symbol,pair_config=pair_config, fastklines=fastk,slowklines=slowk)
520
+ ema_direction = self.judge_ema_direction(symbol=symbol, pair_config=pair_config,ema=fastk)
498
521
  # 金叉死叉逻辑
499
522
  if last_cross_direction and last_cross_direction['cross'] == 1 : # 金叉
500
523
  self.logger.debug(f"{symbol} 金叉:{last_cross_direction},清理空单,挂多单!!")
501
524
  is_bearish_trend = False
502
- if is_apex :
503
- self.logger.debug(f"{symbol} 金叉:{last_cross_direction},周期见顶 {is_apex},不开单!!")
525
+ if is_apex or ema_direction == -1:
526
+ self.logger.debug(f"{symbol} 金叉:{last_cross_direction},周期见顶={is_apex} ,ema_平={ema_direction},不开单!!")
504
527
  is_bullish_trend = False
505
528
 
506
529
  elif last_cross_direction and last_cross_direction['cross'] == 0 : # 死叉
507
530
  self.logger.debug(f"{symbol} 死叉:{last_cross_direction},清理多单,挂空单!!")
508
531
  is_bullish_trend = False
509
- if is_apex :
510
- self.logger.debug(f"{symbol} 死叉:{last_cross_direction},周期见顶 {is_apex},不开单!!")
532
+ if is_apex or ema_direction == -1:
533
+ self.logger.debug(f"{symbol} 死叉:{last_cross_direction},周期见顶={is_apex} ,ema_平={ema_direction},不开单!!")
511
534
  is_bearish_trend = False
512
535
 
513
536
  else:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: openfund-maker
3
- Version: 1.0.16
3
+ Version: 1.0.17
4
4
  Summary: Openfund-maker.
5
5
  Requires-Python: >=3.9,<4.0
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,4 +1,4 @@
1
- maker/ThreeLineOrderBot.py,sha256=e-KVx6a4cW2Afe0kNOKcA8isDObnzM_7wEmIlfw9tcI,25741
1
+ maker/ThreeLineOrderBot.py,sha256=c9RTJAGFYaSBOpA4OKxTm-LxFPyiof-EkKUOvjfyfaw,26378
2
2
  maker/WickReversalOrderBot.py,sha256=Oc6wChdWu39lfWh3NRHM8BqvaRIYDNZiDR6PDnE9XUM,17374
3
3
  maker/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  maker/config.py,sha256=YPxghO5i0vgRg9Cja8kGj9O7pgSbbtzOgf3RexqXXwY,1188
@@ -7,7 +7,7 @@ maker/main_m.py,sha256=0PzDTnuBrxfpy5WDfsIHKAzZ_7pkuvuqqeWik0vpWio,15522
7
7
  maker/okxapi.py,sha256=_9G0U_o0ZC8NxaT6PqpiLgxBm9gPobC9PsFHZE1c5w0,553
8
8
  maker/zhen.py.bak,sha256=HNkrQbJts8G9umE9chEFsc0cLQApcM9KOVNMYPpkBXM,10918
9
9
  maker/zhen_2.py,sha256=4IaHVtTCMSlrLGSTZrWpW2q-f7HZsUNRkW_-5QgWv24,10509
10
- openfund_maker-1.0.16.dist-info/METADATA,sha256=CkBN5XbxLll1H5OvDev47s8mYi4cS80yqIg50Q-fVJw,1966
11
- openfund_maker-1.0.16.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
12
- openfund_maker-1.0.16.dist-info/entry_points.txt,sha256=gKMytICEKcMRFQDFkHZLnIpID7UQFoTIM_xcpiiV6Ns,50
13
- openfund_maker-1.0.16.dist-info/RECORD,,
10
+ openfund_maker-1.0.17.dist-info/METADATA,sha256=tYHu6KxF0Da_dVhzZMWvTcdCnq6baxN4jrF6H_Q78G0,1966
11
+ openfund_maker-1.0.17.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
12
+ openfund_maker-1.0.17.dist-info/entry_points.txt,sha256=gKMytICEKcMRFQDFkHZLnIpID7UQFoTIM_xcpiiV6Ns,50
13
+ openfund_maker-1.0.17.dist-info/RECORD,,