【问题标题】:Trying to program trading signals in R尝试在 R 中编写交易信号
【发布时间】:2016-09-24 19:20:14
【问题描述】:

我是 R 新手,正在尝试在 R 中编写配对交易策略。 我已经编写了下载数据的代码。并创建了额外的列并准备了数据。现在我需要计算交易信号。 我的信号规则如下。 - 如果 Z-Score 大于 2.25 ,则卖出该货币对;当 Z-Score 小于 0.25 时回购。 - 如果 Z-Score 小于 -2.25 ,则购买该货币对;当 z-score 高于 -0.25 时卖出(退出)。 - 如果信号发生变化,关闭任何未平仓头寸。

当我们卖出一对时,我们卖出第一只股票并买入第二只股票。在这种情况下,我们出售 ACC 并购买 Ambujacem。 当我们买入一对时,我们买入第一只股票并卖出第二只股票。在这种情况下,我们买入 ACC 并卖出 Ambujacem。 谁能帮我编写交易信号的编码。 附上代码。

问候, 子目录

# Trading Code

library(quantmod)

getSymbols("ACC.NS", from=as.Date('2007-01-01'), to=as.Date('2015-07-24'))

getSymbols("AMBUJACEM.NS", from=as.Date('2007-01-01'), to=as.Date('2015-07-24'))

acc=ACC.NS[,6]

amb=AMBUJACEM.NS[,6]

t.zoo <- merge(acc, amb, all=TRUE)

t.zoo=as.data.frame(t.zoo)

typeof(t.zoo)

t.zoo=na.omit(t.zoo)

#adding columns

t.zoo$spread <- 0

t.zoo$adfTest <- 0

t.zoo$mean <- 0

t.zoo$stdev <- 0

t.zoo$zScore <- 0

t.zoo$signal <- 0

t.zoo$BuyPrice <- 0

t.zoo$SellPrice <- 0

t.zoo$LongReturn <- 0

t.zoo$ShortReturn <- 0

t.zoo$Slippage <- 0

t.zoo$TotalReturn <- 0

#preparing the data
#Calculating the pair ratio
t.zoo$pairRatio <- t.zoo$ACC.NS.Adjusted/t.zoo$AMBUJACEM.NS.Adjusted


#Calculate the log prices of the two time series
t.zoo$LogA <- log10(t.zoo$ACC.NS.Adjusted)

t.zoo$LogB <- log10(t.zoo$AMBUJACEM.NS.Adjusted)

#Calculating the spread
t.zoo$spread <- t.zoo$ACC.NS.Adjusted/t.zoo$AMBUJACEM.NS.Adjusted

#Calculating the mean
# Computes the mean using the SMA function
# choose the number of days for calculating the mean
SMAdays = 20

t.zoo$mean <- SMA(t.zoo$spread,SMAdays)

#Calculating the Std Deviation
t.zoo$stdev <- rollapply(t.zoo$spread,20,sd, fill=NA, align='right')

#Calculating the Z Score
t.zoo$zScore <- (t.zoo$pairRatio - t.zoo$mean)/t.zoo$spread

View(t.zoo)

#Calculation of trading signals and trading prices
#Trigger sell or buy signal if Z Score moves above 2.25 or below -2.25.
# Close position if Z Score reaches 0.2 or -0.2.

# close any open position if there is a change in signal.

【问题讨论】:

  • 这似乎意味着-0.25和+ 0.25之间的区域总是买入,而-2.25和+2.25之外的区域总是卖出。两边0.25到2.25之间的区域不明确。 close any open position if there is a change in signal 这行是什么意思?
  • 我的意思是,如果 Z 分数超过 2.25,那就是卖出。然后,当 Z 分数下降到 0.2 或以下时,我们回购这个头寸。
  • 如果 Z 分数低于 -2.25,则为买入。然后,当 Z 分数移动到 -0.2 或更高时,我们会调整这个位置。
  • 如果有买入头寸,我们必须在开始任何新的卖出之前平仓。
  • 我自己尝试过一些编码。但是,我遇到了一些错误。谁能帮忙。

标签: r


【解决方案1】:

我认为主要问题是为策略提出交易信号,该策略不仅取决于当前指标水平,还取决于指标交叉的方向。

在 cmets 中发布的代码存在许多问题,包括使用单个 = 进行比较。所以我重新开始工作了

这是我解决这个问题的尝试。好像没问题。我添加了一些绘图代码来观察结果。我建议你检查不同时期的结果。

此代码位于原始问题中的代码之后。唯一的区别是我t.zoo 保留为xts/zoo 对象 并且 将其转换为data.frame。另外,我将zScores 乘以 100

它会生成触发日期以及描述策略状态的列。从那里计算回报会很容易

colnames(t.zoo)
#t.zoo must be an xts object 

#working on a separate xts object    
sigs<- t.zoo[, c("ACC.NS.Adjusted",       "AMBUJACEM.NS.Adjusted" , "zScore")]

# creating my own triggers as there are not enough good values
# buyTrig<- mean(t.zoo$zScore ,na.rm = T) - 1*sd(t.zoo$zScore ,na.rm = T)
# sellTrig<- (-1) * buyTrig
# sqOffTrig<- mean(t.zoo$zScore ,na.rm = T) - 0.5*sd(t.zoo$zScore ,na.rm = T)

# Another approach: scaling tz.zoo to fit your criterion
sigs$zScore<- sigs$zScore*100

buyTrig<- (-2.25)
sellTrig<- (-1) * buyTrig
sqOffTrig<- 0.25 
cat ( buyTrig, sellTrig , sqOffTrig)

hist(sigs$zScore, breaks = 40)
abline(v=c(buyTrig,sellTrig), col="red")
abline(v=c(-sqOffTrig, sqOffTrig), col="green")

sum(sigs$zScore >= -sqOffTrig  & sigs$zScore<= sqOffTrig  , na.rm = T) # 139

sigs$action<- 0
sigs$mode <- NA


sigs$zLag<- lag.xts(sigs$zScore,1)
sigs[19:22,]

#these are not the real trigger dates, but they will serve our purpose
# along with na.locf 

buyTrigDays<- time(sigs[sigs$zScore<= buyTrig & sigs$zLag > buyTrig,  ])
sellTrigDays<- time(sigs[sigs$zScore>= sellTrig & sigs$zLag < sellTrig,  ])

#square offs
buySqOffDays<- time( sigs[sigs$zScore>= (-1*sqOffTrig) & sigs$zLag < (-1*sqOffTrig),  ] ) 
buySqOffDays
sellSqOffDays<- time( sigs[sigs$zScore<= (sqOffTrig) & sigs$zLag > (sqOffTrig),  ] ) 
sellSqOffDays

sigs$mode[buyTrigDays]=1 ; sigs$mode[sellTrigDays]= -1;
sigs$mode[buySqOffDays]=0 ; sigs$mode[sellSqOffDays]= 0;

sigs$mode

# use local fill to repeat these triggered position into future 
# till you meet another non NA value 

sigs$mode<- na.locf(sigs$mode, fromLast = F)

plot((sigs$zScore["2015"] ))
points(sigs$zScore[sigs$mode==1], col="red", on=1, pch = 19)
points(sigs$zScore[sigs$mode==-1], col="green", on=1 , pch = 19)
points(sigs$zScore[sigs$mode==0], col="blue", on=1)

sum(is.na(sigs$mode))

#now  to get the real dates when square off is triggered

trigdays<- time( sigs[diff(sigs$mode,1) != 0, ] ) #when the value changes

squareOffTrigger_real<- time(sigs[sigs$mode==0][trigdays])
buyTrigger_real<- time(sigs[sigs$mode==1] [trigdays])
sellTrigger_real<- time(sigs[sigs$mode==-1][trigdays])

#check
length(sellTrigger_real) + length(buyTrigger_real) == length(squareOffTrigger_real)


plot(sigs$zScore["2015"])
points(sigs$zScore[buyTrigger_real]  , col="blue", pch = 19, on=1)
points(sigs$zScore[sellTrigger_real]  , col="red", pch = 19, on=1)
points(sigs$zScore[squareOffTrigger_real]  , col="green", pch = 19, on=1)
abline(h=c(-sqOffTrig, sqOffTrig) , col= "green" )

# further calculations can  be easily made using either the mode 
# column or the trigger dates computed at the end

【讨论】:

猜你喜欢
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 2018-06-14
  • 2018-12-06
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多