Programming: Grab ASNB Fund Prices Using Cronjob
I have some holdings in ASNB mutual funds, both variable- and fixed-priced. However, I've found no available price history for the variable-priced funds out there.
I have written a simple bash script to extract the fund prices from the daily price ASNB website (http://www.asnb.com.my/price.aspx). I then placed a cronjob for this script so that it would grab the prices daily (or Monday to Friday, rather).
Let's call target file to which I'll write the fund prices daily_prices.txt
curl http://www.asnb.com.my/price.aspx > $page
awk -v OFS="\t" '{name[NR]=$1}
END{
line="Date"
for(i=1;i<=NR;i++)
line=line"\t"name[i]
print line }' > daily_prices.txt
This will list the name of the ASNB funds as the header in the file.
Create a bash script file, for example grab_ASNBprices.sh, and paste this code:
#download page
page=$(mktemp)
curl http://www.asnb.com.my/price.aspx > $page
#grab date
date_malay=$(awk '$0~"lblDate"{
split($0,x,">")
split(x[2],y,"<")
print y[1]
}' $page | sed 's/ /_/g')
#grab price list
price_list=$(mktemp)
cat $page | \
awk -v OFS="\t" '$0~"GridView1"{
ok=1
}
(ok){
if($0~"width:90px")
{
split($0,x,">")
split(x[2],y,"<")
fund_name=y[1]
gsub(" ","_",fund_name)
split(x[4],z,"<")
fund_price=z[1]
print fund_name,fund_price
}
}' > $price_list
#check if date is already present
#if not, append new price
if grep "$date_malay" daily_prices.txt; then
echo "Date already present"
else
cat $price_list | \
awk -v OFS="\t" -v p="$date_malay" '{name[NR]=$2}
END{
line=p
for(i=1;i<=NR;i++)
line=line"\t"name[i]
print line
}' >> daily_prices.txt
fi
#clean up
rm $page
Change the permission of grab_ASNBprices.sh so that bash can execute it:
chmod 755 grab_ASNBprices.sh
Then, set up your cronjob to execute grab_ASNBprices.sh daily.
First, do:
crontab -e
And paste this line:
15 8 * * 1-5 /Users/mzabidi/ASBprice/grab_ASNBprices.sh
And your script will be run at every 8.15AM from Monday to Friday. The fund prices will be written to the file daily_prices.txt
This script will only be able to track the fund prices prospectively (from that day on). It will not enable you to view prior prices.
This is the output of the cronjob on 26th of October (click the image to enlarge):
I have written a simple bash script to extract the fund prices from the daily price ASNB website (http://www.asnb.com.my/price.aspx). I then placed a cronjob for this script so that it would grab the prices daily (or Monday to Friday, rather).
Let's call target file to which I'll write the fund prices daily_prices.txt
First, download the page into a temporary file:
page=$(mktemp)curl http://www.asnb.com.my/price.aspx > $page
Then, print a header line into daily_prices.txt:
cat $price_list | \awk -v OFS="\t" '{name[NR]=$1}
END{
line="Date"
for(i=1;i<=NR;i++)
line=line"\t"name[i]
print line }' > daily_prices.txt
This will list the name of the ASNB funds as the header in the file.
Create a bash script file, for example grab_ASNBprices.sh, and paste this code:
#download page
page=$(mktemp)
curl http://www.asnb.com.my/price.aspx > $page
#grab date
date_malay=$(awk '$0~"lblDate"{
split($0,x,">")
split(x[2],y,"<")
print y[1]
}' $page | sed 's/ /_/g')
#grab price list
price_list=$(mktemp)
cat $page | \
awk -v OFS="\t" '$0~"GridView1"{
ok=1
}
(ok){
if($0~"width:90px")
{
split($0,x,">")
split(x[2],y,"<")
fund_name=y[1]
gsub(" ","_",fund_name)
split(x[4],z,"<")
fund_price=z[1]
print fund_name,fund_price
}
}' > $price_list
#check if date is already present
#if not, append new price
if grep "$date_malay" daily_prices.txt; then
echo "Date already present"
else
cat $price_list | \
awk -v OFS="\t" -v p="$date_malay" '{name[NR]=$2}
END{
line=p
for(i=1;i<=NR;i++)
line=line"\t"name[i]
print line
}' >> daily_prices.txt
fi
#clean up
rm $page
Change the permission of grab_ASNBprices.sh so that bash can execute it:
chmod 755 grab_ASNBprices.sh
Then, set up your cronjob to execute grab_ASNBprices.sh daily.
First, do:
crontab -e
And paste this line:
15 8 * * 1-5 /Users/mzabidi/ASBprice/grab_ASNBprices.sh
And your script will be run at every 8.15AM from Monday to Friday. The fund prices will be written to the file daily_prices.txt
This script will only be able to track the fund prices prospectively (from that day on). It will not enable you to view prior prices.
This is the output of the cronjob on 26th of October (click the image to enlarge):
Comments
Post a Comment