Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Provide example with Pandas / Pandas DataReader #16

Open
femtotrader opened this issue Feb 7, 2021 · 1 comment
Open

Provide example with Pandas / Pandas DataReader #16

femtotrader opened this issue Feb 7, 2021 · 1 comment

Comments

@femtotrader
Copy link
Contributor

Following #9 (comment)

Pandas is a great Python library for data analysis
See https://en.wikipedia.org/wiki/Pandas_(software) for more information

Pandas DataReader, a sibling project of Pandas, provides several remote data sources
https://pydata.github.io/pandas-datareader/remote_data.html

Maybe TermGraph could provide 2 examples with Pandas / Pandas DataReader.

One with static data (like https://github.com/sgeisler/termgraph/blob/master/examples/data.csv)
#9 (comment)
An other example with Python Pandas read_csv function

import pandas as pd

df = pd.read_csv("data.csv", sep=";")
example_data = df.apply(lambda row: Candle(row["open"], row["high"], row["low"], row["close"]), axis=1)

One with remote data source and a cache mechanism (to avoid too much requests)

import pandas_datareader.data as web
import datetime
import requests_cache
expire_after = datetime.timedelta(days=3)
session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=expire_after)

df = web.DataReader('^DJI', 'stooq', session=session)
example_data = df.apply(lambda row: Candle(row["Open"], row["High"], row["Low"], row["Close"]), axis=1)
@errnoh
Copy link

errnoh commented Feb 7, 2021

Is there an easy way to get the width of the vertical axis units?

import pandas_datareader.data as web
import datetime
import requests_cache
import os
from time import sleep
from termgraph import CandleStickGraph, Candle

size = os.get_terminal_size()
window=size.columns-20

expire_after = datetime.timedelta(days=3)
session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=expire_after)

df = web.DataReader('^DJI', 'stooq', session=session)
example_data = df.apply(lambda row: Candle(row["Open"], row["High"], row["Low"], row["Close"]), axis=1)

for i in range(window, len(example_data)):
    g = CandleStickGraph(example_data[i-window:i], 55)
    print(g.draw())
    sleep(0.1)

Could be one example with rolling window, but the hardcoded columns-20 is rather ugly :) (also the sqlite cache should be .gitignored)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants