Skip to content

Algo Trading

Supercharge Your Algo Trading with Chartink Screener Extractor

15 March 20257 min readAlgo Trading
FabTrader author portrait

FabTrader

A skilled mechanic depends on the right tools to diagnose and fix a car efficiently. Similarly, an algo trader needs a powerful set of tools to build, refine, and execute trading strategies effectively. One such indispensable tool is the Chartink Screener Python Extractor Utility – a Python-based module that allows traders to extract stock screening results from the Chartink Scanner website with ease.

Why Use the Chartink Screener Python Extractor?

As an algo trader, you often spend countless hours building automated strategies, gathering stock data, and backtesting setups. The traditional approach involves sourcing historical data, calculating indicators, and then implementing the strategy logic. However, this process can be cumbersome and resource-intensive.

With Chartink, traders can define their screening conditions directly on the platform, and the scanner does the heavy lifting by fetching live results. The Python Extractor Utility takes it a step further by automating the extraction of these results, enabling seamless execution of trading strategies without unnecessary overhead.

How the Utility Works

The Chartink Screener Python Extractor Utility requires just two simple inputs:

  1. Chartink Screen URL: The URL of the scanner that contains your trading logic.
  2. Scan Clause: This is the payload request that Chartink sends when executing a scan. It can be obtained by inspecting the Chartink webpage and navigating to the 'Network' tab under Developer Tools.

Using the Python module, you can extract and process these results programmatically, allowing for rapid execution of trades based on the scanner output.

https://youtu.be/PpDN1eks-Bo
#
"""
Fetch Chartink Scanner results from Chartink.com website using python

Input:
1. Chartink scan URL
2. Scan Clause (Inspect webpage and find this under 'Network' / 'process' / 'Payload' section)

-- Dependencies to be installed --
pip install requests
pip install pandas
pip install beautifulsoup4

Disclaimer:
The information provided is for educational and informational purposes only and
should not be construed as financial, investment, or legal advice. The content is based on publicly available
information and personal opinions and may not be suitable for all investors. Investing involves risks,
including the loss of principal.

"""

# Import all dependencies
import requests
import pandas as pd
from bs4 import BeautifulSoup as bs
import time


def chartink_scraper(url, scan_clause):
    # Retrieves the results of Chartink scanner. Input is the url and the scan clause
    # Example of scan clause : {'scan_clause': '( {33619} ( latest close > latest sma( latest close , 200 )
    # and latest close > latest sma( latest close , 100 ) and latest close > latest sma( latest close , 50 ) )'}
    # Scan clause can be obtained by doing an 'inspect' on the page and under 'Network' / 'process' / 'Payload'

    df = pd.DataFrame()

    try:
        with requests.Session() as s:
            r = s.get(url)
            soup = bs(r.text, "html.parser")
            csrf = soup.select_one("[name='csrf-token']")['content']
            s.headers['x-csrf-token'] = csrf
            s.headers['Content-Type'] = 'application/x-www-form-urlencoded'
            r = s.post('https://chartink.com/screener/process', data=scan_clause)
            df = pd.DataFrame().from_dict(r.json()['data'])
        return df
    except requests.exceptions.HTTPError as e:
        print("Error in network connection")
    except requests.exceptions.RequestException as e:
        print("Error extracting data from website: ", e)

if __name__ == "__main__":

    # Extract Chartink scan results into a dataframe
    # Sometimes extract fails temporarily due to network congestion. The loop below will try thrice before giving up
    try_again = 0
    smart_money_stocks = []
    while try_again <= 3:  # If the chartink extract fails, try for a couple of times
        scan_clause = {
            'scan_clause': '( {57960} ( ( {57960} ( latest sma( latest close , 5 ) '
                           '< latest close * 1.03 and latest sma( latest close , 20 ) '
                           '< latest close * 1.03 and latest sma( latest close , 50 ) '
                           '< latest close * 1.03 and latest sma( latest close , 100 ) '
                           '< latest close * 1.03 and latest sma( latest close , 200 ) '
                           '< latest close * 1.03 and latest sma( latest close , 5 ) '
                           '> latest close * 0.97 and latest sma( latest close , 20 ) '
                           '> latest close * 0.97 and latest sma( latest close , 50 ) '
                           '> latest close * 0.97 and latest sma( latest close , 100 ) '
                           '> latest close * 0.97 and latest sma( latest close , 200 ) '
                           '> latest close * 0.97 ) ) ) )'}
        url = 'https://chartink.com/screener/consolidatedbo'
        df = chartink_scraper(url, scan_clause)
        print(df)
        stocks_list = df['nsecode'].tolist()
        print(stocks_list)
        print("Extract Successful!")
        if df.empty:
            print("Extract failed. Going to try again...")
            time.sleep(10)
            try_again += 1
        else:
            break
#

More from Algo Trading