计算具有交易信号的金融时间序列的回报

时间:2020-12-19 19:21:17

I have a financial time series data for which i want to calculate Returns , Maximum Draw down etc based on a signal series . My actual time series is a big one. I am giving here a toy example so that i can tell want i need. Here 1 is for buy signal and -1 is for sell signal. I initiate and hold the trade position till the opposite signal is received and then reverse the position ans so on. Returns should be calculated for every data point so that an Equity Curve can be plotted.

我有一个金融时间序列数据,我想根据信号序列计算回报,最大亏损等。我的实际时间序列很大。我在这里给出一个玩具示例,以便我能说出我需要的东西。这里1代表买入信号,-1代表卖出信号。我启动并持有交易头寸,直到收到相反的信号,然后反转头寸等等。应为每个数据点计算返回值,以便绘制权数曲线。

data<- rnorm(20,100,3)
signal<- c( 1,1,1,1,1,1,1,-1,-1,-1,-1,-1,1,-1,1,-1,-1,-1,-1,1)

For this purpose Quantmod and PerformanceAnalytics comes to my mind.

为此,我想到了Quantmod和PerformanceAnalytics。

Any help appreciated.

任何帮助赞赏。

1 个解决方案

#1


1  

I have no idea of the R financial packages (I wish I knew). I am guessing that your main problem is to know when to trade and when not to trade, and, that after figuring out that, your problem is solved.

我不知道R财务套餐(我希望我知道)。我猜你的主要问题是知道什么时候交易,什么时候不交易,并且在弄清楚之后,你的问题就解决了。

First you may try with a pure R solution. I am a fan of Reduce so you may try with this.

首先,您可以尝试使用纯R解决方案。我是Reduce的粉丝所以你可以试试这个。

deltaTrade <- function(currentTrend,nextSignal) ifelse(lastOp != nextSignal,1,-1)
trade <- Reduce('deltaTrade',signal,init=signal[1],accumulate=TRUE)
tradePeriods = which(trade==1)

If it is too slow I have recently seen in other SO questions that switching to C++ for an efficient solution is a good way to tackle the problem. You can do that with the cpp package, which apparently has become a real hip.

如果它太慢我最近在其他SO问题中看到,切换到C ++以获得有效的解决方案是解决问题的好方法。你可以用cpp包来做到这一点,这显然已成为一个真正的时尚。

library(Rcpp)

cppFunction("NumericVector selectTrades(NumericVector x, NumericVector out) {
  int n = x.length();
  int current = x[0];
  for(int i = 0; i < n; ++i) {
    if (x[i] == current) {
      out[i] = 0; // hold position
    } else {
      current = x[i];
      out[i] = 1; // play position
    }
  }
  return out;
}")

trades = which(selectTrades(signal,out)==1)

Anyway, I hope that any of these helps.

无论如何,我希望这些都有帮助。

#1


1  

I have no idea of the R financial packages (I wish I knew). I am guessing that your main problem is to know when to trade and when not to trade, and, that after figuring out that, your problem is solved.

我不知道R财务套餐(我希望我知道)。我猜你的主要问题是知道什么时候交易,什么时候不交易,并且在弄清楚之后,你的问题就解决了。

First you may try with a pure R solution. I am a fan of Reduce so you may try with this.

首先,您可以尝试使用纯R解决方案。我是Reduce的粉丝所以你可以试试这个。

deltaTrade <- function(currentTrend,nextSignal) ifelse(lastOp != nextSignal,1,-1)
trade <- Reduce('deltaTrade',signal,init=signal[1],accumulate=TRUE)
tradePeriods = which(trade==1)

If it is too slow I have recently seen in other SO questions that switching to C++ for an efficient solution is a good way to tackle the problem. You can do that with the cpp package, which apparently has become a real hip.

如果它太慢我最近在其他SO问题中看到,切换到C ++以获得有效的解决方案是解决问题的好方法。你可以用cpp包来做到这一点,这显然已成为一个真正的时尚。

library(Rcpp)

cppFunction("NumericVector selectTrades(NumericVector x, NumericVector out) {
  int n = x.length();
  int current = x[0];
  for(int i = 0; i < n; ++i) {
    if (x[i] == current) {
      out[i] = 0; // hold position
    } else {
      current = x[i];
      out[i] = 1; // play position
    }
  }
  return out;
}")

trades = which(selectTrades(signal,out)==1)

Anyway, I hope that any of these helps.

无论如何,我希望这些都有帮助。