<commit_before>"""
InteractiveBrokersCsv

Description:
    Gives TransactionGenerator for Interactive Brokers CSV files
"""
import csv
import datetime
import decimal
import sys

import transaction
from transaction import Transaction,TransactionGenerator,TransactionGeneratorError

class InteractiveBrokersCsvTransactionGenerator(TransactionGenerator):
    def __init__(self, file=None):
        self.file = file

    def get_transactions(self, file=None):
        if not file:
            file = self.file

        transactions = []
        reader = csv.DictReader(self.filter_trades_lines(file))

        try:
            for entry in reader:
                if entry.get('Asset Category') != "Stocks":
                    continue
                if 'Exchange' in entry and entry['Exchange'] != '-':
                    continue
                if 'Exchange' not in entry and entry['Date/Time'] == "":
                    continue

                dt = datetime.datetime.strptime(entry['Date/Time'], "%Y-%m-%d, %H:%M:%S")
                quantity = int(self._fix_number(entry['Quantity']))
                is_buy = quantity > 0
                quantity = abs(quantity)
                gross_proceeds = abs(
                    decimal.Decimal(
                        self._fix_number(entry['Proceeds'])
                    )
                )
                comm = abs(decimal.Decimal(self._fix_number(entry['Comm/Fee'])))

                if is_buy:
                    net_proceeds = gross_proceeds + comm
                else:
                    net_proceeds = gross_proceeds - comm

                transactions.append(
                    Transaction(<commit_msg>[StockGainCalculator] Parse the date only if the transaction does not have time for InteractiveBrokersCsv<commit_after>"""
InteractiveBrokersCsv

Description:
    Gives TransactionGenerator for Interactive Brokers CSV files
"""
import csv
import datetime
import decimal
import sys

import transaction
from transaction import Transaction,TransactionGenerator,TransactionGeneratorError

class InteractiveBrokersCsvTransactionGenerator(TransactionGenerator):
    def __init__(self, file=None):
        self.file = file

    def get_transactions(self, file=None):
        if not file:
            file = self.file

        transactions = []
        reader = csv.DictReader(self.filter_trades_lines(file))

        try:
            for entry in reader:
                is_stock = False
                is_options = False
                if entry.get('Asset Category') == "Stocks":
                    is_stock = True
                elif entry.get('Asset Category').endswith("Options"):
                    is_options = True
                else:
                    continue
                if 'Exchange' in entry and entry['Exchange'] != '-':
                    continue
                if 'Exchange' not in entry and entry['Date/Time'] == "":
                    continue

                try:
                    dt = datetime.datetime.strptime(entry['Date/Time'], "%Y-%m-%d, %H:%M:%S")
                except:
                    # Hong Kong stock transactions do not have time
                    dt = datetime.datetime.strptime(entry['Date/Time'], "%Y-%m-%d")
                quantity = int(self._fix_number(entry['Quantity']))
                is_buy = quantity > 0
                quantity = abs(quantity)
                gross_proceeds = abs(
                    decimal.Decimal(
                        self._fix_number(entry['Proceeds'])
                    )
                )
                comm = abs(decimal.Decimal(self._fix_number(entry['Comm/Fee'])))

                if is_buy:
                    net_proceeds = gross_proceeds + comm
                else:
                    net_proceeds = gross_proceeds - comm

                transactions.append(
                    Transaction(
{'fraction_non_alphanumeric': 0.08804544280919184, 'fraction_numerical': 0.0005163955589981926, 'mean_word_length': 1.6497948016415869, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1680452', 'n_tokens_mistral': 999, 'n_tokens_neox': 909, 'n_words': 227}