# BEFORE running the script:
# download price data in .csv format from ICAP allowance price explorer https://icapcarbonaction.com/en/ets-prices and save as icap2025.csv
# adjust directories
# adjust column numbers
fillnan=function(r){
x=0;v=NA;t=ets[,r]
while(x<length(t)) {
x=x+1
if (!is.na(t[x])) {v=t[x]}
else {t[x]=v}}
ets[,r]<<-t}
ets=read.csv("c:\\source\\icap2025.csv", skip=2,header=FALSE,sep=",")
ets<-ets[order(ets[, 1]), ]
#ets<-ets[12:length(ets[,1]),]
leg=c('EU-ETS','UK','Switzerland','New Zealand','RGGI (USA)','China','South Korea')
rows=c(15,29,5,44,62,10,35) #columns for ETS price in local currency
exch=c(12,23,2,37,55,7,31) #columns for exchange rates
ets[,12]<-1 #EUR exchange rate is 1
#merge values which appear in seperate columns in the CSV after system changes (for NZ and EU)
cond<-is.na(ets[,37])
ets[cond,37]<-ets[cond,46]
cond<-is.na(ets[,44])
ets[cond,44]<-ets[cond,53]
cond<-is.na(ets[,12])
ets[cond,12]<-ets[cond,17]
cond<-is.na(ets[,15])
ets[cond,15]<-ets[cond,20]
for (x in 1:length(rows)){
fillnan(rows[x])
fillnan(exch[x])
for (y in 1:length(ets[,1])) {ets[y,rows[x]]=as.numeric(ets[y,rows[x]])*as.numeric(ets[y,exch[x]]) #apply exchange rate
}}
svg(filename="c:/source/ets-carbon-prices2025.svg", width = 8, height = 6, pointsize = 14, family = "sans", bg = "white", antialias = c("default", "none", "gray", "subpixel"))
colors=1:7
matplot(as.Date(ets[,1],format="%Y-%m-%d"), ets[,rows],type="l",lwd=2,lty=1,col=colors,main="Global ETS prices",xlab="",ylab="€/tCO2")
legend("topleft",legend=leg,col=colors,lty=1,cex=1,lwd=3)
axis(side=4, tck=0.05, las=0,tick=TRUE,labels = FALSE)
mtext(side=4,cex=0.75, line=0.05,R.version.string)
dev.off()