Package: tsetmc
AnasyncPython library to fetch data fromhttp://tsetmc.com.Note: The API is provisional and may change rapidly without deprecation.InstallationRequires Python 3.12 or later.pip install tsetmcOverviewLet’s start with a simple script:importasynciofromtsetmc.instrumentsimportInstrumentasyncdefmain():inst=awaitInstrument.from_l18('فملی')info=awaitinst.info()print(info)asyncio.run(main())TheInstrumentclass provides many methods for getting information about an instrument.
The following code blocks try to demonstrate some of its capabilities.Note: You need an asyncio capable REPL, likepython-masyncioorIPython, to run the following code samples, otherwise you’ll have to run them inside an async function like the sample code above.>>>fromtsetmc.instrumentsimportInstrument>>>inst=awaitInstrument.from_l18('فملی')>>>awaitinst.info(){'eps':{'epsValue':None,'estimatedEPS':'721','sectorPE':12.02,'psr':1472.8279},'sector':{'dEven':0,'cSecVal':'27 ','lSecVal':'فلزات اساسی'},'staticThreshold':{'insCode':None,'dEven':0,'hEven':0,'psGelStaMax':8270.0,'psGelStaMin':7190.0},'minWeek':7630.0,'maxWeek':7970.0,'minYear':4630.0,'maxYear':10670.0,'qTotTran5JAvg':179233329.0,'kAjCapValCpsIdx':'43','dEven':0,'topInst':1,'faraDesc':'','contractSize':0,'nav':0.0,'underSupervision':0,'cValMne':None,'lVal18':'S*I. N. C. Ind.','cSocCSAC':None,'lSoc30':None,'yMarNSC':None,'yVal':'300','insCode':'35425587644337450','lVal30':'ملی\u200cصنایع\u200cمس\u200cایران\u200c','lVal18AFC':'فملی','flow':1,'cIsin':'IRO1MSMI0000','zTitad':600000000000.0,'baseVol':15584416,'instrumentID':'IRO1MSMI0001','cgrValCot':'N1','cComVal':'1','lastDate':0,'sourceID':0,'flowTitle':'بازار بورس','cgrValCotTitle':'بازار اول (تابلوی اصلی) بورس'}Getting the latest price information:>>>awaitinst.closing_price_info(){'instrumentState':{'idn':0,'dEven':0,'hEven':0,'insCode':None,'cEtaval':'A ','realHeven':0,'underSupervision':0,'cEtavalTitle':'مجاز'},'instrument':None,'lastHEven':170725,'finalLastDate':20230524,'nvt':0.0,'mop':0,'thirtyDayClosingHistory':None,'priceChange':0.0,'priceMin':7630.0,'priceMax':7900.0,'priceYesterday':7730.0,'priceFirst':7750.0,'last':True,'id':0,'insCode':'0','dEven':20230524,'hEven':170725,'pClosing':7700.0,'iClose':False,'yClose':False,'pDrCotVal':7670.0,'zTotTran':7206.0,'qTotTran5J':84108817.0,'qTotCap':648015842640.0}Getting the daily trade history for the last n days: (as a DataFrame)>>>awaitinst.daily_closing_price(n=2)priceChangepriceMinpriceMax...zTotTranqTotTran5JqTotCap030.07490.07600.0...4555.075649965.05.689944e+11110.07500.07590.0...4614.083570336.06.276337e+11[2rowsx17columns]Getting adjusted daily prices:>>>awaitinst.price_history(adjusted=True)pmaxpminpfpltvolpcdate2007-02-0445414542172898994422007-02-054343434310826496432007-02-064444444426850133442007-02-074545454531086849452007-02-10454545454064552845..................2021-07-1213340128401311012860106208763130202021-07-131301012640128401268066812306127702021-07-141283012450125401269070277940126702021-07-171296012550128001264068542961127502021-07-18128801253012600126308810616212650[3192rowsx6columns]Getting intraday data for a specific date:>>>awaitinst.on_date(20210704).states()# a dataframe:idndEvenhEveninsCodecEtavalrealHevenunderSupervisioncEtavalTitle00010A948380NoneSearching for an instrument:>>>awaitInstrument.from_search('چادرملو')Instrument(18027801615184692,'کچاد')Theinstruments.price_adjustmentsfunction gets all the price adjustments for a specified flow.Themarket_watchmodule contains several function to fetch market watch data. They include:market_watch_initmarket_watch_plusclosing_price_allclient_type_allkey_statsombud_messagesstatus_changesUsemarket_watch.MarketWatchfor watching the market. Here is how:fromasyncioimportnew_event_loopfromtsetmc.market_watchimportMarketWatchasyncdeflisten_to_update_events():whileTrue:awaitmarket_watch.update_event.wait()df=market_watch.dfprint(df.at['35425587644337450','pl'])# last price of فملیmarket_watch=MarketWatch()loop=new_event_loop()loop.create_task(listen_to_update_events())loop.run_until_complete(market_watch.start())There are many other functions and methods that are not covered here. Explore the codebase to learn more.If you are interested in other information available on tsetmc.com that this library has no API for, pleaseopen an issuefor them.See alsohttps://github.com/5j9/fipiran