-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdumped.r
322 lines (295 loc) · 18.1 KB
/
dumped.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
##' initializestates
##'
##' initialize simulator states, including simulation back ground functionality and many ohter simulator related parameters. return an environment named 'tradingstates'. queuing orders and capital state will be saved and kept updated in tradingstates, see 'Details' for more information. *please read this documentation carefully before running any strategy!*
##'
##' @param tc logical, indicating wehter to use a simulated tradecenter. when tc=TRUE, submitmethod will be coerced to 'lazysubmission'(type ?lazysubmission for details). see 'Details' for more about tradecenter
##' @param Sleep numeric, idle time length of simulated tradecenter, measured in seconds, default 1. see 'Details' for more information.
##' @param IMLAZY logical, pleas set it to TRUE if you are lazy. type ?initializeinstrument for more infromation.
##' @param DIGITSSECS integer, second digits, default 3
##' @param septraded logical, indicating wether to record traded orders separately.
##' @param unclosed logical, indicating wether to track all unclosed orders, set unclosed=TRUE when you need to calculate mean open price and open profit. type ?meanopen for more infromation.
##' @param closed logical, indicating wether to track all zero holding states, set closed=TRUE when you need to calculate close profit.
##' @param interdaily logical, indicating wether to support interdaily trading.
##' @return tradingstates env, an environment in .GlobelEnv containing all the parameters specified above.
##' @details tradingstates: an environment containing all the simulators' parameters, there are two improtant dataframes stored in this envrionment, 'orders' and 'capital'. All current queuing orders will be recorded as one row in 'orders' during simulation. if there haven't submitted any orders or all the orders are traded(i.e. no queuing orders), 'orders' will be a data.frame with 0 rows. each instrument's capital state will be stored as one row in 'capital'. 'capital' has at least one row. one can use queryorder() and qureycapital() inside their strategy to fetch 'orders' and 'capital' from tradingstates.
##'
##' orderhistory: every changed order will be recorded as one additional row in orderhistory after every updating(submit new order, cancel order, partial traded, all traded ...) of orders, saved as either a comma separated table in Redis or a local file. columns of the table are: instrumentid,orderid,direction,price, hands,action,trade time,trade price,cost and status. 'cost' represent the commission of current update, it's calculated from parameter `fee` specified in initializeinstrument(), type ?initializeinstrument for more details; 'status' represent current order's status: 0, all traded; 1, part traded, rest queuing; 2, part traded, rest canceled; 3, no trade, queuing; 4, no trade, no queuing; 5, canceled; 6, submission failed;
##'
##' capitalhistory: the newest capital state will be recorded as one additional row in capitalhistory after each change, saved the same way as orderhistory. the columns are: instrumentid,today's long holdings, today's short holdings, previous long holdings, previous short holdings, total long holdings, total short holdings, cash, update time, trade price, traded hands and cost.
##'
##' tradecenter: a simulated trade center. automatically check for unsatisfied orders and repalce them with new ones to achieve target holdings(target holdings are set by lazysubmission). 'Sleep' set the idle time between each checking, default 0.
##' @seealso \link{initializeinstrument} \link{lazysubmission} \link{meanopen}
##' @export
##' @examples
##'\dontrun{
##' ## wirte capital and order histories to local file, don't sychronize target holdings, don't use simulated trade center and tell the simulator 'I'm not lazy'.
##' initializestates(realtime=FALSE,writeholding=FALSE,tc=FALSE,IMLAZY = FALSE)
##' }
##' @author Chen
##'
initializestates <- function(tc=FALSE,Sleep=1,IMLAZY=FALSE,DIGITSSECS=3,STRINGSASFACTORS=FALSE,septraded=FALSE,unclosed=TRUE,closed=TRUE,interdaily=FALSE){
## second digits, default 3
options(digits.secs=DIGITSSECS)
options(stringsAsFactors = STRINGSASFACTORS)
.tradingstates <- new.env(parent = globalenv())
.tradingstates$orders <- data.frame(
instrumentid=character(),
orderid=character(),direction=numeric(),
price=numeric(),hands=numeric(),
action=character(),
initialhands=numeric(),
timeoutlist=logical(), #wether to check timeout
timeoutchase=logical(), #wether to chase after timeout
timeoutsleep=numeric(), #length of timeout,in secs
chaselist=logical(), #wether to chase
chasesleep=numeric(), #length of chase sleep time,secs
submitstart=character(), #chase or timeout start time
stringsAsFactors=FALSE)
.tradingstates$limitprior <- NULL #high prior limit orders
.tradingstates$capital <- data.frame(
instrumentid=character(),
longholdingstoday=numeric(), shortholdingstoday=numeric(),
longholdingspreday=numeric(),shortholdingspreday=numeric(),
totallongholdings=numeric(),totalshortholdings=numeric(),
cash=numeric(),stringsAsFactors=FALSE
)
.tradingstates$tc <- tc #trade center?
## target holding of trade center
.tradingstates$th <- data.frame(instrumentid=character(),longholding=numeric(),
shortholding=numeric(),stringsAsFactors = FALSE)
## write history to memory
.tradingstates$orderhistory <- data.frame(
instrumentid=character(),orderid=character(),
direction=numeric(),price=numeric(),
hands=numeric(),action=character(),
tradetime=character(),tradeprice=numeric(),
cost=numeric(),status=numeric(),
initialhands=numeric(),
stringsAsFactors = FALSE)
.tradingstates$capitalhistory <- data.frame(
instrumentid=character(),
longholdingstoday=numeric(), shortholdingstoday=numeric(),
longholdingspreday=numeric(),shortholdingspreday=numeric(),
totallongholdings=numeric(),totalshortholdings=numeric(),
cash=numeric(),tradetime=character(),
tradeprice=numeric(),tradehands=numeric(),cost=numeric(),
stringsAsFactors=FALSE)
## save seprated traded order history when septraded=TRUE
.tradingstates$septraded <- septraded
.tradingstates$longopen <- data.frame(
instrumentid=character(),orderid=character(),
action=character(),
direction=numeric(),
tradehands=numeric(),
tradeprice=numeric(),
stringsAsFactors = FALSE)
.tradingstates$shortclose <- .tradingstates$longopen
.tradingstates$shortopen <- .tradingstates$longopen
.tradingstates$shortclose <- .tradingstates$longopen
## current time
.tradingstates$currenttradetime <- character()
## interdaily or not
.tradingstates$interdaily <- interdaily
.tradingstates$startoftheday <- logical()
## verbose
.tradingstates$verbosepriors <- NULL
## trade center invoke tag and sleep recorder
.tradingstates$justchanged <- NULL
.tradingstates$lastchange <- NULL
.tradingstates$Sleep <- Sleep
## instrument-closeprofit tracker
.tradingstates$closed <- closed
.tradingstates$closedtracker <- data.frame(instrumentid=character(),cash=numeric(),stringsAsFactors=FALSE)
## track unclosed orders
.tradingstates$unclosed <- unclosed
.tradingstates$unclosedlong <- .tradingstates$longopen
.tradingstates$unclosedshort <- .tradingstates$longopen
.tradingstates$unclosedsettleprice <- logical()
## I'm lazy?
.tradingstates$IMLAZY <- IMLAZY
if(IMLAZY){
.lazyfunctions()
}
## .tradingstates will be deleted after exit
## assign it to an object in globalenv to aviod deletion
assign(".tradingstates",.tradingstates,envir = globalenv())
}
##' initializeTF
##'
##' a simple wrapper of initializestates() and initializeinstrument() for Treasury Futures. type ?initializestates and ?initializeinstrument for details.
##'
##' @param realtimeDATA logical, indicating wether to use realtime data.
##' @param TFs character, specifying TF ids to be initialized.
##' @param fee named numeric, specifying conmissions of different actions, including open, close, closetoday and closepreday. 'cost' in orderhistory and capitalhistory are result calculated by 'fee'.
##' @param closeprior character, specifying close priority when specified action='close' in ordersubmission. closeprior can only be one of 'today' and 'preday'. when closeprior='today', ordersubmission will close today's holdings prior than previous days', vise versa. type ?ordersubmission for details.
##' @param tc logical, indicating wehter to use a simulated tradecenter. when tc=TRUE, submitmethod will be coerced to 'lazysubmission'(type ?lazysubmission for details). see 'Details' for more about tradecenter
##' @param Sleep numeric, idle time length of simulated tradecenter, measured in seconds, default 1. see 'Details' for more information.
##' @param IMLAZY logical, pleas set it to TRUE if you are lazy. type ?initializeinstrument for more infromation.
##' @param DIGITSSECS integer, second digits, default 3
##' @param septraded logical, indicating wether to save traded orders separately.
##' @param unclosed logical, indicating wether to track all unclosed orders, set unclosed=TRUE when you need to calculate mean open price and open profit. type ?meanopen for more infromation.
##' @param closed logical, indicating wether to track all zero holding states, set closed=TRUE when you need to calculate close profit.
##' @param interdaily logical, indicating wether to support interdaily trading.
##' @return nothing
##' @seealso \link{initializestates} \link{initializeinstrument}
##' @export
##' @author Chen
##'
initializeTF <- function(realtimeDATA=TRUE,TFs="TF1512",fee=c(long=0.00000225,short=0.00000225,closetoday=0.00000225,closepreday=0.00000225),closeprior="today",tc=FALSE,Sleep=1,IMLAZY=FALSE,DIGITSSECS=3,STRINGSASFACTORS=FALSE,septraded=FALSE,unclosed=TRUE,closed=TRUE,interdaily=FALSE){
if(missing(realtimeDATA)){
stop("realtimeDATA must be specified!")
}
## initialize states
initializestates(tc=tc,Sleep=Sleep,IMLAZY=IMLAZY,DIGITSSECS=DIGITSSECS,STRINGSASFACTORS=STRINGSASFACTORS,septraded=septraded,unclosed=unclosed,closed=closed,interdaily = interdaily)
## initialize instruments
if(realtimeDATA){
for(tf in TFs){
initializeinstrument(tf,
pbuyhands = seq(from = 19,by = 2,length.out = 5),
pbuyprice = seq(from = 20,by = 2,length.out = 5),
psellhands = seq(from = 29,by = 2,length.out = 5),
psellprice = seq(from = 30,by = 2,length.out = 5),
ptradetime = 2,plastprice = 8,pvolume = 17,
ppresettleprice=4,
fee = fee,
closeprior = "today",
timeformat="%Y%m%d%H%M%OS",
endoftheday="15:15:00.000",
multiplier=10000)
}
}
else{
for(tf in TFs){
initializeinstrument(tf,
pbuyhands = seq(from = 32,by = 1,length.out = 5),
pbuyprice = seq(from = 22,by = 1,length.out = 5),
psellhands = seq(from = 37,by = 1,length.out = 5),
psellprice = seq(from = 27,by = 1,length.out = 5),
ptradetime = 2,plastprice = 4,pvolume = 12,
ppresettleprice=8,
fee = fee,
closeprior = "today",
timeformat = "%Y-%m-%d %H:%M:%OS",
endoftheday="15:15:00.000",
multiplier = 10000)
}
}
}
##' initializeEnv
##'
##' initialize all simulator states
##'
##' @export
##' @examples
##'\dontrun{
##' instruments <- c("AL1603", "TF1606", "AU1606")
##' detail <- getdetail(instruments)
##' initializeENV(realtimeDATA = FALSE,ENVname = "AL",instruments = instruments[1],exchange = detail$exchanges[1],multiplier = detail$multipliers[1],endoftheday = detail$endofthedays[1])
##' initializeENV(realtimeDATA = FALSE,ENVname = "TF",instruments = instruments[2],exchange = detail$exchanges[2],multiplier = detail$multipliers[2],endoftheday = detail$endofthedays[2])
##' }
##'
initializeENV <- function(realtimeDATA,instruments,exchanges,multipliers,endofthedays,fee=c(long=0.000004,short=0.000004,closetoday=0.000004,closepreday=0.000004),closeprior="preday",tc=FALSE,Sleep=1,IMLAZY=FALSE,DIGITSSECS=3,STRINGSASFACTORS=FALSE,septraded=FALSE,unclosed=TRUE,closed=TRUE,interdaily=FALSE){
if(missing(realtimeDATA)|missing(exchanges)|missing(multipliers)|missing(endofthedays)){
stop("realtimeDATA, exchanges, multipliers and endofthedays must be specified!")
}
if(!all(exchanges%in%c("CFE","SHF","DCE","CZCE"))){
stop("exchange must be one of CFE, SHF, DCE or CZCE")
}
## initialize states
initializestates(tc=tc,Sleep=Sleep,IMLAZY=IMLAZY,DIGITSSECS=DIGITSSECS,STRINGSASFACTORS=STRINGSASFACTORS,septraded=septraded,unclosed=unclosed,closed=closed,interdaily = interdaily)
tryCatch(tmp <- data.frame(instruments=instruments,exchanges=exchanges,multipliers=multipliers,endofthedays=endofthedays,stringsAsFactors = FALSE),
warning=function(w){
stop("exchanges, multipliers, endofthedays can either be length one or the same length as instruments!")
},
error=function(e){
stop("exchanges, multipliers, endofthedays can either be length one or the same length as instruments!")
}
)
## for ifelse only supprot length one output
IFELSE <- function(CONDITION,yes,no){
if(CONDITION){
return(yes)
}else{
return(no)
}
}
for(i in 1:nrow(tmp)){
exchange <- tmp$exchanges[i]
instrument <- tmp$instruments[i]
endoftheday <- tmp$endofthedays[i]
multiplier <- tmp$multipliers[i]
pbuyhands <- switch(exchange,
CFE=IFELSE(realtimeDATA,
seq(from = 19,by = 2,length.out = 5),
seq(from = 32,by = 1,length.out = 5)),
SHF=IFELSE(realtimeDATA,20,30),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
pbuyprice <- switch(exchange,
CFE=IFELSE(realtimeDATA,
seq(from = 20,by = 2,length.out = 5),
seq(from = 22,by = 1,length.out = 5)),
SHF=IFELSE(realtimeDATA,19,28),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
psellhands <- switch(exchange,
CFE=IFELSE(realtimeDATA,
seq(from = 29,by = 2,length.out = 5),
seq(from = 37,by = 1,length.out = 5)),
SHF=IFELSE(realtimeDATA,22,29),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
psellprice <- switch(exchange,
CFE=IFELSE(realtimeDATA,
seq(from = 30,by = 2,length.out = 5),
seq(from = 27,by = 1,length.out = 5)),
SHF=IFELSE(realtimeDATA,21,27),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
ptradetime <- switch(exchange,
CFE=IFELSE(realtimeDATA,2,2),
SHF=IFELSE(realtimeDATA,2,2),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
plastprice <- switch(exchange,
CFE=IFELSE(realtimeDATA,8,4),
SHF=IFELSE(realtimeDATA,3,4),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
pvolume <- switch(exchange,
CFE=IFELSE(realtimeDATA,17,12),
SHF=IFELSE(realtimeDATA,10,8),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
ppresettleprice <- switch(exchange,
CFE=IFELSE(realtimeDATA,4,8),
SHF=IFELSE(realtimeDATA,4,16),
DCE=IFELSE(realtimeDATA,0,0),
CZCE=IFELSE(realtimeDATA,0,0)
)
## closeprior <- "preday" #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
timeformat <- IFELSE(realtimeDATA,"%Y%m%d%H%M%OS",
"%Y-%m-%d %H:%M:%OS")
initializeinstrument(instrument,
pbuyhands = pbuyhands,
pbuyprice = pbuyprice,
psellhands = psellhands,
psellprice = psellprice,
ptradetime = ptradetime,
plastprice = plastprice,
pvolume = pvolume,
ppresettleprice=ppresettleprice,
fee = fee,
closeprior = closeprior,
timeformat= timeformat,
endoftheday= endoftheday,
multiplier=multiplier)
}
}