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

Add Finam importer #2

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
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
68 changes: 68 additions & 0 deletions actions/finam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
from pathlib import Path
from typing import Any, Tuple

import pandas as pd

from common import Cmd, Symbol, p, tzinfo
from fs import Block, Store


class Import(Cmd):
def run(self, *args: str, **kwargs: Any) -> Tuple[int | None, str | Exception | None]:
symbols: dict[str, Symbol] | None = kwargs.get("symbols")
if not symbols:
return None, None
path: str | os.PathLike[str] | None = kwargs.get("path")
path = path if path is not None else ""
store = Store(path)
for arg in args:
sym, err = self.__parse_arg(arg)
if err is not None:
return 1, err
if sym.lower() not in symbols:
p(f"Skipping {arg}")
continue
p(f"Processing {arg}... ", end="")
err = self.__process_arg(arg, symbols[sym.lower()], store)
if isinstance(err, FileNotFoundError):
p("file not found")
return 1, None
if err is not None:
p()
return 2, err
p("done.")
return None, None

@staticmethod
def __parse_arg(arg: str) -> Tuple[str, str | Exception | None]:
symbol, sep, _ = Path(arg).stem.partition("_")
if sep != "_":
return "", f"invalid arg: {arg}"
return symbol, None

@staticmethod
def __process_arg(arg: str, symbol: Symbol, store: Store) -> str | Exception | None:
try:
df = pd.read_csv(arg, sep="\t", dtype="str", skiprows=1, header=None, usecols=range(8))
except OSError as e:
return e
if symbol.time is None:
df[0] = pd.to_datetime(df[0] + " " + df[1], format="%Y.%m.%d %H:%M:%S", utc=True)
else:
df[0] = pd.to_datetime(df[0] + " " + df[1], format="%Y.%m.%d %H:%M:%S")
df[0] = df[0].dt.tz_localize(tzinfo(symbol.time))
df = df.drop(columns=[1, 6])
for date, gf in df.groupby(df[0].dt.date):
err = store.put(Block(symbol.name, symbol.market, date, gf))
if err is not None:
return err
return None


def getcmd(name: str) -> Tuple[Cmd | None, str | None]:
match name:
case "import":
return Import(), None
case _:
return None, f"command {name} not found in module {__name__}"
5 changes: 2 additions & 3 deletions actions/high.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import os
from pathlib import Path
from typing import Any, Tuple, cast
from typing import Any, Tuple

import pandas as pd

Expand Down Expand Up @@ -51,7 +50,7 @@ def __process_arg(arg: str, symbol: Symbol, store: Store) -> str | Exception | N
df[0] = pd.to_datetime(df[0], format="%Y%m%d %H:%M")
df[0] = df[0].dt.tz_localize(tzinfo(symbol.time))
for date, gf in df.groupby(df[0].dt.date):
err = store.put(Block(symbol.name, symbol.market, cast(datetime.date, date), gf))
err = store.put(Block(symbol.name, symbol.market, date, gf))
if err is not None:
return err
return None
Expand Down
5 changes: 2 additions & 3 deletions actions/kraken.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import os
import zipfile
from typing import IO, Any, Tuple, cast
from typing import IO, Any, Tuple

import pandas as pd

Expand Down Expand Up @@ -56,7 +55,7 @@ def __process_file(file: IO[bytes], symbol: Symbol, store: Store) -> str | Excep
if symbol.time is not None:
df[0] = df[0].dt.tz_convert(tzinfo(symbol.time))
for date, gf in df.groupby(df[0].dt.date):
err = store.put(Block(symbol.name, symbol.market, cast(datetime.date, date), gf))
err = store.put(Block(symbol.name, symbol.market, date, gf))
if err is not None:
return err
return None
Expand Down
4 changes: 4 additions & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def _walk_symbols(
else:
symbol.start = symbol.start.replace(tzinfo=datetime.timezone.utc)
if name is not None:
if type(name) != str:
raise TypeError
yield replace(symbol, name=name)
elif symbols is not None:
if not isinstance(symbols, Iterable):
Expand Down Expand Up @@ -132,6 +134,8 @@ def _walk_actions(
case _:
raise Error(f"unexpected key: {k}")
if name is not None:
if type(name) != str:
raise TypeError
yield replace(action, name=name)
elif actions is not None:
if not isinstance(actions, Iterable):
Expand Down