Untitled

mail@pastecode.io avatar
unknown
java
a month ago
2.9 kB
1
Indexable
Never
class TradingCalculator:
    def __init__(self, broker="Binance"):
        self.broker = broker
        self.set_fees()

    def set_fees(self):
        # Set fees for Binance and Bybit
        if self.broker == "Binance":
            self.market_order_fee = 0.001  # 0.1%
            self.limit_order_fee = 0.001  # 0.1%
        elif self.broker == "Bybit":
            self.market_order_fee = 0.00075  # 0.075%
            self.limit_order_fee = -0.00025  # -0.025% (rebate)
        else:
            raise ValueError("Unsupported broker. Please choose Binance or Bybit.")

    def calculate_fees(self, trade_size, order_type="market"):
        # Calculate the fee based on order type
        if order_type == "market":
            fee = trade_size * self.market_order_fee
        elif order_type == "limit":
            fee = trade_size * self.limit_order_fee
        else:
            raise ValueError("Unsupported order type. Please choose market or limit.")
        return fee

    def calculate_pnl(self, trades, win_rate, avg_trade_size, profit_factor):
        # Calculate profit and loss
        wins = trades * (win_rate / 100)
        losses = trades - wins

        # Assume profit factor is the ratio of profit to risk
        avg_profit_per_trade = avg_trade_size * profit_factor
        avg_loss_per_trade = avg_trade_size

        total_profit = wins * avg_profit_per_trade
        total_loss = losses * avg_loss_per_trade

        return total_profit, total_loss

    def calculate_total(self, trades, win_rate, avg_trade_size, profit_factor, order_type="market"):
        # Calculate profit, loss, and fees
        total_profit, total_loss = self.calculate_pnl(trades, win_rate, avg_trade_size, profit_factor)
        total_fees = 0
        for _ in range(trades):
            total_fees += self.calculate_fees(avg_trade_size, order_type)

        net_profit = total_profit - total_loss - total_fees
        return {
            "total_profit": total_profit,
            "total_loss": total_loss,
            "total_fees": total_fees,
            "net_profit": net_profit
        }


# Example usage
if __name__ == "__main__":
    # User inputs
    broker = "Binance"  # Can be "Binance" or "Bybit"
    trades = 100  # Number of trades
    win_rate = 60  # Win rate percentage
    avg_trade_size = 1000  # Average trade size in USD
    profit_factor = 1.5  # Profit factor (profit per winning trade relative to risk)
    order_type = "market"  # Can be "market" or "limit"

    # Initialize calculator
    calculator = TradingCalculator(broker)

    # Calculate results
    results = calculator.calculate_total(trades, win_rate, avg_trade_size, profit_factor, order_type)

    # Output the results
    print(f"Total Profit: ${results['total_profit']:.2f}")
    print(f"Total Loss: ${results['total_loss']:.2f}")
    print(f"Total Fees: ${results['total_fees']:.2f}")
    print(f"Net Profit: ${results['net_profit']:.2f}")
Leave a Comment