Exemplo n.º 1
0
def login():
    kite = Zerodha()
    #kite.set_access_token()        
    kite.load_creds()
    kite.login()
    logging.info("Kite Login details")
    return kite
Exemplo n.º 2
0
    def start_live_feed(self):

        self.alice.start_websocket(subscribe_callback=self.get_ltp,
                                   socket_open_callback=self.open_callback,
                                   run_in_background=True)

        while (self.socket_opened == False):
            pass

        for key in self.ins_exc:

            self.alice.subscribe(
                self.alice.get_instrument_by_symbol(
                    self.ins_exc[key]['exchange'], key), LiveFeedType.COMPACT)

            kite = Zerodha()
            kite.load_creds()
            kite.login()

            if (key[-3:] == 'FUT'):

                ins_kite = self.ins_exc[key]['exchange'] + ':' + key.split(
                    ' ')[0] + str(datetime.date.today().year)[2:] + key.split(
                        ' ')[1] + key.split(' ')[2]
                ins_kite_number = kite.quote(
                    ins_kite)[ins_kite]['instrument_token']
                from_date = datetime.date.today() - datetime.timedelta(days=5)
                to_date = datetime.date.today()
                interval = "minute"
                self.data[key] = pd.DataFrame(
                    kite.historical_data(ins_kite_number,
                                         from_date,
                                         to_date,
                                         interval,
                                         continuous=False,
                                         oi=False))

            else:

                ins_kite = self.ins_exc[key]['exchange'] + ':' + key
                ins_kite_number = kite.quote(
                    ins_kite)[ins_kite]['instrument_token']
                from_date = datetime.date.today() - datetime.timedelta(days=5)
                to_date = datetime.date.today()
                interval = "minute"
                self.data[key] = pd.DataFrame(
                    kite.historical_data(ins_kite_number,
                                         from_date,
                                         to_date,
                                         interval,
                                         continuous=False,
                                         oi=False))

            self.data[key]['datetime'] = self.data[key]['date']
            self.data[key]['ticker'] = key

            self.data[key]['date'] = self.data[key]['datetime'].apply(
                lambda x: x.date())
            self.data[key]['time'] = self.data[key]['datetime'].apply(
                lambda x: x.time())

            self.data[key] = self.data[key][[
                'datetime', 'ticker', 'open', 'high', 'low', 'close', 'volume',
                'date', 'time'
            ]]
            self.data[key]['datetime'] = self.data[key]['date'].apply(
                str) + self.data[key]['time'].apply(str)
            self.data[key]['datetime'] = self.data[key]['datetime'].apply(
                lambda x: datetime.datetime.strptime(x, '%Y-%m-%d%H:%M:%S'))
            self.data[key].set_index('datetime', inplace=True)
Exemplo n.º 3
0
    def start_live_feed(self):
    
        # Start Alice Blue Websocket and set subscribe callback to get_ltp class method and 
        # socket open callback to open_callback class method
        self.alice.start_websocket(subscribe_callback=self.get_ltp,
                          socket_open_callback=self.open_callback,
                          run_in_background=True)

        # Wait and do nothing until socket_opened turns True
        while(self.socket_opened==False):
            pass

        # Create a Zerodha object to get historical data using Jugaad Trader
        kite = Zerodha()
        # Automatically load pre-stored credentials
        kite.load_creds()
        # Login to Zerodha with loaded credentials
        kite.login()
        
        # Iterate over the keys in instrument_exchange dictionary, which are the instrument names
        for key in self.instrument_exchange:
        
            # Subscribe the particular instrument to alice blue by passing the instrument symbol name and exchange name
            self.alice.subscribe(self.alice.get_instrument_by_symbol(self.instrument_exchange[key]['exchange'], key), LiveFeedType.COMPACT)

            # Check if the particular instrument is Futures
            if (key[-3:] == 'FUT'):

                # Store the particular instrument in the form Zerodha can extract its token number
                ins_kite = self.instrument_exchange[key]['exchange'] + ':' + key.split(' ')[0] + str(datetime.date.today().year)[2:] + key.split(' ')[1] + key.split(' ')[2]
                
                # Extract the instrument's token number using quote method from Zerodha object
                ins_kite_number = kite.quote(ins_kite)[ins_kite]['instrument_token']
            
            # If the instrument is not Futures
            else:

                # Store the particular instrument in the form Zerodha can extract its token number
                ins_kite = self.instrument_exchange[key]['exchange'] + ':' + key
                
                # Extract the instrument's token number using quote method from Zerodha object
                ins_kite_number = kite.quote(ins_kite)[ins_kite]['instrument_token']
                
            # Define date from when you want historical data
            # in this case, it is from 5 days ago
            from_date = datetime.date.today() - datetime.timedelta(days = 5)

            # Define date till when you want historical data
            # in this case, it is until the current time
            to_date = datetime.date.today()

            # Define timeframe for the historical data's frequency
            interval = "minute"

            # Save historical data to class attribute data and append live resampled data to this eventually
            self.data[key] = pd.DataFrame(kite.historical_data(ins_kite_number, from_date, to_date, interval, continuous=False, oi=False))
   
            ### Clean the Zerodha historical data to match its format to AliceBlue live data ###
    
            # Create datetime column which is equal to the Zerodha historical data's date column
            self.data[key]['datetime'] = self.data[key]['date']
            
            # Create new column ticker to store symbol name
            self.data[key]['ticker'] = key

            # Reassign date column to the date extracted from datetime column
            self.data[key]['date'] = self.data[key]['datetime'].apply(lambda x : x.date())
            
            # Create time column to the time extracted from datetime column
            self.data[key]['time'] = self.data[key]['datetime'].apply(lambda x : x.time())

            # Reorder column names in the dataframe
            self.data[key] = self.data[key][['datetime','ticker','open','high','low','close','volume','date','time']]
            
            # Reassign datetime column as a combined string form of date and time from the respective columns
            self.data[key]['datetime'] = self.data[key]['date'].apply(str) + self.data[key]['time'].apply(str)
            
            # Set datetime column to datetime format using strptime method
            self.data[key]['datetime'] = self.data[key]['datetime'].apply(lambda x : datetime.datetime.strptime(x, '%Y-%m-%d%H:%M:%S'))
            
            # Set datetime column as index
            self.data[key].set_index('datetime', inplace=True)
Exemplo n.º 4
0
# -----------------------------------------------------------
### Save Credentials ###

# $ jtrader zerodha savecreds
# Saves your creds in app config folder in file named .zcred
# User ID >: USERID
# Password >:
# Pin >:
# Saved credentials successfully

# Once you have done this, you can call load_creds followed by login.

from jugaad_trader import Zerodha
kite = Zerodha()
kite.load_creds()
kite.login()
print(kite.profile())

# -----------------------------------------------------------

# -----------------------------------------------------------
### Config Directory ###

# $ jtrader zerodha configdir

###### Delete configuration ######

### To delete SESSION ###

# $ jtrader zerodha rm SESSION