我试图在python2.7中计算比特币的指数移动平均值,但我的结果总是相同的,我不知道为什么。你知道吗
def calcSMA(data,counter,timeframe):
closesum = 0
for i in range(timeframe):
closesum = float(closesum) + float(data[counter-i])
return float(closesum / timeframe)
def calcEMA(price,timeframe,prevema):
multiplier = float(2/(timeframe+1))
ema = ((float(price) - float(prevema))*multiplier) + float(prevema)
return float(ema)
counter = 0
closeprice = [7242.4,7240,7242.8,7253.8,7250.6,7255.7,7254.9,7251.4,7234.3,7237.4
,7240.7,7232,7230.2,7232.2,7236.1,7230.5,7230.5,7230.4,7236.4]
while counter < len(closeprice):
if counter == 3:
movingaverage = calcSMA(closeprice,counter,3)
print movingaverage
if counter > 3:
movingaverage = calcEMA(closeprice[counter],3,movingaverage)
print movingaverage
counter +=1
这是如何计算均线: {Close-EMA(前一天)}x乘数+EMA(前一天) 你可以用一个简单的移动平均数来建立这个公式。你知道吗
在Excel中这样做可能是因为我使用了变量?你知道吗
如果有人能告诉我我做错了什么,我会很高兴的,因为我在这个简单的问题上失败了好几个小时,也没有弄清楚我已经试着把我以前的ema存储在一个单独的变量中,我甚至把它们都存储在一个列表中,但我总是在每个时间步得到相同的值。你知道吗
表达式
2/(timeframe+1)
总是零,因为组件都是整数,因此python2使用整数除法。将结果包装成float()
是没有好处的;只会得到0.0
,而不是0
。你知道吗试试
2.0/(timeframe+1)
。你知道吗相关问题 更多 >
编程相关推荐