Upload 2 files
Browse files- 100_tick.csv +101 -0
- random_plot.py +85 -0
100_tick.csv
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
,Company Name,Industry,Symbol,Series,ISIN Code
|
2 |
+
0,ACC Ltd..NS,Construction Materials.NS,ACC.NS,EQ.NS,INE012A01025.NS
|
3 |
+
1,Adani Enterprises Ltd..NS,Metals & Mining.NS,ADANIENT.NS,EQ.NS,INE423A01024.NS
|
4 |
+
2,Adani Green Energy Ltd..NS,Power.NS,ADANIGREEN.NS,EQ.NS,INE364U01010.NS
|
5 |
+
3,Adani Ports and Special Economic Zone Ltd..NS,Services.NS,ADANIPORTS.NS,EQ.NS,INE742F01042.NS
|
6 |
+
4,Adani Total Gas Ltd..NS,Oil Gas & Consumable Fuels.NS,ATGL.NS,EQ.NS,INE399L01023.NS
|
7 |
+
5,Adani Transmission Ltd..NS,Power.NS,ADANITRANS.NS,EQ.NS,INE931S01010.NS
|
8 |
+
6,Ambuja Cements Ltd..NS,Construction Materials.NS,AMBUJACEM.NS,EQ.NS,INE079A01024.NS
|
9 |
+
7,Apollo Hospitals Enterprise Ltd..NS,Healthcare.NS,APOLLOHOSP.NS,EQ.NS,INE437A01024.NS
|
10 |
+
8,Asian Paints Ltd..NS,Consumer Durables.NS,ASIANPAINT.NS,EQ.NS,INE021A01026.NS
|
11 |
+
9,Avenue Supermarts Ltd..NS,Consumer Services.NS,DMART.NS,EQ.NS,INE192R01011.NS
|
12 |
+
10,Axis Bank Ltd..NS,Financial Services.NS,AXISBANK.NS,EQ.NS,INE238A01034.NS
|
13 |
+
11,Bajaj Auto Ltd..NS,Automobile and Auto Components.NS,BAJAJ-AUTO.NS,EQ.NS,INE917I01010.NS
|
14 |
+
12,Bajaj Finance Ltd..NS,Financial Services.NS,BAJFINANCE.NS,EQ.NS,INE296A01024.NS
|
15 |
+
13,Bajaj Finserv Ltd..NS,Financial Services.NS,BAJAJFINSV.NS,EQ.NS,INE918I01026.NS
|
16 |
+
14,Bajaj Holdings & Investment Ltd..NS,Financial Services.NS,BAJAJHLDNG.NS,EQ.NS,INE118A01012.NS
|
17 |
+
15,Bandhan Bank Ltd..NS,Financial Services.NS,BANDHANBNK.NS,EQ.NS,INE545U01014.NS
|
18 |
+
16,Bank of Baroda.NS,Financial Services.NS,BANKBARODA.NS,EQ.NS,INE028A01039.NS
|
19 |
+
17,Berger Paints India Ltd..NS,Consumer Durables.NS,BERGEPAINT.NS,EQ.NS,INE463A01038.NS
|
20 |
+
18,Bharat Electronics Ltd..NS,Capital Goods.NS,BEL.NS,EQ.NS,INE263A01024.NS
|
21 |
+
19,Bharat Petroleum Corporation Ltd..NS,Oil Gas & Consumable Fuels.NS,BPCL.NS,EQ.NS,INE029A01011.NS
|
22 |
+
20,Bharti Airtel Ltd..NS,Telecommunication.NS,BHARTIARTL.NS,EQ.NS,INE397D01024.NS
|
23 |
+
21,Biocon Ltd..NS,Healthcare.NS,BIOCON.NS,EQ.NS,INE376G01013.NS
|
24 |
+
22,Bosch Ltd..NS,Automobile and Auto Components.NS,BOSCHLTD.NS,EQ.NS,INE323A01026.NS
|
25 |
+
23,Britannia Industries Ltd..NS,Fast Moving Consumer Goods.NS,BRITANNIA.NS,EQ.NS,INE216A01030.NS
|
26 |
+
24,Cholamandalam Investment and Finance Company Ltd..NS,Financial Services.NS,CHOLAFIN.NS,EQ.NS,INE121A01024.NS
|
27 |
+
25,Cipla Ltd..NS,Healthcare.NS,CIPLA.NS,EQ.NS,INE059A01026.NS
|
28 |
+
26,Coal India Ltd..NS,Oil Gas & Consumable Fuels.NS,COALINDIA.NS,EQ.NS,INE522F01014.NS
|
29 |
+
27,Colgate Palmolive (India) Ltd..NS,Fast Moving Consumer Goods.NS,COLPAL.NS,EQ.NS,INE259A01022.NS
|
30 |
+
28,DLF Ltd..NS,Realty.NS,DLF.NS,EQ.NS,INE271C01023.NS
|
31 |
+
29,Dabur India Ltd..NS,Fast Moving Consumer Goods.NS,DABUR.NS,EQ.NS,INE016A01026.NS
|
32 |
+
30,Divi's Laboratories Ltd..NS,Healthcare.NS,DIVISLAB.NS,EQ.NS,INE361B01024.NS
|
33 |
+
31,Dr. Reddy's Laboratories Ltd..NS,Healthcare.NS,DRREDDY.NS,EQ.NS,INE089A01023.NS
|
34 |
+
32,Eicher Motors Ltd..NS,Automobile and Auto Components.NS,EICHERMOT.NS,EQ.NS,INE066A01021.NS
|
35 |
+
33,FSN E-Commerce Ventures Ltd..NS,Consumer Services.NS,NYKAA.NS,EQ.NS,INE388Y01029.NS
|
36 |
+
34,GAIL (India) Ltd..NS,Oil Gas & Consumable Fuels.NS,GAIL.NS,EQ.NS,INE129A01019.NS
|
37 |
+
35,Gland Pharma Ltd..NS,Healthcare.NS,GLAND.NS,EQ.NS,INE068V01023.NS
|
38 |
+
36,Godrej Consumer Products Ltd..NS,Fast Moving Consumer Goods.NS,GODREJCP.NS,EQ.NS,INE102D01028.NS
|
39 |
+
37,Grasim Industries Ltd..NS,Construction Materials.NS,GRASIM.NS,EQ.NS,INE047A01021.NS
|
40 |
+
38,HCL Technologies Ltd..NS,Information Technology.NS,HCLTECH.NS,EQ.NS,INE860A01027.NS
|
41 |
+
39,HDFC Asset Management Company Ltd..NS,Financial Services.NS,HDFCAMC.NS,EQ.NS,INE127D01025.NS
|
42 |
+
40,HDFC Bank Ltd..NS,Financial Services.NS,HDFCBANK.NS,EQ.NS,INE040A01034.NS
|
43 |
+
41,HDFC Life Insurance Company Ltd..NS,Financial Services.NS,HDFCLIFE.NS,EQ.NS,INE795G01014.NS
|
44 |
+
42,Havells India Ltd..NS,Consumer Durables.NS,HAVELLS.NS,EQ.NS,INE176B01034.NS
|
45 |
+
43,Hero MotoCorp Ltd..NS,Automobile and Auto Components.NS,HEROMOTOCO.NS,EQ.NS,INE158A01026.NS
|
46 |
+
44,Hindalco Industries Ltd..NS,Metals & Mining.NS,HINDALCO.NS,EQ.NS,INE038A01020.NS
|
47 |
+
45,Hindustan Aeronautics Ltd..NS,Capital Goods.NS,HAL.NS,EQ.NS,INE066F01012.NS
|
48 |
+
46,Hindustan Unilever Ltd..NS,Fast Moving Consumer Goods.NS,HINDUNILVR.NS,EQ.NS,INE030A01027.NS
|
49 |
+
47,Housing Development Finance Corporation Ltd..NS,Financial Services.NS,HDFC.NS,EQ.NS,INE001A01036.NS
|
50 |
+
48,ICICI Bank Ltd..NS,Financial Services.NS,ICICIBANK.NS,EQ.NS,INE090A01021.NS
|
51 |
+
49,ICICI Lombard General Insurance Company Ltd..NS,Financial Services.NS,ICICIGI.NS,EQ.NS,INE765G01017.NS
|
52 |
+
50,ICICI Prudential Life Insurance Company Ltd..NS,Financial Services.NS,ICICIPRULI.NS,EQ.NS,INE726G01019.NS
|
53 |
+
51,ITC Ltd..NS,Fast Moving Consumer Goods.NS,ITC.NS,EQ.NS,INE154A01025.NS
|
54 |
+
52,Indian Oil Corporation Ltd..NS,Oil Gas & Consumable Fuels.NS,IOC.NS,EQ.NS,INE242A01010.NS
|
55 |
+
53,Indian Railway Catering And Tourism Corporation Ltd..NS,Consumer Services.NS,IRCTC.NS,EQ.NS,INE335Y01020.NS
|
56 |
+
54,Indus Towers Ltd..NS,Telecommunication.NS,INDUSTOWER.NS,EQ.NS,INE121J01017.NS
|
57 |
+
55,IndusInd Bank Ltd..NS,Financial Services.NS,INDUSINDBK.NS,EQ.NS,INE095A01012.NS
|
58 |
+
56,Info Edge (India) Ltd..NS,Consumer Services.NS,NAUKRI.NS,EQ.NS,INE663F01024.NS
|
59 |
+
57,Infosys Ltd..NS,Information Technology.NS,INFY.NS,EQ.NS,INE009A01021.NS
|
60 |
+
58,InterGlobe Aviation Ltd..NS,Services.NS,INDIGO.NS,EQ.NS,INE646L01027.NS
|
61 |
+
59,JSW Steel Ltd..NS,Metals & Mining.NS,JSWSTEEL.NS,EQ.NS,INE019A01038.NS
|
62 |
+
60,Kotak Mahindra Bank Ltd..NS,Financial Services.NS,KOTAKBANK.NS,EQ.NS,INE237A01028.NS
|
63 |
+
61,LTIMindtree Ltd..NS,Information Technology.NS,LTIM.NS,EQ.NS,INE214T01019.NS
|
64 |
+
62,Larsen & Toubro Ltd..NS,Construction.NS,LT.NS,EQ.NS,INE018A01030.NS
|
65 |
+
63,Life Insurance Corporation of India.NS,Financial Services.NS,LICI.NS,EQ.NS,INE0J1Y01017.NS
|
66 |
+
64,Mahindra & Mahindra Ltd..NS,Automobile and Auto Components.NS,M&M.NS,EQ.NS,INE101A01026.NS
|
67 |
+
65,Marico Ltd..NS,Fast Moving Consumer Goods.NS,MARICO.NS,EQ.NS,INE196A01026.NS
|
68 |
+
66,Maruti Suzuki India Ltd..NS,Automobile and Auto Components.NS,MARUTI.NS,EQ.NS,INE585B01010.NS
|
69 |
+
67,MphasiS Ltd..NS,Information Technology.NS,MPHASIS.NS,EQ.NS,INE356A01018.NS
|
70 |
+
68,Muthoot Finance Ltd..NS,Financial Services.NS,MUTHOOTFIN.NS,EQ.NS,INE414G01012.NS
|
71 |
+
69,NTPC Ltd..NS,Power.NS,NTPC.NS,EQ.NS,INE733E01010.NS
|
72 |
+
70,Nestle India Ltd..NS,Fast Moving Consumer Goods.NS,NESTLEIND.NS,EQ.NS,INE239A01016.NS
|
73 |
+
71,Oil & Natural Gas Corporation Ltd..NS,Oil Gas & Consumable Fuels.NS,ONGC.NS,EQ.NS,INE213A01029.NS
|
74 |
+
72,One 97 Communications Ltd..NS,Financial Services.NS,PAYTM.NS,EQ.NS,INE982J01020.NS
|
75 |
+
73,PI Industries Ltd..NS,Chemicals.NS,PIIND.NS,EQ.NS,INE603J01030.NS
|
76 |
+
74,Pidilite Industries Ltd..NS,Chemicals.NS,PIDILITIND.NS,EQ.NS,INE318A01026.NS
|
77 |
+
75,Power Grid Corporation of India Ltd..NS,Power.NS,POWERGRID.NS,EQ.NS,INE752E01010.NS
|
78 |
+
76,Procter & Gamble Hygiene & Health Care Ltd..NS,Fast Moving Consumer Goods.NS,PGHH.NS,EQ.NS,INE179A01014.NS
|
79 |
+
77,Reliance Industries Ltd..NS,Oil Gas & Consumable Fuels.NS,RELIANCE.NS,EQ.NS,INE002A01018.NS
|
80 |
+
78,SBI Cards and Payment Services Ltd..NS,Financial Services.NS,SBICARD.NS,EQ.NS,INE018E01016.NS
|
81 |
+
79,SBI Life Insurance Company Ltd..NS,Financial Services.NS,SBILIFE.NS,EQ.NS,INE123W01016.NS
|
82 |
+
80,SRF Ltd..NS,Chemicals.NS,SRF.NS,EQ.NS,INE647A01010.NS
|
83 |
+
81,Samvardhana Motherson International Ltd..NS,Automobile and Auto Components.NS,MOTHERSON.NS,EQ.NS,INE775A01035.NS
|
84 |
+
82,Shree Cement Ltd..NS,Construction Materials.NS,SHREECEM.NS,EQ.NS,INE070A01015.NS
|
85 |
+
83,Siemens Ltd..NS,Capital Goods.NS,SIEMENS.NS,EQ.NS,INE003A01024.NS
|
86 |
+
84,State Bank of India.NS,Financial Services.NS,SBIN.NS,EQ.NS,INE062A01020.NS
|
87 |
+
85,Sun Pharmaceutical Industries Ltd..NS,Healthcare.NS,SUNPHARMA.NS,EQ.NS,INE044A01036.NS
|
88 |
+
86,Tata Consultancy Services Ltd..NS,Information Technology.NS,TCS.NS,EQ.NS,INE467B01029.NS
|
89 |
+
87,Tata Consumer Products Ltd..NS,Fast Moving Consumer Goods.NS,TATACONSUM.NS,EQ.NS,INE192A01025.NS
|
90 |
+
88,Tata Motors Ltd..NS,Automobile and Auto Components.NS,TATAMOTORS.NS,EQ.NS,INE155A01022.NS
|
91 |
+
89,Tata Power Co. Ltd..NS,Power.NS,TATAPOWER.NS,EQ.NS,INE245A01021.NS
|
92 |
+
90,Tata Steel Ltd..NS,Metals & Mining.NS,TATASTEEL.NS,EQ.NS,INE081A01020.NS
|
93 |
+
91,Tech Mahindra Ltd..NS,Information Technology.NS,TECHM.NS,EQ.NS,INE669C01036.NS
|
94 |
+
92,Titan Company Ltd..NS,Consumer Durables.NS,TITAN.NS,EQ.NS,INE280A01028.NS
|
95 |
+
93,Torrent Pharmaceuticals Ltd..NS,Healthcare.NS,TORNTPHARM.NS,EQ.NS,INE685A01028.NS
|
96 |
+
94,UPL Ltd..NS,Chemicals.NS,UPL.NS,EQ.NS,INE628A01036.NS
|
97 |
+
95,UltraTech Cement Ltd..NS,Construction Materials.NS,ULTRACEMCO.NS,EQ.NS,INE481G01011.NS
|
98 |
+
96,United Spirits Ltd..NS,Fast Moving Consumer Goods.NS,MCDOWELL-N.NS,EQ.NS,INE854D01024.NS
|
99 |
+
97,Vedanta Ltd..NS,Metals & Mining.NS,VEDL.NS,EQ.NS,INE205A01025.NS
|
100 |
+
98,Wipro Ltd..NS,Information Technology.NS,WIPRO.NS,EQ.NS,INE075A01022.NS
|
101 |
+
99,Zomato Ltd..NS,Consumer Services.NS,ZOMATO.NS,EQ.NS,INE758T01015.NS
|
random_plot.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from sklearn.kernel_ridge import KernelRidge
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import yfinance as yf
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
class Backtest:
|
12 |
+
def __init__(self, symbol='ACC.NS', tim='1y', sd=9):
|
13 |
+
self.tim = tim
|
14 |
+
self.symbol = symbol
|
15 |
+
self.df = yf.download(tickers=symbol, period = self.tim)
|
16 |
+
self.src = self.df["Close"].values
|
17 |
+
self.h = sd
|
18 |
+
y2, y1 = self.nadaraya_watson_envelope()
|
19 |
+
self.gen_signals(y1,y2)
|
20 |
+
|
21 |
+
|
22 |
+
def nadaraya_watson_envelope(self):
|
23 |
+
n = len(self.src)
|
24 |
+
y2 = np.empty(n)
|
25 |
+
y1 = np.empty(n)
|
26 |
+
h= self.h
|
27 |
+
for i in range(n):
|
28 |
+
sum = 0
|
29 |
+
sumw = 0
|
30 |
+
for j in range(n):
|
31 |
+
w = np.exp(-(np.power(i-j,2)/(h*h*2)))
|
32 |
+
sum += self.src[j]*w
|
33 |
+
sumw += w
|
34 |
+
y2[i] = sum/sumw
|
35 |
+
if i > 0:
|
36 |
+
y1[i] = (y2[i] + y2[i-1]) / 2
|
37 |
+
self.df['y2'] = y2
|
38 |
+
self.df['y1'] = y1
|
39 |
+
return y2, y1
|
40 |
+
|
41 |
+
def gen_signals(self,y1,y2):
|
42 |
+
buy_signals = []
|
43 |
+
sell_signals = []
|
44 |
+
thld = 0.01
|
45 |
+
|
46 |
+
for i in range(1, len(y2)):
|
47 |
+
d = y2[i] - y2[i-1]
|
48 |
+
if d > thld and y2[i-1] < y1[i-1]:
|
49 |
+
buy_signals.append(i)
|
50 |
+
elif d < -thld and y2[i-1] > y1[i-1]:
|
51 |
+
sell_signals.append(i)
|
52 |
+
money = 100
|
53 |
+
profit = []
|
54 |
+
for i in range(len(buy_signals)):
|
55 |
+
buy_index = buy_signals[i]
|
56 |
+
if i < len(sell_signals):
|
57 |
+
sell_index = sell_signals[i]
|
58 |
+
money *= self.src[sell_index] / self.src[buy_index]
|
59 |
+
profit.append(money - 100)
|
60 |
+
self.profit = pd.DataFrame(profit)
|
61 |
+
self.rets = "Returns "+ self.tim +" = " + str(round(((money/100-1)*100),2)) + "%"
|
62 |
+
plt.figure(figsize=(30,15))
|
63 |
+
plt.plot(y2)
|
64 |
+
plt.plot(self.src,color='black', label='close')
|
65 |
+
for signal in buy_signals:
|
66 |
+
plt.axvline(x=signal, color='green',linewidth=2)
|
67 |
+
|
68 |
+
for signal in sell_signals:
|
69 |
+
plt.axvline(x=signal, color='red',linewidth=2)
|
70 |
+
plt.legend()
|
71 |
+
plt.show()
|
72 |
+
|
73 |
+
st.title("Backtesting Nadaraya Regerssor")
|
74 |
+
ticks = pd.read_csv("100_tick.csv")
|
75 |
+
|
76 |
+
companies = ticks['Symbol'].values
|
77 |
+
selected_company = st.sidebar.selectbox("Select a company", companies)
|
78 |
+
tim = st.sidebar.text_input("time frame ex: 1y,2y,100d,10d..")
|
79 |
+
std = st.sidebar.text_input("Standard devation, like 5,6,7,8,9....")
|
80 |
+
backtest = Backtest(selected_company, tim, int(std))
|
81 |
+
st.pyplot(plt)
|
82 |
+
st.text(backtest.rets)
|
83 |
+
st.title("What is Nadaraya-Watson envelope?")
|
84 |
+
st.write("The Nadaraya-Watson envelope is a type of moving average calculated by taking a weighted average of data points over a period of time. The envelope is created by drawing two lines (one above and one below) parallel to the moving average at a user-defined percentage distance. Top and bottom lines form a “band” around the moving average that can be used to identify potential trend changes and generate trading signals. The Nadaraya-Watson cover is named after its creators, Vasily Nadaraya and his Geoffrey Watson.")
|
85 |
+
st.write(" [How we made this](https://share.streamlit.io/mesmith027/streamlit_webapps/main/MC_pi/streamlit_app.py)")
|