简体   繁体   English

试图使用python计算EMA,但我无法弄清楚为什么我的代码总是产生相同的结果

[英]Trying to calculate EMA using python and i cant figure out why my code is always producing the same result

I am trying to calculate an exponential moving average of bitcoin in python2.7 but my result is always the same value and I have no idea why. 我正在尝试计算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  

This is how to calculate the EMA: {Close - EMA(previous day)} x multiplier + EMA(previous day) you seed the formula with a simple moving average. 这是计算EMA的方法:{Close-EMA(前一天)} x乘数+ EMA(前一天)为您使用简单的移动平均数播种公式。

Doing this in Excel works so might it be my use of variables? 在Excel中做到这一点,是否可能是我对变量的使用?

I would be really glad if someone could tell me what I am doing wrong because I have failed on this simple problem for hours and can't figure it out I've tried storing my previous ema in a separate variable and I even stored all of them in a list but I am always getting the same values at every timestep. 如果有人可以告诉我我做错了什么,我将非常高兴,因为我在这个简单的问题上已经失败了几个小时,并且无法弄清楚,我已经尝试将以前的ema存储在一个单独的变量中,甚至将所有它们在列表中,但我总是在每个时间步上获得相同的值。

The expression 2/(timeframe+1) is always zero, because the components are all integers and therefore Python 2 uses integer division. 表达式2/(timeframe+1)始终为零,因为组件都是整数,因此Python 2使用整数除法。 Wrapping that result in float() does no good; 包装导致float()结果没有好处; you just get 0.0 instead of 0 . 您只得到0.0而不是0

Try 2.0/(timeframe+1) instead. 尝试改为2.0/(timeframe+1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不明白为什么我的 python 代码没有以英里为单位返回答案 - I cant figure out why my python code isn't returning the answer in miles 我的代码无法完全运行在字典中,我无法弄清楚为什么 - My code wont fully run through my dictionary and i cant figure out why 我只是想不出为什么在将int()添加到输入时我的代码会引发语法错误 - I just cant figure out why my code throws a syntax error when I add int() to a input 我的表单没有显示字段,我无法弄清楚为什么 - My form is not displaying fields and I cant figure out why 我无法弄清楚我的 python 游戏一直崩溃 - i cant figure out my python game keep crashing 我有这个 python 代码,我试图将其转换为 javascript 但我似乎无法弄清楚是哪个 if 语句。 使用 - I have this python code that I am trying to turn into javascript but I cant seem to figure out which if statement. to use 我是 python 的新手,我不知道如何在我的“计算机”代码上输入密码,我把代码放给你观察 - Im new to python And i cant figure out how to put a password on my "COMPUTER" code ILL put the code for you to observe 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 这段代码有什么问题? 我想不通 - What is wrong with this code? I cant figure it out 我无法弄清楚这段代码有什么问题 - i cant figure out what is wrong in this code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM