2

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 for each.

I need Python to check the next location ( the signal or entry point or date + 1 ) in the High and Low lists ( the lists: close, highs, and lows will have the same number of values ) 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.

This must occur for each entry in signal.

In essence, I need a limit to sell at 102.5% and a stop at 97%.

Unfortunately, the code I developed so far doesn't seem to be working.

What am I missing?

signals = [1,5,7]
close   = [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:
    entry = close[i]
    print i
    for high in highs[i+1:]:
        profit = ( ( high - entry ) / entry ) * 100
    for low in lows[i+1:]:
        loss   = ( ( low  - entry ) / entry ) * 100
    if abs( loss ) <  3:
        if profit  >= 2.5:
            print 'Win'
        else:
            print 'Loss'
user3666197
  • 1
  • 6
  • 50
  • 92
user1526586
  • 93
  • 1
  • 3
  • 10

2 Answers2

2

Your profit is only calculated for highs[-1] while loss is only calculated for lows[-1]. Everything else is discarded, as you replace profit and loss in each loop.

You want to find the set of values where your condition is true. Use zip to put lows and highs together:

for i in signals:
    entry = float(close[i])
    for high, low in zip(high[i + 1:], low[i + 1:]):
        profit = ((high - entry) / entry) * 100
        loss = ((low - entry) / entry) * 100
        if loss > -3:
            if profit >= 2.5:
                print "Win"
            else:
                print "Loss"
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I need it to look at each high and in the case that one is 2.5% higher than the entry point, while not having a low that is less than 3% below the entry point before the 2.5% increase, it will print "Win." – user1526586 Jul 23 '12 at 09:14
  • @user1526586: in pairs? So if high[3] is 2.5 higher, and low[3] is > 3% below? Your algorithm is not clear. – Martijn Pieters Jul 23 '12 at 09:18
  • If the absolute value of the low > 3%, then it is no longer a win if it occurs before any high is 2.5% or higher. – user1526586 Jul 23 '12 at 09:20
  • @user1526586: that's not what I meant. Which low? Which high? You have lists of both, not one value of each. – Martijn Pieters Jul 23 '12 at 09:22
  • Ok, on a given day, the stock has a high, a low, and a closing value. I'm assuming the purchase will occur at the closing value and each close, high, and low correspond to the other as far as date. Therefore, if a high on any day is 3% above the closing price on the day I purchased the stock, then I should be able to get my 2.5% profit. However, if at any point, their is a low that is below 3% loss between the day that the price increases to 2.5% or more,then the trade is a loss. I want to start at purchase date + 1. Does that make sense? – user1526586 Jul 23 '12 at 09:28
  • @user1526586: and what is date + 1? The value to the left or right in your lists when printed? – Martijn Pieters Jul 23 '12 at 09:30
  • It should be the value to the right. Earlier dates on left and later dates on right. – user1526586 Jul 23 '12 at 09:31
  • @user1526586: you do realize you are asking people to write the code for you, instead of understand what you are doing wrong.. I'll update with more hints. – Martijn Pieters Jul 23 '12 at 09:34
2

Did you already check python-libraries for backtesting? In fact I use other libraries, but there are some very popular python-based solutions such as "pybacktest", "PyAlgoTrade", or "UltraFinance". Maybe integrating such a library could be advantageous for your use case...