|
import math |
|
import pandas as pd |
|
import talib |
|
|
|
|
|
def technology(df): |
|
slippage = 0.001 |
|
commission = 0.0013 |
|
try: |
|
df=df.sort_values(by="日期") |
|
if "换手率" in df.columns: |
|
df['涨跌幅(开盘原值)'] = df['涨跌幅(开盘)'].shift(-1) |
|
df['涨跌幅(原值)'] = df['涨跌幅']+1 |
|
for n in range(1, 30): |
|
if n == 1: |
|
df[f"{n}日后总涨跌幅(未来函数)"] = df['涨跌幅(原值)'].shift(-1)-1 |
|
if n > 1: |
|
df[f"{n}日后总涨跌幅(未来函数)"] = ( |
|
(df[f"{n-1}日后总涨跌幅(未来函数)"]+1)*df['涨跌幅(原值)'].shift(-n))-1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
df[f"{5}日量比"] = df["成交量"] / \ |
|
talib.MA(df["成交量"], timeperiod=5, matype=0) |
|
df[f"{40}日成交量低点"] = df['成交量'].rolling(40).min() |
|
df[f"{40}日成交量高点"] = df['成交量'].rolling(40).max() |
|
df[f"{5}日收盘高点"] = df['收盘'].rolling(40).min() |
|
|
|
else: |
|
for n in range(1, 50): |
|
|
|
df[f"{n}日后总涨跌幅(未来函数)"] = ( |
|
df["收盘"].copy().shift(-n) / df["收盘"]) - 1 |
|
df[f"{5}日量比"] = df["成交量"] / \ |
|
talib.MA(df["成交量"], timeperiod=5, matype=0) |
|
df[f"{40}日成交量低点"] = df['成交量'].rolling(40).min() |
|
df[f"{40}日成交量高点"] = df['成交量'].rolling(40).max() |
|
df[f"{5}日收盘高点"] = df['收盘'].rolling(40).min() |
|
|
|
except Exception as e: |
|
print(f"发生bug: {e}") |
|
return df |
|
|
|
|
|
def rank(df): |
|
|
|
for column in df.columns: |
|
if (("未来函数" or "日期" or "代码") not in str(column)): |
|
df = pd.concat([df, (df[str(column)].rank(method="max", ascending=False) / len(df)).rename(f"{str(column)}_rank")], axis=1) |
|
return df |
|
|
|
|
|
def choose(name, df): |
|
df = df.sort_values(by="日期") |
|
code = df[df["日期"] == df["日期"].min()]["代码"] |
|
print(name, "首日标的数量", len(code)) |
|
if ("股票" in name): |
|
df = df[(df["开盘"] >= 4)].copy() |
|
df = df[(df["涨跌幅(开盘原值)"] <= 0.09)].copy() |
|
m = 0.01 |
|
n = 25 |
|
if ("COIN" in name): |
|
m = 0.001 |
|
n = 25 |
|
print(name, n) |
|
return df, m, n |
|
|