Skip to content

Commit

Permalink
refactor: remove useless param in simulations and small improvements …
Browse files Browse the repository at this point in the history
…on some models
  • Loading branch information
gianlucapagliara committed Jan 13, 2025
1 parent 29ab4f6 commit 1c77968
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 186 deletions.
6 changes: 6 additions & 0 deletions financepype/owners/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from financepype.owners.owner_id import OwnerIdentifier
from financepype.platforms.centralized import CentralizedPlatform


class AccountIdentifier(OwnerIdentifier):
platform: CentralizedPlatform
4 changes: 4 additions & 0 deletions financepype/platforms/centralized.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ class CentralizedPlatform(Platform):
default=None,
description="The sub-identifier for the platform",
)
domain: str | None = Field(
default=None,
description="The domain for the platform",
)
2 changes: 1 addition & 1 deletion financepype/secrets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SubaccountSecrets(BaseModel):
subaccount_name: str
api_key: SecretStr
api_secret: SecretStr
api_passphrase: SecretStr | None
api_passphrase: SecretStr | None = None


class ExchangeSecrets(BaseModel):
Expand Down
11 changes: 5 additions & 6 deletions financepype/simulations/balances/engines/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import streamlit as st

from financepype.markets.trading_pair import TradingPair
from financepype.operations.fees import FeeImpactType, FeeType
from financepype.operations.fees import OperationFee as Fee
from financepype.operations.fees import FeeImpactType, FeeType, OperationFee
from financepype.operations.orders.models import OrderType, PositionAction, TradeType
from financepype.platforms.platform import Platform
from financepype.platforms.centralized import CentralizedPlatform
from financepype.rules.trading_rule import DerivativeTradingRule, TradingRule
from financepype.simulations.balances.engines.models import AssetCashflow, OrderDetails
from financepype.simulations.balances.engines.multiengine import BalanceMultiEngine
Expand All @@ -25,7 +24,7 @@ def create_sample_order(
fee_amount: Decimal = Decimal("0.1"),
) -> OrderDetails:
"""Create a sample order for simulation."""
platform = Platform(platform_id)
platform = CentralizedPlatform(identifier=platform_id)

trading_pair_obj = TradingPair(name=trading_pair)

Expand Down Expand Up @@ -55,7 +54,7 @@ def create_sample_order(
leverage=1,
position_action=position_action,
entry_index_price=price,
fee=Fee(
fee=OperationFee(
asset=None,
fee_type=fee_type,
impact_type=fee_impact,
Expand Down Expand Up @@ -185,7 +184,7 @@ def main() -> None:
st.json(involved_assets)

# Complete simulation
simulation = engine.get_complete_simulation(order, current_balances={})
simulation = engine.get_complete_simulation(order)

# Display cashflow simulation
st.subheader("Cashflow Simulation")
Expand Down
46 changes: 10 additions & 36 deletions financepype/simulations/balances/engines/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
... ]
...
... @classmethod
... def get_opening_outflows(cls, operation_details, current_balances):
... def get_opening_outflows(cls, operation_details):
... return [
... AssetCashflow(
... asset=operation_details.trading_pair.base_asset,
Expand All @@ -51,10 +51,8 @@
"""

from abc import ABC, abstractmethod
from decimal import Decimal
from typing import Any

from financepype.assets.asset import Asset
from financepype.simulations.balances.engines.models import (
AssetCashflow,
OperationSimulationResult,
Expand Down Expand Up @@ -86,7 +84,6 @@ class BalanceEngine(ABC):
>>> engine = MyBalanceEngine()
>>> result = engine.get_complete_simulation(
... operation_details=order,
... current_balances={"BTC": Decimal("1.0")}
... )
>>> print(result.opening_outflows) # Assets used to open position
"""
Expand Down Expand Up @@ -114,11 +111,7 @@ def get_involved_assets(cls, operation_details: Any) -> list[AssetCashflow]:

@classmethod
@abstractmethod
def get_opening_outflows(
cls,
operation_details: Any,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_opening_outflows(cls, operation_details: Any) -> list[AssetCashflow]:
"""Get all assets leaving the account at position opening.
This method calculates all assets that will leave the account when opening
Expand All @@ -129,7 +122,6 @@ def get_opening_outflows(
Args:
operation_details: Details of the operation (e.g., OrderDetails)
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing outflows at opening
Expand All @@ -142,11 +134,7 @@ def get_opening_outflows(

@classmethod
@abstractmethod
def get_opening_inflows(
cls,
operation_details: Any,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_opening_inflows(cls, operation_details: Any) -> list[AssetCashflow]:
"""Get all assets entering the account at position opening.
This method calculates all assets that will enter the account when opening
Expand All @@ -158,7 +146,6 @@ def get_opening_inflows(
Args:
operation_details: Details of the operation (e.g., OrderDetails)
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing inflows at opening
Expand All @@ -171,11 +158,7 @@ def get_opening_inflows(

@classmethod
@abstractmethod
def get_closing_outflows(
cls,
operation_details: Any,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_closing_outflows(cls, operation_details: Any) -> list[AssetCashflow]:
"""Get all assets leaving the account at position closing.
This method calculates all assets that will leave the account when closing
Expand All @@ -187,7 +170,6 @@ def get_closing_outflows(
Args:
operation_details: Details of the operation (e.g., OrderDetails)
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing outflows at closing
Expand All @@ -200,11 +182,7 @@ def get_closing_outflows(

@classmethod
@abstractmethod
def get_closing_inflows(
cls,
operation_details: Any,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_closing_inflows(cls, operation_details: Any) -> list[AssetCashflow]:
"""Get all assets entering the account at position closing.
This method calculates all assets that will enter the account when closing
Expand All @@ -216,7 +194,6 @@ def get_closing_inflows(
Args:
operation_details: Details of the operation (e.g., OrderDetails)
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing inflows at closing
Expand All @@ -229,9 +206,7 @@ def get_closing_inflows(

@classmethod
def get_complete_simulation(
cls,
operation_details: Any,
current_balances: dict[Asset, Decimal],
cls, operation_details: Any
) -> OperationSimulationResult:
"""Get a complete simulation of all cashflows for the operation.
Expand All @@ -245,7 +220,6 @@ def get_complete_simulation(
Args:
operation_details: Details of the operation (e.g., OrderDetails)
current_balances: Current balances of all assets
Returns:
OperationSimulationResult containing all cashflows
Expand All @@ -258,10 +232,10 @@ def get_complete_simulation(
result = OperationSimulationResult(
operation_details=operation_details,
cashflows=[
*cls.get_opening_outflows(operation_details, current_balances),
*cls.get_opening_inflows(operation_details, current_balances),
*cls.get_closing_outflows(operation_details, current_balances),
*cls.get_closing_inflows(operation_details, current_balances),
*cls.get_opening_outflows(operation_details),
*cls.get_opening_inflows(operation_details),
*cls.get_closing_outflows(operation_details),
*cls.get_closing_inflows(operation_details),
],
)
return result
41 changes: 8 additions & 33 deletions financepype/simulations/balances/engines/multiengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@
>>> # Simulate the order
>>> result = BalanceMultiEngine.get_complete_simulation(
... order_details=order,
... current_balances={"BTC": Decimal("1.0")}
... )
>>>
>>> # The simulation is handled by SpotBalanceEngine internally
>>> print(result.opening_outflows) # Shows the cost of the trade
"""

from decimal import Decimal

from financepype.assets.asset import Asset
from financepype.markets.market import InstrumentType
from financepype.markets.trading_pair import TradingPair
from financepype.simulations.balances.engines.engine import BalanceEngine
Expand Down Expand Up @@ -78,7 +74,6 @@ class BalanceMultiEngine(BalanceEngine):
>>> # Simulate the order
>>> result = BalanceMultiEngine.get_complete_simulation(
... order_details=order,
... current_balances={"BTC": Decimal("1.0")}
... )
"""

Expand Down Expand Up @@ -140,19 +135,14 @@ def get_involved_assets(cls, order_details: OrderDetails) -> list[AssetCashflow]
return engine.get_involved_assets(order_details)

@classmethod
def get_opening_outflows(
cls,
order_details: OrderDetails,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_opening_outflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
"""Get all assets leaving the account at position opening.
This method delegates to the appropriate specialized engine based on
the trading pair's instrument type.
Args:
order_details: Complete specification of the order
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing outflows at opening
Expand All @@ -162,22 +152,17 @@ def get_opening_outflows(
>>> print(flows[0].amount) # Cost of opening the position
"""
engine = cls.get_engine(order_details.trading_pair)
return engine.get_opening_outflows(order_details, current_balances)
return engine.get_opening_outflows(order_details)

@classmethod
def get_opening_inflows(
cls,
order_details: OrderDetails,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_opening_inflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
"""Get all assets entering the account at position opening.
This method delegates to the appropriate specialized engine based on
the trading pair's instrument type.
Args:
order_details: Complete specification of the order
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing inflows at opening
Expand All @@ -187,22 +172,17 @@ def get_opening_inflows(
>>> print(flows[0].reason) # CashflowReason.FEE for rebates
"""
engine = cls.get_engine(order_details.trading_pair)
return engine.get_opening_inflows(order_details, current_balances)
return engine.get_opening_inflows(order_details)

@classmethod
def get_closing_outflows(
cls,
order_details: OrderDetails,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_closing_outflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
"""Get all assets leaving the account at position closing.
This method delegates to the appropriate specialized engine based on
the trading pair's instrument type.
Args:
order_details: Complete specification of the order
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing outflows at closing
Expand All @@ -212,22 +192,17 @@ def get_closing_outflows(
>>> print(flows[0].reason) # CashflowReason.FEE
"""
engine = cls.get_engine(order_details.trading_pair)
return engine.get_closing_outflows(order_details, current_balances)
return engine.get_closing_outflows(order_details)

@classmethod
def get_closing_inflows(
cls,
order_details: OrderDetails,
current_balances: dict[Asset, Decimal],
) -> list[AssetCashflow]:
def get_closing_inflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
"""Get all assets entering the account at position closing.
This method delegates to the appropriate specialized engine based on
the trading pair's instrument type.
Args:
order_details: Complete specification of the order
current_balances: Current balances of all assets
Returns:
List of AssetCashflow objects representing inflows at closing
Expand All @@ -237,4 +212,4 @@ def get_closing_inflows(
>>> print(flows[0].reason) # CashflowReason.OPERATION
"""
engine = cls.get_engine(order_details.trading_pair)
return engine.get_closing_inflows(order_details, current_balances)
return engine.get_closing_inflows(order_details)
16 changes: 4 additions & 12 deletions financepype/simulations/balances/engines/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@ def get_involved_assets(cls, order_details: OrderDetails) -> list[AssetCashflow]
return result

@classmethod
def get_opening_outflows(
cls, order_details: OrderDetails, current_balances: dict[Asset, Decimal]
) -> list[AssetCashflow]:
def get_opening_outflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
result: list[AssetCashflow] = []
collateral_asset = cls._get_outflow_asset(order_details)

Expand Down Expand Up @@ -460,9 +458,7 @@ def get_opening_outflows(
return result

@classmethod
def get_opening_inflows(
cls, order_details: OrderDetails, current_balances: dict[Asset, Decimal]
) -> list[AssetCashflow]:
def get_opening_inflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
result: list[AssetCashflow] = []
collateral_asset = cls._get_outflow_asset(order_details)

Expand All @@ -485,9 +481,7 @@ def get_opening_inflows(
return result

@classmethod
def get_closing_outflows(
cls, order_details: OrderDetails, current_balances: dict[Asset, Decimal]
) -> list[AssetCashflow]:
def get_closing_outflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
result: list[AssetCashflow] = []
collateral_asset = cls._get_outflow_asset(order_details)

Expand Down Expand Up @@ -525,9 +519,7 @@ def get_closing_outflows(
return result

@classmethod
def get_closing_inflows(
cls, order_details: OrderDetails, current_balances: dict[Asset, Decimal]
) -> list[AssetCashflow]:
def get_closing_inflows(cls, order_details: OrderDetails) -> list[AssetCashflow]:
result: list[AssetCashflow] = []
collateral_asset = cls._get_outflow_asset(order_details)

Expand Down
Loading

0 comments on commit 1c77968

Please sign in to comment.