turtle_quant_1.backtesting.portfolio¶
turtle_quant_1.backtesting.portfolio
¶
Portfolio management for backtesting.
Portfolio
¶
Portfolio class to track holdings, capital, and transactions during backtesting.
Initialize the portfolio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_capital
|
float
|
Starting capital in dollars (default: 0.0). |
0.0
|
Source code in src/turtle_quant_1/backtesting/portfolio.py
buy
¶
buy(
symbol: str,
quantity: float,
take_profit_value: float,
stop_loss_value: float,
price: float,
timestamp: datetime,
) -> bool
Execute a buy order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol to buy. |
required |
quantity
|
float
|
Quantity to buy. |
required |
take_profit_value
|
float
|
Take profit value. |
required |
stop_loss_value
|
float
|
Stop loss value. |
required |
price
|
float
|
Price per unit. |
required |
timestamp
|
datetime
|
Time of transaction. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction was successful, False if insufficient funds. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
check_stop_loss_triggers
¶
Check if any stop losses have been triggered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_prices
|
Dict[str, float]
|
Dictionary of symbol -> current price. |
required |
Source code in src/turtle_quant_1/backtesting/portfolio.py
check_take_profit_triggers
¶
Check if any take profits have been triggered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_prices
|
Dict[str, float]
|
Dictionary of symbol -> current price. |
required |
Source code in src/turtle_quant_1/backtesting/portfolio.py
get_current_holdings
¶
Get current holdings.
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary of symbol -> quantity held. |
get_portfolio_value
¶
Calculate total portfolio value (cash + holdings).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_prices
|
Dict[str, float]
|
Dictionary of symbol -> current price. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total portfolio value in dollars. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
get_quantity_held
¶
Get quantity held for a specific symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The symbol to check. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Quantity held (0.0 if not held). |
Source code in src/turtle_quant_1/backtesting/portfolio.py
get_return_dollars
¶
Calculate total return in dollars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_prices
|
Dict[str, float]
|
Dictionary of symbol -> current price. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total return in dollars (final value - initial capital). |
Source code in src/turtle_quant_1/backtesting/portfolio.py
get_return_percent
¶
Calculate total return as percentage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_prices
|
Dict[str, float]
|
Dictionary of symbol -> current price. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total return as percentage. Returns 0.0 if initial capital was 0. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
get_summary
¶
Get portfolio summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_prices
|
Optional[Dict[str, float]]
|
Dictionary of symbol -> current price for valuation. |
None
|
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary with portfolio summary statistics. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
get_transaction_history
¶
Get transaction history as DataFrame.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with transaction history. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
sell
¶
Execute a sell order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol to sell. |
required |
quantity
|
float
|
Quantity to sell. |
required |
price
|
float
|
Price per unit. |
required |
timestamp
|
datetime
|
Time of transaction. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction was successful, False if insufficient holdings. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
sell_holdings
¶
Sell all holdings of a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol to sell. |
required |
price
|
float
|
Price per unit. |
required |
timestamp
|
datetime
|
Time of transaction. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if transaction was successful, False if no holdings. |
Source code in src/turtle_quant_1/backtesting/portfolio.py
set_stop_loss
¶
Set a stop loss for a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol to set stop loss for. |
required |
stop_loss
|
float
|
Stop loss trigger price. |
required |
Source code in src/turtle_quant_1/backtesting/portfolio.py
set_take_profit
¶
Set a take profit for a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Symbol to set take profit for. |
required |
take_profit
|
float
|
Take profit trigger price. |
required |