1

好的,我已经阅读了文档、Guy Yollin 的更新幻灯片以及我过去的所有帖子,但我找不到这个问题的答案。在我的 for 循环之后,我仍然收到无法找到对象“USDCHF”的错误。

这是我的代码:

################
# HOUSEKEEPING #
################
setwd("O:/R/R Programs/")
rm(list=ls(all=TRUE))
library(blotter)
.blotter <- new.env()
.instrument <- new.env()


Sys.setenv(TZ="UTC")

source("VariableAndDataFunctions.R")


############
# SETTINGS #
############
StrategyName<-"Test"
CurrencyPair<-"USD_CHF"
RiskIndex<-"RiskIndex"

MAPeriod <- 10
ParamUp <- 0.96
ParamDown <- 0.95
InitialInvestment <- 10000

Lag=1 #default 1, in order not to peek into the future

CrossCurrency <- paste(substrLeft(CurrencyPair, 3), substrRight(CurrencyPair,3),sep="")

#############
# DATASETUP #
#############
#Import datasets, both as XTS
RiskIndicator<-readVariableAsXTS(RiskIndex)
FXCrossTable<-read.table(paste("O:/Data/",CurrencyPair, ".asc", sep=""),sep=",",skip=1,header=FALSE)
FXCrossTable[,4]<-as.Date(as.character(FXCrossTable[,4]), format="%Y%m%d")
colnames(FXCrossTable)<-c("V1","V2","V3", "Date","Close")
FXCrossXTS<-xts(FXCrossTable[,5], order.by=FXCrossTable[,4])
MergedXTS<-merge.xts(FXCrossXTS,RiskIndicator,join='inner')
MergedXTS[,'RiskIndicator']<-SMA(MergedXTS[,'RiskIndicator'],MAPeriod)
colnames(MergedXTS)<-c("Close", "RiskIndicatorMA")


currency("USD")
currency("CHF")
exchange_rate(CrossCurrency)

####################################
# Initialize portfolio and account #
####################################
#Initialize portfolio and account
Risk.Strategy <- StrategyName  #Is only the name for the portfolio strategy
initPortf(Risk.Strategy,CrossCurrency, initDate='1970-12-31')
initAcct(Risk.Strategy,portfolios=Risk.Strategy, initDate='1970-12-31', initEq=1e6)

#######################
# Formating the chart #
#######################
theme<-chart_theme()
theme$col$up.col<-'lightgreen'
theme$col$up.border<-'lightgreen'
theme$col$dn.col<-'pink'
theme$col$dn.border<-'pink'
chart_Series(MergedXTS,theme=theme,name=CrossCurrency)
plot(add_SMA(n=10,col=4,lwd=2))

#################
# Trading logic # (buy when monthly price > 10-month SMA, sell when monthly price < 10-month SMA)
#################
for(i in (MAPeriod+1):nrow(MergedXTS) ) {
  CurrentDate <- time(MergedXTS)[i]
  ClosePrice <- as.numeric(MergedXTS[i,'Close'])
  if(!(is.na(as.numeric(MergedXTS[i,'Close']))) && !(is.na(as.numeric(MergedXTS[i-1,'Close'])))){

    RiskClose <- as.numeric(MergedXTS[i,'RiskIndicatorMA'])
    RiskClose.PreviousDay <- as.numeric(MergedXTS[i-1,'RiskIndicatorMA'])
    Posn <- getPosQty(Risk.Strategy, Symbol=CrossCurrency, Date=CurrentDate)


    if(Posn != 0){
      if(Posn>0){
        #Long active, sell long
        if(RiskClose <= ParamUp && RiskClose.PreviousDay > ParamUp){
          addTxn(Risk.Strategy, Symbol=CrossCurrency, TxnDate=CurrentDate, 
                 TxnPrice=ClosePrice, TxnQty = -1000 , TxnFees=0)
        }
      } else {
        #Short active sell short
        if(RiskClose >= ParamDown && RiskClose.PreviousDay < ParamDown){
          addTxn(Risk.Strategy, Symbol=CrossCurrency, TxnDate=CurrentDate, 
                 TxnPrice=ClosePrice, TxnQty = 1000 , TxnFees=0)
        }
      }
    } else {
      if(RiskClose >= ParamUp && RiskClose.PreviousDay < ParamUp){
        #Buy
        addTxn(Risk.Strategy, Symbol=CrossCurrency, TxnDate=CurrentDate, 
               TxnPrice=ClosePrice, TxnQty = 1000 , TxnFees=0)
      }
      if(RiskClose <= ParamDown && RiskClose.PreviousDay > ParamDown){
        #Short
        addTxn(Risk.Strategy, Symbol=CrossCurrency, TxnDate=CurrentDate, 
               TxnPrice=ClosePrice, TxnQty = -1000 , TxnFees=0)
      }
    }

    # Calculate P&L and resulting equity with blotter
    updatePortf(Risk.Strategy)
    updateAcct(Risk.Strategy)
    updateEndEq(Risk.Strategy)
  }
}

这是错误:

Error in get(Symbol, pos = env) : object 'USDCHF' not found

然而,这行代码给出了以下输出:

> ls(envir=FinancialInstrument:::.instrument)
[1] "CHF"    "SPY"    "USD"    "USDCHF"

所以对象在那里,但找不到..为什么?如果我从 GuyYollins 网站 quantstrat-IR 执行示例代码,它完全可以正常工作......

4

1 回答 1

2

好的,在查看了几个小时的代码后解决了它。错误出在作业中。我将汇率的时间序列数据存储在 MergedXTS 变量中。然而,投资组合中的符号称为 USDCHF。它们需要具有相同的名称,因为blotter 通过使用符号名称(USDCHF)访问所有符号的价格数据。由于到目前为止我还没有 USDCHF 变量,所以它崩溃了。

以下是解决它的代码行:

CurrencyPair<-"USD_CHF"
CrossCurrency <- paste(substrLeft(CurrencyPair, 3), substrRight(CurrencyPair,3), sep="")
assign(CrossCurrency,MergedXTS)

现在我还有另一个问题..但为此我将打开一个新线程。

于 2013-08-19T08:40:11.930 回答