Ron
Ron

Reputation: 193

TypeError: unsupported operand type(s) for -: 'str' and 'numpy.ndarray'

I'm having an issue with min() max() and str and numpy.ndarray. I get this error:

TypeError: unsupported operand type(s) for -: 'str' and 'numpy.ndarray'

I suspect the problem is with the haHigh0 in the line that defines haUpLeg0, and I would appreciate any input as to how I could correct. I have looked through stack at some similar problems but cant seem to get a fix to translate to my problem.

Data:

Date        Time    Open    High    Low    Close    Volume  Ticker 
02/01/2015  14:30   79.52   79.73   79.52   79.71   10841   DVY 
02/01/2015  14:31   79.6999 79.6999 79.61   79.67   1426    DVY 
02/01/2015  14:32   79.69   79.7    79.69   79.7    800     DVY 
02/01/2015  14:33   79.7    79.759  79.7    79.759  1158    DVY 
02/01/2015  14:34   79.747  79.747  79.73   79.73   1125    DVY 
02/01/2015  14:35   79.73   79.73   79.71   79.71   301     DVY 
02/01/2015  14:36   79.8    79.82   79.75   79.82   7818    DVY 
02/01/2015  14:37   79.82   79.83   79.778  79.82   6985    DVY

Code:

mkt_data = index_data[['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']]
open0 = mkt_data['Open'] #
high0 = mkt_data['High'] #
low0 = mkt_data['Low'] #
close0 = mkt_data['Close'] #
date0 = mkt_data['Date'] #
time0 = mkt_data['Time'] #
ticker0 = index_data['Ticker'] #

close1 = mkt_data['Close'].shift(1) #
open1 = mkt_data['Open'].shift(1) # 
high1 = mkt_data['High'].shift(1) #
low1 = mkt_data['Low'].shift(1) #
high2 = mkt_data['High'].shift(2) #
low2 = mkt_data['Low'].shift(2) #
close2 = mkt_data['Close'].shift(2) # 
open2 = mkt_data['Open'].shift(2) #
close3 = mkt_data['Close'].shift(3) # 
open3 = mkt_data['Open'].shift(3) #

haClose2 = (open2 + high2 + low2 + close2) / 4  #
haOpen2 = (close3 + open3) / 2  #       

haClose1 = (open1 + high1 + low1 + close1) / 4  #
haOpen1 = (haClose2 + haOpen2) / 2  #
haHigh1 = max('high1', 'haOpen1', 'haClose1') #
haLow1 = min('low1', 'haOpen0', 'haClose1') # 

haClose0 = (open0 + high0 + low0 + close0) / 4 #
haOpen0 = (haClose1 + haOpen1) / 2 #


haHigh0 = max('high0', 'haOpen0', 'haClose0') #
haLow0 = min('low0', 'haOpen0', 'haClose0')  #
haUpLeg0 = (haHigh0 - haClose0) / (close1) #

Upvotes: 0

Views: 2964

Answers (2)

RKD314
RKD314

Reputation: 1185

If you are trying to find the highest value out of the three--high0, haOpen0, haClose0--you could do

max(max(high0),max(haOpen0),max(haClose0))

I think you should not use quotes around the variable names. 'high0' is a string, but high0 is a pandas Series.

Upvotes: 2

DreadPirateShawn
DreadPirateShawn

Reputation: 8402

Near the bottom, it looks like you start to use variable names and strings interchangeably, eg:

haClose1 = (open1 + high1 + low1 + close1) / 4  #
haOpen1 = (haClose2 + haOpen2) / 2  #
haHigh1 = max('high1', 'haOpen1', 'haClose1') #
haLow1 = min('low1', 'haOpen0', 'haClose1') # 

You probably want to keep using variables:

haClose1 = (open1 + high1 + low1 + close1) / 4  #
haOpen1 = (haClose2 + haOpen2) / 2  #
haHigh1 = max(high1, haOpen1, haClose1) #
haLow1 = min(low1, haOpen0, haClose1) # 

etc.

That is to say, you're getting a TypeError because - (and + etc) is an unsupported operand type between str and numpy.ndarray -- you can't subtract one from (or add one to) the other.

Upvotes: 2

Related Questions