Package: polygon
polygon: A Complete Polygon.io API Wrapperwhat ispolygonpolygonis a Complete Python Wrapper forPolygon.io APIs. It offers
simple and elegant programmatic access over each endpoint. Functionalities include but not limited to:Stocks and Options dataForex and Crypto dataReal time Websocket streaming (both callback and async based)Technical Indicators (SMA, EMA, RSI, MACD)Market Info, News, Holidays, Schedules, Tickers, Conditions, Dividends, SplitsAsync support for REST endpointsBuilt In stream reconnection functionality (Experimental, async streamer only)Full builtin Pagination support (with internal merging of responses)Bulkdata download functions: bulk OCHLV aggregates bars, bulk ticker details etc.Extensive Option Symbology supporting 6 option symbol formatsand a lot more...How Do I UsepolygonThe complete description of everything you need to know is available in theDocumentationwhich has answers to
any question you might have with example uses included wherever needed. Docs are a must-read for most people.For examples to get started with, see theexamplesFor an advanced use case example code involving redis queue and postgresql database, check outMarketMakerLite/polygonThe lib is also officially supported by the popular TA librarypandas-taas an underlying data source instead of the defaultyfinance. See relevant docs to know how to useTo see an advanced use case of downloading ALL OCHLV data for ALL tickers for 20+ years and write it to a
timescale enabled postgresql database efficiently, seeThis ScriptbyMML.Thedocumentationalso contain TONS of sample
code snippets wherever necessary.Quick Setup Guide With ExamplesBefore you do anything, you'll need to have a polygon account and get your API key.
VisitYour Dashboardto get yours.Next, you'd need to installpolygonpipinstallpolygonand You're good to Go!Here are a few quick usage examples.Getting a Stock's Previous Day OCHLVimportpolygonapi_key='YOUR_KEY'stocks_client=polygon.StocksClient(api_key)previous_close=stocks_client.get_previous_close('AMD')print(previous_close)An Async Example for REST endpoints - Previous Closeimportpolygonimportasyncioasyncdefmain():api_key='YOUR_KEY'stocks_client=polygon.StocksClient(api_key,True)previous_close=awaitstocks_client.get_previous_close('AMD')awaitstocks_client.close()# Recommended to close the httpx session when it's not needed.print(previous_close)if__name__=='__main__':asyncio.run(main())A Streaming Example (Callback Based)importpolygonfrompolygon.enumsimportStreamClusterdefmy_own_message_handler(ws,msg):print(f'msg received:{msg}')defmain():api_key='YOUR_KEY'stream_client=polygon.StreamClient(api_key,StreamCluster.STOCKS,on_message=my_own_message_handler)stream_client.start_stream_thread()stream_client.subscribe_stock_trades(['AMD','NVDA'])if__name__=='__main__':main()An Async Streaming Exampleimportasyncioimportpolygonfrompolygon.enumsimportStreamClusterasyncdefstock_trades_handler(msg):# it is possible to create one common message handler for different services.print(f'msg received:{msg}')asyncdefmain():api_key='YOUR_KEY'stream_client=polygon.AsyncStreamClient(api_key,StreamCluster.STOCKS)awaitstream_client.subscribe_stock_trades(['AMD','NVDA'],stock_trades_handler)while1:awaitstream_client.handle_messages()# the lib provides auto reconnect functionality. See docs for infoif__name__=='__main__':asyncio.run(main())This only scratches the surface of the library.See theDocumentationto start using the library with its full functionalities.Latest development source code of the library can be found on thedevelopment branchWhat if I need help?We have a helpful & vibrant community in ourDiscord Server. Join in to ask a
question, share your ideas or observations or to just chat with interesting people, or maybe just for lurking :eyes:SeeGetting Helpor you can alsostart a quick discussionQuick Links forSpeed RunnersGetting Started- a must-read for almost everyoneAsync support for HTTP endpointsBulk Data Download FunctionsPagination Support||Option SymbologyStocks||Options||Reference APIsForex & CryptoCallback Streaming||Async StreamingEasy guide to enums||Library Interface Docsbugs, discussions, wikis and FAQsWhat else?Bug reports, suggestions and pull requests are always welcome.SeeContributingif you wish to contribute.ReadThisbefore raising a bug.CHANGELOGfor the project is available within the same repositorypolygonis released under theMIT LicenseMade with Passion & Python byP S Solanki