币安 - 现货市场利润计算器

问题描述 投票:0回答:1

我有一个Python方法,可以在考虑佣金结构后计算利润。然而,它无法复制币安交易历史中的确切值。例如,我使用

ETH/USDT
订单以
LIMIT
的价格购买了
2595
,购买金额为
57.609 USDT
。然后我使用
LIMIT
订单以
2700
的价格出售。据我了解,这对的精度值为 8,但计算器仍然无法给出正确的结果。下面是我正在使用的代码。

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:
    if order_type_buy == 'MARKET':
        buy_fee_rate = fee_rate_taker
    else:
        buy_fee_rate = fee_rate_maker

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

    # Calculate the amount of asset bought
    asset_quantity = buy_amount_usdt / buy_price
    
    # 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 - buy_amount_usdt
    
    return profit

enter image description here

python trading binance
1个回答
0
投票

利润计算与binnacle交易不匹配的原因是可用资产数量与卖单数量不同。根据您的方法计算和输入中提供的结果,

asset_quantity_available
应为 0.0221778。然而,在卖出订单上,卖出的资产数量为 0.0221。我的猜测是,binance 在其卖出订单中只能允许 4 位浮点精度,并将其余部分四舍五入以避免超卖。我认为你可以使用 python 中的
math.floor
函数来执行向下舍入操作

asset_quantity_available_rounded = math.floor(asset_quantity_available * 10000) / 10000.0

当您使用四舍五入的可用资产数量来计算卖出费用时,您会看到费用为 0.05967,与卖单费用 0.05967 USDT 相匹配。当您乘以

asset_quantity_available_rounded
sell_price
时,您会看到总卖出价格为 59.67 USDT(不包括卖出费用),与上面卖出订单上的数字相符

我认为您的计算是正确的,但不匹配是由于销售金额与实际可用金额不同。您可以在函数中添加地板计算,如下面的代码

import math

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:
    if order_type_buy == 'MARKET':
        buy_fee_rate = fee_rate_taker
    else:
        buy_fee_rate = fee_rate_maker

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

    # Calculate the amount of asset bought
    asset_quantity = buy_amount_usdt / buy_price
    
    # Deduct commission on the asset bought
    asset_quantity_available = asset_quantity * (1 - buy_fee_rate)

    # Round down the asset quantity tradable on binance
    asset_quantity_available_floor = math.floor(asset_quantity_available * 10000) / 10000.0
    
    # Selling commission in USDT
    sell_commission = asset_quantity_available_floor * sell_price * sell_fee_rate

    # Profit after sell commission
    profit = asset_quantity_available_floor * sell_price - sell_commission - buy_amount_usdt
    
    return profit
© www.soinside.com 2019 - 2024. All rights reserved.