def test_unknown_orderLegType(self): historical_order = json.loads('''{ "session": "NORMAL", "duration": "DAY", "orderType": "MARKET", "complexOrderStrategyType": "NONE", "quantity": 1.0, "filledQuantity": 1.0, "remainingQuantity": 0.0, "requestedDestination": "AUTO", "destinationLinkName": "NITE", "orderLegCollection": [ { "orderLegType": "BOGUS", "legId": 1, "instrument": { "assetType": "EQUITY", "cusip": "1234567890", "symbol": "FAKE" }, "instruction": "BUY", "positionEffect": "OPENING", "quantity": 1.0 } ], "orderStrategyType": "SINGLE", "orderId": 987654321, "cancelable": false, "editable": false, "status": "FILLED", "enteredTime": "2021-01-01T12:01:00+0000", "closeTime": "2021-01-01T12:01:01+0000", "tag": "tag", "accountId": 19191919, "orderActivityCollection": [ { "activityType": "EXECUTION", "executionType": "FILL", "quantity": 1.0, "orderRemainingQuantity": 0.0, "executionLegs": [ { "legId": 1, "quantity": 1.0, "mismarkedQuantity": 0.0, "price": 999.99, "time": "2021-01-01T12:01:01+0000" } ] } ] }''') with self.assertRaises(ValueError, msg='unknown orderLegType'): construct_repeat_order(historical_order)
def latest_order_main(sys_args): parser = argparse.ArgumentParser( description='Utilities for generating code from historical orders') required = parser.add_argument_group('required arguments') required.add_argument('--token_file', required=True, help='Path to token file') required.add_argument('--api_key', required=True) parser.add_argument('--account_id', type=int, help='Restrict lookups to a specific account ID') args = parser.parse_args(args=sys_args) client = client_from_token_file(args.token_file, args.api_key) if args.account_id: orders = client.get_orders_by_path(args.account_id).json() if 'error' in orders: print(('TDA returned error: "{}", This is most often caused by ' + 'an invalid account ID').format(orders['error'])) return -1 else: orders = client.get_orders_by_query().json() if 'error' in orders: print('TDA returned error: "{}"'.format(orders['error'])) return -1 if orders: order = sorted(orders, key=lambda o: -o['orderId'])[0] print('# Order ID', order['orderId']) print(code_for_builder(construct_repeat_order(order))) else: print('No recent orders found') return 0
def test_oco_inside_oto(self): historical_order = json.loads('''{ "session": "NORMAL", "duration": "DAY", "orderType": "LIMIT", "complexOrderStrategyType": "NONE", "quantity": 1.0, "filledQuantity": 0.0, "remainingQuantity": 1.0, "requestedDestination": "AUTO", "destinationLinkName": "AutoRoute", "price": 2.71, "orderLegCollection": [ { "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0BIGC.JF10060000", "symbol": "BIGC_101521C60", "description": "BIGC OCT 15 2021 60.0 Call" }, "instruction": "BUY_TO_OPEN", "positionEffect": "OPENING", "quantity": 1.0 } ], "orderStrategyType": "TRIGGER", "orderId": 4403477551, "cancelable": true, "editable": false, "status": "QUEUED", "enteredTime": "2021-05-13T03:12:54+0000", "accountId": 123123123, "childOrderStrategies": [ { "orderStrategyType": "OCO", "orderId": 4403477554, "cancelable": true, "editable": false, "accountId": 123123123, "childOrderStrategies": [ { "session": "NORMAL", "duration": "DAY", "orderType": "LIMIT", "complexOrderStrategyType": "NONE", "quantity": 1.0, "filledQuantity": 0.0, "remainingQuantity": 1.0, "requestedDestination": "AUTO", "destinationLinkName": "AutoRoute", "orderLegCollection": [ { "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0BIGC.JF10060000", "symbol": "BIGC_101521C60", "description": "BIGC OCT 15 2021 60.0 Call" }, "instruction": "SELL_TO_CLOSE", "positionEffect": "CLOSING", "quantity": 1.0 } ], "orderStrategyType": "SINGLE", "orderId": 4403477553, "cancelable": true, "editable": false, "status": "ACCEPTED", "enteredTime": "2021-05-13T03:12:54+0000", "accountId": 12312312 }, { "session": "NORMAL", "duration": "DAY", "orderType": "STOP", "complexOrderStrategyType": "NONE", "quantity": 1.0, "filledQuantity": 0.0, "remainingQuantity": 1.0, "requestedDestination": "AUTO", "destinationLinkName": "AutoRoute", "orderLegCollection": [ { "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0BIGC.JF10060000", "symbol": "BIGC_101521C60", "description": "BIGC OCT 15 2021 60.0 Call" }, "instruction": "SELL_TO_CLOSE", "positionEffect": "CLOSING", "quantity": 1.0 } ], "orderStrategyType": "SINGLE", "orderId": 4403477554, "cancelable": true, "editable": false, "status": "ACCEPTED", "enteredTime": "2021-05-13T03:12:54+0000", "accountId": 488435533 } ] } ] }''') repeat_order = construct_repeat_order(historical_order) self.assertEquals(json.dumps({ 'session': 'NORMAL', 'duration': 'DAY', 'orderType': 'LIMIT', 'complexOrderStrategyType': 'NONE', 'quantity': 1.0, 'requestedDestination': 'AUTO', 'orderStrategyType': 'TRIGGER', 'price': 2.71, 'orderLegCollection': [{ 'instruction': 'BUY_TO_OPEN', 'instrument': { 'assetType': 'OPTION', 'symbol': 'BIGC_101521C60' }, 'quantity': 1.0, }], 'childOrderStrategies': [{ 'orderStrategyType': 'OCO', 'childOrderStrategies': [{ 'session': 'NORMAL', 'duration': 'DAY', 'orderType': 'LIMIT', 'complexOrderStrategyType': 'NONE', 'quantity': 1.0, 'requestedDestination': 'AUTO', 'orderStrategyType': 'SINGLE', 'orderLegCollection': [{ 'instruction': 'SELL_TO_CLOSE', 'instrument': { 'assetType': 'OPTION', 'symbol': 'BIGC_101521C60', }, 'quantity': 1.0, }] }, { 'session': 'NORMAL', 'duration': 'DAY', 'orderType': 'STOP', 'complexOrderStrategyType': 'NONE', 'quantity': 1.0, 'requestedDestination': 'AUTO', 'orderStrategyType': 'SINGLE', 'orderLegCollection': [{ 'instruction': 'SELL_TO_CLOSE', 'instrument': { 'assetType': 'OPTION', 'symbol': 'BIGC_101521C60', }, 'quantity': 1.0, }] }] }] }, indent=4, sort_keys=True), json.dumps(repeat_order.build(), indent=4, sort_keys=True))
def test_one_triggers_other(self): historical_order = json.loads('''{ "session": "NORMAL", "duration": "GOOD_TILL_CANCEL", "orderType": "LIMIT", "complexOrderStrategyType": "NONE", "quantity": 2.0, "filledQuantity": 2.0, "remainingQuantity": 0.0, "requestedDestination": "AUTO", "destinationLinkName": "AutoRoute", "price": 3.6, "orderLegCollection": [ { "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0AEO..HK10035000", "symbol": "AEO_082021C35", "description": "AEO AUG 20 2021 35.0 Call" }, "instruction": "BUY_TO_OPEN", "positionEffect": "OPENING", "quantity": 2.0 } ], "orderStrategyType": "TRIGGER", "orderId": 29292929, "cancelable": false, "editable": false, "status": "FILLED", "enteredTime": "2021-04-20T02:40:28+0000", "closeTime": "2021-04-20T13:31:53+0000", "accountId": 19191919, "orderActivityCollection": [ { "activityType": "EXECUTION", "executionType": "FILL", "quantity": 2.0, "orderRemainingQuantity": 0.0, "executionLegs": [ { "legId": 1, "quantity": 2.0, "mismarkedQuantity": 0.0, "price": 3.6, "time": "2021-04-20T13:31:53+0000" } ] } ], "childOrderStrategies": [ { "session": "NORMAL", "duration": "GOOD_TILL_CANCEL", "orderType": "LIMIT", "complexOrderStrategyType": "NONE", "quantity": 2.0, "filledQuantity": 2.0, "remainingQuantity": 0.0, "requestedDestination": "NYSE", "destinationLinkName": "AutoRoute", "price": 3.7, "orderLegCollection": [ { "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0AEO..HK10035000", "symbol": "AEO_082021C35", "description": "AEO AUG 20 2021 35.0 Call" }, "instruction": "SELL_TO_CLOSE", "positionEffect": "CLOSING", "quantity": 2.0 } ], "orderStrategyType": "SINGLE", "orderId": 22992992, "cancelable": false, "editable": false, "status": "FILLED", "enteredTime": "2021-04-20T02:40:28+0000", "closeTime": "2021-04-29T15:02:53+0000", "accountId": 19191919, "orderActivityCollection": [ { "activityType": "EXECUTION", "executionType": "FILL", "quantity": 2.0, "orderRemainingQuantity": 0.0, "executionLegs": [ { "legId": 1, "quantity": 2.0, "mismarkedQuantity": 0.0, "price": 3.7, "time": "2021-04-29T15:02:53+0000" } ] } ] } ] }''') repeat_order = construct_repeat_order(historical_order) self.assertEquals(json.dumps({ 'session': 'NORMAL', 'duration': 'GOOD_TILL_CANCEL', 'orderType': 'LIMIT', 'complexOrderStrategyType': 'NONE', 'quantity': 2.0, 'requestedDestination': 'AUTO', 'orderStrategyType': 'TRIGGER', 'price': 3.6, 'orderLegCollection': [{ 'instruction': 'BUY_TO_OPEN', 'instrument': { 'assetType': 'OPTION', 'symbol': 'AEO_082021C35' }, 'quantity': 2.0, }], 'childOrderStrategies': [{ 'session': 'NORMAL', 'duration': 'GOOD_TILL_CANCEL', 'orderType': 'LIMIT', 'complexOrderStrategyType': 'NONE', 'quantity': 2.0, 'price': 3.7, 'requestedDestination': 'NYSE', 'orderStrategyType': 'SINGLE', 'orderLegCollection': [{ 'instruction': 'SELL_TO_CLOSE', 'instrument': { 'assetType': 'OPTION', 'symbol': 'AEO_082021C35' }, 'quantity': 2.0, }] }] }, indent=4, sort_keys=True), json.dumps(repeat_order.build(), indent=4, sort_keys=True))
def test_complex_options_order(self): historical_order = json.loads('''{ "session": "NORMAL", "duration": "DAY", "orderType": "NET_DEBIT", "complexOrderStrategyType": "BUTTERFLY", "quantity": 1.0, "filledQuantity": 1.0, "remainingQuantity": 0.0, "requestedDestination": "AUTO", "destinationLinkName": "AutoRoute", "price": 0.03, "orderLegCollection": [ { "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0SPY..F110409000", "symbol": "SPY_060121C409", "description": "SPY JUN 1 2021 409.0 Call" }, "instruction": "BUY_TO_OPEN", "positionEffect": "OPENING", "quantity": 1.0 }, { "orderLegType": "OPTION", "legId": 2, "instrument": { "assetType": "OPTION", "cusip": "0SPY..F110410000", "symbol": "SPY_060121C410", "description": "SPY JUN 1 2021 410.0 Call" }, "instruction": "SELL_TO_OPEN", "positionEffect": "OPENING", "quantity": 2.0 }, { "orderLegType": "OPTION", "legId": 3, "instrument": { "assetType": "OPTION", "cusip": "0SPY..F110411000", "symbol": "SPY_060121C411", "description": "SPY JUN 1 2021 411.0 Call" }, "instruction": "BUY_TO_OPEN", "positionEffect": "OPENING", "quantity": 1.0 } ], "orderStrategyType": "SINGLE", "orderId": 1919191919, "cancelable": false, "editable": false, "status": "FILLED", "enteredTime": "2021-05-12T14:39:58+0000", "closeTime": "2021-05-12T14:39:58+0000", "accountId": 700000007, "orderActivityCollection": [ { "activityType": "EXECUTION", "executionType": "FILL", "quantity": 1.0, "orderRemainingQuantity": 0.0, "executionLegs": [ { "legId": 1, "quantity": 1.0, "mismarkedQuantity": 0.0, "price": 8.24, "time": "2021-05-12T14:39:58+0000" }, { "legId": 2, "quantity": 2.0, "mismarkedQuantity": 0.0, "price": 7.585, "time": "2021-05-12T14:39:58+0000" }, { "legId": 3, "quantity": 1.0, "mismarkedQuantity": 0.0, "price": 6.96, "time": "2021-05-12T14:39:58+0000" } ] } ] } ''') repeat_order = construct_repeat_order(historical_order) self.assertEquals(json.dumps({ 'session': 'NORMAL', 'duration': 'DAY', 'orderType': 'NET_DEBIT', 'complexOrderStrategyType': 'BUTTERFLY', 'quantity': 1.0, 'requestedDestination': 'AUTO', 'price': 0.03, 'orderStrategyType': 'SINGLE', 'orderLegCollection': [{ 'instruction': 'BUY_TO_OPEN', 'instrument': { 'assetType': 'OPTION', 'symbol': 'SPY_060121C409' }, 'quantity': 1.0 }, { 'instruction': 'SELL_TO_OPEN', 'instrument': { 'assetType': 'OPTION', 'symbol': 'SPY_060121C410' }, 'quantity': 2.0 }, { 'instruction': 'BUY_TO_OPEN', 'instrument': { 'assetType': 'OPTION', 'symbol': 'SPY_060121C411' }, 'quantity': 1.0 }] }, indent=4, sort_keys=True), json.dumps(repeat_order.build(), indent=4, sort_keys=True))
def test_limit_options_order(self): historical_order = json.loads('''{ "session": "NORMAL", "duration": "DAY", "orderType": "LIMIT", "complexOrderStrategyType": "NONE", "quantity": 1.0, "filledQuantity": 1.0, "remainingQuantity": 0.0, "requestedDestination": "AUTO", "destinationLinkName": "CDRG", "price": 0.21, "orderLegCollection": [{ "orderLegType": "OPTION", "legId": 1, "instrument": { "assetType": "OPTION", "cusip": "0SPY..RJ00309000", "symbol": "SPY_061920P309", "description": "SPY Jun 19 2020 309.0 Put", "underlyingSymbol": "SPY" }, "instruction": "SELL_TO_CLOSE", "positionEffect": "CLOSING", "quantity": 1.0 }], "orderStrategyType": "SINGLE", "orderId": 987654321, "cancelable": false, "editable": false, "status": "FILLED", "enteredTime": "2021-01-01T12:01:00+0000", "closeTime": "2021-01-01T12:01:01+0000", "tag": "tag", "accountId": 19191919, "orderActivityCollection": [{ "activityType": "EXECUTION", "executionType": "FILL", "quantity": 1.0, "orderRemainingQuantity": 0.0, "executionLegs": [{ "legId": 1, "quantity": 1.0, "mismarkedQuantity": 0.0, "price": 0.21, "time": "2021-01-01T12:01:01+0000" }] }] }''') repeat_order = construct_repeat_order(historical_order) self.assertEquals(json.dumps({ 'session': 'NORMAL', 'duration': 'DAY', 'orderType': 'LIMIT', 'complexOrderStrategyType': 'NONE', 'quantity': 1.0, 'requestedDestination': 'AUTO', 'price': 0.21, 'orderStrategyType': 'SINGLE', 'orderLegCollection': [{ 'instruction': 'SELL_TO_CLOSE', 'instrument': { 'assetType': 'OPTION', 'symbol': 'SPY_061920P309' }, 'quantity': 1.0 }] }, indent=4, sort_keys=True), json.dumps(repeat_order.build(), indent=4, sort_keys=True))
def test_market_equity_order(self): historical_order = json.loads('''{ "session": "NORMAL", "duration": "DAY", "orderType": "MARKET", "complexOrderStrategyType": "NONE", "quantity": 1.0, "filledQuantity": 1.0, "remainingQuantity": 0.0, "requestedDestination": "AUTO", "destinationLinkName": "NITE", "orderLegCollection": [ { "orderLegType": "EQUITY", "legId": 1, "instrument": { "assetType": "EQUITY", "cusip": "1234567890", "symbol": "FAKE" }, "instruction": "BUY", "positionEffect": "OPENING", "quantity": 1.0 } ], "orderStrategyType": "SINGLE", "orderId": 987654321, "cancelable": false, "editable": false, "status": "FILLED", "enteredTime": "2021-01-01T12:01:00+0000", "closeTime": "2021-01-01T12:01:01+0000", "tag": "tag", "accountId": 19191919, "orderActivityCollection": [ { "activityType": "EXECUTION", "executionType": "FILL", "quantity": 1.0, "orderRemainingQuantity": 0.0, "executionLegs": [ { "legId": 1, "quantity": 1.0, "mismarkedQuantity": 0.0, "price": 999.99, "time": "2021-01-01T12:01:01+0000" } ] } ] }''') repeat_order = construct_repeat_order(historical_order) self.assertEquals(json.dumps({ 'session': 'NORMAL', 'duration': 'DAY', 'orderType': 'MARKET', 'complexOrderStrategyType': 'NONE', 'quantity': 1.0, 'requestedDestination': 'AUTO', 'orderStrategyType': 'SINGLE', 'orderLegCollection': [{ 'instruction': 'BUY', 'instrument': { 'assetType': 'EQUITY', 'symbol': 'FAKE' }, 'quantity': 1.0 }] }, indent=4, sort_keys=True), json.dumps(repeat_order.build(), indent=4, sort_keys=True))