Skip to content

Commit

Permalink
tweak (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
TexasCoding authored Jun 26, 2024
1 parent efad654 commit a9202e3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/alpaca_daily_losers/liquidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from alpaca_daily_losers.global_functions import send_message, send_position_messages

LIQUIDATE_PERCENTAGE = 1.0


class Liquidate:
FIXED_FEE = 5.00

def __init__(self, trading_client: Trading, py_logger: logging.Logger):
self.trade = trading_client
Expand All @@ -25,7 +26,7 @@ def calculate_cash_needed(total_holdings: float, cash_row: pd.DataFrame) -> floa
Returns:
float: The amount of cash needed for liquidation, including a fixed fee of $5.00.
"""
return (total_holdings * 0.1 - cash_row["market_value"].iloc[0]) + Liquidate.FIXED_FEE
return total_holdings * 0.1 - cash_row["market_value"].iloc[0]

@staticmethod
def get_top_performers(current_positions: pd.DataFrame) -> pd.DataFrame:
Expand All @@ -39,13 +40,11 @@ def get_top_performers(current_positions: pd.DataFrame) -> pd.DataFrame:
pd.DataFrame: DataFrame containing the top performers.
"""
non_cash_positions = current_positions[current_positions["symbol"] != "Cash"]
non_cash_positions = non_cash_positions[non_cash_positions["profit_pct"] > 0.5].sort_values(
by="profit_pct", ascending=False
)
non_cash_positions = non_cash_positions[
non_cash_positions["profit_pct"] > LIQUIDATE_PERCENTAGE
].sort_values(by="profit_pct", ascending=False)

return non_cash_positions.iloc[
: len(non_cash_positions) // 2 if len(non_cash_positions) > 1 else 1
]
return non_cash_positions.iloc[: len(non_cash_positions)]

def liquidate_positions(self) -> None:
"""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_liquidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def test_calculate_cash_needed():
cash_row = pd.DataFrame({"market_value": [200.0]})

# Test with a total_holdings value that would result in a positive cash direction after fee
assert Liquidate.calculate_cash_needed(3000.0, cash_row) == 105.0
assert Liquidate.calculate_cash_needed(3000.0, cash_row) == 100.0

# Test with a negative cash direction in cash_row and total_holdings being 2000.0
negative_cash_row = pd.DataFrame({"market_value": [-200.0]})
assert Liquidate.calculate_cash_needed(2000.0, negative_cash_row) == 405.00
assert Liquidate.calculate_cash_needed(0.0, cash_row) == -195.0
assert Liquidate.calculate_cash_needed(2000.0, negative_cash_row) == 400.00
assert Liquidate.calculate_cash_needed(0.0, cash_row) == -200.00


def test_liquidates_positions_when_cash_less_than_10_percent_with_sold_positions_with_empty(
Expand All @@ -31,7 +31,7 @@ def test_liquidates_positions_when_cash_less_than_10_percent_with_sold_positions
{
"symbol": ["AAPL", "GOOGL", "Cash"],
"market_value": [5000, 3000, 500],
"profit_pct": [2.0, 1.1, 0],
"profit_pct": [2.0, 0.4, 0],
}
)
mock_trade.positions.get_all.return_value = current_positions
Expand Down
2 changes: 1 addition & 1 deletion tests/test_liquidate4.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_calculate_cash_needed(self):
total_holdings = 13245.0
cash_row = pd.DataFrame([{"symbol": "Cash", "market_value": 0}])
cash_needed = self.liquidator.calculate_cash_needed(total_holdings, cash_row)
assert cash_needed == 1324.5 + 5.00
assert cash_needed == 1324.5

def test_get_top_performers(self):
current_positions = pd.DataFrame(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_liquidate5.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def test_calculate_cash_needed():
total_holdings = 10000.0
cash_row = pd.DataFrame({"market_value": [500.0]})
result = Liquidate.calculate_cash_needed(total_holdings, cash_row)
assert result == 505.0
assert result == 500.0


def test_get_top_performers():
current_positions = pd.DataFrame(
{"symbol": ["AAPL", "MSFT", "GOOGL", "Cash"], "profit_pct": [5, 20, 30, 0]}
{"symbol": ["AAPL", "MSFT", "GOOGL", "Cash"], "profit_pct": [0.2, 20, 30, 0]}
)
result = Liquidate.get_top_performers(current_positions)
expected = pd.DataFrame({"symbol": ["GOOGL", "MSFT"], "profit_pct": [30, 20]})
assert (
result["symbol"].tolist()
== expected["symbol"]
.iloc[: len(current_positions[current_positions["symbol"] != "Cash"]) // 2]
.iloc[: len(current_positions[current_positions["symbol"] != "Cash"])]
.tolist()
)

Expand Down

0 comments on commit a9202e3

Please sign in to comment.