quantstrat を使用して簡単な取引戦略を構築したいと考えています。パッケージを使わずに書いたストラテジーとは結果が違うので苦労しています。特に、これはアイデアです:
EMA1 > EMA2 & EMA1 > EMA3 & EMA1_lag < EMA1 の場合にロングを取りたい
EMA1 < EMA3 の場合、エグジットして横ばいにしたい
ここに私が書いたものと作品があります:
# trading algo
for( i in 1:nrow(SPY) ) {
# update values for this date
CurrentDate <- time(SPY)[i]
equity = getEndEq(b.strategy, CurrentDate)
ClosePrice <- as.numeric(Cl(SPY[i,]))
Posn <- getPosQty(b.strategy, Symbol='SPY', Date=CurrentDate)
UnitSize = as.numeric(trunc(equity/ClosePrice))
EMA1 <- as.numeric(SPY[i,'EMA_1'])
EMA2 <- as.numeric(SPY[i,'EMA_2'])
EMA3 <- as.numeric(SPY[i,'EMA_3'])
EMA1_lag <- as.numeric(SPY[i,'EMA_1_lag'])
# change market position if necessary
if( !is.na(EMA1) & !is.na(EMA2) & !is.na(EMA3) & !is.na(EMA1_lag) ) {
if( Posn == 0 ) { # No position, test to go Long
if( EMA1 > EMA2 & EMA1 > EMA3 & EMA1_lag<EMA1) {
# enter long position
addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty = UnitSize, TxnFees=0)
}
} else { # Have a position, so check exit
if( EMA1 < EMA3) {
# exit position
addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty = -Posn, TxnFees=0)
} else {
if( i==nrow(SPY) ) # exit on last day
addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
TxnPrice=ClosePrice, TxnQty = -Posn, TxnFees=0)
}
}
}
updatePortf(b.strategy,Dates=CurrentDate)
updateAcct(b.strategy,Dates=CurrentDate)
updateEndEq(b.strategy,CurrentDate)
} # End dates loop
quantstrat では次のように書きましたが、異なる結果が得られます。
add.indicator(strategy = qs.strategy, name = "EMA",
arguments = list(x = quote(na.locf(Cl(mktdata))), n=10), label="EMA1")
add.indicator(strategy = qs.strategy, name = "EMA",
arguments = list(x = quote(na.locf(Cl(mktdata))), n=25), label="EMA2")
add.indicator(strategy = qs.strategy, name = "EMA",
arguments = list(x = quote(na.locf(Cl(mktdata))), n=30), label="EMA3")
add.indicator(strategy = qs.strategy, name = "EMA",
arguments = list(x = quote(lag(na.locf(Cl(mktdata)))), n=10), label="EMA1_lag")
# entry signals
add.signal(qs.strategy,name="sigComparison",
arguments = list(columns=c("EMA1","EMA2"),relationship="gt"),
label="EMA1.gt.EMA2")
add.signal(qs.strategy,name="sigComparison",
arguments = list(columns=c("EMA1","EMA3"),relationship="gt"),
label="EMA1.gt.EMA3")
add.signal(qs.strategy,name="sigComparison",
arguments = list(columns=c("EMA1","EMA1_lag"),relationship="gt"),
label="EMA1.gt.EMA1_lag")
add.signal(qs.strategy, name = "sigFormula",
arguments = list(formula="EMA1.gt.EMA2 & EMA1.gt.EMA3 & EMA1.gt.EMA1_lag"),
label="longEntry")
# exit signals
add.signal(qs.strategy,name="sigComparison",
arguments = list(columns=c("EMA1","EMA3"),relationship="lt"),
label="EMA1.lt.EMA3")
# RULES
# go long when 3 condition
add.rule(qs.strategy, name='ruleSignal',
arguments = list(sigcol="longEntry", sigval=TRUE, orderqty=900,
ordertype='market', orderside='long'),
type='enter')
# exit when 1 condition
add.rule(qs.strategy, name='ruleSignal',
arguments = list(sigcol="EMA1.lt.EMA3", sigval=TRUE, orderqty='all',
ordertype='market', orderside='long'),
type='exit')
同様の結果が得られない理由を理解するのを手伝ってくれる人はいますか?