INVESTOCK – Simple Python computer programing code to fetch & back test NSE historical (15+ years) stock price data

Fetch historical price data and back test between any two dates of NSE Stocks using this simple Python code and import them in .CSV file and predict the winning percentage.
Step 1. Install NSEPY
Open Command Prompt (in Windows 10) as Administrator.
Then paste the following commands (shown in red) one after another.
C:\Python\python.exe -m pip install --upgrade pip
Wait to upgrade...
pip install nsepy
Wait to upgrade...
pip install nsepy –upgrade
Wait to upgrade...
Screenshot for installation in Windows 10 OS.



Step 2. Fetching the price data
Now open Python IDLE and paste the following code.
Save it with a suitable filename, with an extension '.py'.
Stock symbols and Date
Enter the equity code as in NSE, India. Example: “TCS”)
Enter starting and closing date digits, in the format, “yyyy,mm,dd”
Example: “7-Aug-2021” is entered as: “2021,8,7")
Change Symbol and starting as well as closing date in the required format. The data will be saved as .CSV file.


Code
Copy and paste the following code in IDLE, save and run the module.
from nsepy import get_history
from datetime import date
import csv
a1 = get_history(symbol="TCS",  start = date(2015,8,1), end = date(2021,8, 27))
# change the equity symbol, start and end dates in the above line.
# Use the symbol used in NSE. Example:  Infosys as INFY.
# Date should be in the format (yyyy, mm, dd).
# Example 4-May-2020 is enetered as (2020,5,4).
a2 = a1.Close 
a3 = a2.to_string()
print(a3)
a4 = open('Price.csv', 'w')
print(a3, file=a4)
a4.close()
Fetched data will be saved in 'Price.csv' file.



INVESTOCK Project
I have written a Python coded computer program, named as ‘INVESTOCK’ to analyze the NSE stocks price data.
  • It can analyze, back test and estimate the maximum rate of return from the historical prices of a stock listed in NSE, India between any two selected dates for more than 20 years.
  • It predicts the winning percentage of an equity.
  • Interactive graphs will be displayed for the analyzed data.
  • Analyzed date can be saved as .csv file.
  • 15+ years data (if available) can be fetched.
  • Maximum Profit & Loss of an equity can be estimated.
  • Though this is for basic analysis, write me for more advanced options & analyses.
You can download ‘INVESTOCK Project’ from the following link.