均线交易策略的回测 r_使用r创建交易策略并进行回测

均线交易策略的回测 r

R Programming language is an open-source software developed by statisticians and it is widely used among Data Miners for developing Data Analysis. R can be best programmed and developed in RStudio which is an IDE (Integrated Development Environment) for R. The advantages of R programming language include quality plotting and eye catching reports, vast source of packages used for various functions, exemplary source for Data Wrangling, performance of various Machine Learning operations and flashy for Statistics. In my experience, R is the best language for self-learning and also it is very flexible for Financial Analysis and Graphing.

R编程语言是由统计学家开发的开源软件,在数据挖掘人员中广泛用于开发数据分析。 R可以在RStudio(R的一个IDE(集成开发环境))中最好地进行编程和开发。R编程语言的优点包括质量绘图和醒目报告,用于各种功能的大量软件包,用于数据整理的示例性源,各种机器学习操作的性能,并且对统计信息不满意。 以我的经验,R是用于自学的最佳语言,并且对于财务分析和图形处理非常灵活。

In this article, we are going to create six trading strategies and backtest its results using R and its powerful libraries.

在本文中,我们将创建六种交易策略,并使用R及其强大的库对其结果进行回测。

此过程涉及的步骤 (Steps Involved in this process)

1. Importing required libraries

1.导入所需的库

2. Extracting stock data (Apple, Tesla, Netflix) from Yahoo and Basic Plotting

2.从Yahoo和Basic Plotting提取股票数据(Apple,Tesla,Netflix)

3. Creating technical indicators

3.创建技术指标

4. Creating trading signals

4.创建交易信号

5. Creating trading strategies

5.制定交易策略

6. Backtesting and Comparing the results

6.回测和比较结果

步骤1:导入所需的库 (Step-1 : Importing required libraries)

Firstly, to perform our process of creating trading strategies, we have to import the required libraries to our environment. Throughout this process, we will be using some of the most popular finance libraries in R namely Quantmod, TTR and Performance Analytics. Using the library function in R, we can import our required packages. Let’s do it!

首先,要执行创建交易策略的过程,我们必须将所需的库导入到我们的环境中。 在整个过程中,我们将使用R中一些最受欢迎的财务库,即Quantmod,TTR和Performance Analytics。 使用R中的库函数,我们可以导入所需的包。 我们开始做吧!

Yes! We successfully completed our first step!

是! 我们成功地完成了第一步!

步骤2:从Yahoo和基本绘图中提取数据 (Step-2 : Extracting Data from Yahoo and Basic Plotting)

Throughout our process, we will be working with stock price data of Apple, Tesla and Netflix. Let’s extract the data of these companies from Yahoo in R.

在整个过程中,我们将使用Apple,Tesla和Netflix的股价数据。 让我们从R中的Yahoo中提取这些公司的数据。

Now let’s do some visualization of our extracted data! The following code produces a financial bar chart of stock prices along with volume.

现在,让我们对提取的数据进行一些可视化! 以下代码生成股票价格和数量的财务条形图。

Image for post
Image for post
Image for post
Images by Author
图片作者

步骤3:创建技术指标 (Step-3: Creating Technical Indicators)

There are many technical indicators used for financial analysis but, for our analysis we are going to use six of the most famous technical indicators namely : Simple Moving Average (SMA), Parabolic Stop And Reverse (SAR), Commodity Channel Index (CCI), Rate Of Change (ROC), Stochastic Momentum Index (SMI) and finally Williams %R

有许多技术指标可用于财务分析,但在我们的分析中,我们将使用六种最著名的技术指标:简单移动平均线(SMA),抛物线止损和反向(SAR),商品通道指数(CCI),变化率(ROC),随机动量指数(SMI),最后是威廉姆斯%R

Simple Moving Average (SMA) :

简单移动平均线(SMA):

The standard interval of time we are going to use is 20 days SMA and 50 days SMA. But, there no restrictions to use any interval of time.

我们将使用的标准时间间隔是20天SMA和50天SMA。 但是,使用任何时间间隔都没有限制。

The following code will calculate three companies’ 20 days SMA and 50 days SMA along with a plot:

以下代码将计算三个公司的20天SMA和50天SMA以及图表:

# 1. AAPL
sma20_aapl <- SMA(AAPL$AAPL.Close, n = 20)
sma50_aapl <- SMA(AAPL$AAPL.Close, n = 50)
lineChart(AAPL, theme = chartTheme('black'))
addSMA(n = 20, col = 'blue')
addSMA(n = 50, col = 'orange')
legend('left', col = c('green','blue','orange'),
       legend = c('AAPL','SMA20','SMA50'), lty = 1, bty = 'n',
       text.col = 'white', cex = 0.8)
       
# 2. TSLA
sma20_tsla <- SMA(TSLA$TSLA.Close, n = 20)
sma50_tsla <- SMA(TSLA$TSLA.Close, n = 50)
lineChart(TSLA, theme = 'black')
addSMA(n = 20, col = 'blue')
addSMA(n = 50, col = 'orange')
legend('left', col = c('green','blue','orange'),
       legend = c('AAPL','SMA20','SMA50'), lty = 1, bty = 'n',
       text.col = 'white', cex = 0.8)
       
# 3. NFLX
sma20_nflx <- SMA(NFLX$NFLX.Close, n = 20)
sma50_nflx <- SMA(NFLX$NFLX.Close, n = 50)
lineChart(NFLX, theme = 'black')
addSMA(n = 20, col = 'blue')
addSMA(n = 50, col = 'orange')
legend('left', col = c('green','blue','orange'),
       legend = c('AAPL','SMA20','SMA50'), lty = 1, bty = 'n',
       text.col = 'white', cex = 0.8)
Image for post
Image for post
Image for post
Images by Author
图片作者

Parabolic Stop And Reverse (SAR) :

抛物线反转(SAR):

To calculate Parabolic SAR, we have to pass on daily High and Low prices of the companies along with a given acceleration value.

要计算抛物线比吸收率,我们必须传递公司的每日最高价和最低价以及给定的加速值。

The following code will calculate the companies’ Parabolic SAR along with a plot:

以下代码将计算公司的抛物线比吸收率以及图:

# 1. AAPL
sar_aapl <- SAR(cbind(Hi(AAPL),Lo(AAPL)), accel = c(0.02, 0.2))
barChart(AAPL, theme = 'black')
addSAR(accel = c(0.02, 0.2), col = 'lightblue')


# 2. TSLA
sar_tsla <- SAR(cbind(Hi(TSLA),Lo(TSLA)), accel = c(0.02, 0.2))
barChart(TSLA, theme = 'black')
addSAR(accel = c(0.02, 0.2), col = 'lightblue')


# 3. NFLX
sar_nflx <- SAR(cbind(Hi(NFLX),Lo(NFLX)), accel = c(0.02, 0.2))
barChart(NFLX, theme = 'black')
addSAR(accel = c(0.02, 0.2), col = 'lightblue')
Image for post
Image for post
Image for post
Images by Author
图片作者

Commodity Channel Index (CCI) :

商品渠道指数(CCI):

To calculate CCI, we have to pass on daily High, Low and Close prices of companies along with a specified time period and a constant value. In this step, we are going to take 20 days as time period and 0.015 as the constant value.

要计算CCI,我们必须传递公司的每日最高价,最低价和收盘价以及指定的时间段和恒定值。 在此步骤中,我们将时间段设为20天,将常数值设为0.015。

The following code will calculate the companies’ CCI along with a plot:

以下代码将计算公司的CCI以及图:

# 1. AAPL
cci_aapl <- CCI(HLC(AAPL), n = 20, c = 0.015)
barChart(AAPL, theme = 'black')
addCCI(n = 20, c = 0.015)


# 2. TSLA
cci_tsla <- CCI(HLC(TSLA), n = 20, c = 0.015)
barChart(TSLA, theme = 'black')
addCCI(n = 20, c = 0.015)


# 3. NFLX
cci_nflx <- CCI(HLC(NFLX), n = 20, c = 0.015)
barChart(NFLX, theme = 'black')
addCCI(n = 20, c = 0.015)
Image for post
Image for post
Image for post
Images by Author
图片作者

Rate Of Change (ROC)

变化率(ROC)

To calculate ROC, we have to pass on a specified interval of time and there is no restrictions in using any number of period. In this step, we are going to use 25 days as the period of time.

要计算ROC,我们必须经过指定的时间间隔,并且使用任何数量的周期都没有限制。 在此步骤中,我们将使用25天作为时间段。

The following code will calculate the companies’ ROC along with a plot:

以下代码将计算公司的ROC以及图表:

# 1. AAPL
roc_aapl <- ROC(AAPL$AAPL.Close, n = 25)
barChart(AAPL, theme = 'black')
addROC(n = 25)
legend('left', col = 'red', legend = 'ROC(25)', lty = 1, bty = 'n',
       text.col = 'white', cex = 0.8)
       
# 1. TSLA
roc_tsla <- ROC(TSLA$TSLA.Close, n = 25)
barChart(TSLA, theme = 'black')
addROC(n = 25)
legend('left', col = 'red', legend = 'ROC(25)', lty = 1, bty = 'n',
       text.col = 'white', cex = 0.8)
       
# 1. NFLX
roc_nflx <- ROC(NFLX$NFLX.Close, n = 25)
barChart(NFLX, theme = 'black')
addROC(n = 25)
legend('right', col = 'red', legend = 'ROC(25)', lty = 1, bty = 'n',
       text.col = 'white', cex = 0.8)
Image for post
Image for post
Image for post
Images by Author
图片作者

Stochastic Momentum Index (SMI) :

随机动量指数(SMI):

To calculate SMI, we have to pass on daily High, Low and Close prices of companies, a specified interval of time, two smoothing parameters and a signal value.

要计算SMI,我们必须传递公司的每日最高价,最低价和收盘价,指定的时间间隔,两个平滑参数和一个信号值。

The following code will calculate companies’ SMI along with a plot:

以下代码将计算公司的SMI以及图表:

# 1. AAPL
smi_aapl <- SMI(HLC(AAPL),
                n = 13, nFast = 2, nSlow = 25, nSig = 9)
barChart(AAPL, theme = 'black')
addSMI(n = 13, fast = 2, slow = 2, signal = 9)


# 2. TSLA
smi_tsla <- SMI(HLC(TSLA),
                n = 13, nFast = 2, nSlow = 25, nSig = 9)
barChart(TSLA, theme = 'black')
addSMI(n = 13, fast = 2, slow = 2, signal = 9)


# 3. NFLX
smi_nflx <- SMI(HLC(NFLX),
                n = 13, nFast = 2, nSlow = 25, nSig = 9)
barChart(NFLX, theme = 'black')
addSMI(n = 13, fast = 2, slow = 2, signal = 9)
Image for post
Image for post
Image for post
Images by Author
图片作者

Williams %R

威廉姆斯%R

To calculate Williams %R, we have to pass on the daily High, Low and Close prices of companies along with a specified number of periods.

要计算Williams%R,我们必须传递公司的每日最高价,最低价和收盘价以及指定的期间数。

The following code will calculate the companies’ Williams %R along with a plot:

以下代码将计算公司的Williams%R以及图表:

# 1. AAPL
wpr_aapl <- WPR(HLC(AAPL), n = 14)
colnames(wpr_aapl) <- 'wpr'
barChart(AAPL, theme = 'black')
addWPR(n = 14)


# 1. TSLA
wpr_tsla <- WPR(HLC(TSLA), n 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值