|
import choose |
|
from pymongo import MongoClient |
|
import pandas as pd |
|
import numpy as np |
|
import datetime |
|
import os |
|
|
|
client = MongoClient( |
|
"mongodb://wth000:[email protected]:27017/dbname?authSource=wth000") |
|
db = client["wth000"] |
|
|
|
|
|
names = ["COIN",] |
|
mubiao = f"{5}日量比" |
|
a = 20 |
|
|
|
file_path = os.path.abspath(__file__) |
|
|
|
dir_path = os.path.dirname(file_path) |
|
|
|
dir_path = os.path.dirname(os.path.dirname(dir_path)) |
|
files = os.listdir(dir_path) |
|
for file in files: |
|
for filename in names: |
|
if (filename in file): |
|
try: |
|
print(f"{mubiao}") |
|
|
|
name, extension = os.path.splitext(file) |
|
path = os.path.join(dir_path, f"{name}.csv") |
|
print(name) |
|
df = pd.read_csv(path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watchtime = 1999 |
|
|
|
|
|
|
|
|
|
|
|
df = df.dropna() |
|
|
|
|
|
df = df[df["成交量"] == df[f"{40}日成交量高点"]] |
|
|
|
|
|
|
|
|
|
df, m, n = choose.choose(name, df) |
|
if ("股票" in name): |
|
for i in range(1, n+1): |
|
df = df[df[f"{i}日后总涨跌幅(未来函数)"] <= 3*(1+0.1*n)] |
|
else: |
|
for i in range(1, n+1): |
|
df = df[df[f"{i}日后总涨跌幅(未来函数)"] <= 20*(1+0.1*n)] |
|
df = df.dropna() |
|
df.to_csv(f"实际统计数据{name}_{mubiao}_{watchtime}年.csv") |
|
sorted_data = np.sort(df[f"{mubiao}"]) |
|
indices = np.linspace( |
|
0, len(df[f"{mubiao}"]), num=a+1, endpoint=True, dtype=int) |
|
|
|
ranges = [] |
|
for i in range(len(indices) - 1): |
|
start_idx = indices[i] |
|
end_idx = indices[i+1] if i != len(indices) - \ |
|
2 else len(df[f"{mubiao}"]) |
|
upper_bound = sorted_data[end_idx-1] |
|
ranges.append((sorted_data[start_idx], upper_bound)) |
|
result_dicts = [] |
|
day = n |
|
for n in range(1, day): |
|
for rank_range in ranges: |
|
sub_df = df.copy()[(df[f"{mubiao}"] >= rank_range[0]) & |
|
(df[f"{mubiao}"] <= rank_range[1])] |
|
future_returns = np.array(sub_df[f"{n}日后总涨跌幅(未来函数)"]) |
|
|
|
up_rate = len( |
|
future_returns[future_returns >= 0]) / len(future_returns) |
|
avg_return = np.mean(future_returns) |
|
result_dict = { |
|
f"{mubiao}": f"from{rank_range[0]}to{rank_range[1]}", |
|
f"未来{n}日上涨次数": len(future_returns[future_returns >= 0]), |
|
f"未来{n}日上涨概率": up_rate, |
|
f"未来{n}日平均涨跌幅": avg_return, |
|
} |
|
result_dicts.append(result_dict) |
|
|
|
result_df = pd.DataFrame(result_dicts) |
|
for n in range(1, day): |
|
cols_to_shift = [f"未来{n}日上涨概率", |
|
f"未来{n}日上涨次数", f"未来{n}日平均涨跌幅"] |
|
result_df[cols_to_shift] = result_df[cols_to_shift].shift( |
|
-a*(n-1)) |
|
|
|
path = os.path.join(os.path.abspath("."), "资产单指标平均收益分布") |
|
if not os.path.exists(path): |
|
os.makedirs(path) |
|
result_df.round(decimals=6).to_csv( |
|
f"{path}/{name}{mubiao}持有{n}日{str(watchtime)}平均收益分布.csv", index=False) |
|
print(name, "已完成") |
|
except Exception as e: |
|
print(f"发生bug: {e}") |
|
|