Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Price decimals processing fix #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion finam/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import operator
from enum import IntEnum
from io import StringIO
from decimal import Decimal

try:
from urllib import urlencode
Expand Down Expand Up @@ -329,6 +330,10 @@ def __init__(self, export_host=None, fetcher=fetch_url):
else:
self._export_host = self.DEFAULT_EXPORT_HOST

@staticmethod
def _convert_decimals(value):
return float(Decimal(value))

def _build_url(self, params):
url = ('http://{}/table.csv?{}&{}'
.format(self._export_host,
Expand Down Expand Up @@ -393,14 +398,18 @@ def download(self,
self._sanity_check(data)
if timeframe == Timeframe.TICKS:
date_cols = [2, 3]
decimal_cols = ["<LAST>"]
else:
date_cols = [0, 1]
decimal_cols = ["<OPEN>", "<HIGH>", "<LOW>", "<CLOSE>"]

try:
decimals_converter_dict = {column: self._convert_decimals for column in decimal_cols}
df = pd.read_csv(StringIO(data),
index_col=0,
parse_dates={'index': date_cols},
sep=';')
sep=';',
converters=decimals_converter_dict)
df.sort_index(inplace=True)
except ParserError as e:
raise FinamParsingError(e.message)
Expand Down