This commit is contained in:
27942
2025-10-13 11:22:02 +08:00
parent 48028edf56
commit ea085f244b
5 changed files with 68 additions and 256 deletions

Binary file not shown.

View File

@@ -1,4 +1,4 @@
from peewee import *
# 连接到 SQLite 数据库,如果文件不存在会自动创建
db = SqliteDatabase('database.db')
db = SqliteDatabase(r'C:\Users\27942\Desktop\job\models\database.db')

Binary file not shown.

View File

@@ -14,20 +14,9 @@ class Weex(Model):
database = db
table_name = 'weex'
# 连接到数据库
db.connect()
# 创建表(如果表不存在)
db.create_tables([Weex])
# 示例数据
data = ['100', '101', '99', '100.5', 1]
# 使用 get_or_create 方法插入数据
Weex.get_or_create(
id=data[1],
open=data[0],
high=data[1],
low=data[2],
close=data[3]
)

View File

@@ -1,256 +1,79 @@
import datetime
import time
import pandas as pd
import requests
from loguru import logger
from curl_cffi import requests
from models.weex import Weex
# ================= 辅助函数 =================
headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'appversion': '2.0.0',
'bundleid': '',
'cache-control': 'no-cache',
'dnt': '1',
'language': 'zh_CN',
'origin': 'https://www.weeaxs.site',
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': 'https://www.weeaxs.site/',
'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'sidecar': '01bfdfef90d2d6213c86b09f3a00d696d366752996a0b6692a33969a67fcd243df',
'terminalcode': '89adf61538715df59eb4f6414981484e',
'terminaltype': '1',
'traceid': 'mgoh8eoehxp15lxnbv',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0',
'vs': 'G5h7sX78yNSykC9xtTmA7O2lSKqAk6Sp',
'x-sig': '74ec6a65f3886f24a8b062e3b41edb1a',
'x-timestamp': '1760320261454',
}
def is_bullish(candle):
"""判断是否是阳线(开盘价 < 收盘价,即涨)"""
return float(candle['open']) < float(candle['close'])
klineId = None
klineTime = None
contractId = None
def is_bearish(candle):
"""判断是否是阴线(开盘价 > 收盘价,即跌)"""
return float(candle['open']) > float(candle['close'])
def check_signal(prev, curr):
"""
判断是否出现包住形态,返回信号类型和方向:
1. 前跌后涨包住 -> 做多信号 (bear_bull_engulf)
2. 前涨后跌包住 -> 做空信号 (bull_bear_engulf)
3. 前涨后涨包住 -> 做多信号 (bull_bull_engulf)
4. 前跌后跌包住 -> 做空信号 (bear_bear_engulf)
"""
p_open, p_close = float(prev['open']), float(prev['close']) # 前一笔
c_open, c_close = float(curr['open']), float(curr['close']) # 当前一笔
# 情况1前跌后涨且涨线包住前跌线 -> 做多信号
if is_bullish(curr) and is_bearish(prev):
if c_open <= p_close and c_close >= p_open:
return "long", "bear_bull_engulf"
# 情况2前涨后跌且跌线包住前涨线 -> 做空信号
if is_bearish(curr) and is_bullish(prev):
if c_open >= p_close and c_close <= p_open:
return "short", "bull_bear_engulf"
# # 情况3前涨后涨且后涨线包住前涨线 -> 做多信号
# if is_bullish(curr) and is_bullish(prev):
# if c_open < p_open and c_close > p_close:
# return "long", "bull_bull_engulf"
# # 情况4前跌后跌且后跌线包住前跌线 -> 做空信号
# if is_bearish(curr) and is_bearish(prev):
# if c_open > p_open and c_close < p_close:
# return "short", "bear_bear_engulf"
return None, None
def simulate_trade(direction, entry_price, future_candles, take_profit_diff=30, stop_loss_diff=-10):
"""
模拟交易逐根K线回测
使用价差来控制止盈止损:
- 盈利达到 take_profit_diff 就止盈
- 亏损达到 stop_loss_diff 就止损
direction信号类型
entry_price开盘价格
future_candles未来的行情数据
"""
for candle in future_candles:
high, low, close = float(candle['high']), float(candle['low']), float(candle['close'])
if direction == "long":
# 做多:检查最高价是否达到止盈,最低价是否触及止损
if high >= entry_price + take_profit_diff:
return entry_price + take_profit_diff, take_profit_diff, candle['id'] # 止盈
if low <= entry_price + stop_loss_diff:
return entry_price + stop_loss_diff, stop_loss_diff, candle['id'] # 止损
elif direction == "short":
# 做空:检查最低价是否达到止盈,最高价是否触及止损
if low <= entry_price - take_profit_diff:
return entry_price - take_profit_diff, take_profit_diff, candle['id'] # 止盈
if high >= entry_price - stop_loss_diff:
return entry_price - stop_loss_diff, stop_loss_diff, candle['id'] # 止损
# 如果未来都没触发,最后一根收盘平仓
final_price = float(future_candles[-1]['close'])
if direction == "long":
diff_money = final_price - entry_price
else:
diff_money = entry_price - final_price
return final_price, diff_money, future_candles[-1]['id']
if __name__ == '__main__':
# zh_project = 0 # 累计盈亏
# all_trades = [] # 保存所有交易明细
#
# # 四种信号类型的统计
# signal_stats = {
# "bear_bull_engulf": {"count": 0, "wins": 0, "total_profit": 0, "name": "涨包跌"},
# "bull_bear_engulf": {"count": 0, "wins": 0, "total_profit": 0, "name": "跌包涨"},
# # "bull_bull_engulf": {"count": 0, "wins": 0, "total_profit": 0, "name": "涨包涨"},
# # "bear_bear_engulf": {"count": 0, "wins": 0, "total_profit": 0, "name": "跌包跌"}
# }
headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'appversion': '2.0.0',
'bundleid': '',
'cache-control': 'no-cache',
'dnt': '1',
'language': 'zh_CN',
'origin': 'https://www.weeaxs.site',
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': 'https://www.weeaxs.site/',
'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'sidecar': '01bfdfef90d2d6213c86b09f3a00d696d366752996a0b6692a33969a67fcd243df',
'terminalcode': '89adf61538715df59eb4f6414981484e',
'terminaltype': '1',
'traceid': 'mgoh8eoehxp15lxnbv',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0',
'vs': 'G5h7sX78yNSykC9xtTmA7O2lSKqAk6Sp',
'x-sig': '74ec6a65f3886f24a8b062e3b41edb1a',
'x-timestamp': '1760320261454',
for i in range(500):
print(i)
params = {
'languageType': '1',
'sign': 'SIGN',
'timeZone': 'string',
'contractId': '10000002',
'productCode': 'ethusdt',
'priceType': 'LAST_PRICE',
'klineType': 'MINUTE_15',
'limit': '329',
}
datas = []
if klineId:
params['nextKey.klineId'] = klineId
params['nextKey.contractId'] = contractId
klineId = None
klineTime = None
for i in range(500):
if klineTime:
params['nextKey.klineTime'] = klineTime
params = {
'languageType': '1',
'sign': 'SIGN',
'timeZone': 'string',
'contractId': '10000002',
'productCode': 'ethusdt',
'priceType': 'LAST_PRICE',
'klineType': 'MINUTE_15',
'limit': '329',
}
print(params)
response = requests.get('https://http-gateway1.janapw.com/api/v1/public/quote/v1/getKlineV2', params=params,
headers=headers)
# if klineId:
# params['nextKey.klineId'] = klineId
# params['nextKey.contractId'] = '10000002'
#
# if klineTime:
# params['nextKey.klineTime'] = klineTime
klineId = response.json()["data"]["nextKey"]["klineId"]
klineTime = response.json()["data"]["nextKey"]["klineTime"]
contractId = response.json()["data"]["nextKey"]["contractId"]
response = requests.get(
'https://http-gateway2.janapw.com/api/v1/public/quote/v1/getKlineV2',
params=params,
headers=headers
for data in response.json()["data"]["dataList"]:
# print(data)
Weex.get_or_create(
id=int(data[4]),
defaults={
'open': float(data[3]),
'high': float(data[1]),
'low': float(data[2]),
'close': float(data[0]),
}
)
klineId = response.json()["data"]["nextKey"]["klineId"]
klineTime = response.json()["data"]["nextKey"]["klineTime"]
for data in response.json()["data"]["dataList"]:
# print(data)
datas.append(
{
"open": data[3],
"high": data[1],
"low": data[2],
"close": data[0],
"id": data[4],
}
)
datas = sorted(datas, key=lambda x: x["id"])
for data in datas:
print(data)
# 将列表转换为 DataFrame
df = pd.DataFrame(datas)
# 将 DataFrame 保存为 Excel 文件
df.to_excel('stock_data.xlsx', index=False)
print("数据已成功保存到 stock_data.xlsx 文件中。")
# daily_signals = 0 # 信号总数
# daily_wins = 0
# daily_profit = 0 # 价差总和
#
# # 遍历每根K线寻找信号
# for idx in range(1, len(datas) - 2): # 留出未来K线
# prev, curr = datas[idx - 1], datas[idx] # 前一笔,当前一笔
# entry_candle = datas[idx + 1] # 下一根开盘价作为入场价
# future_candles = datas[idx + 2:] # 未来行情
#
# entry_open = float(entry_candle['open']) # 开仓价格
# direction, signal_type = check_signal(prev, curr) # 判断开仓方向和信号类型
#
# if direction and signal_type:
# daily_signals += 1
#
# exit_price, diff, exit_time = simulate_trade(direction, entry_open, future_candles, take_profit_diff=30,
# stop_loss_diff=-2)
#
# # 统计该信号类型的表现
# signal_stats[signal_type]["count"] += 1
# signal_stats[signal_type]["total_profit"] += diff
# if diff > 0:
# signal_stats[signal_type]["wins"] += 1
# daily_wins += 1
#
# daily_profit += diff
#
# # 将时间戳转换为本地时间
# local_time = datetime.datetime.fromtimestamp(int(entry_candle['id']) / 1000)
# formatted_time = local_time.strftime("%Y-%m-%d %H:%M:%S")
#
# # 保存交易详情
# all_trades.append(
# (
# f"{formatted_time}号",
# "做多" if direction == "long" else "做空",
# signal_stats[signal_type]["name"],
# entry_open,
# exit_price,
# diff,
# exit_time
# )
# )
#
# # ===== 输出每笔交易详情 =====
# logger.info("===== 每笔交易详情 =====")
# n = n1 = 0 # n = 总盈利n1 = 总手续费
# for date, direction, signal_name, entry, exit, diff, end_time in all_trades:
# profit_amount = diff / entry * 10000 # 计算盈利金额
# close_fee = 10000 / entry * exit * 0.0005 # 平仓手续费
#
# logger.info(
# f"{date} {direction}({signal_name}) 入场={entry:.2f} 出场={exit:.2f} 出场时间={end_time} "
# f"差价={diff:.2f} 盈利={profit_amount:.2f} "
# f"开仓手续费=5u 平仓手续费={close_fee:.2f}"
# )
# n1 += 5 + close_fee
# n += profit_amount
#
# print(f'一共笔数:{len(all_trades)}')
# print(f"一共盈利:{n:.2f}")
# print(f'一共手续费:{n1:.2f}')
# time.sleep(1)