diff --git a/bn.py b/bn.py index e582343..0f5f0ff 100644 --- a/bn.py +++ b/bn.py @@ -3,8 +3,8 @@ from binance.spot import Spot client = Spot() # Get klines -def klines(symbol, interval): - lines = client.klines(symbol,interval) +def klines(symbol, interval, limit=1000): + lines = client.klines(symbol,interval, limit=limit) return lines # Get Symbols diff --git a/main.py b/main.py index 43ab79b..28741ee 100644 --- a/main.py +++ b/main.py @@ -2,29 +2,21 @@ import schedule import bn import settings import time -import strategy.crossover as crossover +import strategy.ma_arrangement as maa import strategy.large_trans as lt # 获取交易所交易对 -# symbols = bn.symbols() +symbols = bn.symbols() -# for s in symbols: - # 5m - # schedule.every(settings.whaleAlert_minutes).minutes.do(lt.strategy_run) +for s in symbols: + #15m + schedule.every(15).minutes.do(maa.strategy_run, symbol=s, interval='15m') + #1h + schedule.every(1).hours.do(maa.strategy_run, symbol=s, interval='1h') - # 5m - # schedule.every(5).minutes.do(crossover.strategy_run, symbol=s, interval='5m') - - # 30m - # schedule.every(30).minutes.do(crossover.strategy_run, symbol=s, interval='30m') - - # 1h - # schedule.every(1).hours.do(crossover.strategy_run, symbol=s, interval='1h') - - # 4h - # schedule.every(4).hours.do(crossover.strategy_run, symbol=s, interval='4h') - + #4h + schedule.every(4).hours.do(maa.strategy_run, symbol=s, interval='4h') # 监控 schedule.every(settings.whaleAlert_minutes).minutes.do(lt.strategy_run) diff --git a/strategy/crossover.py b/strategy/crossover.py deleted file mode 100644 index 1c5605c..0000000 --- a/strategy/crossover.py +++ /dev/null @@ -1,59 +0,0 @@ -import talib -import numpy as np -import bn -import tg -import settings - -# 检查是否出现多头排列信号 -def check_bullish_crossover(data): - # 提取收盘价 - close_prices = np.array([float(entry[4]) for entry in data]) - - # 计算移动平均线 - ema7 = talib.EMA(close_prices, timeperiod=7) - ema30 = talib.EMA(close_prices, timeperiod=30) - ema100 = talib.EMA(close_prices, timeperiod=100) - 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] and ema7[-2] <= ema30[-2] <= ema100[-2]: - return True - else: - return False - -# 检查是否出现空头排列信号 -def check_bearish_crossover(data): - # 提取收盘价 - close_prices = np.array([float(entry[4]) for entry in data]) - - # 计算移动平均线 - ema7 = talib.EMA(close_prices, timeperiod=7) - ema30 = talib.EMA(close_prices, timeperiod=30) - ema100 = talib.EMA(close_prices, timeperiod=100) - ema200 = talib.EMA(close_prices, timeperiod=200) - - # 判断是否出现空头排列信号 - 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 - - -def strategy_run(symbol, interval): - # 获取kline数据 - data = bn.klines(symbol, interval) - - if check_bullish_crossover(data): - print('多头排列信号出现!') - - text = f'${symbol} - {interval}\r\n\r\n出现【多头排列】信号' - tg.send_message(settings.chat_id, text) - - - if check_bearish_crossover(data): - print("空头排列信号出现!") - text = f'${symbol} - {interval}\r\n\r\n出现【空头排列】信号' - tg.send_message(settings.chat_id, text) \ No newline at end of file diff --git a/strategy/large_trans.py b/strategy/large_trans.py index 31f46fd..c943911 100644 --- a/strategy/large_trans.py +++ b/strategy/large_trans.py @@ -9,31 +9,31 @@ import logging def strategy_run(): - start_time = datetime.now() - timedelta(minutes=settings.whaleAlert_minutes) - start_ts = int(start_time.timestamp()) - - url = f'https://api.whale-alert.io/v1/transactions?api_key={settings.whaleAlert_apikey}&start={start_ts}&min_value={settings.whaleAlert_max_limit}' - headers = {'Accept': 'application/json'} + try: + start_time = datetime.now() - timedelta(minutes=settings.whaleAlert_minutes) + start_ts = int(start_time.timestamp()) + + url = f'https://api.whale-alert.io/v1/transactions?api_key={settings.whaleAlert_apikey}&start={start_ts}&min_value={settings.whaleAlert_max_limit}' + headers = {'Accept': 'application/json'} - resp = requests.get(url, headers=headers).json() - - print(resp) - if resp['result'] == "success": - cursor = resp['cursor'] - try: + resp = requests.get(url, headers=headers).json() + + print(resp) + if resp['result'] == "success": if 'transactions' in resp: data = resp['transactions'] for ts in data: if ts['to']['owner_type'] == 'exchange': from_text = 'unknown wallet' - content = f"🚨 {ts['amount']} #{ts['symbol']} ({ts['amount_usd']} USD) 从 {from_text} 转入 #{ts['to']['owner']}" + content = f"🚨大额转入提醒🚨\r\n\r\n {ts['amount']} #{ts['symbol']} ({ts['amount_usd']} USD) 从 {from_text} 转入 #{ts['to']['owner']}" print(content) tg.send_message(settings.chat_id, content) - except Exception as e: - logging.error(traceback.format_exc()) - tg.send_message(settings.chat_id, traceback.format_exc()) - else: - print(resp['message']) + + else: + print(resp['message']) + tg.send_message(settings.chat_id, resp['message']) + except: + logging.error(traceback.format_exc()) tg.send_message(settings.chat_id, traceback.format_exc()) \ No newline at end of file diff --git a/strategy/ma_arrangement.py b/strategy/ma_arrangement.py new file mode 100644 index 0000000..1dd16a6 --- /dev/null +++ b/strategy/ma_arrangement.py @@ -0,0 +1,39 @@ +import talib +import numpy as np +import bn +import tg +import datetime +import settings + + +def check_ma_arrange(data): + # 提取收盘价 + close_prices = np.array([float(entry[4]) for entry in data]) + + # 计算移动平均线 + ema7 = talib.EMA(close_prices, timeperiod=7) + ema30 = talib.EMA(close_prices, timeperiod=30) + ema100 = talib.EMA(close_prices, timeperiod=100) + ema200 = talib.EMA(close_prices, timeperiod=200) + + bullish = ema7[-1] > ema30[-1] > ema100[-1] > ema200[-1] + bearish = ema7[-1] < ema30[-1] < ema100[-1] < ema200[-1] + + return bullish, bearish + + +def strategy_run(symbol, interval): + # 获取kline数据 + data = bn.klines(symbol, interval) + + bullish, bearish = check_ma_arrange(data) + + text="" + if bullish: + text = f'📶信号预警📶\r\n\r\n品种:【${symbol}】\r\n周期:{interval}\r\n信号:【多头】排列\r\n\r\n{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}' + if bearish: + text = f'📶信号预警📶\r\n\r\n品种:【${symbol}】\r\n周期:{interval}\r\n信号:【空头】排列\r\n\r\n{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}' + + if text != "": + print(text) + tg.send_message(settings.chat_id, text) \ No newline at end of file diff --git a/strategy_test.py b/strategy_test.py index 8b8d2b8..0be11c8 100644 --- a/strategy_test.py +++ b/strategy_test.py @@ -1,3 +1,8 @@ import strategy.large_trans as lt +import bn +import strategy.ma_arrangement as maa -lt.strategy_run() \ No newline at end of file +# symbols = bn.symbols() + +# for s in symbols: +maa.strategy_run('LINKUSDT', '15m') \ No newline at end of file diff --git a/test.py b/test.py index f83e3a2..8b8fe73 100644 --- a/test.py +++ b/test.py @@ -4,39 +4,12 @@ import mplfinance as mpf import datetime as dt import strategy.large_trans as lt -# 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') +klines = bn.klines('BTCUSDT', '1h', limit=10) +# print(klines[0]) -# return df +# print(klines[0][0]) +dt = dt.datetime.fromtimestamp((klines[-1][0]/1000)) - -# 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') - - -# lt.strategy_run() -from datetime import datetime, timedelta -import time,settings - -last_min = datetime.now() - timedelta(minutes=settings.whaleAlert_minutes) -print(last_min) -print(time.mktime(last_min.timetuple())) -print(last_min.timestamp()) \ No newline at end of file +print(dt) \ No newline at end of file