Simple python framework to keep track of NSE stocks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

166 lines
4.2 KiB

#!/usr/bin/python
from tvDatafeed import TvDatafeed, Interval
import talib
import numpy
import sqlite3
import re
import requests
import secvars
tv = TvDatafeed()
db = sqlite3.connect(secvars.dbfile)
cursor = db.cursor()
cursor.execute('''SELECT * FROM symbols''')
d = cursor.fetchall()
t=0
high=''
med=''
low=''
shigh=''
smed=''
slow=''
csvfile=open('data.csv', 'w')
csvfile.write('symbol,period,status,status2,status3\n')
for x in d:
sym=x[0]
sym = re.sub(r'[-&]','_',sym)
print("grabbing things for ", sym)
for myinterval in [Interval.in_monthly, Interval.in_weekly, Interval.in_daily]:
notifyme = ''
print(myinterval)
nifty_index_data = tv.get_hist(symbol=sym,exchange='NSE',interval=myinterval,n_bars=201)
close = nifty_index_data['close']
ema20 = talib.EMA(close,timeperiod=20)[-2]
ema50 = talib.EMA(close,timeperiod=50)[-2]
sma200 = talib.SMA(close,timeperiod=200)[-2]
sema20 = talib.EMA(close,timeperiod=20)[-1]
sema50 = talib.EMA(close,timeperiod=50)[-1]
ssma200 = talib.SMA(close,timeperiod=200)[-1]
nma = {'ema20':sema20, 'ema50':sema50, 'sma200':ssma200}
if(max(ema20,ema50,sma200)==ema20):
high='ema20'
elif(max(ema20,ema50,sma200)==ema50):
high='ema50'
else:
high='sma200'
if(max(sema20,sema50,ssma200)==sema20):
shigh='ema20'
elif(max(sema20,sema50,ssma200)==sema50):
shigh='ema50'
else:
shigh='sma200'
if(min(ema20,ema50,sma200) ==ema20):
low='ema20'
elif(min(ema20,ema50,sma200) ==ema50):
low='ema50'
else:
low='sma200'
if(min(sema20,sema50,ssma200) == sema20):
slow='ema20'
elif(min(sema20,sema50,ssma200) == sema50):
slow='ema50'
else:
slow='sma200'
if('ema20' not in [high, low]):
med = 'ema20'
elif('ema50' not in [high, low]):
med = 'ema50'
else:
med='sma200'
if('ema20' not in [shigh, slow]):
smed = 'ema20'
elif('ema50' not in [shigh, slow]):
smed = 'ema50'
else:
smed='sma200'
#print("Order is ", [high, med, low])
if( [high, med, low] == [shigh, smed, slow]):
#do nothing
#print("no change in averages")
#continue
above=False
else:
above=True
#print("Avarages have crossed over")
notify=[]
notifyme = sym+','
notifyme += str(myinterval)[12:-1]+'y,'
if([shigh, smed, slow].index(high)==0):
notify.append(high)
elif([shigh, smed, slow].index(high)==1):
print(high, "has fallen below", shigh)
notify.append(high)
notifyme = notifyme + shigh+' > '+high+'\n'
else:
print(high, "has fallen below ", shigh, "and ", smed)
notify.append(high)
if([shigh, smed, slow].index(med)==0):
print(med, "has risen above", smed)
notify.append(med)
if (not smed in notify):
notifyme += med + '>' + smed +'\n'
elif([shigh, smed, slow].index(med)==1):
print(med, "has remained in middle")
notify.append(med)
#do nothing
elif([shigh, smed, slow].index(med)==2):
print(med, "has fallen below", smed)
notifyme += smed+' > '+med+'\n'
notify.append(med)
if([shigh, smed, slow].index(low) == 0):
print(low, "has risen above", smed, 'and ', slow)
elif([shigh, smed, slow].index(low) == 1):
print(low, "has risen above", slow)
if (not slow in notify):
notifyme += low + '>' + slow
else:
print(low, "has remained below")
#do nothing
#print(shigh, ">", smed, ">", slow)
#candles
if(above):
addstring+=','
else:
addstring=sym+','+str(myinterval)[12:-1]+'y,,'
chigh = nifty_index_data['open'][-1]
clow = nifty_index_data['close'][-1]
midpoint = abs(chigh+clow)/2
tolerance = abs(midpoint-clow)/2.0+abs((chigh-clow)/10.0)
g=0
for ma in [shigh, smed, slow]:
distance = abs(nma[ma]-midpoint)
if (distance <= tolerance):
print(ma, nma[ma], chigh, clow, distance, tolerance)
#exit()
if(g==0):
notifyme+=addstring
g = g+1
notifyme += '|'+ma+' touching price '
if(ma == slow and g>0):
notifyme += '|\n'
print(notifyme)
csvfile.write(notifyme)
csvfile.close()
csvfile = open('data.csv', 'rb')
myobj = {'chat_id':secvars.chat_id}
files = {'document':csvfile}
headers = {'Content-Type':'multipart/form-data'}
r = requests.post('https://api.telegram.org/bot'+secvars.bot_key+'/sendDocument', data=myobj, files=files)
print(r)
print(r.text)
csvfile.close()
#symbol, duration, status