diff --git a/bn.py b/bn.py index 926196e..e582343 100644 --- a/bn.py +++ b/bn.py @@ -2,7 +2,6 @@ from binance.spot import Spot client = Spot() - # Get klines def klines(symbol, interval): lines = client.klines(symbol,interval) diff --git a/strategy/crossover.py b/strategy/crossover.py index 9676fd9..1c5605c 100644 --- a/strategy/crossover.py +++ b/strategy/crossover.py @@ -16,7 +16,8 @@ def check_bullish_crossover(data): ema200 = talib.EMA(close_prices, timeperiod=200) # 判断是否出现多头排列信号 - if ema7[-1] > ema30[-1] > ema100[-1] > ema200[-1] and ema7[-2] <= ema30[-2] <= ema100[-2] <= ema200[-2]: + # if ema7[-1] > ema30[-1] > ema100[-1] > ema200[-1] and ema7[-2] <= ema30[-2] <= ema100[-2] <= ema200[-2]: + if ema7[-1] > ema30[-1] > ema100[-1] and ema7[-2] <= ema30[-2] <= ema100[-2]: return True else: return False @@ -33,7 +34,9 @@ def check_bearish_crossover(data): ema200 = talib.EMA(close_prices, timeperiod=200) # 判断是否出现空头排列信号 - if ema7[-1] < ema30[-1] < ema100[-1] < ema200[-1] and ema7[-2] >= ema30[-2] >= ema100[-2] >= ema200[-2]: + if ema7[-1] < ema30[-1] < ema100[-1] < ema200[-1] >= ema30[-2] >= ema100[-2]: + # if ema7[-1] < ema30[-1] < ema100[-1] < ema200[-1] and ema7[-2] >= ema30[-2] >= ema100[-2] >= ema200[-2]: + return True else: return False diff --git a/strategy/support_resistance.py b/strategy/support_resistance.py new file mode 100644 index 0000000..d1a5d27 --- /dev/null +++ b/strategy/support_resistance.py @@ -0,0 +1,44 @@ +import talib +import numpy as np +import bn +import tg +import settings + +# 获取K线价格数据 +def get_prices(data): + # 提取最高价、最低价和收盘价 + high_prices = [float(entry[2]) for entry in data] + low_prices = [float(entry[3]) for entry in data] + close_prices = [float(entry[4]) for entry in data] + return np.array(high_prices), np.array(low_prices), np.array(close_prices) + + +# 计算支撑位和压力位 +def calculate_support_resistance(data): + # 获取价格数据 + high_prices, low_prices, close_prices = get_prices(data) + + # 计算平均真实范围(ATR) + atr = talib.ATR(high_prices, low_prices, close_prices, timeperiod=14) + + # 计算支撑位和压力位 + support = talib.SMA(close_prices - 1.618 * atr, timeperiod=20) + resistance = talib.SMA(close_prices + 1.618 * atr, timeperiod=20) + + return support, resistance + +def strategy_run(symbol, interval): + # 获取kline数据 + data = bn.klines(symbol, interval) + + support_levels, resistance_levels = calculate_support_resistance(data) + + # 打印支撑位和压力位 + print("支撑位:", support_levels) + print("压力位:", resistance_levels) + + # if check_bullish_crossover(data): + # print('多头排列信号出现!') + + # text = f'${symbol} - {interval}\r\n\r\n出现【多头排列】信号' + # tg.send_message(settings.chat_id, text) diff --git a/strategy_test.py b/strategy_test.py new file mode 100644 index 0000000..d509d57 --- /dev/null +++ b/strategy_test.py @@ -0,0 +1,4 @@ +import strategy.support_resistance as sr + + +sr.strategy_run('BTCUSDT', '1h') \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..c13fe68 --- /dev/null +++ b/test.py @@ -0,0 +1,31 @@ +import bn +import pandas as pd +import mplfinance as mpf +import datetime as dt + +def binanceDataFrame(klines): + df = pd.DataFrame(klines.reshape(-1,12),dtype=float, columns = ('Open Time', + 'Open', + 'High', + 'Low', + 'Close', + 'Volume', + 'Close time', + 'Quote asset volume', + 'Number of trades', + 'Taker buy base asset volume', + 'Taker buy quote asset volume', + 'Ignore')) + + df['Open Time'] = pd.to_datetime(df['Open Time'], unit='ms') + + + return df + + +klines = bn.klines('BTCUSDT', '1h') + +df = pd.DataFrame(klines,dtype=float) +df.columns = ['Open time', 'Open', 'High', 'Low', 'Close', 'Volume', 'ctime', 'Quote asset volume', 'Number of trades', 'Taker buy base asset volume', 'Taker buy quote asset volume', 'Can be ignored'] +df.index = [dt.datetime.fromtimestamp(x/1000.0) for x in df.ctime] +mpf.plot(df, type='line')