Skip to content
Merged
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
Next Next commit
add error if no price is found for target weight
  • Loading branch information
kic committed Apr 25, 2021
commit 0b9fa20ddee9d9acb4e0127ae3c2a8fdfdaff8e1
15 changes: 9 additions & 6 deletions pandas-ta-quant/pandas_ta_quant/portfolio/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def value(self,
idx = (underlying, instrument)

current_nav = current_portfolio.loc[idx]["liquidation_value"].sum().item() if idx in current_portfolio.index else 0
if current_nav <= -np.inf:
raise ValueError(f"TargetWeight needs prices! Missing price for {idx}")

if price is not None:
qty = (target_nav - current_nav) / price
Expand Down Expand Up @@ -257,18 +259,19 @@ def get_current_position(self, underlying, instrument=None):

def evaluate_liquidation_value(pos: pd.Series):
res = pos[['quantity', 'nav', 'fee']].sum()
try:
tst = pos.index.to_list()[-1]
if isinstance(tst, tuple):
tst = tst[-1]
tst = pos.index.to_list()[-1]
if isinstance(tst, tuple):
tst = tst[-1]

inst = pos.name[1] if instrument is None else instrument

inst = pos.name[1] if instrument is None else instrument
try:
val = res['nav'] if inst == self.currency else \
self.prices.get_price(inst, tst, pos["currency"].values[-1])[1][0] * res["quantity"].item()
res["liquidation_value"] = val
except KeyError:
res["liquidation_value"] = -np.inf
_log.error(f"Failed to get price for {instrument} @ {tst}")
# _log.error(f"Failed to get price for {inst} @ {tst}")

if pos["currency"].values[-1] != self.currency:
# we need to get an FX rate as well
Expand Down