股票公式是股市分析中常用的工具,可以帮助投资者分析股票的走势和预测未来的价格。下面是一些常见的股票公式的源码大全。
1. 移动平均线(Moving Average)
def moving_average(data, period):
ma_list = []
for i in range(period-1, len(data)):
ma = sum(data[i-period+1:i+1]) / period
ma_list.append(ma)
return ma_list
2. 相对强弱指标(Relative Strength Index,简称RSI)
def rsi(data, period):
rsi_list = []
for i in range(period-1, len(data)):
up_sum = 0
down_sum = 0
for j in range(i-period+1, i):
diff = data[j+1] - data[j]
if diff > 0:
up_sum += diff
else:
down_sum += abs(diff)
rs = up_sum / down_sum
rsi = 100 - (100 / (1 + rs))
rsi_list.append(rsi)
return rsi_list
3. 布林线(Bollinger Bands)
def bollinger_bands(data, period, k):
bb_upper = []
bb_middle = []
bb_lower = []
for i in range(period-1, len(data)):
std = statistics.stdev(data[i-period+1:i+1])
ma = sum(data[i-period+1:i+1]) / period
upper = ma + k * std
middle = ma
lower = ma - k * std
bb_upper.append(upper)
bb_middle.append(middle)
bb_lower.append(lower)
return bb_upper, bb_middle, bb_lower
4. 相对强弱指数(Relative Strength Index,简称RSI)
def rsi(data, period):
rsi_list = []
for i in range(period-1, len(data)):
up_sum = 0
down_sum = 0
for j in range(i-period+1, i):
diff = data[j+1] - data[j]
if diff > 0:
up_sum += diff
else:
down_sum += abs(diff)
rs = up_sum / down_sum
rsi = 100 - (100 / (1 + rs))
rsi_list.append(rsi)
return rsi_list
5. 相对强弱指数平均线(Relative Strength Index Moving Average,简称RSIMA)
def rsima(data, period, ma_period):
rsi_list = rsi(data, period)
rsima_list = moving_average(rsi_list, ma_period)
return rsima_list
以上是一些常见的股票公式的源码大全,希望对股市分析和投资有所帮助。