Background:
-
This was not an issue until I added the try/except. I added this because at times I feel there was a deeper issue, perhaps rooted in the function I call from kucoin.helpers. (I call a while loop, while inside of a while loop in my code.)
-
Specifically I would find myself unable to consistently finish the cycle of purchase and then sale of asset: This failure would stem from between a successful purchase of asset and the sale.
-
The try/except loop was the only way to "flawlessly execute my code".
-
I know the try/except loop was not utilized properly, I am inexperienced and will delve into it.
import kucoin_helpers
import time
import random
from twilio.rest import Client as TwilioClient
import twiliodata
####Set API Credentials for Twilio
text_client = TwilioClient(twiliodata.account_sid, twiliodata.auth_token)
#This is going to be Lumiere himself. The man, the bot, the first iteration and legend.
########IMMEDIATELY BEGIN KEEPING rounds of data.
#Leave room for INFO: in database to allow comment made to first and last entry in database for round
##This will allow for easy diefin tin
#look forward to using minimax
take_profit = 0
stop_loss = 0
keep_testing = True
while keep_testing:
try:
starting_account_balance = kucoin_helpers.doge_musk_check_balance()
print(f"BEGINNING BALANCE: {starting_account_balance}")
list_of_winLOSS = []
####Start keeping this data
total_times_to_run = random.randrange(start=20, stop=50)
total_times_to_run = int(total_times_to_run)
#####We need to run this loop repetitively until we reach an output ratio where Success to Failure is near (0.7/0.3)==2.63
#Add Each trials results (tp to sl limits/)
##Consider using limits that are proportionate to the "confidence" in current trend in timeframe
for i in range(total_times_to_run):
input_tp = random.uniform(1.001, 1.009)
take_profit=input_tp
print(input_tp)
input_sl = random.uniform(0.991, 0.99989)
take_profit=input_sl
print(input_sl)
answer = kucoin_helpers.execute_doge_musktrade(input_tp=input_tp, input_sl=input_sl)
list_of_winLOSS.append(answer)
time.sleep(0.5)
failure = 0
success = 0
for outcome in list_of_winLOSS:
if outcome == "Failure":
failure += 1
if outcome == "Success":
success += 1
ending_account_balance = kucoin_helpers.doge_musk_check_balance()
total_PNL = ending_account_balance - starting_account_balance
print(f"SUCCESS: {success}\nFailure: {failure}")
print(f"BEGINNING BALANCE: {starting_account_balance}")
print(f"ENDING BALANCE: {ending_account_balance}")
print(f"PNL : {total_PNL}")
body = f"SUCCESS: {success}\nFAILURE: {failure}\nBEGINNING BALANCE: {starting_account_balance}\nENDING BALANCE: {ending_account_balance}\nPNL: {total_PNL}"
text_client.messages.create(to="7177295751", from_=twiliodata.phone_number, body=body)
except:
print("Unable to perform Action")
>Solution :
As you already mentioned, a good use of try-except clauses would capture your specific exceptions and not all. What is happening right now is that you are throwing a KeyboardInterrupt exception by pressing Ctrl+c, which is caught by your except statement and results in printing "Unable to perform Action".
You could add an exception handler for the KeyboardInterrupt exception with exit() to end the program, ones you hit Ctrl+c:
try:
...
except KeyboardInterrupt:
exit()
except:
print("Unable to perform Action")