|
import datetime |
|
import pandas as pd |
|
import os |
|
from pymongo import MongoClient |
|
import choose |
|
|
|
|
|
def technology(df): |
|
try: |
|
df=df.dropna() |
|
df=df.sort_values(by="日期") |
|
df["涨跌幅"] = df["收盘"]/df["收盘"].copy().shift(1) - 1 |
|
df["振幅"] = (df["最高"].copy()-df["最低"].copy())/df["开盘"].copy() |
|
df["资金波动"] = df["振幅"] / df["成交额"] |
|
df["资金贡献"] = df["涨跌幅"] / df["成交额"] |
|
for n in range(1, 5): |
|
df[f"过去{n}日成交额"] = df["成交额"].shift(n) |
|
df[f"过去{n}日资金波动"] = df["资金波动"].shift(n) |
|
df[f"过去{n}日资金贡献"] = df["资金贡献"].shift(n) |
|
df[f"过去{n}日总涨跌"] = df["开盘"]/(df["开盘"].copy().shift(n)) |
|
except Exception as e: |
|
print(f"发生bug: {e}") |
|
return df |
|
|
|
|
|
def tradelist(name): |
|
collection = db[f"{name}"] |
|
|
|
if ("股票" in name) and ("可转债" not in name): |
|
watchtime = 2017 |
|
df = pd.DataFrame(list(collection.find({"日期": {"$gt": datetime.datetime( |
|
watchtime, 1, 1).strftime("%Y-%m-%d")}}))).drop('_id', axis=1) |
|
|
|
df = df.groupby("代码", group_keys=False).apply(technology) |
|
df = df.groupby("日期", group_keys=False).apply(choose.rank) |
|
|
|
new_collection = db[f"{name}{watchtime}指标"] |
|
else: |
|
df = pd.DataFrame(list(collection.find())).drop('_id', axis=1) |
|
|
|
df = df.groupby("代码", group_keys=False).apply(technology) |
|
df = df.groupby("日期", group_keys=False).apply(choose.rank) |
|
|
|
new_collection = db[f"{name}指标"] |
|
new_collection.drop() |
|
|
|
file_path = os.path.abspath(__file__) |
|
|
|
dir_path = os.path.dirname(file_path) |
|
|
|
dir_path = os.path.dirname(os.path.dirname(dir_path)) |
|
|
|
file_path = os.path.join(dir_path, f"{name}指标.csv") |
|
df.to_csv(file_path, index=False) |
|
print(f"{name}准备插入数据") |
|
new_collection.insert_many(df.to_dict("records")) |
|
print(f"{name}数据插入结束") |
|
|
|
|
|
|
|
client = MongoClient( |
|
"mongodb://wth000:wth000@43.159.47.250:27017/dbname?authSource=wth000") |
|
db = client["wth000"] |
|
|
|
names = list(db.list_collection_names()) |
|
print(names) |
|
for name in names: |
|
if ("指标" not in name) & ("实盘" not in name) & ("聚宽" not in name) & ("测试" not in name) & ("ST" not in name) & ("order" not in name) & ("js" not in name): |
|
|
|
|
|
|
|
if ("ETF" in name) | ("COIN" in name): |
|
|
|
print(f"当前计算{name}") |
|
try: |
|
tradelist(name) |
|
except Exception as e: |
|
print(f"tradelist发生bug: {e}") |
|
|