Python scripts for checking volume spikes in stocks

vagar11

Well-Known Member
#1
This compares the today's volume with the past 30 days volume.

import re
import urllib
import json

def check(stockname):
current=0
url="https://www.google.com/finance/getprices?q="+stockname+"&x=NSE&i=86400&p=30d&f=d,c,h,l,o,v"
html=urllib.urlopen(url).read()
sum=0
change=0
for line in html.split()[7:]:
current=(int)(line.split(',')[5])
sum=sum+current
avg=sum/30;
if(avg<current):
change=((current-avg)*100)/avg
print(stockname +" change is " +(str)(change) +"%" )
return;

symbolslist=["ACC", "ADANIPORTS", "AMBUJACEM", "APOLLOHOSP", "ASHOKLEY", "ASIANPAINT", "AUROPHARMA", "AXISBANK", "BAJAJ-AUTO", "BAJAJFINSV", "BAJAJHLDNG", "BANKBARODA", "BANKINDIA", "BHARATFORG", "BHEL", "BPCL", "BHARTIARTL", "INFRATEL", "BOSCHLTD", "BRITANNIA", "CAIRN", "CANBK", "CIPLA", "COALINDIA", "COLPAL", "CONCOR", "CUMMINSIND", "DABUR", "DIVISLAB", "DRREDDY", "EICHERMOT", "EXIDEIND", "FEDERALBNK", "GAIL", "GSKCONS", "GLAXO", "GLENMARK", "GODREJCP", "GRASIM", "HCLTECH", "HDFCBANK", "HEROMOTOCO", "HINDALCO", "HINDPETRO", "HINDUNILVR", "HDFC", "ITC", "ICICIBANK", "IDEA", "IBULHSGFIN", "IOC", "INDUSINDBK", "INFY", "JSWSTEEL", "KOTAKBANK", "LICHSGFIN", "LT", "LUPIN", "MRF", "M&MFIN", "M&M", "MARICO", "MARUTI", "MOTHERSUMI", "NMDC", "NTPC", "ONGC", "OIL", "OFSS", "PETRONET", "PFC", "POWERGRID", "PNB", "RELCAPITAL", "RCOM", "RELIANCE", "RELINFRA", "RECLTD", "SRTRANSFIN", "SIEMENS", "SBIN", "SAIL", "SUNPHARMA", "SUNDARMFIN", "TATACHEM", "TCS", "TATAGLOBAL", "TATAMOTORS", "TATAPOWER", "TATASTEEL", "TECHM", "TITAN", "UPL", "ULTRACEMCO", "UBL", "MCDOWELL-N", "VEDL", "WIPRO", "YESBANK", "ZEEL"]
i=0
while(i<len(symbolslist)-1):
check(symbolslist)
i=i+1


 

vagar11

Well-Known Member
#2
This compares the last 5 days volume with previous 50 days volume and tell us the volume jump in %.
This script makes a fake browser request.

import re
import urllib2
import json
import time

result=[]
def check(stockname):
current=0
url="https://www.google.com/finance/getprices?q="+stockname+"&x=NSE&i=86400&p=50d&f=d,c,h,l,o,v"
req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0' })
html = urllib2.urlopen(req).read()
#html=urllib.urlopen(url).read()
longTermSum=0
change=0
shortTermSum=0
shortTermCount=-45
for line in html.split()[7:]:
current=(int)(line.split(',')[5])
longTermSum=longTermSum+current
shortTermCount=shortTermCount+1
if(shortTermCount>0):
shortTermSum=shortTermSum+current

shortTermAverage=shortTermSum/5
longTermAvg=longTermSum/50;

if(longTermAvg<shortTermAverage):
change=((shortTermAverage-longTermAvg)*100)/longTermAvg
temp=[]
temp.append(change)
temp.append(stockname)
result.append(temp)
print(stockname +" change is " +(str)(change) +"%" )
return;

symbolslist=["3MINDIA", "ABB", "ABGSHIP", "ACC", "AIAENG", "AARTIIND", "ABAN", "ADANIPORTS", "ADANIPOWER", "ADLABS", "ADVANTA", "ATFL", "AJANTPHARM", "AKZOINDIA", "APLLTD", "ALBK", "ALLCARGO", "ALOKTEXT", "AIL", "ALSTOMT&D", "AMARAJABAT", "AMBUJACEM", "AMTEKAUTO", "ANANTRAJ", "ANDHRABANK", "APARINDS", "APOLLOHOSP", "APOLLOTYRE", "ASAHIINDIA", "ASHOKLEY", "ASHOKA", "ASIANPAINT", "ASTRAZEN", "ASTRAL", "ATUL", "AUROPHARMA", "AUTOAXLES", "AXISBANK", "BASF", "BEML", "BFUTILITIE", "BAJAJ-AUTO", "BAJAJCORP", "BAJAJELEC", "BAJFINANCE", "BAJAJFINSV", "BAJAJHIND", "BAJAJHLDNG", "BALKRISIND", "BALLARPUR", "BALMLAWRIE", "BALRAMCHIN", "BANKBARODA", "BANKINDIA", "BATAINDIA", "BERGEPAINT", "BEL", "BHARATFORG", "BHEL", "BPCL", "BHARTIARTL", "INFRATEL", "BHUSANSTL", "BIOCON", "BIRLACORPN", "BLUEDART", "BLUESTARCO", "BBTC", "BOMDYEING", "BOSCHLTD", "BRIGADE", "BRITANNIA", "CARERATING", "CESC", "CRISIL", "CADILAHC", "CAIRN", "CAMLINFINE", "CANFINHOME", "CANBK", "CAPF", "CARBORUNIV", "CASTEXTECH", "CASTROLIND", "CEATLTD", "CENTRALBK", "CENTENKA", "CENTURYPLY", "CENTURYTEX", "CHAMBLFERT", "CHENNPETRO", "CHOLAFIN", "CIPLA", "CUB", "CLNINDIA", "COALINDIA", "COLPAL", "CONCOR", "COROMANDEL", "CORPBANK", "COX&KINGS", "CUMMINSIND", "CYIENT", "DBREALTY", "DBCORP", "DCBBANK", "DCMSHRIRAM", "DLF", "DABUR", "DEEPAKFERT", "DELTACORP", "DEN", "DENABANK", "DHFL", "DHANBANK", "DISHMAN", "DIVISLAB", "DRREDDY", "DREDGECORP", "DYNAMATECH", "EIDPARRY", "EIHOTEL", "ESABINDIA", "EDELWEISS", "EICHERMOT", "ELECTCAST", "ELGIEQUIP", "EMAMILTD", "ENGINERSIN", "ENIL", "EROSMEDIA", "ESCORTS", "ESSDEE", "ESSELPACK", "EVEREADY", "EXIDEIND", "FDC", "FAGBEARING", "FEDERALBNK", "FMGOETZE", "FINANTECH", "FINCABLES", "FINPIPE", "FSL", "FLEXITUFF", "FORTIS", "FCEL", "FLFL", "FRL", "GAIL", "GHCL", "GMRINFRA", "GTLINFRA", "GVKPIL", "GABRIEL", "GAMMNINFRA", "GDL", "GATI", "GEOJITBNPP", "GEOMETRIC", "GILLETTE", "GITANJALI", "GSKCONS", "GLAXO", "GLENMARK", "GODFRYPHLP", "GODREJCP", "GODREJIND", "GODREJPROP", "GRAPHITE", "GRASIM", "GESHIP", "GREAVESCOT", "GRUH", "GUJALKALI", "GUJFLUORO", "GIPCL", "GMDCLTD", "GNFC", "GPPL", "GSFC", "GSPL", "GULFOILLUB", "HEG", "HCL-INSYS", "HCLTECH", "HDFCBANK", "HSIL", "HTMEDIA", "HATHWAY", "HAVELLS", "HERITGFOOD", "HEROMOTOCO", "HEXAWARE", "HFCL", "HIMATSEIDE", "HINDALCO", "HCC", "HINDCOPPER", "HINDPETRO", "HINDUNILVR", "HINDZINC", "HONAUT", "HDFC", "HDIL", "ITC", "ICICIBANK", "ICRA", "IDBI", "IFCI", "IIFL", "IL&FSENGG", "IL&FSTRANS", "IRB", "ITDCEM", "IVRCLINFRA", "IDEA", "INDIACEM", "IBULHSGFIN", "IBREALEST", "IBVENTURES", "INDIANB", "INDHOTEL", "IOC", "IOB", "IGL", "INDUSINDBK", "NAUKRI", "INFY", "INGERRAND", "INOXLEISUR", "INOXWIND", "IPCALAB", "JBCHEPHARM", "JKCEMENT", "JKIL", "JBFIND", "JKLAKSHMI", "JKTYRE", "JMFINANCIL", "JSWENERGY", "JSWHL", "JSWSTEEL", "JAGRAN", "JAICORPLTD", "JISLJALEQS", "JPASSOCIAT", "JPPOWER", "J&KBANK", "JPINFRATEC", "JINDALSAW", "JINDALSTEL", "JUBLFOOD", "JUBILANT", "JUSTDIAL", "JYOTHYLAB", "JYOTISTRUC", "KPRMILL", "KCP", "KNRCON", "KPIT", "KRBL", "KSBPUMPS", "KSK", "KAJARIACER", "KALPATPOWR", "KANSAINER", "KTKBANK", "KARURVYSYA", "KSCL", "KEC", "KKCL", "KITEX", "KOLTEPATIL", "KOTAKBANK", "L&TFH", "LGBBROSLTD", "LICHSGFIN", "LAOPALA", "LAXMIMACH", "LAKSHVILAS", "LITL", "LT", "LINDEINDIA", "LUPIN", "MBLINFRA", "MMTC", "MOIL", "MRF", "MTEDUCARE", "MAGMA", "MAHSCOOTER", "MAHSEAMLES", "M&MFIN", "M&M", "MHRIL", "MAHLIFE", "MANDHANA", "MRPL", "MARICO", "MARKSANS", "MARUTI", "MAYURUNIQ", "MCLEODRUSS", "MERCATOR", "MERCK", "METALFORGE", "MINDTREE", "MONSANTO", "MOTHERSUMI", "MOTILALOFS", "MPHASIS", "MUNJALSHOW", "MUTHOOTFIN", "NATCOPHARM", "NCC", "NDTV", "NHPC", "NIITLTD", "NIITTECH", "NMDC", "NOCIL", "NTPC", "NATIONALUM", "NBCC", "NBVENTURES", "NAVNETEDUL", "NETWORK18", "NEYVELILIG", "NITINFIRE", "NOIDATOLL", "OBEROIRLTY", "ONGC", "OIL", "OMAXE", "OPTOCIRCUI", "OFSS", "ORIENTCEM", "ORIENTBANK", "ORISSAMINE", "BINDALAGRO", "PIIND", "PNCINFRA", "PFS", "PTC", "PVR", "PAGEIND", "PARSVNATH", "PATELENG", "PENINLAND", "PENIND", "PERSISTENT", "PETRONET", "PFIZER", "PHOENIXLTD", "PIDILITIND", "PIPAVAVDOC", "PEL", "PFC", "POWERGRID", "PRAJIND", "PRAKASH", "PRESTIGE", "PRISMCEM", "PGHH", "PUNJLLOYD", "PNB", "PURVA", "RADICO", "RAIN", "RAJESHEXPO", "RALLIS", "RCF", "RATNAMANI", "RAYMOND", "REDINGTON", "RELCAPITAL", "RCOM", "RIIL", "RELIANCE", "RELINFRA", "RPOWER", "RELIGARE", "REPCOHOME", "RICOAUTO", "ROLTA", "RUCHISOYA", "RECLTD", "SEINV", "SKFINDIA", "SKSMICRO", "SREINFRA", "SRF", "SADBHAV", "SANOFI", "SHANTIGEAR", "SCI", "SHOPERSTOP", "SHREECEM", "RENUKA", "SHRENUJ", "SHRIRAMCIT", "SRTRANSFIN", "SIEMENS", "SIMPLEXINF", "SINTEX", "SITICABLE", "SOBHA", "SONASTEER", "SONATSOFTW", "SOUTHBANK", "SRIPIPES", "SBBJ", "SBIN", "SBT", "SAIL", "STRTECH", "STAR", "SPARC", "SUNPHARMA", "SUNTV", "SUNDARMFIN", "SUNDRMFAST", "SUNTECK", "SUPREMEIND", "SUPREMEINF", "SUPPETRO", "SUVEN", "SUZLON", "SWANENERGY", "SWARAJENG", "SYMPHONY", "SYNDIBANK", "TDPOWERSYS", "TTKPRESTIG", "TVTODAY", "TV18BRDCST", "TVSMOTOR", "TNPL", "TATACHEM", "TATACOFFEE", "TATACOMM", "TCS", "TATAELXSI", "TATAGLOBAL", "TATAINVEST", "TATAMOTORS", "TATAPOWER", "TATASPONGE", "TATASTEEL", "TECHM", "TECHNO", "RAMCOCEM", "THERMAX", "THOMASCOOK", "TITAN", "TORNTPHARM", "TORNTPOWER", "TREEHOUSE", "TRENT", "TBZ", "TRIDENT", "TUBEINVEST", "UCOBANK", "UFLEX", "UPL", "ULTRACEMCO", "UNICHEMLAB", "UNIONBANK", "UNITECH", "UBL", "MCDOWELL-N", "USHAMART", "UTTAMSTL", "VIPIND", "VRLLOG", "VSTIND", "WABAG", "VAIBHAVGBL", "VAKRANGEE", "VTL", "VEDL", "VESUVIUS", "VIDEOIND", "VIJAYABANK", "VINATIORGA", "VIVIDHA", "VOLTAS", "WABCOINDIA", "WELCORP", "WELSPUNIND", "WHEELS", "WHIRLPOOL", "WIPRO", "WOCKPHARMA", "YESBANK", "ZEEL", "ZENSARTECH", "ZYDUSWELL", "ECLERX"]
i=0

while(i<len(symbolslist)):
check(symbolslist)
i=i+1
result.sort()
i=0
while(i<len(result)):
print(result[1] +" change is " +(str)(result[0]) +"%" )
i=i+1
if(i%4==0):
time.sleep(10)
end=raw_input()

 
#3
Whats the strategy behind this? Meaning how will we exactly use it in trading? When volume spikes should be buy or sell?. Any way thanks for this code. I am also thinking to publish mine.


Hey did your script caused any trouble while downloding data from google? I heard that IP gets blocked in google if you queried a lot. Did that happened to you?
 
Last edited:

vagar11

Well-Known Member
#4
Whats the strategy behind this? Meaning how will we exactly use it in trading? When volume spikes should be buy or sell?. Any way thanks for this code. I am also thinking to publish mine.


Hey did your script caused any trouble while downloding data from google? I heard that IP gets blocked in google if you queried a lot. Did that happened to you?
Yes, Google will block it. So, I tried to make a fake browser request. Even, that is not helping.
req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0' })
html = urllib2.urlopen(req).read()
I am putting the method for sleep for 10 seconds after every 5 hits.

Let me know if you get something.

The idea behind the volume script is to find the trending stock as lot of days people get stuck in choppy stocks. We can't just buy/sell using this. The idea was posted by someone else on this forum. I have just coded it.
 
#5
Yes, Google will block it. So, I tried to make a fake browser request. Even, that is not helping.


I am putting the method for sleep for 10 seconds after every 5 hits.

Let me know if you get something.

The idea behind the volume script is to find the trending stock as lot of days people get stuck in choppy stocks. We can't just buy/sell using this. The idea was posted by someone else on this forum. I have just coded it.
User agent is a good move but will not wok because google is monitoring our IP. Try adding proxy to it http://stackoverflow.com/questions/5620263/using-an-http-proxy-python with sleep for at least 5 seconds

Can you post the thread here?
 

vagar11

Well-Known Member
#6
User agent is a good move but will not wok because google is monitoring our IP. Try adding proxy to it http://stackoverflow.com/questions/5620263/using-an-http-proxy-python with sleep for at least 5 seconds

Can you post the thread here?
http://www.traderji.com/advanced-trading-strategies/101085-intraday-stock-selection-best-method.html

I am also using a different google URL for getting the real time feed at a gap of 10 seconds.

link = "http://finance.google.com/finance/info?client=ig&q="
url = link+"%s:%s" % (exchange, symbol)
# print url
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

u = opener.open(url)
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?