daily pastebin goal
31%
SHARE
TWEET

R - FOM effect

tmshu1 Mar 26th, 2011 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(TTR);
  2. library(Hmisc);
  3.  
  4. #symbols = c("SPY","DBC","EEM","QQQQ","DIA","XLF","IWM","VWO","FXI","XLB","XRT","XLE","GLD","DBA","OIL");
  5. #symbols = c("AAPL", "GOOG", "WMT", "KO", "MCD", "IBM", "INTC", "XOM", "AA", "X", "BAC", "GS", "CAT", "CLF", "COST", "AMGN", "ESRX", "CHK");
  6. symbols = c("SPY", "DIA", "QQQQ");
  7.  
  8.  
  9. #create empty avgReturnTables
  10. avgReturnTable = c();
  11. avgReturnTableRev = c();
  12.  
  13. #loop to get the TradingDay, RevTradingDay, and Close for each symbol
  14. #then get get the avgDailyReturns, store them in the avgDailyReturns matrix
  15. for (i in 1:length(symbols)) {
  16.  
  17.         stockdata = getYahooData(symbols[i]);
  18.         closes = stockdata$Close;
  19.        
  20.        
  21.  
  22.         dates = row.names(as.matrix(stockdata));
  23.  
  24.         #calculate 1 day returns
  25.         returns = c();
  26.         for (day in 2:length(dates)) {
  27.                 returns[day]=as.numeric(closes[day])/as.numeric(closes[day-1])-1;
  28.         }
  29.  
  30.         days = c();
  31.         months = c();
  32.  
  33.         #loop through dates, getting the day and month
  34.         for (day in 1:length(dates)) {
  35.                 months[day] = as.numeric(substr(dates[day],6,7));
  36.                 days[day] = as.numeric(substr(dates[day],9,10));
  37.         }
  38.  
  39.         #reverse loop through dates, populating revTradingDay
  40.         revTradingDay=c();
  41.         switch = 0; #switch/flag to start populating revTradingDay
  42.         revTradingDay[length(dates)]=NA;
  43.         for (day in (length(dates)-1):1) {
  44.                 #print(day); #diagnostic
  45.                 #when month switches, flip the switch to start populating
  46.                 #revTradingDay, set revTradingDay to 1
  47.                 if (months[day]<months[day+1]) {
  48.                         #print("month change"); #diagnostic
  49.                         switch = 1;
  50.                         revTradingDay[day]=1;
  51.                 }
  52.                 else if (switch == 1) {
  53.                         revTradingDay[day]=revTradingDay[day+1]+1;
  54.                 }
  55.                 else if (switch==0) {
  56.                         revTradingDay[day]=NA;
  57.                 }
  58.         }
  59.  
  60.         #forward loop through dates, populating tradingDay
  61.         tradingDay = c();
  62.         switch = 0;
  63.         for (day in 2:(length(dates))) {
  64.                 if (months[day]!=months[day-1]) {
  65.                         switch=1;
  66.                         tradingDay[day]=1;
  67.                 }
  68.                 else if (switch ==1) {
  69.                         tradingDay[day]=tradingDay[day-1]+1;
  70.                 }
  71.         }
  72.        
  73.         # create data frame of closes, dates, and their tradingDay numbers
  74.         data=data.frame(cbind(revTradingDay,tradingDay,days,months,closes,returns));
  75.         rownames(data)=dates;
  76.         colnames(data)=c("TradingDayRev","TradingDay","Day","Month","Close","Return");
  77.        
  78.         #populate avgReturnTables
  79.         #first days of the month
  80.         daysin = 22;
  81.         avgReturnTableTemp = c();
  82.         for (i in 1:daysin) {
  83.                 avgReturnTableTemp[i] = mean(data[data$TradingDay==i,]$Return[!is.na(data[data$TradingDay==i,]$Return)]);
  84.         }
  85.  
  86.         #last days of the month
  87.         avgReturnTableRevTemp = c();
  88.         for (i in 1:daysin) {
  89.                 avgReturnTableRevTemp[1+daysin-i] = mean(data[data$TradingDayRev==i,]$Return[!is.na(data[data$TradingDayRev==i,]$Return)]);
  90.         }
  91.         avgReturnTable = cbind(avgReturnTable,avgReturnTableTemp);
  92.         avgReturnTableRev = cbind(avgReturnTableRev,avgReturnTableRevTemp);
  93. }
  94.  
  95. colnames(avgReturnTable)=symbols;
  96. colnames(avgReturnTableRev)=symbols;
  97.  
  98. avgReturnTable = data.frame(avgReturnTable);
  99. avgReturnTableRev = data.frame(avgReturnTableRev);
  100.  
  101. #matplot(avgReturnTable,type="l");
  102.  
  103. #construct vector of avg returns from 10 to 1 TradingDayRev, then from 1 to 10 TradingDay
  104. avgReturnWindow = c();
  105. avgReturnWindow = rbind(avgReturnTableRev[10:1,],avgReturnTable[1:10,]);
  106.  
  107. #plot
  108. arwf = data.frame(avgReturnWindow);
  109. arwf=cbind(c(-10,-9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),arwf);
  110. names(arwf)[1]="day";
  111. matplot(arwf$day,arwf[-1],type="l");
  112. minor.tick(5);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top