8.1 Interday Trading
Trading signal:
Buy signal arises if 14-day RSI < 30
Sell signal arises if 14-day RSI > 50
Trading Rule:
Buy 300 units under buy signal
Sell all when sell signal appears
Initial wealth: 10,000
Note that we need to keep track of both cash and stock holdings.
300
qty <-14
day <-
c() #trade signal
signal <-1:(day+1)] <- 0
signal[
Cl(MSFT)
price <-
c() #stock holding
stock <-1:(day+1)] <-0
stock[
c()
cash <-1:(day+1)] <- 10000 cash[
Trading signal is based on simple RSI:
RSI(price, day) #rsi is the lag of RSI
rsi <-for (i in (day+1): length(price)){
if (rsi[i] < 30){ #buy one more unit if rsi < 30
1
signal[i] <-else if (rsi[i] < 50){ #no change if rsi < 50
} 0
signal[i] <-else { #sell if rsi > 50
} -1
signal[i] <-
}
}reclass(signal,price) signal<-
Assume buying at closing price. We keep track of how cash and stock changes:
Lag(signal) #rsi is the lag of RSI
trade <-for (i in (day+1): length(price)){
if (trade[i]>=0){
stock[i-1] + qty*trade[i]
stock[i] <- cash[i-1] -
cash[i] <- qty*trade[i]*price[i]
else{
} 0
stock[i] <- cash[i-1] +
cash[i] <- stock[i-1]*price[i]
}
}reclass(stock,price)
stock<-reclass(cash,price) cash<-
To evaluate performance, we calculate equity using cash and stock holdings.
c()
equity <-1:(day+1)] <- 10000
equity[
c()
return<-1:(day+1)] <- 0
return[
for (i in (day+1): length(price)){
stock[i] * price[i] + cash[i]
equity[i] <- equity[i]/equity[i-1]-1
return[i] <-
}reclass(equity,price)
equity<-reclass(return,price) return<-
Performance Charts
charts.PerformanceSummary(return,
main="Non-Day-Trading")
We can plot the equity line showing how the performance of the strategy:
chart_Series(equity, main="equity line")
We can check the cash account over time:
chart_Series(cash, name="Cash Holding")
Finall, We can stock holdings:
chart_Series(stock, name="Stock Holding")