Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # package import statement
- #from smartapi import SmartConnect
- #from smartapi.smartConnect import SmartConnect
- #import smartapi.smartExceptions(for smartExceptions)
- from smartapi import SmartWebSocket
- # datetime and canlender import
- from datetime import datetime
- # pandas import
- import pandas as pd
- # time import
- import time
- # json import
- import json
- # requests import
- import requests
- # http client import
- import http.client
- # sqlite3 import
- import sqlite3
- # local files import
- import auth
- # sqlie3 connection with database
- connection = sqlite3.connect('angelonealgo.db')
- connection.row_factory = sqlite3.Row
- cursor = connection.cursor()
- # get stock token data from database
- cursor.execute("""select name, token from stocktoken""")
- rows = cursor.fetchall()
- # generate totp code
- totp = auth.TOTP
- # api connection
- conn = http.client.HTTPSConnection("apiconnect.angelbroking.com")
- # create object of call
- smartAPI = auth.SMART_API
- # login api call
- login = auth.LOGIN
- # client code
- CLIENT_CODE= auth.CLIENT_ID
- # login and create fetch, bearer tokens and header
- if login['message'] == 'SUCCESS':
- # fetch refresh token
- REFRESH_TOKEN = login['data']['refreshToken']
- # fetch the feed token
- FEED_TOKEN = smartAPI.getfeedToken()
- # fetch jwt token
- BEARER_TOKEN = login['data']['jwtToken']
- # get the profile
- res = smartAPI.getProfile(REFRESH_TOKEN)
- SMART_SOCKET = SmartWebSocket(FEED_TOKEN, CLIENT_CODE)
- token="nse_cm|3787"
- task="mw"
- def on_message(ws, message):
- print("Ticks message: {}".format(message))
- def on_open(ws):
- print("on open")
- SMART_SOCKET.subscribe(task,token)
- def on_error(ws, error):
- print(error)
- def on_close(ws):
- print("Close")
- ws.stop()
- def on_tick(ws, tick):
- print("Ticks: {}".format(tick))
- def on_connect(ws, response):
- ws.websocket_connection() # Websocket connection
- ws.send_request(token,task)
- # Assign the callbacks.
- SMART_SOCKET._on_open = on_open
- SMART_SOCKET._on_message = on_message
- SMART_SOCKET._on_error = on_error
- SMART_SOCKET.on_ticks = on_tick
- SMART_SOCKET.on_connect = on_connect
- SMART_SOCKET._on_close = on_close
- SMART_SOCKET.connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement