如何将使用不同参数运行相同计算的代码简化为不同的输出变量?
How can I simplify code that runs the same calculation with different parameters into different output variables?
我有一个数据框 df,它包含 140 万行数据,其中每行代表 BTC 从 2018 年到 2020 年 1 分钟的开盘价、最高价、最低价和收盘价。我想添加 MACD(热门交易指标)到我的 df,而不是像这样只计算 1 分钟时间范围内的 macd:
ShortEMA = df.Close.ewm(span=12, adjust=False).mean()
LongEMA = df.Close.ewm(span=26, adjust=False).mean()
MACD = ShortEMA - LongEMA
signal = MACD.ewm(span=9, adjust=False).mean()
df["MACD"] = MACD
df["Signal Line"] = signal
我想计算 1 分钟、15 分钟、30 分钟、1 小时等每个时间范围的 MACD...
我用以下代码(花了很长时间)做到了这一点:
MySet = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
ShortEMA1 = df.Close.ewm(span=12 * MySet[0], adjust=False).mean()
LongEMA1 = df.Close.ewm(span=26 * MySet[0], adjust=False).mean()
MACD1 = ShortEMA1 - LongEMA1
signal1 = MACD.ewm(span=9 * MySet[0], adjust=False).mean()
ShortEMA5 = df.Close.ewm(span=12 * MySet[1], adjust=False).mean()
LongEMA5 = df.Close.ewm(span=26 * MySet[1], adjust=False).mean()
MACD5 = ShortEMA5 - LongEMA5
signal5 = MACD.ewm(span=9 * MySet[1], adjust=False).mean()
ShortEMA15 = df.Close.ewm(span=12 * MySet[2], adjust=False).mean()
LongEMA15 = df.Close.ewm(span=26 * MySet[2], adjust=False).mean()
MACD15 = ShortEMA15 - LongEMA15
signal15 = MACD.ewm(span=9 * MySet[2], adjust=False).mean()
ShortEMA30 = df.Close.ewm(span=12 * MySet[3], adjust=False).mean()
LongEMA30 = df.Close.ewm(span=26 * MySet[3], adjust=False).mean()
MACD30 = ShortEMA30 - LongEMA30
signal30 = MACD.ewm(span=9 * MySet[3], adjust=False).mean()
ShortEMA60 = df.Close.ewm(span=12 * MySet[4], adjust=False).mean()
LongEMA60 = df.Close.ewm(span=26 * MySet[4], adjust=False).mean()
MACD60 = ShortEMA60 - LongEMA60
signal60 = MACD.ewm(span=9 * MySet[4], adjust=False).mean()
ShortEMA240 = df.Close.ewm(span=12 * MySet[5], adjust=False).mean()
LongEMA240 = df.Close.ewm(span=26 * MySet[5], adjust=False).mean()
MACD240 = ShortEMA240 - LongEMA240
signal240 = MACD.ewm(span=9 * MySet[5], adjust=False).mean()
ShortEMA360 = df.Close.ewm(span=12 * MySet[6], adjust=False).mean()
LongEMA360 = df.Close.ewm(span=26 * MySet[6], adjust=False).mean()
MACD360 = ShortEMA360 - LongEMA360
signal360 = MACD.ewm(span=9 * MySet[6], adjust=False).mean()
ShortEMA720 = df.Close.ewm(span=12 * MySet[7], adjust=False).mean()
LongEMA720 = df.Close.ewm(span=26 * MySet[7], adjust=False).mean()
MACD720 = ShortEMA720 - LongEMA720
signal720 = MACD.ewm(span=9 * MySet[7], adjust=False).mean()
ShortEMA1440 = df.Close.ewm(span=12 * MySet[8], adjust=False).mean()
LongEMA1440 = df.Close.ewm(span=26 * MySet[8], adjust=False).mean()
MACD1440 = ShortEMA1440 - LongEMA1440
signal1440 = MACD.ewm(span=9 * MySet[8], adjust=False).mean()
ShortEMA10080 = df.Close.ewm(span=12 * MySet[9], adjust=False).mean()
LongEMA10080 = df.Close.ewm(span=26 * MySet[9], adjust=False).mean()
MACD10080 = ShortEMA10080 - LongEMA10080
signal10080 = MACD.ewm(span=9 * MySet[9], adjust=False).mean()
df["MACD1"] = MACD1
df["Signal Line1"] = signal1
df["MACD5"] = MACD1
df["Signal Line5"] = signal5
df["MACD15"] = MACD1
df["Signal Line15"] = signal15
df["MACD30"] = MACD1
df["Signal Line30"] = signal30
df["MACD60"] = MACD60
df["Signal Line60"] = signal60
df["MACD240"] = MACD240
df["Signal Line240"] = signal240
df["MACD360"] = MACD360
df["Signal Line360"] = signal360
df["MACD720"] = MACD720
df["Signal Line720"] = signal720
df["MACD1440"] = MACD1440
df["Signal Line1440"] = signal1440
df["MACD10080"] = MACD10080
df["Signal Line10080"] = signal10080
如何简化整个过程?
与其让 Short1
和 Short5
成为单独的变量,不如有 one Shorts
字典,并且有 1
、5
等是键。因此:
MySet = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
Shorts = {}
Longs = {}
MACDs = {}
Signals = {}
for val in MySet:
Shorts[val] = df.Close.ewm(span=12 * val, adjust=False).mean()
Longs[val] = df.Close.ewm(span=26 * val, adjust=False).mean()
MACDs[val] = Shorts[val] - Longs[val]
Signals[val] = MACDs[val].ewm(span=9 * val, adjust=False).mean()
df[f'MACD{val}'] = MACDs[val]
df[f'Signal Line{val}'] = Signals[val]
如果唯一的持久输出是存储在 DataFrame
中的值,而中间值 Series
以后不会被使用或重新使用,那么简单地更新 DataFrame 和在每次迭代中分配具有 f-string
的列:
MySet = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
for val in MySet:
ShortEMA = df.Close.ewm(span=12 * val, adjust=False).mean()
LongEMA = df.Close.ewm(span=26 * val, adjust=False).mean()
df[f"MACD{val}"] = ShortEMA - LongEMA
df[f"Signal Line{val}"] = df[f"MACD{val}"].ewm(span=9 * val, adjust=False).mean()
如果稍后需要访问值,可以通过 DataFrame
.
访问它们
我有一个数据框 df,它包含 140 万行数据,其中每行代表 BTC 从 2018 年到 2020 年 1 分钟的开盘价、最高价、最低价和收盘价。我想添加 MACD(热门交易指标)到我的 df,而不是像这样只计算 1 分钟时间范围内的 macd:
ShortEMA = df.Close.ewm(span=12, adjust=False).mean()
LongEMA = df.Close.ewm(span=26, adjust=False).mean()
MACD = ShortEMA - LongEMA
signal = MACD.ewm(span=9, adjust=False).mean()
df["MACD"] = MACD
df["Signal Line"] = signal
我想计算 1 分钟、15 分钟、30 分钟、1 小时等每个时间范围的 MACD...
我用以下代码(花了很长时间)做到了这一点:
MySet = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
ShortEMA1 = df.Close.ewm(span=12 * MySet[0], adjust=False).mean()
LongEMA1 = df.Close.ewm(span=26 * MySet[0], adjust=False).mean()
MACD1 = ShortEMA1 - LongEMA1
signal1 = MACD.ewm(span=9 * MySet[0], adjust=False).mean()
ShortEMA5 = df.Close.ewm(span=12 * MySet[1], adjust=False).mean()
LongEMA5 = df.Close.ewm(span=26 * MySet[1], adjust=False).mean()
MACD5 = ShortEMA5 - LongEMA5
signal5 = MACD.ewm(span=9 * MySet[1], adjust=False).mean()
ShortEMA15 = df.Close.ewm(span=12 * MySet[2], adjust=False).mean()
LongEMA15 = df.Close.ewm(span=26 * MySet[2], adjust=False).mean()
MACD15 = ShortEMA15 - LongEMA15
signal15 = MACD.ewm(span=9 * MySet[2], adjust=False).mean()
ShortEMA30 = df.Close.ewm(span=12 * MySet[3], adjust=False).mean()
LongEMA30 = df.Close.ewm(span=26 * MySet[3], adjust=False).mean()
MACD30 = ShortEMA30 - LongEMA30
signal30 = MACD.ewm(span=9 * MySet[3], adjust=False).mean()
ShortEMA60 = df.Close.ewm(span=12 * MySet[4], adjust=False).mean()
LongEMA60 = df.Close.ewm(span=26 * MySet[4], adjust=False).mean()
MACD60 = ShortEMA60 - LongEMA60
signal60 = MACD.ewm(span=9 * MySet[4], adjust=False).mean()
ShortEMA240 = df.Close.ewm(span=12 * MySet[5], adjust=False).mean()
LongEMA240 = df.Close.ewm(span=26 * MySet[5], adjust=False).mean()
MACD240 = ShortEMA240 - LongEMA240
signal240 = MACD.ewm(span=9 * MySet[5], adjust=False).mean()
ShortEMA360 = df.Close.ewm(span=12 * MySet[6], adjust=False).mean()
LongEMA360 = df.Close.ewm(span=26 * MySet[6], adjust=False).mean()
MACD360 = ShortEMA360 - LongEMA360
signal360 = MACD.ewm(span=9 * MySet[6], adjust=False).mean()
ShortEMA720 = df.Close.ewm(span=12 * MySet[7], adjust=False).mean()
LongEMA720 = df.Close.ewm(span=26 * MySet[7], adjust=False).mean()
MACD720 = ShortEMA720 - LongEMA720
signal720 = MACD.ewm(span=9 * MySet[7], adjust=False).mean()
ShortEMA1440 = df.Close.ewm(span=12 * MySet[8], adjust=False).mean()
LongEMA1440 = df.Close.ewm(span=26 * MySet[8], adjust=False).mean()
MACD1440 = ShortEMA1440 - LongEMA1440
signal1440 = MACD.ewm(span=9 * MySet[8], adjust=False).mean()
ShortEMA10080 = df.Close.ewm(span=12 * MySet[9], adjust=False).mean()
LongEMA10080 = df.Close.ewm(span=26 * MySet[9], adjust=False).mean()
MACD10080 = ShortEMA10080 - LongEMA10080
signal10080 = MACD.ewm(span=9 * MySet[9], adjust=False).mean()
df["MACD1"] = MACD1
df["Signal Line1"] = signal1
df["MACD5"] = MACD1
df["Signal Line5"] = signal5
df["MACD15"] = MACD1
df["Signal Line15"] = signal15
df["MACD30"] = MACD1
df["Signal Line30"] = signal30
df["MACD60"] = MACD60
df["Signal Line60"] = signal60
df["MACD240"] = MACD240
df["Signal Line240"] = signal240
df["MACD360"] = MACD360
df["Signal Line360"] = signal360
df["MACD720"] = MACD720
df["Signal Line720"] = signal720
df["MACD1440"] = MACD1440
df["Signal Line1440"] = signal1440
df["MACD10080"] = MACD10080
df["Signal Line10080"] = signal10080
如何简化整个过程?
与其让 Short1
和 Short5
成为单独的变量,不如有 one Shorts
字典,并且有 1
、5
等是键。因此:
MySet = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
Shorts = {}
Longs = {}
MACDs = {}
Signals = {}
for val in MySet:
Shorts[val] = df.Close.ewm(span=12 * val, adjust=False).mean()
Longs[val] = df.Close.ewm(span=26 * val, adjust=False).mean()
MACDs[val] = Shorts[val] - Longs[val]
Signals[val] = MACDs[val].ewm(span=9 * val, adjust=False).mean()
df[f'MACD{val}'] = MACDs[val]
df[f'Signal Line{val}'] = Signals[val]
如果唯一的持久输出是存储在 DataFrame
中的值,而中间值 Series
以后不会被使用或重新使用,那么简单地更新 DataFrame 和在每次迭代中分配具有 f-string
的列:
MySet = [1, 5, 15, 30, 60, 240, 360, 720, 1440, 10080]
for val in MySet:
ShortEMA = df.Close.ewm(span=12 * val, adjust=False).mean()
LongEMA = df.Close.ewm(span=26 * val, adjust=False).mean()
df[f"MACD{val}"] = ShortEMA - LongEMA
df[f"Signal Line{val}"] = df[f"MACD{val}"].ewm(span=9 * val, adjust=False).mean()
如果稍后需要访问值,可以通过 DataFrame
.