美文网首页
he &yue5m 趋势走势

he &yue5m 趋势走势

作者: Leoguo小哥 | 来源:发表于2024-08-29 19:07 被阅读0次

import json

import requests
import numpy as np
from datetime import datetime, timedelta, timezone


import requests
import pandas as pd
import time
import hmac
import hashlib
import base64
import urllib.parse
import json

from tools.read_write import read_last_status, update_last_status

webhook="https://oapi.dingtalk.com/robot/send?access_token=8a6ddcf98d3b47c63333580bfe9d0bad55b17272eea05cc9c0af7f7be4de070d"
secret=""
last_status_up_down=None #上次趋势的状态


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 = '{}&timestamp={}&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 get_klines(symbol, interval, limit=100):
   # url = "https://api.binance.com/api/v3/klines" #现货的
   url = "https://fapi.binance.com/fapi/v1/klines" #合约的
   params = {'symbol': symbol, 'interval': interval, 'limit': limit}
   print(url)
   print(json.dumps(params, indent=4))
   response = requests.get(url, params=params)
   return response.json()

def calculate_sma(prices, window):
   return np.convolve(prices, np.ones(window)/window, mode='valid')

def calculate_ema(prices, period, smoothing=2):
   ema = [sum(prices[:period]) / period]
   for price in prices[period:]:
       ema.append((price * (smoothing / (1 + period))) + ema[-1] * (1 - (smoothing / (1 + period))))
   return ema

def calculate_macd(prices):
   fast_ema = calculate_ema(prices, 6)
   slow_ema = calculate_ema(prices, 7)
   macd = np.subtract(fast_ema[-len(slow_ema):], slow_ema)
   signal = calculate_ema(macd, 4)
   return macd, signal

def calculate_boll(prices, window=21, num_std=2):
   sma = calculate_sma(prices, window)
   std = np.sqrt(calculate_sma(np.power(np.subtract(prices[window-1:], sma), 2), 1))
   upper_band = sma + (std * num_std)
   lower_band = sma - (std * num_std)
   return upper_band, sma, lower_band


# def find_special_moments(symbol, interval):
#     global last_status_up_down
#     klines = get_klines(symbol, interval, 100)
#     close_prices = np.array([float(kline[4]) for kline in klines], dtype=np.float64)
#     high_prices = np.array([float(kline[2]) for kline in klines], dtype=np.float64)  # 获取最高价格
#     low_prices = np.array([float(kline[3]) for kline in klines], dtype=np.float64)  # 获取最低价格
#     dates = [datetime.fromtimestamp(int(kline[0]) / 1000, tz=timezone.utc) for kline in klines]  # 时间戳转换为datetime对象
#
#     upper_band, middle_band, lower_band = calculate_boll(close_prices)
#     macd_line, _ = calculate_macd(close_prices)
#
#
#     boll_reduction = len(close_prices) - len(middle_band)
#     macd_reduction = len(close_prices) - len(macd_line)
#     start_index = max(boll_reduction, macd_reduction) + 2
#
#     for i in range(start_index, len(close_prices)):
#         adjusted_index = i - boll_reduction
#         macd_index = i - macd_reduction
#         prev_kline_time = dates[i - 1].astimezone(timezone(timedelta(hours=8)))  # 前一个K线的时间,转换为UTC+8时区
#         if close_prices[i] < middle_band[adjusted_index - 2] and macd_line[macd_index] < 0 and macd_line[macd_index] < \
#                 macd_line[macd_index - 1] < macd_line[macd_index - 2]:
#             mess = f"下跌趋势穿越--中线下方DIF减小: {dates[i].astimezone(timezone(timedelta(hours=8)))}, 前一个K线时间: {prev_kline_time}, 最高价: {high_prices[i - 1]}, 最低价: {low_prices[i - 1]}"
#             print(mess)
#             last_status_up_down = read_last_status()
#             print("1"*88)
#             print(last_status_up_down)
#             if last_status_up_down !="down":
#                 print("发送钉钉消息拉 down  down down ")
#                 send_dingtalk_message(webhook,secret,message=mess)
#             last_status_up_down="down"
#         elif close_prices[i] > middle_band[adjusted_index - 2] and macd_line[macd_index] > 0 and macd_line[macd_index] > \
#                 macd_line[macd_index - 1] > macd_line[macd_index - 2]:
#             mess = f"上涨趋势穿越--中线上方DIF增大: {dates[i].astimezone(timezone(timedelta(hours=8)))}, 前一个K线时间: {prev_kline_time}, 最高价: {high_prices[i - 1]}, 最低价: {low_prices[i - 1]}"
#             print(mess)
#             last_status_up_down = read_last_status()
#             print("2"*88)
#             print(last_status_up_down)
#             if last_status_up_down !="up":
#                 print("发送钉钉消息拉 up  up  up ")
#                 send_dingtalk_message(webhook,secret,message=mess)
#             last_status_up_down="up"


# 调用函数


def find_special_moments(symbol, interval):
   global last_status_up_down
   klines = get_klines(symbol, interval, 100)  # 获取最新的100条K线数据
   close_prices = np.array([float(kline[4]) for kline in klines], dtype=np.float64)
   high_prices = np.array([float(kline[2]) for kline in klines], dtype=np.float64)  # 获取最高价格
   low_prices = np.array([float(kline[3]) for kline in klines], dtype=np.float64)  # 获取最低价格
   dates = [datetime.fromtimestamp(int(kline[0]) / 1000, tz=timezone.utc) for kline in klines]  # 时间戳转换为datetime对象

   upper_band, middle_band, lower_band = calculate_boll(close_prices)
   macd_line, _ = calculate_macd(close_prices)

   # 开始处理最后3条数据
   for i in range(len(close_prices) - 1, len(close_prices)):
       boll_reduction = len(close_prices) - len(middle_band)
       macd_reduction = len(close_prices) - len(macd_line)
       adjusted_index = i - boll_reduction
       macd_index = i - macd_reduction
       if i > 0:  # 确保有前一个K线的时间可以引用
           prev_kline_time = dates[i - 1].astimezone(timezone(timedelta(hours=8)))  # 前一个K线的时间,转换为UTC+8时区

       if close_prices[i] < middle_band[adjusted_index - 2] and macd_line[macd_index] < 0 and macd_line[macd_index] < \
               macd_line[macd_index - 1] < macd_line[macd_index - 2]:
           mess = f"下跌趋势穿越--中线下方DIF减小: {dates[i].astimezone(timezone(timedelta(hours=8)))}, 前一个K线时间: {prev_kline_time}, 最高价: {high_prices[i - 1]}, 最低价: {low_prices[i - 1]}"
           print(mess)
           last_status_up_down = read_last_status()
           if last_status_up_down != "down":
               print("发送钉钉消息拉 down down down ")
               send_dingtalk_message(webhook, secret, message=mess)

           last_status_up_down = "down"
           update_last_status(last_status_up_down)
       elif close_prices[i] > middle_band[adjusted_index - 2] and macd_line[macd_index] > 0 and macd_line[macd_index] > \
               macd_line[macd_index - 1] > macd_line[macd_index - 2]:
           mess = f"上涨趋势穿越--中线上方DIF增大: {dates[i].astimezone(timezone(timedelta(hours=8)))}, 前一个K线时间: {prev_kline_time}, 最高价: {high_prices[i - 1]}, 最低价: {low_prices[i - 1]}"
           print(mess)
           last_status_up_down = read_last_status()
           if last_status_up_down != "up":
               print("发送钉钉消息拉 up up up ")
               send_dingtalk_message(webhook, secret, message=mess)
           last_status_up_down = "up"
           update_last_status(last_status_up_down)

symbol = 'BTCUSDT'
interval = '5m'

while True:  # 开始一个无限循环
   find_special_moments(symbol, interval)  # 调用原有的查询和分析函数
   time.sleep(10)  # 暂停5秒钟再次执行

相关文章

  • 11. 上涨趋势起点分析

    趋势分为上涨趋势和下跌趋势。走势分为趋势和盘整。最近30分钟级别走势走了一个趋势走势。趋势走势的起点,也就是中枢震...

  • 趋势结构

    趋势的结构是由趋势性走势和调整性走势构成;趋势性走势是利润之源,而调整性走势是进场的最佳时机,一般而言合理的进程点...

  • 博涛缠论系列:中枢的延伸扩展C#05(二)

    一、缠论趋势定义: 在任何级别的任何走势中,某完成的走势类型至少包含两个以上依次同向的走势中枢,就称为该级别的趋势...

  • BTC指标背离,下跌风险增加

    #市场行情走势 BTC昨日盘整趋势为主,最低来到5076,最高5200,盘整区间为2%,目前走势仍然撑在上升趋势线...

  • 2019-10-01理论知识梳理

    基础理论构架 打开图表先有了走势,站在已经完成的角度,走势只有趋势、方向,...

  • 会看这俩点你就会看股票的趋势了

    如何判定股票出现正确的走势? 力生制药 1:关注走势的基本趋势,入上图为上升趋势中的大区间震荡 2:关注区间的高低...

  • 2021-04-13 缠中说禅

    没有趋势,没有背驰。在盘整中是无所谓“背驰”的。 “走势终完美”,任何走势,无论是趋势还是盘整,在图形上最终都要完...

  • 妖股45亿吸筹欲“杀鸡取卵”, 四月行情初现

    大盘走势 1、股价上升趋势完好,目前沿着上升趋势稳步上行,因为是周线级别的市场,更能真实的反映出短期市场的走势。 ...

  • 闲话《缠论》(七七)

    闲话《缠论》(七七) 闲话:缠中说缠17章《走势终完美》 1.任何级别的所有走势,都能分解成趋势与盘整两类,而趋势...

  • 【转】教你炒股票17:走势终完美

    教你炒股票17:走势终完美 (2006年12月18) 第1段:任何级别的所有走势,都能分解成趋势与盘整两类,而趋势...

网友评论

      本文标题:he &yue5m 趋势走势

      本文链接:https://www.haomeiwen.com/subject/hbixljtx.html