Tikupanga ntchito yaying’ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

Программирование

Zomwe zidalimbikitsa chitukuko cha ntchito ya ziwerengero za Tinkoff Investments zinali:

Kodi tidzakambirana chiyani?

  • Gawo lokhalo lachitukuko.
  • Chidziwitso chenicheni ndi zochitika, zomwe ndizofunikira kwambiri pogwira ntchito ndi zida zachuma.
  • Mwachidule za nkhani zofunika kugwirirapo ntchito

Chifukwa chake, ndikufuna kuwerengera ziwerengero zamalonda ndikuzichita m’njira yabwino. 

Kupanga ma statistics service sitepe ndi sitepe: 

  1. Kulumikizana ndi Tinkoff Invest API
  2. Kujambula deta kuchokera ku Tinkoff Invest API mu msakatuli
  3. Kulandira malipoti a brokerage ndi zochitika
  4. Kuwerengera ndi kutulutsa chidziwitso cha chidwi
  5. Mapeto ndi mapulani amtsogolo

Kulumikizana ndi Tinkoff Invest API

Kuti mulumikizane ndi API, mutha kutenga sdk iliyonse kuchokera pazolembedwa https://github.com/Tinkoff/investAPI#sdk . Kapena npm phukusi ` tinkoff-sdk-grpc-js` . Ndikofunikira kuti phukusili lisinthidwe ku mtundu waposachedwa ndi opanga. Ikani

npm ndi tinkoff-sdk-grpc-js

Kuyang’ana

const {createSdk} = amafuna(‘tinkoff-sdk-grpc-js’);   // Chizindikiro chomwe chingapezeke monga  const TOKEN = ‘YOURAPI’;   // Dzina la ntchito yomwe mungapezeke muzolemba za TCS. const appName = ‘tcsstat’;   const sdk = createSdk(TOKEN, appName); (async () => {     console.log(await sdk.users.getAccounts()); })();

Zotsatira: mndandanda wamaakaunti anu udzawonetsedwa mu console. Mwachitsanzo, tiyeni tipende ma nuances:Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

  • Pamndandanda wamaakaunti pali “Investment bank”, yomwe simungagwire nayo ntchito pogwiritsa ntchito API
  • Chonde dziwani kuti minda imabwera mu camelCase, pomwe muzolemba mindayi ikuwonetsedwa mu under_score. 
  • Zidzakhala monga chonchi paliponse, kotero simungathe kungotenga ndikukopera gawo kuchokera pazolembedwa.

Zothandiza:

  • Mutha kupeza code iyi munthambi ya polojekiti

https://github.com/pskucherov/tcsstat/tree/step1 https://github.com/pskucherov/tcsstat/compare/step1   

Kujambula deta kuchokera ku Tinkoff Invest API mu msakatuli

Ndinatenga next.js ndi socket.io. Uku siupangiri wamphamvu, sankhani mwakufuna kwanu. 

npx pangani-next-app@latest npm ine socket.io socket.io-client

Nthawi yomweyo timapita ku sitepe yaubwenzi yotsatira+socket+investapi, ndikuwona Gawo Lothandiza la sitepe iyi kuti mudziwe zambiri.  Ndifotokoza zambiri: 

  • Pa mbali ya nodejs (seva), pali fayilo ya masamba/api/investapi.js. Apa ndipamene timapanga seva ya socket.io ndikulumikiza ku investapi.
  • Pamsakatuli (kasitomala), timalumikizana ndi seva kudzera pa socket ndikupempha deta ya akaunti kuchokera kwa broker. 
  • Timalandila deta kuchokera kwa broker pa seva, kenako ndikutumiza kwa kasitomala. Akalandira pa kasitomala, amawonetsedwa mu msakatuli. 

Zotsatira:  mu msakatuli console timatha kuwona zambiri zamaakaunti. Ndiko kuti, mu sitepe yotsiriza, tinawona zambiri za akaunti mu seva console (nodejs), mu sitepe yamakono, tinasamutsa chidziwitsochi kwa kasitomala (osatsegula).

Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

Tsopano tiyeni tipange kuti muthe kusankha akaunti kuchokera kwa osatsegula, ndipo ngati palibe chizindikiro, ndiye kuti cholakwika chimatumizidwa ku console. Ntchitoyi ndi yosavuta ndipo palibe chatsopano, kotero ndimangopereka maulalo ochita

  1. https://github.com/pskucherov/tcsstat/commit/7e1ac57061e5e971588479015b06d8814d6609a9
  2. https://github.com/pskucherov/tcsstat/commit/b28ac973a57494f5232589b4cb6b9fb13b8af759 

Zothandiza:

  • Momwe mungapangire mabwenzi motsatira ndi socket ikufotokozedwa mwatsatanetsatane apa
  • Khodi yaubwenzi yotsatira+socket+investapi:

https://github.com/pskucherov/tcsstat/commit/a443a4ac1bb4f0aa898f638128755fe7391ee381 Kwa omwe zili pamwambapa ndizovuta, ndiye timakhalabe panthawiyi ndikuchita ndi code. Ngati muli ndi mafunso – funsani. https://github.com/pskucherov/tcsstat/tree/step2 https://github.com/pskucherov/tcsstat/compare/step1…step2

Kulandira malipoti a brokerage ndi zochitika

Pali njira zitatu zolandirira malipoti a brokerage ndi zochitika

  1. GetBrokerReport
  2. GetDividendsForeignIssuer
  3. GetOperationsByCursor

Kuyambira pachiyambi ndikofunikira kudziwa: 

  • Lipoti la brokerage limapangidwa mu T-3 mode, i.e. malonda amawonetsedwa pamenepo pambuyo pa kuphedwa kwawo kwenikweni. 
  • Chifukwa chake, ngati mupempha lipotili kwa masiku awiri apitawa, likhala lokonzeka m’masiku atatu. 
  • Kuti tipeze zochitika zamasiku otsiriza, timagwiritsa ntchito njira yolandirira malonda, koma kumbukirani kuti id ndi zomwe zili zawo zikhoza kusintha lipoti la brokerage litapangidwa.

GetBrokerReport

Kuti mupeze lipoti la brokerage, muyenera kutenga id ya akaunti, tsiku loyambira ndi tsiku lomaliza la lipoti, koma osapitirira masiku 31. Timatumiza pempho loti tipange lipoti ku API ndikupanga _broker_report_request , pezani taskId poyankha. Pambuyo pake, pogwiritsa ntchito taskId iyi, timapeza deta kuchokera ku get _broker_report_response.

Momwemo zolembedwazo zikuti, zoona zake zimakhala zamitundumitundu. Onerani manja anu:
  • Muyenera kusunga TaskID kwanthawi zonse pamasiku awa. 
  • Popeza ngati mutataya, ndiye kuti pamasiku omwe mwafunsidwa lipotilo limabwera poyankha pempho la m’badwo, 
  • Ndiyeno izo sizibwera konse.
Tiyeni tiyambe kulemba khodi

Njira yopezera tsiku, poganizira zochotsa kuyambira tsiku lomwe lilipo

const getDateSubDay = (subDay = 5, start = true) => {     const date = new Date();     date.setUTCDate(date.getUTCDate() – subDay);       ngati (kuyamba) {         date.setUTCHours(0, 0, 0, 0);     } china {         date.setUTCHours(23, 59, 59, 999);     }       tsiku lobwerera; };

Lembani pempho lopanga lipoti 

const brokerReport = await (sdk.operations.getBrokerReport)({         generateBrokerReportRequest: {             accountId,             from,             to,         }, });

Zotsatira:

  • Chifukwa cha kuphedwa koyamba kwa lamulo, timapeza taskId. 
  • Lipotilo likuyamba kupangidwa kumbali ya broker. Zikakonzeka sizikudziwika, timadikirira ndikukoka ntchitoId nthawi ndi nthawi poyembekezera lipoti.
  • Chifukwa chiyani? Chifukwa ngati lipoti silinakonzekere, limaponya cholakwika. Ngati lipoti silinakonzekere kumbali ya broker, ndiye kuti izi ndi zolakwika mu code yanu. Chonde konzani: 30058|INVALID_ARGUMENT|ntchito yomwe sinamalizidwe, chonde yesaninso nthawi ina

Khodi yodikirira ndi kulandira lipoti ikuwoneka motere.

const timer = async time => {     kubwezerani Lonjezo latsopano (kuthetsa => setTimeout(kuthetsa, nthawi)); }   const getBrokerResponseByTaskId = async (taskId, page = 0) => {     yesani {         kubwerera kuyembekezera (sdk.operations.getBrokerReport)({             getBrokerReportRequest: {                 taskId,                 page,             },         });     } kugwira (e) {         console.log(‘wait’, e);         kuyembekezera nthawi (10000);         bwererani kuyembekezera getBrokerResponseByTaskId(taskId, tsamba);     } };

Kenako matsenga omwewo amachitika. Timayimitsa zolemba zathu, tiyambenso, tilibe taskId. Timapereka code ndi pempho la taskId, koma sitipezanso taskId, koma nthawi yomweyo lipoti. Zamatsenga! Ndipo zonse zikhala bwino ngati zikanakhala choncho nthawi zonse. Koma mu mwezi sipadzakhala deta konse. Zothandiza :

  • Chiphunzitso china chikufotokozedwa apa ndi apa .
  • Kuyika code pamodzi, zolembazo zidzawoneka ngati izi.

https://github.com/pskucherov/tcsstat/tree/step3.1 https://github.com/pskucherov/tcsstat/compare/step3.1

  • Ngati wina akumana ndi izi, landirani ku nkhaniyi . Atatha kukonza matsengawa, adzataya mphamvu zake ndipo adzakhala mwanjira ina. Koma pakadali pano (03/21/2023) imagwira ntchito monga choncho.

GetDividendsForeignIssuer

Wina angaganize kuti njirayo ndi yofanana ndi yapitayi ndipo mungagwiritse ntchito njira imodzi yomwe mumangosintha dzina la ntchito. Koma sanayerekeze!  Kutchula mayina kumeneko ndi kosiyana kwambiri m’njira zonse komanso muzodziwitso zomwe zabwezedwa. Ndipo chiwerengero cha masamba chimayamba kuchokera ku 0, kenako kuchokera ku 1. Kuti musasokonezedwe mu zonsezi, ndizosavuta kulemba njira ziwiri zosiyana. Chimene chiri chodabwitsa, chifukwa malingaliro a ntchito ndi omwewo. Ndinalavulira kwa nthawi yayitali ndikuyesera kupanga njira imodzi ndipo panalibe code yochepa. Sipadzakhala zitsanzo apa.

GetOperationsByCursor

Ndimakonda mwa atatuwo. Ngakhale kuti si zolondola kwambiri, koma zokwanira kwambiri. Timapempha kuyambira pachiyambi kupanga akaunti mpaka tsiku lotheka (kutseka akaunti kapena yomwe ilipo). Timapeza yankho, kutenga cholozera ndikupemphanso malinga ngati pali deta.  Ndipo code ndi yachidule kuposa zitsanzo pamwambapa.

const timer = async time => {     kubwezerani Lonjezo latsopano (kuthetsa => setTimeout(kuthetsa, nthawi)); }   const getOperationsByCursor = async (sdk, accountId, kuchokera, mpaka, cursor = ”) => {     yesani {         const reqData = {             accountId,             kuchokera,             mpaka,             malire: 1000,             state: sdk.OperationState.OPERATION_STATE_EXECUTED,             withoutCommissions: zabodza,             withoutTrades: zabodza,             popandaOvernights: zabodza,             cholozera,         };           kubwerera kudikira sdk.operations.getOperationsByCursor(reqData);     } gwira (e) {         wait timer(60000);         bwererani dikirani getOperationsByCursor(sdk, accountId, from, to, cursor = ”);     } };

Zoyenera kuchita zili pano: https://github.com/pskucherov/tcsstat/tree/step3.3 https://github.com/pskucherov/tcsstat/compare/step3.3 Tsopano takonzeka kuwonjezera ntchito zolandila ku ntchito yathu. Ngati mwachita bwino, ndiye kuti muyenera kupeza malipoti a brokerage akupezeka konse kwa akauntiyo. Ndipo pazomwe zikusowa, ma T-3 omwewo, tsegulaninso kuchokera pamachitidwe. Koma izi zikhoza kugawidwa m’nkhani ina. Mwazinthu zazikulu zomwe mungakumane nazo ndikumatira ntchito ndi lipoti la brokerage.

  •  Ngati lero mwalandira lipoti la brokerage ndi zochitika zamasiku ofunikira, ikani zonse mu database, ndiye palibe mavuto. 
  • Mudzakhala ndi mavuto mawa mukalandira gawo lotsatira la deta kuchokera ku lipoti ndi ntchito ndikusankha kuzigwirizanitsa ndi deta yomwe ilipo. 
  • Zambiri zokhudzana ndi kusagwirizana kapena kusintha ma id mutatha kukonza
  • Kenako pamsika wa OTC, ma id samafanana konse.
  •  Komanso ma nuances a synchronizing zida, zomwe sizigwirizananso, chifukwa cha mawonekedwe a API. Koma imeneyo ndi nkhani ina.

Tiyeni tiwonjeze kupeza zambiri zokhudzana ndi ntchito ku pulogalamu yathu. Funso lalikulu lidzakhala komwe deta idzakonzedwa ndikusungidwa.

  •  Ngati mumadzipangira nokha, mudzadya deta yomweyo kuchokera kuzipangizo zosiyanasiyana. Ndiye muyenera kukonza ndi kusunga deta pa seva.
  • Ngati muli ndi zambiri zosiyana zomwe zimagwiritsidwa ntchito ndi ogwiritsa ntchito osiyanasiyana, ndiye kuti muyenera kusankha chomwe chili chofunika kwambiri: kuthamanga kwa ogwiritsa ntchito kapena kupulumutsa chitsulo kumbali yanu. Aliyense amene angakwanitse kugula zida zopanda malire amawerengera zonse pa seva yake ndipo zimapangitsa kuti zikhale zofulumira kwambiri kwa ogwiritsa ntchito, kusunga zinthu zomwe zimagwiritsidwa ntchito, monga batri ndi magalimoto, zomwe ndizofunikira kwambiri pa mafoni.

Komanso, kuwerengera mu osatsegula si mulingo woyenera kwambiri yothetsera mfundo. Chifukwa chake, zomwe sizokwera mtengo, timaziwona pa seva yathu. Zina zonse timasiya kwa kasitomala. Ndikufunadi kutenga ndikuwerengera komishoni pa seva. Koma apa pakubwera nuance yotchedwa “interactivity”. Tiyerekeze kuti muli ndi maopaleshoni masauzande ambiri ndipo zimatenga mphindi zisanu kuti muwalandire. Kodi wosuta adzakhala ndi chiyani panthawiyi? Spinner? Kupita patsogolo? Infa zakwezedwa zingati? Ndikwabwino kugwiritsa ntchito “kudikirira mwachangu” pomwe wogwiritsa ntchitoyo atha kuwona kale china chake. Zotsatira zake ndi izi : Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

  • Kutsegula tsamba
  • Ma invoice onse amafunsidwa
  • Pambuyo pake, zochitika zonse zokhala ndi ma komisheni ochita zomwe zachitika zimafunsidwa pamaakaunti onse. Monga momwe deta ikulandirira, imaperekedwa mu msakatuli.

Kuti tisasefe zomwe zikuchitika nthawi iliyonse, timakoka zochitika zathu pa akaunti iliyonse. Ngati chonchi:

socket.emit(‘sdk:getOperationsCommissionResult_’ + accountId, {                 zinthu: data?.items,                 inProgress: Boolean(nextCursor), });

Zokonzekera kukhazikitsa zili pano: https://github.com/pskucherov/tcsstat/tree/step3 https://github.com/pskucherov/tcsstat/compare/step2…step3 Moving on. Ndizosangalatsa kuti mwawerenga mzerewu! 

Kuwerengera ndi kutulutsa chidziwitso cha chidwi

Zimatengera yemwe akufunika chidziwitso. Chifukwa chake, nthawi yomweyo ndikukuwuzani ma nuances akulu omwe mungakumane nawo.

Kugwira ntchito ndi mitengo 

Aliyense amene amagwira ntchito ndi zachuma amadziwa kuti ndalama ziyenera kuchitidwa ndi manambala athunthu. Chifukwa chakusokonekera kwamitengo pambuyo pa decimal point komanso cholakwika chochulukirapo ndi kuchuluka kwa magwiridwe antchito. Ichi ndichifukwa chake mitengo yonse imawonetsedwa mumtundu wotsatira wa MoneyValueTikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

mundamtunduKufotokozera
ndalamachingweString ISO ndalama kodi
mayunitsiint64Nambala yonse ya chiwerengerocho, ikhoza kukhala nambala yotsutsa
nanoinu 32Gawo laling’ono la ndalamazo, likhoza kukhala nambala yolakwika

Timazikonza padera, kenako kuzibweretsa pamtengo wamtengo:

quotation.mayunitsi + quotation.nano / 1e9

Mtengo wa makontrakitala am’tsogolo

Mtengo wamtsogolo umaperekedwa mu mfundo, mukakhala ndi tsogolo la ndalama, muyenera kudziwa mlingo. Ndipo ndithudi mtengo mu mfundo ndi sitepe mtengo. Mukawerengera phindu kuchokera pazogulitsa, izi zimatha kuwombera, chifukwa. ngati muwerengera ndalama zonse pochulukitsa mtengo ndi kuchuluka kwake. Apa muyenera kusamala. Pakadali pano, tiwona momwe zingakhalire. Izi zikugwiranso ntchito pazamtsogolo zandalama, m’malo ena zonse zili bwino ndi izi.Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

Msika wa OTC

Msikawu uli ndi zinthu zambiri zapadera, kotero tiyeni tiphunzire momwe zimagwirira ntchito padera.Mukayamba kulunzanitsa ntchito, zidzapezeka kuti muyenera kubweretsa figi / ticker mu mawonekedwe omwewo kuti mufanane ndi chidacho. Mukayamba kulunzanitsa izi ndi lipoti la brokerage, zidzawoneka kuti tradeID yamalonda omwewo ali ndi makalata pachiyambi pazochitikazo ndipo sali mu lipoti la brokerage. Chifukwa chake, sangayerekezedwe … ahem-ahem … poyerekeza! Ndinafananiza nthawi yamalonda, ticker ndikufananiza kuti tradeId imodzi ili mu ina. Kulondola, sindikudziwa. Aliyense amene akukumana ndi izi ndi amene amasamala za izo, bwerani ku nkhaniyo kapena yambani ina.Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

Masamu pazida

Ndizosatheka, popanda kuyang’ana, kuchita masamu ndi mndandanda wonse. Kuti tisawonjezere kutentha kwa zofewa, nthawi zonse timayang’ana ndalama ndi ndondomeko pokhapokha ngati tikutsimikiza kuti ndalamazo zimagwirizana, ndipo mfundozo zimasinthidwa kukhala ndalama zomwe tikufuna. Pokhala ndi chidziwitso chogwira ntchito ndi manambala a banki, tidzawerengera ndalama zomwe zimagwiritsidwa ntchito pa akaunti iliyonse. Monga izi: https://github.com/pskucherov/tcsstat/tree/step4 https://github.com/pskucherov/tcsstat/compare/step3…step4Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.   

Microservice yakonzeka!

https://github.com/pskucherov/tcsstat Monga homuweki, mukhoza kuyang’ana ngati ntchitoyo ikugwira ntchito ndi kugwirizana kwapang’onopang’ono, pamene malumikizidwe athyoledwa, pamene intaneti imachotsedwa, pamene zolakwika kapena malire atha pa mbali ya broker. 

Mapeto ndi mapulani amtsogolo

  • Phunzirani za ntchito zoyambira ndikugwira ntchito ndi Invest API
  • Nthawi yogwiritsidwa ntchito ~ 10 hours
  • Mulingo wovuta ~ junior + / otsika pakati 

Ngati mupitiliza kuyeretsa microservice, mutha kukhala ndi izi

https://opexbot.info

  Ichi ndi chitukuko changa, kwa iwo omwe ali aulesi kwambiri kuti amvetsetse, kuthamanga ndikuwerengera okha. Ndikukonzekera kuwonjezera ma analytics pamenepo pa pempho la ogwiritsa ntchito. Ngati mudakonda nkhaniyi, lembetsani ku njira yanga ya telegalamu . Tikupanga ntchito yaying'ono pogwiritsa ntchito Tinkoff Invest API kuti igwire ntchito ndi malipoti a brokerage ndikuwerengera ma komisheni.

Pavel
Rate author
Add a comment

  1. Isakiiev

    Полезная статья. Не могу представить, сколько усилий автора потребовалось, чтобы все описать. Благодарю.

    Reply