poc / modules /technical.py
ryanrahmadifa
Updated technical anlysis
ffe3438
import pandas as pd
import pandas_ta as ta
class TechnicalAnalysis:
def __init__(self, csv_file):
# Load the CSV data
self.data = pd.read_csv(csv_file)
self.data['Date'] = pd.to_datetime(self.data['Date'])
self.data.set_index('Date', inplace=True)
def moving_average(self, length):
# Calculate moving average
self.data[f'MA_{length}'] = ta.sma(self.data['Close'], length=length)
def rsi(self, length=14):
# Calculate RSI
self.data[f'RSI_{length}'] = ta.rsi(self.data['Close'], length=length)
def bollinger_bands(self, length=20, std=2):
# Calculate Bollinger Bands
bbands = ta.bbands(self.data['Close'], length=length, std=std)
self.data['BB_upper'], self.data['BB_middle'], self.data['BB_lower'] = bbands.iloc[:, 0], bbands.iloc[:, 1], bbands.iloc[:, 2]
def macd(self, fast=12, slow=26, signal=9):
# Calculate MACD
macd = ta.macd(self.data['Close'], fast=fast, slow=slow, signal=signal)
self.data['MACD'], self.data['MACD_Signal'], self.data['MACD_Hist'] = macd.iloc[:, 0], macd.iloc[:, 1], macd.iloc[:, 2]
def ultimate_oscillator(self, fast=7, medium=14, slow=28):
# Calculate Ultimate Oscillator
self.data['Ultimate_Oscillator'] = ta.uo(self.data['High'], self.data['Low'], self.data['Close'], fast=fast, medium=medium, slow=slow)
def adx(self, length=14):
# Calculate ADX
self.data['ADX'], self.data['ADX_DMP'], self.data['ADX_DMN'] = ta.adx(self.data['High'], self.data['Low'], self.data['Close'], length=length)
def get_data(self):
return self.data