Skip to content

dev-Priyanshu07/recipe-app-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Sure, I will modify the class to return only the Supertrend values without the direction. Here's the updated code:

import pandas as pd
import numpy as np

class SupertrendCalculator:
    def __init__(self, data, period=14, multiplier=3):
        """
        Initializes the SupertrendCalculator with data, period, and multiplier.
        
        :param data: A pandas DataFrame with columns 'High', 'Low', 'Close'.
        :param period: The look-back period for ATR calculation.
        :param multiplier: The multiplier for the ATR in Supertrend calculation.
        """
        self.data = data
        self.period = period
        self.multiplier = multiplier
    
    def calculate_atr(self):
        """
        Calculates the Average True Range (ATR).
        
        :return: A pandas Series with the ATR values.
        """
        high_low = self.data['High'] - self.data['Low']
        high_close = np.abs(self.data['High'] - self.data['Close'].shift())
        low_close = np.abs(self.data['Low'] - self.data['Close'].shift())
        
        tr = pd.concat([high_low, high_close, low_close], axis=1).max(axis=1)
        atr = tr.rolling(self.period).mean()
        
        return atr
    
    def calculate_supertrend(self):
        """
        Calculates the Supertrend indicator.
        
        :return: A pandas Series with the Supertrend values.
        """
        atr = self.calculate_atr()
        
        hl2 = (self.data['High'] + self.data['Low']) / 2
        upper_band = hl2 + (self.multiplier * atr)
        lower_band = hl2 - (self.multiplier * atr)
        
        supertrend = pd.Series(index=self.data.index, dtype='float64')
        in_uptrend = True
        
        for i in range(1, len(self.data)):
            if self.data['Close'][i] > upper_band[i-1]:
                in_uptrend = True
            elif self.data['Close'][i] < lower_band[i-1]:
                in_uptrend = False
            
            if in_uptrend:
                supertrend[i] = lower_band[i]
            else:
                supertrend[i] = upper_band[i]
        
        return supertrend

# Example usage:
# data = pd.read_csv('your_data.csv')  # Make sure your CSV has 'High', 'Low', 'Close' columns
# stc = SupertrendCalculator(data)
# supertrend_values = stc.calculate_supertrend()
# print(supertrend_values)

This class now returns only the Supertrend values as a pandas Series. The Supertrend calculation logic has been streamlined, and the direction logic has been simplified and integrated into the Supertrend value assignment.

To use this class, follow the same steps as before:

import pandas as pd

# Load your data into a DataFrame
data = pd.read_csv('your_data.csv')  # Replace with your actual data file

# Initialize the SupertrendCalculator
stc = SupertrendCalculator(data)

# Calculate the Supertrend
supertrend_values = stc.calculate_supertrend()

# Print the Supertrend values
print(supertrend_values)

This will give you a pandas Series containing the Supertrend values for each time period in your dataset.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published