Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

tse-client

Package Overview
Dependencies
7
Maintainers
1
Versions
128
Issues
File Explorer

Advanced tools

tse-client

A client for fetching stock data from the Tehran Stock Exchange (TSETMC). Works in Browser, Node and as CLI.

    2.17.4latest

Version published
Maintainers
1
Yearly downloads
5,868
decreased by-4.51%

Weekly downloads

Readme

Source

TSE Client GitHub tag GitHub issues

A client for fetching stock data from the Tehran Stock Exchange (TSETMC).
Works in Browser, Node, and as CLI.
The 0.x and 1.x versions were a direct port of the official Windows app.

Table of Contents

CLI

Install:

npm i tse-client -g

Basic:

tse ذوب tse ذوب فولاد خساپا شپنا tse "شاخص کل6" tse "شاخص کل فرابورس6" "شاخص کل (هم وزن)6"

Adjust prices:

tse آپ -j 1 # افزایش سرمایه + سود نقدی tse آپ -j 2 # افزایش سرمایه

Select columns:

tse فملی -c "2,3,4,6" tse فملی -c "2:OPEN 3:HIGH 4:LOW 6:CLOSE" tse فملی -c "0 6 8 7" tse فملی -c "0:date 6:closing 8:trades 7:volume" tse ls -A tse ls -D # default: "0 2 3 4 5 6 7 8 9"

History depth:

tse ذوب -b 3m # سه ماه گذشته tse ذوب -b 40d # چهل روز گذشته tse ذوب -b 2y # دو سال گذشته tse ذوب -b 13920101 # تاریخ شمسی tse ذوب -b 20160101 # تاریخ میلادی tse ذوب -b 13800101 # کمترین تاریخ ممکن (پیش فرض) tse ذوب -b 20030123 # کمترین تاریخ ممکن به میلادی

File generation:

tse کگل -o /mydir # output directory tse کگل -n 3 # file name tse کگل -x txt # file extension tse کگل -e ascii # file encoding tse کگل -l @ # file delimiter char tse کگل -H # file without headers

Select symbols from file:

tse ls -F "i=34" > car.txt tse ls -F "i=27" > iron.txt tse -i car.txt -o ./car-group tse -i iron.txt -o ./iron-group

Select symbols by filter:

# tse -f "t=نوع نماد i=کد گروه صنعت m=بازار b=کد تابلو y=کد بازار g=کد گروه نماد" # tse -f "P=الگوی نماد D=الگوی تاریخ میلادی آخرین روز معامله نماد R حذف نمادهای تغییرنام داده شده" tse -f "i=27" # گروه صنعت: فلزات اساسی tse -f "i=27 t=300" # گروه صنعت: فلزات اساسی & نوع نماد: سهم بورس tse -f "m=4 i=27,53,38 t=404" # بازار: پایه & گروه صنعت: فلزات و سیمان و قند & نوع نماد: حق تقدم tse -f "t=300 i=43 b=1" # بازار: بورس & گروه صنعت: دارویی & کد تابلو: تابلوی اصلی tse ls -T # نمایش کد انواع نماد tse ls -I # نمایش کد گروههای صنعت tse ls -B # نمایش کد تابلوها tse ls -T -I -M -B -Y -G # نمایش همه tse ls -F "i=27" # فقط نمایش دادن گروه فلزات tse ls -F "i=27 t=300" # گروه فلزات بازار بورس tse ls -F "i=27 t=303" # گروه فلزات بازار فرابورس tse ls -F "i=27 t=309" # گروه فلزات بازار پایه tse ls -F "i=34 t=300" # گروه خودرو بازار بورس tse ls -F "i=34 t=303" # گروه خودرو بازار فرابورس tse ls -F "i=34 t=309" # گروه خودرو بازار پایه tse ls -F "t=68" # شاخص های بازار بورس tse ls -F "t=69" # شاخص های بازار فرابورس tse ls -F "i=43 b=3 t=300" # گروه دارویی تابلوی فرعی بازار بورس tse ls -F "i=43 b=1 t=303" # گروه دارویی تابلوی اصلی بازار فرابورس tse ls -F "i=44 b=5 t=300" # گروه شیمیایی بازار دوم بورس tse ls -F "P=\d$" # نماد هایی که حرف آخرشان یک عدد (یک رقمی) است tse ls -F "t=309 P=\d$" # نمادهای بازار پایه که حرف آخرشان یک عدد (یک رقمی) است tse ls -F "D=^2022" # نمادهایی که روز آخری که معامله شده اند در سال 2022 بوده است tse ls -F "i=72 D=^2022" # نمادهای گروه رایانه که روز آخری که معامله شده اند در سال 2022 بوده است tse ls -F "i=57 t=303" # گروه بانکی بازار فرابورس tse ls -F "i=57 t=303 R" # گروه بانکی بازار فرابورس منهای نمادهای تغییرنام داده شده

Disable caching:

tse ذوب -k

Similar renamed symbols are merged by default:

tse ذوب # merged data of ذوب and ذوب-ق2 tse ذوب -u # only the data of ذوب tse ذوب-ق2 -u # only the data of ذوب-ق2

Save settings:

tse ذوب فولاد -o ./mytse --save tse tse -x txt tse -n 3 --save tse -o ./myother --save tse

View saved settings and more:

tse ls -S tse ls -D tse ls -F "i=34" tse ls -F "i=27 t=300" tse ls -M -T -I tse ls -T -O 1 # order by count (descending) tse ls -T -O 1_ # order by count (ascending) tse ls -h

Intraday crawler (Experimental):

tse itd ذوب -o ./mydata # basic (crawl last day) tse itd ذوب # update but do not generate output tse itd ذوب -b 6d # crawl last 6 days tse itd ذوب -b 30d -m 20d # crawl 10 days (from 30 days ago to 20 days ago) tse itd ذوب -b 13991201 -m 13991206 # crawl 6 days tse itd ذوب -b 20210219 -m 20210224 # crawl 6 days (Western dates) tse itd ذوب -k -o . # do not cache the data tse itd ذوب -z -o . # output gzip files tse itd ذوب -y -o . # generate results with Shamsi dates tse itd ذوب -r # re-download already cached days that have no `trade` data tse itd ذوب -o . -e ascii # file encoding tse itd ذوب -o . -n 2 # directory name tse itd ذوب -o . -H # file without headers tse itd ذوب --retry 0 # only request once without any retrying tse itd ذوب --retry-delay 5000 # wait 5 seconds between each retry tse itd ذوب --chunk-delay 2000 # wait 2 seconds before requesting another day tse itd ذوب --chunk-max-wait 20000 # force end a request if not finished in 20 seconds (only in Node v15+) tse itd ذوب --servers "4 8" # only use "cdn4.tsetmc.com" and "cdn8.tsetmc.com" servers tse itd ذوب --servers "7" # only use "cdn7.tsetmc.com" server

Node

Install:

npm i tse-client

Usage:

const tse = require('tse-client'); (async () => { // basic let res = await tse.getPrices(['ذوب', 'فولاد']); if (!res.error) console.log(res.data); // adjusted data res = await tse.getPrices(['خساپا'], {adjustPrices: 1}); // select columns (default names) res = await tse.getPrices(['شپنا'], {columns: [0,3,4]}); // select columns (custom names) res = await tse.getPrices(['شپنا'], {columns: [[0,'DATE'],[3,'MAX'],[4,'MIN']]}); // view column info console.table(tse.columnList); // list of instruments const instruments = await tse.getInstruments(); console.log( instruments.filter(i => i.YVal === '300' && i.CSecVal === '27 ') // گروه فلزات بازار بورس ); // intraday crawler res = await tse.getIntraday(['ذوب', 'فولاد']), { startDate: '20201122', endDate: '20201122', gzip: false }); })();

Browser

Using standalone bundle:

(bundled with the 4 dependencies)

<script src="https://cdn.jsdelivr.net/npm/tse-client/dist/tse.bundle.min.js"></script> <script> tse.getPrices(['فولاد']).then(res => console.log(res.data[0])); </script>

Using the module itself:

(dependencies must be loaded before)

<script src="https://cdn.jsdelivr.net/npm/big.js"></script> <script src="https://cdn.jsdelivr.net/npm/localforage"></script> <script src="https://cdn.jsdelivr.net/npm/jalaali-js/dist/jalaali.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/pako/dist/pako.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/tse-client/dist/tse.min.js"></script> <script> tse.getPrices(['فولاد']).then(res => console.log(res.data[0])); </script>

Using behind a simple PHP proxy:

// proxy.php <?php $t = isset($_GET['t']) ? $_GET['t'] : ''; $a = isset($_GET['a']) ? $_GET['a'] : ''; $a2 = isset($_GET['a2']) ? $_GET['a2'] : ''; echo file_get_contents("http://service.tsetmc.com/tsev2/data/TseClient2.aspx?t=$t&a=$a&a2=$a2"); tse.API_URL = 'http://path/to/proxy.php'; tse.getPrices(['فولاد']).then(res => console.log(res.data[0]));

Some Info:

filedesc
dist/tse.jsUnminified code.
dist/tse.min.jsMinified code.
dist/tse.bundle.min.jsMinified dependencies + minified code.
dependencydesc
big.jsRequired. For price adjustment calculations.
localforageRequired. For storing in indexedDB.
jalaali-jsOptional. Only needed for ShamsiDate column. (Recommanded to not exclude this, though you can)
pakoSemi-Required. Only used by getIntraday().

API

tse.API_URL

The API URL to use for HTTP requests.
Only string and valid URL.
Default: http://service.tsetmc.com/tsev2/data/TseClient2.aspx

tse.UPDATE_INTERVAL

Update data only if these many days have passed since the last update.
Only integers.
Default: 1

tse.PRICES_UPDATE_CHUNK

Amount of instruments per request.
Only integers.
Min: 1
Max: 59
Default: 50

tse.PRICES_UPDATE_CHUNK_DELAY

Amount of delay (in ms) to wait before requesting another chunk of instruments.
Default: 300

tse.PRICES_UPDATE_RETRY_COUNT

Amount of retry attempts before giving up.
Only integers.
Default: 3

tse.PRICES_UPDATE_RETRY_DELAY

Amount of delay (in ms) to wait before making another retry.
Only integers.
Default: 1000

tse.CACHE_DIR

Only in Node.
Location of the cache directory.
If the location is changed, existing content is not moved to the new location.
Default:   User's home directoy:require('os').homedir()

tse.getInstruments(struct?: boolean, arr?: boolean, structKey?: string)

Update (if needed) and return list of instruments.

  • struct: Determine the return type for each instrument. Default true
    • true: return an Instrument object for each instrument.
    • false: return a CSV string for each instrument.
  • arr: Determine the return type. Default: true
    • true: return an array.
    • false return an Instruments object.
  • structKey: Which key of Instrument to use when struct is set to true. Default: 'InsCode'

return: Array<Instrument | string> | Instruments

Visit the official documentation for description of each Instrument field.

interface Instrument { // 👇 C# equivalent InsCode: string; // long (int64) InstrumentID: string; // string LatinSymbol: string; // string LatinName: string; // string CompanyCode: string; // string Symbol: string; // string Name: string; // string CIsin: string; // string DEven: string; // int (int32) Flow: string; // byte LSoc30: string; // string CGdSVal: string; // string CGrValCot: string; // string YMarNSC: string; // string CComVal: string; // string CSecVal: string; // string CSoSecVal: string; // string YVal: string; // string SymbolOriginal?: string; } interface Instruments { [Instrument.InsCode]: Instrument | string; }

tse.getPrices(symbols: string[], settings?: PriceSettings)

Update (if needed) and return prices of instruments.

  • symbols: An array of Farsi instrument symbols.
  • settings: A settings object.
    • columns: Select which ClosingPrice props to return and specify optional string for the prop.
      For example: [ [0,'DATE'], [6,'CLOSE'], [7,'VOL'] ]
      Default: [0,2,3,4,5,6,7,8,9]
      See columnList for the list of all column indexes and their names.
    • adjustPrices: The type of adjustment applied to returned prices.
      0: None             (بدون تعدیل)
      1: Capital Increase + Dividends   (افزایش سرمایه + سود نقدی)
      2: Capital Increase        (افزایش سرمایه)
    • startDate: Only return prices after this date. Min: '20010321'. Default: '20010321'
    • daysWithoutTrade: Whether to include days that have 0 trades. Default: false
    • mergeSimilarSymbols: Whether to merge the data of similar renamed symbols. Default: true
    • cache: Whether to cache the downloaded data. Default: true
    • csv: Generate results as CSV strings. Default: false
    • csvHeaders: Include header row when generating CSV results. Default: true
    • csvDelimiter: A cell delimiter character to use when generating CSV results. Default: ','
    • onprogress: A callback function which gets called with a number indicating the progress. Default: undefined
    • progressTotal: A number to use as the completion point of progress. Default: 100

return: Result

interface Result { data: Array<ClosingPrice>; error?: CustomError; } interface ClosingPrice { // 👇 C# equivalent of array item date: number[]; // int (int32) dateshamsi: number[]; // int (int32) open: number[]; // decimal (float128) high: number[]; // decimal (float128) low: number[]; // decimal (float128) last: number[]; // decimal (float128) close: number[]; // decimal (float128) vol: number[]; // decimal (float128) count: number[]; // decimal (float128) value: number[]; // decimal (float128) yesterday: number[]; // decimal (float128) symbol: string[]; // string name: string[]; // string namelatin: string[]; // string companycode: string[]; // string } interface PriceSettings { columns?: Array<[number, string?]>; adjustPrices?: AdjustOption; daysWithoutTrade?: boolean; startDate?: string; mergeSimilarSymbols?: boolean; csv?: boolean; csvHeaders?: boolean; csvDelimiter?: string; cache?: boolean; onprogress?(current: number): void; progressTotal?: number; } interface CustomError { code: ErrorType; title: string; detail?: string | Error; symbols?: string[]; fails?: string[]; succs?: string[]; } enum AdjustOption { None = 0, CapitalIncreasePlusDividends = 1, CapitalIncrease = 2 } enum ErrorType { FailedRequest = 1, IncorrectSymbol = 2, IncompletePriceUpdate = 3 }

Example:

const defaultSettings = { columns: [0,2,3,4,5,6,7,8,9], adjustPrices: 0, daysWithoutTrade: false, startDate: '20010321', mergeSimilarSymbols: true, csv: false, csvHeaders: true, csvDelimiter: ',', cache: true, onprogress: undefined, progressTotal: 100 }; const result = await tse.getPrices(symbols=['sym1', 'sym2', ...], defaultSettings); result.data /* [ // sym1 { open: [0, 0, ...], high: [0, 0, ...], low: [0, 0, ...], last: [0, 0, ...], close: [0, 0, ...], vol: [0, 0, ...], count: [0, 0, ...], value: [0, 0, ...] }, // sym2 { open: [], high: [], ... }, ... ] */ result.error // possible values: undefined { code: 1, title: 'Failed request...', detail: '' | Error } { code: 2, title: 'Incorrect Symbol', symbols: [] } { code: 3, title: 'Incomplete Price Update', fails: [], succs: [] }

tse.columnList

A list of all possible columns.

indexnamefname
0dateتاریخ میلادی
1dateshamsiتاریخ شمسی
2openاولین قیمت
3highبیشترین قیمت
4lowکمترین قیمت
5lastآخرین قیمت
6closeقیمت پایانی
7volحجم معاملات
8countتعداد معاملات
9valueارزش معاملات
10yesterdayقیمت پایانی دیروز
11symbolنماد
12nameنام
13namelatinنام لاتین
14companycodeکد شرکت

tse.INTRADAY_URL

The URL from which the data is crawled.
Must be a function that returns a valid URL string.
Default:

(server='', inscode='', deven='') => `http://${server ? 'cdn'+server+'.' : ''}tsetmc.com/Loader.aspx?ParTree=15131P&i=${inscode}&d=${deven}`;

tse.INTRADAY_UPDATE_CHUNK_DELAY

Amount of delay (in ms) to wait before requesting another chunk of dates.
Default: 100

tse.INTRADAY_UPDATE_CHUNK_MAX_WAIT

Max time (in ms) to wait for a request to finish before force ending it.
In Node, it needs Node v15+ or it has no effect.
Only integers.
Default: 60000

tse.INTRADAY_UPDATE_RETRY_COUNT

Amount of retry attempts before giving up.
Only integers.
Default: 3

tse.INTRADAY_UPDATE_RETRY_DELAY

Amount of delay (in ms) to wait before making another retry.
Only integers.
Default: 1000

tse.INTRADAY_UPDATE_SERVERS

Array of positive integers to use as CDN servers in the update process.
Note that 0 will result to tsetmc.com server.
Only an array of only positive integers.
Default: [0,7,8,9]

tse.getIntradayInstruments(struct?: boolean, arr?: boolean, structKey?: string)

Returns the list of instruments crawled by getIntraday().
The information of each instrument is from the latest crawled day.

  • struct: Determine the return type for each instrument. Default true
    • true: return an InstrumentITD object for each instrument.
    • false: return a CSV string for each instrument.
  • arr: Determine the return type. Default: true
    • true: return an array.
    • false return an InstrumentsITD object.
  • structKey: Which key of InstrumentITD to use when struct is set to true. Default: 'InsCode'

return: Array<InstrumentITD | string> | InstrumentsITD

interface InstrumentITD { InsCode: string; LVal30: string; LVal18AFC: string; FlowTitle: string; CGrValCotTitle: string; Flow: string; CGrValCot: string; CIsin: string; InstrumentID: string; ZTitad: string; BaseVol: string; } interface InstrumentsITD { [InstrumentITD.InsCode]: InstrumentITD | string; }

tse.getIntraday(symbols: string[], settings?: IntradaySettings)

Crawl intraday data from the instrument's history page of the tsetmc.com website. (Experimental)

  • symbols: An array of Farsi instrument symbols.
  • settings: A settings object.
    • startDate: Only return data after this date. Min: '20010321'. Default: '20010321'
    • endDate: Only return data before this date. Default: ''
    • cache: Whether to cache the downloaded data. Default: true
    • gzip: Return data as Gzip Buffer in Node or Uint8Array in Browser. Default: true
    • reUpdateNoTrades: Re-download any cached item that has no trade data. Default: false
    • updateOnly: Perform update but don't return any data. Default: false
    • onprogress: A callback function which gets called with a number indicating the progress. Default: undefined
    • progressTotal: A number to use as the completion point of progress. Default: 100
    • chunkDelay: Per call option of INTRADAY_UPDATE_CHUNK_DELAY. Default: value of constant
    • chunkMaxWait: Per call option of INTRADAY_UPDATE_CHUNK_MAX_WAIT. Default: value of constant
    • retryCount: Per call option of INTRADAY_UPDATE_RETRY_COUNT. Default: value of constant
    • retryDelay: Per call option of INTRADAY_UPDATE_RETRY_DELAY. Default: value of constant
    • servers: Per call option of INTRADAY_UPDATE_SERVERS. Default: value of constant

return: Result

interface Result { data: Array< [number[], Array<Buffer | Uint8Array | string>] >; error?: CustomError; } interface IntradaySettings { startDate?: string; endDate?: string; cache?: boolean; gzip?: boolean; onprogress?(current: number): void; progressTotal?: number; chunkDelay?: number; chunkMaxWait?: number; retryCount?: number; retryDelay?: number; servers?: number[]; } interface CustomError { code: ErrorType; title: string; detail?: string | Error; symbols?: string[]; fails?: string[]; succs?: string[]; } enum ErrorType { FailedRequest = 1, IncorrectSymbol = 2, IncompletePriceUpdate = 3, IncompleteIntradayUpdate = 4 }

tse.itdGroupCols

A list of all intraday data groups and their column names.

price

timelastcloseopenhighlowcountvolumevaluediscarded
زمانآخرین معاملهقیمت پایانیاولینبیشترینکمترینتعداد معاملاتحجم معاملاتارزش معاملاتباطل شده

order

timerowaskcountaskvolaskpricebidpricebidvolbidcount
زمانردیفتعداد تقاضاحجم تقاضاقیمت تقاضاقیمت عرضهحجم عرضهتعداد عرضه

trade

timecountvolumepricediscarded
زمانتعدادحجمقیمتباطل شده

client

columndescdesc Fa
pbvolPerson Buy Volumeحجم خرید حقیقی
pbcountPerson Buy Countتعداد خرید حقیقی
pbvalPerson Buy Valueارزش خرید حقیقی
pbpricePerson Buy Priceقیمت خرید حقیقی
pbvolpotPerson Buy Volume Percentage of Totalدرصد حجم خرید حقیقی از کل حجم
.
psvolPerson Sell Volumeحجم فروش حقیقی
pscountPerson Sell Countتعداد فروش حقیقی
psvalPerson Sell Valueارزش فروش حقیقی
pspricePerson Sell Priceقیمت فروش حقیقی
psvolpotPerson Sell Volume Percentage of Totalدرصد حجم فروش حقیقی از کل حجم
.
lbvolLegal Buy Volumeحجم خرید حقوقی
lbcountLegal Buy Countتعداد خرید حقوقی
lbvalLegal Buy Valueارزش خرید حقوقی
lbpriceLegal Buy Priceقیمت خرید حقوقی
lbvolpotLegal Buy Volume Percentage of Totalدرصد حجم خرید حقوقی از کل حجم
.
lsvolLegal Sell Volumeحجم فروش حقوقی
lscountLegal Sell Countتعداد فروش حقوقی
lsvalLegal Sell Valueارزش فروش حقوقی
lspriceLegal Sell Priceقیمت فروش حقوقی
lsvolpotLegal Sell Volume Percentage of Totalدرصد حجم فروش حقوقی از کل حجم
.
lpchgLegal to Person Ownership Transferتغییر مالکیت حقوقی به حقیقی

misc

basevolflowdaymindaymaxstate
حجم مبنابازارکمینه قیمت مجازبیشینه قیمت مجازوضعیت نماد

shareholder

sharessharespotchangecompanycodecompanyname
تعداد سهمدرصد از کل سهامنوع تغییرکد شرکتنام شرکت

Some Notes

  • Instrument.Symbol characters are cleaned from zero-width characters, ك and ي.
  • The price adjustment algorithm is still a direct port of the official Windows app.
  • In Browser, the InstrumentAndShare data is stored in localStorage.
  • In Browser, the ClosingPrices data is stored in indexedDB.
  • In Node, data compression is done with the zlib module.
  • Renamed Symbols

    :برای منحصر به فرد بودن تمامی نمادها، نمادهای متشابه بر اساس الگوریتم زیر تغییر داده می شوند در صورت مواجه با نمادهای متشابه نمادهای متشابه بر اساس آخرین روزی که فعال بوده اند مرتب شده، سپس نمادی که دارای جدیدترین تاریخ است، دست نخورده باقی مانده، و به باقی نمادها یک خط تیره "-" و حرف "ق" و عدد جایگاه نماد در لیست مرتب شده اضافه می شود .نماد های تغییر داده شده را نمایش می دهد tse ls -R فرمان

Keywords

Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc