Skip to content

Commit

Permalink
Fixes #3: Resolve rounding issues in legacy.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Waldukat committed Sep 22, 2024
1 parent 1c1de75 commit 50c295f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions tastyworksTaxes/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def is_option(symbol: str) -> bool:
def format_price(price, is_option: bool) -> str:
if pd.isna(price) or price == '--' or price == 0:
return ''
price_float = abs(float(str(price).replace(',', '')))
price_str = str(price).replace(',', '')
if is_option:
return f"{price_float / 100:.2f}"
return f"{price_float}".rstrip('0').rstrip('.')
option_price = float(price_str) / 100
return f"{option_price:.5f}".rstrip('0').rstrip('.')
return price_str.rstrip('0').rstrip('.')


def format_fees(fees: float) -> str:
Expand All @@ -89,6 +90,7 @@ def format_amount(amount) -> str:
amount_float = float(str(amount).replace(',', ''))
return f"{amount_float:.0f}" if amount_float.is_integer() else f"{amount_float:.2f}"


def convert_to_legacy_format(df: pd.DataFrame) -> pd.DataFrame:
legacy_df = pd.DataFrame()

Expand Down Expand Up @@ -135,6 +137,7 @@ def convert_to_legacy_format(df: pd.DataFrame) -> pd.DataFrame:

return legacy_df.reindex(columns=column_order)


def convert_csv(input_file: str, output_file: str) -> None:
logger.info(f"Reading input file: {input_file}")
df = pd.read_csv(input_file)
Expand All @@ -145,6 +148,7 @@ def convert_csv(input_file: str, output_file: str) -> None:
logger.info(f"Writing output file: {output_file}")
converted_df.to_csv(output_file, index=False)


def main() -> None:
logger.warning(
"There is an factor of 8 in the fees. The newly exported data from Tastyworks has this offset compared to my old legacy format data.")
Expand All @@ -164,5 +168,6 @@ def main() -> None:
convert_csv(args.input_file, args.output_file)
logger.debug("CSV conversion process completed")


if __name__ == "__main__":
main()
22 changes: 22 additions & 0 deletions test/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,25 @@ def test_money_movement(create_input_df):
assert result['Expiration Date'].iloc[0] == ''
assert result['Strike'].iloc[0] == ''
assert result['Call/Put'].iloc[0] == ''


def test_option_sell_to_open(create_input_df):
input_data = {
'Date': '2024-03-07T18:03:58+0100',
'Type': 'Trade',
'Sub Type': 'Sell to Open',
'Action': 'SELL_TO_OPEN',
'Symbol': './ZBM4 OZBK4 240426P113',
'Quantity': 2,
'Value': '343.75',
'Average Price': 171.875,
'Fees': -2.34,
'Expiration Date': '4/26/24',
'Strike Price': 113,
'Call or Put': 'PUT',
'Description': 'Sold 2 /ZBM4 OZBK4 04/26/24 Put 113\'00 @ 0.171875'
}
input_df = create_input_df(input_data)
result = convert_to_legacy_format(input_df)

assert result['Price'].iloc[0] == '1.71875'

0 comments on commit 50c295f

Please sign in to comment.