Giter Site home page Giter Site logo

zerodhaatom's Introduction

Web Automation Based api Algo Trading for Broker Zerodha

This is python based library created for simple algo trading, It is developed using selenium. It helps for automatic data collection from Zerotha Official Website

These API can be user to place order in live market.

These api can be used to collect the data from website and save them in xml file and used for backtest.

  • Author: Harpal
  • These are unoffical api just created for fun.

Prerequisites

Python 3.x

Also, you need the following modules:

  • selenium
  • chromedriver
  • websocket_client
  • requests
  • bs4

You can clone this repo and install all preraquisites

Getting started with API

Overview

There are three public class Zerodha, StockDataLogger and MarketSimulatore. You can create instance of these class. For creating instance of Zerodha you need to pass Zeroda's credential as argument in constructor. StockDataLogger is used to save live data in xml files and MarkerSimulator class is used to backtest your stratergy using data saved in xml files.

Getting Started

  1. Create a dictonary contaning the credential and watch list no. that you want to subscibe for trading.
creds = {	
          "trading":True,  
          "usr":"AB2111",
          "pswd":"Password@123",
          "pin":"124346",
          "trade_watchlist":1,
          "email":"[email protected]"
        }
  1. Login and Subscibe: Create instance of Zerodha using credential. ZC Call is used to define the Constant Value
  • ZC.MODE_DEPTH_20 :- market depth 20 data
  • ZC.MODE_DEPTH_5 :- market depth 5 data
  • ZC.MODE_LTP :- Last Trading Price data
from Brokers.Zerodha import ZC, ZerodhaConnect
z = ZerodhaConnect(usr=creds, headless = False)
#headless false means browser will open in backround
z.subscribe(time_interval = 1,mode = ZC.MODE_DEPTH_5)
  1. Define a method where you implement your trading stratergy and register this methon with zerodha instance
def on_ticks(ticks):
    print(ticks)
    #Add Stratergy Here 
z.on_ticks=on_ticks

This tick data contains list of ticks in following example format.

    [{'timestamp': datetime.datetime(2021, 2, 14, 12, 26, 35), 'symbol': 'TCS', 'exchange': 'NSE', 'holdings': None, 'ltp': 3190.8, 'change': None, 'Prev. Close': '3206.00', 'Volume': 'NA', 'Avg. price': 'NA', 'LTQ': 'NA', 'LTT': '2021-02-12 15:58:42', 'Lower circuit': '2871.75', 'Upper circuit': '3509.85', 'total_bids': '0', 'total_offers': '0'}]
      
  1. Place Order:-
place_order(self,symbol = "IRCTC",
            exchange= 'NSE', 
            product = ZC.PRODUCT_TYPE_CNC,
            transaction_type =ZC.TRANSACTION_TYPE_BUY,
            order_type = ZC.ORDER_TYPE_MARKET,
            price = None, 
            qtn = 1 ):

Following Constant are define in ZC Class for placing order

transaction_type

  • ZC.TRANSACTION_TYPE_BUY :- Buy Order
  • ZC.TRANSACTION_TYPE_SELL :- Sell Order

product

  • ZC.PRODUCT_TYPE_CNC :- "Cash and carry. Delivery based trades"
  • ZC.PRODUCT_TYPE_MIS = "Intraday squareoff with extra leverage"

order_type

  • ZC.ORDER_TYPE_MARKET = "Market Order"
  • ZC.ORDER_TYPE_LIMIT = "Limit Order"

Price is kept none in case of market order and have some value in case of limit order.

  1. Get Margins
z.get_margin()

# This method return following example output format
{'equity': {'Available margin': 1465.21,
  'Used margin': 0.0,
  'Available cash': 1465.21,
  'Opening balance': 1465.21,
  'Payin': 0.0,
  'SPAN': 0.0,
  'Delivery margin': 0.0,
  'Exposure': 0.0,
  'Options premium': 0.0,
  'Collateral (Liquid funds)': 0.0,
  'Collateral (Equity)': 0.0,
  'Total collateral': 0.0},
 'commodity': {'Available margin': 0.01,
  'Used margin': 0.0,
  'Available cash': 0.01,
  'Opening balance': 0.01,
  'Payin': 0.0,
  'SPAN': 0.0,
  'Delivery margin': 0.0,
  'Exposure': 0.0,
  'Options premium': 0.0}}
  1. Get Holdings
z.get_holdings()
#Return Folloing example output format
{'AVANTIFEED': {'Instrument': 'AVANTIFEED',
  'Qty.': '25',
  'Avg. cost': '483.34',
  'LTP': '499.15',
  'Cur. val': '12,478.75',
  'P&L': '395.20',
  'Net chg.': '+3.27%',
  'Day chg.': '-1.30%'},
 'BPCL EVENT': {'Instrument': 'BPCL EVENT',
  'Qty.': '15',
  'Avg. cost': '378.43',
  'LTP': '418.35',
  'Cur. val': '6,275.25',
  'P&L': '598.75',
  'Net chg.': '+10.55%',
  'Day chg.': '0.00%'}}
  1. Save Data in xml file
from Loggers.StockDataLogger import StockLogger
HsLogger = StockLogger(base_path ="path_for_data_to_be_saved",chunk_size = 100)
z.set_logger(hslogger=HsLogger)

8 Market Simulator: Data Should be avalble for simulator that is saved by HsSimlator.

from Brokers.MarketSimulator import TickSimulator
stocks =['SBICARD','TITAN','AARTIIND','IDEA', 'IRCTC','LT','SETFGOLD','PRINCEPIPE']
tick_sim = TickSimulator(time_interval = 1, usr = 'PAPER_TRADING')
tick_sim.subscribe(tickers = stocks, last_n_days = 60)
def on_ticks(ticks):
    print(ticks)
    #Add Stratergy Here 

tick_sim.on_ticks=on_ticks

Marker Simlator class will exactly behave real market only difference is that no real money and no real order will be place.This class not required broker accound but required data to be availble in xml format

zerodhaatom's People

Contributors

harpalnain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zerodhaatom's Issues

Error on running the code

When i am trying to run the Zerodha atom project it is giving the following error message. Please help. Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@data-balloon='Market Depth (D)']"}

Autofocus error

Getting error : autofocus processing was blocked because a document already has a focused element
DevTools listening on ws://127.0.0.1:55034/devtools/browser/b12eb5f2-0edc-4174-9
829-09e587d49ff3
[0607/010244.725:ERROR:mf_helpers.cc(14)] Error in dxva_video_decode_accelerator
_win.cc on line 515
[0607/010247.462:INFO:CONSOLE(0)] "Autofocus processing was blocked because a do
cument already has a focused element.", source: https://"masked the url before posting issue for privacy"/ (0)
Traceback (most recent call last):
File "ZerodhaAutomationExample.py", line 103, in
z.subscribe(wlist_index = 1,time_interval = 1,mode = ZC.MODE_DEPTH_5)
File "C:\Users\user\Desktop\ZerodhaAtom-master\ZerodhaAtom-master\ZerodhaAtom.
py", line 137, in subscribe
action.find_element_by_xpath("//button[@data-balloon='Market Depth (D)']").c
lick()
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\s
elenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\s
elenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\s
elenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\s
elenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale elemen
t reference: element is not attached to the page document
(Session info: headless chrome=81.0.4044.113)

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.