Skip to content

Instantly share code, notes, and snippets.

View ganigithub's full-sized avatar
🎯
Focusing

Ganesh Nagarvani ganigithub

🎯
Focusing
View GitHub Profile
# launch chrome and open zerodha website
from selenium import webdriver
from time import sleep
from pyotp import TOTP
service = webdriver.chrome.service.Service(f'{webdriver_path}/chromedriver.exe')
service.start()
options = webdriver.ChromeOptions()
import json
with open(*your file path*,'r') as f:
login = json.load(f)
username = login['user_id']
password = login['password']
totp = login['totp']
webdriver_path = login['webdriver_path'] #path of your chromedriver
url = login['url']
import yaml
from yaml.loader import SafeLoaderwith
open('file.yaml') as f:
login = yaml.load(f, Loader=SafeLoader)
username = login['user_id']
password = login['password']
totp = login['totp']
webdriver_path = login['webdriver_path']
#input username
user = driver.find_element_by_xpath("//input[@type = 'text']")
user.send_keys(username)
#input password
pwd = driver.find_element_by_xpath("//input[@type = 'password']")
pwd.send_keys(password)
#click on login
driver.find_element_by_xpath("//button[@type='submit']").click()
sleep(2)
#request token
request_token = driver.current_url.split('request_token=')[1].split('&')[0]
#access token
token = kite.generate_session(request_token, api_secret=api_secret)
access_token = token['access_token']
#connect python with Kite API
######################### ORDER FUNCTION ##########################
def order(trading_symbol, t_type, quantity, kite):
"""
This function will execute order. i.e place Buy/Sell order
trading_symbol : symbol of the stock
t_type : type of order. 'BUY' or 'SELL'
quantity : quantity of the stocks to buy/sell
kite : kite object that is created earlier
######################### ENTRY CONDITION ##########################
import datetime
for i in range(10000000):
try:
t = datetime.datetime.now()
ltp = kite.ltp(ticker_symbol)[ticker_symbol]['last_price']
print('ltp :', ltp)
if ltp > int_high:
########################## EXIT CONDITION #########################
for i in range(1000000):
try:
t = datetime.datetime.now()
ltp = kite.ltp(ticker_symbol)[ticker_symbol]['last_price']
print('ltp :', ltp)
if (ltp >= target):
order(trading_symbol, "SELL", quantity, kite)
ticker = "NSE:INFY"
kite.ltp(ticker)[ticker]["last_price"]
#output : 1602.05
##OR
ticker = "NFO:BANKNIFTY22APRFUT"
kite.ltp(ticker)[ticker]["last_price"]
#output : 36345.8
from pynse import *
nse = Nse() #create an nse object
nse.get_quote("INFY")["lastPrice"]
#output : 1602.05
##OR
quote = nse.get_quote("BANKNIFTY",Segment.FUT)