一个R的程序,判断股票连跌反弹历史记录# Stock
b*5
1 楼
给想要学习R 做简单统计的朋友们,放进去自己改改应该能跑了。这个是新手练习向的。
应该是我发的最后一个主贴了,股版还是吵架的多,讨论技术的确实少。马上回国工作
了也不能看了。
最后可以看出某些股票最多连跌多少天。希望给大家帮助。
这个娱乐性质比较强,真正的model远比这个复杂。
library(quantmod)
library(PerformanceAnalytics)
#library(plyr)
source("calc_return.R")
ticker = "IWM"
bench = "SPY"
start_t = '2010-12-30'
end_t = '2016-01-20'
raw_bench = getSymbols(Symbols=bench,auto.assign = F, from= start_t,to= end_
t)
raw_prices = getSymbols(Symbols=ticker,auto.assign = F, from= start_t,to=
end_t)
dates = index(raw_prices)
l_dates = length(dates)
adj_prices = as.numeric(raw_prices[,6])
adj_bench = as.numeric(raw_bench[,6])
ret_prices = calc_return(adj_prices)
ret_bench = calc_return(adj_bench)
cum_ret_prices = cumprod(ret_prices+1)
cum_ret_bench = cumprod(ret_bench+1)
plot(cum_ret_prices,type='l',col='red',xlab="Dates",ylab="Cummulative
Returns",
main = "Compare of Stock vs. Market")
lines(cum_ret_bench,col='blue')
legend("topleft",col=c("blue","red"),legend = c("Benchmark","Stock"),lty=1)
ret_prices_flags = rep(-1,l_dates-1)
ret_prices_flags[which(ret_prices>0)]=1
ret_bench_flags = rep(-1,l_dates-1)
ret_bench_flags[which(ret_bench>0)]=1
target =-1
a = calc_consecutive_same(ret_prices_flags,target)
b = calc_consecutive_same(ret_bench_flags,target)
diff_ret = ret_prices - ret_bench
diff_flags = rep(-1,l_dates-1)
diff_flags[which(diff_ret>0)]=1
c = calc_consecutive_same(diff_flags,target)
max(a)
max(b)
counts = data.frame(table(c))
countsa = data.frame(table(a))
countsb = data.frame(table(b))
calc_return = function(prices){
num_days = length(prices)
result = (prices[2:num_days]/prices[1:(num_days-1)])-1
return(result)
}
max.subseq cumulative min.cumulative.so.far end begin if (end >= begin) x[begin:end] else x[c()]
}
calc_consecutive_same = function(flags,target){
temp = 0
num_days = length(flags)
result = rep(0,num_days)
for (i in 1: num_days){
if (flags[i]==target){
temp = temp+1
result[i]=temp
}
else{
temp = 0
}
}
return (result)
}
应该是我发的最后一个主贴了,股版还是吵架的多,讨论技术的确实少。马上回国工作
了也不能看了。
最后可以看出某些股票最多连跌多少天。希望给大家帮助。
这个娱乐性质比较强,真正的model远比这个复杂。
library(quantmod)
library(PerformanceAnalytics)
#library(plyr)
source("calc_return.R")
ticker = "IWM"
bench = "SPY"
start_t = '2010-12-30'
end_t = '2016-01-20'
raw_bench = getSymbols(Symbols=bench,auto.assign = F, from= start_t,to= end_
t)
raw_prices = getSymbols(Symbols=ticker,auto.assign = F, from= start_t,to=
end_t)
dates = index(raw_prices)
l_dates = length(dates)
adj_prices = as.numeric(raw_prices[,6])
adj_bench = as.numeric(raw_bench[,6])
ret_prices = calc_return(adj_prices)
ret_bench = calc_return(adj_bench)
cum_ret_prices = cumprod(ret_prices+1)
cum_ret_bench = cumprod(ret_bench+1)
plot(cum_ret_prices,type='l',col='red',xlab="Dates",ylab="Cummulative
Returns",
main = "Compare of Stock vs. Market")
lines(cum_ret_bench,col='blue')
legend("topleft",col=c("blue","red"),legend = c("Benchmark","Stock"),lty=1)
ret_prices_flags = rep(-1,l_dates-1)
ret_prices_flags[which(ret_prices>0)]=1
ret_bench_flags = rep(-1,l_dates-1)
ret_bench_flags[which(ret_bench>0)]=1
target =-1
a = calc_consecutive_same(ret_prices_flags,target)
b = calc_consecutive_same(ret_bench_flags,target)
diff_ret = ret_prices - ret_bench
diff_flags = rep(-1,l_dates-1)
diff_flags[which(diff_ret>0)]=1
c = calc_consecutive_same(diff_flags,target)
max(a)
max(b)
counts = data.frame(table(c))
countsa = data.frame(table(a))
countsb = data.frame(table(b))
calc_return = function(prices){
num_days = length(prices)
result = (prices[2:num_days]/prices[1:(num_days-1)])-1
return(result)
}
max.subseq cumulative min.cumulative.so.far end begin if (end >= begin) x[begin:end] else x[c()]
}
calc_consecutive_same = function(flags,target){
temp = 0
num_days = length(flags)
result = rep(0,num_days)
for (i in 1: num_days){
if (flags[i]==target){
temp = temp+1
result[i]=temp
}
else{
temp = 0
}
}
return (result)
}