Advertisement
Guest User

Untitled

a guest
Jan 17th, 2023
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. # package import statement
  2. #from smartapi import SmartConnect
  3. #from smartapi.smartConnect import SmartConnect
  4. #import smartapi.smartExceptions(for smartExceptions)
  5. from smartapi import SmartWebSocket
  6.  
  7. # datetime and canlender import
  8. from datetime import datetime
  9.  
  10. # pandas import
  11. import pandas as pd
  12.  
  13. # time import
  14. import time
  15.  
  16. # json import
  17. import json
  18.  
  19. # requests import
  20. import requests
  21.  
  22. # http client import
  23. import http.client
  24.  
  25. # sqlite3 import
  26. import sqlite3
  27.  
  28. # local files import
  29. import auth
  30.  
  31. # sqlie3 connection with database
  32. connection = sqlite3.connect('angelonealgo.db')
  33. connection.row_factory = sqlite3.Row
  34. cursor = connection.cursor()
  35.  
  36. # get stock token data from database
  37. cursor.execute("""select name, token from stocktoken""")
  38. rows = cursor.fetchall()
  39.  
  40. # generate totp code
  41. totp = auth.TOTP
  42.  
  43. # api connection
  44. conn = http.client.HTTPSConnection("apiconnect.angelbroking.com")
  45.  
  46. # create object of call
  47. smartAPI = auth.SMART_API
  48.                    
  49. # login api call
  50. login = auth.LOGIN
  51.  
  52. # client code
  53. CLIENT_CODE= auth.CLIENT_ID
  54.  
  55. # login and create fetch, bearer tokens and header
  56. if login['message'] == 'SUCCESS':
  57.  
  58.     # fetch refresh token
  59.     REFRESH_TOKEN = login['data']['refreshToken']                          
  60.  
  61.     # fetch the feed token
  62.     FEED_TOKEN = smartAPI.getfeedToken()
  63.    
  64.     # fetch jwt token
  65.     BEARER_TOKEN = login['data']['jwtToken']
  66.    
  67.     # get the profile
  68.     res = smartAPI.getProfile(REFRESH_TOKEN)
  69.    
  70.     SMART_SOCKET = SmartWebSocket(FEED_TOKEN, CLIENT_CODE)
  71.  
  72. token="nse_cm|3787"
  73. task="mw"
  74.  
  75. def on_message(ws, message):
  76.     print("Ticks message: {}".format(message))
  77.    
  78. def on_open(ws):
  79.     print("on open")
  80.     SMART_SOCKET.subscribe(task,token)
  81.    
  82. def on_error(ws, error):
  83.     print(error)
  84.    
  85. def on_close(ws):
  86.     print("Close")
  87.     ws.stop()
  88.    
  89. def on_tick(ws, tick):
  90.     print("Ticks: {}".format(tick))
  91.  
  92. def on_connect(ws, response):
  93.     ws.websocket_connection() # Websocket connection  
  94.     ws.send_request(token,task)    
  95.  
  96. # Assign the callbacks.
  97. SMART_SOCKET._on_open = on_open
  98. SMART_SOCKET._on_message = on_message
  99. SMART_SOCKET._on_error = on_error
  100. SMART_SOCKET.on_ticks = on_tick
  101. SMART_SOCKET.on_connect = on_connect
  102. SMART_SOCKET._on_close = on_close
  103.  
  104. SMART_SOCKET.connect() 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement