Skip to content

Commit

Permalink
Merge pull request #190 from Glyphack/add-function-to-export-ticker-h…
Browse files Browse the repository at this point in the history
…istorical-data-to-csv
  • Loading branch information
Glyphack authored Oct 8, 2022
2 parents 52b4daf + 54da0a7 commit f00a80a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [اطلاعات لحظه‌ای سهام](#اطلاعات-لحظهای-سهام)
- [ریز معاملات سهام](#ریز-معاملات-سهام)
- [تمامی اطلاعات موجود برای فیلترنویسی](#تمامی-اطلاعات-موجود-برای-فیلترنویسی)
- [گرفتن تمام اطلاعات تاریخی یا لحظه‌ای نماد به صورت CSV](#گرفتن-تمام-اطلاعات-تاریخی-یا-لحظهای-نماد-به-صورت-csv)
- [کامیونیتی](#کامیونیتی)
- [منابع آموزشی](#منابع-آموزشی)
- [الهام گرفته از:](#الهام-گرفته-از)
Expand Down Expand Up @@ -760,6 +761,31 @@ key_stats = get_stats(base_path="hello", to_csv=True)

</div>

### گرفتن تمام اطلاعات تاریخی یا لحظه‌ای نماد به صورت CSV

برای استفاده راحت‌تر از اطلاعات لحظه‌ای یا تاریخی و یا درست کردن فایل برای نرم افزارهای دیگه توابعی وجود داره که تمام اطلاعات نماد رو در یک فایل برمیگردونه.

اطلاعات تاریخی برگشته شامل اطلاعات خرید و فروش حقیقی و حقوقی و تاریخچه سهم هست که در هر سطر با ذکر تاریخ وجود دارند
اطلاعات لحظه‌ای تنها یک سطر هست و شامل اطلاعات تابلو هست.

توجه کنید این اطلاعات چیزی بیشتر از توابع موجود در پکیج ندارند و صرفا برای راحتی کار کاربران توسعه داده شده‌اند.
<div dir="ltr">

```python
ticker = Ticker("وبملت")
historical_data = export_ticker_history_as_csv(ticker)
real_time_data = ticker_real_time_data_to_csv(ticker)

# برای نوشتن این اطلاعات به شکل فایل csv

historical_data.to_csv("history.csv")
real_time_data.to_csv("realtime.csv")

```

</div>



## کامیونیتی

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytse-client"
version = "0.13.0"
version = "0.14.0"

description = "tehran stock exchange(TSE) client in python"
authors = ["glyphack <sh.hooshyari@gmail.com>"]
Expand Down
37 changes: 37 additions & 0 deletions pytse_client/ticker/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,44 @@ def ticker_real_time_data_to_csv(ticker: Ticker):
[flat_dict] = pandas.json_normalize(data, sep=".").to_dict(
orient="records"
)

df = pandas.DataFrame([flat_dict])
df["symbol"] = ticker.symbol
df["name"] = ticker.title
df["group_name"] = ticker.group_name
df["fiscal_year"] = ticker.fiscal_year
df["eps"] = ticker.eps
df["p_e_ratio"] = ticker.p_e_ratio
df["group_p_e_ratio"] = ticker.group_p_e_ratio
df["psr"] = ticker.psr
df["p_s_ratio"] = ticker.p_s_ratio
df["base_volume"] = ticker.base_volume
df["flow"] = ticker.flow
df["sta_max"] = ticker.sta_max
df["sta_min"] = ticker.sta_min
df["min_week"] = ticker.min_week
df["max_week"] = ticker.max_week
df["min_year"] = ticker.min_year
df["max_year"] = ticker.max_year
df["month_average_volume"] = ticker.month_average_volume
return df


def export_ticker_history_as_csv(ticker: Ticker):
trade_day_history = ticker.history
client_types_history = ticker.client_types

# TODO move to original function if okay
trade_day_history["date"] = pandas.to_datetime(trade_day_history["date"])
client_types_history["date"] = pandas.to_datetime(
client_types_history["date"]
)

merged_dataframe = pandas.merge(
trade_day_history,
client_types_history,
on="date",
how="outer",
)

return merged_dataframe
5 changes: 4 additions & 1 deletion pytse_client/ticker/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ def float_shares(self) -> float:

@property
def client_types(self):
return download_ticker_client_types_record(self._index)
client_types = download_ticker_client_types_record(self._index)
if client_types is None:
raise RuntimeError("cannot download client types data try again")
return client_types

@property
def trade_dates(self) -> List[datetime.date]:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_ticker/test_ticker_export.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import unittest

from pytse_client import Ticker
from pytse_client.ticker.export import ticker_real_time_data_to_csv
from pytse_client.ticker.export import (
export_ticker_history_as_csv,
ticker_real_time_data_to_csv,
)


class TestTickerExport(unittest.TestCase):
Expand All @@ -16,6 +19,12 @@ def test_export_ticker_realtime_data_to_csv(self):
df = ticker_real_time_data_to_csv(ticker)
self.assertTrue(df.empty is False)

def test_export_ticker_history_to_csv(self):
ticker = Ticker("وبملت")
df = export_ticker_history_as_csv(ticker)
df.to_csv("test.csv")
self.assertTrue(df.empty is False)


def suite():
test_suite = unittest.TestSuite()
Expand Down

0 comments on commit f00a80a

Please sign in to comment.