[英]Strategy is being executed in the second signal of the indicator (Quantstrat)
The strategy is based on RSI.该策略基于 RSI。
When RSI < 30, It's bought the instrument until RSI > 70当 RSI < 30 时,买入仪器直到 RSI > 70
when RSI > 70, It's sold the instrument until RSI < 30当 RSI > 70 时,卖出工具直到 RSI < 30
The strategy is configured to only take the first signal of a row of positives signals.该策略被配置为仅获取一行肯定信号中的第一个信号。 It means that if there's 4 buying signals in a row, It only takes the first signal and waits until appears the selling signal, and the bullish position is closed and it's opened a new bearish position.
这意味着如果连续有4个买入信号,它只取第一个信号,等到出现卖出信号,看涨的position关闭,打开一个新的看跌的position。
The problem is that entries are being executed only where second signal appears.问题是条目仅在出现第二个信号时执行。 Creating a gap between long and short positions.
在多头和空头头寸之间产生差距。
enter image description here在此处输入图像描述
between lines 60-116, there are the add.rule methods used to entry in the positions.在第 60-116 行之间,有用于进入位置的 add.rule 方法。 I can't find why the strategy is waiting to second signal shows up in order to gets executed.
我找不到为什么该策略要等待第二个信号出现才能执行。
Thank you Guys.感谢你们。
library(quantstrat)
library(blotter)
library(foreach)
library(quantmod)
library(PerformanceAnalytics)
library(FinancialInstrument)
library(TTR)
library(xts)
library(zoo)
symbolString <- 'USDCAD=X'
currency('USD')
stock( symbolString, currency = 'USD', multiplier = 1 )
# Load historical Data
initDate <- '2007-01-01'
startDate <- '2012-01-01'
endDate <- '2015-08-10'
init_equity <- 50000
Sys.setenv( TZ = 'UTC')
getSymbols( symbolString, from = startDate, to = endDate, adjust = TRUE, src = 'yahoo')
`USDCAD=X` <- na.omit(`USDCAD=X`)
# Define names for portfolio, account and strategy
portfolioName <- accountName <- strategyName <- 'firstPortfolio'
rm.strat(strategyName)
# Initializae portfolio and account, orderbook and strategy
initPortf( name = portfolioName, symbols = symbolString, initDate = initDate)
initAcct( name = accountName, portfolios = portfolioName, initDate = initDate, initEq = init_equity)
initOrders( portfolio = portfolioName, symbols = symbolString, initDate = initDate)
addPosLimit(strategyName, symbolString, initDate, 1000, 1)
strategy( strategyName, store = TRUE )
# Add RSI indicator
#
add.indicator( strategy = strategyName, name = 'RSI',
arguments = list( price = quote(Cl(mktdata)), maType = "EMA"),
label = 'RSI')
# Adding signals
# Long signal
add.signal(strategy = strategyName, name="sigThreshold",
arguments = list(threshold=30, column="RSI",
relationship="lt",cross =TRUE),
label="longSignal")
# Short signal
add.signal(strategy = strategyName, name="sigThreshold",
arguments = list(threshold=70, column="RSI",
relationship="gt",cross =TRUE),
label="shortSignal")
# Adding rules
# go Long 100 shares
add.rule( strategyName, name = 'ruleSignal',
arguments = list( sigcol = "longSignal",
sigval = TRUE,
orderqty = 1000,
ordertype = "market",
orderside = "long",
osFUN = osMaxPos,
replace = TRUE,
TxnFees = -10),
enabled = TRUE,
type = 'enter',
label = 'EnterLong')
# Close long positions
add.rule( strategyName, name = "ruleSignal",
arguments = list( sigcol = "shortSignal",
sigval = TRUE,
orderside = "long",
ordertype = "market",
orderqty = "all",
TxnFees = -10,
replace = TRUE),
enabled = TRUE,
type = "exit",
label = "ExitLong")
#
# # go short 100 shares
add.rule( strategyName, name = "ruleSignal",
arguments = list( sigcol = "shortSignal",
sigval = TRUE,
orderqty = -1000,
ordertype = "market",
orderside = "short",
osFUN = osMaxPos,
replace = TRUE,
TxnFees = -10),
type = 'enter',
label = 'EnterShort')
# Closing short positions
add.rule( strategyName, name = "ruleSignal",
arguments = list( sigcol = "longSignal",
sigval = TRUE,
orderside = "short",
ordertype = "market",
orderqty = "all",
TxnFees = -10,
replace = TRUE),
type = "exit",
label = "ExitShort")
# Applying strategy to portfolio
results <- applyStrategy( strategy = strategyName, portfolios = portfolioName, symbols = symbolString)
# Updating portfolio
updatePortf( portfolioName)
dateRange <- time( getPortfolio(portfolioName)$summary)[-1]
updateAcct( portfolioName, dateRange)
updateEndEq(accountName)
chart.Posn( strategyName, "USDCAD=X")
Because you have a position limit defined with addPosLimit
, you can use cross=FALSE
in sigThreshold
in add.signal
.因为您有一个使用 addPosLimit 定义的
addPosLimit
限制,所以您可以在sigThreshold
的add.signal
中使用cross=FALSE
。 This way the signal will be true as long as the RSI is above/below your upper/lower threshold and not only when it crosses the threshold.这样,只要 RSI 高于/低于您的上/下阈值,信号就会为真,而不仅仅是当它超过阈值时。 However, its worth mentioning that you probably want to separate your entry signals from your exit signals.
但是,值得一提的是,您可能希望将入场信号与退出信号分开。 They are generally not optimal when used symmetrically.
当对称使用时,它们通常不是最佳的。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.