Extract the table data from charkink.com dashboard- python

I am trying to extract the first table data ie “Buy” under the chartink dashboard https://chartink.com/dashboard/94000? via web scraping of webpage on chartink.com

I had tried below code. But not able to get the data. Can some one help.

payload={
'query': 'select latest Close - 1 day ago Close / 1 day ago Close * 100 as '% chg', latest Volume as 'Volume' WHERE( {-1} ( latest open = latest low and [0] 5 minute close > [0] 5 minute vwap and [0] 5 minute close > latest "( (1 candle ago high + 1 candle ago low + 1 candle ago close / 3 ) * 2 - 1 candle ago low )" ) ) GROUP BY symbol ORDER BY 1 desc'
}

inputurl="https://chartink.com/dashboard/94000"
with requests.Session() as s:
    r = s.get(inputurl)
    print(r)
    soup = bs(r.content, 'lxml')
    s.headers['X-CSRF-TOKEN'] = soup.select_one('[name=csrf-token]')['content']
    r = s.post('https://chartink.com/widget/process', data=payload).json()
    print(r)

getting the error as “SyntaxError: invalid syntax”. its around ‘% chg’ and as ‘Volume’ WHERE area. but not able to rectify and proceed further.

Aim is to get the data returned from this query as a dataframe.

SyntaxError: invalid syntax

Indeed, you have to reformat the query string. Replace '...' with """...""":

payload = {
    "query": """select latest Close - 1 day ago Close / 1 day ago Close * 100 as '% chg', latest Volume as 'Volume' WHERE( {-1} ( latest open = latest low and [0] 5 minute close > [0] 5 minute vwap and [0] 5 minute close > latest "( (1 candle ago high + 1 candle ago low + 1 candle ago close / 3 ) * 2 - 1 candle ago low )" ) ) GROUP BY symbol ORDER BY 1 desc"""
}

# The rest of your code

Output:

>>> r
{'metaData': [{'columnAliases': ['% chg', 'volume'],
   'availableLimit': 0,
   'maxRows': 10,
   'isTrend': True,
   'limit': 75,
   'groups': ['symbol'],
   'tradeTimes': [1704825000000,
    1704911400000,
    1704997800000,
    1705257000000,
    1705343400000,
    1705429800000,
    1705516200000,
    1705602600000,
    1705689000000,
    1705948200000],
   'lastUpdateTime': 1705994280000}],
 'groups': [],
 'time': 25,
 'groupData': []}

However, it appears that your request is not returning any results.

Example for another request:

ts = pd.to_datetime(r['metaData'][0]['tradeTimes'], unit="ms")
cols = r['metaData'][0]['columnAliases']

records = {}
for item in r['groupData']:
    name = item['name']
    data = [pd.Series(item['results'][i][c], index=ts, name=c)
            for i, c in enumerate(cols)]
    records[name] = pd.concat(data, axis=1)
df = pd.concat(records, axis=1)

Output:

>>> df
                        BRNL              TIL  ...       SUVIDHAA       ESSENTIA               
                       % chg   volume   % chg  ...         volume          % chg         volume
2024-01-09 18:30:00   0.3894   223090  0.3979  ...   4.889600e+04  -4.665600e+00   3.761834e+07
2024-01-10 18:30:00  -0.6206   179725  0.7133  ...   1.441100e+05   4.404600e+00   1.270597e+06
2024-01-11 18:30:00  -2.0297   144481 -0.4722  ...   7.331600e+04   4.687500e+00   1.321922e+06
2024-01-14 18:30:00   2.0717   227678  0.0264  ...   5.702200e+04   4.477600e+00   1.245907e+06
2024-01-15 18:30:00   2.5761   664972 -3.2806  ...   1.281630e+05   5.000000e+00   2.174844e+07
2024-01-16 18:30:00  -2.1309   185063  0.9944  ...   2.905400e+04   4.761900e+00   1.051405e+07
2024-01-17 18:30:00  -0.5443   215660  4.9906  ...   6.092800e+04  -1.948000e+00   3.537970e+06
2024-01-18 18:30:00   1.8765   184217  4.9974  ...   7.002500e+04  -1.986800e+00   1.957707e+06
2024-01-19 18:30:00  19.2632  3852825  2.2880  ...   5.758600e+04  -2.027000e+00   1.571878e+06
2024-01-22 18:30:00  14.5431  4056686  5.0000  ...  1.700000e+308  1.700000e+308  1.700000e+308

[10 rows x 82 columns]

Leave a Comment