Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

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

Ba inspirateurs oyo bazalaki sima ya développement ya service ya statistique pona Tinkoff Investments ezalaki :

Nini ekosolola?

  • Kaka eteni oyo esalelami oyo elobeli bokoli.
  • Boyebi mpe mayele ya solo, oyo ezali na ntina mingi mpo na kosala na bisaleli ya mosolo.
  • Botali ya mozindo ya makambo oyo esengeli kosala

Donc, nalingi na calculer ba statistiques ya commerce pe nasala yango na ndenge ya convenable. 

Kobongisa service ya statistique litambe na litambe: 

  1. Connexion na API ya Tinkoff Invest
  2. Kosala ba données na Tinkoff Invest API na navigateur
  3. Kozwa ba rapports ya courtage na ba transactions
  4. Calcul mpe sortie ya information ya intérêt
  5. Bosukisi mpe myango mpo na mikolo mizali koya

Connexion na API ya Tinkoff Invest

Mpo na kozala na boyokani na API, okoki kozwa sdk nyonso uta na mikanda https://github.com/Tinkoff/investAPI#sdk . To liboke ya npm ` tinkoff-sdk-grpc-js `. Ezali na ntina ete forfait ezala mise à jour na version ya sika na ba développeurs. Ko installer

npm nazali kosala tinkoff-sdk-grpc-js

Kosala vérification

const { kokelaSdk } = kosenga (‘tinkoff-sdk-grpc-js’);   // Jeton oyo ekoki kozuama boye  const TOKEN = ‘YOURAPI’;   // Nkombo ya application oyo okoki kozwama na ba journals ya TCS. const Nkombo ya app = ‘tcsstat’;   const sdk = kosalaSdk (TOKEN, Nkombo ya app); (async () => {     console.log (zela sdk.basaleli.zwa ba comptes ()); }) ();

Résultat : liste ya ba comptes na yo ekolakisama na console. Na ndakisa, tótalela makambo ya mikemike oyo ezali na kati:Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

  • Na liste ya ba comptes ezali na « Banque d’investissement », oyo okoki kosala na yango te na kosalelaka API
  • Toyebi ete bilanga eyaka na camelCase, nzokande na mikanda bilanga oyo elakisami na under_score. 
  • Ekozala boye bisika nionso, yango wana okoki kaka te kozua pe ko copier champ moko na mikanda.

Ya ntina:

  • Okoki kozwa code oyo na filiale ya projet

https://github.com/pskucherov/tcsstat/nzete/etape1 https://github.com/pskucherov/tcsstat/kokanisa/etape1   

Kosala ba données na Tinkoff Invest API na navigateur

Nazwaki next.js mpe socket.io. Oyo ezali recommandation makasi te, pona na discrétion na yo. 

npx kosala-appareil oyo elandi@ya suka npm na socket.io socket.io-client

Tokende mbala moko na étape ya boninga next+socket+investapi, pe to tala eteni ya Utile ya étape oyo pona ba détails nionso.  Nakolimbola makambo ya mikemike: 

  • Na ngambo ya nodejs (serveur), ezali na fichier pages/api/investapi.js. Wana nde tosalaka serveur socket.io pe to connecter na investapi.
  • Na côté ya navigateur (client), to connecter na serveur na nzela ya socket pe tosengaka ba données ya compte na courtier. 
  • Tozuaka ba données epayi ya courtier na serveur, sima totindaka yango na client. Tango bazui yango na client, elakisami na navigateur. 

Résultat:  na console ya navigateur tokoki komona ba informations sur ba comptes. Elingi koloba, na étape ya suka, tomonaki ba informations sur ba comptes na console ya serveur (nodejs), na étape ya lelo, to transférer ba informations oyo na client (navigateur).

Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

Sikoyo tosala yango po opona compte na navigateur, pe soki jeton ezali te, alors erreur etindamaka na console. Mosala ezali simple mpe eloko ya sika te, yango wana napesaka kaka ba liens na ba commits

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

Ya ntina:

  • Ndenge nini kosala baninga sima mpe socket ezali kolimbolama na bozindo awa
  • Code ya boninga oyo elandi+socket+investapi:

https://github.com/pskucherov/tcsstat/commit/a443a4ac1bb4f0aa898f638128755fe7391ee381 Pona oyo oyo tolobeli likolo ezali pasi, alors totikali na étape oyo pe tosalaka na code. Soki ozali na mituna – tuna. https://github.com/pskucherov/tcsstat/nzete/etape2 https://github.com/pskucherov/tcsstat/kokanisa/etape1…etape2

Kozwa ba rapports ya courtage na ba transactions

Ezali na mayele misato mpo na kozwa balapolo ya courtage mpe ba transactions

  1. BozwaLapolo ya Broker
  2. GetDividendsMobimisi ya bapaya
  3. GetOperationsNa Cursor

Uta ebandeli mpenza ezali na ntina koyeba: 

  • Rapport ya courtage esalemi na mode T-3, c.a.d. misala ya mombongo elakisami kuna nsima ya kobomama na yango mpenza. 
  • Na yango, soki osengi rapport oyo na mikolo mibale ya suka, ekozala prêt na mikolo misato. 
  • Pona kosala ba deals pona mikolo ya suka, tosalelaka méthode ya kozua ba opérations, mais kobosana te que id na yango na contenus ekoki ko changer après formation ya rapport ya courtage.

BozwaLapolo ya Broker

Pona kozua rapport ya courtage, esengeli ozua id ya compte, date ya début na date ya fin ya rapport, mais koleka 31 jours te. Totindi bosenga ya kosala rapport na API na generate _broker_report_request , kozua taskId na eyano. Sima na yango, kosalela taskId oyo, tozuaka ba données na get _broker_report_response.

Bongo documentation elobi, en réalité ezali na ba nuances. Botala maboko:

  • Esengeli obomba TaskID libela exactement pona ba dates oyo. 
  • Lokola soki obungisi yango, boye mpo na ba dates oyo osengi rapport ekoya liboso na réponse na demande ya génération, . 
  • Et puis ekoya ata muke te.

Tobanda kokoma code

Méthode ya kozua date, en prenant en compte ya subtraction na date ya lelo

const getDateSubDay = (Mokolo moke = 5, kobanda = solo) => {     const date = Date ya sika ();     date.setUTCDate (mokolo.getUTCDate () – Mokolo moke);       soki (ebandeli) {         date.setUTCHours (0, 0, 0, 0);     } else {         mokolo.setUTCHours (23, 59, 59, 999);     }       mokolo ya kozongisa; };

Bosengi ya bokeli lapolo 

const BrokerReport = zela (sdk.operations.getBrokerReport)({         kobimisaBosenga ya Lapolo ya Broker: {             Id ya compte,             uta,             kino,         }, });

Mbano:

  • Lokola mbano ya exécution ya liboso ya commande, tozuaka taskId. 
  • Rapport ebandi ko généré na côté ya courtier. Tango ezali prêt eyebani te, tozelaka mpe périodiquement tobenda taskId na anticipation ya rapport.
  • Mpo na nini? Po soki rapport ezali prêt te, ebwakaka erreur. Soki rapport ezali prêt te na côté ya courtier, alors oyo ezali erreur na code na yo. Svp bosala: 30058|INVALID_ARGUMENT|mosala esili nanu te, meka lisusu sima

Code ya kozela pe kozua rapport ezo monana ndenge wana.

const timer = async time => {     zongisa Elaka ya sika (kosilisa => setTimeout (kosilisa, ntango)); }   const getBrokerResponseByTaskId = async (Id ya mosala, lokasa = 0) => {     meka {         zonga kozela (sdk.opérations.getBrokerReport)({             getBrokerReportRequest: {                 taskId,                 lokasa,             },         });     } kokanga (e) {         console.log (‘zela’, e);         kozela ntango(10000);         zonga zela getBrokerResponseByTaskId (Id ya mosala, lokasa);     } };

Na nsima, magie yango moko esalemaka. Totelemisi script na biso, tobandi yango lisusu, toza na taskId te. To exécuter code na demande ya taskId, mais tozuaka lisusu taskId te, mais mbala moko rapport. Magie! Mpe makambo nyonso ekozala malamu soki ezalaki ntango nyonso boye. Mais dans un mois ba données ekozala ata muke te. Ezali na ntina :

  • Mwa théorie elakisami awa mpe awa .
  • Kotia code esika moko, projet ekozala ndenge moko boye.

https://github.com/pskucherov/tcsstat/nzete/etape3.1 https://github.com/pskucherov/tcsstat/kokanisa/etape3.1

  • Soki moto akutanaki na likambo oyo, boye boyei malamu na likambo yango . Nsima ya kobongisa magie oyo, ekobungisa nguya na yango mpe ekozala na lolenge moko to mosusu ndenge mosusu. Mais na moment actuel (03/21/2023) ezo sala kaka boye.

GetDividendsMobimisi ya bapaya

Moto moko akoki kokanisa ete méthode ekokani na oyo ya liboso mpe okoki kosalela méthode moko oyo obongoli kaka kombo ya ba opérations. Kasi bakanisaki te!  Kopesa kombo kuna ekeseni mingi ezala na ba méthodes mpe na ba informations oyo ezongisami. Mpe motango ya nkasa ebandi na 0, sima na 1. Pona kobulungana te na nionso oyo, ezali pete kokoma ba méthodes mibale ekeseni. Oyo ezali likambo ya kokamwa, mpo logique ya mosala ezali ndenge moko. Nabwakaki nsoi ntango molai ntango namekaki kosala méthode moko mpe code ezalaki moke. Bandakisa ekozala te awa.

GetOperationsNa Cursor

Oyo nalingaka mingi na kati ya misato. Atako ezali ya sikisiki koleka te, kasi oyo ekoki koleka. Tosalaka demande kobanda na ebandeli ya kosala compte tii na date maximum possible (kokanga compte to oyo ya lelo). Tozuaka réponse, tozuaka curseur pe to re-requer tant que ba données ezali.  Mpe code ezali mokuse koleka na bandakisa oyo ezali awa na likoló.

const timer = async time => {     zongisa Elaka ya sika (kosilisa => setTimeout (kosilisa, ntango)); }   const getOperationsByCursor = async (sdk, Id ya compte, kobanda, na, curseur = ”) => {     meka {         const reqData = {             Id ya compte,             kobanda,             na,             ndelo: 1000,             état: sdk.Etat ya opération.ÉTAT_OPERATION_EXÉCUTÉ,             sansCommissions: lokuta,             sansTrades: lokuta,             sansOvernights: lokuta,             curseur,         };           zonga kozela sdk.opérations.zwaMisalaNa Curseur (reqData);     } kokanga (e) {         kozela ntango (60000);         zonga zela getOperationsByCursor (sdk, Id ya compte, kobanda, na, curseur = ”);     } };

Projet oyo esengeli kosala ezali awa: https://github.com/pskucherov/tcsstat/tree/step3.3 https://github.com/pskucherov/tcsstat/compare/step3.3 Sikoyo tozali prêt ya kobakisa ba opérations ya boyambi na demande na biso. Soki esalemi malamu, alors esengeli ozua ba rapports ya courtage pona existence mobimba ya compte. Et pona ba données oyo ezangi, ba T-3 wana kaka, recharger à partir ya ba opérations. Kasi likambo yango ekoki kokabolama na lisolo mosusu. Ya ba nuances ya minene oyo okokutana na yango ezali ya ko coller ba opérations na rapport ya courtage.

  •  Soki lelo ozui rapport ya courtage na ba transactions pona ba dates oyo esengeli, tia yango nionso na base de données, alors ba problèmes eza te. 
  • Okozala na ba problèmes lobi tango okozua eteni ya ba données oyo elandi na rapport pe ba opérations pe oko décider ko synchroniser yango na base de données oyo ezali. 
  • Ebele ya ba nuances sur mismatched to changer id après traitement
  • Sima pona marché OTC, ba id’s ezo correspondre ata muke te.
  •  Lokola mpe ba nuances ya ba instruments synchronisation, oyo lisusu e coincider te, en raison ya ba peculiarités ya API. Kasi yango ezali lisolo mosusu.

Tobakisa kozwa ba informations oyo etali ba opérations na application na biso. Motuna ya monene ekozala esika nini ba données eko traité mpe ekobombama.

  •  Soki osali yango yo moko, oko consommer ba données ndenge moko na ba appareils différents. Na nsima, osengeli kosala mpe kobomba ba données na serveur.
  • Soki ozali na ebele ya ba données différentes consommées par plusieurs usagers différentes, alors il faut o décider nini ezali plus important : vitesse ya ba usagers to économie ya fer na côté na yo. Oyo akoki kofuta quantité infinie ya matériel atanga nionso na serveur na ye mpe akomisaka yango super rapide mpo na ba usagers, ko sauver ba ressources ya usager, neti pile na trafic, oyo ezali très important na ba téléphones.

Na ngambo na yango, kotanga na navigateur ezali te solution oyo eleki malamu na principe. Yango wana, oyo ezali ntalo mingi te, totalelaka yango na serveur na biso. Totikaka oyo etikali na client. Nalingi vraiment nazua pe na calculer commission na serveur. Kasi awa eyei nuance oyo babengaka « interactivité ». Toloba ete ozali na bankóto ya ba opérations mpe esɛngaka miniti mitano mpo na kozwa yango. Mosaleli akozala na nini na tango oyo? Spinner? Kokende liboso? Infa etali combien ezalaki uploadé? Ezali malamu kosalela “kozela ya mosala” ntango mosaleli oyo azali na mosala yango akokaki deja komona eloko moko. Tala Résultat oyo:Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

  • Chargement ya page
  • Ba factures nionso esengami
  • Sima na yango, ba transactions nionso na ba commissions pona ba transactions exécutées esengami pona ba comptes nionso. Lokola ba données ezuami, ezo rendre na navigateur.

Pona ko filtrer ba données na ba événements mbala nionso te, to benda événement na biso moko pona compte moko na moko. Lokola oyo:

socket.emit (‘sdk:getOperationsCommissionResult_’ + accountId, {                 biloko: ba données?.biloko,                 naProgress: Boolean (Curseur oyo elandi), });

Projet ya ko lancer ezali awa: https://github.com/pskucherov/tcsstat/nzete/step3 https://github.com/pskucherov/tcsstat/kokanisa/étape2…etape3 Kokende liboso. Ezali malamu mingi ndenge otángi molɔngɔ oyo! 

Calcul mpe sortie ya information ya intérêt

Etali nani aza na besoin ya information nini. Yango wana, nayebisaka bino mbala moko ba nuances ya minene oyo bokokutana na yango.

Kosala na ba prix 

Moto nyonso oyo asalaka na makambo ya mbongo ayebi ete esengeli kosala mombongo ya mbongo kaka na mituya mobimba. Na tina ya bozangi bosikisiki ya ba valeurs sima ya point décimal mpe erreur cumulative na nombre monene ya ba opérations. Yango wana ba prix nionso elakisami na format MoneyValue oyo elandiTozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

elanga lolenge Ndimbola
mbongo shene String Code ya mbongo ISO
ba unité int64 Eteni ya motango mobimba ya motango, ekoki kozala motango ya négatif
nano int32 Eteni ya fraction ya montant, ekoki kozala nombre négatif

Tosalaka yango separatement, sima tomemaka yango na valeur ya prix:

citatio.unités + citatio.nano / 1e9

Ntalo ya ba contrats ya futures

Prix ​​ya ba futurs e présenter na points, tango oza na avenir ya mbongo, esengeli oyeba taux. Et bien sûr prix na points na étape ya prix. Tango o calculer profit ya ba transactions, oyo ekoki ko tirer, po. soki osali calcul ya motuya mobimba na ko multiplier prix na quantité. Awa osengeli kokeba. Pona sikoyo, tokotala ndenge ekoleka. Yango etali ba futurs ya mbongo, na bisika mosusu nionso ezali ok na likambo oyo.Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions. Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

Zando ya OTC

Zando oyo ezali na ba peculiarités ebele, yango wana toyekola ba opérations likolo na yango separatement.Tango okobanda ko synchroniser ba opérations, ekobima que esengeli omema figi/ticker na forme moko mpo na ko correspondre correctement na instrument. Tango okobanda ko synchroniser yango na rapport ya courtage, ekobima que tradeID ya même transaction ezali na ba lettres na ebandeli na ba transactions mpe ezali te na rapport ya courtage. Yango wana, bakoki kokokanisa bango te … ahem-ahem … na bokokanisi! Na matchaki temps ya commerce, ticker na matching que tradeId moko ezali na kati ya mosusu. Ezali bongo, nayebi te. Oyo akutani na likambo oyo mpe oyo azali komibanzabanza mpo na yango, aya na likambo to banda ya sika.Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

Ba opérations mathématiques na ba outils

Ezali likoki te, kozanga kotala, kosala ba opérations mathématiques na liste mobimba. Mpo na kobakisa molunge na doux te, totalaka ntango nyonso mbongo mpe tosalaka kaka soki tozali na ntembe te ete mbongo ekokani, mpe ba points ebongwanaka na mbongo oyo tolingi. Armé na connaissance ya kosala na ba numéros ya banque, tokosala calcul ya commission oyo eko dépenser na moko na moko ya ba comptes. Lokola oyo: https://github.com/pskucherov/tcsstat/nzete/etape4 https://github.com/pskucherov/tcsstat/kokanisa/etape3…etape4Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.    

Microservice ezali prêt!

https://github.com/pskucherov/tcsstat Lokola devoir, okoki kotala soki service esalaka na connexion ya malembe, tango ba connexions ebukani, tango Internet ekangami, tango ba erreurs to ba limites expirées na ngambo ya courtier. 

Bosukisi mpe myango mpo na mikolo mizali koya

  • Ayekolaki na ntina ya misala ya moboko mpe kosala na API ya Invest
  • Tango oyo elekisami ~ 10 heures
  • Niveau ya difficulté ~ junior+ / bas milieu 

Soki okobi ko refiner microservice, okoki kosuka na eloko ya boye

https://opexbot.info ya mboka

  Oyo ezali développement na ngai, pona ba oyo bazali trop paresseux po ba comprendre, ba kima pe ba compter bango moko. Na planifier kobakisa analytiques kuna na demande ya ba usagers. Soki osepeli na article, alors abonné na chaîne na ngai ya télégramme . Tozali kosala microservice na kosalelaka API ya Tinkoff Invest mpo na ko automatiser kosala na ba rapports ya courtage mpe kosala calcul ya ba commissions.

Pavel
Rate author
Add a comment

  1. Isakiiev

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

    Reply