我是python的新手,不确定要问这个问题的去向。我的主要目标是为所有期权交易创建交易日志,以查看每只股票,策略和时间表的净利润。我一直在尝试找到一种组织所有事务,忽略重复项并使之可排序的方法。到目前为止,我已经尝试过嵌套词典(添加所有条目时会遇到问题)和嵌套列表(我认为这样可以很好地组织它)。我将发布一个示例,说明我正在寻找的东西和尝试过的东西(底部的字典是我想要的,但不会捕获所有条目。有什么建议吗?
transaction_log = []
results = set()
results_list = []
x = 0
for option in options_portfolio:
transaction_log[options_portfolio[x].chain_symbol] = dict({options_portfolio[x].option_type: {options_portfolio[x].expiration_date: {options_portfolio[x].strike_price: {options_portfolio[x].group: options_portfolio[x].name}}}})
transaction_log[options_portfolio[x].chain_symbol] = options_portfolio[x].option_type
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type] = options_portfolio[x].expiration_date
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type][options_portfolio[x].expiration_date] = options_portfolio[x].strike_price
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type][options_portfolio[x].expiration_date][options_portfolio[x].strike_price] = options_portfolio[x].group
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type][options_portfolio[x].expiration_date][options_portfolio[x].strike_price][options_portfolio[x].group] = options_portfolio[x].name
y = 0
for option in options_portfolio:
if options_portfolio[x].order_created_at == options_portfolio[y].order_created_at:
pass
elif options_portfolio[x].order_created_at != options_portfolio[y].order_created_at:
if options_portfolio[x].chain_symbol == options_portfolio[y].chain_symbol and options_portfolio[x].expiration_date == options_portfolio[y].expiration_date and options_portfolio[x].strike_price == options_portfolio[y].strike_price and options_portfolio[x].option_type == options_portfolio[y].option_type:
results.add((f'{options_portfolio[y].name} and {options_portfolio[x].name} are the same option'))
results_list.append((options_portfolio[y].name,options_portfolio[x].name))
else:
pass
else:
pass
y += 1
x += 1
x = 0
y = 0
{'chain_symbol': 'AAPL', 'expiration_date': '2020-09-18', 'strike_price': 110.0, 'option_type': 'put', 'side': 'buy', 'order_created_at': '2020-09-11T19:48:25.375384Z', 'direction': 'debit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': 'long_put', 'closing_strategy': nan, 'price': 2.45, 'processed_quantity': 1.0, 'quanity_remaining': 1.0, 'is_open': True, 'group': 'AAPL_2020-09-18_110.0_put', 'name': 'AAPL_2020-09-18_110.0_put_buy_2020-09-11T19:48:25.375384Z_1.0'}
{'chain_symbol': 'SQ', 'expiration_date': '2020-10-02', 'strike_price': 130.0, 'option_type': 'put', 'side': 'buy', 'order_created_at': '2020-09-11T17:31:23.914397Z', 'direction': 'debit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': 'long_put', 'closing_strategy': nan, 'price': 5.7, 'processed_quantity': 1.0, 'quanity_remaining': 1.0, 'is_open': True, 'group': 'SQ_2020-10-02_130.0_put', 'name': 'SQ_2020-10-02_130.0_put_buy_2020-09-11T17:31:23.914397Z_1.0'}
{'chain_symbol': 'QQQ', 'expiration_date': '2020-09-18', 'strike_price': 269.0, 'option_type': 'put', 'side': 'sell', 'order_created_at': '2020-09-11T17:20:54.890999Z', 'direction': 'credit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': nan, 'closing_strategy': 'long_put', 'price': 6.14, 'processed_quantity': 1.0, 'quanity_remaining': '-1.0', 'is_open': True, 'group': 'QQQ_2020-09-18_269.0_put', 'name': 'QQQ_2020-09-18_269.0_put_sell_2020-09-11T17:20:54.890999Z_1.0'}
{'chain_symbol': 'QQQ', 'expiration_date': '2020-09-18', 'strike_price': 269.0, 'option_type': 'put', 'side': 'buy', 'order_created_at': '2020-09-11T16:45:29.188917Z', 'direction': 'debit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': 'long_put', 'closing_strategy': nan, 'price': 5.36, 'processed_quantity': 1.0, 'quanity_remaining': 1.0, 'is_open': True, 'group': 'QQQ_2020-09-18_269.0_put', 'name': 'QQQ_2020-09-18_269.0_put_buy_2020-09-11T16:45:29.188917Z_1.0'}
{'chain_symbol': 'SQ', 'expiration_date': '2020-09-18', 'strike_price': 135.0, 'option_type': 'put', 'side': 'sell', 'order_created_at': '2020-09-11T16:43:03.694028Z', 'direction': 'credit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': nan, 'closing_strategy': 'long_put', 'price': 4.4, 'processed_quantity': 1.0, 'quanity_remaining': '-1.0', 'is_open': True, 'group': 'SQ_2020-09-18_135.0_put', 'name': 'SQ_2020-09-18_135.0_put_sell_2020-09-11T16:43:03.694028Z_1.0'}
{'chain_symbol': 'SQ', 'expiration_date': '2020-09-18', 'strike_price': 135.0, 'option_type': 'put', 'side': 'buy', 'order_created_at': '2020-09-11T16:41:49.081863Z', 'direction': 'debit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': 'long_put', 'closing_strategy': nan, 'price': 4.0, 'processed_quantity': 1.0, 'quanity_remaining': 1.0, 'is_open': True, 'group': 'SQ_2020-09-18_135.0_put', 'name': 'SQ_2020-09-18_135.0_put_buy_2020-09-11T16:41:49.081863Z_1.0'}
{'chain_symbol': 'GOOG', 'expiration_date': '2020-09-18', 'strike_price': 1520.0, 'option_type': 'call', 'side': 'buy', 'order_created_at': '2020-09-11T16:39:35.940262Z', 'direction': 'debit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': 'long_call', 'closing_strategy': nan, 'price': 27.4, 'processed_quantity': 1.0, 'quanity_remaining': 1.0, 'is_open': True, 'group': 'GOOG_2020-09-18_1520.0_call', 'name': 'GOOG_2020-09-18_1520.0_call_buy_2020-09-11T16:39:35.940262Z_1.0'}
{'chain_symbol': 'GOOG', 'expiration_date': '2020-09-18', 'strike_price': 1540.0, 'option_type': 'call', 'side': 'sell', 'order_created_at': '2020-09-11T16:39:05.158876Z', 'direction': 'credit', 'order_quantity': 1.0, 'order_type': 'limit', 'opening_strategy': nan, 'closing_strategy': 'long_call', 'price': 18.1, 'processed_quantity': 1.0, 'quanity_remaining': '-1.0', 'is_open': True, 'group': 'GOOG_2020-09-18_1540.0_call', 'name': 'GOOG_2020-09-18_1540.0_call_sell_2020-09-11T16:39:05.158876Z_1.0'}
{'chain_symbol': 'MSFT', 'expiration_date': '2020-09-18', 'strike_price': 200.0, 'option_type': 'put', 'side': 'sell', 'order_created_at': '2020-09-11T16:36:56.414844Z', 'direction': 'credit', 'order_quantity': 2.0, 'order_type': 'limit', 'opening_strategy': nan, 'closing_strategy': 'long_put', 'price': 3.5, 'processed_quantity': 2.0, 'quanity_remaining': '-2.0', 'is_open': True, 'group': 'MSFT_2020-09-18_200.0_put', 'name': 'MSFT_2020-09-18_200.0_put_sell_2020-09-11T16:36:56.414844Z_2.0'}
{'chain_symbol': 'MSFT', 'expiration_date': '2020-09-18', 'strike_price': 200.0, 'option_type': 'put', 'side': 'buy', 'order_created_at': '2020-09-11T16:36:23.445176Z', 'direction': 'debit', 'order_quantity': 2.0, 'order_type': 'limit', 'opening_strategy': 'long_put', 'closing_strategy': nan, 'price': 2.9, 'processed_quantity': 2.0, 'quanity_remaining': 2.0, 'is_open': True, 'group': 'MSFT_2020-09-18_200.0_put', 'name': 'MSFT_2020-09-18_200.0_put_buy_2020-09-11T16:36:23.445176Z_2.0'}
number of unique option traded = 5
{'AAPL': {'put': {'2020-09-18': {110.0: {'AAPL_2020-09-18_110.0_put': 'AAPL_2020-09-18_110.0_put_buy_2020-09-11T19:48:25.375384Z_1.0'}}}}, 'SQ': {'put': {'2020-09-18': {135.0: {'SQ_2020-09-18_135.0_put': 'SQ_2020-09-18_135.0_put_buy_2020-09-11T16:41:49.081863Z_1.0'}}}}, 'QQQ': {'put': {'2020-09-18': {269.0: {'QQQ_2020-09-18_269.0_put': 'QQQ_2020-09-18_269.0_put_buy_2020-09-11T16:45:29.188917Z_1.0'}}}}, 'GOOG': {'call': {'2020-09-18': {1540.0: {'GOOG_2020-09-18_1540.0_call': 'GOOG_2020-09-18_1540.0_call_sell_2020-09-11T16:39:05.158876Z_1.0'}}}}, 'MSFT': {'put': {'2020-09-18': {200.0: {'MSFT_2020-09-18_200.0_put': 'MSFT_2020-09-18_200.0_put_buy_2020-09-11T16:36:23.445176Z_2.0'}}}}}
['AAPL', ['SQ', 'put'], ['QQQ', 'put'], 'SQ', ['GOOG', 'call'], ['MSFT', 'put']]