Learn practical skills, build real-world projects, and advance your career

Strategy and Rules

  • We are going to apply a trading strategy which is possibly the easiest, that is: "Moving Average Crossover Strategy".

Terms:

  • N_moving average : Average of any attribute calculated over last N observed values. Moving average smoothens the curve of a trend line. And, gives us better understanding of the trend.
  • Fast signal : Smaller moving average (ex. 10 days / 20 days moving average) It is called fast signal because it moves closely to the actual trend. (We'll see this later in this notebook)
  • Slow signal : Bigger moving average (ex. 30 dys, 50 days, 200 days) It is called slow signal because it changes less with the changes of values. (Will be clearer with the visualization.)

Rules:

  • When fast signal crosses over slow signal; it indicates a change in trend.
  • If fast signal has been lower than the slow signal and now has a crossover and gets higher than the slow signal; it indicates an upward trend.
  • If fast signal has been higher than the slow signal and now has a crossover and gets smaller than the slow signal; it indicates an downward trend.

Strategy:

  • We'll buy stock at the positive crossover, i.e. on a signal of upward trend.
  • We'll sell stocks at the begetive crossover, i.e. on a signal of negative trend.
  • This way we'll be able to buy a stock in the starting of an upward trend and sell them as soon as the trend starts going downwards.
  • We'll only nuy one share of stock at a time.
### Get all the libraries
import datetime as dt                    ## To work with date time
import pandas as pd                      ## Work with datasets
import pandas_datareader.data as web     ## Get data from web
import numpy as np                       ## Linear Algebra
import plotly.express as px              ## Graphing/Plotting- Visualization
import plotly.graph_objects as go        ## Graphing/Plotting- Visualization
pd.set_option('display.max_columns', 50) ## Display All Columns
import warnings
warnings.filterwarnings("ignore")       ## Mute all the warnings
/opt/conda/lib/python3.7/site-packages/pandas_datareader/compat/__init__.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead. from pandas.util.testing import assert_frame_equal
  • Nifty is an index of 50 most actively traded companies in India from various Sectors.
  • Here, I have a list of NIFTY companies. Since, these are the companies that are traded most; I thought it will be a good idea to work on one of these big companies.
## Names of companies on NIFTY index
company_list = ['ADANIPORTS.NS', 'ASIANPAINT.NS', 'AXISBANK.NS', 'BAJAJ-AUTO.NS', 'BAJFINANCE.NS', 'BAJAJFINSV.NS', 'BPCL.NS',
                'BHARTIARTL.NS', 'INFRATEL.NS', 'BRITANNIA.NS', 'CIPLA.NS', 'COALINDIA.NS', 'DRREDDY.NS', 'EICHERMOT.NS',
                'GAIL.NS', 'GRASIM.NS', 'HCLTECH.NS', 'HDFCBANK.NS', 'HEROMOTOCO.NS', 'HINDALCO.NS', 'HINDUNILVR.NS',
                'HDFC.NS', 'ICICIBANK.NS', 'ITC.NS', 'IOC.NS', 'INDUSINDBK.NS', 'INFY.NS', 'JSWSTEEL.NS','KOTAKBANK.NS',
                'LT.NS', 'M&M.NS', 'MARUTI.NS', 'NTPC.NS', 'NESTLEIND.NS', 'ONGC.NS', 'POWERGRID.NS', 'RELIANCE.NS',
                'SHREECEM.NS', 'SBIN.NS', 'SUNPHARMA.NS', 'TCS.NS', 'TATAMOTORS.NS', 'TATASTEEL.NS', 'TECHM.NS', 
                'TITAN.NS', 'UPL.NS', 'ULTRACEMCO.NS', 'VEDL.NS', 'WIPRO.NS', 'ZEEL.NS']
print(company_list)
['ADANIPORTS.NS', 'ASIANPAINT.NS', 'AXISBANK.NS', 'BAJAJ-AUTO.NS', 'BAJFINANCE.NS', 'BAJAJFINSV.NS', 'BPCL.NS', 'BHARTIARTL.NS', 'INFRATEL.NS', 'BRITANNIA.NS', 'CIPLA.NS', 'COALINDIA.NS', 'DRREDDY.NS', 'EICHERMOT.NS', 'GAIL.NS', 'GRASIM.NS', 'HCLTECH.NS', 'HDFCBANK.NS', 'HEROMOTOCO.NS', 'HINDALCO.NS', 'HINDUNILVR.NS', 'HDFC.NS', 'ICICIBANK.NS', 'ITC.NS', 'IOC.NS', 'INDUSINDBK.NS', 'INFY.NS', 'JSWSTEEL.NS', 'KOTAKBANK.NS', 'LT.NS', 'M&M.NS', 'MARUTI.NS', 'NTPC.NS', 'NESTLEIND.NS', 'ONGC.NS', 'POWERGRID.NS', 'RELIANCE.NS', 'SHREECEM.NS', 'SBIN.NS', 'SUNPHARMA.NS', 'TCS.NS', 'TATAMOTORS.NS', 'TATASTEEL.NS', 'TECHM.NS', 'TITAN.NS', 'UPL.NS', 'ULTRACEMCO.NS', 'VEDL.NS', 'WIPRO.NS', 'ZEEL.NS']
  • WE'LL proceed with Bajaj Fiinserv here. But, the same approach can be applied on any other company as well.
  • Ticker for Bajaj Finserv is "BAJAJFINSV.NS". We'll use this to get historical data of the company.