Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert IBKR Last365CalendarDays (JSON) file to Sharesight Bulk Trades CSV File
import json
from datetime import datetime
# Read Last365CalendarDays.json file
with open('Last365CalendarDays.json') as json_file:
data = json.load(json_file)
def convert_date(date):
date_obj = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
return date_obj.strftime("%d/%m/%Y")
def get_price(tradePrice, putCall):
multiplier = 1
if (putCall == 'C' or putCall == 'P'):
multiplier = 100
return float(tradePrice) * multiplier
# Print to ShareSight format
# Following the `bulk_trades.csv` format
print("Trade Date,Instrument Code,Market Code,Quantity,Price,Transaction Type,Exchange Rate (optional),Brokerage (optional),Brokerage Currency (optional),Comments (optional),")
for trade in data["trades"]:
# Skip currency conversion
if (trade["symbol"] == "AUD.USD"):
continue
# Ignore options
if (trade["putCall"] != None):
continue
formatted_date = convert_date(trade["dateTime"])
instrument_code = ' '.join(trade["symbol"].split())
market_code = 'NASDAQ'
quantity = abs(float(trade["quantity"]))
price = get_price(trade["tradePrice"], trade["putCall"])
transaction_type = "BUY" if float(trade["quantity"]) >= 0 else "SELL"
exchange_rate = ""
brokerage = float(trade["ibCommission"])*-1
brokerage_currency = "USD"
comment = trade["description"]
print(f"{formatted_date},{instrument_code},{market_code},{quantity},{price},{transaction_type},{exchange_rate},{brokerage},{brokerage_currency},{comment},")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment