Intraday prices

Category : Uncategorized

Intraday prices

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

*
net from "http://www.researchforprofit.com/stata/mfd_dm"
net from "http://www.researchforprofit.com/stata/mfd_ta"
net from "http://www.researchforprofit.com/stata/mfd_tex"
* click on the blue mfd_dm package and then "click here to install" 
*

This post is rather technical. It basically explains the steps of downloading intraday prices from different sources.

NASDAQ

Nasdaq provides real-time prices tick-by-tick throughout the day. Prices are available for pre- and port-market hours as well. This is a free and very valuable service.

*
clear all
cd "/Users/mfd/Desktop"
local tarih="130906"
local symbol="AAPL"

* Intraday
mfd_dm_intraday `symbol', time(1)
forval aa=2/13 {
	di "`aa'"
	mfd_dm_intraday `symbol', time(`aa') append
}
gsort time -page -sequence
gen market="intraday"
save `tarih'_`symbol'_intra.dta, replace

* Pre-market hours
capture: mfd_dm_preday `symbol', time(1)
forval aa=2/11 {
	di "`aa'"
	capture: mfd_dm_preday `symbol', time(`aa') append
}
gsort time -page -sequence
gen market="preday"
save `tarih'_`symbol'_pre.dta, replace

* Post-market hours
capture: mfd_dm_postday `symbol', time(1)
forval aa=2/8 {
	di "`aa'"
	capture: mfd_dm_postday `symbol', time(`aa') append
}
gsort time -page -sequence
gen market="postday"
save `tarih'_`symbol'_post.dta, replace

use `tarih'_`symbol'_intra.dta, clear
append using `tarih'_`symbol'_pre.dta
append using `tarih'_`symbol'_post.dta
gen trading_period=-1 if market=="preday"
replace trading_period=0 if market=="intraday"
replace trading_period=1 if market=="postday"
gsort trading_period time -page -sequence
save `tarih'_`symbol'.dta, replace

gen seq=_n
tsset seq
tsline nls_price
*

Yahoo! Finance

* Reference: http://www.quantshare.com/sa-426-6-ways-to-download-free-intraday-and-tick-data-for-the-us-stock-market
* Format : http://chartapi.finance.yahoo.com/instrument/1.0/[TICKER]/chartdata;type=quote;range=1d/csv
* Example: http://chartapi.finance.yahoo.com/instrument/1.0/GOOG/chartdata;type=quote;range=1d/csv

*
clear all
cd "/users/mfd/desktop"

mata:
	string file_get_contents (string scalar raw)
	{
		fh = fopen(raw, "r")
		raw=""
		while ((line=fget(fh))!=J(0,0,"")) {
			raw=raw+char(13)+line
		}
		fclose(fh)
		return (raw)
	}

	void get_data (string scalar symbol)
	{	
		icerik = file_get_contents("http://chartapi.finance.yahoo.com/instrument/1.0/" + symbol + "/chartdata;type=quote;range=1d/csv")
		satir = tokens(icerik, char(13))			
		sutun=satir'
		st_addvar("str244", "myvar")
		st_addobs(rows(sutun))
		st_sstore(.,"myvar",sutun)
	}
end
*
*
local ticker="AAPL"
mata: get_data("`ticker'")

drop if strlen(myvar)<5
capture: split myvar, parse(",") 
rename myvar1 date
rename myvar2 close
rename myvar3 high
rename myvar4 low
rename myvar5 open
rename myvar6 volume

destring date close high low open volume, replace force

* Timestamp
replace date = (date * 1000) + (msofhours(24)*3653) - msofhours(5)
drop myvar*
gen date2 = dofc(date)
format %tc date
format %td date2

drop if date==.
*

Netfonds.no.

* Reference: http://www.quantshare.com/sa-426-6-ways-to-download-free-intraday-and-tick-data-for-the-us-stock-market
* Example for tick data:
* http://hopey.netfonds.no/tradedump.php?date=20120423&paper=AAPL.O&csv_format=txt

*
clear all
cd "/users/mfd/desktop"
insheet using "http://hopey.netfonds.no/tradedump.php?date=20140114&paper=AAPL.O&csv_format=txt"

* Get date and time
capture: split time, parse("T") 
	* date
	gen date=date(time1,"YMD")
	format %td date
	order date

	* time
	gen hour=substr(time2,1,2)
	destring hour, replace force
	gen minute=substr(time2,3,2)
	destring minute, replace force
	gen second=substr(time2,5,2)
	destring second, replace force

keep date hour minute second price quantity
order date hour minute second price quantity
*
*
clear all
cd "/users/mfd/desktop"

* To get bid/ask price and volume data:
insheet using "http://hopey.netfonds.no/posdump.php?date=20140114&paper=AAPL.O&csv_format=txt"

* Get date and time
capture: split time, parse("T") 
	* date
	gen date=date(time1,"YMD")
	format %td date
	order date

	* time
	gen hour=substr(time2,1,2)
	destring hour, replace force
	gen minute=substr(time2,3,2)
	destring minute, replace force
	gen second=substr(time2,5,2)
	destring second, replace force

drop time*
order date hour minute second
*