pmorissette / ffn

ffn - a financial function library for Python
pmorissette.github.io/ffn
MIT License
2.03k stars 300 forks source link

How can I correct this error? #192

Closed akitxu closed 1 year ago

akitxu commented 1 year ago

Dear all. I have created this class to get the indicators. `class PreparadorFFN:

def __init__(self, create_df):
    self.create_df = create_df
    self.value_returns  = self.create_df.value_returns 
    self.value_name =  self.value_returns.columns[0]

def preparar_datos_ffn(self):
    self.df_val = self.value_returns[self.value_name]
    self.df_val['Equity'] = self.df_val.cumsum() + 100
    self.perf = self.df_val['Equity'].calc_stats()
    self.perf.display()
    self.perf.stats
    print ("\n")
    print ("\nRetornos mensuales : \n")
    print ("\n")
    self.perf.display_monthly_returns()
    self.perf.stats

def construir_tabla_estadisticos(self):
    tabla_estadisticos = pd.DataFrame()
    try:
        tabla_estadisticos['yearly_sharpe'] = [self.perf.stats.loc["yearly_sharpe"]]
        tabla_estadisticos['calmar'] = [self.perf.stats.loc["calmar"]]
        tabla_estadisticos['max_drawdowns'] = [self.perf.stats.loc["max_drawdown"]]
        tabla_estadisticos['calmar'] = [self.perf.stats.loc["calmar"]]
        tabla_estadisticos['cagr'] = [self.perf.stats.loc["cagr"]]
    except ZeroDivisionError as e:
        print(f"Se produjo una excepción de división por cero: {e}")
        tabla_estadisticos = pd.DataFrame()
    return tabla_estadisticos

Calcular indicadores con FFN

datos_ffn = PreparadorFFN(create_df) preparadorFFN = PreparadorFFN(create_df) preparadorFFN. preparar_datos_ffn() preparadorFFN.construir_tabla_estadisticos() `

Me devuelve estadisticos y el error '/home/enri/anaconda3/envs/yfinance/lib/python3.8/site-packages/ffn/core.py:2299: RuntimeWarning: divide by zero encountered in divide /home/enri/anaconda3/envs/yfinance/lib/python3.8/site-packages/ffn/core.py:258: RuntimeWarning: divide by zero encountered in divide'

imagen

What could be the cause and how can I fix it? Kind regards

timkpaine commented 1 year ago

I believe this is a duplicate of https://github.com/pmorissette/ffn/issues?q=is%3Aissue+divide+by+zero

akitxu commented 1 year ago

timkpaine Indeed they refer to the same problem, but I still don't have any explanation / solution for this problem that limits me to use FFN. Greetings