Created
April 24, 2012 12:26
-
-
Save kafka399/2479226 to your computer and use it in GitHub Desktop.
rsi blotter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rsi2Vix<-function(ticker,vix) | |
{ | |
getSymbols(c('SPY','VIX'),from='1995-01-01',index.class=c("POSIXt","POSIXct")) | |
rsi2<-RSI(Cl(SPY),2) | |
signal<-ifelse(rsi2>90,-1,0) | |
temp<-ifelse(rsi2<10,1,0) | |
signal<-signal+temp | |
sma<-SMA(Cl(SPY),200) | |
sma<-ifelse(sma<Cl(SPY),1,0) | |
#temp<-ifelse(Cl(VIX)>SMA(Cl(VIX),2),1,0) | |
#signal<-signal*temp | |
signal<-signal*sma | |
#blotter code | |
symbols<-c('SPY') | |
SPY<-Cl(SPY) | |
initDate=time(get(symbols)[1]) | |
initEq=10000 | |
rm(list=ls(envir=.blotter),envir=.blotter) | |
ltportfolio='rsi2' | |
ltaccount='rsi2' | |
initPortf(ltportfolio,symbols, initDate=initDate) | |
initAcct(ltaccount,portfolios=c(ltportfolio), initDate=initDate,initEq=initEq) | |
currency("USD") | |
stock(symbol,currency="USD",multiplier=1) | |
signal[is.na(signal)]<-0 | |
for(i in 2:length(signal)) | |
{ | |
currentDate= time(signal)[i] | |
equity = 10000 #getEndEq(ltaccount, currentDate) | |
position = getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate) | |
print(position) | |
print(currentDate) | |
if(position==0) | |
{ | |
#open a new position if signal is !=0 | |
if((signal[i])!=0 ) | |
{ | |
print('open position') | |
closePrice<-as.double(Cl(SPY[currentDate])) | |
print(closePrice) | |
unitSize = as.numeric(trunc((equity/closePrice))) | |
print(unitSize) | |
commssions=-unitSize*closePrice*0.0003 | |
addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate, TxnPrice=closePrice, TxnQty = as.integer(unitSize*signal[i]) , TxnFees=commssions, verbose=T) | |
} | |
} | |
else | |
{ | |
#position is open. If signal is 0 - close it. | |
if((position)>0 &rsi2[currentDate]>70) | |
{ | |
position = getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate) | |
closePrice<-as.double((Cl(SPY[currentDate])))#as.double(get(symbols[1])[i+100]) | |
commssions=-position*closePrice*0.0003 | |
addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate, TxnPrice=closePrice, TxnQty = -position , TxnFees=commssions, verbose=T) | |
} | |
else if((position)<0 &rsi2[currentDate]<30) | |
{ | |
position = getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate) | |
closePrice<-as.double((Cl(SPY[currentDate])))#as.double(get(symbols[1])[i+100]) | |
commssions=-position*closePrice*0.0003 | |
addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate, TxnPrice=closePrice, TxnQty = -position , TxnFees=commssions, verbose=T) | |
} | |
} | |
updatePortf(ltportfolio, Dates = currentDate) | |
updateAcct(ltaccount, Dates = currentDate) | |
updateEndEq(ltaccount, Dates = currentDate) | |
} | |
rez1<-(getPortfolio(ltaccount)) | |
plot(cumsum(rez1$symbols$SPY$txn[,9])) | |
return(rez1) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment