-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiinvest.py
717 lines (647 loc) · 25.8 KB
/
iinvest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
from flask import Flask, jsonify, render_template, request
import json
from nsetools import Nse
import mysql.connector as mariadb
from nsepy import get_history
import datetime
from datetime import date, timedelta
from decimal import Decimal
import threading
import multiprocessing as mp
from multiprocessing import Pool
from multiprocessing import Process
#import bson
# -*- coding: utf-8 -*-
### Decalre the app
app = Flask(__name__)
#DB Connection
nse = Nse()
dayWiseDetail={}
def fnconnDB():
mariadb_connection = mariadb.connect(host= 'Host IP', user='xxx', password='xxx',database='xxx')
# cursor = mariadb_connection.cursor()
return mariadb_connection
def fnAllStkCode():
allStkCodes=[]
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "SELECT stksymbol from stkname"
cursor.execute(connString)
for codeList in cursor:
if codeList[0] in "\,":
codeList[0].rplace("\,","")
allStkCodes.append(codeList[0].strip())
cursor.close()
return allStkCodes
def fnAllStkNames():
allStkNames=[]
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "SELECT stkname from stkname"
cursor.execute(connString)
for codeList in cursor:
if codeList != None:
allStkNames.append(codeList)
cursor.close()
return allStkNames
#Sync DB
def fnInsertStkDetails(cnvrtDate,symbol,previousClose,dayopen,dayclose,dayhigh,dayLow,volume,percntchg):
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "INSERT INTO stkhistory set sDate = '" + cnvrtDate + "', Symbol = '" + symbol + \
"', prevclose = " + previousClose + ", dayopen = " + dayopen + ", dayclose = " + dayclose + \
", dayhigh = " + dayhigh + ", dayLow = " + dayLow + ",volume = " + volume + \
", percntchg = " + percntchg + ""
cursor.execute(connString)
mariadb_connection.commit()
cursor.close()
mariadb_connection.close()
#Add to Watch list
def fnAddWatchList(addDate,stksymbol,trackprice,comment):
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
print "Inside fnAddWatchList"
connString = "INSERT INTO stkwatchlist set addDate = '" + addDate + "', stksymbol = '" + stksymbol + \
"', trackprice = " + trackprice + ", comment = '" + comment + "'"
print connString
cursor.execute(connString)
mariadb_connection.commit()
cursor.close()
mariadb_connection.close()
#Add to Watch list
def fnAddstkName(stkCode,stkName):
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "INSERT INTO stkname set stksymbol = '" + stkCode + "', stkname = '" + stkName + "'"
print connString
cursor.execute(connString)
mariadb_connection.commit()
cursor.close()
mariadb_connection.close()
@app.route('/fortest')
def fortest():
print test
##### Working fine for new stock add
# stklistFilePath = "/media/prasant/Prasanta/Python_Flask/Iinvest/all_symbols"
# stkListFopen = open(stklistFilePath,"r")
# for stkCode in stkListFopen:
# try:
# # stkQuote = nse.get_quote(str(stkCode))
# stkCode = stkCode.strip()
# print "Inside TRY....", stkCode, stkName
# stkName = nse.get_quote(str(stkCode))['companyName']
# print "Inside TRY....", stkCode, stkName
# except:
# stkName = stkCode
# print stkCode,stkName
# fnAddstkName(stkCode,stkName.replace("'",""))
##### Working fine for new stock add
# dateList = []
# fromDate = request.args['FromDate']
# toDate = request.args['ToDate']
# print "Date is......" + str(fromDate).replace("/","-"),toDate.replace("/","-")
# fromDate = datetime.datetime.strptime(fromDate, '%m/%d/%Y')
# fromDate = '{0}-{1}-{2}'.format(fromDate.year, fromDate.month, fromDate.day % 100)
# toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
# toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
# dateList.append(fromDate)
# dateList.append(toDate)
# return json.dumps({"results":dateList})
@app.route("/searchStk")
def searchStk():
allStkNames = fnAllStkNames()
# print "2nd ............. " , allStkNames[1]
return json.dumps(allStkNames)
# all_stock_codes = nse.get_stock_codes()
# return json.dumps(sorted(all_stock_codes.values()))
def fnStkNameswithStartchar(initailChars):
allStkNames=[]
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "SELECT stkname,stksymbol from stkname where stkname like '%" + initailChars + \
"%' or stksymbol like '%" + initailChars + "%'"
cursor.execute(connString)
for codeList in cursor:
if codeList != None:
allStkNames.append(codeList)
cursor.close()
return allStkNames
@app.route("/serchstkName")
def serchstkName():
# stkCode = fnGetStkCodefromName(request.args['stkName'])
allStkNames = fnStkNameswithStartchar(request.args['stkName'])
# print "2nd ............. " , allStkNames
return json.dumps(allStkNames)
def checkmaxdata(stkcode):
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select max(sDate) from stkhistory where symbol='" + stkcode + "';"
cursor.execute(connString)
for i in cursor:
maxDate = i
cursor.close()
return maxDate[0]
@app.route("/addWatchList")
def addWatchList():
addStkDetails = {}
stkCode = fnGetStkCodefromName(request.args['stkCode'])
comment = request.args['stkComment']
print stkCode , comment
try:
# reqStkList = ['previousClose','open','closePrice','dayHigh','dayLow','low52','high52',
# 'totalTradedVolume','faceValue','companyName','symbol','basePrice','averagePrice']
# reqStkQuote = {}
stkQuote = nse.get_quote(str(stkCode))
print stkQuote["lastPrice"]
addDate = datetime.datetime.now().strftime('%Y/%m/%d')
# addDate = datetime.datetime.now().strptime(addDate,'%m/%d/%Y')
# addDate = str(dataDetails["Date"].split("T")[0])
print addDate
fnAddWatchList(addDate,stkCode,str(stkQuote["lastPrice"]),comment)
result = "Success"
print "Success..."
except:
result = "Failed..."
print "Error"
return json.dumps(result)
#Cnver date time to make json readble
def myconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__()
@app.route("/showHistory")
def showHistory():
# maxDate = request.args['ToDate']
# minDate = request.args['ToDate']
stkCode = request.args['stkCode']
# maxDate = (datetime.datetime.now()).date()
# maxDate = datetime.datetime.now().strftime('%Y-%m-%d')
# # maxDate = '{0}-{1}-{2}'.format(maxDate.year, maxDate.month, maxDate.day % 100)
# minDate = datetime.datetime.strptime(minDate, '%m/%d/%Y')
# minDate = '{0}-{1}-{2}'.format(minDate.year, minDate.month, minDate.day % 100)
allStkInWatchList = []
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
# connString = "SELECT sDate,dayopen,dayclose,dayhigh,daylow,volume,percntchg from stkhistory where symbol = '" + stkCode + \
# "' and sDate between '" + minDate + "' and '" + maxDate + "'"
connString = "SELECT sDate,dayopen,dayclose,dayhigh,daylow,volume,percntchg from stkhistory where symbol = '" + stkCode + \
"' order by sDate DESC limit 20 "
print connString
cursor.execute(connString)
for codeList in cursor:
if codeList != None:
# print codeList
allStkInWatchList.append(codeList)
# print allStkInWatchList
cursor.close()
# print allStkInWatchList[0][2]
return json.dumps(allStkInWatchList,default=to_serializable)
@app.route("/showHistoryAll")
def showHistoryAll():
# maxDate = request.args['ToDate']
# minDate = request.args['ToDate']
stkCode = request.args['stkCode']
# maxDate = (datetime.datetime.now()).date()
# maxDate = datetime.datetime.now().strftime('%Y-%m-%d')
# # maxDate = '{0}-{1}-{2}'.format(maxDate.year, maxDate.month, maxDate.day % 100)
# minDate = datetime.datetime.strptime(minDate, '%m/%d/%Y')
# minDate = '{0}-{1}-{2}'.format(minDate.year, minDate.month, minDate.day % 100)
allStkInWatchList = []
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
# connString = "SELECT sDate,dayopen,dayclose,dayhigh,daylow,volume,percntchg from stkhistory where symbol = '" + stkCode + \
# "' and sDate between '" + minDate + "' and '" + maxDate + "'"
connString = "SELECT sDate,dayopen,dayclose,dayhigh,daylow,volume,percntchg from stkhistory where symbol = '" + stkCode + \
"' order by sDate DESC"
print connString
cursor.execute(connString)
for codeList in cursor:
if codeList != None:
# print codeList
allStkInWatchList.append(codeList)
# print allStkInWatchList
cursor.close()
# print allStkInWatchList[0][2]
return json.dumps(allStkInWatchList,default=to_serializable)
def to_serializable(val):
"""Used by default."""
return str(val)
@app.route("/listWatchList")
def listWatchList():
allStkInWatchList=[]
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "SELECT addDate,stksymbol,trackprice,comment from stkwatchlist order by addDate"
cursor.execute(connString)
for codeList in cursor:
if codeList != None:
# print codeList
allStkInWatchList.append(codeList)
cursor.close()
# print allStkInWatchList[0][2]
return json.dumps(allStkInWatchList, default = myconverter)
@app.route("/removeWatchlist")
def removeWatchlist():
stkCode = request.args['stkCode']
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "delete from stkwatchlist where stksymbol = '" + stkCode +"'"
print connString
try:
cursor.execute(connString)
mariadb_connection.commit()
cursor.close()
mariadb_connection.close()
result = "Success"
except:
result = "Error"
return json.dumps(result)
def syncdbtread(currentStk):
## Check the data exist or not for stock
if checkmaxdata(currentStk) == None :
# If no data found then sync the date for 1 year.
fromDate = (datetime.datetime.now() - timedelta(90)).date()
else:
# If data found sync the data from max available to today.
fromDate = checkmaxdata(currentStk) + timedelta(1)
# fromDate = (datetime.datetime.now() - timedelta(2)).date()
toDate = (datetime.datetime.now()).date()
# print fromDate , ".............", toDate
print "...........Importing data for ........for" , fromDate , toDate , currentStk
# print fromDate < toDate
if fromDate <= toDate :
fromDate = fromDate.strftime('%m/%d/%Y')
fromDate = datetime.datetime.now().strptime(fromDate,'%m/%d/%Y')
toDate = datetime.datetime.now().strftime('%m/%d/%Y')
toDate= datetime.datetime.now().strptime(toDate,'%m/%d/%Y')
stkHistData = []
# print fromDate.year,fromDate.month,fromDate.day , ".............", toDate.year,toDate.month,toDate.day
nextStkcode = get_history(symbol=currentStk,\
start=date(fromDate.year,fromDate.month,fromDate.day),\
end=date(toDate.year,toDate.month,toDate.day))
pandaTableData = nextStkcode.to_json(orient="table")
# print pandaTableData
for dataDetails in json.loads(pandaTableData)['data']:
cnvrtDate = str(dataDetails["Date"].split("T")[0])
symbol = str(dataDetails["Symbol"])
previousClose = str(dataDetails["Prev Close"])
dayopen = str(dataDetails["Open"])
dayclose = str(dataDetails["Close"])
dayhigh = str(dataDetails["High"])
dayLow = str(dataDetails["Low"])
volume = str(dataDetails["Volume"])
percntchg = str(round(100 * (float(dataDetails["Close"] - float(dataDetails["Prev Close"]))) / float(dataDetails["Prev Close"]),2))
# print cnvrtDate , symbol , previousClose , dayopen , dayclose , dayhigh , dayLow , volume , percntchg
# stkHistData = [cnvrtDate , symbol , previousClose , dayopen , dayclose , dayhigh , dayLow , volume , percntchg]
# print stkHistData
fnInsertStkDetails(cnvrtDate , symbol , previousClose , dayopen , dayclose , dayhigh , dayLow , volume , percntchg)
# dayWiseDetail = {symbol + cnvrtDate : stkHistData }
# print dayWiseDetail
# Not working as of now...
@app.route("/showsyncstatus")
def showsyncstatus():
stkCode = request.args['stkCode']
return json.dumps(stkCode)
@app.route("/syncdb")
def syncdb():
threads = []
allStkCodes = fnAllStkCode()
for currentStk in allStkCodes:
# showsyncstatus()
syncdbtread(currentStk)
# processes = [mp.Process(target=syncdbtread(currentStk)) for currentStk in allStkCodes]
# pool = Pool(8)
# pool.map()
# allStkCodes = ["MOHOTAMILL"]
# for currentStk in allStkCodes:
# t = Process(target=syncdbtread, args=(currentStk,))
# # t = threading.Thread(target=syncdbtread, args=(currentStk,))
# # t.daemon = True
# threads.append(t)
# # print "hello"
# for i in threads:
# i.start()
# # for i in threads:
# # i.join()
# print dayWiseDetail
return json.dumps("Success")
def fnGetStkCodefromName(stkName):
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select stksymbol from stkname where stkname = '" + stkName + "'"
cursor.execute(connString)
for i in cursor:
stkName = i[0]
return stkName
@app.route("/showDetails")
def showDetails():
#
# print request.args['stkName']
try:
stkCode = fnGetStkCodefromName(request.args['stkName'])
# print stkCode
# Lsit of details to be required
reqStkList = ['previousClose','open','closePrice','lastPrice','dayHigh','dayLow','low52','high52',
'totalTradedVolume','faceValue','companyName','symbol','basePrice','averagePrice']
reqStkQuote = {}
# print "Hellooo............." + stkCode
stkQuote = nse.get_quote(str(stkCode))
# print stkQuote
# Get the values of listed details from quote..
for reqCode in sorted(reqStkList):
reqStkQuote[reqCode]=stkQuote[reqCode]
#Calacute percentage change with prev. day close
if float(stkQuote['closePrice']) == 0:
reqStkQuote['ChngInPercnt'] = 100 * (float(stkQuote['lastPrice'] - float(stkQuote['previousClose']))) / float(stkQuote['previousClose'])
else:
reqStkQuote['ChngInPercnt'] = 100 * (float(stkQuote['closePrice'] - float(stkQuote['previousClose']))) / float(stkQuote['previousClose'])
except:
reqStkQuote["symbol"] = stkCode
return json.dumps(reqStkQuote)
# def updateStkName():
@app.route('/')
def index():
return render_template('index.html')
# def candleStatus(stkCode, prevclose, openPrice, closePrice, highPrice, lowPrice ):
# bodySize = float(closePrice) - float(openPrice)
# upperShadow = float(highPrice) - float(closePrice)
# lowerShadow = float(openPrice) - float(lowPrice)
# if float(lowerShadow) >= 2 * float(bodySize) :
# if float(upperShadow == 0):
# return stkCode
# def hammerstick(stkCode, openPrice, closePrice, highPrice, lowPrice ):
# bodySize = float(closePrice) - float(openPrice)
# upperShadow = float(highPrice) - float(closePrice)
# lowerShadow = float(openPrice) - float(lowPrice)
# if float(lowerShadow) >= 2 * float(bodySize) :
# if float(highPrice) == float(closePrice):
# return stkCode
@app.route('/redhammer')
def redhammer():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listHammerStick = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol,dayopen,dayclose,dayhigh,daylow from stkhistory as atab where sDate= '" + toDate + "'\
and (atab.dayhigh >= atab.dayclose) and (atab.dayopen >= atab.dayclose) \
and ( (atab.dayhigh - atab.dayclose) >= (atab.dayopen - atab.dayclose)) \
and ((atab.dayopen - atab.daylow) > 3 * (atab.dayhigh - atab.dayclose))"
# and ((atab.dayclose - atab.daylow) > 2 * (atab.dayopen - atab.dayclose)) and (atab.dayopen=atab.dayhigh)"
# and (atab.dayclose = atab.dayhigh)
# print connString
cursor.execute(connString)
for data in cursor:
listHammerStick.append(data[0])
return json.dumps(listHammerStick)
@app.route('/greenhammer')
def greenhammer():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listlonghammer = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol,dayopen,dayclose,dayhigh,daylow,percntchg from stkhistory as atab where sDate= '" + toDate + "'\
and (atab.dayhigh >= atab.dayclose) and (atab.dayopen <= atab.dayclose) \
and ( (atab.dayhigh - atab.dayclose) >= (atab.dayclose - atab.dayopen)) \
and ((atab.dayopen - atab.daylow) > 3 * (atab.dayhigh - atab.dayclose))"
# print connString
cursor.execute(connString)
for data in cursor:
listlonghammer.append(data[0])
return json.dumps(listlonghammer)
@app.route('/bullishmarubozu')
def bullishmarubozu():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listBullishMarubozu = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol,dayopen,dayclose,dayhigh,daylow from stkhistory as atab where sDate= '" + toDate + "'\
and (atab.dayopen = atab.daylow) and (atab.dayhigh = atab.dayclose) and (atab.dayopen != atab.dayclose)"
# print connString
cursor.execute(connString)
for data in cursor:
listBullishMarubozu.append(data[0])
return json.dumps(listBullishMarubozu)
@app.route('/bearishmarubozu')
def bearishmarubozu():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listBearishMarubozu = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol,dayopen,dayclose,dayhigh,daylow from stkhistory as atab where sDate= '" + toDate + "'\
and (atab.dayopen = atab.dayhigh) and (atab.daylow = atab.dayclose) and (atab.dayopen != atab.dayclose)"
# print connString
cursor.execute(connString)
for data in cursor:
listBearishMarubozu.append(data[0])
return json.dumps(listBearishMarubozu)
@app.route('/top20Gain')
def top20Gain():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listBearishMarubozu = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol from stkhistory as atab where sDate= '" + toDate + "'\
and atab.dayclose >= 45 and atab.dayclose < 999 order by percntchg DESC limit 20"
# print connString
cursor.execute(connString)
for data in cursor:
listBearishMarubozu.append(data[0])
return json.dumps(listBearishMarubozu)
@app.route('/top20looser')
def top20looser():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listBearishMarubozu = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol from stkhistory as atab where sDate= '" + toDate + "' \
and atab.dayclose >= 45 and atab.dayclose < 999 order by percntchg ASC limit 20"
# print connString
cursor.execute(connString)
for data in cursor:
listBearishMarubozu.append(data[0])
return json.dumps(listBearishMarubozu)
@app.route('/top20volume')
def top20volume():
toDate = request.args['ToDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
print toDate
listBearishMarubozu = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol from stkhistory as atab where sDate= '" + toDate + "'\
and atab.dayclose >= 45 and atab.dayclose < 999 and atab.percntchg > 1 order by volume DESC limit 20"
# print connString
cursor.execute(connString)
for data in cursor:
listBearishMarubozu.append(data[0])
return json.dumps(listBearishMarubozu)
@app.route('/bulishengulfing')
def bulishengulfing():
allStkCodes = fnAllStkCode()
toDate = request.args['ToDate']
prevDate = request.args['FromDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
prevDate = datetime.datetime.strptime(prevDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
prevDate = '{0}-{1}-{2}'.format(prevDate.year, prevDate.month, prevDate.day % 100)
print prevDate, toDate
listBulishEngulfing = []
bulishEngulfingSuccess = []
cnt=0
# for i in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol,dayopen,dayclose,dayhigh,daylow from stkhistory as atab where \
atab.sDate >= '" + prevDate +"' and atab.sDate <= '" + toDate + "' \
and atab.dayclose >= 45 and atab.dayclose < 999 order by symbol,sDate"
# print connString
cursor.execute(connString)
for data in cursor:
listBulishEngulfing.append(data)
# print listBulishEngulfing
while cnt < len(listBulishEngulfing):
# print cnt
if listBulishEngulfing[cnt][0] == listBulishEngulfing[cnt + 1][0]:
if listBulishEngulfing[cnt][1] > listBulishEngulfing[cnt][2]:
if listBulishEngulfing[cnt][4] > listBulishEngulfing[cnt + 1][4] and \
listBulishEngulfing[cnt][1] < listBulishEngulfing[cnt + 1][2] and \
listBulishEngulfing[cnt][2] > listBulishEngulfing[cnt + 1][1] and \
listBulishEngulfing[cnt][3] < listBulishEngulfing[cnt + 1][3] :
# print listBulishEngulfing[cnt][0],listBulishEngulfing[cnt][1], listBulishEngulfing[cnt + 1][1]
bulishEngulfingSuccess.append(listBulishEngulfing[cnt][0])
cnt = cnt + 2
else:
cnt = cnt + 2
else:
cnt = cnt + 1
# print listBulishEngulfing[2695], listBulishEngulfing[2696]
return json.dumps(bulishEngulfingSuccess)
@app.route('/gapUpOpen')
def gapUpOpen():
allStkCodes = fnAllStkCode()
toDate = request.args['ToDate']
prevDate = request.args['FromDate']
toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
prevDate = datetime.datetime.strptime(prevDate, '%m/%d/%Y')
toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
prevDate = '{0}-{1}-{2}'.format(prevDate.year, prevDate.month, prevDate.day % 100)
# print prevDate, toDate
listGapUpOpen = []
GapUpOpenSuccess = []
cnt=0
# for i in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
connString = "select symbol,dayopen,dayclose,dayhigh,daylow,volume from stkhistory as atab where \
atab.sDate >= '" + prevDate +"' and atab.sDate <= '" + toDate + "' and atab.volume > 100000 \
and atab.dayclose >= 45 and atab.dayclose < 999 order by symbol,sDate"
# print connString
cursor.execute(connString)
for data in cursor:
listGapUpOpen.append(data)
# print listBulishEngulfing
while cnt < len(listGapUpOpen) - 1:
# print listGapUpOpen[cnt][0], listGapUpOpen[cnt + 1][0] , cnt , listGapUpOpen[cnt][5]
if listGapUpOpen[cnt][0] == listGapUpOpen[cnt + 1][0]:
if listGapUpOpen[cnt + 1][1] > listGapUpOpen[cnt][2]:
if listGapUpOpen[cnt + 1][2] > listGapUpOpen[cnt][3] and \
listGapUpOpen[cnt][2] > listGapUpOpen[cnt][1]:
# and \
# listGapUpOpen[cnt][1] < listGapUpOpen[cnt + 1][2] and \
# listGapUpOpen[cnt][2] > listGapUpOpen[cnt + 1][1] and \
# listGapUpOpen[cnt][3] < listGapUpOpen[cnt + 1][3] :
# print listBulishEngulfing[cnt][0],listBulishEngulfing[cnt][1], listBulishEngulfing[cnt + 1][1]
# print listGapUpOpen[cnt][0]
GapUpOpenSuccess.append(listGapUpOpen[cnt][0])
cnt = cnt + 2
else:
cnt = cnt + 2
else:
cnt = cnt + 1
# print listBulishEngulfing[2695], listBulishEngulfing[2696]
return json.dumps(GapUpOpenSuccess)
# @app.route('/threeDma')
# def threeDma():
# allStkCodes = fnAllStkCode()
# toDate = request.args['ToDate']
# prevDate = request.args['FromDate']
# toDate = datetime.datetime.strptime(toDate, '%m/%d/%Y')
# prevDate = datetime.datetime.strptime(prevDate, '%m/%d/%Y')
# toDate = '{0}-{1}-{2}'.format(toDate.year, toDate.month, toDate.day % 100)
# prevDate = '{0}-{1}-{2}'.format(prevDate.year, prevDate.month, prevDate.day % 100)
# print prevDate, toDate
# listBulishEngulfing = []
# bulishEngulfingSuccess = []
# cnt=0
# # for i in allStkCodes:
# mariadb_connection = fnconnDB()
# cursor = mariadb_connection.cursor()
# connString = "select symbol,dayopen,dayclose,dayhigh,daylow from stkhistory as atab where \
# atab.sDate >= '" + prevDate +"' and atab.sDate <= '" + toDate + "' order by symbol,sDate"
# # print connString
# cursor.execute(connString)
@app.route('/candleGraphData')
def candleGraphData():
datalist = []
dataToDump = []
dataToDump1 = []
stkCode = request.args['stkCode']
print "Stk code is......................",request.args['stkCode']
stkCode=request.args['stkCode']
print stkCode
maxDate = checkmaxdata(stkCode)
minDate = maxDate - timedelta(3)
maxDate = maxDate.strftime('%Y-%m-%d')
minDate = minDate.strftime('%Y-%m-%d')
print minDate, maxDate, "............................................ "
# lsitHammerStick = []
# allStkCodes = fnAllStkCode()
# for stkdCode in allStkCodes:
mariadb_connection = fnconnDB()
cursor = mariadb_connection.cursor()
# select * from stkhistory where symbol='ongc' order by sDate DESC limit 20
connString = "select sDate,daylow,dayopen,dayclose,dayhigh from stkhistory where symbol = '" + stkCode + "' \
order by sDate DESC limit 3"
cursor.execute(connString)
for stkdetails in cursor:
datalist.append(stkdetails)
for i in datalist:
dataToDump.append("["+str(i[0])+","+ str(i[1]) + "," + str(i[2]) +"," + str(i[3]) + "," + str(i[4]) + "]")
# dataToDump1.append("dummy")
# dataToDump1.append(str(dataToDump).replace("\"",""))
print dataToDump
return json.dumps(dataToDump)
if __name__=="__main__":
app.run(host='0.0.0.0',port=int('8000'),debug=True,threaded=True)