Quantstrat - Triggering chain rule with lag period

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Quantstrat - Triggering chain rule with lag period

Rmetrics mailing list
Hello all,
Here is what i am trying to achieve : For every entry order (which gets filled, note that some might get rejected because of MaxPos criteria), i want to exit based on the exit rule (which is based on signal generation) or on a second exit rule (which is fixed no. of periods, "n",  from entry) depending upon whichever of these two exit criteria is met first.

The result i am getting is : exit rule 2 (fixed no. of periods) triggers off the following day from the entry rule and not after desired "n" periods. Please if someone can advise - if i am doing something wrong with the syntax here ? Below is the code snapshot of rules
**********************************************************************************eb3 = TRUE
#entry rulestrat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.gt.zero"),sigval=TRUE,orderqty=1,ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos),type='enter',label='enter_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
#exit rule 1strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.lt.zero"), sigval=TRUE,orderqty='all',ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos,orderset='ocolong'),type='exit', label='exit_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
#exit rule 2 strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list(sigcol=c("longExit"), sigval=TRUE,orderqty=-1,ordertype='market',orderside='long',prefer="Open",replace=FALSE, orderset='ocolong'), type='chain', parent='enter_long_alpha5c',label='Sqroff_LONG',enabled=eb3, path.dep = TRUE)
*********************************************************** Note that - in exit rule 2 , the sigcol "longexit" is just a "n" period lag on "signal4.gt.zero" column
Any guidance in this regard will be very helpful.
Regards,Mayank
        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - Triggering chain rule with lag period

Ilya Kipnis
You might want to send a minimum reproducible example.

On Mon, Aug 1, 2016 at 11:38 PM, Mayank Singhal via R-SIG-Finance <
[hidden email]> wrote:

> Hello all,
> Here is what i am trying to achieve : For every entry order (which gets
> filled, note that some might get rejected because of MaxPos criteria), i
> want to exit based on the exit rule (which is based on signal generation)
> or on a second exit rule (which is fixed no. of periods, "n",  from entry)
> depending upon whichever of these two exit criteria is met first.
>
> The result i am getting is : exit rule 2 (fixed no. of periods) triggers
> off the following day from the entry rule and not after desired "n"
> periods. Please if someone can advise - if i am doing something wrong with
> the syntax here ? Below is the code snapshot of rules
> **********************************************************************************eb3
> = TRUE
> #entry rulestrat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.gt.zero"),sigval=TRUE,orderqty=1,ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos),type='enter',label='enter_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep
> = TRUE)
> #exit rule 1strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.lt.zero"), sigval=TRUE,orderqty='all',ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos,orderset='ocolong'),type='exit',
> label='exit_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
> #exit rule 2 strat.st <- add.rule(strat.st,name='ruleSignal',arguments =
> list(sigcol=c("longExit"),
> sigval=TRUE,orderqty=-1,ordertype='market',orderside='long',prefer="Open",replace=FALSE,
> orderset='ocolong'), type='chain',
> parent='enter_long_alpha5c',label='Sqroff_LONG',enabled=eb3, path.dep =
> TRUE)
> *********************************************************** Note that - in
> exit rule 2 , the sigcol "longexit" is just a "n" period lag on
> "signal4.gt.zero" column
> Any guidance in this regard will be very helpful.
> Regards,Mayank
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - Triggering chain rule with lag period

Rmetrics mailing list
Sure, please find attached the sample case.


On Tuesday, August 2, 2016 9:13 AM, Ilya Kipnis <[hidden email]> wrote:


You might want to send a minimum reproducible example.

On Mon, Aug 1, 2016 at 11:38 PM, Mayank Singhal via R-SIG-Finance <[hidden email]> wrote:
Hello all,
Here is what i am trying to achieve : For every entry order (which gets filled, note that some might get rejected because of MaxPos criteria), i want to exit based on the exit rule (which is based on signal generation) or on a second exit rule (which is fixed no. of periods, "n",  from entry) depending upon whichever of these two exit criteria is met first.

The result i am getting is : exit rule 2 (fixed no. of periods) triggers off the following day from the entry rule and not after desired "n" periods. Please if someone can advise - if i am doing something wrong with the syntax here ? Below is the code snapshot of rules
**********************************************************************************eb3 = TRUE
#entry rulestrat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.gt.zero"),sigval=TRUE,orderqty=1,ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos),type='enter',label='enter_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
#exit rule 1strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.lt.zero"), sigval=TRUE,orderqty='all',ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos,orderset='ocolong'),type='exit', label='exit_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
#exit rule 2 strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list(sigcol=c("longExit"), sigval=TRUE,orderqty=-1,ordertype='market',orderside='long',prefer="Open",replace=FALSE, orderset='ocolong'), type='chain', parent='enter_long_alpha5c',label='Sqroff_LONG',enabled=eb3, path.dep = TRUE)
*********************************************************** Note that - in exit rule 2 , the sigcol "longexit" is just a "n" period lag on "signal4.gt.zero" column
Any guidance in this regard will be very helpful.
Regards,Mayank
        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.




_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.

demo_msinghal.R (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - Triggering chain rule with lag period

Rmetrics mailing list


Apologies for repeated email. I think my previous email had some formatting issues.

Here is what i am trying to achieve : For every entry order (which gets filled, note that some might get rejected because of MaxPos criteria), i want to exit based on the exit rule (which is based on signal generation) or on a second exit rule (which is fixed no. of periods, "n",  from entry) depending upon whichever of these two exit criteria is met first.

The result i am getting is : exit rule 2 (fixed no. of periods) triggers off the following day from the entry rule and not after desired "n" periods.


Please see below the code :

##########################################################
require(quantstrat)
Sys.setenv(TZ="UTC")

sigLag <- function(label, data=mktdata, column, cross=FALSE, nLag=1){
colNum <- match.names(column, colnames(data))
ret_sig <- lag(data[,colNum], nLag)
if (isTRUE(cross))
ret_sig <- diff(ret_sig) == 1
if (!missing(label))
colnames(ret_sig) <- label
return(ret_sig)
} # Thanks to Ilya Kipnis

stock1="SPY"
getSymbols(stock1, from = "1998-01-01", to = "2015-12-31")
xts1 = get(stock1)["2010/"]

init.date = "1990-01-01"
start.date = start(xts1)
endDate=end(xts1)
init.equity=100000
MaxPos <- 2
lvls <- 1
eb3 = TRUE

rm.strat(portfolio.st)
rm.strat(account.st)
portfolio.st='msinghal'
account.st='msinghal'

initPortf(portfolio.st,symbols="xts1", initDate = init.date)
initAcct(account.st,portfolios=portfolio.st, initDate = init.date, initEq = init.equity)
initOrders(portfolio=portfolio.st, initDate = init.date)

strat.st<-portfolio.st
strategy(strat.st, store=TRUE)

addPosLimit(portfolio=portfolio.st, timestamp=start.date, symbol="xts1",maxpos=MaxPos, longlevels=lvls, minpos=-MaxPos, shortlevels=lvls)

# indicators
strat.st <- add.indicator(strat.st, name = "SMA", arguments = list(
x = quote(Cl(mktdata)),
n = 50),
label="ind1"
)

strat.st <- add.indicator(strat.st, name="SMA",
arguments = list(
x = quote(Cl(mktdata)),
n = 20),
label="ind2"
)

### signals

strat.st <- add.signal(strat.st, name='sigCrossover',
arguments = list(
columns=c("SMA.ind1","SMA.ind2"),
relationship="gte"
),
label='signal4.gt.zero'
)

strat.st <- add.signal(strat.st, name='sigCrossover',
arguments = list(
columns=c("SMA.ind1","SMA.ind2"),
relationship="lt"
),
label='signal4.lt.zero'
)

strat.st <- add.signal(strat.st,name="sigLag",
arguments=list(
column="signal4.gt.zero",
nLag=5, cross=FALSE
),
label="longExit"
)

#entry rule
strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list(
sigcol=c("signal4.gt.zero"),sigval=TRUE,orderqty=1,
ordertype='market',orderside='long',prefer="Open",
replace=FALSE,threshold=NULL,osFUN=osMaxPos),type='enter',
label='enter_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)

#exit rule 1
strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list(
sigcol=c("signal4.lt.zero"), sigval=TRUE,orderqty='all',
ordertype='market',orderside='long',prefer="Open",
replace=FALSE,threshold=NULL,osFUN=osMaxPos,orderset='ocolong'),
type='exit', label='exit_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)

#exit rule 2
strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list(
sigcol=c("longExit"), sigval=TRUE,orderqty=-1,ordertype='market',
orderside='long',prefer="Open",replace=FALSE, orderset='ocolong'),
type='chain', parent='enter_long_alpha5c',label='Sqroff_LONG',
enabled=eb3, path.dep = TRUE)

start_t<-Sys.time()
out<-applyStrategy(strat.st , portfolios=portfolio.st,mktdata = xts1,verbose=FALSE)
end_t<-Sys.time()
print(end_t-start_t)

start_t<-Sys.time()
updatePortf(Portfolio=portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
try(updateAcct(account.st,Dates = paste(start.date,endDate,sep='/')))
updateEndEq(account.st,Dates = paste(start.date,endDate,sep='/'))
end_t<-Sys.time()
print(end_t-start_t)

ob <- getOrderBook(portfolio.st)
ob2 <- ob$msinghal$xts1
print(ob2)

#########################################################################

Regards,
Mayank




Sure, please find attached the sample case.


On Tuesday, August 2, 2016 9:13 AM, Ilya Kipnis <[hidden email]> wrote:



You might want to send a minimum reproducible example.


On Mon, Aug 1, 2016 at 11:38 PM, Mayank Singhal via R-SIG-Finance <[hidden email]> wrote:

Hello all,

>Here is what i am trying to achieve : For every entry order (which gets filled, note that some might get rejected because of MaxPos criteria), i want to exit based on the exit rule (which is based on signal generation) or on a second exit rule (which is fixed no. of periods, "n",  from entry) depending upon whichever of these two exit criteria is met first.
>
>The result i am getting is : exit rule 2 (fixed no. of periods) triggers off the following day from the entry rule and not after desired "n" periods. Please if someone can advise - if i am doing something wrong with the syntax here ? Below is the code snapshot of rules
>**********************************************************************************eb3 = TRUE
>#entry rulestrat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.gt.zero"),sigval=TRUE,orderqty=1,ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos),type='enter',label='enter_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
>#exit rule 1strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list( sigcol=c("signal4.lt.zero"), sigval=TRUE,orderqty='all',ordertype='market',orderside='long',prefer="Open",replace=FALSE,threshold=NULL,osFUN=osMaxPos,orderset='ocolong'),type='exit', label='exit_long_alpha5c',storefun=FALSE,enabled=eb3,path.dep = TRUE)
>#exit rule 2 strat.st <- add.rule(strat.st,name='ruleSignal',arguments = list(sigcol=c("longExit"), sigval=TRUE,orderqty=-1,ordertype='market',orderside='long',prefer="Open",replace=FALSE, orderset='ocolong'), type='chain', parent='enter_long_alpha5c',label='Sqroff_LONG',enabled=eb3, path.dep = TRUE)
>*********************************************************** Note that - in exit rule 2 , the sigcol "longexit" is just a "n" period lag on "signal4.gt.zero" column
>Any guidance in this regard will be very helpful.
>Regards,Mayank
>        [[alternative HTML version deleted]]
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>-- Subscriber-posting only. If you want to post, subscribe first.
>-- Also note that this is not the r-help list where general R questions should go.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.