均線交易策略的回測 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 = 14)
colnames(wpr_tsla) <- 'wpr'
barChart(TSLA, theme = 'black')
addWPR(n = 14)# 1. NFLX
wpr_nflx <- WPR(HLC(NFLX), n = 14)
colnames(wpr_nflx) <- 'wpr'
barChart(NFLX, theme = 'black')
addWPR(n = 14)
Image for post
Image for post
Image for post
Images by Author
圖片作者

步驟4:創建交易信號 (Step-4 : Creating Trading Signals)

In this step, we are going to use the previously created indicators to build ‘BUY’ or ‘SELL’ trading signals. Basically, we will pass on specific conditions and if the condition satisfies a ‘BUY’ signal our created trading signal will turn to 1 which means a buy. If the condition satisfies a ‘SELL’ signal our created trading signal will turn to -1 which means a sell. If it doesn’t satisfies any of the mentioned conditions, then it will turn to 0 which means nothing. It might sounds like a whole bunch of fuzz but, it will be clear once we dive into the coding section. After coding your trading signals, you can use the ‘which’ command in R to see how may ‘BUY’ signals and how many ‘SELL’ signals.

在這一步中,我們將使用先前創建的指標來建立“買入”或“賣出”交易信號。 基本上,我們將傳遞特定的條件,如果條件滿足“買入”信號,我們創建的交易信號將變為1,表示買入。 如果條件滿足“賣出”信號,我們創建的交易信號將變為-1,這意味著賣出。 如果不滿足上述任何條件,則它將變為0,這意味著沒有任何意義。 聽起來似乎很模糊,但是一旦我們深入到編碼部分,這一點就很清楚了。 在對交易信號進行編碼之后,您可以使用R中的“哪個”命令來查看“買入”信號的數量和“賣出”信號的數量。

Simple Moving Average

簡單移動平均線

The following code will create SMA trading signals for companies if it satisfies our given condition:

如果滿足我們的條件,以下代碼將為公司創建SMA交易信號:

# SMA # a. AAPL
# SMA 20 Crossover Signal 
sma20_aapl_ts <- Lag(ifelse(Lag(Cl(AAPL)) < Lag(sma20_aapl) & Cl(AAPL) > sma20_aapl,1,ifelse(Lag(Cl(AAPL)) > Lag(sma20_aapl) & Cl(AAPL) < sma20_aapl,-1,0)))
sma20_aapl_ts[is.na(sma20_aapl_ts)] <- 0
# SMA 50 Crossover Signal
sma50_aapl_ts <- Lag(ifelse(Lag(Cl(AAPL)) < Lag(sma50_aapl) & Cl(AAPL) > sma50_aapl,1,ifelse(Lag(Cl(AAPL)) > Lag(sma50_aapl) & Cl(AAPL) < sma50_aapl,-1,0)))
sma50_aapl_ts[is.na(sma50_aapl_ts)] <- 0
# SMA 20 and SMA 50 Crossover Signal
sma_aapl_ts <- Lag(ifelse(Lag(sma20_aapl) < Lag(sma50_aapl) & sma20_aapl > sma50_aapl,1,ifelse(Lag(sma20_aapl) > Lag(sma50_aapl) & sma20_aapl < sma50_aapl,-1,0)))
sma_aapl_ts[is.na(sma_aapl_ts)] <- 0# b. TSLA
# SMA 20 Crossover Signal 
sma20_tsla_ts <- Lag(ifelse(Lag(Cl(TSLA)) < Lag(sma20_tsla) & Cl(TSLA) > sma20_tsla,1,ifelse(Lag(Cl(TSLA)) > Lag(sma20_tsla) & Cl(TSLA) < sma20_tsla,-1,0)))
sma20_tsla_ts[is.na(sma20_tsla_ts)] <- 0
# SMA 50 Crossover Signal
sma50_tsla_ts <- Lag(ifelse(Lag(Cl(TSLA)) < Lag(sma50_tsla) & Cl(TSLA) > sma50_tsla,1,ifelse(Lag(Cl(TSLA)) > Lag(sma50_tsla) & Cl(TSLA) < sma50_tsla,-1,0)))
sma50_tsla_ts[is.na(sma50_tsla_ts)] <- 0
# SMA 20 and SMA 50 Crossover Signal
sma_tsla_ts <- Lag(ifelse(Lag(sma20_tsla) < Lag(sma50_tsla) & sma20_tsla > sma50_tsla,1,ifelse(Lag(sma20_tsla) > Lag(sma50_tsla) & sma20_tsla < sma50_tsla,-1,0)))
sma_tsla_ts[is.na(sma_tsla_ts)] <- 0# c. NFLX
# SMA 20 Crossover Signal 
sma20_nflx_ts <- Lag(ifelse(Lag(Cl(NFLX)) < Lag(sma20_nflx) & Cl(NFLX) > sma20_nflx,1,ifelse(Lag(Cl(NFLX)) > Lag(sma20_nflx) & Cl(NFLX) < sma20_nflx,-1,0)))
sma20_nflx_ts[is.na(sma20_nflx_ts)] <- 0
# SMA 50 Crossover Signal
sma50_nflx_ts <- Lag(ifelse(Lag(Cl(NFLX)) < Lag(sma50_nflx) & Cl(NFLX) > sma50_nflx,1,ifelse(Lag(Cl(NFLX)) > Lag(sma50_nflx) & Cl(NFLX) < sma50_nflx,-1,0)))
sma50_nflx_ts[is.na(sma50_nflx_ts)] <- 0
# SMA 20 and SMA 50 Crossover Signal
sma_nflx_ts <- Lag(ifelse(Lag(sma20_nflx) < Lag(sma50_nflx) & sma20_nflx > sma50_nflx,1,ifelse(Lag(sma20_nflx) > Lag(sma50_nflx) & sma20_nflx < sma50_nflx,-1,0)))
sma_nflx_ts[is.na(sma_nflx_ts)] <- 0

Parabolic Stop and Reverse (SAR)

拋物線停止和反向(SAR)

The following code will create Parabolic SAR trading signal for companies if it satisfies our conditions:

如果滿足我們的條件,以下代碼將為公司創建拋物線SAR交易信號:

# Parabolic Stop And Reverse (SAR) # a. AAPL
sar_aapl_ts <- Lag(ifelse(Lag(Cl(AAPL)) < Lag(sar_aapl) & Cl(AAPL) > sar_aapl,1,ifelse(Lag(Cl(AAPL)) > Lag(sar_aapl) & Cl(AAPL) < sar_aapl,-1,0)))
sar_aapl_ts[is.na(sar_aapl_ts)] <- 0# b. TSLA
sar_tsla_ts <- Lag(ifelse(Lag(Cl(TSLA)) < Lag(sar_tsla) & Cl(TSLA) > sar_tsla,1,ifelse(Lag(Cl(TSLA)) > Lag(sar_tsla) & Cl(TSLA) < sar_tsla,-1,0)))
sar_tsla_ts[is.na(sar_tsla_ts)] <- 0# c. NFLX
sar_nflx_ts <- Lag(ifelse(Lag(Cl(NFLX)) < Lag(sar_nflx) & Cl(NFLX) > sar_nflx,1,ifelse(Lag(Cl(NFLX)) > Lag(sar_nflx) & Cl(NFLX) < sar_nflx,-1,0)))
sar_nflx_ts[is.na(sar_nflx_ts)] <- 0

Commodity Channel Index (CCI)

商品渠道指數(CCI)

The following code will create CCI trading signals for companies if it satisfies our conditions:

如果滿足我們的條件,以下代碼將為公司創建CCI交易信號:

# Commodity Channel Index  (CCI)# a. AAPL
cci_aapl_ts <- Lag(ifelse(Lag(cci_aapl) < (-100) & cci_aapl > (-100),1,ifelse(Lag(cci_aapl) < (100) & cci_aapl > (100),-1,0)))
cci_aapl_ts[is.na(cci_aapl_ts)] <- 0# b. TSLA
cci_tsla_ts <- Lag(ifelse(Lag(cci_tsla) < (-100) & cci_tsla > (-100),1,ifelse(Lag(cci_tsla) < (100) & cci_tsla > (100),-1,0)))
cci_tsla_ts[is.na(cci_tsla_ts)] <- 0# c. NFLX
cci_nflx_ts <- Lag(ifelse(Lag(cci_nflx) < (-100) & cci_nflx > (-100),1,ifelse(Lag(cci_nflx) < (100) & cci_nflx > (100),-1,0)))
cci_nflx_ts[is.na(cci_nflx_ts)] <- 0

Rate of Change (ROC)

變化率(ROC)

The following code will create Rate Of Change (ROC) trading signals for companies if it satisfies our conditions:

如果滿足我們的條件,以下代碼將為公司創建更改率(ROC)交易信號:

# Rate of Change (ROC)# a. AAPL
roc_aapl_ts <- Lag(ifelse(Lag(roc_aapl) < (-0.05) & roc_aapl > (-0.05),1,ifelse(Lag(roc_aapl) < (0.05) & roc_aapl > (0.05),-1,0)))
roc_aapl_ts[is.na(roc_aapl_ts)] <- 0# b. TSLA
roc_tsla_ts <- Lag(ifelse(Lag(roc_tsla) < (-0.05) & roc_tsla > (-0.05),1,ifelse(Lag(roc_tsla) < (0.05) & roc_tsla > (0.05),-1,0)))
roc_tsla_ts[is.na(roc_tsla_ts)] <- 0# c. NFLX
roc_nflx_ts <- Lag(ifelse(Lag(roc_nflx) < (-0.05) & roc_nflx > (-0.05),1,ifelse(Lag(roc_nflx) < (0.05) & roc_nflx > (0.05),-1,0)))
roc_nflx_ts[is.na(roc_nflx_ts)] <- 0

Stochastic Momentum Index (SMI)

隨機動量指數(SMI)

The following code will create Stochastic Momentum Index (SMI) trading signals for companies if it satisfies our conditions:

如果滿足我們的條件,以下代碼將為公司創建隨機動量指數(SMI)交易信號:

# Stochastic Momentum Index (SMI)# a. AAPL
smi_aapl_ts <- Lag(ifelse(Lag(smi_aapl[,1]) < Lag(smi_aapl[,2]) & smi_aapl[,1] > smi_aapl[,2],1, ifelse(Lag(smi_aapl[,1]) > Lag(smi_aapl[,2]) & smi_aapl[,1] < smi_aapl[,2],-1,0)))
smi_aapl_ts[is.na(smi_aapl_ts)] <- 0# b. TSLA
smi_tsla_ts <- Lag(ifelse(Lag(smi_tsla[,1]) < Lag(smi_tsla[,2]) & smi_tsla[,1] > smi_tsla[,2],1, ifelse(Lag(smi_tsla[,1]) > Lag(smi_tsla[,2]) & smi_tsla[,1] < smi_tsla[,2],-1,0)))
smi_tsla_ts[is.na(smi_tsla_ts)] <- 0# a. NFLX
smi_nflx_ts <- Lag(ifelse(Lag(smi_nflx[,1]) < Lag(smi_nflx[,2]) & smi_nflx[,1] > smi_nflx[,2],1, ifelse(Lag(smi_nflx[,1]) > Lag(smi_nflx[,2]) & smi_nflx[,1] < smi_nflx[,2],-1,0)))
smi_nflx_ts[is.na(smi_nflx_ts)] <- 0

Williams %R

威廉姆斯%R

The following code will create Williams %R trading signals for companies if it satisfies our conditions:

如果滿足我們的條件,以下代碼將為公司創建Williams%R交易信號:

# Williams %R# a. AAPL
wpr_aapl_ts <- Lag(ifelse(Lag(wpr_aapl) > 0.8 & wpr_aapl < 0.8,1,ifelse(Lag(wpr_aapl) > 0.2 & wpr_aapl < 0.2,-1,0)))
wpr_aapl_ts[is.na(wpr_aapl_ts)] <- 0# b. TSLA
wpr_tsla_ts <- Lag(ifelse(Lag(wpr_tsla) > 0.8 & wpr_tsla < 0.8,1,ifelse(Lag(wpr_tsla) > 0.2 & wpr_tsla < 0.2,-1,0)))
wpr_tsla_ts[is.na(wpr_tsla_ts)] <- 0# c. NFLX
wpr_nflx_ts <- Lag(ifelse(Lag(wpr_nflx) > 0.8 & wpr_nflx < 0.8,1,ifelse(Lag(wpr_nflx) > 0.2 & wpr_nflx < 0.2,-1,0)))
wpr_nflx_ts[is.na(wpr_nflx_ts)] <- 0

步驟5:制定交易策略 (Step-5 : Creating Trading Strategies)

This is the most interesting step in our process as we are going to create Trading Strategies using our previously created Trading Signals. In our previous step, we created signals whether to buy or sell and in this step we are going to create conditions to check our holding position in that stock (i.e., whether we hold, bought or sold the stock). The trading strategy we are about to code will return 1 if we hold the stock or returns 0 if we don’t own the stock.

這是我們流程中最有趣的步驟,因為我們將使用先前創建的交易信號來創建交易策略。 在上一步中,我們創建了買入或賣出信號,在這一步中,我們將創建條件來檢查我們在該股票中的持倉狀況(即我們是否持有,買入或賣出該股票)。 如果我們持有股票,我們將要編碼的交易策略將返回1;如果我們不擁有股票,則將返回0。

Simple Moving Average

簡單移動平均線

The following code will create a SMA trading strategy if satisfies our given conditions:

如果滿足我們的條件,以下代碼將創建SMA交易策略:

# SMA 20 and SMA 50 Crossover Strategy# a. AAPLsma_aapl_strat <- ifelse(sma_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {sma_aapl_strat[i] <- ifelse(sma_aapl_ts[i] == 1,1,ifelse(sma_aapl_ts[i] == -1,0,sma_aapl_strat[i-1]))
}
sma_aapl_strat[is.na(sma_aapl_strat)] <- 1
sma_aapl_stratcomp <- cbind(sma20_aapl, sma50_aapl, sma_aapl_ts, sma_aapl_strat)
colnames(sma_aapl_stratcomp) <- c('SMA(20)','SMA(50)','SMA SIGNAL','SMA POSITION')# b. TSLA
sma_tsla_strat <- ifelse(sma_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {sma_tsla_strat[i] <- ifelse(sma_tsla_ts[i] == 1,1,ifelse(sma_tsla_ts[i] == -1,0,sma_tsla_strat[i-1]))
}
sma_tsla_strat[is.na(sma_tsla_strat)] <- 1
sma_tsla_stratcomp <- cbind(sma20_tsla, sma50_tsla, sma_tsla_ts, sma_tsla_strat)
colnames(sma_tsla_stratcomp) <- c('SMA(20)','SMA(50)','SMA SIGNAL','SMA POSITION')# c. NFLX
sma_nflx_strat <- ifelse(sma_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {sma_nflx_strat[i] <- ifelse(sma_nflx_ts[i] == 1,1,ifelse(sma_nflx_ts[i] == 'SEL',0,sma_nflx_strat[i-1]))
}
sma_nflx_strat[is.na(sma_nflx_strat)] <- 1
sma_nflx_stratcomp <- cbind(sma20_nflx, sma50_nflx, sma_nflx_ts, sma_nflx_strat)
colnames(sma_nflx_stratcomp) <- c('SMA(20)','SMA(50)','SMA SIGNAL','SMA POSITION')
Image for post
Image by Author
圖片作者

This is an example table of our created SMA Apple trading strategy. In this table we can observe that, on 2019–05–24 SMA 20 went below SMA 50 so our created signal returns -1 (‘SELL’) the next day also our position changes to 0 which means we don’t hold the stock. From this, we can say that our created strategy works as we expected. Now, let’s proceed to the remaining Trading Strategies.

這是我們創建的SMA Apple交易策略的示例表。 在此表中,我們可以觀察到,在2019–05–24 SMA 20低于SMA 50,因此我們創建的信號在第二天返回-1(“賣出”),我們的頭寸也變為0,這意味著我們不持有股票。 由此,我們可以說我們創建的策略可以按預期工作。 現在,讓我們繼續其余的交易策略。

Parabolic Stop And Reverse (SAR)

拋物線停止和反轉(SAR)

The following code will create a Parabolic SAR trading strategy if satisfies our given conditions:

如果滿足我們的條件,以下代碼將創建拋物線形SAR交易策略:

# Parabolic SAR Strategy # a. AAPL
sar_aapl_strat <- ifelse(sar_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {sar_aapl_strat[i] <- ifelse(sar_aapl_ts[i] == 1,1,ifelse(sar_aapl_ts[i] == -1,0,sar_aapl_strat[i-1]))
}
sar_aapl_strat[is.na(sar_aapl_strat)] <- 1
sar_aapl_stratcomp <- cbind(Cl(AAPL), sar_aapl, sar_aapl_ts, sar_aapl_strat)
colnames(sar_aapl_stratcomp) <- c('Close','SAR','SAR SIGNAL','SAR POSITION')# b. TSLA
sar_tsla_strat <- ifelse(sar_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {sar_tsla_strat[i] <- ifelse(sar_tsla_ts[i] == 1,1,ifelse(sar_tsla_ts[i] == -1,0,sar_tsla_strat[i-1]))
}
sar_tsla_strat[is.na(sar_tsla_strat)] <- 1
sar_tsla_stratcomp <- cbind(Cl(TSLA), sar_tsla, sar_tsla_ts, sar_tsla_strat)
colnames(sar_tsla_stratcomp) <- c('Close','SAR','SAR SIGNAL','SAR POSITION')# c. NFLX
sar_nflx_strat <- ifelse(sar_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {sar_nflx_strat[i] <- ifelse(sar_nflx_ts[i] == 1,1,ifelse(sar_nflx_ts[i] == -1,0,sar_nflx_strat[i-1]))
}
sar_nflx_strat[is.na(sar_nflx_strat)] <- 1
sar_nflx_stratcomp <- cbind(Cl(NFLX), sar_nflx, sar_nflx_ts, sar_nflx_strat)
colnames(sar_nflx_stratcomp) <- c('Close','SAR','SAR SIGNAL','SAR POSITION')

Commodity Channel Index (CCI) :

商品渠道指數(CCI):

The following code will create a CCI trading strategy if satisfies our given conditions:

如果滿足我們的條件,以下代碼將創建CCI交易策略:

# CCI# a. AAPL
cci_aapl_strat <- ifelse(cci_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {cci_aapl_strat[i] <- ifelse(cci_aapl_ts[i] == 1,1,ifelse(cci_aapl_ts[i] == -1,0,cci_aapl_strat[i-1]))
}
cci_aapl_strat[is.na(cci_aapl_strat)] <- 1
cci_aapl_stratcomp <- cbind(cci_aapl, cci_aapl_ts, cci_aapl_strat)
colnames(cci_aapl_stratcomp) <- c('CCI','CCI SIGNAL','CCI POSITION')# b. TSLA
cci_tsla_strat <- ifelse(cci_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {cci_tsla_strat[i] <- ifelse(cci_tsla_ts[i] == 1,1,ifelse(cci_tsla_ts[i] == -1,0,cci_tsla_strat[i-1]))
}
cci_tsla_strat[is.na(cci_tsla_strat)] <- 1
cci_tsla_stratcomp <- cbind(cci_tsla, cci_tsla_ts, cci_tsla_strat)
colnames(cci_tsla_stratcomp) <- c('CCI','CCI SIGNAL','CCI POSITION')# c. NFLX
cci_nflx_strat <- ifelse(cci_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {cci_nflx_strat[i] <- ifelse(cci_nflx_ts[i] == 1,1,ifelse(cci_nflx_ts[i] == -1,0,cci_nflx_strat[i-1]))
}
cci_nflx_strat[is.na(cci_nflx_strat)] <- 1
cci_nflx_stratcomp <- cbind(cci_nflx, cci_nflx_ts, cci_nflx_strat)
colnames(cci_nflx_stratcomp) <- c('CCI','CCI SIGNAL','CCI POSITION')

Rate Of Change (ROC)

變化率(ROC)

The following code will create a Rate Of Change (ROC) trading strategy if satisfies our given conditions:

如果滿足我們的給定條件,以下代碼將創建一個變化率(ROC)交易策略:

# ROC# a. AAPL
roc_aapl_strat <- ifelse(roc_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {roc_aapl_strat[i] <- ifelse(roc_aapl_ts[i] == 1,1,ifelse(roc_aapl_ts[i] == -1,0,roc_aapl_strat[i-1]))
}
roc_aapl_strat[is.na(roc_aapl_strat)] <- 1
roc_aapl_stratcomp <- cbind(roc_aapl, roc_aapl_ts, roc_aapl_strat)
colnames(roc_aapl_stratcomp) <- c('ROC(25)','ROC SIGNAL','ROC POSITION')# b. TSLA
roc_tsla_strat <- ifelse(roc_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {roc_tsla_strat[i] <- ifelse(roc_tsla_ts[i] == 1,1,ifelse(roc_tsla_ts[i] == -1,0,roc_tsla_strat[i-1]))
}
roc_tsla_strat[is.na(roc_tsla_strat)] <- 1
roc_tsla_stratcomp <- cbind(roc_tsla, roc_tsla_ts, roc_tsla_strat)
colnames(roc_tsla_stratcomp) <- c('ROC(25)','ROC SIGNAL','ROC POSITION')# c. NFLX
roc_nflx_strat <- ifelse(roc_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {roc_nflx_strat[i] <- ifelse(roc_nflx_ts[i] == 1,1,ifelse(roc_nflx_ts[i] == -1,0,roc_nflx_strat[i-1]))
}
roc_nflx_strat[is.na(roc_nflx_strat)] <- 1
roc_nflx_stratcomp <- cbind(roc_nflx, roc_nflx_ts, roc_nflx_strat)
colnames(roc_nflx_stratcomp) <- c('ROC(25)','ROC SIGNAL','ROC POSITION')

Stochastic Momentum Index (SMI)

隨機動量指數(SMI)

The following code will create a SMI trading strategy if it satisfies our given conditions:

如果滿足我們的條件,以下代碼將創建一個SMI交易策略:

# SMI# a. AAPL
smi_aapl_strat <- ifelse(smi_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {smi_aapl_strat[i] <- ifelse(smi_aapl_ts[i] == 1,1,ifelse(smi_aapl_ts[i] == -1,0,smi_aapl_strat[i-1]))
}
smi_aapl_strat[is.na(smi_aapl_strat)] <- 1
smi_aapl_stratcomp <- cbind(smi_aapl[,1],smi_aapl[,2],smi_aapl_ts,smi_aapl_strat)
colnames(smi_aapl_stratcomp) <- c('SMI','SMI(S)','SMI SIGNAL','SMI POSITION')# b. TSLA
smi_tsla_strat <- ifelse(smi_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {smi_tsla_strat[i] <- ifelse(smi_tsla_ts[i] == 1,1,ifelse(smi_tsla_ts[i] == -1,0,smi_tsla_strat[i-1]))
}
smi_tsla_strat[is.na(smi_tsla_strat)] <- 1
smi_tsla_stratcomp <- cbind(smi_tsla[,1],smi_tsla[,2],smi_tsla_ts,smi_tsla_strat)
colnames(smi_tsla_stratcomp) <- c('SMI','SMI(S)','SMI SIGNAL','SMI POSITION')# c. NFLX
smi_nflx_strat <- ifelse(smi_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {smi_nflx_strat[i] <- ifelse(smi_nflx_ts[i] == 1,1,ifelse(smi_nflx_ts[i] == -1,0,smi_nflx_strat[i-1]))
}
smi_nflx_strat[is.na(smi_nflx_strat)] <- 1
smi_nflx_stratcomp <- cbind(smi_nflx[,1],smi_nflx[,2],smi_nflx_ts,smi_nflx_strat)
colnames(smi_nflx_stratcomp) <- c('SMI','SMI(S)','SMI SIGNAL','SMI POSITION')

Williams %R

威廉姆斯%R

The following code will create a Williams %R trading strategy if it satisfies our given conditions:

如果滿足我們給定的條件,以下代碼將創建Williams%R交易策略:

# WPR# a. AAPL
wpr_aapl_strat <- ifelse(wpr_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {wpr_aapl_strat[i] <- ifelse(wpr_aapl_ts[i] == 1,1,ifelse(wpr_aapl_ts[i] == -1,0,wpr_aapl_strat[i-1]))
}
wpr_aapl_strat[is.na(wpr_aapl_strat)] <- 1
wpr_aapl_stratcomp <- cbind(wpr_aapl, wpr_aapl_ts, wpr_aapl_strat)
colnames(wpr_aapl_stratcomp) <- c('WPR(14)','WPR SIGNAL','WPR POSITION')# b. TSLA
wpr_tsla_strat <- ifelse(wpr_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {wpr_tsla_strat[i] <- ifelse(wpr_tsla_ts[i] == 1,1,ifelse(wpr_tsla_ts[i] == -1,0,wpr_tsla_strat[i-1]))
}
wpr_tsla_strat[is.na(wpr_tsla_strat)] <- 1
wpr_tsla_stratcomp <- cbind(wpr_tsla, wpr_tsla_ts, wpr_tsla_strat)
colnames(wpr_tsla_stratcomp) <- c('WPR(14)','WPR SIGNAL','WPR POSITION')# c. NFLX
wpr_nflx_strat <- ifelse(wpr_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {wpr_nflx_strat[i] <- ifelse(wpr_nflx_ts[i] == 1,1,ifelse(wpr_nflx_ts[i] == -1,0,wpr_nflx_strat[i-1]))
}
wpr_nflx_strat[is.na(wpr_nflx_strat)] <- 1
wpr_nflx_stratcomp <- cbind(wpr_nflx, wpr_nflx_ts, wpr_nflx_strat)
colnames(wpr_nflx_stratcomp) <- c('WPR(14)','WPR SIGNAL','WPR POSITION')

步驟6:回測和比較結果 (Step-6 : Backtesting and Comparing the Results)

In this step we are going to conduct backtests on our created trading strategies vs our created trading strategies commission adjusted (0.5%) vs the companies’ benchmark returns. Before conducting our backtests, we have calculate our daily benchmark returns i.e., daily returns of Apple, Tesla and Netflix. Let’s do it!

此步驟中,我們將對我們創建的交易策略與調整后的交易策略傭金(0.5%)與公司的基準收益進行回測。 在進行回測之前,我們已經計算了每日基準回報,即Apple,Tesla和Netflix的每日回報。 我們開始做吧!

# Calculating Returns & setting Benchmark for companiesret_aapl <- diff(log(Cl(AAPL)))
ret_tsla <- diff(log(Cl(TSLA)))
ret_nflx <- diff(log(Cl(NFLX)))benchmark_aapl <- ret_aapl
benchmark_tsla <- ret_tsla
benchmark_nflx <- ret_nflx

Now, we are set to conduct our backtests.

現在,我們準備進行回測。

Simple Moving Average (SMA)

簡單移動平均線(SMA)

The following code will first calculate SMA strategy daily returns, commission adjusted SMA daily returns and finally runs the backtest (Comparison chart and an Annualized returns table) :

以下代碼將首先計算SMA策略的每日收益,傭金調整后的SMA日收益,最后運行回溯測試(比較圖和年化收益表):

# SMA # 1. AAPL
sma_aapl_ret <- ret_aapl*sma_aapl_strat
sma_aapl_ret_commission_adj <- ifelse((sma_aapl_ts == 1|sma_aapl_ts == -1) & sma_aapl_strat != Lag(sma_aapl_ts), (ret_aapl-0.05)*sma_aapl_strat, ret_aapl*sma_aapl_strat)
sma_aapl_comp <- cbind(sma_aapl_ret, sma_aapl_ret_commission_adj, benchmark_aapl)
colnames(sma_aapl_comp) <- c('SMA','SMA Commission Adj','Apple Benchmark')
charts.PerformanceSummary(sma_aapl_comp, main = 'Apple SMA Performance')
sma_aapl_comp_table <- table.AnnualizedReturns(sma_aapl_comp)# 2. TSLA
sma_tsla_ret <- ret_tsla*sma_tsla_strat
sma_tsla_ret_commission_adj <- ifelse((sma_tsla_ts == 1|sma_tsla_ts == -1) & sma_tsla_strat != Lag(sma_tsla_ts), (ret_tsla-0.05)*sma_tsla_strat, ret_tsla*sma_tsla_strat)
sma_tsla_comp <- cbind(sma_tsla_ret, sma_tsla_ret_commission_adj, benchmark_tsla)
colnames(sma_tsla_comp) <- c('SMA','SMA Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(sma_tsla_comp, main = 'Tesla SMA Performance')
sma_tsla_comp_table <- table.AnnualizedReturns(sma_tsla_comp)# 3. NFLX
sma_nflx_ret <- ret_nflx*sma_nflx_strat
sma_nflx_ret_commission_adj <- ifelse((sma_nflx_ts == 1|sma_nflx_ts == -1) & sma_nflx_strat != Lag(sma_nflx_ts), (ret_nflx-0.05)*sma_nflx_strat, ret_nflx*sma_nflx_strat)
sma_nflx_comp <- cbind(sma_nflx_ret, sma_nflx_ret_commission_adj, benchmark_nflx)
colnames(sma_nflx_comp) <- c('SMA','SMA Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(sma_nflx_comp, main = 'Netflix SMA Performance')
sma_nflx_comp_table <- table.AnnualizedReturns(sma_nflx_comp)
Image for post
Image for post
Image for post
Images by Author
圖片作者

Parabolic SAR

拋物線SAR

The following code will first calculate Parabolic SAR strategy daily returns, commission adjusted Parabolic SAR daily returns and finally runs the backtest (Comparison chart and an Annualized returns table) :

以下代碼將首先計算拋物線SAR策略的每日收益,傭金調整后的拋物線SAR每日收益,最后運行回測(比較圖和年度收益表):

# Parabolic SAR # 1. AAPL
sar_aapl_ret <- ret_aapl*sar_aapl_strat
sar_aapl_ret_commission_adj <- ifelse((sar_aapl_ts == 1|sar_aapl_ts == -1) & sar_aapl_strat != Lag(sar_aapl_ts), (ret_aapl-0.05)*sar_aapl_strat, ret_aapl*sar_aapl_strat)
sar_aapl_comp <- cbind(sar_aapl_ret, sar_aapl_ret_commission_adj, benchmark_aapl)
colnames(sar_aapl_comp) <- c('SAR','SAR Commission Adj','Apple Benchmark')
charts.PerformanceSummary(sar_aapl_comp, main = 'Apple Parabolic SAR Performance')
sar_aapl_comp_table <- table.AnnualizedReturns(sar_aapl_comp)# 2. TSLA
sar_tsla_ret <- ret_tsla*sar_tsla_strat
sar_tsla_ret_commission_adj <- ifelse((sar_tsla_ts == 1|sar_tsla_ts == -1) & sar_tsla_strat != Lag(sar_tsla_ts), (ret_tsla-0.05)*sar_tsla_strat, ret_tsla*sar_tsla_strat)
sar_tsla_comp <- cbind(sar_tsla_ret, sar_tsla_ret_commission_adj, benchmark_tsla)
colnames(sar_tsla_comp) <- c('SAR','SAR Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(sar_tsla_comp, main = 'Tesla Parabolic SAR Performance')
sar_tsla_comp_table <- table.AnnualizedReturns(sar_tsla_comp)# 3. NFLX
sar_nflx_ret <- ret_nflx*sar_nflx_strat
sar_nflx_ret_commission_adj <- ifelse((sar_nflx_ts == 1|sar_nflx_ts == -1) & sar_nflx_strat != Lag(sar_nflx_ts), (ret_nflx-0.05)*sar_nflx_strat, ret_nflx*sar_nflx_strat)
sar_nflx_comp <- cbind(sar_nflx_ret, sar_nflx_ret_commission_adj, benchmark_nflx)
colnames(sar_nflx_comp) <- c('SAR','SAR Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(sar_nflx_comp, main = 'Netflix Parabolic SAR Performance')
sar_nflx_comp_table <- table.AnnualizedReturns(sar_nflx_comp)
Image for post
Image for post
Image for post
Images by Author
圖片作者

Commodity Channel Index (CCI)

商品渠道指數(CCI)

The following code will first calculate CCI strategy daily returns, commission adjusted CCI daily returns and finally runs the backtest (Comparison chart and an Annualized returns table) :

以下代碼將首先計算CCI戰略每日收益,傭金調整后的CCI日收益,最后運行回溯測試(比較圖和年化收益表):

# CCI  # 1. AAPL
cci_aapl_ret <- ret_aapl*cci_aapl_strat
cci_aapl_ret_commission_adj <- ifelse((cci_aapl_ts == 1|cci_aapl_ts == -1) & cci_aapl_strat != Lag(cci_aapl_ts), (ret_aapl-0.05)*cci_aapl_strat, ret_aapl*cci_aapl_strat)
cci_aapl_comp <- cbind(cci_aapl_ret, cci_aapl_ret_commission_adj, benchmark_aapl)
colnames(cci_aapl_comp) <- c('CCI','CCI Commission Adj','Apple Benchmark')
charts.PerformanceSummary(cci_aapl_comp, main = 'Apple CCI Performance')
cci_aapl_comp_table <- table.AnnualizedReturns(cci_aapl_comp)# 2. TSLA
cci_tsla_ret <- ret_tsla*cci_tsla_strat
cci_tsla_ret_commission_adj <- ifelse((cci_tsla_ts == 1|cci_tsla_ts == -1) & cci_tsla_strat != Lag(cci_tsla_ts), (ret_tsla-0.05)*cci_tsla_strat, ret_tsla*cci_tsla_strat)
cci_tsla_comp <- cbind(cci_tsla_ret, cci_tsla_ret_commission_adj, benchmark_tsla)
colnames(cci_tsla_comp) <- c('CCI','CCI Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(cci_tsla_comp, main = 'Tesla CCI Performance')
cci_tsla_comp_table <- table.AnnualizedReturns(cci_tsla_comp)# 3. NFLX
cci_nflx_ret <- ret_nflx*cci_nflx_strat
cci_nflx_ret_commission_adj <- ifelse((cci_nflx_ts == 1|cci_nflx_ts == -1) & cci_nflx_strat != Lag(cci_nflx_ts), (ret_nflx-0.05)*cci_nflx_strat, ret_nflx*cci_nflx_strat)
cci_nflx_comp <- cbind(cci_nflx_ret, cci_nflx_ret_commission_adj, benchmark_nflx)
colnames(cci_nflx_comp) <- c('CCI','CCI Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(cci_nflx_comp, main = 'Netflix CCI Performance')
cci_nflx_comp_table <- table.AnnualizedReturns(cci_nflx_comp)
Image for post
Image for post
Image for post
Images by Author
圖片作者

Rate Of Change (ROC)

變化率(ROC)

The following code will first calculate ROC strategy daily returns, commission adjusted ROC daily returns and finally runs the backtest (Comparison chart and an Annualized returns table) :

以下代碼將首先計算ROC策略的每日收益,傭金調整后的ROC每日收益,最后運行回溯測試(比較圖和年化收益表):

# ROC  # 1. AAPL
roc_aapl_ret <- ret_aapl*roc_aapl_strat
roc_aapl_ret_commission_adj <- ifelse((roc_aapl_ts == 1|roc_aapl_ts == -1) & roc_aapl_strat != Lag(roc_aapl_ts), (ret_aapl-0.05)*roc_aapl_strat, ret_aapl*roc_aapl_strat)
roc_aapl_comp <- cbind(roc_aapl_ret, roc_aapl_ret_commission_adj, benchmark_aapl)
colnames(roc_aapl_comp) <- c('ROC','ROC Commission Adj','Apple Benchmark')
charts.PerformanceSummary(roc_aapl_comp, main = 'Apple ROC Performance')
roc_aapl_comp_table <- table.AnnualizedReturns(roc_aapl_comp)# 2. TSLA
roc_tsla_ret <- ret_tsla*roc_tsla_strat
roc_tsla_ret_commission_adj <- ifelse((roc_tsla_ts == 1|roc_tsla_ts == -1) & roc_tsla_strat != Lag(roc_tsla_ts), (ret_tsla-0.05)*roc_tsla_strat, ret_tsla*roc_tsla_strat)
roc_tsla_comp <- cbind(roc_tsla_ret, roc_tsla_ret_commission_adj, benchmark_tsla)
colnames(roc_tsla_comp) <- c('ROC','ROC Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(roc_tsla_comp, main = 'Tesla ROC Performance')
roc_tsla_comp_table <- table.AnnualizedReturns(roc_tsla_comp)# 3. NFLX
roc_nflx_ret <- ret_nflx*roc_nflx_strat
roc_nflx_ret_commission_adj <- ifelse((roc_nflx_ts == 1|roc_nflx_ts == -1) & roc_nflx_strat != Lag(roc_nflx_ts), (ret_nflx-0.05)*roc_nflx_strat, ret_nflx*roc_nflx_strat)
roc_nflx_comp <- cbind(roc_nflx_ret, roc_nflx_ret_commission_adj, benchmark_nflx)
colnames(roc_nflx_comp) <- c('ROC','ROC Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(roc_nflx_comp, main = 'Netflix ROC Performance')
roc_nflx_comp_table <- table.AnnualizedReturns(roc_nflx_comp)
Image for post
Image for post
Image for post
Images by Author
圖片作者

Stochastic Momentum Index (SMI)

隨機動量指數(SMI)

The following code will first calculate SMI strategy daily returns, commission adjusted SMI daily returns and finally runs the backtest (Comparison chart and an Annualized returns table) :

以下代碼將首先計算SMI策略的每日收益,傭金調整后的SMI每日收益,最后運行回溯測試(比較圖和年化收益表):

# SMI  # 1. AAPL
smi_aapl_ret <- ret_aapl*smi_aapl_strat
smi_aapl_ret_commission_adj <- ifelse((smi_aapl_ts == 1|smi_aapl_ts == -1) & smi_aapl_strat != Lag(smi_aapl_ts), (ret_aapl-0.05)*smi_aapl_strat, ret_aapl*smi_aapl_strat)
smi_aapl_comp <- cbind(smi_aapl_ret, smi_aapl_ret_commission_adj, benchmark_aapl)
colnames(smi_aapl_comp) <- c('SMI','SMI Commission Adj','Apple Benchmark')
charts.PerformanceSummary(smi_aapl_comp, main = 'Apple SMI Performance')
smi_aapl_comp_table <- table.AnnualizedReturns(smi_aapl_comp)# 2. TSLA
smi_tsla_ret <- ret_tsla*smi_tsla_strat
smi_tsla_ret_commission_adj <- ifelse((smi_tsla_ts == 1|smi_tsla_ts == -1) & smi_tsla_strat != Lag(smi_tsla_ts), (ret_tsla-0.05)*smi_tsla_strat, ret_tsla*smi_tsla_strat)
smi_tsla_comp <- cbind(smi_tsla_ret, smi_tsla_ret_commission_adj, benchmark_tsla)
colnames(smi_tsla_comp) <- c('SMI','SMI Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(smi_tsla_comp, main = 'Tesla SMI Performance')
smi_tsla_comp_table <- table.AnnualizedReturns(smi_tsla_comp)# 3. NFLX
smi_nflx_ret <- ret_nflx*smi_nflx_strat
smi_nflx_ret_commission_adj <- ifelse((smi_nflx_ts == 1|smi_nflx_ts == -1) & smi_nflx_strat != Lag(smi_nflx_ts), (ret_nflx-0.05)*smi_nflx_strat, ret_nflx*smi_nflx_strat)
smi_nflx_comp <- cbind(smi_nflx_ret, smi_nflx_ret_commission_adj, benchmark_nflx)
colnames(smi_nflx_comp) <- c('SMI','SMI Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(smi_nflx_comp, main = 'Netflix SMI Performance')
smi_nflx_comp_table <- table.AnnualizedReturns(smi_nflx_comp)
Image for post
Image for post
Image for post
Images by Author
圖片作者

Williams %R

威廉姆斯%R

The following code will first calculate Williams %R strategy daily returns, commission adjusted Williams %R daily returns and finally runs the backtest (Comparison chart and an Annualized returns table) :

以下代碼將首先計算Williams%R策略的每日收益,傭金調整后的Williams%R日收益,最后運行回溯測試(比較圖和年化收益表):

# WPR  # 1. AAPL
wpr_aapl_ret <- ret_aapl*wpr_aapl_strat
wpr_aapl_ret_commission_adj <- ifelse((wpr_aapl_ts == 1|wpr_aapl_ts == -1) & wpr_aapl_strat != Lag(wpr_aapl_ts), (ret_aapl-0.05)*wpr_aapl_strat, ret_aapl*wpr_aapl_strat)
wpr_aapl_comp <- cbind(wpr_aapl_ret, wpr_aapl_ret_commission_adj, benchmark_aapl)
colnames(wpr_aapl_comp) <- c('WPR','WPR Commission Adj','Apple Benchmark')
charts.PerformanceSummary(wpr_aapl_comp, main = 'Apple WPR Performance')
wpr_aapl_comp_table <- table.AnnualizedReturns(wpr_aapl_comp)# 2. TSLA
wpr_tsla_ret <- ret_tsla*wpr_tsla_strat
wpr_tsla_ret_commission_adj <- ifelse((wpr_tsla_ts == 1|wpr_tsla_ts == -1) & wpr_tsla_strat != Lag(wpr_tsla_ts), (ret_tsla-0.05)*wpr_tsla_strat, ret_tsla*wpr_tsla_strat)
wpr_tsla_comp <- cbind(wpr_tsla_ret, wpr_tsla_ret_commission_adj, benchmark_tsla)
colnames(wpr_tsla_comp) <- c('WPR','WPR Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(wpr_tsla_comp, main = 'Tesla WPR Performance')
wpr_tsla_comp_table <- table.AnnualizedReturns(wpr_tsla_comp)# 3. NFLX
wpr_nflx_ret <- ret_nflx*wpr_nflx_strat
wpr_nflx_ret_commission_adj <- ifelse((wpr_nflx_ts == 1|wpr_nflx_ts == -1) & wpr_nflx_strat != Lag(wpr_nflx_ts), (ret_nflx-0.05)*wpr_nflx_strat, ret_nflx*wpr_nflx_strat)
wpr_nflx_comp <- cbind(wpr_nflx_ret, wpr_nflx_ret_commission_adj, benchmark_nflx)
colnames(wpr_nflx_comp) <- c('WPR','WPR Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(wpr_nflx_comp, main = 'Netflix WPR Performance')
wpr_nflx_comp_table <- table.AnnualizedReturns(wpr_nflx_comp)
Image for post
Image for post
Image for post
Images by Author
圖片作者

最后的想法! (Final Thoughts!)

Hurrah! We successfully finished our process of creating trading strategies and backtesting the results with R. I want to highlight that, every strategies and backtesting results are only for educational purpose and should not be taken as an investment advice. Apart from our created strategies, you can do your coding and frame your own trading strategies based on our needs and flexibility. If you’ve missed any of the coding sections, no worries. I’ve added the full code file at the end. In this article, we’ve covered only six major technical indicators but, there are more to discover. So, never stop learning and never stop coding!

歡呼! 我們成功地完成了創建交易策略并使用R對結果進行回測的過程。我想強調一點,每一種策略和回測結果僅用于教育目的,不應被視為投資建議。 除了我們創建的策略之外,您還可以根據我們的需求和靈活性進行編碼并制定自己的交易策略。 如果您錯過了任何編碼部分,請不要擔心。 我在末尾添加了完整的代碼文件。 在本文中,我們僅介紹了六個主要技術指標,但還有更多發現之處。 因此,永遠不要停止學習,永遠不要停止編碼!

Happy Coding and Analyzing!

快樂的編碼和分析!

Full Code :

完整代碼:

library(quantmod)
library(TTR)
library(PerformanceAnalytics)# Getting stock prices of AAPL, TSLA and NFLX
getSymbols('AAPL', src = 'yahoo', from = '2019-01-01')
getSymbols('TSLA', src = 'yahoo', from = '2019-01-01')
getSymbols('NFLX', src = 'yahoo', from = '2019-01-01')# Basic plot of the three stocks
barChart(AAPL, theme = chartTheme('black'))
barChart(TSLA, theme = chartTheme('black'))
barChart(NFLX, theme = chartTheme('black'))# Creating Leading and Lagging Technical Indicators# a. Simple Moving Average (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)# b.  Parabolic Stop And Reverse (SAR)# 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')# c. Commodity Channel Index (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)# d. Rate of Change (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)# e. Stochastic Momentum Index (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)# f. 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 = 14)
colnames(wpr_tsla) <- 'wpr'
barChart(TSLA, theme = 'black')
addWPR(n = 14)
# 1. NFLX
wpr_nflx <- WPR(HLC(NFLX), n = 14)
colnames(wpr_nflx) <- 'wpr'
barChart(NFLX, theme = 'black')
addWPR(n = 14)# Creating Trading signal with Indicators# SMA # a. AAPL
# SMA 20 Crossover Signal 
sma20_aapl_ts <- Lag(ifelse(Lag(Cl(AAPL)) < Lag(sma20_aapl) & Cl(AAPL) > sma20_aapl,1,ifelse(Lag(Cl(AAPL)) > Lag(sma20_aapl) & Cl(AAPL) < sma20_aapl,-1,0)))
sma20_aapl_ts[is.na(sma20_aapl_ts)] <- 0
# SMA 50 Crossover Signal
sma50_aapl_ts <- Lag(ifelse(Lag(Cl(AAPL)) < Lag(sma50_aapl) & Cl(AAPL) > sma50_aapl,1,ifelse(Lag(Cl(AAPL)) > Lag(sma50_aapl) & Cl(AAPL) < sma50_aapl,-1,0)))
sma50_aapl_ts[is.na(sma50_aapl_ts)] <- 0
# SMA 20 and SMA 50 Crossover Signal
sma_aapl_ts <- Lag(ifelse(Lag(sma20_aapl) < Lag(sma50_aapl) & sma20_aapl > sma50_aapl,1,ifelse(Lag(sma20_aapl) > Lag(sma50_aapl) & sma20_aapl < sma50_aapl,-1,0)))
sma_aapl_ts[is.na(sma_aapl_ts)] <- 0# b. TSLA
# SMA 20 Crossover Signal 
sma20_tsla_ts <- Lag(ifelse(Lag(Cl(TSLA)) < Lag(sma20_tsla) & Cl(TSLA) > sma20_tsla,1,ifelse(Lag(Cl(TSLA)) > Lag(sma20_tsla) & Cl(TSLA) < sma20_tsla,-1,0)))
sma20_tsla_ts[is.na(sma20_tsla_ts)] <- 0
# SMA 50 Crossover Signal
sma50_tsla_ts <- Lag(ifelse(Lag(Cl(TSLA)) < Lag(sma50_tsla) & Cl(TSLA) > sma50_tsla,1,ifelse(Lag(Cl(TSLA)) > Lag(sma50_tsla) & Cl(TSLA) < sma50_tsla,-1,0)))
sma50_tsla_ts[is.na(sma50_tsla_ts)] <- 0
# SMA 20 and SMA 50 Crossover Signal
sma_tsla_ts <- Lag(ifelse(Lag(sma20_tsla) < Lag(sma50_tsla) & sma20_tsla > sma50_tsla,1,ifelse(Lag(sma20_tsla) > Lag(sma50_tsla) & sma20_tsla < sma50_tsla,-1,0)))
sma_tsla_ts[is.na(sma_tsla_ts)] <- 0# c. NFLX
# SMA 20 Crossover Signal 
sma20_nflx_ts <- Lag(ifelse(Lag(Cl(NFLX)) < Lag(sma20_nflx) & Cl(NFLX) > sma20_nflx,1,ifelse(Lag(Cl(NFLX)) > Lag(sma20_nflx) & Cl(NFLX) < sma20_nflx,-1,0)))
sma20_nflx_ts[is.na(sma20_nflx_ts)] <- 0
# SMA 50 Crossover Signal
sma50_nflx_ts <- Lag(ifelse(Lag(Cl(NFLX)) < Lag(sma50_nflx) & Cl(NFLX) > sma50_nflx,1,ifelse(Lag(Cl(NFLX)) > Lag(sma50_nflx) & Cl(NFLX) < sma50_nflx,-1,0)))
sma50_nflx_ts[is.na(sma50_nflx_ts)] <- 0
# SMA 20 and SMA 50 Crossover Signal
sma_nflx_ts <- Lag(ifelse(Lag(sma20_nflx) < Lag(sma50_nflx) & sma20_nflx > sma50_nflx,1,ifelse(Lag(sma20_nflx) > Lag(sma50_nflx) & sma20_nflx < sma50_nflx,-1,0)))
sma_nflx_ts[is.na(sma_nflx_ts)] <- 0# 2. Parabolic Stop And Reverse (SAR) # a. AAPL
sar_aapl_ts <- Lag(ifelse(Lag(Cl(AAPL)) < Lag(sar_aapl) & Cl(AAPL) > sar_aapl,1,ifelse(Lag(Cl(AAPL)) > Lag(sar_aapl) & Cl(AAPL) < sar_aapl,-1,0)))
sar_aapl_ts[is.na(sar_aapl_ts)] <- 0
# b. TSLA
sar_tsla_ts <- Lag(ifelse(Lag(Cl(TSLA)) < Lag(sar_tsla) & Cl(TSLA) > sar_tsla,1,ifelse(Lag(Cl(TSLA)) > Lag(sar_tsla) & Cl(TSLA) < sar_tsla,-1,0)))
sar_tsla_ts[is.na(sar_tsla_ts)] <- 0
# c. NFLX
sar_nflx_ts <- Lag(ifelse(Lag(Cl(NFLX)) < Lag(sar_nflx) & Cl(NFLX) > sar_nflx,1,ifelse(Lag(Cl(NFLX)) > Lag(sar_nflx) & Cl(NFLX) < sar_nflx,-1,0)))
sar_nflx_ts[is.na(sar_nflx_ts)] <- 0# 3. Commodity Channel Index  (CCI)# a. AAPL
cci_aapl_ts <- Lag(ifelse(Lag(cci_aapl) < (-100) & cci_aapl > (-100),1,ifelse(Lag(cci_aapl) < (100) & cci_aapl > (100),-1,0)))
cci_aapl_ts[is.na(cci_aapl_ts)] <- 0
# b. TSLA
cci_tsla_ts <- Lag(ifelse(Lag(cci_tsla) < (-100) & cci_tsla > (-100),1,ifelse(Lag(cci_tsla) < (100) & cci_tsla > (100),-1,0)))
cci_tsla_ts[is.na(cci_tsla_ts)] <- 0
# c. NFLX
cci_nflx_ts <- Lag(ifelse(Lag(cci_nflx) < (-100) & cci_nflx > (-100),1,ifelse(Lag(cci_nflx) < (100) & cci_nflx > (100),-1,0)))
cci_nflx_ts[is.na(cci_nflx_ts)] <- 0# 4. Rate of Change (ROC)# a. AAPL
roc_aapl_ts <- Lag(ifelse(Lag(roc_aapl) < (-0.05) & roc_aapl > (-0.05),1,ifelse(Lag(roc_aapl) < (0.05) & roc_aapl > (0.05),-1,0)))
roc_aapl_ts[is.na(roc_aapl_ts)] <- 0
# b. TSLA
roc_tsla_ts <- Lag(ifelse(Lag(roc_tsla) < (-0.05) & roc_tsla > (-0.05),1,ifelse(Lag(roc_tsla) < (0.05) & roc_tsla > (0.05),-1,0)))
roc_tsla_ts[is.na(roc_tsla_ts)] <- 0
# c. NFLX
roc_nflx_ts <- Lag(ifelse(Lag(roc_nflx) < (-0.05) & roc_nflx > (-0.05),1,ifelse(Lag(roc_nflx) < (0.05) & roc_nflx > (0.05),-1,0)))
roc_nflx_ts[is.na(roc_nflx_ts)] <- 0# 5. Stochastic Momentum Index (SMI)# a. AAPL
smi_aapl_ts <- Lag(ifelse(Lag(smi_aapl[,1]) < Lag(smi_aapl[,2]) & smi_aapl[,1] > smi_aapl[,2],1, ifelse(Lag(smi_aapl[,1]) > Lag(smi_aapl[,2]) & smi_aapl[,1] < smi_aapl[,2],-1,0)))
smi_aapl_ts[is.na(smi_aapl_ts)] <- 0
# b. TSLA
smi_tsla_ts <- Lag(ifelse(Lag(smi_tsla[,1]) < Lag(smi_tsla[,2]) & smi_tsla[,1] > smi_tsla[,2],1, ifelse(Lag(smi_tsla[,1]) > Lag(smi_tsla[,2]) & smi_tsla[,1] < smi_tsla[,2],-1,0)))
smi_tsla_ts[is.na(smi_tsla_ts)] <- 0
# a. NFLX
smi_nflx_ts <- Lag(ifelse(Lag(smi_nflx[,1]) < Lag(smi_nflx[,2]) & smi_nflx[,1] > smi_nflx[,2],1, ifelse(Lag(smi_nflx[,1]) > Lag(smi_nflx[,2]) & smi_nflx[,1] < smi_nflx[,2],-1,0)))
smi_nflx_ts[is.na(smi_nflx_ts)] <- 0# 6. williams %R# a. AAPL
wpr_aapl_ts <- Lag(ifelse(Lag(wpr_aapl) > 0.8 & wpr_aapl < 0.8,1,ifelse(Lag(wpr_aapl) > 0.2 & wpr_aapl < 0.2,-1,0)))
wpr_aapl_ts[is.na(wpr_aapl_ts)] <- 0
# b. TSLA
wpr_tsla_ts <- Lag(ifelse(Lag(wpr_tsla) > 0.8 & wpr_tsla < 0.8,1,ifelse(Lag(wpr_tsla) > 0.2 & wpr_tsla < 0.2,-1,0)))
wpr_tsla_ts[is.na(wpr_tsla_ts)] <- 0
# c. NFLX
wpr_nflx_ts <- Lag(ifelse(Lag(wpr_nflx) > 0.8 & wpr_nflx < 0.8,1,ifelse(Lag(wpr_nflx) > 0.2 & wpr_nflx < 0.2,-1,0)))
wpr_nflx_ts[is.na(wpr_nflx_ts)] <- 0# Creating Trading Strategies using Signals# 1. SMA 20 and SMA 50 Crossover Strategy# a. AAPL
sma_aapl_strat <- ifelse(sma_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {sma_aapl_strat[i] <- ifelse(sma_aapl_ts[i] == 1,1,ifelse(sma_aapl_ts[i] == -1,0,sma_aapl_strat[i-1]))
}
sma_aapl_strat[is.na(sma_aapl_strat)] <- 1
sma_aapl_stratcomp <- cbind(sma20_aapl, sma50_aapl, sma_aapl_ts, sma_aapl_strat)
colnames(sma_aapl_stratcomp) <- c('SMA(20)','SMA(50)','SMA SIGNAL','SMA POSITION')
# b. TSLA
sma_tsla_strat <- ifelse(sma_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {sma_tsla_strat[i] <- ifelse(sma_tsla_ts[i] == 1,1,ifelse(sma_tsla_ts[i] == -1,0,sma_tsla_strat[i-1]))
}
sma_tsla_strat[is.na(sma_tsla_strat)] <- 1
sma_tsla_stratcomp <- cbind(sma20_tsla, sma50_tsla, sma_tsla_ts, sma_tsla_strat)
colnames(sma_tsla_stratcomp) <- c('SMA(20)','SMA(50)','SMA SIGNAL','SMA POSITION')
# c. NFLX
sma_nflx_strat <- ifelse(sma_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {sma_nflx_strat[i] <- ifelse(sma_nflx_ts[i] == 1,1,ifelse(sma_nflx_ts[i] == 'SEL',0,sma_nflx_strat[i-1]))
}
sma_nflx_strat[is.na(sma_nflx_strat)] <- 1
sma_nflx_stratcomp <- cbind(sma20_nflx, sma50_nflx, sma_nflx_ts, sma_nflx_strat)
colnames(sma_nflx_stratcomp) <- c('SMA(20)','SMA(50)','SMA SIGNAL','SMA POSITION')# Parabolic SAR Strategy # a. AAPL
sar_aapl_strat <- ifelse(sar_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {sar_aapl_strat[i] <- ifelse(sar_aapl_ts[i] == 1,1,ifelse(sar_aapl_ts[i] == -1,0,sar_aapl_strat[i-1]))
}
sar_aapl_strat[is.na(sar_aapl_strat)] <- 1
sar_aapl_stratcomp <- cbind(Cl(AAPL), sar_aapl, sar_aapl_ts, sar_aapl_strat)
colnames(sar_aapl_stratcomp) <- c('Close','SAR','SAR SIGNAL','SAR POSITION')
# b. TSLA
sar_tsla_strat <- ifelse(sar_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {sar_tsla_strat[i] <- ifelse(sar_tsla_ts[i] == 1,1,ifelse(sar_tsla_ts[i] == -1,0,sar_tsla_strat[i-1]))
}
sar_tsla_strat[is.na(sar_tsla_strat)] <- 1
sar_tsla_stratcomp <- cbind(Cl(TSLA), sar_tsla, sar_tsla_ts, sar_tsla_strat)
colnames(sar_tsla_stratcomp) <- c('Close','SAR','SAR SIGNAL','SAR POSITION')
# c. NFLX
sar_nflx_strat <- ifelse(sar_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {sar_nflx_strat[i] <- ifelse(sar_nflx_ts[i] == 1,1,ifelse(sar_nflx_ts[i] == -1,0,sar_nflx_strat[i-1]))
}
sar_nflx_strat[is.na(sar_nflx_strat)] <- 1
sar_nflx_stratcomp <- cbind(Cl(NFLX), sar_nflx, sar_nflx_ts, sar_nflx_strat)
colnames(sar_nflx_stratcomp) <- c('Close','SAR','SAR SIGNAL','SAR POSITION')# CCI# a. AAPL
cci_aapl_strat <- ifelse(cci_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {cci_aapl_strat[i] <- ifelse(cci_aapl_ts[i] == 1,1,ifelse(cci_aapl_ts[i] == -1,0,cci_aapl_strat[i-1]))
}
cci_aapl_strat[is.na(cci_aapl_strat)] <- 1
cci_aapl_stratcomp <- cbind(cci_aapl, cci_aapl_ts, cci_aapl_strat)
colnames(cci_aapl_stratcomp) <- c('CCI','CCI SIGNAL','CCI POSITION')
# b. TSLA
cci_tsla_strat <- ifelse(cci_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {cci_tsla_strat[i] <- ifelse(cci_tsla_ts[i] == 1,1,ifelse(cci_tsla_ts[i] == -1,0,cci_tsla_strat[i-1]))
}
cci_tsla_strat[is.na(cci_tsla_strat)] <- 1
cci_tsla_stratcomp <- cbind(cci_tsla, cci_tsla_ts, cci_tsla_strat)
colnames(cci_tsla_stratcomp) <- c('CCI','CCI SIGNAL','CCI POSITION')
# c. NFLX
cci_nflx_strat <- ifelse(cci_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {cci_nflx_strat[i] <- ifelse(cci_nflx_ts[i] == 1,1,ifelse(cci_nflx_ts[i] == -1,0,cci_nflx_strat[i-1]))
}
cci_nflx_strat[is.na(cci_nflx_strat)] <- 1
cci_nflx_stratcomp <- cbind(cci_nflx, cci_nflx_ts, cci_nflx_strat)
colnames(cci_nflx_stratcomp) <- c('CCI','CCI SIGNAL','CCI POSITION')# ROC# a. AAPL
roc_aapl_strat <- ifelse(roc_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {roc_aapl_strat[i] <- ifelse(roc_aapl_ts[i] == 1,1,ifelse(roc_aapl_ts[i] == -1,0,roc_aapl_strat[i-1]))
}
roc_aapl_strat[is.na(roc_aapl_strat)] <- 1
roc_aapl_stratcomp <- cbind(roc_aapl, roc_aapl_ts, roc_aapl_strat)
colnames(roc_aapl_stratcomp) <- c('ROC(25)','ROC SIGNAL','ROC POSITION')
# b. TSLA
roc_tsla_strat <- ifelse(roc_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {roc_tsla_strat[i] <- ifelse(roc_tsla_ts[i] == 1,1,ifelse(roc_tsla_ts[i] == -1,0,roc_tsla_strat[i-1]))
}
roc_tsla_strat[is.na(roc_tsla_strat)] <- 1
roc_tsla_stratcomp <- cbind(roc_tsla, roc_tsla_ts, roc_tsla_strat)
colnames(roc_tsla_stratcomp) <- c('ROC(25)','ROC SIGNAL','ROC POSITION')
# c. NFLX
roc_nflx_strat <- ifelse(roc_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {roc_nflx_strat[i] <- ifelse(roc_nflx_ts[i] == 1,1,ifelse(roc_nflx_ts[i] == -1,0,roc_nflx_strat[i-1]))
}
roc_nflx_strat[is.na(roc_nflx_strat)] <- 1
roc_nflx_stratcomp <- cbind(roc_nflx, roc_nflx_ts, roc_nflx_strat)
colnames(roc_nflx_stratcomp) <- c('ROC(25)','ROC SIGNAL','ROC POSITION')# SMI# a. AAPL
smi_aapl_strat <- ifelse(smi_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {smi_aapl_strat[i] <- ifelse(smi_aapl_ts[i] == 1,1,ifelse(smi_aapl_ts[i] == -1,0,smi_aapl_strat[i-1]))
}
smi_aapl_strat[is.na(smi_aapl_strat)] <- 1
smi_aapl_stratcomp <- cbind(smi_aapl[,1],smi_aapl[,2],smi_aapl_ts,smi_aapl_strat)
colnames(smi_aapl_stratcomp) <- c('SMI','SMI(S)','SMI SIGNAL','SMI POSITION')
# b. TSLA
smi_tsla_strat <- ifelse(smi_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {smi_tsla_strat[i] <- ifelse(smi_tsla_ts[i] == 1,1,ifelse(smi_tsla_ts[i] == -1,0,smi_tsla_strat[i-1]))
}
smi_tsla_strat[is.na(smi_tsla_strat)] <- 1
smi_tsla_stratcomp <- cbind(smi_tsla[,1],smi_tsla[,2],smi_tsla_ts,smi_tsla_strat)
colnames(smi_tsla_stratcomp) <- c('SMI','SMI(S)','SMI SIGNAL','SMI POSITION')
# c. NFLX
smi_nflx_strat <- ifelse(smi_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {smi_nflx_strat[i] <- ifelse(smi_nflx_ts[i] == 1,1,ifelse(smi_nflx_ts[i] == -1,0,smi_nflx_strat[i-1]))
}
smi_nflx_strat[is.na(smi_nflx_strat)] <- 1
smi_nflx_stratcomp <- cbind(smi_nflx[,1],smi_nflx[,2],smi_nflx_ts,smi_nflx_strat)
colnames(smi_nflx_stratcomp) <- c('SMI','SMI(S)','SMI SIGNAL','SMI POSITION')# WPR# a. AAPL
wpr_aapl_strat <- ifelse(wpr_aapl_ts > 1,0,1)
for (i in 1 : length(Cl(AAPL))) {wpr_aapl_strat[i] <- ifelse(wpr_aapl_ts[i] == 1,1,ifelse(wpr_aapl_ts[i] == -1,0,wpr_aapl_strat[i-1]))
}
wpr_aapl_strat[is.na(wpr_aapl_strat)] <- 1
wpr_aapl_stratcomp <- cbind(wpr_aapl, wpr_aapl_ts, wpr_aapl_strat)
colnames(wpr_aapl_stratcomp) <- c('WPR(14)','WPR SIGNAL','WPR POSITION')
# b. TSLA
wpr_tsla_strat <- ifelse(wpr_tsla_ts > 1,0,1)
for (i in 1 : length(Cl(TSLA))) {wpr_tsla_strat[i] <- ifelse(wpr_tsla_ts[i] == 1,1,ifelse(wpr_tsla_ts[i] == -1,0,wpr_tsla_strat[i-1]))
}
wpr_tsla_strat[is.na(wpr_tsla_strat)] <- 1
wpr_tsla_stratcomp <- cbind(wpr_tsla, wpr_tsla_ts, wpr_tsla_strat)
colnames(wpr_tsla_stratcomp) <- c('WPR(14)','WPR SIGNAL','WPR POSITION')
# c. NFLX
wpr_nflx_strat <- ifelse(wpr_nflx_ts > 1,0,1)
for (i in 1 : length(Cl(NFLX))) {wpr_nflx_strat[i] <- ifelse(wpr_nflx_ts[i] == 1,1,ifelse(wpr_nflx_ts[i] == -1,0,wpr_nflx_strat[i-1]))
}
wpr_nflx_strat[is.na(wpr_nflx_strat)] <- 1
wpr_nflx_stratcomp <- cbind(wpr_nflx, wpr_nflx_ts, wpr_nflx_strat)
colnames(wpr_nflx_stratcomp) <- c('WPR(14)','WPR SIGNAL','WPR POSITION')# Trading Strategy Performance # Calculating Returns & setting Benchmark for companies
ret_aapl <- diff(log(Cl(AAPL)))
ret_tsla <- diff(log(Cl(TSLA)))
ret_nflx <- diff(log(Cl(NFLX)))
benchmark_aapl <- ret_aapl
benchmark_tsla <- ret_tsla
benchmark_nflx <- ret_nflx# SMA # 1. AAPL
sma_aapl_ret <- ret_aapl*sma_aapl_strat
sma_aapl_ret_commission_adj <- ifelse((sma_aapl_ts == 1|sma_aapl_ts == -1) & sma_aapl_strat != Lag(sma_aapl_ts), (ret_aapl-0.05)*sma_aapl_strat, ret_aapl*sma_aapl_strat)
sma_aapl_comp <- cbind(sma_aapl_ret, sma_aapl_ret_commission_adj, benchmark_aapl)
colnames(sma_aapl_comp) <- c('SMA','SMA Commission Adj','Apple Benchmark')
charts.PerformanceSummary(sma_aapl_comp, main = 'Apple SMA Performance')
sma_aapl_comp_table <- table.AnnualizedReturns(sma_aapl_comp)
# 2. TSLA
sma_tsla_ret <- ret_tsla*sma_tsla_strat
sma_tsla_ret_commission_adj <- ifelse((sma_tsla_ts == 1|sma_tsla_ts == -1) & sma_tsla_strat != Lag(sma_tsla_ts), (ret_tsla-0.05)*sma_tsla_strat, ret_tsla*sma_tsla_strat)
sma_tsla_comp <- cbind(sma_tsla_ret, sma_tsla_ret_commission_adj, benchmark_tsla)
colnames(sma_tsla_comp) <- c('SMA','SMA Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(sma_tsla_comp, main = 'Tesla SMA Performance')
sma_tsla_comp_table <- table.AnnualizedReturns(sma_tsla_comp)
# 3. NFLX
sma_nflx_ret <- ret_nflx*sma_nflx_strat
sma_nflx_ret_commission_adj <- ifelse((sma_nflx_ts == 1|sma_nflx_ts == -1) & sma_nflx_strat != Lag(sma_nflx_ts), (ret_nflx-0.05)*sma_nflx_strat, ret_nflx*sma_nflx_strat)
sma_nflx_comp <- cbind(sma_nflx_ret, sma_nflx_ret_commission_adj, benchmark_nflx)
colnames(sma_nflx_comp) <- c('SMA','SMA Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(sma_nflx_comp, main = 'Netflix SMA Performance')
sma_nflx_comp_table <- table.AnnualizedReturns(sma_nflx_comp)# Parabolic SAR # 1. AAPL
sar_aapl_ret <- ret_aapl*sar_aapl_strat
sar_aapl_ret_commission_adj <- ifelse((sar_aapl_ts == 1|sar_aapl_ts == -1) & sar_aapl_strat != Lag(sar_aapl_ts), (ret_aapl-0.05)*sar_aapl_strat, ret_aapl*sar_aapl_strat)
sar_aapl_comp <- cbind(sar_aapl_ret, sar_aapl_ret_commission_adj, benchmark_aapl)
colnames(sar_aapl_comp) <- c('SAR','SAR Commission Adj','Apple Benchmark')
charts.PerformanceSummary(sar_aapl_comp, main = 'Apple Parabolic SAR Performance')
sar_aapl_comp_table <- table.AnnualizedReturns(sar_aapl_comp)
# 2. TSLA
sar_tsla_ret <- ret_tsla*sar_tsla_strat
sar_tsla_ret_commission_adj <- ifelse((sar_tsla_ts == 1|sar_tsla_ts == -1) & sar_tsla_strat != Lag(sar_tsla_ts), (ret_tsla-0.05)*sar_tsla_strat, ret_tsla*sar_tsla_strat)
sar_tsla_comp <- cbind(sar_tsla_ret, sar_tsla_ret_commission_adj, benchmark_tsla)
colnames(sar_tsla_comp) <- c('SAR','SAR Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(sar_tsla_comp, main = 'Tesla Parabolic SAR Performance')
sar_tsla_comp_table <- table.AnnualizedReturns(sar_tsla_comp)
# 3. NFLX
sar_nflx_ret <- ret_nflx*sar_nflx_strat
sar_nflx_ret_commission_adj <- ifelse((sar_nflx_ts == 1|sar_nflx_ts == -1) & sar_nflx_strat != Lag(sar_nflx_ts), (ret_nflx-0.05)*sar_nflx_strat, ret_nflx*sar_nflx_strat)
sar_nflx_comp <- cbind(sar_nflx_ret, sar_nflx_ret_commission_adj, benchmark_nflx)
colnames(sar_nflx_comp) <- c('SAR','SAR Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(sar_nflx_comp, main = 'Netflix Parabolic SAR Performance')
sar_nflx_comp_table <- table.AnnualizedReturns(sar_nflx_comp)# CCI  # 1. AAPL
cci_aapl_ret <- ret_aapl*cci_aapl_strat
cci_aapl_ret_commission_adj <- ifelse((cci_aapl_ts == 1|cci_aapl_ts == -1) & cci_aapl_strat != Lag(cci_aapl_ts), (ret_aapl-0.05)*cci_aapl_strat, ret_aapl*cci_aapl_strat)
cci_aapl_comp <- cbind(cci_aapl_ret, cci_aapl_ret_commission_adj, benchmark_aapl)
colnames(cci_aapl_comp) <- c('CCI','CCI Commission Adj','Apple Benchmark')
charts.PerformanceSummary(cci_aapl_comp, main = 'Apple CCI Performance')
cci_aapl_comp_table <- table.AnnualizedReturns(cci_aapl_comp)
# 2. TSLA
cci_tsla_ret <- ret_tsla*cci_tsla_strat
cci_tsla_ret_commission_adj <- ifelse((cci_tsla_ts == 1|cci_tsla_ts == -1) & cci_tsla_strat != Lag(cci_tsla_ts), (ret_tsla-0.05)*cci_tsla_strat, ret_tsla*cci_tsla_strat)
cci_tsla_comp <- cbind(cci_tsla_ret, cci_tsla_ret_commission_adj, benchmark_tsla)
colnames(cci_tsla_comp) <- c('CCI','CCI Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(cci_tsla_comp, main = 'Tesla CCI Performance')
cci_tsla_comp_table <- table.AnnualizedReturns(cci_tsla_comp)
# 3. NFLX
cci_nflx_ret <- ret_nflx*cci_nflx_strat
cci_nflx_ret_commission_adj <- ifelse((cci_nflx_ts == 1|cci_nflx_ts == -1) & cci_nflx_strat != Lag(cci_nflx_ts), (ret_nflx-0.05)*cci_nflx_strat, ret_nflx*cci_nflx_strat)
cci_nflx_comp <- cbind(cci_nflx_ret, cci_nflx_ret_commission_adj, benchmark_nflx)
colnames(cci_nflx_comp) <- c('CCI','CCI Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(cci_nflx_comp, main = 'Netflix CCI Performance')
cci_nflx_comp_table <- table.AnnualizedReturns(cci_nflx_comp)# ROC  # 1. AAPL
roc_aapl_ret <- ret_aapl*roc_aapl_strat
roc_aapl_ret_commission_adj <- ifelse((roc_aapl_ts == 1|roc_aapl_ts == -1) & roc_aapl_strat != Lag(roc_aapl_ts), (ret_aapl-0.05)*roc_aapl_strat, ret_aapl*roc_aapl_strat)
roc_aapl_comp <- cbind(roc_aapl_ret, roc_aapl_ret_commission_adj, benchmark_aapl)
colnames(roc_aapl_comp) <- c('ROC','ROC Commission Adj','Apple Benchmark')
charts.PerformanceSummary(roc_aapl_comp, main = 'Apple ROC Performance')
roc_aapl_comp_table <- table.AnnualizedReturns(roc_aapl_comp)
# 2. TSLA
roc_tsla_ret <- ret_tsla*roc_tsla_strat
roc_tsla_ret_commission_adj <- ifelse((roc_tsla_ts == 1|roc_tsla_ts == -1) & roc_tsla_strat != Lag(roc_tsla_ts), (ret_tsla-0.05)*roc_tsla_strat, ret_tsla*roc_tsla_strat)
roc_tsla_comp <- cbind(roc_tsla_ret, roc_tsla_ret_commission_adj, benchmark_tsla)
colnames(roc_tsla_comp) <- c('ROC','ROC Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(roc_tsla_comp, main = 'Tesla ROC Performance')
roc_tsla_comp_table <- table.AnnualizedReturns(roc_tsla_comp)
# 3. NFLX
roc_nflx_ret <- ret_nflx*roc_nflx_strat
roc_nflx_ret_commission_adj <- ifelse((roc_nflx_ts == 1|roc_nflx_ts == -1) & roc_nflx_strat != Lag(roc_nflx_ts), (ret_nflx-0.05)*roc_nflx_strat, ret_nflx*roc_nflx_strat)
roc_nflx_comp <- cbind(roc_nflx_ret, roc_nflx_ret_commission_adj, benchmark_nflx)
colnames(roc_nflx_comp) <- c('ROC','ROC Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(roc_nflx_comp, main = 'Netflix ROC Performance')
roc_nflx_comp_table <- table.AnnualizedReturns(roc_nflx_comp)# SMI  # 1. AAPL
smi_aapl_ret <- ret_aapl*smi_aapl_strat
smi_aapl_ret_commission_adj <- ifelse((smi_aapl_ts == 1|smi_aapl_ts == -1) & smi_aapl_strat != Lag(smi_aapl_ts), (ret_aapl-0.05)*smi_aapl_strat, ret_aapl*smi_aapl_strat)
smi_aapl_comp <- cbind(smi_aapl_ret, smi_aapl_ret_commission_adj, benchmark_aapl)
colnames(smi_aapl_comp) <- c('SMI','SMI Commission Adj','Apple Benchmark')
charts.PerformanceSummary(smi_aapl_comp, main = 'Apple SMI Performance')
smi_aapl_comp_table <- table.AnnualizedReturns(smi_aapl_comp)
# 2. TSLA
smi_tsla_ret <- ret_tsla*smi_tsla_strat
smi_tsla_ret_commission_adj <- ifelse((smi_tsla_ts == 1|smi_tsla_ts == -1) & smi_tsla_strat != Lag(smi_tsla_ts), (ret_tsla-0.05)*smi_tsla_strat, ret_tsla*smi_tsla_strat)
smi_tsla_comp <- cbind(smi_tsla_ret, smi_tsla_ret_commission_adj, benchmark_tsla)
colnames(smi_tsla_comp) <- c('SMI','SMI Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(smi_tsla_comp, main = 'Tesla SMI Performance')
smi_tsla_comp_table <- table.AnnualizedReturns(smi_tsla_comp)
# 3. NFLX
smi_nflx_ret <- ret_nflx*smi_nflx_strat
smi_nflx_ret_commission_adj <- ifelse((smi_nflx_ts == 1|smi_nflx_ts == -1) & smi_nflx_strat != Lag(smi_nflx_ts), (ret_nflx-0.05)*smi_nflx_strat, ret_nflx*smi_nflx_strat)
smi_nflx_comp <- cbind(smi_nflx_ret, smi_nflx_ret_commission_adj, benchmark_nflx)
colnames(smi_nflx_comp) <- c('SMI','SMI Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(smi_nflx_comp, main = 'Netflix SMI Performance')
smi_nflx_comp_table <- table.AnnualizedReturns(smi_nflx_comp)# WPR  # 1. AAPL
wpr_aapl_ret <- ret_aapl*wpr_aapl_strat
wpr_aapl_ret_commission_adj <- ifelse((wpr_aapl_ts == 1|wpr_aapl_ts == -1) & wpr_aapl_strat != Lag(wpr_aapl_ts), (ret_aapl-0.05)*wpr_aapl_strat, ret_aapl*wpr_aapl_strat)
wpr_aapl_comp <- cbind(wpr_aapl_ret, wpr_aapl_ret_commission_adj, benchmark_aapl)
colnames(wpr_aapl_comp) <- c('WPR','WPR Commission Adj','Apple Benchmark')
charts.PerformanceSummary(wpr_aapl_comp, main = 'Apple WPR Performance')
wpr_aapl_comp_table <- table.AnnualizedReturns(wpr_aapl_comp)
# 2. TSLA
wpr_tsla_ret <- ret_tsla*wpr_tsla_strat
wpr_tsla_ret_commission_adj <- ifelse((wpr_tsla_ts == 1|wpr_tsla_ts == -1) & wpr_tsla_strat != Lag(wpr_tsla_ts), (ret_tsla-0.05)*wpr_tsla_strat, ret_tsla*wpr_tsla_strat)
wpr_tsla_comp <- cbind(wpr_tsla_ret, wpr_tsla_ret_commission_adj, benchmark_tsla)
colnames(wpr_tsla_comp) <- c('WPR','WPR Commission Adj','Tesla Benchmark')
charts.PerformanceSummary(wpr_tsla_comp, main = 'Tesla WPR Performance')
wpr_tsla_comp_table <- table.AnnualizedReturns(wpr_tsla_comp)
# 3. NFLX
wpr_nflx_ret <- ret_nflx*wpr_nflx_strat
wpr_nflx_ret_commission_adj <- ifelse((wpr_nflx_ts == 1|wpr_nflx_ts == -1) & wpr_nflx_strat != Lag(wpr_nflx_ts), (ret_nflx-0.05)*wpr_nflx_strat, ret_nflx*wpr_nflx_strat)
wpr_nflx_comp <- cbind(wpr_nflx_ret, wpr_nflx_ret_commission_adj, benchmark_nflx)
colnames(wpr_nflx_comp) <- c('WPR','WPR Commission Adj','Netflix Benchmark')
charts.PerformanceSummary(wpr_nflx_comp, main = 'Netflix WPR Performance')
wpr_nflx_comp_table <- table.AnnualizedReturns(wpr_nflx_comp)

翻譯自: https://medium.com/swlh/creating-trading-strategies-and-backtesting-with-r-bc206a95da00

均線交易策略的回測 r

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389564.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389564.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389564.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

opencv入門課程:彩色圖像灰度化和二值化(采用skimage庫和opencv庫兩種方法)

用最簡單的辦法實現彩色圖像灰度化和二值化&#xff1a; 首先采用skimage庫&#xff08;skimage庫現在在scikit_image庫中&#xff09;實現&#xff1a; from skimage.color import rgb2gray import numpy as np import matplotlib.pyplot as plt""" skimage庫…

SVN中Revert changes from this revision 跟Revert to this revision

譬如有個文件&#xff0c;有十個版本&#xff0c;假定版本號是1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5&#xff0c;6&#xff0c;7&#xff0c;8&#xff0c;9&#xff0c;10。Revert to this revision&#xff1a; 如果是在版本6這里點擊“Revert to this rev…

歸 [拾葉集]

歸 心歸故鄉 想象行走在 鄉間恬靜小路上 讓那些疲憊的夢 都隨風飛散吧&#xff01; 不去想那些世俗 人來人往 熙熙攘攘 秋日午后 陽光下 細數落葉 來日方長 世上的路 有詩人、浪子 歌詠吟唱 世上的人 在欲望、信仰中 彷徨 彷徨又迷茫 親愛的人兒 快結束那 無休止的獨自流浪 莫要…

instagram分析以預測與安的限量版運動鞋轉售價格

Being a sneakerhead is a culture on its own and has its own industry. Every month Biggest brands introduce few select Limited Edition Sneakers which are sold in the markets according to Lottery System called ‘Raffle’. Which have created a new market of i…

opencv:用最鄰近插值和雙線性插值法實現上采樣(放大圖像)與下采樣(縮小圖像)

上采樣與下采樣 概念&#xff1a; 上采樣&#xff1a; 放大圖像&#xff08;或稱為上采樣&#xff08;upsampling&#xff09;或圖像插值&#xff08;interpolating&#xff09;&#xff09;的主要目的 是放大原圖像,從而可以顯示在更高分辨率的顯示設備上。 下采樣&#xff…

CSS魔法堂:那個被我們忽略的outline

前言 在CSS魔法堂&#xff1a;改變單選框顏色就這么吹毛求疵&#xff01;中我們要模擬原生單選框通過Tab鍵獲得焦點的效果&#xff0c;這里涉及到一個常常被忽略的屬性——outline&#xff0c;由于之前對其印象確實有些模糊&#xff0c;于是本文打算對其進行稍微深入的研究^_^ …

初創公司怎么做銷售數據分析_初創公司與Faang公司的數據科學

初創公司怎么做銷售數據分析介紹 (Introduction) In an increasingly technological world, data scientist and analyst roles have emerged, with responsibilities ranging from optimizing Yelp ratings to filtering Amazon recommendations and designing Facebook featu…

opencv:灰色和彩色圖像的像素直方圖及直方圖均值化的實現與展示

直方圖及直方圖均值化的理論&#xff0c;實現及展示 直方圖&#xff1a; 首先&#xff0c;我們來看看什么是直方圖&#xff1a; 理論概念&#xff1a; 在圖像處理中&#xff0c;經常用到直方圖&#xff0c;如顏色直方圖、灰度直方圖等。 圖像的灰度直方圖就描述了圖像中灰度分…

mysql.sock問題

Cant connect to local MySQL server through socket /tmp/mysql.sock 上述提示可能在啟動mysql時遇到&#xff0c;即在/tmp/mysql.sock位置找不到所需要的mysql.sock文件&#xff0c;主要是由于my.cnf文件里對mysql.sock的位置設定導致。 mysql.sock默認的是在/var/lib/mysql,…

交換機的基本原理配置(一)

1、配置主機名 在全局模式下輸入hostname 名字 然后回車即可立馬生效&#xff08;在生產環境交換機必須有自己唯一的名字&#xff09; Switch(config)#hostname jsh-sw1jsh-sw1(config)#2、顯示系統OS名稱及版本信息 特權模式下&#xff0c;輸入命令 show version Switch#show …

opencv:卷積涉及的基礎概念,Sobel邊緣檢測代碼實現及Same(相同)填充與Vaild(有效)填充

濾波 線性濾波可以說是圖像處理最基本的方法&#xff0c;它可以允許我們對圖像進行處理&#xff0c;產生很多不同的效果。 卷積 卷積的概念&#xff1a; 卷積的原理與濾波類似。但是卷積卻有著細小的差別。 卷積操作也是卷積核與圖像對應位置的乘積和。但是卷積操作在做乘…

機器學習股票_使用概率機器學習來改善您的股票交易

機器學習股票Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seek…

BZOJ 2818 Gcd

傳送門 題解&#xff1a;設p為素數 &#xff0c;則gcd(x/p,y/p)1也就是說求 x&#xff0f;p以及 y&#xff0f;p的歐拉函數。歐拉篩前綴和就可以解決 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map&…

LeetCode387-字符串中的第一個唯一字符(查找,自定義數據結構)

一開始想用HashMap&#xff0c;把每個字符放進去&#xff0c;然后統計出現的次數。 使用LinkedHashMap的話&#xff0c;鍵值對的順序都是不會變的。 LinkedHashMap<Character,Integer> map new LinkedHashMap<>();map.put(i,1111);map.put(j,2222);map.put(k,3333…

r psm傾向性匹配_南瓜香料指標psm如何規劃季節性廣告

r psm傾向性匹配Retail managers have been facing an extraordinary time with the COVID-19 pandemic. But the typical plans to prepare for seasonal sales will be a new challenge. More seasonal products have been introduced over the years, making August the bes…

主成分分析:PCA的思想及鳶尾花實例實現

主成份分析算法PCA 非監督學習算法 PCA的實現&#xff1a; 簡單來說&#xff0c;就是將數據從原始的空間中轉換到新的特征空間中&#xff0c;例如原始的空間是三維的(x,y,z)&#xff0c;x、y、z分別是原始空間的三個基&#xff0c;我們可以通過某種方法&#xff0c;用新的坐…

兩家大型網貸平臺竟在借款人審核問題上“偷懶”?

python信用評分卡&#xff08;附代碼&#xff0c;博主錄制&#xff09; https://study.163.com/course/introduction.htm?courseId1005214003&utm_campaigncommission&utm_sourcecp-400000000398149&utm_mediumshare 放貸流量增加&#xff0c;逾期率也會隨之增加&…

解決 Alfred 每次開機都提示請求通訊錄權限的問題

安裝完 Alfred 以后&#xff0c;每次開機都會提示請求通訊錄權限&#xff0c;把設置里的通訊錄關掉也沒用&#xff0c;每次都提示又非常煩人&#xff0c;這里把解決方法記錄一下。 依次打開 應用程序 - Alfred 3.app - 右鍵顯示包內容 - Contents - Frameworks - Alfred Framew…

【轉】DCOM遠程調用權限設置

原文&#xff1a;https://blog.csdn.net/ervinsas/article/details/36424127 最近幾天被搞得焦頭爛額&#xff0c;由于DCOM客戶端程序是在32位系統下開發的&#xff0c;調試時DCOM服務端也是安裝在同一臺機器上&#xff0c;所有過程一直還算順利。可這次項目實施的時候&#xf…

opencv:邊緣檢測之Laplacian算子思想及實現

Laplacian算子邊緣檢測的來源 在邊緣部分求取一階導數&#xff0c;你會看到極值的出現&#xff1a; 如果在邊緣部分求二階導數會出現什么情況? 從上例中我們可以推論檢測邊緣可以通過定位梯度值大于鄰域的相素的方法找到(或者推廣到大 于一個閥值). 從以上分析中&#xff0c…