refactor: rename WalletHistory fieldname from price to rate
This commit is contained in:
@@ -39,7 +39,7 @@ def convert_bt_wallet_collection(wallet_captures: list[tuple]) -> DataFrame:
|
||||
return DataFrame()
|
||||
return DataFrame(
|
||||
wallet_captures,
|
||||
columns=["date", "currency", "price", "balance"],
|
||||
columns=["date", "currency", "rate", "balance"],
|
||||
)
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ def generate_wallet_stats(wallet_df: DataFrame, stake_currency: str) -> dict[str
|
||||
"""Generate wallet statistics from the wallet DataFrame."""
|
||||
if wallet_df is None or wallet_df.empty:
|
||||
return {}
|
||||
wallet_df.loc[:, "total"] = wallet_df["price"] * wallet_df["balance"]
|
||||
wallet_df.loc[:, "total"] = wallet_df["rate"] * wallet_df["balance"]
|
||||
# Group by date to get total wallet value at each timestamp
|
||||
wallet = wallet_df.groupby("date")["total"].sum().reset_index()
|
||||
start_balance = wallet.iloc[0]["total"]
|
||||
|
||||
@@ -18,7 +18,7 @@ class WalletHistory(ModelBase):
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, index=True)
|
||||
currency: Mapped[str] = mapped_column(String(25), nullable=False)
|
||||
price: Mapped[float] = mapped_column(Float, nullable=True)
|
||||
rate: Mapped[float] = mapped_column(Float, nullable=True)
|
||||
balance: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
leverage: Mapped[float] = mapped_column(Float, nullable=False, default=1.0)
|
||||
bot_managed: Mapped[bool] = mapped_column(nullable=False, default=True)
|
||||
@@ -31,5 +31,5 @@ class WalletHistory(ModelBase):
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"WalletHistory(timestamp={self.timestamp}, currency={self.currency}, "
|
||||
f"price={self.price}, balance={self.balance}, leverage={self.leverage})"
|
||||
f"rate={self.rate}, balance={self.balance}, leverage={self.leverage})"
|
||||
)
|
||||
|
||||
@@ -791,7 +791,7 @@ class RPC:
|
||||
:return: DataFrame with the balance history and the timestamp of the migration
|
||||
"""
|
||||
results = read_sql("wallet_history", con=Trade.session.bind, parse_dates=["timestamp"])
|
||||
results.loc[:, "total"] = results["price"] * results["balance"] / results["leverage"]
|
||||
results.loc[:, "total"] = results["rate"] * results["balance"] / results["leverage"]
|
||||
results = results.rename({"timestamp": "date"}, axis=1)
|
||||
results.loc[:, "__date_ts"] = results.loc[:, "date"].astype("int64") // 1000 // 1000
|
||||
# Exclude non-bot managed for now
|
||||
|
||||
@@ -85,7 +85,7 @@ def _migrate_wallet_history(config: Config, exchange: Exchange, starting_balance
|
||||
pair_leverage_idx = {
|
||||
pair: balance_dist.columns.get_loc(f"{pair}_leverage") + 1 for pair in pairlist_valid
|
||||
}
|
||||
pair_price_idx = {
|
||||
pair_rate_idx = {
|
||||
pair: balance_dist.columns.get_loc(f"{pair}_open") + 1 for pair in pairlist_valid
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ def _migrate_wallet_history(config: Config, exchange: Exchange, starting_balance
|
||||
WalletHistory(
|
||||
timestamp=date,
|
||||
currency=stake_currency,
|
||||
price=1.0, # Stake currency price is always 1.0
|
||||
rate=1.0, # Stake currency price is always 1.0
|
||||
balance=stake_balance,
|
||||
leverage=1.0,
|
||||
bot_managed=True,
|
||||
@@ -115,14 +115,14 @@ def _migrate_wallet_history(config: Config, exchange: Exchange, starting_balance
|
||||
leverage_value = row[pair_leverage_idx[pair]]
|
||||
# Only add entry if balance is not empty/NaN
|
||||
if not pd.isna(balance_value) and balance_value > 0:
|
||||
price_value = row[pair_price_idx[pair]]
|
||||
price = price_value if not pd.isna(price_value) else None
|
||||
rate_value = row[pair_rate_idx[pair]]
|
||||
rate = rate_value if not pd.isna(rate_value) else None
|
||||
|
||||
wallet_entries.append(
|
||||
WalletHistory(
|
||||
timestamp=date,
|
||||
currency=base_currency,
|
||||
price=price,
|
||||
rate=rate,
|
||||
balance=balance_value,
|
||||
leverage=leverage_value if not pd.isna(leverage_value) else 1.0,
|
||||
bot_managed=True,
|
||||
|
||||
@@ -465,7 +465,7 @@ class Wallets:
|
||||
position_record = WalletHistory(
|
||||
timestamp=timestamp,
|
||||
currency=position.symbol,
|
||||
price=rate,
|
||||
rate=rate,
|
||||
balance=position.position,
|
||||
leverage=position.leverage or 1.0,
|
||||
bot_managed=base in open_assets,
|
||||
@@ -483,7 +483,7 @@ class Wallets:
|
||||
wallet_record = WalletHistory(
|
||||
timestamp=timestamp,
|
||||
currency=wallet.currency,
|
||||
price=rate,
|
||||
rate=rate,
|
||||
balance=wallet.total
|
||||
- (position_collaterals if wallet.currency == self._stake_currency else 0),
|
||||
leverage=1.0,
|
||||
|
||||
@@ -643,10 +643,10 @@ def test_record_wallet_state_stores_wallet_history(mocker, default_conf):
|
||||
|
||||
records_by_currency = {entry.currency: entry for entry in wallet_entries}
|
||||
assert records_by_currency[stake_currency].balance == 1.5
|
||||
assert records_by_currency[stake_currency].price == 1.0
|
||||
assert records_by_currency["ETH"].price == 0.5
|
||||
assert records_by_currency[stake_currency].rate == 1.0
|
||||
assert records_by_currency["ETH"].rate == 0.5
|
||||
assert records_by_currency["ETH/BTC"].balance == 0.8
|
||||
assert records_by_currency["ETH/BTC"].price == 2500.0
|
||||
assert records_by_currency["ETH/BTC"].rate == 2500.0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_persistence")
|
||||
|
||||
@@ -232,14 +232,14 @@ def test_migrate_wallet_history_with_multiple_pairs(default_conf_usdt, fee, time
|
||||
|
||||
# Stake currency should have price = 1.0
|
||||
for entry in usdt_entries:
|
||||
assert entry.price == 1.0
|
||||
assert entry.rate == 1.0
|
||||
|
||||
eth_entries = [e for e in wallet_entries if e.currency == "ETH"]
|
||||
btc_entries = [e for e in wallet_entries if e.currency == "BTC"]
|
||||
assert len(eth_entries) == 4
|
||||
assert len(btc_entries) == 2
|
||||
assert all(entry.price and entry.price > 1400 and entry.price < 1600 for entry in eth_entries)
|
||||
assert all(entry.price and entry.price > 29000 and entry.price < 31000 for entry in btc_entries)
|
||||
assert all(entry.rate and entry.rate > 1400 and entry.rate < 1600 for entry in eth_entries)
|
||||
assert all(entry.rate and entry.rate > 29000 and entry.rate < 31000 for entry in btc_entries)
|
||||
assert all(entry.balance == 10 for entry in btc_entries)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user