Jump to content
Matt

IBKR fixed fees python estimator

Recommended Posts

Posted (edited)

Hello, if like me you are using a simulator and you want to get a realistic idea of broker costs associated with trading, here is a Python script that will allow you to estimate fees per ticker. When I trade a ticker more then once, I divide the total transaction price in my journal.

image.png.4757585d395d07a16afc075fcfdd4ea2.png

To use it, you need to copy the "trade" spreadsheet from Das Trader into the Python prompt. The calculations are based on IBKR's fixed costs.

Here's a video demonstrating how to use it:

 

I can't attach python files so here the code:
 

# Function to get the table from the user
def get_table_from_prompt():
    print("Please copy and paste the table (press Enter twice to finish):")
    table = []
    while True:
        line = input()
        if not line:
            break
        table.append(line.split())
    return table

# Function to calculate transaction fees
def calculate_transaction_fee(num_shares, share_price):
    commission = round(num_shares * 0.005, 2) + num_shares * share_price * 0.000008 + min(0.000145 * num_shares, 7.27)
    # Apply a minimum of $1 per transaction
    commission = max(commission, 1.00)
    # Apply a max of 1% per transaction
    total_cost_one_percent = round(num_shares * share_price * 0.01, 2)
    commission = min(commission, total_cost_one_percent)  # Apply maximum commission of 1%
    return round(commission, 2)

# Get the table from the user
table = get_table_from_prompt()

# Dictionary to store cumulative fees per action
cumulative_fees = {}

# Calculate fees for each transaction and accumulate fees per action
for transaction in table:
    time, action, transaction_type, num_shares, share_price, _ = transaction
    fee = calculate_transaction_fee(int(num_shares), float(share_price))
    if action not in cumulative_fees:
        cumulative_fees[action] = 0.0
    cumulative_fees[action] += fee

# Display cumulative fees per action, rounded to two decimals
for action, total_fee in cumulative_fees.items():
    total_fee_rounded = round(total_fee, 2)
    print(f"For action {action}, cumulative fees are {total_fee_rounded} USD.")

# Calculate and display cumulative fees for the day
daily_cumulative_fees = sum(cumulative_fees.values())
daily_cumulative_fees_rounded = round(daily_cumulative_fees, 2)
print(f"\nThe cumulative fees for the day are {daily_cumulative_fees_rounded} USD.")

 

good luck!

Edited by Matt
forget the 1% max rules in the code, this is now fixed

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.