一个万能指标公式,代码简洁且功能强大

import pandas as pd
import numpy as np
class UniversalIndicator:
def __init__(self, data):
self.data = data
self.close = data[\’close\’] if isinstance(data, pd.DataFrame) else data
def sma(self, period=20):
\”\”\”简单移动平均\”\”\”
return self.close.rolling(window=period).mean()
def ema(self, period=12):
\”\”\”指数移动平均\”\”\”
return self.close.ewm(span=period, adjust=False).mean()
def rsi(self, period=14):
\”\”\”相对强弱指数\”\”\”
delta = self.close.diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(window=period).mean()
avg_loss = loss.rolling(window=period).mean()
rs = avg_gain / avg_loss
return 100 – (100 / (1 + rs))
def macd(self, fast=12, slow=26, signal=9):
\”\”\”MACD指标\”\”\”
ema_fast = self.close.ewm(span=fast).mean()
ema_slow = self.close.ewm(span=slow).mean()
macd_line = ema_fast – ema_slow
signal_line = macd_line.ewm(span=signal).mean()
histogram = macd_line – signal_line
return macd_line, signal_line, histogram
def bollinger_bands(self, period=20, std=2):
\”\”\”布林带\”\”\”
sma = self.sma(period)
rolling_std = self.close.rolling(window=period).std()
upper = sma + (rolling_std * std)
lower = sma – (rolling_std * std)
return upper, sma, lower
def stochastic(self, k_period=14, d_period=3):
\”\”\”随机指标\”\”\”
low_min = self.data[\’low\’].rolling(window=k_period).min()
high_max = self.data[\’high\’].rolling(window=k_period).max()
k_line = 100 * ((self.close – low_min) / (high_max – low_min))
d_line = k_line.rolling(window=d_period).mean()
return k_line, d_line
def atr(self, period=14):
\”\”\”平均真实波幅\”\”\”
high = self.data[\’high\’]#oc #算法 #量化
import numpy as np
class UniversalIndicator:
def __init__(self, data):
self.data = data
self.close = data[\’close\’] if isinstance(data, pd.DataFrame) else data
def sma(self, period=20):
\”\”\”简单移动平均\”\”\”
return self.close.rolling(window=period).mean()
def ema(self, period=12):
\”\”\”指数移动平均\”\”\”
return self.close.ewm(span=period, adjust=False).mean()
def rsi(self, period=14):
\”\”\”相对强弱指数\”\”\”
delta = self.close.diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(window=period).mean()
avg_loss = loss.rolling(window=period).mean()
rs = avg_gain / avg_loss
return 100 – (100 / (1 + rs))
def macd(self, fast=12, slow=26, signal=9):
\”\”\”MACD指标\”\”\”
ema_fast = self.close.ewm(span=fast).mean()
ema_slow = self.close.ewm(span=slow).mean()
macd_line = ema_fast – ema_slow
signal_line = macd_line.ewm(span=signal).mean()
histogram = macd_line – signal_line
return macd_line, signal_line, histogram
def bollinger_bands(self, period=20, std=2):
\”\”\”布林带\”\”\”
sma = self.sma(period)
rolling_std = self.close.rolling(window=period).std()
upper = sma + (rolling_std * std)
lower = sma – (rolling_std * std)
return upper, sma, lower
def stochastic(self, k_period=14, d_period=3):
\”\”\”随机指标\”\”\”
low_min = self.data[\’low\’].rolling(window=k_period).min()
high_max = self.data[\’high\’].rolling(window=k_period).max()
k_line = 100 * ((self.close – low_min) / (high_max – low_min))
d_line = k_line.rolling(window=d_period).mean()
return k_line, d_line
def atr(self, period=14):
\”\”\”平均真实波幅\”\”\”
high = self.data[\’high\’]#oc #算法 #量化
夜雨聆风
