0

我是 R 新手,我正试图弄清楚如何让 quantstrat 使用自定义 ordersize 函数。我们的想法是始终将所有可用股权投资于比特币,以便与 B&H 策略相媲美。我提供了可重现的代码。起初它似乎工作正常,但当我查看我的订单时出现问题。也许他们的收盘价之间有些不匹配,但 quantstrat 并没有根据可用资产来订购比特币的数量。例如(从=“2016-12-31”到=“2018-01-01”)在“2017-01-30”,quantstrat 首先在 920.730042 购买价值 10000 美元的比特币,订单数量为 10.8609467963901。在“2017-03-20”,它以 1047.51001 出售初始投资,因此可用权益现在应该是 (10.8609467963901 x 1047.51001)= 11376。9504873,我希望 quantstrat 在 BTCUSD = 1129.869995 时在“2017-04-04”再次购买时应该订购 (11376.9504873 / 1129.869995)= 10.0692562309 BTC。而是订购了 10.3482135951968 BTC。有人可以指出我正确的方向吗?

Order.Qty                    Order.Price    Order.Type Order.Side 
2017-01-30 "10.8609467963901" "920.730042"   "market"   "long"     NA             
2017-03-20 "all"              "1047.51001"   "market"   "long"     NA             
2017-04-04 "10.3482135951968" "1129.869995"  "market"   "long"     NA             
2017-06-27 "all"              "2577.73999"   "market"   "long"     NA             
2017-07-23 "9.36005714412325" "2763.42041"   "market"   "long"     NA             
2017-09-12 "all"              "3870.289551"  "market"   "long"     NA             
2017-09-28 "7.68025279952473" "4172.790527"  "market"   "long"     NA             
2017-12-27 "all"              "15416.633789" "market"   "long"     NA 
        

library("quantstrat")
init.portf <- "2016-12-31"
.from <- init.portf
.to <-"2018-01-01"

Sys.setenv(TZ="UTC")
initEq <- 10000

getSymbols("BTCUSD=X", src = "yahoo", from= .from, to= .to)
BTCUSD <- `BTCUSD=X`
currency(c("USD", "BTC"))
exchange_rate("BTCUSD", currency = "USD")

trend1.strat <- "TrendStrat1"
rm.strat(trend1.strat)

strategy(name=trend1.strat,store=TRUE)

add.indicator(strategy=trend1.strat,name="SMA",
arguments=list(x=quote(Cl(mktdata)),n=5),label="FastSMA")

add.indicator(strategy=trend1.strat,name="SMA",
arguments=list(x=quote(Cl(mktdata)),n=20),label="SlowSMA")

add.signal(strategy=trend1.strat,name="sigCrossover",
arguments=list(columns=c("FastSMA","SlowSMA"),
relationship="gt"),label="BuySignal")

add.signal(strategy=trend1.strat,name="sigCrossover",
arguments=list(columns=c("FastSMA","SlowSMA"),
relationship="lt"),label="SellSignal")

osInvestAll <- function (data, timestamp, orderqty, ordertype, 
orderside, equity, portfolio, symbol, ruletype, ..., initEq) {

     datePos <- format(timestamp,"%Y-%m-%d")
     updatePortf(Portfolio=portfolio,Symbol=symbol,
     Dates=paste0(start(data),"/", datePos))

     trading_pl <- sum(.getPortfolio(portfolio)$summary$Net.Trading.PL)
     equity <- initEq + trading_pl
     ClosePrice <- getPrice(data, prefer = "Close")[datePos]
     UnitSize <- as.numeric((equity / ClosePrice))

     UnitSize
}

add.rule(strategy=trend1.strat,name='ruleSignal',
arguments=list(sigcol="BuySignal",sigval=TRUE,ordertype='market',
orderside='long', osFUN = osInvestAll, prefer = 
"Close"),type='enter',label="EnterRule",enabled=T)

add.rule(strategy=trend1.strat,name='ruleSignal',
arguments=list(sigcol="SellSignal",sigval=TRUE,orderqty='all',
ordertype='market',orderside='long', prefer 
="Close"),type='exit',label="ExitRule",enabled=T)

trend1.portf <- "TrendPort1"
rm.strat(trend1.portf)

initPortf(name=trend1.portf,symbols="BTCUSD",initDate=init.portf)

initAcct(name=trend1.strat,portfolios=trend1.portf,
initDate=init.portf,initEq= initEq)

initOrders(portfolio=trend1.portf,initDate=init.portf)

applyStrategy(strategy=trend1.strat,portfolios=trend1.portf, initEq = 
initEq)

updatePortf(Portfolio=trend1.portf)

updateAcct(name=trend1.strat)

updateEndEq(Account=trend1.strat)

trend1.book <- getOrderBook(portfolio=trend1.portf)
trend1.book
4

1 回答 1

0

通过调整 ordersize 函数修复了这个问题:

osInvestAll <- function (data, timestamp, orderqty, ordertype, 
orderside, equity, portfolio, symbol, ruletype, ..., initEq) {
datePos <- format(timestamp,"%Y-%m-%d %H:%M:%OS")

datePos <- strptime(c(datePos), format = "%Y-%m-%d %H:%M:%OS", tz = 
"UTC") + 86400 #for daily data

updatePortf(Portfolio=portfolio,Symbol=symbol,Dates=paste0(start(data), 
"/", datePos))
# After updating portfolio profit, we can extract the Net.Trading.PL 
earned up to datePos.
trading_pl <- sum(.getPortfolio(portfolio)$summary$Net.Trading.PL)
# The total equity in the strategy for this symbol (and this symbol 
only in isolation always, as this is how quantstrat by default works 
with applyStrategy)
equity <- initEq + trading_pl
ClosePrice <- getPrice(data, prefer = "Close")[datePos]
UnitSize <- as.numeric((equity / ClosePrice))
UnitSize1 <- round(UnitSize, digits = 8)
UnitSize1
}
于 2018-04-30T16:36:17.837 回答