[R-SIG-Finance] Specify time segment to calculate indicator and trade ?
domodo
1111938 at qq.com
Thu Apr 9 05:17:09 CEST 2015
hi,guys.
quantstrat is very powerful and convient when coding strategy and backtest
it with indicator and pre-define price pattern.but i'm not familiar with
specify the strategy to trade in a segment of trading-time,below are two
cases:
1)establish position from 10:00 to 14:00 only,and close position with
technical exits
2)get the highest and lowest price of the first hour of a day
for example,the strategy below is about price and single sma cross over.
for case 1,i should store price series' date&time in varible 'datetime' as:
[code]
datetime <- as.POSIXlt(index(C))
[/code]
set entry time segment as:
[code]
entrytime1 <- 10
entrytime2 <- 14
[/code]
and add some code below to judge whether the entry signal is in trading time
:
[code]
signal <- ifelse(lag(C)>lag(MA) & lag(C,2)<lag(MA,2),1,
ifelse(lag(C)<lag(MA) & lag(C,2)>lag(MA,2),-1,0))
signal <- ifelse(signal>0 & datetime$hour>entrytime1 &
datetime$hour<entrytime2,1,0)
signal <- ifelse(signal<0 & datetime$hour>entrytime1 &
datetime$hour<entrytime2,-1,0)
[/code]
but the code above is not so elegant at all,and low-efficient.
for the case 2,i should code a function to calculate the first hour's
highest & lowest price of each day,my idea in this function is similiar
to code for case 1,that's, store price series's date&time as a POSIXlt
variable 'datetime',and judge whether hour == 10,min == 15,sec==0,but this
idea is also rough and low-efficient.
any help or tips please ?
below is the example strategy
[code]
library(blotter)
library(quantstrat)
#initialize environtments
currency("USD")
startdate <- '2012-01-18 09:14:00'
finaldate <- '2012-01-19 15:14:00'
future("if2", currency = "USD", multiplier = 300,
tick_size = 0.2)
Sys.setenv(TZ = "UTC")
rm(list=ls(envir=.blotter),envir=.blotter)
b.strategy <- "strategy"
try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE)
initPortf(b.strategy, "if2", initDate = startdate)
initAcct(b.strategy, portfolios = b.strategy, initDate = startdate, initEq =
1000000)
ifBAC2 <- read.table("C:/ifBAC2.csv", head = F, sep =
",")
coredata <- ifBAC2[3:6]
rownames(coredata) <- as.POSIXlt(paste(ifBAC2[,1],ifBAC2[,2]))
ifxts <- as.xts(coredata)
colnames(ifxts) <-
c("open","high","low","close")
if2 <- ifxts['2012-01-18 09:15:00/2012-01-19 15:14:00']
if2$SMA15 <- SMA(Cl(if2),15)
#custom theme
myTheme <- chart_theme()
myTheme$col$dn.col <- "lightgreen"
myTheme$col$up.col <- "red"
myTheme$col$dn.border <- "grey"
myTheme$col$up.border <- "grey"
MA <- if2$SMA15
C <- Cl(if2)
O <- Op(if2)
#trading signal judgement
signal <- ifelse(lag(C)>lag(MA) & lag(C,2)<lag(MA,2),1,
ifelse(lag(C)<lag(MA) & lag(C,2)>lag(MA,2),-1,0))
signal[is.na(signal)] <- 0
#Bar-by-bar treatment
for( i in 1:nrow(if2) )
{
currentDate <- time(if2)[i]
equity<-getEndEq(b.strategy, currentDate)
Posn <- getPosQty(b.strategy, Symbol='if2', Date=currentDate)
#cat(as.character(i),"position on current bar is ",Posn, append = FALSE)
if(!is.na(MA[i]))
{
if( Posn == 0 & signal[i] == 1 ) { #no marketposition, and long signal
occurs
#long entry
openprice <- as.double((Op(if2[currentDate])))
unitsize <- abs(as.numeric(trunc(equity/(openprice*300*0.15))))
addTxn(b.strategy, Symbol='if2', TxnDate=currentDate,
TxnPrice=openprice, TxnQty = unitsize ,
TxnFees=-openprice*300*0.00005*unitsize, verbose = F)
}
else if( Posn != 0 & signal[i] == -1 ) {
#exit position
openprice <- as.double((Op(if2[currentDate])))
unitsize <- abs(getPosQty(b.strategy, Symbol='if2',
Date=currentDate))
addTxn(b.strategy, Symbol='if2', TxnDate=currentDate,
TxnPrice=openprice, TxnQty = -unitsize ,
TxnFees=-openprice*300*0.00005*unitsize, verbose = F)
}
}
updatePortf(b.strategy, Dates = currentDate)
updateAcct(b.strategy, Dates = currentDate)
updateEndEq(b.strategy, Dates = currentDate)
}
chart.Posn(b.strategy, Symbol = "if2", theme = myTheme, TA =
"add_SMA(n=15,col=4)")
txns <- getTxns(Portfolio = b.strategy, Symbol = "if2")
tstats <- tradeStats(Portfolio = b.strategy, Symbol = "if2")
[/code]
data format is as below
[code]
2012/1/18 9:15:00 2577.6 2580.6 2573.6 2577.2
2012/1/18 9:16:00 2577.2 2583.6 2577 2583.2
2012/1/18 9:17:00 2583.4 2583.8 2580.6 2580.8
2012/1/18 9:18:00 2580.8 2581.4 2576.4 2577
2012/1/18 9:19:00 2577 2582 2576.6 2580.4
2012/1/18 9:20:00 2580.4 2581.4 2578.2 2581
2012/1/18 9:21:00 2580.8 2580.8 2578.8 2580
2012/1/18 9:22:00 2580.2 2581 2579.4 2579.8
2012/1/18 9:23:00 2580 2584.4 2579.8 2583.6
2012/1/18 9:24:00 2583.8 2585.2 2576 2576.2
2012/1/18 9:25:00 2577 2579.8 2576.8 2579
2012/1/18 9:26:00 2578.8 2584.2 2578.8 2581.2
2012/1/18 9:27:00 2581 2582.2 2580.6 2581.6
2012/1/18 9:28:00 2581.6 2581.6 2578.8 2579.4
2012/1/18 9:29:00 2580 2583 2579.8 2581.4
[/code]
--
View this message in context: http://r.789695.n4.nabble.com/Specify-time-segment-to-calculate-indicator-and-trade-tp4705639.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list