Become a pro in the stock market in 5 mins

Jumei Lin
5 min readSep 20, 2021

--

Have you dreamed of making money in the share market? This article loops through financial concepts you need to trade in the stock market! Also, it shows you the most important technical index you need to focus on by using XGBoost. if you are interested in predicting stock price, please visit Predicting Stock Prices is as easy as 123

Today, we will use Apple, Google, Amazon, and Microsoft stock as an example to illustrate the concepts:

# The tech stocks we'll use for this analysis
tech_list = ['AAPL', 'GOOG', 'MSFT', 'AMZN']
# Set up End and Start times for data grab
end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)
#For loop for grabing yahoo finance data and setting as a dataframe
for stock in tech_list:
# Set DataFrame as the Stock Ticker
stock_info=yf.Ticker(stock)
hist = stock_info.history(period="max")
globals()[stock] = hist
company_list = [AAPL, GOOG, MSFT, AMZN]
company_name = ["APPLE", "GOOGLE", "MICROSOFT", "AMAZON"]
for company, com_name in zip(company_list, company_name):
company["company_name"] = com_name

#plot the Close values
plt.figure(figsize=(15, 6))
plt.subplots_adjust(top=1.25, bottom=1.2)
for i, company in enumerate(company_list, 1):
plt.subplot(2, 2, i)
company['Close'].plot()
plt.ylabel('Close')
plt.xlabel(None)
plt.title(f"Closing Price of {tech_list[i - 1]}")

plt.tight_layout()

The basic financial statistics

Here are useful calculations that you cant skip when picking a stock:

  • Logarithmic Return. It compares the price of a stock between yesterday and today.

For example, we invest $1000 in both stock A and stock B.

✓ The price of stock A increases from $100 to $102.

We will have a total of 10 shares of A ($1000 / $100=10), which leads to a profit of 20 (($102-$100)*10=20).

✓ The price of stock B increases from $10 to $11

We will have a total of 10 shares of B ($1000 / $10=100), which leads to a profit of 100 (($11-$10)*100=100).

Lumei Digital

The price of stock A was larger although the profit from stock B was much larger. And we divide the move by the starting price of the stock and calculate the percentage change in price, aka the stock return, to resolve this.

The stock return coincides with the percentage change in our invested capital. And when implementing mathematical modeling, we will go with log returns over fixed time intervals.

Your support would be awesome❤️

Please help me get 100 followers.

import numpy as np

def log_return(prices):
return np.log(prices / prices.shift(1))
returns = log_return(AAPL['Close'])
  • Moving Averages (MA): it’s used to identify trend direction and you can simply get the cost of holding stock by calculating the arithmetic mean of the prices over the previous 15, 30, 100, or 200 days.
import plotly.graph_objects as goAAPL['EMA_9'] = AAPL['Close'].ewm(9).mean().shift()
AAPL['SMA_5'] = AAPL['Close'].rolling(5).mean().shift()
AAPL['SMA_10'] = AAPL['Close'].rolling(10).mean().shift()
AAPL['SMA_15'] = AAPL['Close'].rolling(15).mean().shift()
AAPL['SMA_30'] = AAPL['Close'].rolling(30).mean().shift()
fig = go.Figure()
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.EMA_9, name='EMA 9'))
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.SMA_5, name='SMA 5'))
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.SMA_10, name='SMA 10'))
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.SMA_15, name='SMA 15'))
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.SMA_30, name='SMA 30'))
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.Close, name='Close', opacity=0.2))
fig.show()
  • Relative Strength Index(RSI). It’s used to predict whether a stock is overbought/oversold
def relative_strength_idx(df, n=14):
close = df['Close']
delta = close.diff()
delta = delta[1:]
pricesUp = delta.copy()
pricesDown = delta.copy()
pricesUp[pricesUp < 0] = 0
pricesDown[pricesDown > 0] = 0
rollUp = pricesUp.rolling(n).mean()
rollDown = pricesDown.abs().rolling(n).mean()
rs = rollUp / rollDown
rsi = 100.0 - (100.0 / (1.0 + rs))
return rsi
AAPL['RSI'] = relative_strength_idx(hist).fillna(0)fig = go.Figure(go.Scatter(x=AAPL.Date, y=AAPL.RSI, name='RSI'))
fig.show()
  • MACD
EMA_12 = pd.Series(AAPL['Close'].ewm(span=12, min_periods=12).mean())
EMA_26 = pd.Series(AAPL['Close'].ewm(span=26, min_periods=26).mean())
AAPL['MACD'] = pd.Series(EMA_12 - EMA_26)
AAPL['MACD_signal'] = pd.Series(AAPL.MACD.ewm(span=9, min_periods=9).mean())
fig = make_subplots(rows=2, cols=1)
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL.Close, name='Close'), row=1, col=1)
fig.add_trace(go.Scatter(x=AAPL.Date, y=EMA_12, name='EMA 12'), row=1, col=1)
fig.add_trace(go.Scatter(x=AAPL.Date, y=EMA_26, name='EMA 26'), row=1, col=1)
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL['MACD'], name='MACD'), row=2, col=1)
fig.add_trace(go.Scatter(x=AAPL.Date, y=AAPL['MACD_signal'], name='Signal line'), row=2, col=1)
fig.show()
  • Variance. It measures how far a price is from its average and can be used to decide the volatility of investments.
AAPL.var() 
#628.3749178125261

If you compare the variance between a stock and a bond, you will find that stocks normally are more dispersed than bond prices, in other words, stocks are a more volatile investment.

  • Covariance. It’s a correlation in finance. Covariance demonstrates the relationship between the returns of 2 different investments over a period of time and can be used to balance your portfolio.
AAPL.cov()

The above statement will generate an array that defines the covariance values between each pair of columns in the DataFrame.

Find the most important technical index

#prepare for the train/test dataset
test_size = 0.15
valid_size = 0.15
test_split_idx = int(df.shape[0] * (1-test_size))
valid_split_idx = int(df.shape[0] * (1-(valid_size+test_size)))
train_df = df.loc[:valid_split_idx].copy()
valid_df = df.loc[valid_split_idx+1:test_split_idx].copy()
test_df = df.loc[test_split_idx+1:].copy()
drop_cols = ['Date']
train_df = train_df.drop(drop_cols, 1)
valid_df = valid_df.drop(drop_cols, 1)
test_df = test_df.drop(drop_cols, 1)
y_train = train_df['Close'].copy()
X_train = train_df.drop(['Close'], 1)
y_valid = valid_df['Close'].copy()
X_valid = valid_df.drop(['Close'], 1)
y_test = test_df['Close'].copy()
X_test = test_df.drop(['Close'], 1)
#Fine-tune XGBoostRegressor
parameters = {
'n_estimators': [100, 200, 300, 400],
'learning_rate': [0.001, 0.005, 0.01, 0.05],
'max_depth': [8, 10, 12, 15],
'gamma': [0.001, 0.005, 0.01, 0.02],
'random_state': [42]
}
eval_set = [(X_train, y_train), (X_valid, y_valid)]
model = xgb.XGBRegressor(eval_set=eval_set, objective='reg:squarederror', verbose=False)
clf = GridSearchCV(model, parameters)
clf.fit(X_train, y_train)model = xgb.XGBRegressor(**clf.best_params_, objective='reg:squarederror')
model.fit(X_train, y_train, eval_set=eval_set, verbose=False)
#plot the feature importance
plot_importance(model);
importances = model.feature_importances_
print(importances)

It looks like Volume and Daily Return is the key factors that impact the stock close price!

Two useful Python libraries for the stock trading:

1️⃣ PyAlgoTrade: helps you backtest stock trading strategies

2️⃣ TA-Lib: is widely used by trading software developers who carry out technical analysis of financial market data

More Stock Market Topics:

Predicting Stock Prices is as easy as 123

Your support would be awesome❤️

Having more followers will encourage me to write more articles.

--

--

Jumei Lin

Entrepreneur, Writes about artificial intelligence, AWS, and mobile app. @Lumei Digital