基于类的方向指示器 [英] Class based directional indicator
问题描述
我创建了一个给定的天数基于类方向指示器( n_days
)和号码的列表,它给号码的(数出来的最近 n_days
上的数量比previous数量减去 n_days
出$的对更高$ pvious n_days
上的号码去了)。所以,如果我想它返回列表的数量增加 +1
,如果它减少我希望它返回 1
,否则应 0
所以第一个数字应始终是 0
,因为你不能把它比作任何东西。然后根据 n_days
我基本上要采取的近期n_days的总和,因此,例如在列表[1,2,2 ,1,2,1]
的变化应 [0,+ 1,0,1,1,-1]
,如果我希望每个日子,所以它应该是 [0,+ 1,-1,0,+ 1,0]
,因为对在2号最近变化的总和第一天只有0,第二天你把最新的2天 0 +(+ 1)= 1
,第三天<$ C $的总和C>(+ 1)+ 0 = + 1 ,第四天 0 +( - 1)= - 1
等等。这里是我的code,它不工作:
I'm creating a class based directional indicator that given a number of days (n_days
) and a list of numbers, it gives the (number of numbers out of the most recent n_days
on which the number was higher than the previous number minus the n_days
out of the previous n_days
on which the number went down). So if the number in the list increases I want it to return +1
, if it decreases I want it to return -1
, otherwise it should be 0
so the first number should always be 0
since you can't compare it to anything. Then based on n_days
I basically want to take the sum of the of the recent n_days, so for example in a list of [1,2,2,1,2,1]
the change should be [0,+1,0,-1,1,-1]
and if I want the sum of the change in the 2 recent numbers for each day so it should be [0,+1,-1,0,+1,0]
because on the first day there is only 0, on the second day you take the sum of the most recent two days 0+(+1)=1
, the third day (+1)+0=+1
, the fourth day 0+(-1)=-1
and so forth. Here is my code that's not working:
class Directionalindicator():
def __init__(self, n_days, list_of_prices):
self.n_days = n_days
self.list_of_prices = list_of_prices
def calculate(self):
change = []
for i in range(len(self.list_of_prices)):
if self.list_of_prices[i-1] < self.list_of_prices[i]:
change.append(1)
elif self.list_of_prices[i-1] > self.list_of_prices[i]:
change.append(-1)
else:
change.append(0)
directional = []
for i in range(len(change)):
directional.append(sum(change[i+1-self.n_days:i+1]))
return directional
与测试它:
y = Directionalindicator(2,[1,2,2,1,2,1])
y.calculate()
应该返回:
[0,+1,+1,-1,0,0]
和它。
但随着测试它:
y = Directionalindicator(3, [1,2,3,4,5,6,7,8,9,10])
y.calculate()
应该返回
[0, 0, 2, 3, 3, 3, 3, 3, 3, 3]
但它返回
[0, 0, 1, 3, 3, 3, 3, 3, 3, 3]
我打印的变化,看它是做什么,第一个值是-1,而不是0。此外,在回答一个code工作使用拉链,但我不明白为什么我的没有按T该列表1-10工作。
I printed change to see what it was doing and the first value is a -1 instead of a 0. Also, the code in one of the answers works using zip, but I don't understand why mine doesn't work for that list from 1-10.
推荐答案
您比较
i > i-1
将总是的是真
。你正在减一,这永远是较小每个价格比较本身。相反,你应该比较对的价格。 拉链
是有用的:
will always be True
. You are comparing each price to itself minus one, which will always be smaller. Instead, you should be comparing pairs of prices. zip
is useful for this:
change = [0] # first price always zero change
for a, b in zip(self.list_of_prices, self.list_of_prices[1:]):
if a < b: # price went up
change.append(1)
elif a > b: # price went down
change.append(-1)
else: # price stayed the same
change.append(0)
在此插入到你的code和使用您的例子
When you plug this into your code and use your example
Directionalindicator(2, [1, 2, 2, 1, 2, 1])
您可以:
change == [0, 1, 0, -1, 1, -1]
directional == [0, 1, 1, -1, 0, 0]
这似乎根据你的规则最初说法是正确的,但由于某些原因不符合你的期望的输出 [0,1,-1,0,1,0]
从你的问题的结束。
This seems to be correct according to your initial statement of the rules, but for some reason doesn't match your "expected output" [0, 1, -1, 0, 1, 0]
from the end of your question.
您的编辑不工作的原因是您正在使用的列表上的指数 I
。当我== 0
, I-1 == -1
。当作为索引使用 list_of_prices [-1]
,这让你在列表的最后一个元素。因此,变更
包含 [ - 1,1,1,1,1,1,1,1,1,1]
,因为它比较 1
与 10
,而不是 [0,1,1,1 ,1个,1个,1个,1个,1个,1]
如您所愿。
The reason your edit doesn't work is that you are using an index i
on the list. When i == 0
, i-1 == -1
. When used as an index list_of_prices[-1]
, this gives you the last element in the list. Therefore change
contains [-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
, as it compares 1
with 10
, not [0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
as you expected.
这篇关于基于类的方向指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!