shner-elmo / TradingView-Screener

A package that lets you create TradingView screeners in Python
https://shner-elmo.github.io/TradingView-Screener/2.5.0/tradingview_screener.html
MIT License
268 stars 43 forks source link

Options Data #40

Closed somethingbloodyfunky closed 3 months ago

somethingbloodyfunky commented 3 months ago

I'm trying to get options data for the exchange NSE. However if I do,

market = "options"
exchange = "NSE"
max_rows = 50000
#default_columns.remove('Market Capitalization')
(n_rows, df_) = Query().select(*default_columns).limit(max_rows).set_tickers('NSE:HDFCBANK').set_markets(market).get_scanner_data(cookies=cookies)

I get:

HTTPError: 400 Client Error: Bad Request
 Body: {"totalCount":0,"error":"required index \"underlying_symbol\" is missing from request","data":null}
 for url: https://scanner.tradingview.com/options/scan

Are options actually supported? If not, any tips on the correct HTTP request would be appreciated.

shner-elmo commented 3 months ago

So tradingview lists options as a market. So it definetly exists.

Now how do you access this data? I have yet to figure it out as there is no example on the website.

The error message says that you're missing the key underlying_symbol, which I'm not sure how you are supposed to pass it, but you can try to add it manually; q.query[key] = val.

If you figure it out let us know.

somethingbloodyfunky commented 3 months ago

Many thanks Shneor for this excellent utility and your prompt response. :) I've been trying to hack away it since then as well but haven't been successful thus far. However, once I am, I will certainly revert.

shner-elmo commented 3 months ago

Thanks for your kind words! I'm glad to hear that you're finding the utility useful.

As an example, I tried the following changes but I still get the same error.

image

Also, I'm pretty sure that they either have or are working on options data, because they have OPRA in the list of exchanges here, and there are other fields like: strike, expiration, and option-type.

shner-elmo commented 3 months ago

I will keep this issue open, in the hopes that somebody will find a way to access the data and share it, cheers

somethingbloodyfunky commented 3 months ago

Thanks so much for your efforts! I tried something along those lines too, but have been unsuccessful thus far.

Currently a search for AAPL does not list any options in TradingView any way. In order to facilitate testing your code, may I suggest that the Indian market, in particular the exchange NSE already has options data listed if you do a regular symbol search. E.g. NIFTY240725C24600 is available and it can be loaded just like any other chart.

shner-elmo commented 3 months ago

Btw there is actually another way that you can select a certain market, its done by setting the symbols.query.types key:

q = Query()
q.url = 'https://scanner.tradingview.com/global/scan'  # reset the URL to global
q.query['markets'] = []
q.query['symbols']['query']['types'] = ['forex']
q.get_scanner_data()
(6113,
                   ticker        name        close      volume market_cap_basic
 0        MOEX:CNYRUB_TOM  CNYRUB_TOM     11.97150  6119158000             None
 1        MOEX:KZTRUB_TOM  KZTRUB_TOM     18.49250  3240740000             None
 2        MOEX:AMDRUB_TOM  AMDRUB_TOM     22.99500   232340000             None
 3          FX_IDC:AUDITL      AUDITL   1188.98000     1067068             None
 4          FX_IDC:NZDITL      NZDITL   1068.93000     1067068             None
 ...
 45             FX:USDJPY      USDJPY    157.38100      353060             None
 46    PEPPERSTONE:USDJPY      USDJPY    157.52100      350233             None
 47       SKILLING:GBPJPY      GBPJPY    203.29800      264094             None
 48             FX:EURHUF      EURHUF    389.65600      137787             None
 49         FX_IDC:PENPTE      PENPTE     49.26230     1067111             None)

The list of valid values are (for now):

 ['fund',
  'stock',
  'dr',
  'spot',
  'swap',
  'futures',
  'structured',
  'index',
  'commodity',
  'option',
  'forex',
  'bond',
  'economic']

But when we select option we get an empty dataframe:

q = Query()
q.url = 'https://scanner.tradingview.com/global/scan'
q.query['markets'] = []
q.query['symbols']['query']['types'] = ['option']
q.get_scanner_data()
(0,
 Empty DataFrame
 Columns: [ticker, name, close, volume, market_cap_basic]
 Index: [])

And its the only type in the list mentioned above that returns an empty DF, this leads me to believe that they have no options data yet, but they might be working on it.

shner-elmo commented 3 months ago

Also if you do:

q = Query()
q.select('market')
q.limit(1_000_000)
q.url = 'https://scanner.tradingview.com/global/scan'
q.query['markets'] = []
_, df = q.get_scanner_data()

print(df['market'].value_counts().to_dict())
{'bond': 194590,
 'futures': 75443,
 'crypto': 42884,
 'germany': 20926,
 'america': 17758,
 'economics2': 14530,
 'canada': 7833,
 'uk': 7825,
 'india': 7031,
 'china': 6593,
 'forex': 6113,
 'japan': 4393,
 'korea': 3984,
 'italy': 3143,
 'mexico': 2771,
 'hongkong': 2735,
 'switzerland': 2532,
 'taiwan': 2495,
 'australia': 2255,
 'brazil': 2143,
 'thailand': 1811,
 'vietnam': 1332,
 'france': 1294,
 'sweden': 1218,
 'bonds': 1078,
 'malaysia': 1048,
 'israel': 1035,
 'austria': 944,
 'indonesia': 901,
 'coin': 804,
 'poland': 777,
 'netherlands': 653,
 'singapore': 612,
 'turkey': 596,
 'argentina': 554,
 'pakistan': 462,
 'cfd': 440,
 'bangladesh': 394,
 'rsa': 365,
 'ksa': 342,
 'norway': 331,
 'russia': 313,
 'luxembourg': 312,
 'philippines': 302,
 'srilanka': 288,
 'spain': 266,
 'egypt': 248,
 'romania': 232,
 'peru': 215,
 'finland': 192,
 'chile': 188,
 'denmark': 165,
 'newzealand': 164,
 'uae': 159,
 'greece': 151,
 'kuwait': 141,
 'nigeria': 130,
 'colombia': 125,
 'belgium': 119,
 'hungary': 80,
 'tunisia': 74,
 'morocco': 70,
 'czech': 57,
 'qatar': 54,
 'kenya': 54,
 'cyprus': 40,
 'portugal': 39,
 'venezuela': 33,
 'iceland': 31,
 'estonia': 31,
 'lithuania': 27,
 'serbia': 20,
 'bahrain': 19,
 'latvia': 11,
 'slovakia': 7}

You wont find any rows with the options market.

Rip, there really is no options data yet :(