Wednesday, April 29, 2015

Getting stock price and exchange rate from Yahoo by Python



#download.py gets stock and exchange rates from Yahoo and then saves the data as a csv file in local drive.
#You may change the url and the filepath if necessary.
#Author: David Tsang 28 APR 2015

import urllib

#historical data
def downloadFile(symbol, filename):
    url = "http://real-chart.finance.yahoo.com/table.csv?s=" + symbol
    directory = "C:\\Python27\\Lib\\site-packages\\QSTK\\QSData\\Yahoo\\"
    print "Downloading " + symbol
    filepath = directory + filename + ".csv"
    urllib.urlretrieve (url, filepath)
    print "- Completed"

#latest quote
def quote(symbol, filepath):
    url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv"
    #print "Downloading " + symbol
    urllib.urlretrieve (url, filepath)

#exchange rate
def exchrate(symbol, filepath):
    url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol +"=X&f=sl1d1t1c1ohgv&e=.csv"
    print "Downloading " + symbol
    urllib.urlretrieve (url, filepath)

Friday, April 24, 2015

Consolidating excel worksheets by VB Script

'This VBS consolidates all worksheets in a excel file and outputs as a csv/text file

Dim conn
Dim Connstr
Dim rs, strSQL
Dim fieldIdx
Dim fileName
Dim recIdx
dim fs,fname
dim linetxt
dim separator
dim schemars
dim headerFlag

dim inputFile
dim outputFile
dim workDir

defaultDir = "C:\"
inputFile = "C:\input.xlsx"
outputFile = "output.csv"

Connstr ="Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=""DSN=Excel Files;DBQ=" & inputFile &";DefaultDir=" & defaultDir &";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"""

Set conn = CreateObject("adodb.connection")
conn.Open Connstr

Set schemars = CreateObject("adodb.recordset")
Set schemars = conn.OpenSchema(20)

Set rs = CreateObject("ADODB.Recordset")

set fs=CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile(outputFile,true)

headerFlag = 0

'For all worksheets in the excel file
 Do until schemaRs.eof

'Process worksheet only. Worksheet name must be ended with a $ sign
'https://support.microsoft.com/en-us/kb/257819

if right(schemaRs(2),2) = "$'" then

strSQL = "select * from ["& schemaRs(2) & "]"

On Error resume next
set rs = conn.execute(strSQL)

if err.number = 0 then

if headerFlag = 0 then
fname.write printHeader(rs)
headerFlag = 1
end if

fname.write printData(rs, schemaRs(2))

rs.close

end if

On Error Goto 0

end if

schemaRs.movenext
 Loop

fname.Close
set fname=nothing
set fs=nothing

conn.Close

msgbox "Complete"

function printHeader(rs)

Dim fieldIdx
Dim returnVal
Dim separator

separator = vbtab

returnVal = "Index"

For fieldidx = 0 to rs.fields.count -1

returnVal = returnVal & separator & rs.fields(fieldidx).name

next

printHeader = returnVal

end function

function printData(rs, idx)

Dim returnVal
Dim linetxt
Dim separator

rs.movenext

Do until rs.eof

  'Add the worksheet name as index in the first column
linetxt = idx
separator = vbtab

for fieldidx = 0 to rs.fields.count -1

linetxt = linetxt & separator & rs(fieldIdx)

next

returnVal = returnVal & vbcrlf & lineTxt

rs.movenext

Loop

printData = returnVal

end function

Sunday, April 12, 2015

Converting Google Map routine to GPX or other GPS device

It is always a good idea to have your driving direction planned before travel. Not only does this allow you to choose the best routine to fit your travel plan, you can also save heaps of money in renting GPS and network usage with the use of offline map.

The most easiest way to do is to use Google Map for finding your directions. However, Google Map routine can only be downloaded in kmz format. There is no direct way to convert kmz to gpx file, yet, there is a free conversion tool named GPSBabel for converting kml file format to GPX or other common file format for GPS devices.

Have these programs ready, and follow the steps below. You will safely arrive your destination.

GPSBabel http://www.gpsbabel.org/download.html

7-zip http://www.7-zip.org/


First thing first, find your direction in Google Map
Say, travelling from McCarran International Airport to Red Rock Canyon National Park

Press the "More" (yes, the button with 3 vertical dots) button , select "Export to KML"
Choose what you want to export, you can export the entire map or just one particular direction..

Once you have the kmz file downloaded, open it with 7-zip (7-zip is free). Extract the kml file.

By using GPS Babel, you can now convert the kml file to the file format which is readable by your GPS /Map program.
In GPSBabel, you could examine the file converted with a preview in Google Map. It is totally optional.

Now you have the file, the final move is to copy the converted to your GPS/Map program.

Have a safe trip :)

Applying SMA10/20, SMA20/50 as trading signals

This is the comparison for results before and after applying SMA10/20 and SMA20/50 in the stock trader. Background Trading 3 stock ma...