Calculating commission for SL, TP and limits in python

Hello, everyone.
I want to create a trading sandbox for myself to test strategies but not risk real money. Testnet is isolated and can be considered dead, so it is not relevant to me.
But I have problems with certain calculations, namely the calculation of commissions (binance future order fee) for opening (closing) positions, partial closing (increasing) of a position and a separate issue with position addition, how to calculate the new entry price, commissions, profit, etc. when adding a position.
P.S. I know that margin price often appears in the formulas, but this will be a closed system, I have only the entry and exit price it will collect the basic parameters about trading pairs (minimum and maximum position, leverage, etc.) and the exchange will not perform any more operations, except for determining the current price.

Well, if we talk about commissions, then from the official website, if we do not take into account VIP, payment by BNB, etc., the formula is approximately as follows:

order fee = dollar * leverage * 0.0004
dollar = coin * price / leverage
order fee = coin * price * 0.0004

0.0004 is a 0.04% commission for opening or closing a position. Now I am talking about buying and selling on the market, if we take into account limit orders, then limit_order_fee = order_fee / 2. Again, this is rough.

Accordingly, if, for example, I opened a long ETHUSDT at a price of 1400 per 50 coins ($700), then when creating a long order, open_long_order_fee = 1400 * 50 * 0.0004 = $28.
I think it should be the same with a short position, I’m doing rough math.
The price has reached 1450, I want to close the position.
order_close_fee = 1450 * 50 * 0.0004 = $29
profit = (1 / input_price - 1 / last_price) * coin * long_short * input_price * last_price = $2500
long_short = 1 # if buy
long_short = -1 # if sell
In total, a commission of $57 will be deducted from my profit, for a total profit of $2443
But if my strategy was wrong and the price dropped to 1350, what is the commission?
sl_order_fee = 1350 * 50 * 0.0004 = $27 (O_o) I think I’m thinking badly somewhere and maybe I’m not taking something into account, because it looks like the exchange took pity on me and gave me $1 in gratis)))
The same issue with partial position closure. Theoretically, if I close 20%(10 coins) of the position in price 1410, then limit1_fee = 1410 * 10 * 0.0002 = $2.82
This will leave 40 coins that will be taken into account when calculating profit and fees in future.
On the other hand, I have a second problem. Adding to the position.
For example, if I opened a long ETHUSDT at 1400 for 50 coins ($700), and on price 1410 I added another 15 coins to the long position, how should I calculate the total profit, stop loss, take limit, etc.
I also searched the Internet but did not find any formulas or dependencies.
Theoretically, these are 2 separate orders with 2 commissions and 2 take_profit(tp_fee) and stop_lost(st_fee) and partial position reductions.
If I do my math well, if I partially reduce the position by 10 coins at the price of 1450, then the commission is limit1_fee = 1450 * 10 * 0.0002 = $2.82.
But how do I calculate the profit? from price 1400 or 1410?
Or maybe there is a formula that will determine the new input_price?

Based on my recent reasoning, namely that these are 2 separate orders, you can subtract 10 coins from the order that started with 1400 or from the one with 1410. Which one should I take it from first?
Profit_limit1_2 = (1/1410-1/1450)10114101450 = $400
There will be 50 coins left in order 1
There will be 5 coins left in order 2

Profit_limit1_1 = (1/140-1/1450)10114001450 = $500
There will be 40 coins left in order 1
Order 2 will leave 15 coins

And again, your sixth sense tells me that probably missed something.
Maybe there are experts in binance futures formulas who can e help in calculator that is as useful as possible.

Hi @mcsham, thank you for posting in the forum, however I would suggest reaching out to a Customer Support from Binance’s website to understand the commission according to your individual case. They have access to your account set up, historical trades and balance, so you’ll be able to get specific clarifications.