Python:确定盈亏

时间:2012-07-23 03:38:13

标签: python numerical stocks

我需要能够确定特定的“交易”(由“信号”表示)是否通过表明赢或输而导致盈利或亏损。我需要Python检查“高”和“低”列表中的下一个位置,以便在超出输入信号的某个点上增加值等于或大于2.5%。但是,我还希望Python确定在升值2.5%或更多之前该值是否下降3%或更多。不幸的是,到目前为止我开发的代码似乎没有用。我错过了什么?

Signal = [1,5,7]
Close = [5,10,10,10.5,11,12,11.9,14,14,15,16]
High =  [7,10.2,10.1,11,12,12.1,12.2,14.5,18,19,20]
Low =   [4,9.9,9.8,10,10,11.8,11.8,12,13.8,13.85,14]

for i in Signal:
    Entry = []
    Entry.append(Close[i])
    for Ent in Entry:
        print [Ent]
        for Value in High[i+1:]:
            Profit = ((Value - Ent)/Ent)*100
        for Value in Low[i+1:]:
            Loss = ((Value - Ent)/Ent)*100
        while (abs(Loss) < 3):
            if Profit >= 2.5:
                print 'Win'
                break
            else:
                print 'Loss'
                break

3 个答案:

答案 0 :(得分:1)

有时你正在进行整数运算,有时候是浮点运算。这可能会给你看似疯狂的结果。尝试类似:

Minimum = map(float, [1, 5, 7])

答案 1 :(得分:1)

不太确定你要做什么。这里有很多代码没有意义:

    Entry = []
    Entry.append(Close[i])

可以替换为

    Entry = [Close[i]]

这些线就在这里?

        for Value in High[i+1:]:
            Profit = ((Value - Ent)/Ent)*100

在语义上与

相同
        Profit = ((High[-1] - Ent) / Ent) * 100

同样的事情:

        for Value in Low[i+1:]:
            Loss = ((Value - Ent)/Ent)*100

他们的意思是,基本上:

        Loss = ((Low[-1] - Ent) / Ent) * 100

至于此:

        while (abs(Loss) < 3):
            if Profit >= 2.5:
                print 'Win'
                break
            else:
                print 'Loss'
                break

可以替换为:

        if abs(Loss) < 3:
            if Profit >= 2.5:
                print 'Win'
            else:
                print 'Loss'

当你把它们放在一起时会发生什么:

for i in Signal:
    Entry = Close[i]
    print [Entry]
    Profit = ((High[-1] - Entry)/Entry)*100
    Loss = ((Low[-1] - Entry)/Entry)*100
    if abs(Loss) < 3:
        if Profit >= 2.5:
            print 'Win'
        else:
            print 'Loss'

仍然没有多大意义,是吗?这基本上就是您的代码所做的事情。如果你需要我的建议,我会废弃整件事并重新开始。

答案 2 :(得分:0)

这是对您的代码的自由重新解释以及您对代码的想象的描述,坦率地说我并不理解。我确实看到你的代码在合理计算方面做的很少,这让我觉得这种重新解释可能不会伤害任何东西,即使它最终没有解决你的问题。

from __future__ import division

signals = [1, 5, 7]
closes = [5, 10, 10, 10.5, 11, 12, 11.9, 14, 14, 15, 16]
highs = [7, 10.2, 10.1, 11, 12, 12.1, 12.2, 14.5, 18, 19, 20]
lows = [4, 9.9, 9.8, 10, 10, 11.8, 11.8, 12, 13.8, 13.85, 14]

for i in signals:
    print "TESTING SIGNAL %d" % i

    entry = closes[i]
    profit_pos = []
    loss_pos = []

    for j in range(i+1, len(highs)):
        # I'm assuming highs and lows are always same length ...
        profit = ((highs[j] - entry) / entry) * 100
        if profit >= 2.5:
            print "Signal %d profit: greater than or equal to 2.5%% at position %d!" % (i, j)
            profit_pos.append(j)

        loss = ((lows[j] - entry) / entry) * 100
        if abs(loss) < 3:
            print "Signal %d loss: less than 3%% at position %d!" % (i, j)
            loss_pos.append(j)

    profit_status = "Value does not drop before profit."
    if len(loss_pos) > 0:
        if len(profit_pos) == 0:
            profit_status = "Value drops, no profit."
        elif min(loss_pos) < min(profit_pos):
            profit_status = "Value drops before profit."
    else:
        if len(profit_pos) == 0:
            profit_status = "No significant profit or loss found."
    print "Signal %d result: %s" % (i, profit_status)