I am trying to read Indian stock market data using API calls. For this example, I have used 10 stocks. My current program is:
First I define the Function:
def get_prices(stock):
start_unix = 1669794745
end_unix = start_unix + 1800
interval = 1
url = 'https://priceapi.moneycontrol.com/techCharts/indianMarket/stock/history?symbol=' + str(stock) + "&resolution="+ str(interval) + "&from=" + str(start_unix) + "&to=" + str(end_unix)
url_data = requests.get(url).json()
print(url_data['c'])
Next, I use multi-threading. I do not know much about the functioning of multithreading - I just used the code from a tutorial on the web.
from threading import Thread
stocks = ['ACC','ADANIENT','ADANIGREEN','ADANIPORTS','ADANITRANS','AMBUJACEM','ASIANPAINT','ATGL','BAJAJ-AUTO','BAJAJHLDNG']
threads = []
for i in stocks:
threads.append(Thread(target=get_prices, args=(i,)))
threads[-1].start()
for thread in threads:
thread.join()
The time it takes is around 250 to 300 ms for the above program to run. In reality, I shall need to run the program for thousands of stocks. Is there any way to make it run faster. I am running the code in Jupyter Notebook on an apple M1 8 core chip. Any help will be greatly appreciated. Thank You!