misc
This commit is contained in:
Binary file not shown.
Binary file not shown.
@ -373,7 +373,7 @@ class DataInterface:
|
||||
|
||||
return df_copy
|
||||
|
||||
def calculate_pnl(self, predictions, actual_prices, position_size=1.0):
|
||||
def calculate_pnl(self, predictions, actual_prices, position_size=1.0, fee_rate=0.0002):
|
||||
"""
|
||||
Robust PnL calculator that handles:
|
||||
- Action predictions (0=SELL, 1=HOLD, 2=BUY)
|
||||
@ -384,6 +384,7 @@ class DataInterface:
|
||||
predictions: Array of predicted actions or probabilities
|
||||
actual_prices: Array of actual prices (can be 1D or 2D OHLC format)
|
||||
position_size: Position size multiplier
|
||||
fee_rate: Trading fee rate (default: 0.0002 for 0.02% per trade)
|
||||
|
||||
Returns:
|
||||
tuple: (total_pnl, win_rate, trades)
|
||||
@ -443,13 +444,33 @@ class DataInterface:
|
||||
price_change = (next_price - current_price) / current_price
|
||||
|
||||
if action == 2: # BUY
|
||||
trade_pnl = price_change * position_size
|
||||
# Calculate raw PnL
|
||||
raw_pnl = price_change * position_size
|
||||
|
||||
# Calculate fees (entry and exit)
|
||||
entry_fee = position_size * fee_rate
|
||||
exit_fee = position_size * (1 + price_change) * fee_rate
|
||||
total_fees = entry_fee + exit_fee
|
||||
|
||||
# Net PnL after fees
|
||||
trade_pnl = raw_pnl - total_fees
|
||||
|
||||
trade_type = 'BUY'
|
||||
is_win = price_change > 0
|
||||
is_win = trade_pnl > 0
|
||||
elif action == 0: # SELL
|
||||
trade_pnl = -price_change * position_size
|
||||
# Calculate raw PnL
|
||||
raw_pnl = -price_change * position_size
|
||||
|
||||
# Calculate fees (entry and exit)
|
||||
entry_fee = position_size * fee_rate
|
||||
exit_fee = position_size * (1 - price_change) * fee_rate
|
||||
total_fees = entry_fee + exit_fee
|
||||
|
||||
# Net PnL after fees
|
||||
trade_pnl = raw_pnl - total_fees
|
||||
|
||||
trade_type = 'SELL'
|
||||
is_win = price_change < 0
|
||||
is_win = trade_pnl > 0
|
||||
else:
|
||||
continue # Invalid action
|
||||
|
||||
@ -462,6 +483,8 @@ class DataInterface:
|
||||
'entry': current_price,
|
||||
'exit': next_price,
|
||||
'pnl': trade_pnl,
|
||||
'raw_pnl': price_change * position_size if trade_type == 'BUY' else -price_change * position_size,
|
||||
'fees': total_fees,
|
||||
'win': is_win,
|
||||
'duration': 1 # In number of candles
|
||||
})
|
||||
|
Reference in New Issue
Block a user