137 lines
4.5 KiB
Python
137 lines
4.5 KiB
Python
from flask import Flask, render_template, jsonify
|
||
import pandas as pd
|
||
import numpy as np
|
||
import os
|
||
import ast
|
||
|
||
app = Flask(__name__)
|
||
|
||
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
||
# os.chdir(current_dir)
|
||
# print("已更改为新的工作目录:", current_dir)
|
||
|
||
# 获取当前工作目录
|
||
current_directory = os.getcwd()
|
||
print("当前工作目录:", current_directory)
|
||
# 设置新的工作目录
|
||
new_directory = "C:/simnow_trader/traderdata"
|
||
os.chdir(new_directory)
|
||
# 验证新的工作目录
|
||
updated_directory = os.getcwd()
|
||
print("已更改为新的工作目录:", updated_directory)
|
||
|
||
# 获取当前文件夹中所有包含"ofdata"字符的CSV文件
|
||
def get_csv_files():
|
||
files = {}
|
||
for filename in os.listdir():
|
||
if "ofdata" in filename and filename.endswith(".csv"):
|
||
files[filename] = os.path.join(os.getcwd(), filename)
|
||
return files
|
||
|
||
|
||
# 根据文件路径加载数据,只读取前12列
|
||
def load_data(file_path):
|
||
df = pd.read_csv(file_path, usecols=range(12)) # 只读取前12列
|
||
df["delta"] = df["delta"].astype(float)
|
||
df['datetime'] = pd.to_datetime(df['datetime'], format='mixed')
|
||
df['delta累计'] = df.groupby(df['datetime'].dt.date)['delta'].cumsum()
|
||
df['终极平滑值'],df['趋势方向'] = ultimate_smoother(df["close"],48)
|
||
df['datetime'] = df['datetime'].dt.strftime("%Y-%m-%d %H:%M:%S")
|
||
df['POC'] = add_poc_column(df)
|
||
return df.tail(20).to_dict(orient="records")
|
||
|
||
def safe_literal_eval(x):
|
||
"""带异常处理的安全转换"""
|
||
try:
|
||
return ast.literal_eval(x)
|
||
except:
|
||
return [] # 返回空列表作为占位符
|
||
|
||
def add_poc_column(df):
|
||
# 安全转换列数据
|
||
df['price'] = df['price'].apply(safe_literal_eval)
|
||
df['Ask'] = df['Ask'].apply(lambda x: list(map(int, safe_literal_eval(x))))
|
||
df['Bid'] = df['Bid'].apply(lambda x: list(map(int, safe_literal_eval(x))))
|
||
|
||
# 定义处理函数(带数据验证)
|
||
def find_poc(row):
|
||
# 验证三个列表长度一致且非空
|
||
if not (len(row['price']) == len(row['Ask']) == len(row['Bid']) > 0):
|
||
return 0 # 返回空值标记异常数据
|
||
|
||
sums = [a + b for a, b in zip(row['Ask'], row['Bid'])]
|
||
try:
|
||
max_index = sums.index(max(sums))
|
||
return row['price'][max_index]
|
||
except ValueError:
|
||
return 0 # 处理空求和列表情况
|
||
|
||
# 应用处理函数
|
||
df['POC'] = df.apply(find_poc, axis=1)
|
||
|
||
# 可选:统计异常数据
|
||
error_count = df['POC'].isnull().sum()
|
||
if error_count > 0:
|
||
print(f"警告:发现 {error_count} 行异常数据(已标记为NaN)")
|
||
|
||
return df['POC']
|
||
|
||
|
||
def ultimate_smoother(price, period):
|
||
# 初始化变量(修正角度单位为弧度)
|
||
a1 = np.exp(-1.414 * np.pi / period)
|
||
b1 = 2 * a1 * np.cos(1.414 * np.pi / period) # 将180改为np.pi
|
||
c2 = b1
|
||
c3 = -a1 ** 2
|
||
c1 = (1 + c2 - c3) / 4
|
||
|
||
# 准备输出序列
|
||
us = np.zeros(len(price))
|
||
us_new = np.zeros(len(price))
|
||
trend = [None]*(len(price))
|
||
ma_close = np.zeros(len(price))
|
||
|
||
# 前4个点用原始价格初始化
|
||
for i in range(len(price)):
|
||
if i < 4:
|
||
us[i] = price[i]
|
||
else:
|
||
# 应用递归公式
|
||
us[i] = (1 - c1) * price[i] + (2 * c1 - c2) * price[i-1] \
|
||
- (c1 + c3) * price[i-2] + c2 * us[i-1] + c3 * us[i-2]
|
||
|
||
us_new = np.around(us, decimals=2)
|
||
ma_close = price.rolling(window=period).mean()
|
||
|
||
if us_new[i]>price[i] and ma_close[i]>price[i]:
|
||
trend[i] = '空头趋势'
|
||
elif us_new[i]<price[i] and ma_close[i]<price[i]:
|
||
trend[i] = '多头趋势'
|
||
else:
|
||
trend[i] = '无趋势'
|
||
|
||
|
||
return us_new,trend
|
||
|
||
|
||
@app.route("/")
|
||
def index():
|
||
files = get_csv_files() # 获取所有符合条件的文件
|
||
# 默认显示第一个文件的数据
|
||
first_file = list(files.keys())[0] if files else None
|
||
data = load_data(files[first_file]) if first_file else []
|
||
return render_template("index.html", data=data, files=files, file_name=first_file)
|
||
|
||
|
||
@app.route("/data/<file_name>")
|
||
def switch_data(file_name):
|
||
files = get_csv_files() # 获取所有符合条件的文件
|
||
if file_name in files:
|
||
data = load_data(files[file_name])
|
||
return jsonify(data)
|
||
return jsonify({"error": "File not found"}), 404
|
||
|
||
|
||
if __name__ == "__main__":
|
||
app.run(host='0.0.0.0', port=5000, debug=True) # 监听所有网络接口
|