[R-SIG-Finance] Trailing Stop Loss Execution at Custom Levels
Atakan Okan
atakanokan at outlook.com
Tue May 24 18:39:53 CEST 2016
Hi r-sig-finance,
I want Trailing stops to execute at levels different than the default level, which I assume is the High-TrailingStopDistance. How can I implement trailing stops that get executed at levels different than this? For example: ((High-TrailingStopDistance)+Low)/2 or at the Low of the bar (maybe for stress testing). Below is a reproducible example with prefer=".." arguments commented out. I tried using the prefer argument with different levels such as "High","Low","Open" in add.rule() with ordertype argument passed as "stoptrailing".
Any help is appreciated, thanks.
Atakan Okan
Code:
library(quantstrat)
Sys.setenv(TZ="UTC")
.strategy<- new.env();.blotter<- new.env()
currency('USD')
stock("AAPL", currency="USD", multiplier=1,tick_size= 0.01)
getSymbols('AAPL',src = 'yahoo', from="2014-01-01", to="2015-05-31")
AAPL <- adjustOHLC(AAPL)
strategy.st <- paste("AAPL","SMA_D1",sep = "_")
rm.strat(strategy.st)
initialEquity = 100000
initDate = "2013-12-30"
initPortf(strategy.st, "AAPL", initDate=initDate, currency = "USD")
initAcct(strategy.st, portfolios=strategy.st, initDate=initDate, initEq=initialEquity, currency = "USD")
initOrders(portfolio=strategy.st,initDate=initDate)
strategy(strategy.st,store=TRUE)
txn.model <- 0
positionSizeLong = 1000
positionSizeShort = -1000
longStopLossDistance <- 0.025
shortStopLossDistance <- 0.025
longTrailingStopDistance <- 0.01
shortTrailingStopDistance <- 0.01
add.indicator(strategy.st,
name = "SMA",
arguments = list(x=Cl(eval(parse(text = "AAPL")))
,n=5
),
label='fastsma')
add.indicator(strategy.st,
name = "SMA",
arguments = list(x=Cl(eval(parse(text = "AAPL")))
,n=50
),
label='slowsma')
add.signal(strategy.st,
name="sigCrossover",
arguments = list(columns=c("fastsma","slowsma"),relationship="gt"),
label="fastsma.gt.slowsma")
add.signal(strategy.st,
name="sigCrossover",
arguments = list(columns=c("fastsma","slowsma"),relationship="lt"),
label="fastsma.lt.slowsma")
# Long Entry Rule-------------------------------------------------------------------
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="fastsma.gt.slowsma",
sigval=TRUE,
prefer="Open",
orderqty= positionSizeLong,
#osFUN="osAllInLong",
ordertype='market',
orderside='long',
orderset='ocolong',
TxnFees = txn.model),
type='enter',
label='longenter',
enabled=TRUE
)
# Long Exit Rule-------------------------------------------------------------------
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="fastsma.lt.slowsma",
sigval=TRUE,
prefer="Open",
orderqty='all',
ordertype='market',
orderside='long',
orderset='ocolong',
TxnFees = txn.model),
type='exit',
label='longexit',
enabled=TRUE
)
# Long StopLoss Rule---------------------------------------------------------------------------
add.rule(strategy.st,name='ruleSignal',
arguments = list( sigcol="fastsma.lt.slowsma", sigval=TRUE,
replace=FALSE,
orderside='long',
ordertype='stoplimit',
tmult=TRUE,
threshold=quote( longStopLossDistance ),
orderqty='all',
orderset='ocolong',
TxnFees = txn.model),
type='chain', parent="longenter",
label='StopLossLong',
enabled=TRUE)
# Long Trailing Stop Rule--------------------------------------------------------
add.rule(strategy.st, name = 'ruleSignal',
arguments=list(sigcol="fastsma.lt.slowsma" , sigval=TRUE,
replace=FALSE,
orderside='long',
#prefer="High",
ordertype='stoptrailing',
tmult=TRUE,
threshold=quote(longTrailingStopDistance),
orderqty='all',
orderset='ocolong',
TxnFees = txn.model),
type='chain', parent="longenter",
label='StopTrailingLong',
enabled=TRUE
)
# Short Entry Rule--------------------------------------------------------------------
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="fastsma.lt.slowsma",
sigval=TRUE,
prefer="Open",
orderqty=positionSizeShort,
#osFUN="osAllInShort",
ordertype='market',
orderside='short',
orderset='ocoshort',
TxnFees = txn.model),
type='enter',
label='shortenter',
enabled=TRUE
)
# Short Exit Rule---------------------------------------------------------------------
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="fastsma.gt.slowsma",
sigval=TRUE,
prefer="Open",
orderqty='all',
ordertype='market',
orderside='short',
orderset='ocoshort',
TxnFees = txn.model),
type='exit',
label='shortexit',
enabled=TRUE
)
# Short Stop Loss Rule-----------------------------------------------------------------
add.rule(strategy.st,name='ruleSignal',
arguments = list( sigcol="fastsma.gt.slowsma", sigval=TRUE,
replace=FALSE,
orderside='short',
ordertype='stoplimit',
tmult=TRUE,
threshold=quote( shortStopLossDistance ),
orderqty='all',
orderset='ocoshort',
TxnFees = txn.model),
type='chain', parent="shortenter",
label='StopLossShort',
enabled=TRUE)
# Short Trailing Stop Loss Rule-------------------------------------------------------
add.rule(strategy.st, name = 'ruleSignal',
arguments=list(sigcol="fastsma.gt.slowsma" , sigval=TRUE,
replace=FALSE,
orderside='short',
#prefer="Open",
ordertype='stoptrailing',
tmult=TRUE,
threshold=quote( shortTrailingStopDistance),
orderqty='all',
orderset='ocoshort',
TxnFees = txn.model),
type='chain', parent="shortenter",
label='StopTrailingShort',
enabled=TRUE
)
summary(getStrategy(strategy.st))
applyStrategy( strategy=strategy.st , portfolios=strategy.st
#,parameters=list(n = 5)
,verbose=TRUE)
updatePortf(strategy.st)
updateAcct(strategy.st)
updateEndEq(strategy.st)
results.df.2 <- data.frame(tradeStats(strategy.st))
chart.Posn(strategy.st, Symbol = "AAPL"
, Dates = '2014-04-01::2014-06-25'
)
More information about the R-SIG-Finance
mailing list