1

I need to be able to determine whether a particular "trade" (indicated by "signal") resulted in a profit or loss by indicating a win or loss. I need Python to check the next location in the High and Low lists for an increase in value equal to or greater than 2.5% at some point beyond the entry signal. However, I also want Python to determine if the value drops 3% or more prior to appreciating 2.5% or more. Unfortunately, the code I developed so far doesn't seem to be working. What am I missing?

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
BryanH
  • 5,826
  • 3
  • 34
  • 47
user1526586
  • 93
  • 1
  • 3
  • 10
  • Yes, I meant to change Minimum to Signal for the sake of clarity here, however, apparently I didn't. I'm using the Minimum values as the index for the other values. – user1526586 Jul 23 '12 at 03:47
  • Can't say I totally understand what you're aiming to accomplish. But I do have two notes. 1) What's the point of the embedded for loop? There is only ever going to be one item in 'Entry'; the list is unnecessary. 2) Variable names in Python should be all lower case (distinguishing them from class names, which are always capitalized as proper nouns). – joe Jul 23 '12 at 04:05
  • I would appreciate if you can provide more details and the program logic. It is not quite clear what you wish to accomplish. BTW, I am active trader in the stock market. Currently writing an app for preparing candlestick and P&F charts. :) – Vineet Jul 23 '12 at 04:10
  • To be more clear, I'm basically attempting to backtest a strategy. Rather than Minimum, I changed it to Signal to make it more clear. The Signal is the location or index at the close of the day at which I will enter a position. I then am attempting to set a sell limit at 2.5% and a stop at -3%. – user1526586 Jul 23 '12 at 04:17
  • Have you tried tracing your code? – Joel Cornett Jul 23 '12 at 04:17
  • If I don't have break, then it gets stuck in a seemingly endless loop. – user1526586 Jul 23 '12 at 04:19
  • No, you break at either branch. This `while` will never loop. – Joel Cornett Jul 23 '12 at 04:19
  • No, I have not tried tracing. I will attempt it momentarily. – user1526586 Jul 23 '12 at 04:21
  • Target 2.5% & SL -3%? i.e. risk/reward ratio 2.5/3 ? – Vineet Jul 23 '12 at 04:38

3 Answers3

1

Sometimes you are doing integer arithmetic, sometimes float arithmetic. This will likely give you seemingly insane results. Try something like:

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

Etc.

Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
1

Not quite sure what you're trying to do. There's a lot of code here that doesn't make sense:

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

Can be replaced with

    Entry = [Close[i]]

And these lines right here?

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

Are semantically identical to

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

Same thing with these:

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

They mean this, basically:

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

As for this:

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

It can be replaced with this:

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

And here's what happens when you put it all together:

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'

Still doesn't make much sense, does it? This is basically what your code is doing. If you want my advice, I'd scrap the whole thing and start over.

Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
0

Here is a liberal reinterpretation of your code and your description of what you imagine your code to be doing, which frankly I don't really understand. I do see that your code does little in the realm of sensible calculation, though, which makes me think this reinterpretation probably won't hurt anything, even if it doesn't end up solving your problem.

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)
joe
  • 777
  • 5
  • 10