我的量化交易策略存在问题。 Endequity的结果不考虑我的init_equity为100000.如果我将init_equity更改为100,我得到相同的Endequity。我的代码看起来像这样。有人可以想象我的问题是什么?谢谢
Sys.setenv(TZ = 'UTC')
currency('USD')
init_date <- '2006-12-31'
start_date <- '2007-01-01'
end_date <- '2017-12-31'
init_equity <- '100000'
adjustment <- TRUE
getSymbols(Symbols = 'QQQ', src = 'yahoo', index.class = 'POSIXct',
from = start_date, to = end_date)
QQQ = na.omit(QQQ)
symbols <- 'QQQ'
stock(symbols, currency = 'USD', multiplier = 1)
strategy.st <- 'basic_strat'
portfolio.st <- 'basic_portfolio'
account.st <- 'basic_account'
rm.strat(strategy.st)
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st, symbols = 'QQQ', initDate = init_date)
initAcct(name = account.st, portfolios = portfolio.st,
initDate = init_date, initEq = init_equity)
initOrders(portfolio = portfolio.st, symbols = 'QQQ', initDate = init_date)
strategy(strategy.st, store = TRUE)
tradesize <- 100
for (sym in symbols) {
addPosLimit(portfolio.st, sym, start(get(sym)), tradesize)
}
add.indicator(strategy = strategy.st, name = 'DonchianChannel',
arguments = list(HL = quote(HLC(mktdata)[, 1:2]),
n = 260),
label = 'DCH' )
ratioPriceSeries <- function(x, multiplier = 0.85,
col = 'high.DCH') {
x$ratioPrice <- multiplier * x[, col]
x[,'ratioPrice']
}
add.indicator(strategy = strategy.st, name = 'ratioPriceSeries',
arguments = list(x = quote(mktdata), multiplier = 0.85, col = 'high.DCH'), label = 'Lratio')
mktdata_ind <- applyIndicators(strategy.st, mktdata = QQQ)
mktdata_ind[is.na(mktdata)] = 0
knitr::kable(tail(mktdata_ind))
add.signal(strategy.st, name = 'sigCrossover',
arguments = list(label = 'co', data = quote(mktdata), columns = c('Close', 'Lratio'),
relation = 'gt'), label = 'enterL')
add.signal(strategy.st, name = 'sigCrossover',
arguments = list(label = 'co', data = quote(mktdata), columns = c('Close', 'Lratio'),
relation = 'lt'), label = 'exitL')
mktdata_sig <- applySignals(strategy.st, mktdata = mktdata_ind)
mktdata_sig[is.na(mktdata_sig)] = 0
knitr::kable(tail(mktdata_sig))
sum(mktdata_sig[,'enterL'] == 1,na.rm = TRUE)
sum(mktdata_sig[,'exitL'] == 1, na.rm = TRUE)
add.rule(strategy.st,name='ruleSignal',
arguments = list(sigcol="enterL",
sigval=TRUE,
orderqty=100,
ordertype='market',
orderside='long',
osFUN=osMaxPos,
threshold=NULL,
prefer = 'Open'),
type='enter',
label='enterLong'
)
add.rule(strategy.st,name='ruleSignal',
arguments = list(sigcol="exitL",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='long',
orderset='DC.set',
prefer = 'Open'),
type='exit',
label='exitLong'
)
applyStrategy(strategy.st, portfolio=portfolio.st)
updatePortf(Portfolio = portfolio.st)
updateAcct(account.st)
[1] "basic_account"
Warning message:
In rbind(deparse.level, ...) :
mismatched types: converting objects to numeric
updateEndEq(account.st)
orderbook <- getOrderBook(portfolio.st)
trade_stats <- tradeStats(portfolio.st)
trade_stats1 <- as.data.frame(t(tradeStats(portfolio.st)))
knitr::kable(trade_stats1)
chart.Posn(Portfolio = portfolio.st, Symbol = 'QQQ')
结果如下:
knitr::kable(trade_stats1[c('Portfolio', 'Symbol', 'Num.Txns', 'Num.Trades', 'End.Equity'),])
| |x |
|:----------|:---------------|
|Portfolio |basic_portfolio |
|Symbol |QQQ |
|Num.Txns |17 |
|Num.Trades |8 |
|End.Equity |10570 |
问题在于End.Equity在我看来是错误的。
答案 0 :(得分:2)
我运行了您的代码,并将其更改为:
init_equity <-'100000'
到
init_equity <-'100'
这是我得到的结果:
End.Eq = 2017-12-29 110570
End.Eq = 2017-12-29 10670
尝试运行以下脚本以获取End.Eq:
a <- getAccount(account.st)
End.Eq <- tail(a$summary$End.Eq, n = 1)
代码tradeStats(portfolio.st)
只是为您提供累计PL结束。等式
希望它有所帮助!