Sub Hi_stock()
'Dim date_1 As Date
'date_1 = Date
'year_now = Year(date_1)
'month_now = Month(date_1)
'day_now = Day(date_1)
'request_day = year_input_1 * 365
'start_year = year_now - year_input_1
'start_date_1 = start_year & "/" & month_now & "/" & day_now '萬年曆顯示
'start_date_2 = DateDiff("s", "1970-1-1 0:0:0", start_date_1) 'Unix 十位數顯示
'start_date_2 = DateDiff("s", "1970-1-1 0:0:0", start_date_1) & "000" 'Unix 十三位數顯示
'end_date_1 = year_now & "/" & month_now & "/" & day_now '萬年曆顯示
'end_date_2 = DateDiff("s", "1970-1-1 0:0:0", end_date_1) 'Unix 十位數顯示
'end_date_2 = DateDiff("s", "1970-1-1 0:0:0", end_date_1) & "000" 'Unix 十三位數顯示
'Hi 投資 歷史股價下載
'url_1 = "https://histock.tw/stock/chip/chartdata.aspx?no=" & stock_no_1 & "&days=" & request_day & _
"&m=dailyk%2Cclose%2Cvolume%2Cmean5%2Cmean10%2Cmean20%2Cmean60%2Cmean120%2Cmean5volume%2Cmean20volume" & _
"%2Ck9%2Cd9%2Crsi6%2Crsi12%2Cdif%2Cmacd%2Cosc&fbclid=IwAR0w4tNJGIm8iSRsh7Zj_DBWESNSJ8DLJurdLZZR3--7vAkh5Xd7rPpT_bw"
Dim myXML As Object
Dim url_1 As String
Dim response As String
Dim newstr As String
Dim json As Object
'Dim data_1 As Object
Set myXML = CreateObject("MSXML2.XMLHTTP")
url_1 = "https://histock.tw/stock/chip/chartdata.aspx?no=1101&days=1095&m=dailyk"
With myXML
.Open "GET", url_1, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
.send
response = myXML.responseText
'Debug.Print response
'TYPE 1
'newstr = Replace(response, "{" & """" & "DailyK" & """" & ":" & """" & "[", "{" & """" & "1" & """" & ":", 1, 1) 'head
'newstr = Replace(newstr, "]" & """", "", 1, 1) 'end
'Debug.Print newstr
'TYPE 2
'newstr = Replace(response, """" & "[", "{" & """" & "1" & """" & ":", 1, 1) 'head
'newstr = Replace(newstr, "]" & """", "}", 1, 1) 'end
'Debug.Print newstr
'For a_1 = 1 To a
'newstr = Replace(newstr, "],[", "]," & """" & b & """" & ":[", 1, 1) 'TYPE 1
'b = b + 1 'TYPE 1 & 2
'Next a_1
'TYPE 3
'newstr = Replace(response, """" & "[[", "[[" & """", 1, 1) 'head
'newstr = Replace(newstr, "]]" & """", """" & "]", 1, 1) 'end
'newstr = Replace(newstr, ",", """" & "," & """") 'TYPE 3
'newstr = Replace(newstr, "]" & """" & "," & """" & "[", """" & "]" & "," & "[" & """", 1, 1) 'TYPE 3
'newstr = Replace(newstr, "{", "[{", 1, 1) 'TYPE 3
'newstr = Replace(newstr, "}", "]}]", 1, 1) 'TYPE 3
'TYPE 4
newstr = Replace(response, "{" & """" & "DailyK" & """" & ":" & """" & "[", "", 1, 1) 'head
newstr = Replace(newstr, "]" & """" & "}", "", 1, 1) 'end
newstr = Replace(newstr, "[", "{" & """" & "time" & """" & ":")
newstr = Replace(newstr, "]", "}")
For a = 1 To 9999
For b = 1 To 5
If b = 1 Then newstr = Replace(newstr, ",", """" & "open" & """" & ":", 1, 1)
If b = 2 Then newstr = Replace(newstr, ",", """" & "high" & """" & ":", 1, 1)
If b = 3 Then newstr = Replace(newstr, ",", """" & "low" & """" & ":", 1, 1)
If b = 4 Then newstr = Replace(newstr, ",", """" & "close" & """" & ":", 1, 1)
If b = 5 Then newstr = Replace(newstr, ",", "test", 1, 1)
Next b
Next a
newstr = Replace(newstr, """" & "open", "," & """" & "open")
newstr = Replace(newstr, """" & "high", "," & """" & "high")
newstr = Replace(newstr, """" & "low", "," & """" & "low")
newstr = Replace(newstr, """" & "close", "," & """" & "close")
newstr = Replace(newstr, "test", ",")
newstr = "[" + newstr + "]"
'Debug.Print newstr
Set json = JsonConverter.ParseJson(newstr)
For a = 1 To 9999
On Error GoTo end_1
data_1 = json(a)("time")
open_1 = json(a)("open")
high_1 = json(a)("high")
low_1 = json(a)("low")
close_1 = json(a)("close")
'Debug.Print data_1, open_1, high_1, low_1, close_1
Workbooks("test1.xlsm").Sheets("Sheet2").Activate
'Columns("A:A").Select
'Selection.NumberFormatLocal = "0000000000000"
Cells(a, 1) = json(a)("time") / 86400000 + DateSerial(1970, 1, 1)
Cells(a, 2) = json(a)("open")
Cells(a, 3) = json(a)("high")
Cells(a, 4) = json(a)("low")
Cells(a, 5) = json(a)("close")
Columns("A:A").Select
Selection.NumberFormatLocal = "yyyy/m/d"
Next a
End With
end_1:
'nothing
Exit Sub
End Sub