-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.R
217 lines (181 loc) · 5.34 KB
/
template.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright (c) 2020 Sergey Dugaev
# Licensed under the MIT license.
# See the LICENSE file in the project root for more information.
#
# Functions to be copied into local environments specific to each set of input
# signals and/or prices.
prices <- function() {
# Initializes a list for arguments holding calculation results
# Requires:
# ast - a list for arguments to hold calculation results
# sig - a data frame containing:
# Date (character) - date, time stamp, period or bar ID
# C (numeric) - close price
# T (numeric) - trade price
# ast <<- list()
ast$Date <<- sig$Date
ast$ClosePx <<- sig$C
ast$TradePx <<- sig$T
}
removing <- function(x, keep = 5) {
# Allows to correctly calculate the quantitity of stocks remaining in
# position by deriving close signals from opening signals (additions)
# and the duration of holding period.
#
# Args:
# x: a numeric vector of additions
# keep: an integer indicating the increment of the index to
# the next trade; the default is 5, as if the position was closed
# on the 5th trade day after opening
#
# Returns:
# a vector of removals (num): It has 1 column.
#
# Requires:
# Package 'magrittr' for a forward-pipe operator (%>%)
require("magrittr")
# sdiff <- -x[1:(length(x) - keep)]
# rem <- c(rep(0, keep), sdiff[1:(length(sdiff))])
# return(rem)
-x[1:(length(x) - keep)] %>% shift(., keep)
}
positions <- function() {
positionsAdd()
positionsRem()
positionsEnd()
}
positionsAdd <- function() {
# Positions, additions
ast$S$Pos$I <<- -abs(sig$Dn)
ast$L$Pos$I <<- -abs(sig$Up)
}
positionsRem <- function() {
# Positions, removals
ast$S$Pos$O <<- removing(ast$S$Pos$I, keep = HoldPer)
ast$L$Pos$O <<- removing(ast$L$Pos$I, keep = HoldPer)
}
positionsEnd <- function() {
# Positions, ending balance
ast$S$Pos$E <<- cumsum(ast$S$Pos$I + ast$S$Pos$O)
ast$L$Pos$E <<- cumsum(ast$L$Pos$I + ast$L$Pos$O)
}
quantity <- function() {
stocksAdd()
stocksRem()
stocksVar()
stocksEnd()
}
stocksAdd <- function() {
# Stocks, additions
ast$S$Qty$I <<- -abs(sig$Dn) * LimPP / ast$TradePx
ast$L$Qty$I <<- +abs(sig$Up) * LimPP / ast$TradePx
}
stocksRem <- function() {
# Stocks, removals
ast$S$Qty$O <<- removing(ast$S$Qty$I, keep = HoldPer)
ast$L$Qty$O <<- removing(ast$L$Qty$I, keep = HoldPer)
}
stocksVar <- function() {
# Stocks, change of quantity (variance). Useful for calculating commissions.
# Note: The sum of broker's commissions depend on the difference between
# the quantities of added and removed stocks
ast$S$VarQty <<- ast$S$Qty$I + ast$S$Qty$O
ast$L$VarQty <<- ast$L$Qty$I + ast$L$Qty$O
}
stocksEnd <- function() {
# Stocks, ending balance
ast$S$Qty$E <<- cumsum(ast$S$VarQty)
ast$L$Qty$E <<- cumsum(ast$L$VarQty)
}
commissions <- function() {
# Broker's commissions
ast$S$Comm <<- abs(ast$S$VarQty) * Fee
ast$L$Comm <<- abs(ast$L$VarQty) * Fee
}
proceeds <- function() {
proceedsAdd()
proceedsRem()
}
proceedsAdd <- function() {
# PROCEEDS (CASH FLOW), additions (Gross)
ast$S$CF$I <<- -ast$S$Qty$I * ast$TradePx
ast$L$CF$I <<- -ast$L$Qty$I * ast$TradePx
}
proceedsRem <- function() {
# PROCEEDS (CASH FLOW), removals (Gross)
ast$S$CF$O <<- -ast$S$Qty$O * ast$TradePx
ast$L$CF$O <<- -ast$L$Qty$O * ast$TradePx
}
basis <- function() {
basisAdd()
basisRem()
basisEnd()
}
basisAdd <- function() {
# BASIS, additions
ast$S$Basis$I <<- -ast$S$CF$I
ast$L$Basis$I <<- -ast$L$CF$I
}
basisRem <- function() {
# BASIS, removals
ast$S$Basis$O <<- removing(ast$S$Basis$I, keep = HoldPer)
ast$L$Basis$O <<- removing(ast$L$Basis$I, keep = HoldPer)
}
basisEnd <- function() {
# BASIS, ending balance
ast$S$Basis$E <<- cumsum(ast$S$Basis$I + ast$S$Basis$O)
ast$L$Basis$E <<- cumsum(ast$L$Basis$I + ast$L$Basis$O)
}
totals <- function() {
values()
unrealized()
unrealizedVar()
realized()
netReturns()
cumReturns()
}
values <- function() {
# Value, ending (at the Close price)
ast$S$ValueEoD <<- ast$S$Qty$E * ast$ClosePx
ast$L$ValueEoD <<- ast$L$Qty$E * ast$ClosePx
}
unrealized <- function() {
# Unrealized results
ast$S$Unr <<- ast$S$ValueEoD - ast$S$Basis$E
ast$L$Unr <<- ast$L$ValueEoD - ast$L$Basis$E
}
unrealizedVar <- function() {
# Change in unrealized (variance)
ast$S$VarUnr <<- c(0, diff(ast$S$Unr))
ast$L$VarUnr <<- c(0, diff(ast$L$Unr))
}
realized <- function() {
# Realized results
ast$S$Rzd <<- ast$S$CF$O + ast$S$Basis$O
ast$L$Rzd <<- ast$L$CF$O + ast$L$Basis$O
}
netReturns <- function() {
# Ending returns (daily)
ast$S$Return <<- ast$S$Rzd + ast$S$VarUnr - ast$S$Comm
ast$L$Return <<- ast$L$Rzd + ast$L$VarUnr - ast$L$Comm
}
cumReturns <- function() {
# Cumulative (running) returns
ast$S$CumReturn <<- cumsum(ast$S$Return)
ast$L$CumReturn <<- cumsum(ast$L$Return)
}
assets <- function() {
# Net Asset Values
ast$NAV <<- Cash + ast$S$CumReturn + ast$L$CumReturn
}
drawdowns <- function() {
ast$Drawdown <<- ifelse((ast$NAV - cummax(ast$NAV)) < 0,
(ast$NAV - cummax(ast$NAV)), 0) / Cash
# Maximum Drawdown
ast$MDD <<- cummin(ast$Drawdown)
}
shift <- function(x, n) {
# Returns a numeric vector of the same length as x with 0 propagated n times
# at the beginning and values shifted forward by n positions.
c(rep(0, n), x[1:(length(x))])
}