基于类的方向指示器 [英] Class based directional indicator

查看:25
本文介绍了基于类的方向指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基于类的方向指示器,它给出了天数 (n_days) 和一个数字列表,它给出了(最近 n_days 中的数字数 上的数字高于前一个数字减去前一个 n_days 中数字下降的 n_days).所以如果列表中的数字增加我希望它返回+1,如果减少我希望它返回-1,否则它应该是0 所以第一个数字应该总是 0 因为你不能将它与任何东西进行比较.然后基于 n_days 我基本上想取最近 n_days 的总和,例如在 [1,2,2,1,2,1] 的列表中code> 更改应该是 [0,+1,0,-1,1,-1] 并且如果我想要每天 2 个最近数字的更改总和,那么它应该是[0,+1,-1,0,+1,0] 因为第一天只有0,第二天取最近两天的总和0+(+1)=1,第三天(+1)+0=+1,第四天0+(-1)=-1等等.这是我的代码不起作用:

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.此外,其中一个答案中的代码使用 zip 工作,但我不明白为什么我的不起作用对于 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

永远True.您正在将每个价格与自身减去一进行比较,后者将始终较小.相反,您应该比较价格对.zip 对此很有用:

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)

当您将其插入代码并使用示例时

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.当i == 0i-1 == -1.当用作索引 list_of_prices[-1] 时,它为您提供列表中的最后一个元素.因此change包含[-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆