From 653a1d3e8929ab450e1243385cb99f43dcf8f6f4 Mon Sep 17 00:00:00 2001 From: maxxrk Date: Thu, 14 Nov 2024 19:18:47 -0600 Subject: [PATCH 1/8] add ft dance --- firstradeAPI.py | 128 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 101 insertions(+), 27 deletions(-) diff --git a/firstradeAPI.py b/firstradeAPI.py index 20fb88ca..650e7cf0 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -125,10 +125,6 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non # Buy on each account for s in orderObj.get_stocks(): for key in firstrade_o.get_account_numbers(): - printAndDiscord( - f"{key} {orderObj.get_action()}ing {orderObj.get_amount()} {s} @ {orderObj.get_price()}", - loop, - ) for account in firstrade_o.get_account_numbers(key): obj: ft_account.FTSession = firstrade_o.get_logged_in_objects(key) print_account = maskString(account) @@ -138,53 +134,131 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non "Running in DRY mode. No transactions will be made.", loop ) try: + old_amount = orderObj.get_amount() + original_action = orderObj.get_action() + should_dance = False symbol_data = symbols.SymbolQuote(obj, account, s) if symbol_data.last < 1.00: + if int(orderObj.get_amount()) < 100: + should_dance = True price_type = order.PriceType.LIMIT + orderObj.set_price("limit") if orderObj.get_action().capitalize() == "Buy": price = symbol_data.last + 0.01 else: price = symbol_data.last - 0.01 else: price_type = order.PriceType.MARKET + orderObj.set_price("market") price = 0.00 if orderObj.get_action().capitalize() == "Buy": order_type = order.OrderType.BUY else: order_type = order.OrderType.SELL - ft_order = order.Order(obj) - order_conf = ft_order.place_order( - account=account, - symbol=s, - price_type=price_type, - order_type=order_type, - quantity=int(orderObj.get_amount()), - duration=order.Duration.DAY, - price=price, - dry_run=orderObj.get_dry(), - ) - - print("The order verification produced the following messages: ") - pprint.pprint(order_conf) printAndDiscord( - ( - f"{key} account {print_account}: The order verification was " - + "successful" - if order_conf["error"] == "" - else "unsuccessful" - ), + f"{key} {orderObj.get_action()}ing {orderObj.get_amount()} {s} @ {orderObj.get_price()}", loop, ) - if not order_conf["error"] == "": + if should_dance and orderObj.get_action() == "buy": + quantity = 100 + print( + f"Buying {quantity} then selling {quantity - orderObj.get_amount()} of {s}" + ) + orderObj.set_amount(quantity) + ft_order = order.Order(obj) + order_conf = ft_order.place_order( + account=account, + symbol=s, + price_type=price_type, + order_type=order_type, + quantity=orderObj.get_amount(), + duration=order.Duration.DAY, + price=price, + dry_run=orderObj.get_dry(), + ) + print("The order verification produced the following messages: ") + #pprint.pprint(order_conf) + printAndDiscord( + ( + f"{key} account {print_account}: The order verification was " + + "successful" + if order_conf["error"] == "" + else "unsuccessful" + ), + loop, + ) + if not order_conf["error"] == "": + printAndDiscord( + f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", + loop, + ) + orderObj.set_amount(quantity - old_amount) + sleep(1) + ft_order = order.Order(obj) + order_conf = ft_order.place_order( + account=account, + symbol=s, + price_type=price_type, + order_type=order.OrderType.SELL, + quantity=orderObj.get_amount(), + duration=order.Duration.DAY, + price=price, + dry_run=orderObj.get_dry(), + ) + print("The order verification produced the following messages: ") + #pprint.pprint(order_conf) + printAndDiscord( + ( + f"{key} account {print_account}: The order verification was " + + "successful" + if order_conf["error"] == "" + else "unsuccessful" + ), + loop, + ) + if not order_conf["error"] == "": + printAndDiscord( + f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", + loop, + ) + else: + ft_order = order.Order(obj) + order_conf = ft_order.place_order( + account=account, + symbol=s, + price_type=price_type, + order_type=order_type, + quantity=orderObj.get_amount(), + duration=order.Duration.DAY, + price=price, + dry_run=orderObj.get_dry(), + ) + print("The order verification produced the following messages: ") + pprint.pprint(order_conf) printAndDiscord( - f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", + ( + f"{key} account {print_account}: The order verification was " + + "successful" + if order_conf["error"] == "" + else "unsuccessful" + ), loop, ) + if not order_conf["error"] == "": + printAndDiscord( + f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", + loop, + ) except Exception as e: printAndDiscord( f"{key} {print_account}: Error submitting order: {e}", loop ) print(traceback.format_exc()) continue + + finally: + # Restore orderObj + orderObj.set_amount(old_amount) + orderObj.set_action(original_action) sleep(1) - print() + print() \ No newline at end of file From 068e03c11acdc2b90147263650e8c56c1dbf6f17 Mon Sep 17 00:00:00 2001 From: maxxrk Date: Thu, 14 Nov 2024 19:20:01 -0600 Subject: [PATCH 2/8] leave the pprints for cli --- firstradeAPI.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firstradeAPI.py b/firstradeAPI.py index 650e7cf0..fbfe3cdc 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -177,7 +177,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non dry_run=orderObj.get_dry(), ) print("The order verification produced the following messages: ") - #pprint.pprint(order_conf) + pprint.pprint(order_conf) printAndDiscord( ( f"{key} account {print_account}: The order verification was " @@ -206,7 +206,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non dry_run=orderObj.get_dry(), ) print("The order verification produced the following messages: ") - #pprint.pprint(order_conf) + pprint.pprint(order_conf) printAndDiscord( ( f"{key} account {print_account}: The order verification was " From 638030f34fa895ee504165d709d12df47a5619d6 Mon Sep 17 00:00:00 2001 From: maxxrk Date: Thu, 14 Nov 2024 19:25:44 -0600 Subject: [PATCH 3/8] add symbol price to dance sell --- firstradeAPI.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firstradeAPI.py b/firstradeAPI.py index fbfe3cdc..1dd6d4fa 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -194,6 +194,8 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non ) orderObj.set_amount(quantity - old_amount) sleep(1) + symbol_data = symbols.SymbolQuote(obj, account, s) + price = symbol_data.last - 0.01 ft_order = order.Order(obj) order_conf = ft_order.place_order( account=account, From 645a31938ed76261f5b4779959c804672ef64e0e Mon Sep 17 00:00:00 2001 From: maxxrk Date: Thu, 14 Nov 2024 20:10:38 -0600 Subject: [PATCH 4/8] add errors in dance --- firstradeAPI.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firstradeAPI.py b/firstradeAPI.py index 1dd6d4fa..09ca2d41 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -192,6 +192,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", loop, ) + raise Exception(f"Error buying {quantity} of {s}") orderObj.set_amount(quantity - old_amount) sleep(1) symbol_data = symbols.SymbolQuote(obj, account, s) @@ -223,6 +224,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", loop, ) + raise Exception(f"Error selling {quantity - old_amount} of {s}") else: ft_order = order.Order(obj) order_conf = ft_order.place_order( From ac5d64f3f5c83e135b9cac4f8ab0abfbf62ea373 Mon Sep 17 00:00:00 2001 From: Nelson Dane <47427072+NelsonDane@users.noreply.github.com> Date: Fri, 22 Nov 2024 23:41:57 -0500 Subject: [PATCH 5/8] cleaning --- firstradeAPI.py | 139 ++++++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 65 deletions(-) diff --git a/firstradeAPI.py b/firstradeAPI.py index 09ca2d41..7d81fdee 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -125,6 +125,10 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non # Buy on each account for s in orderObj.get_stocks(): for key in firstrade_o.get_account_numbers(): + printAndDiscord( + f"{key} {orderObj.get_action()}ing {orderObj.get_amount()} {s} @ {orderObj.get_price()}", + loop, + ) for account in firstrade_o.get_account_numbers(key): obj: ft_account.FTSession = firstrade_o.get_logged_in_objects(key) print_account = maskString(account) @@ -133,9 +137,9 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non printAndDiscord( "Running in DRY mode. No transactions will be made.", loop ) + old_amount = orderObj.get_amount() + original_action = orderObj.get_action() try: - old_amount = orderObj.get_amount() - original_action = orderObj.get_action() should_dance = False symbol_data = symbols.SymbolQuote(obj, account, s) if symbol_data.last < 1.00: @@ -160,72 +164,76 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non loop, ) if should_dance and orderObj.get_action() == "buy": - quantity = 100 - print( - f"Buying {quantity} then selling {quantity - orderObj.get_amount()} of {s}" - ) - orderObj.set_amount(quantity) - ft_order = order.Order(obj) - order_conf = ft_order.place_order( - account=account, - symbol=s, - price_type=price_type, - order_type=order_type, - quantity=orderObj.get_amount(), - duration=order.Duration.DAY, - price=price, - dry_run=orderObj.get_dry(), - ) - print("The order verification produced the following messages: ") - pprint.pprint(order_conf) + # Do the dance + quantity = 100 + print( + f"Buying {quantity} then selling {quantity - orderObj.get_amount()} of {s}" + ) + orderObj.set_amount(quantity) + ft_order = order.Order(obj) + order_conf = ft_order.place_order( + account=account, + symbol=s, + price_type=price_type, + order_type=order_type, + quantity=orderObj.get_amount(), + duration=order.Duration.DAY, + price=price, + dry_run=orderObj.get_dry(), + ) + print("The buy order verification produced the following messages: ") + pprint.pprint(order_conf) + buy_success = order_conf["error"] == "" + printAndDiscord( + ( + f"{key} account {print_account}: The buy order verification was " + + "successful" + if buy_success + else "unsuccessful" + ), + loop, + ) + if not buy_success: printAndDiscord( - ( - f"{key} account {print_account}: The order verification was " - + "successful" - if order_conf["error"] == "" - else "unsuccessful" - ), + f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", loop, ) - if not order_conf["error"] == "": - printAndDiscord( - f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", - loop, - ) - raise Exception(f"Error buying {quantity} of {s}") - orderObj.set_amount(quantity - old_amount) - sleep(1) - symbol_data = symbols.SymbolQuote(obj, account, s) - price = symbol_data.last - 0.01 - ft_order = order.Order(obj) - order_conf = ft_order.place_order( - account=account, - symbol=s, - price_type=price_type, - order_type=order.OrderType.SELL, - quantity=orderObj.get_amount(), - duration=order.Duration.DAY, - price=price, - dry_run=orderObj.get_dry(), - ) - print("The order verification produced the following messages: ") - pprint.pprint(order_conf) + raise Exception(f"Error buying {quantity} of {s}") + orderObj.set_amount(quantity - old_amount) + sleep(1) # Rest before selling + symbol_data = symbols.SymbolQuote(obj, account, s) + price = symbol_data.last - 0.01 + ft_order = order.Order(obj) + order_conf = ft_order.place_order( + account=account, + symbol=s, + price_type=price_type, + order_type=order.OrderType.SELL, + quantity=orderObj.get_amount(), + duration=order.Duration.DAY, + price=price, + dry_run=orderObj.get_dry(), + ) + print("The sell order verification produced the following messages: ") + pprint.pprint(order_conf) + sell_success = order_conf["error"] == "" + printAndDiscord( + ( + f"{key} account {print_account}: The sell order verification was " + + "successful" + if sell_success + else "unsuccessful" + ), + loop, + ) + if not sell_success: printAndDiscord( - ( - f"{key} account {print_account}: The order verification was " - + "successful" - if order_conf["error"] == "" - else "unsuccessful" - ), + f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", loop, ) - if not order_conf["error"] == "": - printAndDiscord( - f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", - loop, - ) - raise Exception(f"Error selling {quantity - old_amount} of {s}") + raise Exception(f"Error selling {quantity - old_amount} of {s}") else: + # Normal buy/sell ft_order = order.Order(obj) order_conf = ft_order.place_order( account=account, @@ -239,20 +247,21 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non ) print("The order verification produced the following messages: ") pprint.pprint(order_conf) + order_success = order_conf["error"] == "" printAndDiscord( ( f"{key} account {print_account}: The order verification was " + "successful" - if order_conf["error"] == "" + if order_success else "unsuccessful" ), loop, ) - if not order_conf["error"] == "": + if not order_success: printAndDiscord( f"{key} account {print_account}: The order verification produced the following messages: {order_conf}", loop, - ) + ) except Exception as e: printAndDiscord( f"{key} {print_account}: Error submitting order: {e}", loop @@ -265,4 +274,4 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non orderObj.set_amount(old_amount) orderObj.set_action(original_action) sleep(1) - print() \ No newline at end of file + print() From c182a1a456a3fa105c47ff574d8bc88aba6f6676 Mon Sep 17 00:00:00 2001 From: Nelson Dane <47427072+NelsonDane@users.noreply.github.com> Date: Fri, 22 Nov 2024 23:43:41 -0500 Subject: [PATCH 6/8] ds fix --- firstradeAPI.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firstradeAPI.py b/firstradeAPI.py index 7d81fdee..39245d11 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -200,7 +200,8 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non ) raise Exception(f"Error buying {quantity} of {s}") orderObj.set_amount(quantity - old_amount) - sleep(1) # Rest before selling + # Rest before selling + sleep(1) symbol_data = symbols.SymbolQuote(obj, account, s) price = symbol_data.last - 0.01 ft_order = order.Order(obj) From 8361ae0748cded61b43c9c441476eda9bb2f4695 Mon Sep 17 00:00:00 2001 From: MaxxRK Date: Wed, 27 Nov 2024 09:04:34 -0600 Subject: [PATCH 7/8] fix messaging --- firstradeAPI.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firstradeAPI.py b/firstradeAPI.py index 39245d11..3f0f54af 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -166,7 +166,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non if should_dance and orderObj.get_action() == "buy": # Do the dance quantity = 100 - print( + printAndDiscord( f"Buying {quantity} then selling {quantity - orderObj.get_amount()} of {s}" ) orderObj.set_amount(quantity) From ac23e929f6dd88a5cd4bb716f1c4fbd341f53f15 Mon Sep 17 00:00:00 2001 From: MaxxRK Date: Wed, 27 Nov 2024 09:22:38 -0600 Subject: [PATCH 8/8] fix message formats --- firstradeAPI.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/firstradeAPI.py b/firstradeAPI.py index 3f0f54af..f3d93e13 100644 --- a/firstradeAPI.py +++ b/firstradeAPI.py @@ -125,10 +125,6 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non # Buy on each account for s in orderObj.get_stocks(): for key in firstrade_o.get_account_numbers(): - printAndDiscord( - f"{key} {orderObj.get_action()}ing {orderObj.get_amount()} {s} @ {orderObj.get_price()}", - loop, - ) for account in firstrade_o.get_account_numbers(key): obj: ft_account.FTSession = firstrade_o.get_logged_in_objects(key) print_account = maskString(account) @@ -167,7 +163,8 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non # Do the dance quantity = 100 printAndDiscord( - f"Buying {quantity} then selling {quantity - orderObj.get_amount()} of {s}" + f"Buying {quantity} then selling {quantity - orderObj.get_amount()} of {s}", + loop, ) orderObj.set_amount(quantity) ft_order = order.Order(obj) @@ -189,7 +186,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non f"{key} account {print_account}: The buy order verification was " + "successful" if buy_success - else "unsuccessful" + else f"{key} account {print_account}: The sell order verification was unsuccessful" ), loop, ) @@ -223,7 +220,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non f"{key} account {print_account}: The sell order verification was " + "successful" if sell_success - else "unsuccessful" + else f"{key} account {print_account}: The sell order verification was unsuccessful" ), loop, ) @@ -254,7 +251,7 @@ def firstrade_transaction(firstrade_o: Brokerage, orderObj: stockOrder, loop=Non f"{key} account {print_account}: The order verification was " + "successful" if order_success - else "unsuccessful" + else f"{key} account {print_account}: The sell order verification was unsuccessful" ), loop, )