Read time out error. I am getting this error frequently while using the kiteconnect to automate my straddle strategy. I am pasting my code here. Can anyone help?

import requests

expiry = '21JUL'
acc_token = 'abcd'

import time as t
import datetime
import random
import logging
from kiteconnect import KiteConnect
from pprint import pprint

entry_time = datetime.datetime.now().replace(hour=9, minute=19, second=59, microsecond=0)
exit_time = datetime.datetime.now().replace(hour=15, minute=00, second=0, microsecond=0)

# Initialise
kite = KiteConnect(api_key="abcd")
kite.set_access_token(acc_token)


def running_status():
    start_now = datetime.datetime.now().replace(hour=8, minute=15, second=0, microsecond=0)
    end_now = datetime.datetime.now().replace(hour=15, minute=30, second=0, microsecond=0)
    return start_now < datetime.datetime.now() < end_now


def place_entry(i):
    m = round(round(i), -2)
    p1 = 'BANKNIFTY' + expiry + str(m) + 'CE'
    p2 = 'BANKNIFTY' + expiry + str(m) + 'PE'
    global bn1
    global a1
    global a2
    bn1 = m

    kite.place_order(tradingsymbol=p1, variety='regular', exchange='NFO', transaction_type='SELL', quantity=25, order_type='MARKET', product='MIS')

    kite.place_order(tradingsymbol=p2, variety='regular', exchange='NFO', transaction_type='SELL', quantity=25, order_type='MARKET', product='MIS')

    print('order ENTRY at ', p1, ' and ', p2)
    a1 = m - 200
    a2 = m + 200
    print('Adjustments at ', a1, a2)


def place_exit(i):
    p1 = 'BANKNIFTY' + expiry + str(i) + 'CE'
    p2 = 'BANKNIFTY' + expiry + str(i) + 'PE'
    global bn2
    bn2 = i
    print('order EXIT at ', p1, ' and ', p2)
    kite.place_order(tradingsymbol=p1, variety='regular', exchange='NFO', transaction_type='BUY',
                     quantity=25, order_type='MARKET', product='MIS')
    kite.place_order(tradingsymbol=p2, variety='regular', exchange='NFO', transaction_type='BUY',
                     quantity=25, order_type='MARKET', product='MIS')


print('actual bn is ', kite.ltp('NSE:NIFTY BANK')['NSE:NIFTY BANK']['last_price'])

k = 1
b = 1
while running_status():

    print('This is loop ', b)
    if exit_time > datetime.datetime.now() > entry_time:  # Initial order
        print('Entry Order time requirement satisfied')
        bn = kite.ltp('NSE:NIFTY BANK')['NSE:NIFTY BANK']['last_price']
        print('Bank Nifty value at entry is ', bn)
        print('Taking first Entry order of the day')
        a1 = 34700
        a2 = 35100
        bn1 = 34900
        # place_entry(bn) #Place order entry, creates global bn1,a1,a2 for entry

        **while running_status()**:  # adjustment orders

            bn = kite.ltp('NSE:NIFTY BANK')['NSE:NIFTY BANK']['last_price']
            print('Bank Nifty is at ', bn, ' on ', datetime.datetime.now().strftime('%H:%M:%S'), ', Open order is at ',
                  bn1)

            try:
                if bn < a1:
                    print('Making Adjustment')
                    place_exit(bn1)
                    place_entry(bn)
                    print('Value of bn is ', bn, 'and the loop is ', k)
                    print('Exit ', bn1, 'Enter', round(round(bn, -2)))

                elif bn > a2:
                    print('Making Adjustment')
                    place_exit(bn1)  # Exit the entry order place at bn1
                    place_entry(bn)  # New Entry order at current bn price. creates new bn1 value with current bn
                    print('Value of bn is ', bn, 'and the loop is ', k)
                    print('Exit ', bn1, 'Enter', round(round(bn, -2)))

                elif datetime.datetime.now() > exit_time:
                    print('Trade time requirement not satisfied. Exiting all orders')
                    place_exit(bn1)
                    print('All order exit at ', bn1)

                k = k + 1

                if datetime.datetime.now() > exit_time:
                    print('Market End')
                    place_exit(bn1)
                    break

                t.sleep(2)


            except:
                continue
    t.sleep(5)
    b = b + 1

This is where I think am getting the error

while running_status(): # adjustment orders

        bn = kite.ltp('NSE:NIFTY BANK')['NSE:NIFTY BANK']['last_price']

Can anyone help?

I tried the try, except method, try, except ReadTimeOut: option, and things related to that but the error keeps coming

    try:
        #Add all the codes.
    except (exceptions.InputException, exceptions.TokenException):
        try:
           #ReLogin 
        except:
            logging.critical('Login Failed.')
    
    
    except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
        logging.critical('Kite Data Stream Disconnected.')
    
    except requests.exceptions.SSLError:
        logging.critical('Max retries exceeded with url.')
    
    except requests.exceptions.ReadTimeout:
        logging.critical('Kite is fucked.')
    
    break
    time.sleep(120)

Here You go.

It is working bro…thank you very much!!!

1 Like