refactor: Convert numpy float64 values to Python floats in position calculation

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 22:55:12 -08:00
parent d1a48dfb25
commit 6ca78eb83f
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -24,13 +24,13 @@ class PositionCalculator:
dict: Position details including shares and dollar amounts dict: Position details including shares and dollar amounts
""" """
# Calculate stop loss price (6% below entry) # Calculate stop loss price (6% below entry)
stop_loss = entry_price * (1 - self.stop_loss_percentage) stop_loss = float(entry_price * (1 - self.stop_loss_percentage))
# Calculate risk amount in dollars # Calculate risk amount in dollars
risk_amount = self.account_size * self.risk_percentage risk_amount = float(self.account_size * self.risk_percentage)
# Calculate per-share risk # Calculate per-share risk
risk_per_share = abs(entry_price - stop_loss) risk_per_share = float(abs(entry_price - stop_loss))
if risk_per_share == 0: if risk_per_share == 0:
raise ValueError("Entry price cannot equal stop loss price") raise ValueError("Entry price cannot equal stop loss price")
@ -39,7 +39,7 @@ class PositionCalculator:
shares = int(risk_amount / risk_per_share) shares = int(risk_amount / risk_per_share)
# Calculate total position value # Calculate total position value
position_value = shares * entry_price position_value = float(shares * entry_price)
result = { result = {
"shares": shares, "shares": shares,
@ -47,15 +47,16 @@ class PositionCalculator:
"risk_amount": risk_amount, "risk_amount": risk_amount,
"risk_per_share": risk_per_share, "risk_per_share": risk_per_share,
"stop_loss": stop_loss, "stop_loss": stop_loss,
"potential_loss": shares * (stop_loss - entry_price) "potential_loss": float(shares * (stop_loss - entry_price))
} }
# Add target price calculations if provided # Add target price calculations if provided
if target_price: if target_price:
target_price = float(target_price)
result.update({ result.update({
"target_price": target_price, "target_price": target_price,
"potential_profit": shares * (target_price - entry_price), "potential_profit": float(shares * (target_price - entry_price)),
"risk_reward_ratio": abs((target_price - entry_price) / (entry_price - stop_loss)) "risk_reward_ratio": float(abs((target_price - entry_price) / (entry_price - stop_loss)))
}) })
return result return result