# Create a Python function to calculate the profit for Spot Market

I have a Python method that calculate the profit after taking into account the commission structure. However, it fails to replicate the exact values from Binance trade history. For example I bought `ETH/USDT` using `LIMIT` order at a price of `2595`with a buy amount of `57.609 USDT`. Then I sold using `LIMIT` order at a price of `2700`. As per my understanding the precision value for this pair is 8 but still the calculator fails to give correct results. Below is the code I am using.

``````def calculate_profit_after_commission_binance(buy_price: float, buy_amount_usdt: float, sell_price: float,
order_type_buy: str = 'MARKET', order_type_sell: str = 'MARKET',
fee_rate_maker: float = 0.001, fee_rate_taker: float = 0.001) -> float:
else:

if order_type_sell == 'MARKET':
sell_fee_rate = fee_rate_taker
else:
sell_fee_rate = fee_rate_maker

# Calculate the amount of asset bought

# Deduct commission on the asset bought
asset_quantity_available = asset_quantity * (1 - buy_fee_rate)

# Selling commission in USDT
sell_commission = asset_quantity_available * sell_price * sell_fee_rate

# Profit after sell commission
profit = asset_quantity_available * sell_price - sell_commission

return profit

``````

order_type_buy: str = ‘MARKET’, order_type_sell: str = ‘MARKET’,
fee_rate_maker: float = 0.001, fee_rate_taker: float = 0.001) → float:
else:

``````if order_type_sell == 'MARKET':
sell_fee_rate = fee_rate_taker
else:
sell_fee_rate = fee_rate_maker

# Calculate the amount of asset bought

# Deduct commission on the asset bought
asset_quantity_available = asset_quantity * (1 - buy_fee_rate)

# Selling commission in USDT
sell_commission = asset_quantity_available * sell_price * sell_fee_rate

# Error: Did not subtract initial investment to find net profit
# profit = asset_quantity_available * sell_price - sell_commission
# Corrected: Subtract the initial amount spent to buy the asset to get the actual net profit
gross_sell_amount = asset_quantity_available * sell_price  # Total amount received from selling the asset
net_sell_amount = gross_sell_amount - sell_commission      # Net amount after selling commission
net_profit = net_sell_amount - buy_amount_usdt             # Correction: Subtracting initial amount spent to find net profit

return net_profit
``````
• the total amount received from selling the asset before any fees was not explicitly defined in your original code, which made it difficult to see the transaction flow.

• Net Sell Amount is the amount after subtracting the selling commission. It is computed to clarify how much is left after the sell fee.

• Net Profit Calculation was calculated without subtracting the initial buy amount. I’ve corrected this by subtracting the `buy_amount_usdt` from the `net_sell_amount` to yield the actual profit after all transactions and fees.

As per the Binance trade history the sell side commission does not match with the code result. Secondly, the profit value is also different as shown in the screenshot from Binance trade history.

Are you using BNB to pay fee or other coins? Since BNB coing payments are discounted at 0.075% against the other coin payments of 0.1%.

No, I don’t own BNB