I am trying to calculate the Average True Range of a data series which has been read and parsed from a .csv file. my code is as follows:
import datetime
import time
import matplotlib.pyplot as plt
import numpy as np
fhand = open('C:\Users\Stuart\Desktop\FX Programming\EURUSD_hour.csv', 'r')
for line in fhand:
line = line.split(',')
fxpair, _date, _time, _open, _high, _low, _close = line[0], line[1], line[2], line[3], float(line[4]), float(line[5]), float(line[6])
date_time = datetime.datetime.strptime('{} {}'.format(_date.partition(' ')[0], _time),'%Y%m%d %H:%M:%S')
#define Average True Range function
def TR(d,c,h,l,o,yc):
x = h-l
y = abs(h-yc)
z = abs(l-yc)
print x
print y
print z
if y <= x >= z:
TR = x
elif x <= y >= z:
TR = y
elif x <= z >= y:
TR = z
print d, TR
return d, TR
x = 1
TRDates = []
TrueRanges = []
while x < len(_date):
TRDate, TrueRange = TR(_date[x],_close[x],_high[x],_low[x],_open[x],_close[x-1])
TRDates.append(TRDate)
TrueRanges.append(TrueRange)
x+=1
def ExpMovingAverage(values, window):
weights = np.exp(np.linspace(-1., 0., window))
weights /= weights.sum()
a = np.convolve(values, weights, mode='full')[:len(values)]
a[:window] = a[window]
return a
print len(TrueRanges)
ATR = ExpMovingAverage(TrueRanges,14)
print ATR
However I am getting the following error message:
46 while x < len(line):
---> 47 TRDate, TrueRange = TR(_date[x],_close[x],_high[x],_low[x],_open[x],_close[x-1])
48 TRDates.append(TRDate)
49 TrueRanges.append(TrueRange)
TypeError: 'float' object has no attribute '__getitem__'
So there is obviously something wrong with the while statement, and it isnt iterating over the _date like I want it to. I have also tried using date_time instead of _date but then I get:
---> 46 while x < len(date_time):
47 TRDate, TrueRange = TR(date_time[x],_close[x],_high[x],_low[x],_open[x],_close[x-1])
48 TRDates.append(TRDate)
TypeError: object of type 'datetime.datetime' has no len()
Can someone help me to get this working?
I have a very similar code which I have pasted below that actually works, which uses a different text file for data and unpacks it a different way. Can someone help me reconcile the two so that the top code works?
import numpy as np
sampleData = open('C:\\Users\\Stuart\\Desktop\\FX Programming\\sampleData.txt','r').read()
splitData = sampleData.split('\n')
date,closep,highp,lowp,openp,volume = np.loadtxt(splitData, delimiter=',',unpack=True)
def TR(d,c,h,l,o,yc):
x = h-l
y = abs(h-yc)
z = abs(l-yc)
print x
print y
print z
if y <= x >= z:
TR = x
elif x <= y >= z:
TR = y
elif x <= z >= y:
TR = z
print d, TR
return d, TR
x = 1
TRDates = []
TrueRanges = []
while x < len(date):
TRDate, TrueRange = TR(date[x],closep[x],highp[x],lowp[x],openp[x],closep[x-1])
TRDates.append(TRDate)
TrueRanges.append(TrueRange)
x+=1
################
def ExpMovingAverage(values, window):
weights = np.exp(np.linspace(-1., 0., window))
weights /= weights.sum()
a = np.convolve(values, weights, mode='full')[:len(values)]
a[:window] = a[window]
return a
print len(TrueRanges)
ATR = ExpMovingAverage(TrueRanges,14)
print ATR
TR, which you attempt to index with[x]is a number, not an array or list. Before jumping to usingTR, make sure that thefor line in fhand:block is producing the desired values (lists of numbers?). Off hand it looks like those variables might be strings.