0

Background:

  1. 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.)

  2. 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.

  3. The try/except loop was the only way to "flawlessly execute my code".

  4. 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")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    Because you have a catch-all `except:` in your code which catches _all_ exceptions, including `KeyboardInterrupt` on some systems. See https://stackoverflow.com/a/4992124/843953 – Pranav Hosangadi Apr 14 '22 at 16:20
  • 1
    Does this answer your question? [What is wrong with using a bare 'except'?](https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except) – ddejohn Apr 14 '22 at 16:22
  • Can you elaborate further on the is catch-all except? I am very very not adept in the try/except functions. –  Apr 14 '22 at 16:35

2 Answers2

1

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")
Leon Menkreo
  • 129
  • 4
  • MY Man. You both answered my question perfectly. Thank you very much and I will delve into better utilizing try/except functions than. Thank you! –  Apr 14 '22 at 16:40
0

Is the code block exactly as it is being run? Because if not, you might have some indentation issues. From what I can tell, from starting_account_balance = kucoin_helpers.doge_musk_check_balance() to except: print("Unable to perform Action") should be one indent further in.

  • This is not exactly as it is. Here is a link to the github page where I have the data, this isnt updated with the branch I have created on my pc, but the concept is there, and the differences are not relevant to outcome, just minor differences like percentage changes and adding random() functions. https://github.com/Gershwinfire/trading_algorithms –  Apr 14 '22 at 16:34
  • Ok, glad to help! – TheDoughSmith Apr 14 '22 at 17:03