今度は、VBScript(Microsoft Visual Basic Scripting Edition)を使って、その日の株価を取得して表示するアプリ?マクロ?を作ってみました。

サンプルは、こちらのGoogleapi.txtの拡張子を、「.vbs」に変えてお使いください。

コンピュータに害を云々~という警告が出ることもありますが、OK押してもらって大丈夫です。


動かした結果は、こんな感じ。

タイトルなし

それまで一週間の終値、高値、安値、始値、出来高と
当日の、1時間ごとの終値等を表示します。

これ、別に5分間隔にでもできますし、
何でもできますよ。

もちろん、VBScriptはマイクロソフトとついているだけあって、Windowsだけでしか動かないみたいですけれども、VBAともほとんど互換性があるみたいなので、
これをエクセルのマクロに張り付けて使うっていう方法もありますね。



色んなアプリや、何かで株のデータを管理していらっしゃると思いますが、
ザラ場にデータを得る方法って、意外と少ないんですよね。
普通に証券会社のページを見たりしていればいいんですけれども、

そういうのができない人でも、ワンクリックで、自分の好きな銘柄のデータを取得できます。


うまくダウンロードできないという方は
以下のサンプルをメモ帳などにコピペして、「a.vbs」とでも名付けるだけでも使えますよ。
VBScriptは、要領さえわかってしまえば何にもいらず作れるのがいいですね。


'サンプルここから

Option Explicit 

Dim strURL, strCheck_txt, strCode, strCodename, strSt_point, strEd_point, strPrice_txt, strTime '文字列 
Dim strPrice_txt1, strPrice_txt2, strPrice_txt3, strPrice_txt4, strPrice_txt5
Dim strTime1, strTime2, strTime3, strTime4, strTime5
Dim n
Dim ohttp 'オブジェクト 

Set ohttp = CreateObject("MSXML2.XMLHTTP") 

For n = 1 to 2
IF n = 1 then
strCode = "5742"
strCodename = "NIC"
Elseif n = 2 then
strCode = "9466"
strCodename = "aidma"
End if

 ' GoogleAPI 1日毎。pが取得日数、年数、iが間隔(秒)fが取得値(o始値c終値l安値h高値v出来高)
 strURL = "https://finance.google.com/finance/getprices?p=7d&i=86400&f=d,o,c,l,h,v&x=TYO&q=" _ 
 & strCode

 ' HTML形式で読み込む 
ohttp.Open "GET", strURL, False 
 ohttp.setRequestHeader "If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT" 
 ohttp.send 
 If ohttp.Status <> 200 Then WScript.Quit 'リクエスト失敗で終了 

'UNIXTIMEを東京時間に変換します
strCheck_txt = "TIMEZONE_OFFSET=540"
strSt_point = InStr(1, ohttp.responsetext, strCheck_txt)
strCheck_txt = "a"
strSt_point = InStr(strSt_point, ohttp.responsetext, strCheck_txt) + 1
strPrice_txt = Mid(ohttp.responsetext, strSt_point, 10)
strTime1 = DateAdd("h", 9, DateAdd("s", strPrice_txt, DateSerial(1970, 1, 1)))

'それ以降のデータを抽出します
strCheck_txt = ","
strSt_point = InStr(strSt_point, ohttp.responsetext, strCheck_txt) + 1
strEd_point = InStr(strSt_point, ohttp.responsetext, vbLf)
strPrice_txt1 = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)

strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt)
strTime2 = DateAdd("d", Mid(ohttp.responsetext, strEd_point + 1, strSt_point - strEd_point), strTime1)
strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt) + 1
strEd_point = InStr(strSt_point, ohttp.responsetext, vbLf)
strPrice_txt2 = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)

strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt)
strTime3 = DateAdd("d", Mid(ohttp.responsetext, strEd_point + 1, strSt_point - strEd_point), strTime1)
strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt) + 1
strEd_point = InStr(strSt_point, ohttp.responsetext, vbLf)
strPrice_txt3 = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)

strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt)
strTime4 = DateAdd("d", Mid(ohttp.responsetext, strEd_point + 1, strSt_point - strEd_point), strTime1)
strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt) + 1
strEd_point = InStr(strSt_point, ohttp.responsetext, vbLf)
strPrice_txt4 = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)

strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt)
strTime5 = DateAdd("d", Mid(ohttp.responsetext, strEd_point + 1, strSt_point - strEd_point), strTime1)
strSt_point = InStr(strEd_point, ohttp.responsetext, strCheck_txt) + 1
strEd_point = InStr(strSt_point, ohttp.responsetext, vbLf)
strPrice_txt5 = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)

 ' GoogleAPI 30分毎。pが取得日数、年数、iが間隔(秒)fが取得値(o始値c終値l安値h高値v出来高)
 strURL = "https://finance.google.com/finance/getprices?p=1d&i=3600&f=d,o,c,l,h,v&x=TYO&q=" _ 
 & strCode

 ' HTML形式で読み込む 
ohttp.Open "GET", strURL, False 
 ohttp.setRequestHeader "If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT" 
 ohttp.send 
 If ohttp.Status <> 200 Then WScript.Quit 'リクエスト失敗で終了 

'UNIXTIMEを東京時間に変換します
strCheck_txt = "TIMEZONE_OFFSET=540"
strSt_point = InStr(1, ohttp.responsetext, strCheck_txt)
strCheck_txt = "a"
strSt_point = InStr(strSt_point, ohttp.responsetext, strCheck_txt) + 1
strPrice_txt = Mid(ohttp.responsetext, strSt_point, 10)
strTime = DateAdd("h", 9, DateAdd("s", strPrice_txt, DateSerial(1970, 1, 1)))

'それ以降のデータを抽出します
strCheck_txt = ","
strSt_point = InStr(strSt_point, ohttp.responsetext, strCheck_txt) + 1
strPrice_txt = Mid(ohttp.responsetext, strSt_point, Len(ohttp.responsetext) - Len(strSt_point))

'メッセージボックスで表示します。
msgbox strCode & strCodename & "の日足データは" & vbCrLf _
 & "日付,終値,高値,安値,始値,出来高" & vbCrLf _
 & strTime1 &"," & strPrice_txt1 & vbCrLf _
 & strTime2 &"," & strPrice_txt2 & vbCrLf _
 & strTime3 &"," & strPrice_txt3 & vbCrLf _
 & strTime4 &"," & strPrice_txt4 & vbCrLf _
 & strTime5 &"," & strPrice_txt5 & vbCrLf & vbCrLf _
 & strTime & "の1時間前から1時間間隔で" & vbCrLf _
 & "1,終値,高値,安値,始値,出来高" & vbCrLf _
 & "0," & strPrice_txt & vbCrLf _
 & "0が10時 1が11時 2が前場終了時" & vbCrLf _
 & "3が13時 4が14時 5が15時かな。" _
 , , strCode & strCodename

'strSt_point = InStr(1, ohttp.responsetext, strCheck_txt) + Len(strCheck_txt) + 1
'strEd_point = InStr(strSt_point, ohttp.responsetext, vbLf)
'If strEd_point - strSt_point > 6 Then
' strPrice_txt = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)
'Else
' strPrice_txt = Mid(ohttp.responsetext, strSt_point, strEd_point - strSt_point)
'End If

Next

Set ohttp = Nothing 

'サンプルここまで


因みに、
For n = 1 to 2
IF n = 1 then
strCode = "5742"
strCodename = "NIC"
Elseif n = 2 then
strCode = "9466"
strCodename = "aidma"
End if
ってところの、
strCodeっていうのが、証券コードになっています。ここを変えればどんな銘柄でも大丈夫ですよ。
また、
1to2を1to3に変えて、
Elseif n = 3 then
strCode = "数字"
strCodename = "銘柄名"
っていうのをEnd ifの前に加えれば、
それだけでもう一つ増やしていけます。


ぜひぜひ試してみてください。
ちょっとした仕事の休憩時間に相場を見たりなんてこともできるかも?


ダウンロードはこちらのGoogleapi.txtの拡張子をvbsに変えてお使いください!

不具合等あったら教えてください。 
分からないことなどもあれば分かる範囲で答えたいと思います。

KABU+、CSVEXのデータの自動取込マクロを作ってみたよ。

Zaifの相場、板情報、取引履歴の自動取得マクロ作りました。

コインチェックの価格自動取得マクロ作りました。

アルトコインの価格自動取得マクロ作りました。

グーグルのAPIから株価を取得して日経平均株価を計算するマクロ作りました。

エクセルVBAの使い方 目次

chart
NB2GYRIF6BVPDZSKRV7A3C2ZGCDZEEIQHCY4PA6B

↑一応自分のNEMのアドレスです。
何かコメント等ありましたら教えてください。


にほんブログ村 ライフスタイルブログ のんびり暮らしへ
にほんブログ村
↑クリックしていただけると単純に嬉しいです。
幼稚園の頃の夢は「のんびり暮らしたい。」でした。
仙人のような暮らしを夢見て
日々、遊んでる毎日です。
読んでいただきありがとうございました。