Skip to content

Commit

Permalink
Fix current holdings when funds had different dates
Browse files Browse the repository at this point in the history
  • Loading branch information
frefrik authored Oct 14, 2021
1 parent dabbeb2 commit a4c6b21
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
53 changes: 50 additions & 3 deletions app/api/v2/crud.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import desc, func
from sqlalchemy.orm import Session
from sqlalchemy.sql.expression import and_

from app.models import Fund, Holding, News, Trades

Expand All @@ -8,6 +9,52 @@ def get_etf_profile(db: Session, symbol: str):
return db.query(Fund).filter(Fund.symbol == symbol).one()


def get_etf_current_holdings(db: Session, symbols: str, limit: int):
subq = (
db.query(
Holding.fund,
func.max(Holding.date).label("maxdate"),
)
.filter(
Holding.fund.in_([s for s in symbols]),
)
.group_by(Holding.fund)
.subquery()
)

q = (
db.query(
Holding.fund,
Holding.date,
Holding.ticker,
Holding.company,
Holding.cusip,
Holding.shares,
Holding.market_value,
Holding.share_price,
Holding.weight,
Holding.weight_rank,
)
.join(
subq,
and_(
Holding.fund == subq.c.fund,
Holding.date == subq.c.maxdate,
),
)
.order_by(
Holding.date,
Holding.fund,
Holding.weight_rank,
)
)

if limit:
return q.order_by("date", "weight_rank").limit(limit).all()
else:
return q.all()


def get_etf_holdings(
db: Session, symbols: str, date_from: str, date_to: str, limit: int
):
Expand Down Expand Up @@ -42,14 +89,14 @@ def get_etf_holdings(
return q.all()


def get_etf_holdings_maxdate(db: Session, symbols: str):
def get_etf_holdings_dates(db: Session, symbols: str):
return (
db.query(
func.min(Holding.date).label("mindate"),
func.max(Holding.date).label("maxdate"),
)
.filter(Holding.fund.in_([s for s in symbols]))
.one()
.group_by(Holding.fund)
.all()
)


Expand Down
22 changes: 14 additions & 8 deletions app/api/v2/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,30 @@ async def etf_holdings(
}

if symbols:
holding_dates = crud.get_etf_holdings_maxdate(db, symbols=symbols)
holdings = None
holding_dates = crud.get_etf_holdings_dates(db, symbols=symbols)
min_date = min(holding_dates)[0]
max_date = max(holding_dates)[0]

if not date_from and not date_to:
date_from = holding_dates.maxdate
date_to = holding_dates.maxdate
date_from = min_date
date_to = max_date

holdings = crud.get_etf_current_holdings(db, symbols=symbols, limit=limit)

elif not date_to:
date_to = holding_dates.maxdate
date_to = max_date

elif not date_from:
date_from = holding_dates.mindate
date_from = min_date

if date_from > date_to:
date_from = date_to

holdings = crud.get_etf_holdings(
db, symbols=symbols, date_from=date_from, date_to=date_to, limit=limit
)
if not holdings:
holdings = crud.get_etf_holdings(
db, symbols=symbols, date_from=date_from, date_to=date_to, limit=limit
)

data["date_from"] = date_from
data["date_to"] = date_to
Expand Down
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OpenAPI settings
OPENAPI_TITLE = "ARK Invest API"
OPENAPI_API_VERSION = "2.2.0"
OPENAPI_API_VERSION = "2.2.1"
OPENAPI_DESCRIPTION = "API for tracking ARK Invest fund holdings and trades. This site is not affiliated with Ark Invest."
OPENAPI_CONTACT = "api (at) arkfunds.io"
OPENAPI_HOST = "arkfunds.io"
Expand Down

0 comments on commit a4c6b21

Please sign in to comment.