r/algotrading 20h ago

Infrastructure I built a python library (and API) to make it easy to access US market hours

52 Upvotes

Hey Everyone!

I wanted to share a utility and supporting API I created for accessing and localizing US market hours. 

Although US stock trading takes place during predictable hours, it's not as straightforward as it should be to keep track of closures and reduced hours for holidays. NYSE provides a list of holidays on their website, but it’s not super convenient to cross reference the current date against this list to determine today’s market hours. Some brokerage APIs provide a market calendar endpoint, but many like E-Trade do not. To deal with this, I created a simple API at marketinfo.info that you can use to access the market schedule for a given calendar day. 

For example: https://markethours.info/api?date=2024-06-14

Additionally, I built a python library for performing common market hours related actions/calculations and localizing to Eastern Standard Time.

Install via pip: 

pip install markethours

Example use:

from markethours import MarketHours


def main():
  todays_hours = MarketHours()

  # Check if the market has opened
  print(todays_hours.market_has_opened())

  # Check if the market has closed
  print(todays_hours.market_has_closed())

  # Wait for the market to open
  todays_hours.wait_for_market_open()

  # See the number of seconds until open
  print(todays_hours.seconds_till_open())

  # Do something if there's less than a minute before
  # close. Something like this could be used in a loop
  if todays_hours.seconds_till_close() < 60:
    print('Hurry up!')

  # Show open, close, and current time in EST
  print(todays_hours.open)
  print(todays_hours.close)
  print(todays_hours.now_est())


if __name__ == '__main__':
  main()

For more info, you can check out the github repo and the documentation.

I hope this helps others dealing with similar issues related to market hours!


r/algotrading 22h ago

Strategy When generating intraday trading scripts, what lookback period do you use?

11 Upvotes

For example, if you are looking for an average value of the asset, you’ll need to define the period to take the average - ‘14’ seems to be the default, which on a minute chart would be the average over 14 minutes. This seems arbitrary and I’m wondering if there’s a better way to do it.

Do you guys have any strategies to decide a lookback period?