0

I am working in python 2.6. Can anyone figure out why my variable squared does not calculate properly when it is inside a min() function? To illustrate, I show the variable**2 performed by itself, works fine. Here is the code, followed by some results:

from __future__ import with_statement
import csv, re, time
timestr = time.strftime("%Y%m%d_%H%M%S")

fileinput = 'mp_20130822.csv'
out = open('output2.csv',"wb")
writer = csv.writer(out, delimiter = ',')

ESPmax = 100.00
dynmax = "enabled"

def config():
    global argument
    if z>30:
        argument = "T"
    else:
        argument = "F"

with open(fileinput,'rU') as inputfile:
    reader = csv.reader(inputfile, delimiter = ';')
    for line in reader:
            line = re.split(",| ",line[0])
            side = str(line[4]) #is "Bid" or "Ask"
            e = float(line[5])
            z = float(line[6])
            t = float(line[33])
            FW = float(line[34])
            FS = max(float(line[35]),200)
            if FW == 0:
                continue
            if (FS == 0) or (FS == 1):
                continue
            if side == "Ask":
                LE = t-e
            else:
                LE = e-t
            LEP = LE/(FW/2)
            ESP = z/(FS/2)
            if dynmax == "enabled":
                ESPmax = min(LEP**2,ESPmax)

    config()
    if (argument == "T"):
        print ('side, e, z, t, FW, FS')
        print ('LEP,LEP,ESPmax')
        print (side, '%.2f'%e, '%.2f'%z, '%.2f'%t, '%.2f'%FW, '%.2f'%FS)
        print ('%.3f'%LEP,'%.3f'%LEP,'%.5f'%ESPmax)
        print '%.5f'%(LEP*LEP)

RESULTS:

side, e, z, t, FW, FS
LEP,LEP,ESPmax
('Ask', '1.90', '50.00', '1.95', '0.24', '651.00')
('0.423', '0.423', '0.00130')
0.17880
side, e, z, t, FW, FS
LEP,LEP,ESPmax
('Ask', '8.40', '40.00', '8.43', '0.17', '4933.00')
('0.348', '0.348', '0.00130')
0.12145
side, e, z, t, FW, FS
LEP,LEP,ESPmax
('Ask', '8.40', '40.00', '8.43', '0.17', '4919.00')
('0.370', '0.370', '0.00130')
0.13667
teachamantofish
  • 75
  • 1
  • 1
  • 7
  • Seems to work fine, `ESPmax = min(LEP**2,ESPmax)` seems to have set `ESPmax` to a value smaller than any of the `LEP**2` you printed out, so of course the minimum is the smaller value `0.00130` you probably got from an `LEP` of `0.036` earlier. – Daniel Fischer Aug 22 '13 at 20:33
  • how could ESPmax have been reduced to 0.00130? – teachamantofish Aug 22 '13 at 21:30
  • By an `LEP` value of approximately `0.036` earlier. Since it's extremely unlikely that `min` or `**2` don't work correctly, and you always get the same value in what you pasted here, it's overwhelmingly likely that something changed the value of `ESPmax` to `0.00130` before. Note that you only print anything if `Z > 30` (which, by the way, doesn't seem to be defined where it's used in `config()`). – Daniel Fischer Aug 22 '13 at 21:35
  • it does seem that ESPmax is getting reset to a random, fixed number after I set it to 100. I have no clue why. I ran my code again and this time it is getting set to 0.0. – teachamantofish Aug 22 '13 at 21:55
  • No, `select` isn't broken. `ESPmax` is getting set by your code. You don't know why, so debug it. I don't know if there's a decent debugger for Python, but old-skool `printf`-debugging works everywhere [except on embedded systems where you have no console to print stuff out]. Print out the values of `ESPmax` and `LEP` before the line `ESPmax = min(LEP**2,ESPmax)`. You may need to look at other values too, but start with these. – Daniel Fischer Aug 22 '13 at 22:04
  • yes, I did that and found the issue. I was assigning ESPmax a new value in the loop, then did not return it to the default value, as I should have at the end of the loop, because I want to start with the default value each time at the top of the loop. The answer was to reset ESPmax to the correct defalut at the end of the loop, or to make and incorporate a separate parameter such as ESPmaxDefault, then set ESPmax = min(LEP**2,ESPmaxDefault) – teachamantofish Aug 23 '13 at 16:51

1 Answers1

0

The indentation is clearly a SyntaxError in Python, so its unclear what your code actually does. Though I'm pretty sure that you somehow manage to assign 0.00130 to ESPmax, and once you get there the

ESPmax = min(LEP**2,ESPmax)

line will not change ESPmax for your values of LEP.

vbraun
  • 1,851
  • 17
  • 14
  • But how could ESPmax have been reduced to 0.00130? I initially gave it a default value of 100. The only time it's assigned a new value is here: ESPmax = min(LEP**2,ESPmax). Also not sure there is an indentation error, or where you see it. The code did not return a syntax error... – teachamantofish Aug 22 '13 at 21:29
  • In the code here, the `config()` call doesn't match any previous indentation level. Probably an artifact from pasting it here, did you perchance use tabs to indent? Also not unlikely that you pasted the code here and forgot to select the last lines when you indented the entire code block. – Daniel Fischer Aug 22 '13 at 21:40
  • yeah, I see it now. probably was an artifact from pasting. So, I figured out my problem. it was so silly. since I am defining ESPmax inside the loop, I forgot to reset its value to the default at the end of the loop. so in the next iteration of the loop, the default value became the previous calculated value. if it was a low number. It kept on getting smaller through the loop whenever a new lower value was passed through the min() function. Thank you – teachamantofish Aug 22 '13 at 22:02