-
Notifications
You must be signed in to change notification settings - Fork 20
/
3lowDays.R
114 lines (94 loc) · 2.82 KB
/
3lowDays.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#Description: http://www.investuotojas.eu/?p=409
# author: Dzidorius Martinaitis
require('xts')
require('quantmod')
require('blotter')
require('PerformanceAnalytics')
require('FinancialInstrument')
Sys.setenv(TZ="GMT")
getSymbols('SPY',from='1995-01-01',index.class=c("POSIXt","POSIXct"))
dividends<-getDividends('SPY',from='1995-01-01',index.class=c("POSIXt","POSIXct"))
temp<-cbind(dividends,SPY)
temp[,1][is.na(temp[,1])]<-0
SPY<-cbind(temp[,2],temp[,3],temp[,4],temp[,1]+temp[,5])
colnames(SPY)<-c('Open','High','Low','Close')
#one day before
lag1<-lag((SPY),1)
#two days defore
lag2<-lag((SPY),2)
signal<-ifelse( (Cl(lag2)>Cl(lag1) & Cl(lag1)>Cl(SPY))&
(Hi(lag2)>Hi(lag1) & Hi(lag1)>Hi(SPY)) &
(Op(lag2)>Op(lag1) & Op(lag1)>Op(SPY)),
1,0
)
#one day later
lag3<-lag(Cl(SPY),-1)
profit<-(lag3/Cl(SPY)-1)*signal
chart.CumReturns(profit)
#------------------
tmp<-(Hi(lag2)/Lo(SPY)-1)*signal
#blotter code
symbols<-c('SPY')
SPY<-Cl(SPY)
initDate=time(get(symbols)[1])
initEq=10000
rm(list=ls(envir=.blotter),envir=.blotter)
ltportfolio='3days'
ltaccount='3days'
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
counter<-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 &counter ==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 = unitSize , TxnFees=commssions, verbose=T)
counter<-1
}
}
else
{
#position is open. If signal is 0 - close it.
if(position>0 &counter>=1)
{
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)
counter<-0
}
else
{
print(counter)
counter<-counter+1
}
}
updatePortf(ltportfolio, Dates = currentDate)
updateAcct(ltaccount, Dates = currentDate)
updateEndEq(ltaccount, Dates = currentDate)
}
rez1<-(getPortfolio(ltaccount))
plot(cumsum(rez1$symbols$SPY$txn[,9]))
#gross profit
result<-rez1$symbols$SPY$txn[,7]
result<-result[result!=0]
#fix commission rate 2*3
plot(cumsum(result-6))
chart.CumReturns(cbind((result)/10000,(result-6)/10000))