currency("RMB")
stock("ZXZQ", currency = "RMB", multiplier = 1)
ls(envir = FinancialInstrument:::.instrument) #设置 .instrument环境
get("RMB", envir = FinancialInstrument:::.instrument)
get("ZXZQ", envir = FinancialInstrument:::.instrument)
Sys.setenv(TZ = "UTC") #设立时区
ZXZQ <- getSymbols("600030.ss", from = "2013-01-01", to = Sys.Date(), src = "yahoo",
auto.assign = FALSE)
Mi=function(x)
{
MI=(Hi(x)+Lo(x))/2
return(MI)
}
ZXZQ$SMA13d <- SMA(Mi(ZXZQ), 13)
ZXZQ$SMA5d <- SMA(Mi(ZXZQ), 5)
ZXZQ$SMA8d <- SMA(Mi(ZXZQ), 8)
head(ZXZQ$SMA13d)
head(ZXZQ$SMA5d)
head(ZXZQ$SMA8d)
myTheme <- chart_theme()
myTheme$col$dn.col <- "lightgreen"
myTheme$col$up.col <- "lightblue"
myTheme$col$dn.border <- "grey"
myTheme$col$up.border <- "grey"
# plot OHLC series
chart_Series(x = ZXZQ, theme = myTheme, name = "ZXZQ", TA = "add_SMA(n=13,col=4)",TB="add_SMA(n=5,col=8)",TC="add_SMA(n=8,col=6)")
b.strategy <- "ck"
initPortf(b.strategy, "ZXZQ", initDate = "2012-12-31")
initAcct(b.strategy, portfolios = b.strategy, initDate = "2012-12-31", initEq = 1e+06)
#初始的资金是1e6,即1,000,000
ls()
ls(.blotter)
ls(envir = FinancialInstrument:::.instrument)
for( i in 1:nrow(ZXZQ) ){
#对日期更新
CurrentDate <- time(ZXZQ)[i]
equity<-getEndEq(b.strategy, CurrentDate)
ClosePrice <- as.numeric(Cl(ZXZQ[i,]))
Posn <- getPosQty(b.strategy, Symbol='ZXZQ', Date=CurrentDate)
UnitSize <-as.numeric(trunc(equity/ClosePrice))#全仓
MA <- as.numeric(ZXZQ[i,'SMA13d'])
MB <- as.numeric(ZXZQ[i,'SMA5d'])
MC <- as.numeric(ZXZQ[i,'SMA8d'])
Ma <- as.numeric(ZXZQ[i-1,'SMA13d'])
Mb <- as.numeric(ZXZQ[i-1,'SMA5d'])
Mc <- as.numeric(ZXZQ[i-1,'SMA8d'])
#如有必要改变头寸
if(!is.na(MA)&!is.na(MB)&!is.na(MC)) #如果移动均线开始{
if( equity != 0 & Posn==0 ) {#没有头寸,测试是否买入
if(ClosePrice > MA &(MA<=Ma|MB<=Mb|MC<=Mc)) {#进入多头头寸(买入)
addTxn(b.strategy, Symbol='ZXZQ', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty =1/2*UnitSize , TxnFees=0)
}else if(ClosePrice > MB &MA>Ma&MB>Mb&MC>Mc){
addTxn(b.strategy, Symbol='ZXZQ', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty=TxnQty+UnitSize , TxnFees=0)
} }else if(equity != 0 & Posn!=0){
if(ClosePrice>MB&MA>Ma&MB>Mb&MC>Mc){
addTxn(b.strategy, Symbol='ZXZQ', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty =UnitSize , TxnFees=0)
} }
else if(equity==0 & Posn!=0 ){
if(ClosePrice < MC&MB<Mb) {#退出头寸
addTxn(b.strategy, Symbol='ZXZQ', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty = -Posn , TxnFees=0)
}}
#计算盈亏并更新
updatePortf(b.strategy, Dates = CurrentDate)
updateAcct(b.strategy, Dates = CurrentDate)
updateEndEq(b.strategy, Dates = CurrentDate)
}
chart.Posn(b.strategy, Symbol = "ZXZQ", Dates = "2013::", theme = myTheme, TRUE)
getTxns(Portfolio = b.strategy, Symbol = "ZXZQ")
tstats <- tradeStats(Portfolio = b.strategy, Symbol = "ZXZQ")
rets <- PortfReturns(Account = b.strategy)
rownames(rets) <- NULL
charts.PerformanceSummary(rets, colorset = redfocus)
tab.perf <- table.Arbitrary(rets, metrics = c("Return.annualized", "SharpeRatio.annualized"),metricsNames = c("Annualized Return", "Annualized Sharpe Ratio"))
tab.perf #计算年化回报和年化的夏普比