试图用python计算EMA,我不明白为什么我的代码总是产生相同的结果

2024-09-28 22:55:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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存储在一个单独的变量中,我甚至把它们都存储在一个列表中,但我总是在每个时间步得到相同的值。你知道吗


Tags: datareturndefcounterfloatpricemultiplierema