GregoryMorse / trendln

Support and Resistance Trend lines Calculator for Financial Analysis
MIT License
663 stars 162 forks source link

Getting trend data into a pandas data frame that has other calculations and using minima and maxima for Fibonacci Retracement #23

Open pranavlal opened 3 years ago

pranavlal commented 3 years ago

Hi all,

I am trying to get the local minimum and local maximum prices into a pandas data frame. This data frame has other data as well such as the opening, high, low and closing prices, a number of exponential moving averages that I have calculated etc. To this end, I need a few clarifications. In terms of data, I have daily price data for a year.

  1. Do the minimaIdxs and maximaIdxs parameters contain indices to each local minimum and maximum price? I am using them as row indices to get to the respective row in the data frame and add the relevant price in its own column.
  2. I want to use Fibonacci Retracement. The local minima and local maxima are perfect for this but the problem I have is that since the number of local maxima and number of local minima are not the same, I am having a hard time finding which is the maximum price and which is the minimum price.

Its possible that I have misunderstood the parameters that calc_support_resistance takes therefore feel free to set me strait.

My code is below.

def calculate_support_resistance(df): minvals, maxvals = trendln.calc_support_resistance(df['close'],accuracy=2)

(minimaIdxs, pmin, mintrend, minwindows), (maximaIdxs, pmax, maxtrend, maxwindows) = minvals, maxvals
minimums=[None]*len(df['close'].tolist())
maximums=[None]*len(df['close'].tolist())
closing_list=list(df["close"])

for lc in range(0,len(minimaIdxs)):
    minimums[minimaIdxs[lc]]=closing_list[minimaIdxs[lc]]
df['support']=minimums
for lc in range(0,len(maximaIdxs)):
    maximums[maximaIdxs[lc]]=closing_list[maximaIdxs[lc]]
df['resistance']=maximums
return df