1

我需要能够通过指示输赢来确定特定的“交易”(由“信号”表示)是否导致了盈利或亏损。我需要 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
4

3 回答 3

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'

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

于 2012-07-23T06:10:21.523 回答
1

有时你在做整数运算,有时是浮点运算。这可能会给您带来看似疯狂的结果。尝试类似:

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

等等。

于 2012-07-23T03:59:34.840 回答
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)
于 2012-07-23T05:05:17.963 回答