Целью этого скрипта является чтение файла csv.
Файл содержит данные форекс.
В файле 7 столбцов Дата, Время, Открытие, Максимум, Минимум, Закрытие и Объем и около 600 тыс. Строк.
После очистки даты и времени скрипт должен произвести вычисление даты и времени, например, месяц и день.
Затем технический анализ с использованием библиотеки TA-LIB.
Вот код:
import pandas as pd
import talib
class Data:
def __init__(self):
self.df = pd.DataFrame()
self.names = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']
self.open = self.df['Open'].astype(float)
self.high = self.df['High'].astype(float)
self.low = self.df['Low'].astype(float)
self.close = self.df['Close'].astype(float)
def file(self, file):
self.df = pd.read_csv(file, names=self.names,
parse_dates={'Release Date': ['Date', 'Time']})
return self.df
def date(self):
self.df['Release Date'] = pd.to_datetime(self.df['Release Date'])
def year(self):
self.df['year'] = pd.to_datetime(self.df['Release Date']).dt.year
def month(self):
self.df['year'] = pd.to_datetime(self.df['Release Date']).dt.month
def day(self):
self.df['year'] = pd.to_datetime(self.df['Release Date']).dt.day
def dema(self):
# DEMA - Double Exponential Moving Average
self.df['DEMA'] = talib.DEMA(self.close, timeperiod=30)
def ema(self):
# EMA - Exponential Moving Average
self.df['EMA'] = talib.EMA(self.close, timeperiod=30)
def HT_TRENDLINE(self):
# HT_TRENDLINE - Hilbert Transform - Instantaneous Trendline
self.df['HT_TRENDLINE '] = talib.HT_TRENDLINE(self.close)
def KAMA(self):
# KAMA - Kaufman Adaptive Moving Average
self.df['KAMA'] = talib.KAMA(self.close, timeperiod=30)
def ma(self):
# MA - Moving average
self.df['MA'] = talib.MA(self.close, timeperiod=30, matype=0)
def print(self):
return print(self.df.head())
x = Data()
x.file(r"D:\Projects\Project Forex\USDJPY.csv")
x.print()
Вот ошибка:
Traceback (most recent call last):
File "C:\Users\Sayed\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Sayed/PycharmProjects/project/Technical Analysis.py", line 55, in <module>
x = Data()
File "C:/Users/Sayed/PycharmProjects/project/Technical Analysis.py", line 9, in __init__
self.open = self.df['Open'].astype(float)
File "C:\Users\Sayed\miniconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\Sayed\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Open'