swaschke / pypnf

GNU General Public License v2.0
17 stars 15 forks source link

How to export pnf result save to excel or csv ? #1

Closed nkta3m closed 5 months ago

nkta3m commented 1 year ago

thank you for this project. i want export result save to csv or excel, i don't know how to do.

swaschke commented 5 months ago

Hi, I would not classify your request as an issue but here is how you can export the point and figure chart to excel.

from pypnf import PointFigureChart
from pypnf.testdata import dataset

import numpy as np
import pandas as pd

data = dataset('^SPX')

pnf = PointFigureChart(ts=data, method='h/l', reversal=2, boxsize=50, scaling='abs', title='^SPX')

# stack boxscale and matrix together and flip direction for readability in excel
pnf_matrix = np.flip(np.hstack((np.expand_dims(pnf.boxscale, axis=1),pnf.matrix)),0)

# convert matrix into a dataframe
df = pd.DataFrame(pnf_matrix)

# replace numbers by strings
df.replace( 0, ' ', inplace=True)
df.replace( 1, 'X', inplace=True)
df.replace(-1, 'O', inplace=True)

# safe as excel file
df.to_excel('pnf.xlsx', header=False, index=False)