import json import datetime, time from datetime import date import http.client symbolList = ["MMM", "AXP", "AAPL", "BA", "CAT", "CVX", "CSCO", "KO", "DIS", "DD", "XOM", "GE", "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "MCD", "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UTX", "UNH", "VZ", "V", "WMT"] def httpRequest(stockSymbol, numberOfDays): conn = http.client.HTTPConnection("dev.markitondemand.com:80") url = "/Api/v2/InteractiveChart/json?parameters={'Normalized':false,'NumberOfDays':" + str(numberOfDays) + ",'DataPeriod':'Day','Elements':[{'Symbol':'" + stockSymbol + "','Type':'price','Params':['c']}]}" conn.request("GET", url) response = conn.getresponse().read() return response.decode('ascii') def convertYahooFinanceToHighChartsJSON(jsonData): #YAHOO Finance JSON File Has a Specific Structure, and the HighStock Chart Has It's Own as Well. I have to Convert between the two #file = open(jsonFile) # open the json file #jsonData = file.read() # read the data counter = 0 jsonDict = json.loads(jsonData) # convert JSON string to array resultDict = [] # this is where the result is exportFile = open(jsonDict["Elements"][0]["Symbol"] + ".json", "w") for record in range(len(jsonDict["Dates"])): # data we need is under query-result-quote #print(record) tempList = [] # for every quote we need to sore [JSTIMESTAMP, PRICE] recordList = jsonDict["Dates"][record][0:10].split("-") # split every date into components so they can be put into the datetime.date() function for the conversion to JS Time Stamp if int(recordList[0]) < 2010 or int(recordList[0]) > 2014: counter += 1 continue tempDate = datetime.date(int(recordList[0]), int(recordList[1]), int(recordList[2])) jsDate = int(time.mktime(tempDate.timetuple())*1000) tempList.append(jsDate) tempList.append((float(jsonDict["Elements"][0]["DataSeries"]["close"]["values"][record])\ +float(jsonDict["Elements"][0]["DataSeries"]["close"]["values"][record]))/2) # average of high and low prices will be the value for a specific day if tempList != []: resultDict.append(tempList) print(counter) resultDict.sort() result = json.dumps(resultDict) exportFile.write(result) exportFile.close() return result for symbol in symbolList: print(symbol) jsonData = httpRequest(symbol, 2190) time.sleep(5) convertYahooFinanceToHighChartsJSON(jsonData)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question