Untitled

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

In this step, we are going to implement ( im-plơ-mần) the discussed Bollinger

Bands ( and Relative ( Re-la-div) Strength Index combined trading strategy ( truây
đing tra-tơ-gy) in python.

Code Explanation
Now that we have built some basic intuitions on both the Bollinger Band
and Relative Strength Index indicator. The strategy. ( s trá tơ gy) is going to be
quite unique. We go long (buy the stock) If the Relative Strength Index reading is
less than 30 and the price is below the lower Bollinger Band. Similarly, we go
short (sell the stock) if the Relative Strength Index reading is greater than 70 and
the price is above the upper Bollinger Band
. Our strategy is basically a combination ( com bay nay sần ) of Bollinger Band
strategy and the Relative Strength Index line crossover strategy

Giải thích output in ra: you can easily see


If the RSI reading is less than 30 and the price is below the lower Bollinger Band,
then the function considers it a buy signal

If the RSI reading is greater than 70 and the price is above the upper Bollinger
Band, then the function considers it a sell signal

Step 10: and in step 10 i show the buy and sell signals created ( cri ây địt) in step 9
to plot on Bolliger Bands , RSI and closing price for easy visualization ( vi zu a lai
zation)

here is the trade signal based on the Bollinger Bands and Relative Strength Index
and I left it at Bollinger Bands and the stock's closing price

step 11: In this step, we are going to create a list that indicates 1 if we hold the
stock or 0 if we don’t own or hold the stock. Finally, we are doing some data
manipulations ( đa tà menisbiu lây sần ) to combine all the created lists into one
dataframe.

Output:
and you can see when the signal is 1 and this is a buy signal, so in the position
from 0 to 1, you are currently holding the stock.
Similarly, when the signal is -1 and this is a sell signal, the position has changed
from 1 to 0 and you have sold that stock and currently do not hold any shares.
Step-12: Backtesting
Before moving on, we need to know what Backtesting is?

Backtesting is a technique used in finance to evaluate the effectiveness of a trading


strategy by testing it against historical market data. The purpose of backtesting is
to assess the profitability and risk of a trading strategy and to identify any potential
flaws or areas for improvement. It is an important step in the development and
implementation of a successful trading strategy.

Explain code
The code is performing a backtesting analysis of the trading strategy implemented
in the previous code. It starts by calculating the daily returns of the AAPL stock
using the close prices. Then, it multiplies each return by the corresponding position
signal obtained from the Bollinger Bands and RSI strategy.

Next, it calculates the investment returns by multiplying the number of stocks that
can be purchased with the initial investment value of $100k with the daily returns
obtained from the trading strategy. The backtesting analysis results in a profit
gained from the BB_RSI strategy by investing $100k in APPL, which is printed
using the print() function.

It can see that investing $100k in Apple using the BB_RSI strategy resulted in a
profit of $66,237.23, which is a profit percentage of 66%. This is a relatively high
profit percentage and suggests that the BB_RSI strategy was successful in
generating positive returns on the investment.

Step-13: SPY ETF Comparison

This step is optional but it is highly recommended as we can get an idea of how
well our trading strategy performs against a benchmark (SPY ETF). In this step,
we are going to extract the data of the SPY ETF using the ‘get_historic_data’
function we created and compare the returns we get from the SPY ETF with our
combined strategy of MACD and Bollinger Bands on Apple

Output
We can see that our trading strategy has outperformed the SPY ETF by 40%.
That’s great!
2.
Although the profit percentage is good but to  improve the effectiveness as well as
limit the high risk we need:
 Incorporating additional technical indicators, analyzing multiple
timeframes, 
and implementing risk management techniques .

 Evaluating performance over a longer time period and backtesting using


historical data can identify potential flaws and provide insights into how the
strategy.

 Adding more performance metrics such as the Sharpe ratio, maximum


drawdown, and average trade duration 

You might also like