If statement bug? Not sure what's happening here

Okay, so I am making a bot to paper trade using alpaca API. The bot itself works fine, no issues there. The problem occurs when I try to tell the bot to wait for the stock market to open so that it doesn’t fill up the logs trying to buy or sell when it can’t.

This code SHOULD work. In fact, If I remove exactly five lines, it does work. The problem is that the lines in question should also work, as it is just an if statement. Here’s the code:

async def wait_until_market_open():
  # Get the current time
  current_time = time.time()
  eastern_timezone_offset = -4 * 3600
  current_time_est = current_time + eastern_timezone_offset
  current_time_of_day_est = (current_time_est -(int(current_time_est) // 86400) * 86400)
  print(current_time_of_day_est)
  # Get the current day of the week (0 = Monday, 6 = Sunday)
  current_day_of_week = (int((current_time_est-86400*4)// 86400)) % 7
  print(current_day_of_week)
  # Wait until the next market open
  while True:
    if (current_day_of_week > 4): #<<<<<< This line.
      print("Market is closed on the weekends!")
      await asyncio.sleep(60)
      continue
    else: #<<<<<< To this line. 
      if (current_time_of_day_est < 9.5 * 3600 or current_time_of_day_est > 16 * 3600):
        print("Market's closed!")
        await asyncio.sleep(60)
        continue
      else:
        break

Oddly, the code does not print the current_day_of_week, which is two lines above the if statement, and it does not print the current_time_of_day, which is even further up, unless the if statement is removed. If I do remove the if statement, both are printed and I get the expected values (on Sunday, the value of current_day_of_week is 6). What is happening here? Is this a bug with the Replit compiler for Python?

I should add that I am getting the current time and day using time.time() because any attempt that I have made to use datetime.datetime() or time.now() results in an immediate hang. Replit seems to be completely unable to access those functions.

Can you please format the code so it is easier to read?
Thanks,

it was properly formatted when I typed it. It lost all formatting when I clicked “Post”

Put ``` At start and end of the code and formatting is preserved. If you edit the post is still there btw

async def wait_until_market_open():
  # Get the current time
  current_time = time.time()
  eastern_timezone_offset = -4 * 3600
  current_time_est = current_time + eastern_timezone_offset
  current_time_of_day_est = (current_time_est -
                             (int(current_time_est) // 86400) * 86400)
  print(current_time_of_day_est)
  # Get the current day of the week (0 = Monday, 6 = Sunday)
  current_day_of_week = (int((current_time_est-86400*4)// 86400)) % 7
  print(current_day_of_week)
  # Wait until the next market open
  while True:
    if (current_day_of_week > 4):
      print("Market is closed on the weekends!")
      await asyncio.sleep(60)
      continue
    else:
      if (current_time_of_day_est < 9.5 * 3600 or current_time_of_day_est > 16 * 3600):
        print("Market's closed!")
        await asyncio.sleep(60)
        continue
      else:
        break
1 Like

Specifically, the if/else immediately inside the while True statement causes the issue. I’ve tried making it all one if statement with or in between each expression and get the same result.

So you say that

import datetime
datetime.datetime.today().weekday()

is not working working?

Correct. The code just hangs.

Works for me.
I ask because using datetime with time zones will avoid making mistakes in determining the week day

Yes, I’m aware. My solution isn’t foolproof because I will need to adjust for daylight savings, but the values I get are 100% accurate until the next daylight savings time change.

I just don’t understand why using the current day variable as the expression for an if statement causes the entire function to stop working.

I just tried in a repl on my side and dateline, calendar and panda’s why it determine the day all work.
I suspect that the day calculation is wrong.

Try to force a day and see if it works instead if calculating from the real time value

I have done that as well, and it does work if I set the value to two days ago or tomorrow. Still, I would expect the function to at least print the statements before the if statement on a weekend, but it doesn’t.

I run it and it gives that is week end … which is correct

Just checked, it works as long as it’s after 4pm. I changed the second if statement to check if it is after 8pm and it stopped working again.

4 hours are also the one you a also using for the time zone …

Correct. The values for time and day that I’m getting are accurate. I keep saying that. The issue is with the if statement. I’m 99% sure that this is a Replit bug or something. Where I am, we are UTC-4. It is presently 4:07. 10 minutes ago, the code was hanging. So the bug only happens when it is between 9:30AM and 4:00PM on a weekend. That’s so odd to me. Especially since the bug doesn’t make sense. Things that have nothing to do with the if statement get broken when the bug occurs.

Is there a way to contact support that doesn’t involve using these forums?

But if it would be a Replit bug it would have been noticed by now already.
You can use the help button from the dashboard.

Can you send a link to your Repl?

Also instead of

use this:

elif (current_time_of_day_est < 9.5 * 3600 or current_time_of_day_est > 16 * 3600):
  print("Market's closed!")
  await asyncio.sleep(60)
  continue
else:
  break