Trading Strategy - Technical Analysis With Python TA-Lib
Trading Strategy - Technical Analysis With Python TA-Lib
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 1/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 2/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
For every trading strategy one needs to define assets to trade, entry/exit points
and money management rules. Bad money management can make a
potentially profitable strategy unprofitable.
— From Wikipedia
. . .
simple strategy. (Please do not directly use the strategy for live trading as
backtest is required). If you want to calculate the indicator by yourself, refer
to my previous post on how to do it in Pandas.
In this post, I will build a strategy with RSI (a momentum indicator) and
Bollinger Bands %b (a volatility indicator). High RSI (usually above 70)
may indicate a stock is overbought, therefore it is a sell signal. Low RSI
(usually below 30) indicates stock is oversold, which means a buy signal.
Bollinger Bands tell us most of price action between the two bands.
Therefore, if %b is above 1, price will likely go down back within the bands.
Hence, it is a sell signal. While if it is lower than 0, it is considered a buy
signal. The strategy is a simple voting mechanism. When two indicators
think it is time to buy, then it issues buy order to enter. When both
indicators think it is time to sell, then it issues sell order to exit.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 4/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
start = '2015-04-22'
end = '2017-04-22'
symbol = 'MCD'
max_holding = 100
price = web.DataReader(name=symbol, data_source='quandl',
start=start, end=end)
price = price.iloc[::-1]
price = price.dropna()
close = price['AdjClose'].values
up, mid, low = BBANDS(close, timeperiod=20, nbdevup=2, nbdevdn=2,
matype=0)
rsi = RSI(close, timeperiod=14)
print("RSI (first 10 elements)\n", rsi[14:24])
Output
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 5/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
def bbp(price):
up, mid, low = BBANDS(close, timeperiod=20, nbdevup=2, nbdevdn=2,
matype=0)
bbp = (price['AdjClose'] - low) / (up - low)
return bbp
holdings.ffill(inplace=True)
holdings.fillna(0, inplace=True)
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 6/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
holdings['Order'] = holdings.diff()
holdings.dropna(inplace=True)
fig.tight_layout()
plt.show()
The below, I plot the action with green points (entry points) and red points
(exit points) with the Adjusted Close Price of the McDonald (2015 April to
2017 April). Alongside, the RSI indicators and Bollinger Bands are plotted
to show how two indicators contribute to a trading action. From the graph,
it shows the strategy is good. It captures a couple relative some low prices
and high price during the period. One should backtest to get how well the
strategy does compared to benchmark.
Result in graph
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 8/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
. . .
Recommended reading:
Python for Data Analysis: Data Wrangling with Pandas, NumPy, and
IPython
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 9/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
. . .
My posts:
From CRUD web app dev to SDE in voice assistant — My ongoing Journey
to Machine Learning
Full Stack Development Tutorial: Visualize Trading Data on Angular SPA (1)
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 10/12
10/03/2020 Trading Strategy: Technical Analysis with Python TA-Lib
Read more stories this month when you create a free Medium account.
https://towardsdatascience.com/trading-strategy-technical-analysis-with-python-ta-lib-3ce9d6ce5614 12/12