-2

I have a stock ticker and I want it to display static text when market is closed. Im not sure how to do this in Python.

I tried this one Python - Working out if time now is between two times but it did not work for my example.


from samplebase import SampleBase
from rgbmatrix import graphics
import time
import requests
from bs4 import BeautifulSoup as bs
from datetime import datetime

class RunText(SampleBase):

    def run(self):
      while True:

        offscreen_canvas = self.matrix.CreateFrameCanvas()
        pos = offscreen_canvas.width

        while True:
            now = time.strftime("%H:%M:%S", time.localtime(time.time()))
            print("Script Start: ") + str(now)

            res = requests.get('https://thecache.io/stocks/index.php')
            soup = bs(res.content, 'lxml')
            price = soup.select_one('#GME').text
            price1 = soup.select_one('#etsy').text
            price2 = soup.select_one('#aapl').text

            font = graphics.Font()
            font.LoadFont("../../../fonts/5x7.bdf")
            textColor = graphics.Color(255, 255, 0)
            stock2 = graphics.Color(139, 0, 0)
            stock3 = graphics.Color(64, 0, 255)

            my_text =  " GME: " + price
            my_text1 = "ETSY: " + price1
            my_text2 = "AAPL: " + price2
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text)
            len2 = graphics.DrawText(offscreen_canvas, font, pos, 18, stock2, my_text1)
            len3 = graphics.DrawText(offscreen_canvas, font, pos, 26, stock3, my_text2)
            pos = 1
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)


#Main function
if __name__ == "__main__":
    run_text = RunText()
#    time.sleep(0.08)
    if (not run_text.process()):
        run_text.print_help()

Basically i dont want to hit the API when stock is closed. Thanks for your help.

  • 2
    Why are you fetching the same page 3 times instead of just fetching once and processing three times? You are going to piss off those folks by pounding on their web site. – Tim Roberts Mar 11 '21 at 00:26
  • 1
    "but it did not work for my example" could you clarify how it didn't work? errors, unexpected behaviour, etc? providing the code you tried using that solution would help as well – Axiumin_ Mar 11 '21 at 00:27
  • You can do your test with a simple text comparison: `if now >= '09' and now < '17'. Of course, that's local time, not stock market time. – Tim Roberts Mar 11 '21 at 00:27
  • 1
    Why do you have two `while True:` loops? The inner loop never exits. – Tim Roberts Mar 11 '21 at 00:28
  • 1
    One more nitpick: don't name a variable `len`. That hides the standard function of the same name. – Tim Roberts Mar 11 '21 at 00:29
  • @TimRoberts I am using the rgb-led-matrix from git and that is how he has it setup. When I remove that and move the items into the other loop it breaks the code. Fixed the multiple url hits. – Diamond Dave Mar 11 '21 at 00:33
  • Sorry, that doesn't make sense. You can simply remove the first `while True:` and the code would work exactly the same. Unless there's more code you haven't shown us. – Tim Roberts Mar 11 '21 at 00:37

1 Answers1

0

One way to check if the your current local time is between 0900-1600 is to use datetimes and the time only component. For example:

import datetime
now = datetime.datetime.now() # returns a local datetime
now_time = now.time() # strips the date component and only take the time part

# create start and end time which can then be compared using >, <, <=, >= etc.
start = datetime.time(9,0,0) 
end = datetime.time(16,0,0)

# this is the between case, if you want to run the api call on the start of the market open use inclusive comparator
if now_time > start and now_time < end: