import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json
import numpy as np
import pandas as pd
from datetime import datetime
def send_dingtalk_message(webhook, secret, message):
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
webhook = '{}×tamp={}&sign={}'.format(webhook, timestamp, sign)
headers = {'Content-Type': 'application/json'}
data = {"msgtype": "text", "text": {"content": message}}
response = requests.post(url=webhook, headers=headers, data=json.dumps(data))
print(response.text)
def read_events_from_file(filename="events.txt"):
try:
with open(filename, "r") as file:
return [line.strip() for line in file.readlines()]
except FileNotFoundError:
return []
def save_events_to_file(events, filename="events.txt"):
with open(filename, "w") as file:
for event in events:
file.write(str(event) + "\n")
def get_klines(symbol, interval, limit=100):
url = "https://fapi.binance.com/fapi/v1/klines"
params = {'symbol': symbol, 'interval': interval, 'limit': limit}
response = requests.get(url, params=params)
return response.json()
def calculate_ema(prices, period):
return pd.Series(prices).ewm(span=period, adjust=False).mean().tolist()
def calculate_macd(prices, fast_period, slow_period, signal_period):
ema_fast = calculate_ema(prices, fast_period)
ema_slow = calculate_ema(prices, slow_period)
macd = np.subtract(ema_fast[-len(ema_slow):], ema_slow)
signal = calculate_ema(macd, signal_period)
return macd, signal
def find_crossovers(macd, signal, times, prices, ema5, ema30):
crosses = []
min_length = min(len(macd), len(signal))
last_valid_time = datetime.min
for i in range(1, min_length):
closing_price = prices[i]
ema5_price = ema5[i]
ema30_price = ema30[i]
current_time = times[i]
cross_time = datetime.utcfromtimestamp(current_time / 1000 + 8*3600)
time_diff = (cross_time - last_valid_time).total_seconds() / 60
confidence_msg = ""
if macd[i-1] < signal[i-1] and macd[i] > signal[i] and closing_price > ema5_price:
if time_diff >= 30 or last_valid_time == datetime.min:
if closing_price > ema5_price and closing_price > ema30_price:
confidence_msg = "放心做吧"
crosses.append((i, 'Gold', cross_time.strftime('%Y-%m-%d %H:%M:%S'), '有效', confidence_msg))
last_valid_time = cross_time
else:
crosses.append((i, 'Gold', cross_time.strftime('%Y-%m-%d %H:%M:%S'), '無(wú)效', ""))
elif macd[i-1] > signal[i-1] and macd[i] < signal[i] and closing_price < ema5_price:
if time_diff >= 30 or last_valid_time == datetime.min:
if closing_price < ema5_price and closing_price < ema30_price:
confidence_msg = "放心做吧"
crosses.append((i, 'Dead', cross_time.strftime('%Y-%m-%d %H:%M:%S'), '有效', confidence_msg))
last_valid_time = cross_time
else:
crosses.append((i, 'Dead', cross_time.strftime('%Y-%m-%d %H:%M:%S'), '無(wú)效', ""))
return crosses
def main():
symbol = 'BTCUSDT'
interval = '5m'
limit = 500
klines = get_klines(symbol, interval, limit)
prices = [float(kline[4]) for kline in klines]
times = [int(kline[0]) for kline in klines]
ema5 = calculate_ema(prices, 5)
ema30 = calculate_ema(prices, 30)
macd, signal = calculate_macd(prices, 6, 7, 4)
crossovers = find_crossovers(macd, signal, times, prices, ema5, ema30)
# 這一段是新增的兵怯,用來(lái)處理事件的存儲(chǔ)和釘釘消息發(fā)送
existing_events = read_events_from_file("events.txt")
new_events = [event for event in crossovers if str(event) not in existing_events]
webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=8a6ddcf98d3b47c63333580bfe9d0bad55b17272eea05cc9c0af7f7be4de070d"
secret = "你的秘鑰"
if new_events:
save_events_to_file(crossovers, "events.txt") # 保存所有當(dāng)前識(shí)別的事件福荸,而不僅僅是新事件
for event in new_events:
message = " 穿越".join(map(str, event))
send_dingtalk_message(webhook_url, secret, message) # 確保替換 YOUR_WEBHOOK_URL 和 YOUR_SECRET
if __name__ == '__main__':
while True:
main()
time.sleep(5)
發(fā)送釘釘有效的穿越事件
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)该肴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人藐不,你說(shuō)我怎么就攤上這事匀哄∏匦В” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵涎嚼,是天一觀的道長(zhǎng)阱州。 經(jīng)常有香客問(wèn)我,道長(zhǎng)法梯,這世上最難降的妖魔是什么苔货? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮立哑,結(jié)果婚禮上夜惭,老公的妹妹穿的比我還像新娘。我一直安慰自己铛绰,他們只是感情好诈茧,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著捂掰,像睡著了一般敢会。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上这嚣,一...
- 那天鸥昏,我揣著相機(jī)與錄音,去河邊找鬼姐帚。 笑死吏垮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的卧土。 我是一名探鬼主播惫皱,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼尤莺!你這毒婦竟也來(lái)了旅敷?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤颤霎,失蹤者是張志新(化名)和其女友劉穎媳谁,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體友酱,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡晴音,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缔杉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锤躁。...
- 正文 年R本政府宣布椒振,位于F島的核電站昭伸,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一铛嘱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧灵份,春花似錦、人聲如沸罩引。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)袁铐。三九已至揭蜒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間剔桨,已是汗流浹背屉更。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像树绩,于是被迫代替她去往敵國(guó)和親萨脑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 一談《道德與法治》課上的有效化活動(dòng)教學(xué) 道德教育要回歸生活,怎樣讓《道德與法治》教學(xué)提高實(shí)效性瘫俊,達(dá)到育人目的...
- 實(shí)例: 比如接受到消息聊天消息后需要分發(fā)不同對(duì)應(yīng)頁(yè)面 1, 在接收到消息之后(需要發(fā)送監(jiān)聽(tīng)的地方)需要自定一個(gè)事件...
- 事件系統(tǒng)主要通過(guò)單例事件管理器:EventManager和事件參數(shù):EventArgs(可以是任意類型)赋朦,來(lái)進(jìn)行事...