GithubHelp home page GithubHelp logo

praveen686 / algo-trading-zerodha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thisisrajatnagpal/algo-trading

0.0 0.0 0.0 983 KB

This repository contains the code snippets used for trading with Zerodha KiteConnect API using python

Python 100.00%

algo-trading-zerodha's Introduction

Algo-Trading

This repository contains the code snippets used for trading with Zerodha KiteConnect API using python

A Hub of necessary python functions for algorithmic trading

Key FeaturesHow To UseDownloadCreditsRelatedLicense

How To Use

To use the functions, you'll need to write the following code beforehand.

from kiteconnect import KiteConnect
import logging
import os
import datetime as dt
import pandas as pd

cwd = os.chdir("Current Working Directory")

# generate trading sesion
# Please go to Gen Access folder to get these two files as input
access_token = open("access_token.txt", 'r').read()
key_secret = open("api_key.txt", 'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
kite.set_access_token(access_token)
                 

# get dump of all NSE instruments
instrument_dump = kite.instruments("NSE")
instrument_df = pd.DataFrame(instrument_dump)
instrument_df.to_csv("NSE_Instruments.csv", index = False)

def instrumentLookup(instrument_df, symbol):
    try:
        return instrument_df[instrument_df['tradingsymbol']==symbol]['instrument_token'].values[0]
    except:
        return -1
    
def fetchOHLC(ticker, interval, from_date, to_date):
    """extracts historical data and outputs in the form of dataframe"""
    # ticker is the NSE instrument you want to use
    # interval is the candlestick time frame for OHLC data
    # The dates should be in this form - "day-month-Year"
    instrument = instrumentLookup(instrument_df, ticker)
    from_date = dt.datetime.strptime(from_date, '%d-%m-%Y')
    to_date = dt.datetime.strptime(to_date, '%d-%m-%Y')
    data = pd.DataFrame(columns=['date', 'open', 'high', 'low', 'close', 'volume'])
    while True:
        if from_date.date() >= dt.date.today()-dt.timedelta(100):        
            data = data.append(pd.DataFrame(kite.historical_data(instrument, from_date, to_date, interval)))
            break
        else:
            to_date = from_date + dt.timedelta(100)
            data = data.append(pd.DataFrame(kite.historical_data(instrument, from_date, to_date, interval)))
            from_date = to_date
    data.set_index("date", inplace=True)
    return data

algo-trading-zerodha's People

Contributors

thisisrajatnagpal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.