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

Trade fee #28

Merged
merged 4 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
✨ Update trades report example for actual version
  • Loading branch information
esemi committed Nov 23, 2020
commit a0481b8026200f6fd1da146a4bf8e133ff5b2484
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ $ pip install investments --upgrade --user
- пока **НЕ** поддерживаются сделки Forex, сделка пропускается и выводится сообщение о том, что это может повлиять на итоговый отчет

*Пример отчета:*
TODO UPDATE example
![ibtax report example](./images/ibtax_2016.jpg)
![ibtax report example](./images/ibtax_2020.jpg)


### Запуск
Expand Down
Binary file removed images/ibtax_2016.jpg
Binary file not shown.
Binary file added images/ibtax_2020.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 3 additions & 9 deletions investments/data_providers/cbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,11 @@ def __init__(self, currency: Currency, year_from: int = 2000, cache_dir: str = N
def dataframe(self) -> pandas.DataFrame:
return self._df

def get_rate(self, dt: datetime.date) -> Money:
if isinstance(dt, datetime.datetime):
dt = datetime.datetime.combine(dt.date(), datetime.datetime.min.time())

if isinstance(dt, datetime.date):
dt = datetime.datetime.combine(dt, datetime.datetime.min.time())

def get_rate(self, dt: datetime.datetime) -> Money:
return self._df.loc[dt].item()

def convert_to_rub(self, source: Money, rate_date: datetime.date) -> Money:
assert isinstance(rate_date, datetime.date)
def convert_to_rub(self, source: Money, rate_date: datetime.datetime) -> Money:
assert isinstance(rate_date, datetime.datetime)

if source.currency == Currency.RUB:
return Money(source.amount, Currency.RUB)
Expand Down
2 changes: 1 addition & 1 deletion investments/ibtax/ibtax.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def prepare_trades_report(finished_trades: List[FinishedTrade], cbr_client_usd:
trade_date_column = 'trade_date'
tax_date_column = 'settle_date'

df = pandas.DataFrame(finished_trades, columns=finished_trades[0]._fields) # noqa: WPS437
df = pandas.DataFrame(finished_trades, columns=finished_trades[0].fields)

df[trade_date_column] = df[trade_date_column].dt.normalize()
df[tax_date_column] = pandas.to_datetime(df[tax_date_column])
Expand Down
4 changes: 0 additions & 4 deletions investments/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class Trade(NamedTuple):
# комиссия за сделку
fee: Money

@property
def settle_datetime(self) -> datetime.datetime:
return datetime.datetime.combine(self.settle_date, datetime.datetime.min.time())

@property
def fee_per_piece(self) -> Money:
"""Комиссия за сделку за одну бумагу, полезно для расчёта налогов."""
Expand Down
10 changes: 7 additions & 3 deletions investments/trades_fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ class FinishedTrade(NamedTuple):
# дата поставки, нужна для расчёта цены сделки в рублях на дату
settle_date: datetime.date

# количество бумаг, "+/-"
# количество бумаг, положительное для операции покупки, отрицательное для операции продажи
quantity: int

# цена одной бумаги, "+"
# цена одной бумаги, всегда положительная
price: Money

# комиссия за сделку с одной бумагой, "-"
# комиссия за сделку с одной бумагой, всегда отрицательная
fee_per_piece: Money

@property
def fields(self) -> Tuple[str, ...]:
return self._fields


class PortfolioElement(NamedTuple):
ticker: Ticker
Expand Down
2 changes: 1 addition & 1 deletion tests/data_providers/cbr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(datetime(2015, 3, 8), Currency.USD, Money('59.9938', Currency.RUB)),
(datetime(2020, 3, 31), Currency.USD, Money('77.7325', Currency.RUB)),
(datetime(2020, 1, 9), Currency.USD, Money('61.9057', Currency.RUB)),
(date(2020, 2, 4), Currency.EUR, Money('70.7921', Currency.RUB)),
(datetime(2020, 2, 4), Currency.EUR, Money('70.7921', Currency.RUB)),

(datetime(2015, 3, 7), Currency.EUR, Money('66.1012', Currency.RUB)),
(datetime(2015, 1, 15), Currency.EUR, Money('77.9629', Currency.RUB)),
Expand Down