Compare commits
830 Commits
310_plus
...
kraken_dep
Author | SHA1 | Date |
---|---|---|
Tyler Goodlet | 1346c33f04 | |
goodboy | d690ad2bab | |
Guillermo Rodriguez | 0f082ed9d4 | |
Guillermo Rodriguez | 2851a0ecc5 | |
Tyler Goodlet | 340045af77 | |
Tyler Goodlet | c1988c4d8d | |
Tyler Goodlet | 6a0c36922e | |
Tyler Goodlet | 459cbfdbad | |
Tyler Goodlet | fc17187ff4 | |
Tyler Goodlet | a7d78a3f40 | |
Tyler Goodlet | 7ce3f10e73 | |
Tyler Goodlet | bfc6014ad3 | |
Tyler Goodlet | a5eed8fc1e | |
Tyler Goodlet | cdec4782f0 | |
Tyler Goodlet | f30a48b82c | |
Tyler Goodlet | 98de22a740 | |
Tyler Goodlet | efbb8e86d4 | |
Tyler Goodlet | b6521498f4 | |
Tyler Goodlet | 06f1b94147 | |
Tyler Goodlet | ffb57f0256 | |
Tyler Goodlet | ed1f64cf43 | |
Tyler Goodlet | bf8ea33697 | |
Tyler Goodlet | bc17308de7 | |
Tyler Goodlet | 1ece704d6e | |
Tyler Goodlet | dea1c1c2d6 | |
Tyler Goodlet | 3300a240c6 | |
Tyler Goodlet | 50ef4efccb | |
Tyler Goodlet | 51f2461e8b | |
Tyler Goodlet | 444768d30f | |
Tyler Goodlet | 0d0675ac7e | |
Tyler Goodlet | 24b384f3ef | |
Tyler Goodlet | 93330954c2 | |
Tyler Goodlet | edf721f755 | |
Tyler Goodlet | 530b2731ba | |
Tyler Goodlet | 14104185d2 | |
Tyler Goodlet | 3019c35e30 | |
Tyler Goodlet | 4d74bc29b4 | |
Tyler Goodlet | 3638ae8d3e | |
Tyler Goodlet | c5dd67e63c | |
Tyler Goodlet | 0663880a6d | |
Tyler Goodlet | 3bed142d15 | |
Tyler Goodlet | 9fcc6f9c44 | |
Tyler Goodlet | 7aef31701b | |
Tyler Goodlet | 135627e142 | |
Tyler Goodlet | 5216a6b732 | |
Tyler Goodlet | 2a797d32dc | |
Tyler Goodlet | 35a16ded2d | |
Tyler Goodlet | 44f50e3d0e | |
Tyler Goodlet | 96b871c4d7 | |
Tyler Goodlet | d2aad74dfc | |
Tyler Goodlet | 50209752c3 | |
Tyler Goodlet | 5ab4e5493e | |
Tyler Goodlet | e252f70253 | |
Tyler Goodlet | 98438e29ef | |
Tyler Goodlet | d649a7d1fa | |
Tyler Goodlet | 2669ced629 | |
Tyler Goodlet | f2c0987a04 | |
Tyler Goodlet | bb84715bf0 | |
Tyler Goodlet | 0bdb7261d1 | |
Tyler Goodlet | 12857a258b | |
Tyler Goodlet | 46808fbb89 | |
Tyler Goodlet | 6ca8334253 | |
Tyler Goodlet | a3844f9922 | |
Tyler Goodlet | 58b36db2e5 | |
Tyler Goodlet | a33f58a61a | |
Tyler Goodlet | a4392696a1 | |
Tyler Goodlet | d5844ce8ff | |
Tyler Goodlet | bf88b40a50 | |
Tyler Goodlet | e4a0d4ecea | |
Tyler Goodlet | cca3417c57 | |
Tyler Goodlet | 031d7967de | |
Tyler Goodlet | 2e67e98b4d | |
Tyler Goodlet | 7124a131dd | |
Tyler Goodlet | 9052ed5ddf | |
Tyler Goodlet | 7ec21c7f3b | |
Tyler Goodlet | 309ae240cf | |
Tyler Goodlet | 382a619a03 | |
Tyler Goodlet | 7f3f6f871a | |
Tyler Goodlet | 6ea04f850d | |
Tyler Goodlet | 3d5695f40a | |
Tyler Goodlet | 5affad942f | |
Tyler Goodlet | eb9ab20646 | |
Tyler Goodlet | f3bab826f6 | |
Tyler Goodlet | 2b9ca5f805 | |
Tyler Goodlet | 25a75e5bec | |
Tyler Goodlet | 702ae29a2c | |
Tyler Goodlet | ac1f37a2c2 | |
Tyler Goodlet | 344d2eeb9e | |
Tyler Goodlet | 9133103f8f | |
Tyler Goodlet | 166d14af69 | |
Tyler Goodlet | 696c6f8897 | |
Tyler Goodlet | be21f9829e | |
Tyler Goodlet | 5a0673d66f | |
Tyler Goodlet | 6cacd7d18b | |
Tyler Goodlet | 5b08e9cba3 | |
Tyler Goodlet | d3f5ff1b4f | |
Tyler Goodlet | e45bc4c619 | |
Tyler Goodlet | baee86a2d6 | |
Tyler Goodlet | 86d09d9305 | |
Tyler Goodlet | 9ace053aaf | |
Guillermo Rodriguez | f0b17cb8f7 | |
Guillermo Rodriguez | 5ca45362c8 | |
Tyler Goodlet | 1f2081911f | |
goodboy | a7d02ecec8 | |
goodboy | 11ba706797 | |
Tyler Goodlet | 50ad7370c7 | |
goodboy | 0616cbd1f1 | |
Tyler Goodlet | af92602027 | |
Tyler Goodlet | d8bf45b02d | |
Tyler Goodlet | 07ab853d3d | |
Tyler Goodlet | 414866fc6b | |
Tyler Goodlet | bc7fe6114d | |
Tyler Goodlet | 8d592886fa | |
Tyler Goodlet | 69ea296a9b | |
Tyler Goodlet | 03821fdf6f | |
Tyler Goodlet | 1aa9ab03da | |
Tyler Goodlet | 1d83b43efe | |
Tyler Goodlet | 6986be1b21 | |
Tyler Goodlet | 92c50aa6a7 | |
Tyler Goodlet | eac79c5cdd | |
Tyler Goodlet | 7aec238f5f | |
Tyler Goodlet | be3dc69290 | |
Tyler Goodlet | 6100bd19c7 | |
Tyler Goodlet | d57bc6c6d9 | |
Tyler Goodlet | 58b42d629f | |
Tyler Goodlet | 36a81cb2de | |
Tyler Goodlet | ae0f3118f4 | |
Tyler Goodlet | 727c7ce2b1 | |
Tyler Goodlet | a39c980266 | |
Tyler Goodlet | 00be100e71 | |
Tyler Goodlet | 9217610734 | |
Tyler Goodlet | 31af7a2c99 | |
Tyler Goodlet | 34fac364fd | |
goodboy | dcdfd2577a | |
goodboy | 6733dc57af | |
Tyler Goodlet | 05c4b6afb9 | |
Tyler Goodlet | 4b22325ffc | |
Tyler Goodlet | 9d16299f60 | |
Tyler Goodlet | ab1f15506d | |
Tyler Goodlet | 0db5451e47 | |
goodboy | 61218f30f5 | |
Tyler Goodlet | fcfc0f31f0 | |
Tyler Goodlet | 69074f4fa5 | |
Tyler Goodlet | fe4fb37b58 | |
Tyler Goodlet | 7cfd431a2b | |
Tyler Goodlet | 61e20a86cc | |
Tyler Goodlet | d9b73e1d08 | |
goodboy | 4833d56ecb | |
Tyler Goodlet | 090d1ba524 | |
Tyler Goodlet | afc45a8e16 | |
Tyler Goodlet | 844626f6dc | |
Tyler Goodlet | 470079665f | |
Tyler Goodlet | 0cd87d9e54 | |
Tyler Goodlet | 09711750bf | |
Tyler Goodlet | 71ca4c8e1f | |
Tyler Goodlet | 9811dcf5f3 | |
Tyler Goodlet | da659cf607 | |
Tyler Goodlet | 37e0ec7b7d | |
Tyler Goodlet | 045b76bab5 | |
Tyler Goodlet | c8c641a038 | |
Tyler Goodlet | 6a1bb13feb | |
Tyler Goodlet | 75591dd7e9 | |
Tyler Goodlet | d792fed099 | |
Tyler Goodlet | d66fb49077 | |
Tyler Goodlet | 78c7c8524c | |
Tyler Goodlet | a746258f99 | |
Tyler Goodlet | 5adb234a24 | |
Tyler Goodlet | 2778ee1401 | |
Tyler Goodlet | e0ca5d5200 | |
Tyler Goodlet | b3d1b1aa63 | |
Tyler Goodlet | 5ec1a72a3d | |
Tyler Goodlet | a342f7d2d4 | |
Tyler Goodlet | 2c76cee928 | |
Tyler Goodlet | b5f2ff854c | |
Tyler Goodlet | 3efb0b5884 | |
Tyler Goodlet | 009bbe456e | |
Tyler Goodlet | daf7b3f4a5 | |
Tyler Goodlet | b0a6dd46e4 | |
Tyler Goodlet | 1c5141f4c6 | |
Tyler Goodlet | 4cdd2271b0 | |
Tyler Goodlet | 89095d4e9f | |
Tyler Goodlet | 04c0d77595 | |
Tyler Goodlet | d1b07c625f | |
Tyler Goodlet | a5bb33b0ff | |
Tyler Goodlet | 8e1ceca43d | |
Tyler Goodlet | c85e7790de | |
Tyler Goodlet | 2399c618b6 | |
Tyler Goodlet | 7ec88f8cac | |
Tyler Goodlet | eacd44dd65 | |
Tyler Goodlet | e5e70a6011 | |
Tyler Goodlet | 7da5c2b238 | |
Tyler Goodlet | 1ee49df31d | |
Tyler Goodlet | f2df32a673 | |
Tyler Goodlet | 125e31dbf3 | |
Tyler Goodlet | 715e693564 | |
Tyler Goodlet | 43717c92d9 | |
Tyler Goodlet | f370685c62 | |
Tyler Goodlet | 4300470786 | |
Tyler Goodlet | b89fd9652c | |
Tyler Goodlet | 51f4afbd88 | |
Tyler Goodlet | 7ef8111381 | |
Tyler Goodlet | 35b097469b | |
Tyler Goodlet | 94290c7d8b | |
Tyler Goodlet | 73379d3627 | |
Tyler Goodlet | 23835f2c08 | |
Tyler Goodlet | d2aee00a56 | |
Tyler Goodlet | cf6e44cb9c | |
Tyler Goodlet | a146ad9e69 | |
Tyler Goodlet | 70ad1a1860 | |
Tyler Goodlet | f3ef73ef41 | |
Tyler Goodlet | a9832dc0cb | |
Tyler Goodlet | 9be245e955 | |
Tyler Goodlet | 800773e585 | |
goodboy | 8d1eb81f16 | |
Tyler Goodlet | 963e5bdd62 | |
Tyler Goodlet | 55de9abc41 | |
Tyler Goodlet | 593db0ed0d | |
Tyler Goodlet | 06622105cd | |
Tyler Goodlet | 008ae47e14 | |
Tyler Goodlet | 81585d9e6e | |
Tyler Goodlet | f6b7057b0d | |
Tyler Goodlet | 76f920a16b | |
Tyler Goodlet | f232d6d4ee | |
Tyler Goodlet | b7e1443618 | |
Tyler Goodlet | 5d021ffb85 | |
Tyler Goodlet | 28fd795280 | |
Tyler Goodlet | c944db5f02 | |
Tyler Goodlet | 967e28b7ac | |
Tyler Goodlet | 2a158aea2c | |
Tyler Goodlet | 88870fdda7 | |
Tyler Goodlet | 326f153a47 | |
Tyler Goodlet | f5cd63ad35 | |
Tyler Goodlet | 1e96ca32df | |
Tyler Goodlet | c088963cf2 | |
Tyler Goodlet | 79fcbcc281 | |
Tyler Goodlet | ddbba76095 | |
Tyler Goodlet | 0a959c1c74 | |
Tyler Goodlet | e348968113 | |
Tyler Goodlet | 7bbe86d6fb | |
Tyler Goodlet | 7b9db86753 | |
Tyler Goodlet | 20a396270e | |
Tyler Goodlet | 81516c5204 | |
Tyler Goodlet | d6fb6fe3ae | |
Tyler Goodlet | 8476d8d056 | |
Tyler Goodlet | 36868bb86e | |
Tyler Goodlet | 29b6b3e54f | |
Tyler Goodlet | 8a01c9e42b | |
Tyler Goodlet | 2c4daf08e0 | |
Tyler Goodlet | 7daab6329d | |
Tyler Goodlet | bb6452b969 | |
Tyler Goodlet | 25bfe6f035 | |
Tyler Goodlet | 32b36aa042 | |
Tyler Goodlet | e7de5404d3 | |
Tyler Goodlet | 18dc8b08e4 | |
Tyler Goodlet | 5bf3cb8e4b | |
Tyler Goodlet | c7d5db5f90 | |
Tyler Goodlet | 1bf1965a8b | |
Tyler Goodlet | 051a8729b6 | |
Tyler Goodlet | 8e85ed92c8 | |
Tyler Goodlet | 2a9042b1b1 | |
Tyler Goodlet | 344a634cb6 | |
Tyler Goodlet | 508de6182a | |
Tyler Goodlet | 40000345a1 | |
goodboy | 220d38b4a9 | |
Esmeralda Gallardo | 888438ca25 | |
goodboy | d84bcf77c0 | |
Guillermo Rodriguez | 0474d66531 | |
algorandpa | f218b804b4 | |
Guillermo Rodriguez | 7b14f498a8 | |
Esmeralda Gallardo | 18e4352faf | |
Esmeralda Gallardo | a6e921548b | |
Esmeralda Gallardo | 3f5dec82ed | |
Esmeralda Gallardo | db0b59abaa | |
algorandpa | f5bcd1d91c | |
algorandpa | db11c3c0f8 | |
Tyler Goodlet | df6071ae9e | |
goodboy | cc1694760c | |
goodboy | 4d8b22dd8f | |
Tyler Goodlet | fd296a557e | |
Tyler Goodlet | 0de2f863bd | |
Tyler Goodlet | de93da202b | |
Tyler Goodlet | 5c459f21be | |
goodboy | 5915cf3acf | |
algorandpa | 997bf31bd4 | |
algorandpa | f3427bb13b | |
algorandpa | 6fa266e3e0 | |
Guillermo Rodriguez | 019a6432fb | |
goodboy | 209e1085ae | |
Tyler Goodlet | 0ef75e6aa6 | |
Tyler Goodlet | 243d0329f6 | |
Tyler Goodlet | a0ce9ecc0d | |
Tyler Goodlet | af9c30c3f5 | |
Zoltan | ebbfa47baf | |
Tyler Goodlet | 02fbc0a0ed | |
goodboy | 4729e4c6bc | |
goodboy | a44b8e3e22 | |
goodboy | 8a89303cb3 | |
Tyler Goodlet | e547b307f6 | |
Tyler Goodlet | 72ec9b1e10 | |
Tyler Goodlet | 40c70ae6d8 | |
Tyler Goodlet | d3fefdeaff | |
Tyler Goodlet | 8be005212f | |
Tyler Goodlet | 5a2795e76b | |
Tyler Goodlet | a987f0ab81 | |
Tyler Goodlet | d99b40317d | |
Tyler Goodlet | 9ae519f6fa | |
Tyler Goodlet | 8f3fe8e542 | |
Tyler Goodlet | 490d85aba5 | |
goodboy | ba2e1e04cd | |
Tyler Goodlet | 5d4929db9c | |
Tyler Goodlet | c41400ae18 | |
Tyler Goodlet | e71bd2cb1e | |
Tyler Goodlet | be24473fb4 | |
Tyler Goodlet | b524ea5c22 | |
Tyler Goodlet | d46945cb09 | |
Tyler Goodlet | 1d4fc6f327 | |
Tyler Goodlet | 5976acbe76 | |
goodboy | 11ecf9cb09 | |
goodboy | 2dac531729 | |
Tyler Goodlet | 1fadf58ab7 | |
Tyler Goodlet | ceca0d9fb7 | |
Tyler Goodlet | df16726211 | |
Tyler Goodlet | fb4f1732b6 | |
Tyler Goodlet | d5b357b69a | |
Tyler Goodlet | 610fb5f7c6 | |
Tyler Goodlet | 2b231ba631 | |
Tyler Goodlet | 286228c290 | |
Tyler Goodlet | a1a24da7b6 | |
Tyler Goodlet | 553d0557b6 | |
Tyler Goodlet | 2f7b272d8c | |
Tyler Goodlet | dc1edeecda | |
Tyler Goodlet | 4ca7817735 | |
Tyler Goodlet | 5b63585398 | |
Tyler Goodlet | 0000d9a314 | |
Tyler Goodlet | f7ec66362e | |
Tyler Goodlet | b7ef0596b9 | |
Tyler Goodlet | 143e86a80c | |
Tyler Goodlet | 956c7d3435 | |
Tyler Goodlet | 330d16262e | |
Tyler Goodlet | c7f57b940c | |
Tyler Goodlet | 27bd3c07af | |
Tyler Goodlet | 55dc27a197 | |
Tyler Goodlet | a11f20fac2 | |
Tyler Goodlet | daebb78755 | |
Tyler Goodlet | 90a395a069 | |
Tyler Goodlet | 23d0353934 | |
Tyler Goodlet | ede67ed184 | |
Tyler Goodlet | 811d21e111 | |
Tyler Goodlet | 54567d33da | |
Tyler Goodlet | 61ca5f7e19 | |
Tyler Goodlet | 7396624be0 | |
Tyler Goodlet | 25b90afbdb | |
Tyler Goodlet | 72dfeb2b4e | |
Tyler Goodlet | 6b34c9e866 | |
Tyler Goodlet | e7ec01b8e6 | |
Tyler Goodlet | fce7055c62 | |
Tyler Goodlet | bf7d5e9a71 | |
Tyler Goodlet | 2a866dde65 | |
Tyler Goodlet | 220981e718 | |
Tyler Goodlet | 8537a4091b | |
Tyler Goodlet | 71a11a23bd | |
Tyler Goodlet | fa368b1263 | |
Tyler Goodlet | e6dd1458f8 | |
Tyler Goodlet | 9486d993ce | |
Tyler Goodlet | 30994dac10 | |
Tyler Goodlet | 8a61211c8c | |
Tyler Goodlet | c43f7eb656 | |
goodboy | d05caa4b02 | |
Tyler Goodlet | 63e9af002d | |
goodboy | 5144299f4f | |
Tyler Goodlet | c437f9370a | |
Tyler Goodlet | 94f81587ab | |
Tyler Goodlet | 2bc25e3593 | |
Tyler Goodlet | 1d9ab7b0de | |
Tyler Goodlet | 4c96a4878e | |
Tyler Goodlet | 8cd56cb6d3 | |
Tyler Goodlet | c246dcef6f | |
Tyler Goodlet | 26d6e10ad7 | |
Tyler Goodlet | 3924c66bd0 | |
Tyler Goodlet | 2fbfe583dd | |
Tyler Goodlet | 525f805cdb | |
Tyler Goodlet | b65c02336d | |
Tyler Goodlet | d3abfce540 | |
Tyler Goodlet | 49433ea87d | |
goodboy | 31b0d8cee8 | |
Tyler Goodlet | 35871d0213 | |
Tyler Goodlet | 4877af9bc3 | |
Tyler Goodlet | 909e068121 | |
Tyler Goodlet | cf835b97ca | |
Tyler Goodlet | 30bce42c0b | |
Tyler Goodlet | 48ff4859e6 | |
Tyler Goodlet | 887583d27f | |
Tyler Goodlet | 45b97bf6c3 | |
Tyler Goodlet | 91397b85a4 | |
Tyler Goodlet | 47f81b31af | |
goodboy | 30c452cfd0 | |
Tyler Goodlet | fda1c5b554 | |
goodboy | d6c9834a9a | |
Tyler Goodlet | 41b0c11aaa | |
Tyler Goodlet | cc67d23eee | |
Tyler Goodlet | 4818af1445 | |
Tyler Goodlet | 2cf1742999 | |
Tyler Goodlet | 25ac6e6665 | |
Tyler Goodlet | 90754f979b | |
Tyler Goodlet | c0d490ed63 | |
Tyler Goodlet | 7c6d12d982 | |
Tyler Goodlet | fd8c05e024 | |
Tyler Goodlet | 5d65c86c84 | |
Tyler Goodlet | cf11e8d7d8 | |
Tyler Goodlet | ed868f6246 | |
goodboy | 5d371ad80e | |
Tyler Goodlet | 6897aed6b6 | |
Tyler Goodlet | a61a11f86b | |
Tyler Goodlet | 286f620f8e | |
Tyler Goodlet | b7e60b9653 | |
Tyler Goodlet | df42e7acc4 | |
Tyler Goodlet | e492e9ca0c | |
Tyler Goodlet | 44c6f6dfda | |
Tyler Goodlet | ad2100fe3f | |
Tyler Goodlet | ae64ac79a6 | |
Tyler Goodlet | 20663dfa1c | |
Tyler Goodlet | 70f2241d22 | |
Tyler Goodlet | b3fcc25e21 | |
Tyler Goodlet | 4f15ce346b | |
Tyler Goodlet | 445849337f | |
Tyler Goodlet | 3fd7107e08 | |
Tyler Goodlet | 73a02d54b7 | |
Tyler Goodlet | b734af6dd0 | |
Tyler Goodlet | f7c0ee930a | |
Tyler Goodlet | ead426abc4 | |
Tyler Goodlet | bcd6bbb7ca | |
Tyler Goodlet | 80929d080f | |
Tyler Goodlet | eed47b3733 | |
Tyler Goodlet | d5f0c59b57 | |
Tyler Goodlet | d11dc787a1 | |
Tyler Goodlet | 1e81feee46 | |
Tyler Goodlet | 40a9761943 | |
Tyler Goodlet | 256bcf36d3 | |
Tyler Goodlet | 9944277096 | |
Tyler Goodlet | f9dc5637fa | |
Tyler Goodlet | addedc20f1 | |
Tyler Goodlet | 1fa6e8d9ba | |
Tyler Goodlet | 2a06dc997f | |
Tyler Goodlet | 6b93eedcda | |
Tyler Goodlet | a786df65de | |
Tyler Goodlet | 8f2823d5f0 | |
Tyler Goodlet | 58fe220fde | |
Tyler Goodlet | 161448c31a | |
Tyler Goodlet | 1c685189d1 | |
Tyler Goodlet | ceac3f2ee4 | |
Tyler Goodlet | a07367fae2 | |
Tyler Goodlet | 006190d227 | |
Tyler Goodlet | 412197019e | |
Tyler Goodlet | 271e378ce3 | |
Tyler Goodlet | 8e07fda88f | |
Tyler Goodlet | a4935b8fa8 | |
Tyler Goodlet | 2b76baeb10 | |
Tyler Goodlet | 2dfa8976a0 | |
Tyler Goodlet | d3402f715b | |
Tyler Goodlet | f070f9a984 | |
Tyler Goodlet | 416270ee6c | |
Tyler Goodlet | 14bee778ec | |
Tyler Goodlet | 10c1944de5 | |
Tyler Goodlet | 7958d8ad4f | |
Tyler Goodlet | 50c5dc255c | |
Tyler Goodlet | 31735f26d3 | |
Tyler Goodlet | 2ef6460853 | |
Tyler Goodlet | 5e98a30537 | |
Tyler Goodlet | dd03ef42ac | |
Tyler Goodlet | 59884d251e | |
Tyler Goodlet | e06e257a81 | |
Tyler Goodlet | 6e574835c8 | |
Tyler Goodlet | 49ccfdd673 | |
Tyler Goodlet | 3a434f312b | |
Tyler Goodlet | bb4dc448b3 | |
Tyler Goodlet | 9846396df2 | |
Tyler Goodlet | f0d417ce42 | |
Tyler Goodlet | 55fc4114b4 | |
Tyler Goodlet | 97b074365b | |
Tyler Goodlet | f79c3617d6 | |
Tyler Goodlet | 861fe791eb | |
Tyler Goodlet | 60052ff73a | |
Tyler Goodlet | 4d2708cd42 | |
Tyler Goodlet | d1cc52dff5 | |
Tyler Goodlet | 4fa901dbcb | |
goodboy | f2c488c1e0 | |
Tyler Goodlet | 4a9c16d298 | |
Tyler Goodlet | b9d5b904f4 | |
Tyler Goodlet | 0aef762d9a | |
goodboy | c724117c1a | |
Tyler Goodlet | cc3bb85c66 | |
goodboy | 20817313b1 | |
Tyler Goodlet | 23d0b8a7ac | |
goodboy | 087a34f061 | |
Tyler Goodlet | 653f5c824b | |
Tyler Goodlet | f9217570ab | |
Tyler Goodlet | 7f224f0342 | |
Tyler Goodlet | 75a5f3795a | |
Tyler Goodlet | de9f215c83 | |
Tyler Goodlet | 848e345364 | |
Tyler Goodlet | 38b190e598 | |
Tyler Goodlet | 3a9bc8058f | |
Guillermo Rodriguez | 739a231afc | |
Tyler Goodlet | 7dfa4c3cde | |
Tyler Goodlet | 7b653fe4f4 | |
goodboy | 77a687bced | |
Tyler Goodlet | d5c1cdd91d | |
Tyler Goodlet | 46d3fe88ca | |
Tyler Goodlet | 5c8c5d8fbf | |
goodboy | 71412310c4 | |
Guillermo Rodriguez | 0c323fdc0b | |
Tyler Goodlet | 02f53d0c13 | |
Tyler Goodlet | 8792c97de6 | |
Tyler Goodlet | 980815d075 | |
Tyler Goodlet | 4cedfedc21 | |
Tyler Goodlet | fe3d0c6fdd | |
Tyler Goodlet | 9200e8da57 | |
Tyler Goodlet | 430d065da6 | |
Tyler Goodlet | ecd93cb05a | |
Guillermo Rodriguez | 4facd161a9 | |
goodboy | c5447fda06 | |
Guillermo Rodriguez | 0447612b34 | |
goodboy | b5499b8225 | |
Guillermo Rodriguez | 00aabddfe8 | |
Guillermo Rodriguez | 43fb720877 | |
Guillermo Rodriguez | 9626dbd7ac | |
Guillermo Rodriguez | f286c79a03 | |
Guillermo Rodriguez | accb0eee6c | |
Guillermo Rodriguez | e97dd1cbdb | |
Guillermo Rodriguez | 34fb497eb4 | |
Guillermo Rodriguez | 6669ba6590 | |
Guillermo Rodriguez | cb8099bb8c | |
Guillermo Rodriguez | 80a1a58bfc | |
Guillermo Rodriguez | d60f222bb7 | |
Guillermo Rodriguez | 2c2e43d8ac | |
Guillermo Rodriguez | 212b3d620d | |
Guillermo Rodriguez | 92090b01b8 | |
Guillermo Rodriguez | 9073fbc317 | |
Guillermo Rodriguez | f55f56a29f | |
Guillermo Rodriguez | 28e025d02e | |
Guillermo Rodriguez | e558e5837e | |
Guillermo Rodriguez | a0b415095a | |
Guillermo Rodriguez | 6df181c233 | |
Guillermo Rodriguez | 7acc4e3208 | |
Guillermo Rodriguez | 10ea242143 | |
Tyler Goodlet | eda6ecd529 | |
goodboy | cf5b0bf9c6 | |
Tyler Goodlet | b9dba48306 | |
Tyler Goodlet | 4d2e23b5ce | |
Tyler Goodlet | 973bf87e67 | |
Tyler Goodlet | 5861839783 | |
Tyler Goodlet | 06845e5504 | |
Tyler Goodlet | 43bdd4d022 | |
Tyler Goodlet | bafd2cb44f | |
Tyler Goodlet | be8fd32e7d | |
Tyler Goodlet | ee8c00684b | |
Tyler Goodlet | 7379dc03af | |
Tyler Goodlet | a602c47d47 | |
Tyler Goodlet | 317610e00a | |
Tyler Goodlet | c4af706d51 | |
Tyler Goodlet | 665bb183f7 | |
Tyler Goodlet | f6ba95a6c7 | |
Tyler Goodlet | e2cd8c4aef | |
Tyler Goodlet | c8bff81220 | |
Tyler Goodlet | 2aec1c5f1d | |
Tyler Goodlet | bec32956a8 | |
Tyler Goodlet | 91fdc7c5c7 | |
Tyler Goodlet | b59ed74bc1 | |
Tyler Goodlet | 16012f6f02 | |
Tyler Goodlet | 2b61672723 | |
Tyler Goodlet | 176b230a46 | |
Tyler Goodlet | 7fa9dbf869 | |
Tyler Goodlet | 87ed9abefa | |
Tyler Goodlet | 2548aae73d | |
Tyler Goodlet | 1cfa04927d | |
Tyler Goodlet | e34ea94f9f | |
Tyler Goodlet | 1510383738 | |
Tyler Goodlet | 016b669d63 | |
Tyler Goodlet | 682a0191ef | |
Tyler Goodlet | 9e36dbe47f | |
goodboy | 8bef67642e | |
Tyler Goodlet | 52febac6ae | |
Tyler Goodlet | f202699c25 | |
Tyler Goodlet | 0fb07670d2 | |
Tyler Goodlet | 73d2e7716f | |
Tyler Goodlet | 999ae5a1c6 | |
Tyler Goodlet | 23ba0e5e69 | |
Tyler Goodlet | 941a2196b3 | |
Tyler Goodlet | 0cf4e07b84 | |
Tyler Goodlet | 7bec989eed | |
Tyler Goodlet | 6856ca207f | |
Guillermo Rodriguez | 2e5616850c | |
Tyler Goodlet | a83bd9c608 | |
goodboy | 9651ca84bf | |
Tyler Goodlet | 109b35f6eb | |
Tyler Goodlet | e28c1748fc | |
Tyler Goodlet | 72889b4d1f | |
Tyler Goodlet | ae001c3dd7 | |
Tyler Goodlet | 2309e7ab05 | |
Tyler Goodlet | 46c51b55f7 | |
goodboy | a9185e7d6f | |
Tyler Goodlet | 3a0987e0be | |
Tyler Goodlet | d280a592b1 | |
goodboy | ef5829a6b7 | |
Tyler Goodlet | 30bcfdcc83 | |
Tyler Goodlet | 1a291939c3 | |
Tyler Goodlet | 69e501764a | |
goodboy | 7f3f7f0372 | |
Tyler Goodlet | 1cbf45b4c4 | |
Tyler Goodlet | 227a80469e | |
Tyler Goodlet | dc8072c6db | |
Tyler Goodlet | 808dbb12e6 | |
Tyler Goodlet | 44e21b1de9 | |
Tyler Goodlet | b3058b8c78 | |
Tyler Goodlet | db564d7977 | |
Tyler Goodlet | e6a3e8b65a | |
Tyler Goodlet | d43ba47ebe | |
Tyler Goodlet | 168c9863cb | |
Tyler Goodlet | 0fb31586fd | |
Tyler Goodlet | 8b609f531b | |
Tyler Goodlet | d502274eb9 | |
Tyler Goodlet | b1419c850d | |
Tyler Goodlet | aa7f24b6db | |
Tyler Goodlet | 319e68c855 | |
Tyler Goodlet | 64f920d7e5 | |
Tyler Goodlet | 3b79743c7b | |
Tyler Goodlet | 54008a1976 | |
Tyler Goodlet | b96b7a8b9c | |
Tyler Goodlet | 0fca1b3e1a | |
Tyler Goodlet | 2386270cad | |
Tyler Goodlet | 5b135fad61 | |
Tyler Goodlet | abb6854e74 | |
Tyler Goodlet | 22f9b2552c | |
Tyler Goodlet | 57f2478dc7 | |
Tyler Goodlet | 5dc9a61ec4 | |
Tyler Goodlet | b0d3d9bb01 | |
Tyler Goodlet | caecbaa231 | |
Tyler Goodlet | a20a8d95d5 | |
Tyler Goodlet | ba93f96c71 | |
Tyler Goodlet | 804e9afdde | |
Tyler Goodlet | 89bcaed15e | |
Tyler Goodlet | bb2f8e4304 | |
Tyler Goodlet | 8ab8268edc | |
Tyler Goodlet | bbcc55b24c | |
Tyler Goodlet | 9fa9c27e4d | |
Tyler Goodlet | d9b4c4a413 | |
Tyler Goodlet | 84cab1327d | |
Tyler Goodlet | df4cec930b | |
Tyler Goodlet | ab08dc582d | |
Tyler Goodlet | f79d9865a0 | |
Tyler Goodlet | 00378c330c | |
goodboy | 180b97b180 | |
Tyler Goodlet | f0b3a4d5c0 | |
goodboy | e2e66324cc | |
Tyler Goodlet | d950c78b81 | |
Tyler Goodlet | 7dbcbfdcd5 | |
Tyler Goodlet | 279c899de5 | |
Tyler Goodlet | db5aacdb9c | |
Tyler Goodlet | c7b84ab500 | |
Tyler Goodlet | 9967adb371 | |
Tyler Goodlet | 30ff793a22 | |
Tyler Goodlet | 666587991a | |
goodboy | 01005e40a8 | |
goodboy | d81e629c29 | |
Tyler Goodlet | 2766fad719 | |
Tyler Goodlet | ae71168216 | |
Tyler Goodlet | a0c238daa7 | |
Tyler Goodlet | 7cbdc6a246 | |
Tyler Goodlet | 2ff8be71aa | |
Tyler Goodlet | ddffaa952d | |
Tyler Goodlet | 5520e9ef21 | |
Tyler Goodlet | 958e542f7d | |
goodboy | 927bbc7258 | |
Tyler Goodlet | 45bef0cea9 | |
goodboy | a3d46f713e | |
Tyler Goodlet | 5684120c11 | |
Tyler Goodlet | ddb195ed2c | |
Tyler Goodlet | 6747831677 | |
Tyler Goodlet | 9326379b04 | |
Tyler Goodlet | 09d9a7ea2b | |
Tyler Goodlet | 45871d5846 | |
goodboy | bf7a49c19b | |
goodboy | 0a7fce087c | |
Tyler Goodlet | d3130ca04c | |
Tyler Goodlet | e30a3c5b54 | |
Tyler Goodlet | 2393965e83 | |
Tyler Goodlet | fb39da19f4 | |
Tyler Goodlet | a27431c34f | |
Tyler Goodlet | 070b9f3dc1 | |
goodboy | f2dba44169 | |
Tyler Goodlet | 0ef5da0881 | |
Tyler Goodlet | 0580b204a3 | |
Tyler Goodlet | 6ce699ae1f | |
Tyler Goodlet | 3aa72abacf | |
Tyler Goodlet | 04004525c1 | |
Tyler Goodlet | a7f0adf1cf | |
Tyler Goodlet | cef511092d | |
Tyler Goodlet | 4e5df973a9 | |
Tyler Goodlet | 6a1a62d8c0 | |
Tyler Goodlet | e0491cf2e7 | |
Tyler Goodlet | 90bc9b9730 | |
goodboy | f449672c68 | |
Tyler Goodlet | fd22f45178 | |
goodboy | 37f634a2ed | |
Tyler Goodlet | dfee9dd97e | |
Tyler Goodlet | 2a99f7a4d7 | |
Tyler Goodlet | b44e2d9ed9 | |
Tyler Goodlet | 795d4d76f4 | |
Tyler Goodlet | c26acb1fa8 | |
Tyler Goodlet | 11b6699a54 | |
Tyler Goodlet | f9bdd643cf | |
Tyler Goodlet | 2baea21c7d | |
Tyler Goodlet | bea0111753 | |
Tyler Goodlet | c870665be0 | |
Tyler Goodlet | 4ff1090284 | |
Tyler Goodlet | f22461a844 | |
Tyler Goodlet | 458c7211ee | |
Tyler Goodlet | 5cc4b19a7c | |
goodboy | f5236f658b | |
goodboy | a360b66cc0 | |
Tyler Goodlet | 4bcb791161 | |
Tyler Goodlet | 4c7c78c815 | |
Tyler Goodlet | 019867b413 | |
Tyler Goodlet | f356fb0a68 | |
goodboy | 756249ff70 | |
goodboy | 419ebebe72 | |
goodboy | a229996ebe | |
Tyler Goodlet | af01e89612 | |
Tyler Goodlet | 609034c634 | |
Tyler Goodlet | 95dd0e6bd6 | |
goodboy | 479ad1bb15 | |
Tyler Goodlet | d506235a8b | |
Tyler Goodlet | 7846446a44 | |
Tyler Goodlet | 214f864dcf | |
Tyler Goodlet | 4c0f2099aa | |
Tyler Goodlet | aea7bec2c3 | |
Tyler Goodlet | 47777e4192 | |
Tyler Goodlet | f6888057c3 | |
Tyler Goodlet | f65f56ec75 | |
Tyler Goodlet | 5d39b04552 | |
Tyler Goodlet | 735fbc6259 | |
Tyler Goodlet | fcd7e0f3f3 | |
Tyler Goodlet | 9106d13dfe | |
Tyler Goodlet | d3caad6e11 | |
Tyler Goodlet | f87a2a810a | |
Tyler Goodlet | 208e2e9e97 | |
Tyler Goodlet | 90cc6eb317 | |
Tyler Goodlet | b118becc84 | |
Tyler Goodlet | 7442d68ecf | |
Tyler Goodlet | 076c167d6e | |
Tyler Goodlet | 64d8cd448f | |
Tyler Goodlet | ec6a28a8b1 | |
Tyler Goodlet | cc15d02488 | |
goodboy | d5bc43e8dd | |
Tyler Goodlet | 287a2c8396 | |
Tyler Goodlet | 453ebdfe30 | |
Tyler Goodlet | 2b1fb90e03 | |
Tyler Goodlet | 695ba5288d | |
Tyler Goodlet | d6c32bba86 | |
Tyler Goodlet | fa89207583 | |
Tyler Goodlet | 557562e25c | |
Tyler Goodlet | c6efa2641b | |
Tyler Goodlet | 8a7e391b4e | |
Tyler Goodlet | aec48a1dd5 | |
Tyler Goodlet | 87f301500d | |
Tyler Goodlet | 566a54ffb6 | |
Tyler Goodlet | f9c4b3cc96 | |
Tyler Goodlet | a12e6800ff | |
Tyler Goodlet | cc68501c7a | |
Tyler Goodlet | 7ebf8a8dc0 | |
Tyler Goodlet | 4475823e48 | |
Tyler Goodlet | 3713288b48 | |
Tyler Goodlet | 4fdfb81876 | |
Tyler Goodlet | f32b4d37cb | |
Tyler Goodlet | 2063b9d8bb | |
Tyler Goodlet | fe14605034 | |
Tyler Goodlet | 68b32208de | |
Tyler Goodlet | f1fe369bbf | |
Tyler Goodlet | 16b2937d23 | |
Tyler Goodlet | bfad676b7c | |
Tyler Goodlet | c617a06905 | |
Tyler Goodlet | ff74f4302a | |
Tyler Goodlet | 21153a0e1e | |
Tyler Goodlet | b6f344f34a | |
Tyler Goodlet | ecdc747ced | |
Tyler Goodlet | 5147cd7be0 | |
Tyler Goodlet | 3dcb72d429 | |
Tyler Goodlet | fbee33b00d | |
Tyler Goodlet | 3991d8f911 | |
Tyler Goodlet | 7b2e8f1ba5 | |
Tyler Goodlet | cbcbb2b243 | |
Tyler Goodlet | cd3bfb1ea4 | |
Tyler Goodlet | 82b718d5a3 | |
Tyler Goodlet | 05a1a4e3d8 | |
Tyler Goodlet | 412138a75b | |
Tyler Goodlet | c1b63f4757 | |
Tyler Goodlet | 5d774bef90 | |
Tyler Goodlet | de77c7d209 | |
Tyler Goodlet | ce1eb11b59 | |
Tyler Goodlet | b629ce177d | |
Tyler Goodlet | 73fa320917 | |
Tyler Goodlet | dd05ed1371 | |
Tyler Goodlet | 2a641ab8b4 | |
Tyler Goodlet | f8f7ca350c | |
Tyler Goodlet | 88b4ccc768 | |
Tyler Goodlet | eb2bad5138 | |
Tyler Goodlet | f768576060 | |
Tyler Goodlet | add0e92335 | |
Tyler Goodlet | 1eb7e109e6 | |
Tyler Goodlet | 725909a94c | |
Tyler Goodlet | 050aa7594c | |
Tyler Goodlet | 450009ff9c | |
goodboy | b2d5892010 | |
goodboy | 5a3b465ac0 | |
Tyler Goodlet | be7afdaa89 | |
Tyler Goodlet | 1c561207f5 | |
Tyler Goodlet | ed2c962bb9 | |
Tyler Goodlet | 147ceca016 | |
Tyler Goodlet | 03a7940f83 | |
Tyler Goodlet | dd2a9f74f1 | |
Tyler Goodlet | 49c720af3c | |
Tyler Goodlet | c620517543 | |
Tyler Goodlet | a425c29ef1 | |
Tyler Goodlet | 783914c7fe | |
Tyler Goodlet | 920a394539 | |
Tyler Goodlet | e977597cd0 | |
Tyler Goodlet | 7a33ba64f1 | |
Tyler Goodlet | 191b94b67c | |
Tyler Goodlet | 4ad7b073c3 | |
Tyler Goodlet | d92ff9c7a0 |
|
@ -3,9 +3,8 @@ name: CI
|
||||||
|
|
||||||
on:
|
on:
|
||||||
# Triggers the workflow on push or pull request events but only for the master branch
|
# Triggers the workflow on push or pull request events but only for the master branch
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
pull_request:
|
||||||
|
push:
|
||||||
branches: [ master ]
|
branches: [ master ]
|
||||||
|
|
||||||
# Allows you to run this workflow manually from the Actions tab
|
# Allows you to run this workflow manually from the Actions tab
|
||||||
|
@ -14,6 +13,27 @@ on:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
|
# test that we can generate a software distribution and install it
|
||||||
|
# thus avoid missing file issues after packaging.
|
||||||
|
sdist-linux:
|
||||||
|
name: 'sdist'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Setup python
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: '3.10'
|
||||||
|
|
||||||
|
- name: Build sdist
|
||||||
|
run: python setup.py sdist --formats=zip
|
||||||
|
|
||||||
|
- name: Install sdist from .zips
|
||||||
|
run: python -m pip install dist/*.zip
|
||||||
|
|
||||||
testing:
|
testing:
|
||||||
name: 'install + test-suite'
|
name: 'install + test-suite'
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
|
@ -50,3 +50,8 @@ prefer_data_account = [
|
||||||
paper = "XX0000000"
|
paper = "XX0000000"
|
||||||
margin = "X0000000"
|
margin = "X0000000"
|
||||||
ira = "X0000000"
|
ira = "X0000000"
|
||||||
|
|
||||||
|
|
||||||
|
[deribit]
|
||||||
|
key_id = 'XXXXXXXX'
|
||||||
|
key_secret = 'Xx_XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx'
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
version: "3.5"
|
version: "3.5"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
ib-gateway:
|
ib_gw_paper:
|
||||||
# other image tags available:
|
# other image tags available:
|
||||||
# https://github.com/waytrade/ib-gateway-docker#supported-tags
|
# https://github.com/waytrade/ib-gateway-docker#supported-tags
|
||||||
image: waytrade/ib-gateway:981.3j
|
# image: waytrade/ib-gateway:981.3j
|
||||||
restart: always
|
image: waytrade/ib-gateway:1012.2i
|
||||||
|
restart: 'no' # restart on boot whenev there's a crash or user clicsk
|
||||||
network_mode: 'host'
|
network_mode: 'host'
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
@ -39,14 +40,12 @@ services:
|
||||||
# this compose file which looks something like:
|
# this compose file which looks something like:
|
||||||
# TWS_USERID='myuser'
|
# TWS_USERID='myuser'
|
||||||
# TWS_PASSWORD='guest'
|
# TWS_PASSWORD='guest'
|
||||||
# TRADING_MODE=paper (or live)
|
|
||||||
# VNC_SERVER_PASSWORD='diggity'
|
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
TWS_USERID: ${TWS_USERID}
|
TWS_USERID: ${TWS_USERID}
|
||||||
TWS_PASSWORD: ${TWS_PASSWORD}
|
TWS_PASSWORD: ${TWS_PASSWORD}
|
||||||
TRADING_MODE: ${TRADING_MODE:-paper}
|
TRADING_MODE: 'paper'
|
||||||
VNC_SERVER_PASSWORD: ${VNC_SERVER_PASSWORD:-}
|
VNC_SERVER_PASSWORD: 'doggy'
|
||||||
|
VNC_SERVER_PORT: '3003'
|
||||||
|
|
||||||
# ports:
|
# ports:
|
||||||
# - target: 4002
|
# - target: 4002
|
||||||
|
@ -62,3 +61,40 @@ services:
|
||||||
# - "127.0.0.1:4001:4001"
|
# - "127.0.0.1:4001:4001"
|
||||||
# - "127.0.0.1:4002:4002"
|
# - "127.0.0.1:4002:4002"
|
||||||
# - "127.0.0.1:5900:5900"
|
# - "127.0.0.1:5900:5900"
|
||||||
|
|
||||||
|
# ib_gw_live:
|
||||||
|
# image: waytrade/ib-gateway:1012.2i
|
||||||
|
# restart: no
|
||||||
|
# network_mode: 'host'
|
||||||
|
|
||||||
|
# volumes:
|
||||||
|
# - type: bind
|
||||||
|
# source: ./jts_live.ini
|
||||||
|
# target: /root/jts/jts.ini
|
||||||
|
# # don't let ibc clobber this file for
|
||||||
|
# # the main reason of not having a stupid
|
||||||
|
# # timezone set..
|
||||||
|
# read_only: true
|
||||||
|
|
||||||
|
# # force our own ibc config
|
||||||
|
# - type: bind
|
||||||
|
# source: ./ibc.ini
|
||||||
|
# target: /root/ibc/config.ini
|
||||||
|
|
||||||
|
# # force our noop script - socat isn't needed in host mode.
|
||||||
|
# - type: bind
|
||||||
|
# source: ./fork_ports_delayed.sh
|
||||||
|
# target: /root/scripts/fork_ports_delayed.sh
|
||||||
|
|
||||||
|
# # force our noop script - socat isn't needed in host mode.
|
||||||
|
# - type: bind
|
||||||
|
# source: ./run_x11_vnc.sh
|
||||||
|
# target: /root/scripts/run_x11_vnc.sh
|
||||||
|
# read_only: true
|
||||||
|
|
||||||
|
# # NOTE: to fill these out, define an `.env` file in the same dir as
|
||||||
|
# # this compose file which looks something like:
|
||||||
|
# environment:
|
||||||
|
# TRADING_MODE: 'live'
|
||||||
|
# VNC_SERVER_PASSWORD: 'doggy'
|
||||||
|
# VNC_SERVER_PORT: '3004'
|
||||||
|
|
|
@ -188,7 +188,7 @@ AcceptNonBrokerageAccountWarning=yes
|
||||||
#
|
#
|
||||||
# The default value is 60.
|
# The default value is 60.
|
||||||
|
|
||||||
LoginDialogDisplayTimeout = 60
|
LoginDialogDisplayTimeout=20
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -292,7 +292,7 @@ ExistingSessionDetectedAction=primary
|
||||||
# be set dynamically at run-time: most users will never need it,
|
# be set dynamically at run-time: most users will never need it,
|
||||||
# so don't use it unless you know you need it.
|
# so don't use it unless you know you need it.
|
||||||
|
|
||||||
OverrideTwsApiPort=4002
|
; OverrideTwsApiPort=4002
|
||||||
|
|
||||||
|
|
||||||
# Read-only Login
|
# Read-only Login
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
[IBGateway]
|
||||||
|
ApiOnly=true
|
||||||
|
LocalServerPort=4001
|
||||||
|
# NOTE: must be set if using IBC's "reject" mode
|
||||||
|
TrustedIPs=127.0.0.1
|
||||||
|
; RemoteHostOrderRouting=ndc1.ibllc.com
|
||||||
|
; WriteDebug=true
|
||||||
|
; RemotePortOrderRouting=4001
|
||||||
|
; useRemoteSettings=false
|
||||||
|
; tradingMode=p
|
||||||
|
; Steps=8
|
||||||
|
; colorPalletName=dark
|
||||||
|
|
||||||
|
# window geo, this may be useful for sending `xdotool` commands?
|
||||||
|
; MainWindow.Width=1986
|
||||||
|
; screenHeight=3960
|
||||||
|
|
||||||
|
|
||||||
|
[Logon]
|
||||||
|
Locale=en
|
||||||
|
# most markets are oriented around this zone
|
||||||
|
# so might as well hard code it.
|
||||||
|
TimeZone=America/New_York
|
||||||
|
UseSSL=true
|
||||||
|
displayedproxymsg=1
|
||||||
|
os_titlebar=true
|
||||||
|
s3store=true
|
||||||
|
useRemoteSettings=false
|
||||||
|
|
||||||
|
[Communication]
|
||||||
|
ctciAutoEncrypt=true
|
||||||
|
Region=usr
|
||||||
|
; Peer=cdc1.ibllc.com:4001
|
|
@ -1,16 +1,35 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
# start vnc server and listen for connections
|
||||||
|
# on port specced in `$VNC_SERVER_PORT`
|
||||||
|
|
||||||
# start VNC server
|
|
||||||
x11vnc \
|
x11vnc \
|
||||||
-ncache_cr \
|
-listen 127.0.0.1 \
|
||||||
-listen localhost \
|
-allow 127.0.0.1 \
|
||||||
|
-rfbport "${VNC_SERVER_PORT}" \
|
||||||
-display :1 \
|
-display :1 \
|
||||||
-forever \
|
-forever \
|
||||||
-shared \
|
-shared \
|
||||||
-logappend /var/log/x11vnc.log \
|
|
||||||
-bg \
|
-bg \
|
||||||
|
-nowf \
|
||||||
|
-noxdamage \
|
||||||
|
-noxfixes \
|
||||||
|
-no6 \
|
||||||
-noipv6 \
|
-noipv6 \
|
||||||
-autoport 3003 \
|
|
||||||
# can't use this because of ``asyncvnc`` issue:
|
|
||||||
|
# -nowcr \
|
||||||
|
# TODO: can't use this because of ``asyncvnc`` issue:
|
||||||
# https://github.com/barneygale/asyncvnc/issues/1
|
# https://github.com/barneygale/asyncvnc/issues/1
|
||||||
# -passwd 'ibcansmbz'
|
# -passwd 'ibcansmbz'
|
||||||
|
|
||||||
|
# XXX: optional graphics caching flags that seem to rekt the overlay
|
||||||
|
# of the 2 gw windows? When running a single gateway
|
||||||
|
# this seems to maybe optimize some memory usage?
|
||||||
|
# -ncache_cr \
|
||||||
|
# -ncache \
|
||||||
|
|
||||||
|
# NOTE: this will prevent logs from going to the console.
|
||||||
|
# -logappend /var/log/x11vnc.log \
|
||||||
|
|
||||||
|
# where to start allocating ports
|
||||||
|
# -autoport "${VNC_SERVER_PORT}" \
|
||||||
|
|
|
@ -18,3 +18,10 @@
|
||||||
piker: trading gear for hackers.
|
piker: trading gear for hackers.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from ._daemon import open_piker_runtime
|
||||||
|
from .data.feed import open_feed
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'open_piker_runtime',
|
||||||
|
'open_feed',
|
||||||
|
]
|
||||||
|
|
483
piker/_daemon.py
483
piker/_daemon.py
|
@ -18,16 +18,27 @@
|
||||||
Structured, daemon tree service management.
|
Structured, daemon tree service management.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from typing import Optional, Union, Callable, Any
|
from __future__ import annotations
|
||||||
from contextlib import asynccontextmanager as acm
|
import os
|
||||||
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
Callable,
|
||||||
|
Any,
|
||||||
|
ClassVar,
|
||||||
|
)
|
||||||
|
from contextlib import (
|
||||||
|
asynccontextmanager as acm,
|
||||||
|
)
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from pydantic import BaseModel
|
import tractor
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
import tractor
|
|
||||||
|
|
||||||
from .log import get_logger, get_console_log
|
from .log import (
|
||||||
|
get_logger,
|
||||||
|
get_console_log,
|
||||||
|
)
|
||||||
from .brokers import get_brokermod
|
from .brokers import get_brokermod
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,28 +46,118 @@ log = get_logger(__name__)
|
||||||
|
|
||||||
_root_dname = 'pikerd'
|
_root_dname = 'pikerd'
|
||||||
|
|
||||||
_registry_addr = ('127.0.0.1', 6116)
|
_default_registry_host: str = '127.0.0.1'
|
||||||
_tractor_kwargs: dict[str, Any] = {
|
_default_registry_port: int = 6116
|
||||||
# use a different registry addr then tractor's default
|
_default_reg_addr: tuple[str, int] = (
|
||||||
'arbiter_addr': _registry_addr
|
_default_registry_host,
|
||||||
}
|
_default_registry_port,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE: this value is set as an actor-global once the first endpoint
|
||||||
|
# who is capable, spawns a `pikerd` service tree.
|
||||||
|
_registry: Registry | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class Registry:
|
||||||
|
addr: None | tuple[str, int] = None
|
||||||
|
|
||||||
|
# TODO: table of uids to sockaddrs
|
||||||
|
peers: dict[
|
||||||
|
tuple[str, str],
|
||||||
|
tuple[str, int],
|
||||||
|
] = {}
|
||||||
|
|
||||||
|
|
||||||
|
_tractor_kwargs: dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_registry(
|
||||||
|
addr: None | tuple[str, int] = None,
|
||||||
|
ensure_exists: bool = True,
|
||||||
|
|
||||||
|
) -> tuple[str, int]:
|
||||||
|
|
||||||
|
global _tractor_kwargs
|
||||||
|
actor = tractor.current_actor()
|
||||||
|
uid = actor.uid
|
||||||
|
if (
|
||||||
|
Registry.addr is not None
|
||||||
|
and addr
|
||||||
|
):
|
||||||
|
raise RuntimeError(
|
||||||
|
f'`{uid}` registry addr already bound @ {_registry.sockaddr}'
|
||||||
|
)
|
||||||
|
|
||||||
|
was_set: bool = False
|
||||||
|
|
||||||
|
if (
|
||||||
|
not tractor.is_root_process()
|
||||||
|
and Registry.addr is None
|
||||||
|
):
|
||||||
|
Registry.addr = actor._arb_addr
|
||||||
|
|
||||||
|
if (
|
||||||
|
ensure_exists
|
||||||
|
and Registry.addr is None
|
||||||
|
):
|
||||||
|
raise RuntimeError(
|
||||||
|
f"`{uid}` registry should already exist bug doesn't?"
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
Registry.addr is None
|
||||||
|
):
|
||||||
|
was_set = True
|
||||||
|
Registry.addr = addr or _default_reg_addr
|
||||||
|
|
||||||
|
_tractor_kwargs['arbiter_addr'] = Registry.addr
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield Registry.addr
|
||||||
|
finally:
|
||||||
|
# XXX: always clear the global addr if we set it so that the
|
||||||
|
# next (set of) calls will apply whatever new one is passed
|
||||||
|
# in.
|
||||||
|
if was_set:
|
||||||
|
Registry.addr = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_tractor_runtime_kwargs() -> dict[str, Any]:
|
||||||
|
'''
|
||||||
|
Deliver ``tractor`` related runtime variables in a `dict`.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return _tractor_kwargs
|
||||||
|
|
||||||
|
|
||||||
_root_modules = [
|
_root_modules = [
|
||||||
__name__,
|
__name__,
|
||||||
'piker.clearing._ems',
|
'piker.clearing._ems',
|
||||||
'piker.clearing._client',
|
'piker.clearing._client',
|
||||||
|
'piker.data._sampling',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class Services(BaseModel):
|
# TODO: factor this into a ``tractor.highlevel`` extension
|
||||||
|
# pack for the library.
|
||||||
|
class Services:
|
||||||
|
|
||||||
actor_n: tractor._supervise.ActorNursery
|
actor_n: tractor._supervise.ActorNursery
|
||||||
service_n: trio.Nursery
|
service_n: trio.Nursery
|
||||||
debug_mode: bool # tractor sub-actor debug mode flag
|
debug_mode: bool # tractor sub-actor debug mode flag
|
||||||
service_tasks: dict[str, tuple[trio.CancelScope, tractor.Portal]] = {}
|
service_tasks: dict[
|
||||||
|
str,
|
||||||
class Config:
|
tuple[
|
||||||
arbitrary_types_allowed = True
|
trio.CancelScope,
|
||||||
|
tractor.Portal,
|
||||||
|
trio.Event,
|
||||||
|
]
|
||||||
|
] = {}
|
||||||
|
locks = defaultdict(trio.Lock)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
async def start_service_task(
|
async def start_service_task(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
|
@ -75,7 +176,12 @@ class Services(BaseModel):
|
||||||
'''
|
'''
|
||||||
async def open_context_in_task(
|
async def open_context_in_task(
|
||||||
task_status: TaskStatus[
|
task_status: TaskStatus[
|
||||||
trio.CancelScope] = trio.TASK_STATUS_IGNORED,
|
tuple[
|
||||||
|
trio.CancelScope,
|
||||||
|
trio.Event,
|
||||||
|
Any,
|
||||||
|
]
|
||||||
|
] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
) -> Any:
|
) -> Any:
|
||||||
|
|
||||||
|
@ -87,131 +193,97 @@ class Services(BaseModel):
|
||||||
) as (ctx, first):
|
) as (ctx, first):
|
||||||
|
|
||||||
# unblock once the remote context has started
|
# unblock once the remote context has started
|
||||||
task_status.started((cs, first))
|
complete = trio.Event()
|
||||||
|
task_status.started((cs, complete, first))
|
||||||
log.info(
|
log.info(
|
||||||
f'`pikerd` service {name} started with value {first}'
|
f'`pikerd` service {name} started with value {first}'
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
# wait on any context's return value
|
# wait on any context's return value
|
||||||
|
# and any final portal result from the
|
||||||
|
# sub-actor.
|
||||||
ctx_res = await ctx.result()
|
ctx_res = await ctx.result()
|
||||||
except tractor.ContextCancelled:
|
|
||||||
return await self.cancel_service(name)
|
# NOTE: blocks indefinitely until cancelled
|
||||||
else:
|
# either by error from the target context
|
||||||
# wait on any error from the sub-actor
|
# function or by being cancelled here by the
|
||||||
# NOTE: this will block indefinitely until
|
# surrounding cancel scope.
|
||||||
# cancelled either by error from the target
|
|
||||||
# context function or by being cancelled here by
|
|
||||||
# the surrounding cancel scope
|
|
||||||
return (await portal.result(), ctx_res)
|
return (await portal.result(), ctx_res)
|
||||||
|
|
||||||
cs, first = await self.service_n.start(open_context_in_task)
|
finally:
|
||||||
|
await portal.cancel_actor()
|
||||||
|
complete.set()
|
||||||
|
self.service_tasks.pop(name)
|
||||||
|
|
||||||
|
cs, complete, first = await self.service_n.start(open_context_in_task)
|
||||||
|
|
||||||
# store the cancel scope and portal for later cancellation or
|
# store the cancel scope and portal for later cancellation or
|
||||||
# retstart if needed.
|
# retstart if needed.
|
||||||
self.service_tasks[name] = (cs, portal)
|
self.service_tasks[name] = (cs, portal, complete)
|
||||||
|
|
||||||
return cs, first
|
return cs, first
|
||||||
|
|
||||||
# TODO: per service cancellation by scope, we aren't using this
|
@classmethod
|
||||||
# anywhere right?
|
|
||||||
async def cancel_service(
|
async def cancel_service(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
|
|
||||||
) -> Any:
|
) -> Any:
|
||||||
|
'''
|
||||||
|
Cancel the service task and actor for the given ``name``.
|
||||||
|
|
||||||
|
'''
|
||||||
log.info(f'Cancelling `pikerd` service {name}')
|
log.info(f'Cancelling `pikerd` service {name}')
|
||||||
cs, portal = self.service_tasks[name]
|
cs, portal, complete = self.service_tasks[name]
|
||||||
# XXX: not entirely sure why this is required,
|
|
||||||
# and should probably be better fine tuned in
|
|
||||||
# ``tractor``?
|
|
||||||
cs.cancel()
|
cs.cancel()
|
||||||
return await portal.cancel_actor()
|
await complete.wait()
|
||||||
|
assert name not in self.service_tasks, \
|
||||||
|
f'Serice task for {name} not terminated?'
|
||||||
_services: Optional[Services] = None
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
|
||||||
async def open_pikerd(
|
|
||||||
start_method: str = 'trio',
|
|
||||||
loglevel: Optional[str] = None,
|
|
||||||
|
|
||||||
# XXX: you should pretty much never want debug mode
|
|
||||||
# for data daemons when running in production.
|
|
||||||
debug_mode: bool = False,
|
|
||||||
|
|
||||||
) -> Optional[tractor._portal.Portal]:
|
|
||||||
'''
|
|
||||||
Start a root piker daemon who's lifetime extends indefinitely
|
|
||||||
until cancelled.
|
|
||||||
|
|
||||||
A root actor nursery is created which can be used to create and keep
|
|
||||||
alive underling services (see below).
|
|
||||||
|
|
||||||
'''
|
|
||||||
global _services
|
|
||||||
assert _services is None
|
|
||||||
|
|
||||||
# XXX: this may open a root actor as well
|
|
||||||
async with (
|
|
||||||
tractor.open_root_actor(
|
|
||||||
|
|
||||||
# passed through to ``open_root_actor``
|
|
||||||
arbiter_addr=_registry_addr,
|
|
||||||
name=_root_dname,
|
|
||||||
loglevel=loglevel,
|
|
||||||
debug_mode=debug_mode,
|
|
||||||
start_method=start_method,
|
|
||||||
|
|
||||||
# TODO: eventually we should be able to avoid
|
|
||||||
# having the root have more then permissions to
|
|
||||||
# spawn other specialized daemons I think?
|
|
||||||
enable_modules=_root_modules,
|
|
||||||
) as _,
|
|
||||||
|
|
||||||
tractor.open_nursery() as actor_nursery,
|
|
||||||
):
|
|
||||||
async with trio.open_nursery() as service_nursery:
|
|
||||||
|
|
||||||
# # setup service mngr singleton instance
|
|
||||||
# async with AsyncExitStack() as stack:
|
|
||||||
|
|
||||||
# assign globally for future daemon/task creation
|
|
||||||
_services = Services(
|
|
||||||
actor_n=actor_nursery,
|
|
||||||
service_n=service_nursery,
|
|
||||||
debug_mode=debug_mode,
|
|
||||||
)
|
|
||||||
|
|
||||||
yield _services
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def open_piker_runtime(
|
async def open_piker_runtime(
|
||||||
name: str,
|
name: str,
|
||||||
enable_modules: list[str] = [],
|
enable_modules: list[str] = [],
|
||||||
start_method: str = 'trio',
|
|
||||||
loglevel: Optional[str] = None,
|
loglevel: Optional[str] = None,
|
||||||
|
|
||||||
# XXX: you should pretty much never want debug mode
|
# XXX NOTE XXX: you should pretty much never want debug mode
|
||||||
# for data daemons when running in production.
|
# for data daemons when running in production.
|
||||||
debug_mode: bool = False,
|
debug_mode: bool = False,
|
||||||
|
|
||||||
) -> Optional[tractor._portal.Portal]:
|
registry_addr: None | tuple[str, int] = None,
|
||||||
|
|
||||||
|
# TODO: once we have `rsyscall` support we will read a config
|
||||||
|
# and spawn the service tree distributed per that.
|
||||||
|
start_method: str = 'trio',
|
||||||
|
|
||||||
|
tractor_kwargs: dict = {},
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
tractor.Actor,
|
||||||
|
tuple[str, int],
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Start a piker actor who's runtime will automatically
|
Start a piker actor who's runtime will automatically sync with
|
||||||
sync with existing piker actors in local network
|
existing piker actors on the local link based on configuration.
|
||||||
based on configuration.
|
|
||||||
|
Can be called from a subactor or any program that needs to start
|
||||||
|
a root actor.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
global _services
|
try:
|
||||||
assert _services is None
|
# check for existing runtime
|
||||||
|
actor = tractor.current_actor().uid
|
||||||
|
|
||||||
|
except tractor._exceptions.NoRuntime:
|
||||||
|
|
||||||
|
registry_addr = registry_addr or _default_reg_addr
|
||||||
|
|
||||||
# XXX: this may open a root actor as well
|
|
||||||
async with (
|
async with (
|
||||||
tractor.open_root_actor(
|
tractor.open_root_actor(
|
||||||
|
|
||||||
# passed through to ``open_root_actor``
|
# passed through to ``open_root_actor``
|
||||||
arbiter_addr=_registry_addr,
|
arbiter_addr=registry_addr,
|
||||||
name=name,
|
name=name,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
debug_mode=debug_mode,
|
debug_mode=debug_mode,
|
||||||
|
@ -220,10 +292,74 @@ async def open_piker_runtime(
|
||||||
# TODO: eventually we should be able to avoid
|
# TODO: eventually we should be able to avoid
|
||||||
# having the root have more then permissions to
|
# having the root have more then permissions to
|
||||||
# spawn other specialized daemons I think?
|
# spawn other specialized daemons I think?
|
||||||
enable_modules=_root_modules,
|
enable_modules=enable_modules,
|
||||||
|
|
||||||
|
**tractor_kwargs,
|
||||||
) as _,
|
) as _,
|
||||||
|
|
||||||
|
open_registry(registry_addr, ensure_exists=False) as addr,
|
||||||
):
|
):
|
||||||
yield tractor.current_actor()
|
yield (
|
||||||
|
tractor.current_actor(),
|
||||||
|
addr,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
async with open_registry(registry_addr) as addr:
|
||||||
|
yield (
|
||||||
|
actor,
|
||||||
|
addr,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_pikerd(
|
||||||
|
loglevel: str | None = None,
|
||||||
|
|
||||||
|
# XXX: you should pretty much never want debug mode
|
||||||
|
# for data daemons when running in production.
|
||||||
|
debug_mode: bool = False,
|
||||||
|
registry_addr: None | tuple[str, int] = None,
|
||||||
|
|
||||||
|
) -> Services:
|
||||||
|
'''
|
||||||
|
Start a root piker daemon who's lifetime extends indefinitely until
|
||||||
|
cancelled.
|
||||||
|
|
||||||
|
A root actor nursery is created which can be used to create and keep
|
||||||
|
alive underling services (see below).
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
async with (
|
||||||
|
open_piker_runtime(
|
||||||
|
|
||||||
|
name=_root_dname,
|
||||||
|
# TODO: eventually we should be able to avoid
|
||||||
|
# having the root have more then permissions to
|
||||||
|
# spawn other specialized daemons I think?
|
||||||
|
enable_modules=_root_modules,
|
||||||
|
|
||||||
|
loglevel=loglevel,
|
||||||
|
debug_mode=debug_mode,
|
||||||
|
registry_addr=registry_addr,
|
||||||
|
|
||||||
|
) as (root_actor, reg_addr),
|
||||||
|
tractor.open_nursery() as actor_nursery,
|
||||||
|
trio.open_nursery() as service_nursery,
|
||||||
|
):
|
||||||
|
assert root_actor.accept_addr == reg_addr
|
||||||
|
|
||||||
|
# assign globally for future daemon/task creation
|
||||||
|
Services.actor_n = actor_nursery
|
||||||
|
Services.service_n = service_nursery
|
||||||
|
Services.debug_mode = debug_mode
|
||||||
|
try:
|
||||||
|
yield Services
|
||||||
|
finally:
|
||||||
|
# TODO: is this more clever/efficient?
|
||||||
|
# if 'samplerd' in Services.service_tasks:
|
||||||
|
# await Services.cancel_service('samplerd')
|
||||||
|
service_nursery.cancel_scope.cancel()
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
|
@ -232,61 +368,89 @@ async def maybe_open_runtime(
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
'''
|
||||||
Start the ``tractor`` runtime (a root actor) if none exists.
|
Start the ``tractor`` runtime (a root actor) if none exists.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
settings = _tractor_kwargs
|
name = kwargs.pop('name')
|
||||||
settings.update(kwargs)
|
|
||||||
|
|
||||||
if not tractor.current_actor(err_on_no_runtime=False):
|
if not tractor.current_actor(err_on_no_runtime=False):
|
||||||
async with tractor.open_root_actor(
|
async with open_piker_runtime(
|
||||||
|
name,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
**settings,
|
**kwargs,
|
||||||
):
|
) as (_, addr):
|
||||||
yield
|
yield addr,
|
||||||
else:
|
else:
|
||||||
yield
|
async with open_registry() as addr:
|
||||||
|
yield addr
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def maybe_open_pikerd(
|
async def maybe_open_pikerd(
|
||||||
loglevel: Optional[str] = None,
|
loglevel: Optional[str] = None,
|
||||||
|
registry_addr: None | tuple = None,
|
||||||
|
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> Union[tractor._portal.Portal, Services]:
|
) -> tractor._portal.Portal | ClassVar[Services]:
|
||||||
"""If no ``pikerd`` daemon-root-actor can be found start it and
|
'''
|
||||||
|
If no ``pikerd`` daemon-root-actor can be found start it and
|
||||||
yield up (we should probably figure out returning a portal to self
|
yield up (we should probably figure out returning a portal to self
|
||||||
though).
|
though).
|
||||||
|
|
||||||
"""
|
'''
|
||||||
if loglevel:
|
if loglevel:
|
||||||
get_console_log(loglevel)
|
get_console_log(loglevel)
|
||||||
|
|
||||||
# subtle, we must have the runtime up here or portal lookup will fail
|
# subtle, we must have the runtime up here or portal lookup will fail
|
||||||
async with maybe_open_runtime(loglevel, **kwargs):
|
query_name = kwargs.pop('name', f'piker_query_{os.getpid()}')
|
||||||
|
|
||||||
async with tractor.find_actor(_root_dname) as portal:
|
# TODO: if we need to make the query part faster we could not init
|
||||||
# assert portal is not None
|
# an actor runtime and instead just hit the socket?
|
||||||
if portal is not None:
|
# from tractor._ipc import _connect_chan, Channel
|
||||||
|
# async with _connect_chan(host, port) as chan:
|
||||||
|
# async with open_portal(chan) as arb_portal:
|
||||||
|
# yield arb_portal
|
||||||
|
|
||||||
|
async with (
|
||||||
|
open_piker_runtime(
|
||||||
|
name=query_name,
|
||||||
|
registry_addr=registry_addr,
|
||||||
|
loglevel=loglevel,
|
||||||
|
**kwargs,
|
||||||
|
) as _,
|
||||||
|
tractor.find_actor(
|
||||||
|
_root_dname,
|
||||||
|
arbiter_sockaddr=registry_addr,
|
||||||
|
) as portal
|
||||||
|
):
|
||||||
|
# connect to any existing daemon presuming
|
||||||
|
# its registry socket was selected.
|
||||||
|
if (
|
||||||
|
portal is not None
|
||||||
|
):
|
||||||
yield portal
|
yield portal
|
||||||
return
|
return
|
||||||
|
|
||||||
# presume pikerd role since no daemon could be found at
|
# presume pikerd role since no daemon could be found at
|
||||||
# configured address
|
# configured address
|
||||||
async with open_pikerd(
|
async with open_pikerd(
|
||||||
|
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
debug_mode=kwargs.get('debug_mode', False),
|
debug_mode=kwargs.get('debug_mode', False),
|
||||||
|
registry_addr=registry_addr,
|
||||||
|
|
||||||
) as _:
|
) as service_manager:
|
||||||
# in the case where we're starting up the
|
# in the case where we're starting up the
|
||||||
# tractor-piker runtime stack in **this** process
|
# tractor-piker runtime stack in **this** process
|
||||||
# we return no portal to self.
|
# we return no portal to self.
|
||||||
yield None
|
assert service_manager
|
||||||
|
yield service_manager
|
||||||
|
|
||||||
|
|
||||||
# brokerd enabled modules
|
# `brokerd` enabled modules
|
||||||
|
# NOTE: keeping this list as small as possible is part of our caps-sec
|
||||||
|
# model and should be treated with utmost care!
|
||||||
_data_mods = [
|
_data_mods = [
|
||||||
'piker.brokers.core',
|
'piker.brokers.core',
|
||||||
'piker.brokers.data',
|
'piker.brokers.data',
|
||||||
|
@ -296,20 +460,17 @@ _data_mods = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class Brokerd:
|
|
||||||
locks = defaultdict(trio.Lock)
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def find_service(
|
async def find_service(
|
||||||
service_name: str,
|
service_name: str,
|
||||||
) -> Optional[tractor.Portal]:
|
) -> tractor.Portal | None:
|
||||||
|
|
||||||
|
async with open_registry() as reg_addr:
|
||||||
log.info(f'Scanning for service `{service_name}`')
|
log.info(f'Scanning for service `{service_name}`')
|
||||||
# attach to existing daemon by name if possible
|
# attach to existing daemon by name if possible
|
||||||
async with tractor.find_actor(
|
async with tractor.find_actor(
|
||||||
service_name,
|
service_name,
|
||||||
arbiter_sockaddr=_registry_addr,
|
arbiter_sockaddr=reg_addr,
|
||||||
) as maybe_portal:
|
) as maybe_portal:
|
||||||
yield maybe_portal
|
yield maybe_portal
|
||||||
|
|
||||||
|
@ -317,14 +478,15 @@ async def find_service(
|
||||||
async def check_for_service(
|
async def check_for_service(
|
||||||
service_name: str,
|
service_name: str,
|
||||||
|
|
||||||
) -> bool:
|
) -> None | tuple[str, int]:
|
||||||
'''
|
'''
|
||||||
Service daemon "liveness" predicate.
|
Service daemon "liveness" predicate.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
async with open_registry(ensure_exists=False) as reg_addr:
|
||||||
async with tractor.query_actor(
|
async with tractor.query_actor(
|
||||||
service_name,
|
service_name,
|
||||||
arbiter_sockaddr=_registry_addr,
|
arbiter_sockaddr=reg_addr,
|
||||||
) as sockaddr:
|
) as sockaddr:
|
||||||
return sockaddr
|
return sockaddr
|
||||||
|
|
||||||
|
@ -336,6 +498,8 @@ async def maybe_spawn_daemon(
|
||||||
service_task_target: Callable,
|
service_task_target: Callable,
|
||||||
spawn_args: dict[str, Any],
|
spawn_args: dict[str, Any],
|
||||||
loglevel: Optional[str] = None,
|
loglevel: Optional[str] = None,
|
||||||
|
|
||||||
|
singleton: bool = False,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> tractor.Portal:
|
) -> tractor.Portal:
|
||||||
|
@ -356,7 +520,7 @@ async def maybe_spawn_daemon(
|
||||||
|
|
||||||
# serialize access to this section to avoid
|
# serialize access to this section to avoid
|
||||||
# 2 or more tasks racing to create a daemon
|
# 2 or more tasks racing to create a daemon
|
||||||
lock = Brokerd.locks[service_name]
|
lock = Services.locks[service_name]
|
||||||
await lock.acquire()
|
await lock.acquire()
|
||||||
|
|
||||||
async with find_service(service_name) as portal:
|
async with find_service(service_name) as portal:
|
||||||
|
@ -367,6 +531,9 @@ async def maybe_spawn_daemon(
|
||||||
|
|
||||||
log.warning(f"Couldn't find any existing {service_name}")
|
log.warning(f"Couldn't find any existing {service_name}")
|
||||||
|
|
||||||
|
# TODO: really shouldn't the actor spawning be part of the service
|
||||||
|
# starting method `Services.start_service()` ?
|
||||||
|
|
||||||
# ask root ``pikerd`` daemon to spawn the daemon we need if
|
# ask root ``pikerd`` daemon to spawn the daemon we need if
|
||||||
# pikerd is not live we now become the root of the
|
# pikerd is not live we now become the root of the
|
||||||
# process tree
|
# process tree
|
||||||
|
@ -377,7 +544,6 @@ async def maybe_spawn_daemon(
|
||||||
|
|
||||||
) as pikerd_portal:
|
) as pikerd_portal:
|
||||||
|
|
||||||
if pikerd_portal is None:
|
|
||||||
# we are the root and thus are `pikerd`
|
# we are the root and thus are `pikerd`
|
||||||
# so spawn the target service directly by calling
|
# so spawn the target service directly by calling
|
||||||
# the provided target routine.
|
# the provided target routine.
|
||||||
|
@ -385,7 +551,9 @@ async def maybe_spawn_daemon(
|
||||||
# do the right things to setup both a sub-actor **and** call
|
# do the right things to setup both a sub-actor **and** call
|
||||||
# the ``_Services`` api from above to start the top level
|
# the ``_Services`` api from above to start the top level
|
||||||
# service task for that actor.
|
# service task for that actor.
|
||||||
await service_task_target(**spawn_args)
|
started: bool
|
||||||
|
if pikerd_portal is None:
|
||||||
|
started = await service_task_target(**spawn_args)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# tell the remote `pikerd` to start the target,
|
# tell the remote `pikerd` to start the target,
|
||||||
|
@ -394,11 +562,14 @@ async def maybe_spawn_daemon(
|
||||||
# non-blocking and the target task will persist running
|
# non-blocking and the target task will persist running
|
||||||
# on `pikerd` after the client requesting it's start
|
# on `pikerd` after the client requesting it's start
|
||||||
# disconnects.
|
# disconnects.
|
||||||
await pikerd_portal.run(
|
started = await pikerd_portal.run(
|
||||||
service_task_target,
|
service_task_target,
|
||||||
**spawn_args,
|
**spawn_args,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if started:
|
||||||
|
log.info(f'Service {service_name} started!')
|
||||||
|
|
||||||
async with tractor.wait_for_actor(service_name) as portal:
|
async with tractor.wait_for_actor(service_name) as portal:
|
||||||
lock.release()
|
lock.release()
|
||||||
yield portal
|
yield portal
|
||||||
|
@ -421,9 +592,6 @@ async def spawn_brokerd(
|
||||||
extra_tractor_kwargs = getattr(brokermod, '_spawn_kwargs', {})
|
extra_tractor_kwargs = getattr(brokermod, '_spawn_kwargs', {})
|
||||||
tractor_kwargs.update(extra_tractor_kwargs)
|
tractor_kwargs.update(extra_tractor_kwargs)
|
||||||
|
|
||||||
global _services
|
|
||||||
assert _services
|
|
||||||
|
|
||||||
# ask `pikerd` to spawn a new sub-actor and manage it under its
|
# ask `pikerd` to spawn a new sub-actor and manage it under its
|
||||||
# actor nursery
|
# actor nursery
|
||||||
modpath = brokermod.__name__
|
modpath = brokermod.__name__
|
||||||
|
@ -436,18 +604,18 @@ async def spawn_brokerd(
|
||||||
subpath = f'{modpath}.{submodname}'
|
subpath = f'{modpath}.{submodname}'
|
||||||
broker_enable.append(subpath)
|
broker_enable.append(subpath)
|
||||||
|
|
||||||
portal = await _services.actor_n.start_actor(
|
portal = await Services.actor_n.start_actor(
|
||||||
dname,
|
dname,
|
||||||
enable_modules=_data_mods + broker_enable,
|
enable_modules=_data_mods + broker_enable,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
debug_mode=_services.debug_mode,
|
debug_mode=Services.debug_mode,
|
||||||
**tractor_kwargs
|
**tractor_kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
# non-blocking setup of brokerd service nursery
|
# non-blocking setup of brokerd service nursery
|
||||||
from .data import _setup_persistent_brokerd
|
from .data import _setup_persistent_brokerd
|
||||||
|
|
||||||
await _services.start_service_task(
|
await Services.start_service_task(
|
||||||
dname,
|
dname,
|
||||||
portal,
|
portal,
|
||||||
_setup_persistent_brokerd,
|
_setup_persistent_brokerd,
|
||||||
|
@ -493,24 +661,21 @@ async def spawn_emsd(
|
||||||
"""
|
"""
|
||||||
log.info('Spawning emsd')
|
log.info('Spawning emsd')
|
||||||
|
|
||||||
global _services
|
portal = await Services.actor_n.start_actor(
|
||||||
assert _services
|
|
||||||
|
|
||||||
portal = await _services.actor_n.start_actor(
|
|
||||||
'emsd',
|
'emsd',
|
||||||
enable_modules=[
|
enable_modules=[
|
||||||
'piker.clearing._ems',
|
'piker.clearing._ems',
|
||||||
'piker.clearing._client',
|
'piker.clearing._client',
|
||||||
],
|
],
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
debug_mode=_services.debug_mode, # set by pikerd flag
|
debug_mode=Services.debug_mode, # set by pikerd flag
|
||||||
**extra_tractor_kwargs
|
**extra_tractor_kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
# non-blocking setup of clearing service
|
# non-blocking setup of clearing service
|
||||||
from .clearing._ems import _setup_persistent_emsd
|
from .clearing._ems import _setup_persistent_emsd
|
||||||
|
|
||||||
await _services.start_service_task(
|
await Services.start_service_task(
|
||||||
'emsd',
|
'emsd',
|
||||||
portal,
|
portal,
|
||||||
_setup_persistent_emsd,
|
_setup_persistent_emsd,
|
||||||
|
@ -537,25 +702,3 @@ async def maybe_open_emsd(
|
||||||
|
|
||||||
) as portal:
|
) as portal:
|
||||||
yield portal
|
yield portal
|
||||||
|
|
||||||
|
|
||||||
# TODO: ideally we can start the tsdb "on demand" but it's
|
|
||||||
# probably going to require "rootless" docker, at least if we don't
|
|
||||||
# want to expect the user to start ``pikerd`` with root perms all the
|
|
||||||
# time.
|
|
||||||
# async def maybe_open_marketstored(
|
|
||||||
# loglevel: Optional[str] = None,
|
|
||||||
# **kwargs,
|
|
||||||
|
|
||||||
# ) -> tractor._portal.Portal: # noqa
|
|
||||||
|
|
||||||
# async with maybe_spawn_daemon(
|
|
||||||
|
|
||||||
# 'marketstored',
|
|
||||||
# service_task_target=spawn_emsd,
|
|
||||||
# spawn_args={'loglevel': loglevel},
|
|
||||||
# loglevel=loglevel,
|
|
||||||
# **kwargs,
|
|
||||||
|
|
||||||
# ) as portal:
|
|
||||||
# yield portal
|
|
||||||
|
|
|
@ -18,7 +18,10 @@
|
||||||
Profiling wrappers for internal libs.
|
Profiling wrappers for internal libs.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
from time import perf_counter
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
# NOTE: you can pass a flag to enable this:
|
# NOTE: you can pass a flag to enable this:
|
||||||
|
@ -44,3 +47,184 @@ def timeit(fn):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
# Modified version of ``pyqtgraph.debug.Profiler`` that
|
||||||
|
# core seems hesitant to land in:
|
||||||
|
# https://github.com/pyqtgraph/pyqtgraph/pull/2281
|
||||||
|
class Profiler(object):
|
||||||
|
'''
|
||||||
|
Simple profiler allowing measurement of multiple time intervals.
|
||||||
|
|
||||||
|
By default, profilers are disabled. To enable profiling, set the
|
||||||
|
environment variable `PYQTGRAPHPROFILE` to a comma-separated list of
|
||||||
|
fully-qualified names of profiled functions.
|
||||||
|
|
||||||
|
Calling a profiler registers a message (defaulting to an increasing
|
||||||
|
counter) that contains the time elapsed since the last call. When the
|
||||||
|
profiler is about to be garbage-collected, the messages are passed to the
|
||||||
|
outer profiler if one is running, or printed to stdout otherwise.
|
||||||
|
|
||||||
|
If `delayed` is set to False, messages are immediately printed instead.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
def function(...):
|
||||||
|
profiler = Profiler()
|
||||||
|
... do stuff ...
|
||||||
|
profiler('did stuff')
|
||||||
|
... do other stuff ...
|
||||||
|
profiler('did other stuff')
|
||||||
|
# profiler is garbage-collected and flushed at function end
|
||||||
|
|
||||||
|
If this function is a method of class C, setting `PYQTGRAPHPROFILE` to
|
||||||
|
"C.function" (without the module name) will enable this profiler.
|
||||||
|
|
||||||
|
For regular functions, use the qualified name of the function, stripping
|
||||||
|
only the initial "pyqtgraph." prefix from the module.
|
||||||
|
'''
|
||||||
|
|
||||||
|
_profilers = os.environ.get("PYQTGRAPHPROFILE", None)
|
||||||
|
_profilers = _profilers.split(",") if _profilers is not None else []
|
||||||
|
|
||||||
|
_depth = 0
|
||||||
|
|
||||||
|
# NOTE: without this defined at the class level
|
||||||
|
# you won't see apprpriately "nested" sub-profiler
|
||||||
|
# instance calls.
|
||||||
|
_msgs = []
|
||||||
|
|
||||||
|
# set this flag to disable all or individual profilers at runtime
|
||||||
|
disable = False
|
||||||
|
|
||||||
|
class DisabledProfiler(object):
|
||||||
|
def __init__(self, *args, **kwds):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __call__(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def finish(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def mark(self, msg=None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
_disabledProfiler = DisabledProfiler()
|
||||||
|
|
||||||
|
def __new__(
|
||||||
|
cls,
|
||||||
|
msg=None,
|
||||||
|
disabled='env',
|
||||||
|
delayed=True,
|
||||||
|
ms_threshold: float = 0.0,
|
||||||
|
):
|
||||||
|
"""Optionally create a new profiler based on caller's qualname.
|
||||||
|
|
||||||
|
``ms_threshold`` can be set to value in ms for which, if the
|
||||||
|
total measured time of the lifetime of this profiler is **less
|
||||||
|
than** this value, then no profiling messages will be printed.
|
||||||
|
Setting ``delayed=False`` disables this feature since messages
|
||||||
|
are emitted immediately.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if (
|
||||||
|
disabled is True
|
||||||
|
or (
|
||||||
|
disabled == 'env'
|
||||||
|
and len(cls._profilers) == 0
|
||||||
|
)
|
||||||
|
):
|
||||||
|
return cls._disabledProfiler
|
||||||
|
|
||||||
|
# determine the qualified name of the caller function
|
||||||
|
caller_frame = sys._getframe(1)
|
||||||
|
try:
|
||||||
|
caller_object_type = type(caller_frame.f_locals["self"])
|
||||||
|
|
||||||
|
except KeyError: # we are in a regular function
|
||||||
|
qualifier = caller_frame.f_globals["__name__"].split(".", 1)[-1]
|
||||||
|
|
||||||
|
else: # we are in a method
|
||||||
|
qualifier = caller_object_type.__name__
|
||||||
|
func_qualname = qualifier + "." + caller_frame.f_code.co_name
|
||||||
|
|
||||||
|
if disabled == 'env' and func_qualname not in cls._profilers:
|
||||||
|
# don't do anything
|
||||||
|
return cls._disabledProfiler
|
||||||
|
|
||||||
|
# create an actual profiling object
|
||||||
|
cls._depth += 1
|
||||||
|
obj = super(Profiler, cls).__new__(cls)
|
||||||
|
obj._name = msg or func_qualname
|
||||||
|
obj._delayed = delayed
|
||||||
|
obj._markCount = 0
|
||||||
|
obj._finished = False
|
||||||
|
obj._firstTime = obj._lastTime = perf_counter()
|
||||||
|
obj._mt = ms_threshold
|
||||||
|
obj._newMsg("> Entering " + obj._name)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def __call__(self, msg=None):
|
||||||
|
"""Register or print a new message with timing information.
|
||||||
|
"""
|
||||||
|
if self.disable:
|
||||||
|
return
|
||||||
|
if msg is None:
|
||||||
|
msg = str(self._markCount)
|
||||||
|
|
||||||
|
self._markCount += 1
|
||||||
|
newTime = perf_counter()
|
||||||
|
ms = (newTime - self._lastTime) * 1000
|
||||||
|
self._newMsg(" %s: %0.4f ms", msg, ms)
|
||||||
|
self._lastTime = newTime
|
||||||
|
|
||||||
|
def mark(self, msg=None):
|
||||||
|
self(msg)
|
||||||
|
|
||||||
|
def _newMsg(self, msg, *args):
|
||||||
|
msg = " " * (self._depth - 1) + msg
|
||||||
|
if self._delayed:
|
||||||
|
self._msgs.append((msg, args))
|
||||||
|
else:
|
||||||
|
print(msg % args)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.finish()
|
||||||
|
|
||||||
|
def finish(self, msg=None):
|
||||||
|
"""Add a final message; flush the message list if no parent profiler.
|
||||||
|
"""
|
||||||
|
if self._finished or self.disable:
|
||||||
|
return
|
||||||
|
|
||||||
|
self._finished = True
|
||||||
|
if msg is not None:
|
||||||
|
self(msg)
|
||||||
|
|
||||||
|
tot_ms = (perf_counter() - self._firstTime) * 1000
|
||||||
|
self._newMsg(
|
||||||
|
"< Exiting %s, total time: %0.4f ms",
|
||||||
|
self._name,
|
||||||
|
tot_ms,
|
||||||
|
)
|
||||||
|
|
||||||
|
if tot_ms < self._mt:
|
||||||
|
# print(f'{tot_ms} < {self._mt}, clearing')
|
||||||
|
# NOTE: this list **must** be an instance var to avoid
|
||||||
|
# deleting common messages during GC I think?
|
||||||
|
self._msgs.clear()
|
||||||
|
# else:
|
||||||
|
# print(f'{tot_ms} > {self._mt}, not clearing')
|
||||||
|
|
||||||
|
# XXX: why is this needed?
|
||||||
|
# don't we **want to show** nested profiler messages?
|
||||||
|
if self._msgs: # and self._depth < 1:
|
||||||
|
|
||||||
|
# if self._msgs:
|
||||||
|
print("\n".join([m[0] % m[1] for m in self._msgs]))
|
||||||
|
|
||||||
|
# clear all entries
|
||||||
|
self._msgs.clear()
|
||||||
|
# type(self)._msgs = []
|
||||||
|
|
||||||
|
type(self)._depth -= 1
|
||||||
|
|
|
@ -26,10 +26,21 @@ asks.init('trio')
|
||||||
|
|
||||||
__brokers__ = [
|
__brokers__ = [
|
||||||
'binance',
|
'binance',
|
||||||
'questrade',
|
|
||||||
'robinhood',
|
|
||||||
'ib',
|
'ib',
|
||||||
'kraken',
|
'kraken',
|
||||||
|
|
||||||
|
# broken but used to work
|
||||||
|
# 'questrade',
|
||||||
|
# 'robinhood',
|
||||||
|
|
||||||
|
# TODO: we should get on these stat!
|
||||||
|
# alpaca
|
||||||
|
# wstrade
|
||||||
|
# iex
|
||||||
|
|
||||||
|
# deribit
|
||||||
|
# kucoin
|
||||||
|
# bitso
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -33,15 +33,23 @@ import asks
|
||||||
from fuzzywuzzy import process as fuzzy
|
from fuzzywuzzy import process as fuzzy
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tractor
|
import tractor
|
||||||
from pydantic.dataclasses import dataclass
|
|
||||||
from pydantic import BaseModel
|
|
||||||
import wsproto
|
import wsproto
|
||||||
|
|
||||||
from .._cacheables import open_cached_client
|
from .._cacheables import open_cached_client
|
||||||
from ._util import resproc, SymbolNotFound
|
from ._util import (
|
||||||
from ..log import get_logger, get_console_log
|
resproc,
|
||||||
from ..data import ShmArray
|
SymbolNotFound,
|
||||||
from ..data._web_bs import open_autorecon_ws, NoBsWs
|
DataUnavailable,
|
||||||
|
)
|
||||||
|
from ..log import (
|
||||||
|
get_logger,
|
||||||
|
get_console_log,
|
||||||
|
)
|
||||||
|
from ..data.types import Struct
|
||||||
|
from ..data._web_bs import (
|
||||||
|
open_autorecon_ws,
|
||||||
|
NoBsWs,
|
||||||
|
)
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
@ -79,12 +87,14 @@ _show_wap_in_history = False
|
||||||
|
|
||||||
|
|
||||||
# https://binance-docs.github.io/apidocs/spot/en/#exchange-information
|
# https://binance-docs.github.io/apidocs/spot/en/#exchange-information
|
||||||
class Pair(BaseModel):
|
class Pair(Struct, frozen=True):
|
||||||
symbol: str
|
symbol: str
|
||||||
status: str
|
status: str
|
||||||
|
|
||||||
baseAsset: str
|
baseAsset: str
|
||||||
baseAssetPrecision: int
|
baseAssetPrecision: int
|
||||||
|
cancelReplaceAllowed: bool
|
||||||
|
allowTrailingStop: bool
|
||||||
quoteAsset: str
|
quoteAsset: str
|
||||||
quotePrecision: int
|
quotePrecision: int
|
||||||
quoteAssetPrecision: int
|
quoteAssetPrecision: int
|
||||||
|
@ -100,18 +110,21 @@ class Pair(BaseModel):
|
||||||
isSpotTradingAllowed: bool
|
isSpotTradingAllowed: bool
|
||||||
isMarginTradingAllowed: bool
|
isMarginTradingAllowed: bool
|
||||||
|
|
||||||
|
defaultSelfTradePreventionMode: str
|
||||||
|
allowedSelfTradePreventionModes: list[str]
|
||||||
|
|
||||||
filters: list[dict[str, Union[str, int, float]]]
|
filters: list[dict[str, Union[str, int, float]]]
|
||||||
permissions: list[str]
|
permissions: list[str]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class OHLC(Struct):
|
||||||
class OHLC:
|
'''
|
||||||
"""Description of the flattened OHLC quote format.
|
Description of the flattened OHLC quote format.
|
||||||
|
|
||||||
For schema details see:
|
For schema details see:
|
||||||
https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-streams
|
https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-streams
|
||||||
|
|
||||||
"""
|
'''
|
||||||
time: int
|
time: int
|
||||||
|
|
||||||
open: float
|
open: float
|
||||||
|
@ -134,7 +147,9 @@ class OHLC:
|
||||||
|
|
||||||
|
|
||||||
# convert datetime obj timestamp to unixtime in milliseconds
|
# convert datetime obj timestamp to unixtime in milliseconds
|
||||||
def binance_timestamp(when):
|
def binance_timestamp(
|
||||||
|
when: datetime
|
||||||
|
) -> int:
|
||||||
return int((when.timestamp() * 1000) + (when.microsecond / 1000))
|
return int((when.timestamp() * 1000) + (when.microsecond / 1000))
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,7 +188,7 @@ class Client:
|
||||||
params = {}
|
params = {}
|
||||||
|
|
||||||
if sym is not None:
|
if sym is not None:
|
||||||
sym = sym.upper()
|
sym = sym.lower()
|
||||||
params = {'symbol': sym}
|
params = {'symbol': sym}
|
||||||
|
|
||||||
resp = await self._api(
|
resp = await self._api(
|
||||||
|
@ -230,7 +245,7 @@ class Client:
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
|
||||||
if end_dt is None:
|
if end_dt is None:
|
||||||
end_dt = pendulum.now('UTC')
|
end_dt = pendulum.now('UTC').add(minutes=1)
|
||||||
|
|
||||||
if start_dt is None:
|
if start_dt is None:
|
||||||
start_dt = end_dt.start_of(
|
start_dt = end_dt.start_of(
|
||||||
|
@ -260,6 +275,7 @@ class Client:
|
||||||
for i, bar in enumerate(bars):
|
for i, bar in enumerate(bars):
|
||||||
|
|
||||||
bar = OHLC(*bar)
|
bar = OHLC(*bar)
|
||||||
|
bar.typecast()
|
||||||
|
|
||||||
row = []
|
row = []
|
||||||
for j, (name, ftype) in enumerate(_ohlc_dtype[1:]):
|
for j, (name, ftype) in enumerate(_ohlc_dtype[1:]):
|
||||||
|
@ -287,7 +303,7 @@ async def get_client() -> Client:
|
||||||
|
|
||||||
|
|
||||||
# validation type
|
# validation type
|
||||||
class AggTrade(BaseModel):
|
class AggTrade(Struct):
|
||||||
e: str # Event type
|
e: str # Event type
|
||||||
E: int # Event time
|
E: int # Event time
|
||||||
s: str # Symbol
|
s: str # Symbol
|
||||||
|
@ -341,7 +357,9 @@ async def stream_messages(ws: NoBsWs) -> AsyncGenerator[NoBsWs, dict]:
|
||||||
|
|
||||||
elif msg.get('e') == 'aggTrade':
|
elif msg.get('e') == 'aggTrade':
|
||||||
|
|
||||||
# validate
|
# NOTE: this is purely for a definition, ``msgspec.Struct``
|
||||||
|
# does not runtime-validate until you decode/encode.
|
||||||
|
# see: https://jcristharif.com/msgspec/structs.html#type-validation
|
||||||
msg = AggTrade(**msg)
|
msg = AggTrade(**msg)
|
||||||
|
|
||||||
# TODO: type out and require this quote format
|
# TODO: type out and require this quote format
|
||||||
|
@ -352,8 +370,8 @@ async def stream_messages(ws: NoBsWs) -> AsyncGenerator[NoBsWs, dict]:
|
||||||
'brokerd_ts': time.time(),
|
'brokerd_ts': time.time(),
|
||||||
'ticks': [{
|
'ticks': [{
|
||||||
'type': 'trade',
|
'type': 'trade',
|
||||||
'price': msg.p,
|
'price': float(msg.p),
|
||||||
'size': msg.q,
|
'size': float(msg.q),
|
||||||
'broker_ts': msg.T,
|
'broker_ts': msg.T,
|
||||||
}],
|
}],
|
||||||
}
|
}
|
||||||
|
@ -384,41 +402,39 @@ async def open_history_client(
|
||||||
async with open_cached_client('binance') as client:
|
async with open_cached_client('binance') as client:
|
||||||
|
|
||||||
async def get_ohlc(
|
async def get_ohlc(
|
||||||
end_dt: Optional[datetime] = None,
|
timeframe: float,
|
||||||
start_dt: Optional[datetime] = None,
|
end_dt: datetime | None = None,
|
||||||
|
start_dt: datetime | None = None,
|
||||||
|
|
||||||
) -> tuple[
|
) -> tuple[
|
||||||
np.ndarray,
|
np.ndarray,
|
||||||
datetime, # start
|
datetime, # start
|
||||||
datetime, # end
|
datetime, # end
|
||||||
]:
|
]:
|
||||||
|
if timeframe != 60:
|
||||||
|
raise DataUnavailable('Only 1m bars are supported')
|
||||||
|
|
||||||
array = await client.bars(
|
array = await client.bars(
|
||||||
symbol,
|
symbol,
|
||||||
start_dt=start_dt,
|
start_dt=start_dt,
|
||||||
end_dt=end_dt,
|
end_dt=end_dt,
|
||||||
)
|
)
|
||||||
start_dt = pendulum.from_timestamp(array[0]['time'])
|
times = array['time']
|
||||||
end_dt = pendulum.from_timestamp(array[-1]['time'])
|
if (
|
||||||
|
end_dt is None
|
||||||
|
):
|
||||||
|
inow = round(time.time())
|
||||||
|
if (inow - times[-1]) > 60:
|
||||||
|
await tractor.breakpoint()
|
||||||
|
|
||||||
|
start_dt = pendulum.from_timestamp(times[0])
|
||||||
|
end_dt = pendulum.from_timestamp(times[-1])
|
||||||
|
|
||||||
return array, start_dt, end_dt
|
return array, start_dt, end_dt
|
||||||
|
|
||||||
yield get_ohlc, {'erlangs': 3, 'rate': 3}
|
yield get_ohlc, {'erlangs': 3, 'rate': 3}
|
||||||
|
|
||||||
|
|
||||||
async def backfill_bars(
|
|
||||||
sym: str,
|
|
||||||
shm: ShmArray, # type: ignore # noqa
|
|
||||||
task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
|
|
||||||
) -> None:
|
|
||||||
"""Fill historical bars into shared mem / storage afap.
|
|
||||||
"""
|
|
||||||
with trio.CancelScope() as cs:
|
|
||||||
async with open_cached_client('binance') as client:
|
|
||||||
bars = await client.bars(symbol=sym)
|
|
||||||
shm.push(bars)
|
|
||||||
task_status.started(cs)
|
|
||||||
|
|
||||||
|
|
||||||
async def stream_quotes(
|
async def stream_quotes(
|
||||||
|
|
||||||
send_chan: trio.abc.SendChannel,
|
send_chan: trio.abc.SendChannel,
|
||||||
|
@ -448,12 +464,20 @@ async def stream_quotes(
|
||||||
d = cache[sym.upper()]
|
d = cache[sym.upper()]
|
||||||
syminfo = Pair(**d) # validation
|
syminfo = Pair(**d) # validation
|
||||||
|
|
||||||
si = sym_infos[sym] = syminfo.dict()
|
si = sym_infos[sym] = syminfo.to_dict()
|
||||||
|
filters = {}
|
||||||
|
for entry in syminfo.filters:
|
||||||
|
ftype = entry['filterType']
|
||||||
|
filters[ftype] = entry
|
||||||
|
|
||||||
# XXX: after manually inspecting the response format we
|
# XXX: after manually inspecting the response format we
|
||||||
# just directly pick out the info we need
|
# just directly pick out the info we need
|
||||||
si['price_tick_size'] = float(syminfo.filters[0]['tickSize'])
|
si['price_tick_size'] = float(
|
||||||
si['lot_tick_size'] = float(syminfo.filters[2]['stepSize'])
|
filters['PRICE_FILTER']['tickSize']
|
||||||
|
)
|
||||||
|
si['lot_tick_size'] = float(
|
||||||
|
filters['LOT_SIZE']['stepSize']
|
||||||
|
)
|
||||||
si['asset_type'] = 'crypto'
|
si['asset_type'] = 'crypto'
|
||||||
|
|
||||||
symbol = symbols[0]
|
symbol = symbols[0]
|
||||||
|
@ -495,6 +519,7 @@ async def stream_quotes(
|
||||||
subs.append("{sym}@bookTicker")
|
subs.append("{sym}@bookTicker")
|
||||||
|
|
||||||
# unsub from all pairs on teardown
|
# unsub from all pairs on teardown
|
||||||
|
if ws.connected():
|
||||||
await ws.send_msg({
|
await ws.send_msg({
|
||||||
"method": "UNSUBSCRIBE",
|
"method": "UNSUBSCRIBE",
|
||||||
"params": subs,
|
"params": subs,
|
||||||
|
|
|
@ -39,6 +39,148 @@ _config_dir = click.get_app_dir('piker')
|
||||||
_watchlists_data_path = os.path.join(_config_dir, 'watchlists.json')
|
_watchlists_data_path = os.path.join(_config_dir, 'watchlists.json')
|
||||||
|
|
||||||
|
|
||||||
|
OK = '\033[92m'
|
||||||
|
WARNING = '\033[93m'
|
||||||
|
FAIL = '\033[91m'
|
||||||
|
ENDC = '\033[0m'
|
||||||
|
|
||||||
|
|
||||||
|
def print_ok(s: str, **kwargs):
|
||||||
|
print(OK + s + ENDC, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def print_error(s: str, **kwargs):
|
||||||
|
print(FAIL + s + ENDC, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def get_method(client, meth_name: str):
|
||||||
|
print(f'checking client for method \'{meth_name}\'...', end='', flush=True)
|
||||||
|
method = getattr(client, meth_name, None)
|
||||||
|
assert method
|
||||||
|
print_ok('found!.')
|
||||||
|
return method
|
||||||
|
|
||||||
|
async def run_method(client, meth_name: str, **kwargs):
|
||||||
|
method = get_method(client, meth_name)
|
||||||
|
print('running...', end='', flush=True)
|
||||||
|
result = await method(**kwargs)
|
||||||
|
print_ok(f'done! result: {type(result)}')
|
||||||
|
return result
|
||||||
|
|
||||||
|
async def run_test(broker_name: str):
|
||||||
|
brokermod = get_brokermod(broker_name)
|
||||||
|
total = 0
|
||||||
|
passed = 0
|
||||||
|
failed = 0
|
||||||
|
|
||||||
|
print(f'getting client...', end='', flush=True)
|
||||||
|
if not hasattr(brokermod, 'get_client'):
|
||||||
|
print_error('fail! no \'get_client\' context manager found.')
|
||||||
|
return
|
||||||
|
|
||||||
|
async with brokermod.get_client(is_brokercheck=True) as client:
|
||||||
|
print_ok(f'done! inside client context.')
|
||||||
|
|
||||||
|
# check for methods present on brokermod
|
||||||
|
method_list = [
|
||||||
|
'backfill_bars',
|
||||||
|
'get_client',
|
||||||
|
'trades_dialogue',
|
||||||
|
'open_history_client',
|
||||||
|
'open_symbol_search',
|
||||||
|
'stream_quotes',
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
for method in method_list:
|
||||||
|
print(
|
||||||
|
f'checking brokermod for method \'{method}\'...',
|
||||||
|
end='', flush=True)
|
||||||
|
if not hasattr(brokermod, method):
|
||||||
|
print_error(f'fail! method \'{method}\' not found.')
|
||||||
|
failed += 1
|
||||||
|
else:
|
||||||
|
print_ok('done!')
|
||||||
|
passed += 1
|
||||||
|
|
||||||
|
total += 1
|
||||||
|
|
||||||
|
# check for methods present con brokermod.Client and their
|
||||||
|
# results
|
||||||
|
|
||||||
|
# for private methods only check is present
|
||||||
|
method_list = [
|
||||||
|
'get_balances',
|
||||||
|
'get_assets',
|
||||||
|
'get_trades',
|
||||||
|
'get_xfers',
|
||||||
|
'submit_limit',
|
||||||
|
'submit_cancel',
|
||||||
|
'search_symbols',
|
||||||
|
]
|
||||||
|
|
||||||
|
for method_name in method_list:
|
||||||
|
try:
|
||||||
|
get_method(client, method_name)
|
||||||
|
passed += 1
|
||||||
|
|
||||||
|
except AssertionError:
|
||||||
|
print_error(f'fail! method \'{method_name}\' not found.')
|
||||||
|
failed += 1
|
||||||
|
|
||||||
|
total += 1
|
||||||
|
|
||||||
|
|
||||||
|
# check for methods present con brokermod.Client and their
|
||||||
|
# results
|
||||||
|
|
||||||
|
syms = await run_method(client, 'symbol_info')
|
||||||
|
total += 1
|
||||||
|
|
||||||
|
if len(syms) == 0:
|
||||||
|
raise BaseException('Empty Symbol list?')
|
||||||
|
|
||||||
|
passed += 1
|
||||||
|
|
||||||
|
first_sym = tuple(syms.keys())[0]
|
||||||
|
|
||||||
|
method_list = [
|
||||||
|
('cache_symbols', {}),
|
||||||
|
('search_symbols', {'pattern': first_sym[:-1]}),
|
||||||
|
('bars', {'symbol': first_sym})
|
||||||
|
]
|
||||||
|
|
||||||
|
for method_name, method_kwargs in method_list:
|
||||||
|
try:
|
||||||
|
await run_method(client, method_name, **method_kwargs)
|
||||||
|
passed += 1
|
||||||
|
|
||||||
|
except AssertionError:
|
||||||
|
print_error(f'fail! method \'{method_name}\' not found.')
|
||||||
|
failed += 1
|
||||||
|
|
||||||
|
total += 1
|
||||||
|
|
||||||
|
print(f'total: {total}, passed: {passed}, failed: {failed}')
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.argument('broker', nargs=1, required=True)
|
||||||
|
@click.pass_obj
|
||||||
|
def brokercheck(config, broker):
|
||||||
|
'''
|
||||||
|
Test broker apis for completeness.
|
||||||
|
|
||||||
|
'''
|
||||||
|
async def bcheck_main():
|
||||||
|
async with maybe_spawn_brokerd(broker) as portal:
|
||||||
|
await portal.run(run_test, broker)
|
||||||
|
await portal.cancel_actor()
|
||||||
|
|
||||||
|
trio.run(run_test, broker)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option('--keys', '-k', multiple=True,
|
@click.option('--keys', '-k', multiple=True,
|
||||||
help='Return results only for these keys')
|
help='Return results only for these keys')
|
||||||
|
@ -193,6 +335,8 @@ def contracts(ctx, loglevel, broker, symbol, ids):
|
||||||
brokermod = get_brokermod(broker)
|
brokermod = get_brokermod(broker)
|
||||||
get_console_log(loglevel)
|
get_console_log(loglevel)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
contracts = trio.run(partial(core.contracts, brokermod, symbol))
|
contracts = trio.run(partial(core.contracts, brokermod, symbol))
|
||||||
if not ids:
|
if not ids:
|
||||||
# just print out expiry dates which can be used with
|
# just print out expiry dates which can be used with
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
``deribit`` backend
|
||||||
|
------------------
|
||||||
|
pretty good liquidity crypto derivatives, uses custom json rpc over ws for
|
||||||
|
client methods, then `cryptofeed` for data streams.
|
||||||
|
|
||||||
|
status
|
||||||
|
******
|
||||||
|
- supports option charts
|
||||||
|
- no order support yet
|
||||||
|
|
||||||
|
|
||||||
|
config
|
||||||
|
******
|
||||||
|
In order to get order mode support your ``brokers.toml``
|
||||||
|
needs to have something like the following:
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[deribit]
|
||||||
|
key_id = 'XXXXXXXX'
|
||||||
|
key_secret = 'Xx_XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx'
|
||||||
|
|
||||||
|
To obtain an api id and secret you need to create an account, which can be a
|
||||||
|
real market account over at:
|
||||||
|
|
||||||
|
- deribit.com (requires KYC for deposit address)
|
||||||
|
|
||||||
|
Or a testnet account over at:
|
||||||
|
|
||||||
|
- test.deribit.com
|
||||||
|
|
||||||
|
For testnet once the account is created here is how you deposit fake crypto to
|
||||||
|
try it out:
|
||||||
|
|
||||||
|
1) Go to Wallet:
|
||||||
|
|
||||||
|
.. figure:: assets/0_wallet.png
|
||||||
|
:align: center
|
||||||
|
:target: assets/0_wallet.png
|
||||||
|
:alt: wallet page
|
||||||
|
|
||||||
|
2) Then click on the elipsis menu and select deposit
|
||||||
|
|
||||||
|
.. figure:: assets/1_wallet_select_deposit.png
|
||||||
|
:align: center
|
||||||
|
:target: assets/1_wallet_select_deposit.png
|
||||||
|
:alt: wallet deposit page
|
||||||
|
|
||||||
|
3) This will take you to the deposit address page
|
||||||
|
|
||||||
|
.. figure:: assets/2_gen_deposit_addr.png
|
||||||
|
:align: center
|
||||||
|
:target: assets/2_gen_deposit_addr.png
|
||||||
|
:alt: generate deposit address page
|
||||||
|
|
||||||
|
4) After clicking generate you should see the address, copy it and go to the
|
||||||
|
`coin faucet <https://test.deribit.com/dericoin/BTC/deposit>`_ and send fake
|
||||||
|
coins to that address.
|
||||||
|
|
||||||
|
.. figure:: assets/3_deposit_address.png
|
||||||
|
:align: center
|
||||||
|
:target: assets/3_deposit_address.png
|
||||||
|
:alt: generated address
|
||||||
|
|
||||||
|
5) Back in the deposit address page you should see the deposit in your history
|
||||||
|
|
||||||
|
.. figure:: assets/4_wallet_deposit_history.png
|
||||||
|
:align: center
|
||||||
|
:target: assets/4_wallet_deposit_history.png
|
||||||
|
:alt: wallet deposit history
|
|
@ -0,0 +1,65 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Guillermo Rodriguez (in stewardship for piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Deribit backend.
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
from piker.log import get_logger
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
from .api import (
|
||||||
|
get_client,
|
||||||
|
)
|
||||||
|
from .feed import (
|
||||||
|
open_history_client,
|
||||||
|
open_symbol_search,
|
||||||
|
stream_quotes,
|
||||||
|
backfill_bars
|
||||||
|
)
|
||||||
|
# from .broker import (
|
||||||
|
# trades_dialogue,
|
||||||
|
# norm_trade_records,
|
||||||
|
# )
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'get_client',
|
||||||
|
# 'trades_dialogue',
|
||||||
|
'open_history_client',
|
||||||
|
'open_symbol_search',
|
||||||
|
'stream_quotes',
|
||||||
|
# 'norm_trade_records',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# tractor RPC enable arg
|
||||||
|
__enable_modules__: list[str] = [
|
||||||
|
'api',
|
||||||
|
'feed',
|
||||||
|
# 'broker',
|
||||||
|
]
|
||||||
|
|
||||||
|
# passed to ``tractor.ActorNursery.start_actor()``
|
||||||
|
_spawn_kwargs = {
|
||||||
|
'infect_asyncio': True,
|
||||||
|
}
|
||||||
|
|
||||||
|
# annotation to let backend agnostic code
|
||||||
|
# know if ``brokerd`` should be spawned with
|
||||||
|
# ``tractor``'s aio mode.
|
||||||
|
_infect_asyncio: bool = True
|
|
@ -0,0 +1,672 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Guillermo Rodriguez (in stewardship for piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Deribit backend.
|
||||||
|
|
||||||
|
'''
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from contextlib import asynccontextmanager as acm, AsyncExitStack
|
||||||
|
from functools import partial
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any, Optional, Iterable, Callable
|
||||||
|
|
||||||
|
import pendulum
|
||||||
|
import asks
|
||||||
|
import trio
|
||||||
|
from trio_typing import Nursery, TaskStatus
|
||||||
|
from fuzzywuzzy import process as fuzzy
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from piker.data.types import Struct
|
||||||
|
from piker.data._web_bs import (
|
||||||
|
NoBsWs,
|
||||||
|
open_autorecon_ws,
|
||||||
|
open_jsonrpc_session
|
||||||
|
)
|
||||||
|
|
||||||
|
from .._util import resproc
|
||||||
|
|
||||||
|
from piker import config
|
||||||
|
from piker.log import get_logger
|
||||||
|
|
||||||
|
from tractor.trionics import (
|
||||||
|
broadcast_receiver,
|
||||||
|
BroadcastReceiver,
|
||||||
|
maybe_open_context
|
||||||
|
)
|
||||||
|
from tractor import to_asyncio
|
||||||
|
|
||||||
|
from cryptofeed import FeedHandler
|
||||||
|
|
||||||
|
from cryptofeed.defines import (
|
||||||
|
DERIBIT,
|
||||||
|
L1_BOOK, TRADES,
|
||||||
|
OPTION, CALL, PUT
|
||||||
|
)
|
||||||
|
from cryptofeed.symbols import Symbol
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
_spawn_kwargs = {
|
||||||
|
'infect_asyncio': True,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_url = 'https://www.deribit.com'
|
||||||
|
_ws_url = 'wss://www.deribit.com/ws/api/v2'
|
||||||
|
_testnet_ws_url = 'wss://test.deribit.com/ws/api/v2'
|
||||||
|
|
||||||
|
|
||||||
|
# Broker specific ohlc schema (rest)
|
||||||
|
_ohlc_dtype = [
|
||||||
|
('index', int),
|
||||||
|
('time', int),
|
||||||
|
('open', float),
|
||||||
|
('high', float),
|
||||||
|
('low', float),
|
||||||
|
('close', float),
|
||||||
|
('volume', float),
|
||||||
|
('bar_wap', float), # will be zeroed by sampler if not filled
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class JSONRPCResult(Struct):
|
||||||
|
jsonrpc: str = '2.0'
|
||||||
|
id: int
|
||||||
|
result: Optional[dict] = None
|
||||||
|
error: Optional[dict] = None
|
||||||
|
usIn: int
|
||||||
|
usOut: int
|
||||||
|
usDiff: int
|
||||||
|
testnet: bool
|
||||||
|
|
||||||
|
class JSONRPCChannel(Struct):
|
||||||
|
jsonrpc: str = '2.0'
|
||||||
|
method: str
|
||||||
|
params: dict
|
||||||
|
|
||||||
|
|
||||||
|
class KLinesResult(Struct):
|
||||||
|
close: list[float]
|
||||||
|
cost: list[float]
|
||||||
|
high: list[float]
|
||||||
|
low: list[float]
|
||||||
|
open: list[float]
|
||||||
|
status: str
|
||||||
|
ticks: list[int]
|
||||||
|
volume: list[float]
|
||||||
|
|
||||||
|
class Trade(Struct):
|
||||||
|
trade_seq: int
|
||||||
|
trade_id: str
|
||||||
|
timestamp: int
|
||||||
|
tick_direction: int
|
||||||
|
price: float
|
||||||
|
mark_price: float
|
||||||
|
iv: float
|
||||||
|
instrument_name: str
|
||||||
|
index_price: float
|
||||||
|
direction: str
|
||||||
|
combo_trade_id: Optional[int] = 0,
|
||||||
|
combo_id: Optional[str] = '',
|
||||||
|
amount: float
|
||||||
|
|
||||||
|
class LastTradesResult(Struct):
|
||||||
|
trades: list[Trade]
|
||||||
|
has_more: bool
|
||||||
|
|
||||||
|
|
||||||
|
# convert datetime obj timestamp to unixtime in milliseconds
|
||||||
|
def deribit_timestamp(when):
|
||||||
|
return int((when.timestamp() * 1000) + (when.microsecond / 1000))
|
||||||
|
|
||||||
|
|
||||||
|
def str_to_cb_sym(name: str) -> Symbol:
|
||||||
|
base, strike_price, expiry_date, option_type = name.split('-')
|
||||||
|
|
||||||
|
quote = base
|
||||||
|
|
||||||
|
if option_type == 'put':
|
||||||
|
option_type = PUT
|
||||||
|
elif option_type == 'call':
|
||||||
|
option_type = CALL
|
||||||
|
else:
|
||||||
|
raise Exception("Couldn\'t parse option type")
|
||||||
|
|
||||||
|
return Symbol(
|
||||||
|
base, quote,
|
||||||
|
type=OPTION,
|
||||||
|
strike_price=strike_price,
|
||||||
|
option_type=option_type,
|
||||||
|
expiry_date=expiry_date,
|
||||||
|
expiry_normalize=False)
|
||||||
|
|
||||||
|
|
||||||
|
def piker_sym_to_cb_sym(name: str) -> Symbol:
|
||||||
|
base, expiry_date, strike_price, option_type = tuple(
|
||||||
|
name.upper().split('-'))
|
||||||
|
|
||||||
|
quote = base
|
||||||
|
|
||||||
|
if option_type == 'P':
|
||||||
|
option_type = PUT
|
||||||
|
elif option_type == 'C':
|
||||||
|
option_type = CALL
|
||||||
|
else:
|
||||||
|
raise Exception("Couldn\'t parse option type")
|
||||||
|
|
||||||
|
return Symbol(
|
||||||
|
base, quote,
|
||||||
|
type=OPTION,
|
||||||
|
strike_price=strike_price,
|
||||||
|
option_type=option_type,
|
||||||
|
expiry_date=expiry_date.upper())
|
||||||
|
|
||||||
|
|
||||||
|
def cb_sym_to_deribit_inst(sym: Symbol):
|
||||||
|
# cryptofeed normalized
|
||||||
|
cb_norm = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
|
||||||
|
|
||||||
|
# deribit specific
|
||||||
|
months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
|
||||||
|
|
||||||
|
exp = sym.expiry_date
|
||||||
|
|
||||||
|
# YYMDD
|
||||||
|
# 01234
|
||||||
|
year, month, day = (
|
||||||
|
exp[:2], months[cb_norm.index(exp[2:3])], exp[3:])
|
||||||
|
|
||||||
|
otype = 'C' if sym.option_type == CALL else 'P'
|
||||||
|
|
||||||
|
return f'{sym.base}-{day}{month}{year}-{sym.strike_price}-{otype}'
|
||||||
|
|
||||||
|
|
||||||
|
def get_config() -> dict[str, Any]:
|
||||||
|
|
||||||
|
conf, path = config.load()
|
||||||
|
|
||||||
|
section = conf.get('deribit')
|
||||||
|
|
||||||
|
# TODO: document why we send this, basically because logging params for cryptofeed
|
||||||
|
conf['log'] = {}
|
||||||
|
conf['log']['disabled'] = True
|
||||||
|
|
||||||
|
if section is None:
|
||||||
|
log.warning(f'No config section found for deribit in {path}')
|
||||||
|
|
||||||
|
return conf
|
||||||
|
|
||||||
|
|
||||||
|
class Client:
|
||||||
|
|
||||||
|
def __init__(self, json_rpc: Callable) -> None:
|
||||||
|
self._pairs: dict[str, Any] = None
|
||||||
|
|
||||||
|
config = get_config().get('deribit', {})
|
||||||
|
|
||||||
|
if ('key_id' in config) and ('key_secret' in config):
|
||||||
|
self._key_id = config['key_id']
|
||||||
|
self._key_secret = config['key_secret']
|
||||||
|
|
||||||
|
else:
|
||||||
|
self._key_id = None
|
||||||
|
self._key_secret = None
|
||||||
|
|
||||||
|
self.json_rpc = json_rpc
|
||||||
|
|
||||||
|
@property
|
||||||
|
def currencies(self):
|
||||||
|
return ['btc', 'eth', 'sol', 'usd']
|
||||||
|
|
||||||
|
async def get_balances(self, kind: str = 'option') -> dict[str, float]:
|
||||||
|
"""Return the set of positions for this account
|
||||||
|
by symbol.
|
||||||
|
"""
|
||||||
|
balances = {}
|
||||||
|
|
||||||
|
for currency in self.currencies:
|
||||||
|
resp = await self.json_rpc(
|
||||||
|
'private/get_positions', params={
|
||||||
|
'currency': currency.upper(),
|
||||||
|
'kind': kind})
|
||||||
|
|
||||||
|
balances[currency] = resp.result
|
||||||
|
|
||||||
|
return balances
|
||||||
|
|
||||||
|
async def get_assets(self) -> dict[str, float]:
|
||||||
|
"""Return the set of asset balances for this account
|
||||||
|
by symbol.
|
||||||
|
"""
|
||||||
|
balances = {}
|
||||||
|
|
||||||
|
for currency in self.currencies:
|
||||||
|
resp = await self.json_rpc(
|
||||||
|
'private/get_account_summary', params={
|
||||||
|
'currency': currency.upper()})
|
||||||
|
|
||||||
|
balances[currency] = resp.result['balance']
|
||||||
|
|
||||||
|
return balances
|
||||||
|
|
||||||
|
async def submit_limit(
|
||||||
|
self,
|
||||||
|
symbol: str,
|
||||||
|
price: float,
|
||||||
|
action: str,
|
||||||
|
size: float
|
||||||
|
) -> dict:
|
||||||
|
"""Place an order
|
||||||
|
"""
|
||||||
|
params = {
|
||||||
|
'instrument_name': symbol.upper(),
|
||||||
|
'amount': size,
|
||||||
|
'type': 'limit',
|
||||||
|
'price': price,
|
||||||
|
}
|
||||||
|
resp = await self.json_rpc(
|
||||||
|
f'private/{action}', params)
|
||||||
|
|
||||||
|
return resp.result
|
||||||
|
|
||||||
|
async def submit_cancel(self, oid: str):
|
||||||
|
"""Send cancel request for order id
|
||||||
|
"""
|
||||||
|
resp = await self.json_rpc(
|
||||||
|
'private/cancel', {'order_id': oid})
|
||||||
|
return resp.result
|
||||||
|
|
||||||
|
async def symbol_info(
|
||||||
|
self,
|
||||||
|
instrument: Optional[str] = None,
|
||||||
|
currency: str = 'btc', # BTC, ETH, SOL, USDC
|
||||||
|
kind: str = 'option',
|
||||||
|
expired: bool = False
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Get symbol info for the exchange.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if self._pairs:
|
||||||
|
return self._pairs
|
||||||
|
|
||||||
|
# will retrieve all symbols by default
|
||||||
|
params = {
|
||||||
|
'currency': currency.upper(),
|
||||||
|
'kind': kind,
|
||||||
|
'expired': str(expired).lower()
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = await self.json_rpc('public/get_instruments', params)
|
||||||
|
results = resp.result
|
||||||
|
|
||||||
|
instruments = {
|
||||||
|
item['instrument_name'].lower(): item
|
||||||
|
for item in results
|
||||||
|
}
|
||||||
|
|
||||||
|
if instrument is not None:
|
||||||
|
return instruments[instrument]
|
||||||
|
else:
|
||||||
|
return instruments
|
||||||
|
|
||||||
|
async def cache_symbols(
|
||||||
|
self,
|
||||||
|
) -> dict:
|
||||||
|
if not self._pairs:
|
||||||
|
self._pairs = await self.symbol_info()
|
||||||
|
|
||||||
|
return self._pairs
|
||||||
|
|
||||||
|
async def search_symbols(
|
||||||
|
self,
|
||||||
|
pattern: str,
|
||||||
|
limit: int = 30,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
data = await self.symbol_info()
|
||||||
|
|
||||||
|
matches = fuzzy.extractBests(
|
||||||
|
pattern,
|
||||||
|
data,
|
||||||
|
score_cutoff=35,
|
||||||
|
limit=limit
|
||||||
|
)
|
||||||
|
# repack in dict form
|
||||||
|
return {item[0]['instrument_name'].lower(): item[0]
|
||||||
|
for item in matches}
|
||||||
|
|
||||||
|
async def bars(
|
||||||
|
self,
|
||||||
|
symbol: str,
|
||||||
|
start_dt: Optional[datetime] = None,
|
||||||
|
end_dt: Optional[datetime] = None,
|
||||||
|
limit: int = 1000,
|
||||||
|
as_np: bool = True,
|
||||||
|
) -> dict:
|
||||||
|
instrument = symbol
|
||||||
|
|
||||||
|
if end_dt is None:
|
||||||
|
end_dt = pendulum.now('UTC')
|
||||||
|
|
||||||
|
if start_dt is None:
|
||||||
|
start_dt = end_dt.start_of(
|
||||||
|
'minute').subtract(minutes=limit)
|
||||||
|
|
||||||
|
start_time = deribit_timestamp(start_dt)
|
||||||
|
end_time = deribit_timestamp(end_dt)
|
||||||
|
|
||||||
|
# https://docs.deribit.com/#public-get_tradingview_chart_data
|
||||||
|
resp = await self.json_rpc(
|
||||||
|
'public/get_tradingview_chart_data',
|
||||||
|
params={
|
||||||
|
'instrument_name': instrument.upper(),
|
||||||
|
'start_timestamp': start_time,
|
||||||
|
'end_timestamp': end_time,
|
||||||
|
'resolution': '1'
|
||||||
|
})
|
||||||
|
|
||||||
|
result = KLinesResult(**resp.result)
|
||||||
|
new_bars = []
|
||||||
|
for i in range(len(result.close)):
|
||||||
|
|
||||||
|
_open = result.open[i]
|
||||||
|
high = result.high[i]
|
||||||
|
low = result.low[i]
|
||||||
|
close = result.close[i]
|
||||||
|
volume = result.volume[i]
|
||||||
|
|
||||||
|
row = [
|
||||||
|
(start_time + (i * (60 * 1000))) / 1000.0, # time
|
||||||
|
result.open[i],
|
||||||
|
result.high[i],
|
||||||
|
result.low[i],
|
||||||
|
result.close[i],
|
||||||
|
result.volume[i],
|
||||||
|
0
|
||||||
|
]
|
||||||
|
|
||||||
|
new_bars.append((i,) + tuple(row))
|
||||||
|
|
||||||
|
array = np.array(new_bars, dtype=_ohlc_dtype) if as_np else klines
|
||||||
|
return array
|
||||||
|
|
||||||
|
async def last_trades(
|
||||||
|
self,
|
||||||
|
instrument: str,
|
||||||
|
count: int = 10
|
||||||
|
):
|
||||||
|
resp = await self.json_rpc(
|
||||||
|
'public/get_last_trades_by_instrument',
|
||||||
|
params={
|
||||||
|
'instrument_name': instrument,
|
||||||
|
'count': count
|
||||||
|
})
|
||||||
|
|
||||||
|
return LastTradesResult(**resp.result)
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def get_client(
|
||||||
|
is_brokercheck: bool = False
|
||||||
|
) -> Client:
|
||||||
|
|
||||||
|
async with (
|
||||||
|
trio.open_nursery() as n,
|
||||||
|
open_jsonrpc_session(
|
||||||
|
_testnet_ws_url, dtype=JSONRPCResult) as json_rpc
|
||||||
|
):
|
||||||
|
client = Client(json_rpc)
|
||||||
|
|
||||||
|
_refresh_token: Optional[str] = None
|
||||||
|
_access_token: Optional[str] = None
|
||||||
|
|
||||||
|
async def _auth_loop(
|
||||||
|
task_status: TaskStatus = trio.TASK_STATUS_IGNORED
|
||||||
|
):
|
||||||
|
"""Background task that adquires a first access token and then will
|
||||||
|
refresh the access token while the nursery isn't cancelled.
|
||||||
|
|
||||||
|
https://docs.deribit.com/?python#authentication-2
|
||||||
|
"""
|
||||||
|
renew_time = 10
|
||||||
|
access_scope = 'trade:read_write'
|
||||||
|
_expiry_time = time.time()
|
||||||
|
got_access = False
|
||||||
|
nonlocal _refresh_token
|
||||||
|
nonlocal _access_token
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if time.time() - _expiry_time < renew_time:
|
||||||
|
# if we are close to token expiry time
|
||||||
|
|
||||||
|
if _refresh_token != None:
|
||||||
|
# if we have a refresh token already dont need to send
|
||||||
|
# secret
|
||||||
|
params = {
|
||||||
|
'grant_type': 'refresh_token',
|
||||||
|
'refresh_token': _refresh_token,
|
||||||
|
'scope': access_scope
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
# we don't have refresh token, send secret to initialize
|
||||||
|
params = {
|
||||||
|
'grant_type': 'client_credentials',
|
||||||
|
'client_id': client._key_id,
|
||||||
|
'client_secret': client._key_secret,
|
||||||
|
'scope': access_scope
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = await json_rpc('public/auth', params)
|
||||||
|
result = resp.result
|
||||||
|
|
||||||
|
_expiry_time = time.time() + result['expires_in']
|
||||||
|
_refresh_token = result['refresh_token']
|
||||||
|
|
||||||
|
if 'access_token' in result:
|
||||||
|
_access_token = result['access_token']
|
||||||
|
|
||||||
|
if not got_access:
|
||||||
|
# first time this loop runs we must indicate task is
|
||||||
|
# started, we have auth
|
||||||
|
got_access = True
|
||||||
|
task_status.started()
|
||||||
|
|
||||||
|
else:
|
||||||
|
await trio.sleep(renew_time / 2)
|
||||||
|
|
||||||
|
# if we have client creds launch auth loop
|
||||||
|
if client._key_id is not None:
|
||||||
|
await n.start(_auth_loop)
|
||||||
|
|
||||||
|
await client.cache_symbols()
|
||||||
|
yield client
|
||||||
|
n.cancel_scope.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_feed_handler():
|
||||||
|
fh = FeedHandler(config=get_config())
|
||||||
|
yield fh
|
||||||
|
await to_asyncio.run_task(fh.stop_async)
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def maybe_open_feed_handler() -> trio.abc.ReceiveStream:
|
||||||
|
async with maybe_open_context(
|
||||||
|
acm_func=open_feed_handler,
|
||||||
|
key='feedhandler',
|
||||||
|
) as (cache_hit, fh):
|
||||||
|
yield fh
|
||||||
|
|
||||||
|
|
||||||
|
async def aio_price_feed_relay(
|
||||||
|
fh: FeedHandler,
|
||||||
|
instrument: Symbol,
|
||||||
|
from_trio: asyncio.Queue,
|
||||||
|
to_trio: trio.abc.SendChannel,
|
||||||
|
) -> None:
|
||||||
|
async def _trade(data: dict, receipt_timestamp):
|
||||||
|
to_trio.send_nowait(('trade', {
|
||||||
|
'symbol': cb_sym_to_deribit_inst(
|
||||||
|
str_to_cb_sym(data.symbol)).lower(),
|
||||||
|
'last': data,
|
||||||
|
'broker_ts': time.time(),
|
||||||
|
'data': data.to_dict(),
|
||||||
|
'receipt': receipt_timestamp
|
||||||
|
}))
|
||||||
|
|
||||||
|
async def _l1(data: dict, receipt_timestamp):
|
||||||
|
to_trio.send_nowait(('l1', {
|
||||||
|
'symbol': cb_sym_to_deribit_inst(
|
||||||
|
str_to_cb_sym(data.symbol)).lower(),
|
||||||
|
'ticks': [
|
||||||
|
{'type': 'bid',
|
||||||
|
'price': float(data.bid_price), 'size': float(data.bid_size)},
|
||||||
|
{'type': 'bsize',
|
||||||
|
'price': float(data.bid_price), 'size': float(data.bid_size)},
|
||||||
|
{'type': 'ask',
|
||||||
|
'price': float(data.ask_price), 'size': float(data.ask_size)},
|
||||||
|
{'type': 'asize',
|
||||||
|
'price': float(data.ask_price), 'size': float(data.ask_size)}
|
||||||
|
]
|
||||||
|
}))
|
||||||
|
|
||||||
|
fh.add_feed(
|
||||||
|
DERIBIT,
|
||||||
|
channels=[TRADES, L1_BOOK],
|
||||||
|
symbols=[piker_sym_to_cb_sym(instrument)],
|
||||||
|
callbacks={
|
||||||
|
TRADES: _trade,
|
||||||
|
L1_BOOK: _l1
|
||||||
|
})
|
||||||
|
|
||||||
|
if not fh.running:
|
||||||
|
fh.run(
|
||||||
|
start_loop=False,
|
||||||
|
install_signal_handlers=False)
|
||||||
|
|
||||||
|
# sync with trio
|
||||||
|
to_trio.send_nowait(None)
|
||||||
|
|
||||||
|
await asyncio.sleep(float('inf'))
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_price_feed(
|
||||||
|
instrument: str
|
||||||
|
) -> trio.abc.ReceiveStream:
|
||||||
|
async with maybe_open_feed_handler() as fh:
|
||||||
|
async with to_asyncio.open_channel_from(
|
||||||
|
partial(
|
||||||
|
aio_price_feed_relay,
|
||||||
|
fh,
|
||||||
|
instrument
|
||||||
|
)
|
||||||
|
) as (first, chan):
|
||||||
|
yield chan
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def maybe_open_price_feed(
|
||||||
|
instrument: str
|
||||||
|
) -> trio.abc.ReceiveStream:
|
||||||
|
|
||||||
|
# TODO: add a predicate to maybe_open_context
|
||||||
|
async with maybe_open_context(
|
||||||
|
acm_func=open_price_feed,
|
||||||
|
kwargs={
|
||||||
|
'instrument': instrument
|
||||||
|
},
|
||||||
|
key=f'{instrument}-price',
|
||||||
|
) as (cache_hit, feed):
|
||||||
|
if cache_hit:
|
||||||
|
yield broadcast_receiver(feed, 10)
|
||||||
|
else:
|
||||||
|
yield feed
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def aio_order_feed_relay(
|
||||||
|
fh: FeedHandler,
|
||||||
|
instrument: Symbol,
|
||||||
|
from_trio: asyncio.Queue,
|
||||||
|
to_trio: trio.abc.SendChannel,
|
||||||
|
) -> None:
|
||||||
|
async def _fill(data: dict, receipt_timestamp):
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
async def _order_info(data: dict, receipt_timestamp):
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
fh.add_feed(
|
||||||
|
DERIBIT,
|
||||||
|
channels=[FILLS, ORDER_INFO],
|
||||||
|
symbols=[instrument.upper()],
|
||||||
|
callbacks={
|
||||||
|
FILLS: _fill,
|
||||||
|
ORDER_INFO: _order_info,
|
||||||
|
})
|
||||||
|
|
||||||
|
if not fh.running:
|
||||||
|
fh.run(
|
||||||
|
start_loop=False,
|
||||||
|
install_signal_handlers=False)
|
||||||
|
|
||||||
|
# sync with trio
|
||||||
|
to_trio.send_nowait(None)
|
||||||
|
|
||||||
|
await asyncio.sleep(float('inf'))
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_order_feed(
|
||||||
|
instrument: list[str]
|
||||||
|
) -> trio.abc.ReceiveStream:
|
||||||
|
async with maybe_open_feed_handler() as fh:
|
||||||
|
async with to_asyncio.open_channel_from(
|
||||||
|
partial(
|
||||||
|
aio_order_feed_relay,
|
||||||
|
fh,
|
||||||
|
instrument
|
||||||
|
)
|
||||||
|
) as (first, chan):
|
||||||
|
yield chan
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def maybe_open_order_feed(
|
||||||
|
instrument: str
|
||||||
|
) -> trio.abc.ReceiveStream:
|
||||||
|
|
||||||
|
# TODO: add a predicate to maybe_open_context
|
||||||
|
async with maybe_open_context(
|
||||||
|
acm_func=open_order_feed,
|
||||||
|
kwargs={
|
||||||
|
'instrument': instrument,
|
||||||
|
'fh': fh
|
||||||
|
},
|
||||||
|
key=f'{instrument}-order',
|
||||||
|
) as (cache_hit, feed):
|
||||||
|
if cache_hit:
|
||||||
|
yield broadcast_receiver(feed, 10)
|
||||||
|
else:
|
||||||
|
yield feed
|
Binary file not shown.
After Width: | Height: | Size: 169 KiB |
Binary file not shown.
After Width: | Height: | Size: 106 KiB |
Binary file not shown.
After Width: | Height: | Size: 59 KiB |
Binary file not shown.
After Width: | Height: | Size: 70 KiB |
Binary file not shown.
After Width: | Height: | Size: 132 KiB |
|
@ -0,0 +1,185 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Guillermo Rodriguez (in stewardship for piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Deribit backend.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any, Optional, Callable
|
||||||
|
import time
|
||||||
|
|
||||||
|
import trio
|
||||||
|
from trio_typing import TaskStatus
|
||||||
|
import pendulum
|
||||||
|
from fuzzywuzzy import process as fuzzy
|
||||||
|
import numpy as np
|
||||||
|
import tractor
|
||||||
|
|
||||||
|
from piker._cacheables import open_cached_client
|
||||||
|
from piker.log import get_logger, get_console_log
|
||||||
|
from piker.data import ShmArray
|
||||||
|
from piker.brokers._util import (
|
||||||
|
BrokerError,
|
||||||
|
DataUnavailable,
|
||||||
|
)
|
||||||
|
|
||||||
|
from cryptofeed import FeedHandler
|
||||||
|
|
||||||
|
from cryptofeed.defines import (
|
||||||
|
DERIBIT, L1_BOOK, TRADES, OPTION, CALL, PUT
|
||||||
|
)
|
||||||
|
from cryptofeed.symbols import Symbol
|
||||||
|
|
||||||
|
from .api import (
|
||||||
|
Client, Trade,
|
||||||
|
get_config,
|
||||||
|
str_to_cb_sym, piker_sym_to_cb_sym, cb_sym_to_deribit_inst,
|
||||||
|
maybe_open_price_feed
|
||||||
|
)
|
||||||
|
|
||||||
|
_spawn_kwargs = {
|
||||||
|
'infect_asyncio': True,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_history_client(
|
||||||
|
instrument: str,
|
||||||
|
) -> tuple[Callable, int]:
|
||||||
|
|
||||||
|
# TODO implement history getter for the new storage layer.
|
||||||
|
async with open_cached_client('deribit') as client:
|
||||||
|
|
||||||
|
async def get_ohlc(
|
||||||
|
end_dt: Optional[datetime] = None,
|
||||||
|
start_dt: Optional[datetime] = None,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
datetime, # start
|
||||||
|
datetime, # end
|
||||||
|
]:
|
||||||
|
|
||||||
|
array = await client.bars(
|
||||||
|
instrument,
|
||||||
|
start_dt=start_dt,
|
||||||
|
end_dt=end_dt,
|
||||||
|
)
|
||||||
|
if len(array) == 0:
|
||||||
|
raise DataUnavailable
|
||||||
|
|
||||||
|
start_dt = pendulum.from_timestamp(array[0]['time'])
|
||||||
|
end_dt = pendulum.from_timestamp(array[-1]['time'])
|
||||||
|
|
||||||
|
return array, start_dt, end_dt
|
||||||
|
|
||||||
|
yield get_ohlc, {'erlangs': 3, 'rate': 3}
|
||||||
|
|
||||||
|
|
||||||
|
async def stream_quotes(
|
||||||
|
|
||||||
|
send_chan: trio.abc.SendChannel,
|
||||||
|
symbols: list[str],
|
||||||
|
feed_is_live: trio.Event,
|
||||||
|
loglevel: str = None,
|
||||||
|
|
||||||
|
# startup sync
|
||||||
|
task_status: TaskStatus[tuple[dict, dict]] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
# XXX: required to propagate ``tractor`` loglevel to piker logging
|
||||||
|
get_console_log(loglevel or tractor.current_actor().loglevel)
|
||||||
|
|
||||||
|
sym = symbols[0]
|
||||||
|
|
||||||
|
async with (
|
||||||
|
open_cached_client('deribit') as client,
|
||||||
|
send_chan as send_chan
|
||||||
|
):
|
||||||
|
|
||||||
|
init_msgs = {
|
||||||
|
# pass back token, and bool, signalling if we're the writer
|
||||||
|
# and that history has been written
|
||||||
|
sym: {
|
||||||
|
'symbol_info': {
|
||||||
|
'asset_type': 'option',
|
||||||
|
'price_tick_size': 0.0005
|
||||||
|
},
|
||||||
|
'shm_write_opts': {'sum_tick_vml': False},
|
||||||
|
'fqsn': sym,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
nsym = piker_sym_to_cb_sym(sym)
|
||||||
|
|
||||||
|
async with maybe_open_price_feed(sym) as stream:
|
||||||
|
|
||||||
|
cache = await client.cache_symbols()
|
||||||
|
|
||||||
|
last_trades = (await client.last_trades(
|
||||||
|
cb_sym_to_deribit_inst(nsym), count=1)).trades
|
||||||
|
|
||||||
|
if len(last_trades) == 0:
|
||||||
|
last_trade = None
|
||||||
|
async for typ, quote in stream:
|
||||||
|
if typ == 'trade':
|
||||||
|
last_trade = Trade(**(quote['data']))
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
last_trade = Trade(**(last_trades[0]))
|
||||||
|
|
||||||
|
first_quote = {
|
||||||
|
'symbol': sym,
|
||||||
|
'last': last_trade.price,
|
||||||
|
'brokerd_ts': last_trade.timestamp,
|
||||||
|
'ticks': [{
|
||||||
|
'type': 'trade',
|
||||||
|
'price': last_trade.price,
|
||||||
|
'size': last_trade.amount,
|
||||||
|
'broker_ts': last_trade.timestamp
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
task_status.started((init_msgs, first_quote))
|
||||||
|
|
||||||
|
feed_is_live.set()
|
||||||
|
|
||||||
|
async for typ, quote in stream:
|
||||||
|
topic = quote['symbol']
|
||||||
|
await send_chan.send({topic: quote})
|
||||||
|
|
||||||
|
|
||||||
|
@tractor.context
|
||||||
|
async def open_symbol_search(
|
||||||
|
ctx: tractor.Context,
|
||||||
|
) -> Client:
|
||||||
|
async with open_cached_client('deribit') as client:
|
||||||
|
|
||||||
|
# load all symbols locally for fast search
|
||||||
|
cache = await client.cache_symbols()
|
||||||
|
await ctx.started()
|
||||||
|
|
||||||
|
async with ctx.open_stream() as stream:
|
||||||
|
|
||||||
|
async for pattern in stream:
|
||||||
|
# repack in dict form
|
||||||
|
await stream.send(
|
||||||
|
await client.search_symbols(pattern))
|
|
@ -0,0 +1,134 @@
|
||||||
|
``ib`` backend
|
||||||
|
--------------
|
||||||
|
more or less the "everything broker" for traditional and international
|
||||||
|
markets. they are the "go to" provider for automatic retail trading
|
||||||
|
and we interface to their APIs using the `ib_insync` project.
|
||||||
|
|
||||||
|
status
|
||||||
|
******
|
||||||
|
current support is *production grade* and both real-time data and order
|
||||||
|
management should be correct and fast. this backend is used by core devs
|
||||||
|
for live trading.
|
||||||
|
|
||||||
|
currently there is not yet full support for:
|
||||||
|
- options charting and trading
|
||||||
|
- paxos based crypto rt feeds and trading
|
||||||
|
|
||||||
|
|
||||||
|
config
|
||||||
|
******
|
||||||
|
In order to get order mode support your ``brokers.toml``
|
||||||
|
needs to have something like the following:
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[ib]
|
||||||
|
hosts = [
|
||||||
|
"127.0.0.1",
|
||||||
|
]
|
||||||
|
# TODO: when we eventually spawn gateways in our
|
||||||
|
# container, we can just dynamically allocate these
|
||||||
|
# using IBC.
|
||||||
|
ports = [
|
||||||
|
4002,
|
||||||
|
4003,
|
||||||
|
4006,
|
||||||
|
4001,
|
||||||
|
7497,
|
||||||
|
]
|
||||||
|
|
||||||
|
# XXX: for a paper account the flex web query service
|
||||||
|
# is not supported so you have to manually download
|
||||||
|
# and XML report and put it in a location that can be
|
||||||
|
# accessed by the ``brokerd.ib`` backend code for parsing.
|
||||||
|
flex_token = '1111111111111111'
|
||||||
|
flex_trades_query_id = '6969696' # live accounts only?
|
||||||
|
|
||||||
|
# 3rd party web-api token
|
||||||
|
# (XXX: not sure if this works yet)
|
||||||
|
trade_log_token = '111111111111111'
|
||||||
|
|
||||||
|
# when clients are being scanned this determines
|
||||||
|
# which clients are preferred to be used for data feeds
|
||||||
|
# based on account names which are detected as active
|
||||||
|
# on each client.
|
||||||
|
prefer_data_account = [
|
||||||
|
# this has to be first in order to make data work with dual paper + live
|
||||||
|
'main',
|
||||||
|
'algopaper',
|
||||||
|
]
|
||||||
|
|
||||||
|
[ib.accounts]
|
||||||
|
main = 'U69696969'
|
||||||
|
algopaper = 'DU9696969'
|
||||||
|
|
||||||
|
|
||||||
|
If everything works correctly you should see any current positions
|
||||||
|
loaded in the pps pane on chart load and you should also be able to
|
||||||
|
check your trade records in the file::
|
||||||
|
|
||||||
|
<pikerk_conf_dir>/ledgers/trades_ib_algopaper.toml
|
||||||
|
|
||||||
|
|
||||||
|
An example ledger file will have entries written verbatim from the
|
||||||
|
trade events schema:
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
["0000e1a7.630f5e5a.01.01"]
|
||||||
|
secType = "FUT"
|
||||||
|
conId = 515416577
|
||||||
|
symbol = "MNQ"
|
||||||
|
lastTradeDateOrContractMonth = "20221216"
|
||||||
|
strike = 0.0
|
||||||
|
right = ""
|
||||||
|
multiplier = "2"
|
||||||
|
exchange = "GLOBEX"
|
||||||
|
primaryExchange = ""
|
||||||
|
currency = "USD"
|
||||||
|
localSymbol = "MNQZ2"
|
||||||
|
tradingClass = "MNQ"
|
||||||
|
includeExpired = false
|
||||||
|
secIdType = ""
|
||||||
|
secId = ""
|
||||||
|
comboLegsDescrip = ""
|
||||||
|
comboLegs = []
|
||||||
|
execId = "0000e1a7.630f5e5a.01.01"
|
||||||
|
time = 1661972086.0
|
||||||
|
acctNumber = "DU69696969"
|
||||||
|
side = "BOT"
|
||||||
|
shares = 1.0
|
||||||
|
price = 12372.75
|
||||||
|
permId = 441472655
|
||||||
|
clientId = 6116
|
||||||
|
orderId = 985
|
||||||
|
liquidation = 0
|
||||||
|
cumQty = 1.0
|
||||||
|
avgPrice = 12372.75
|
||||||
|
orderRef = ""
|
||||||
|
evRule = ""
|
||||||
|
evMultiplier = 0.0
|
||||||
|
modelCode = ""
|
||||||
|
lastLiquidity = 1
|
||||||
|
broker_time = 1661972086.0
|
||||||
|
name = "ib"
|
||||||
|
commission = 0.57
|
||||||
|
realizedPNL = 243.41
|
||||||
|
yield_ = 0.0
|
||||||
|
yieldRedemptionDate = 0
|
||||||
|
listingExchange = "GLOBEX"
|
||||||
|
date = "2022-08-31T18:54:46+00:00"
|
||||||
|
|
||||||
|
|
||||||
|
your ``pps.toml`` file will have position entries like,
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[ib.algopaper."mnq.globex.20221216"]
|
||||||
|
size = -1.0
|
||||||
|
ppu = 12423.630576923071
|
||||||
|
bsuid = 515416577
|
||||||
|
expiry = "2022-12-16T00:00:00+00:00"
|
||||||
|
clears = [
|
||||||
|
{ dt = "2022-08-31T18:54:46+00:00", ppu = 12423.630576923071, accum_size = -19.0, price = 12372.75, size = 1.0, cost = 0.57, tid = "0000e1a7.630f5e5a.01.01" },
|
||||||
|
]
|
|
@ -20,15 +20,10 @@ Interactive Brokers API backend.
|
||||||
Sub-modules within break into the core functionalities:
|
Sub-modules within break into the core functionalities:
|
||||||
|
|
||||||
- ``broker.py`` part for orders / trading endpoints
|
- ``broker.py`` part for orders / trading endpoints
|
||||||
- ``data.py`` for real-time data feed endpoints
|
- ``feed.py`` for real-time data feed endpoints
|
||||||
|
- ``api.py`` for the core API machinery which is ``trio``-ized
|
||||||
- ``client.py`` for the core API machinery which is ``trio``-ized
|
|
||||||
wrapping around ``ib_insync``.
|
wrapping around ``ib_insync``.
|
||||||
|
|
||||||
- ``report.py`` for the hackery to build manual pp calcs
|
|
||||||
to avoid ib's absolute bullshit FIFO style position
|
|
||||||
tracking..
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from .api import (
|
from .api import (
|
||||||
get_client,
|
get_client,
|
||||||
|
@ -38,7 +33,10 @@ from .feed import (
|
||||||
open_symbol_search,
|
open_symbol_search,
|
||||||
stream_quotes,
|
stream_quotes,
|
||||||
)
|
)
|
||||||
from .broker import trades_dialogue
|
from .broker import (
|
||||||
|
trades_dialogue,
|
||||||
|
norm_trade_records,
|
||||||
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'get_client',
|
'get_client',
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -22,6 +22,7 @@ import asyncio
|
||||||
from contextlib import asynccontextmanager as acm
|
from contextlib import asynccontextmanager as acm
|
||||||
from dataclasses import asdict
|
from dataclasses import asdict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import partial
|
||||||
from math import isnan
|
from math import isnan
|
||||||
import time
|
import time
|
||||||
from typing import (
|
from typing import (
|
||||||
|
@ -38,10 +39,14 @@ import tractor
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
|
|
||||||
from piker.data._sharedmem import ShmArray
|
from .._util import (
|
||||||
from .._util import SymbolNotFound, NoData
|
NoData,
|
||||||
|
DataUnavailable,
|
||||||
|
SymbolNotFound,
|
||||||
|
)
|
||||||
from .api import (
|
from .api import (
|
||||||
_adhoc_futes_set,
|
# _adhoc_futes_set,
|
||||||
|
con2fqsn,
|
||||||
log,
|
log,
|
||||||
load_aio_clients,
|
load_aio_clients,
|
||||||
ibis,
|
ibis,
|
||||||
|
@ -102,7 +107,7 @@ async def open_data_client() -> MethodProxy:
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def open_history_client(
|
async def open_history_client(
|
||||||
symbol: str,
|
fqsn: str,
|
||||||
|
|
||||||
) -> tuple[Callable, int]:
|
) -> tuple[Callable, int]:
|
||||||
'''
|
'''
|
||||||
|
@ -110,26 +115,75 @@ async def open_history_client(
|
||||||
that takes in ``pendulum.datetime`` and returns ``numpy`` arrays.
|
that takes in ``pendulum.datetime`` and returns ``numpy`` arrays.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
# TODO:
|
||||||
|
# - add logic to handle tradable hours and only grab
|
||||||
|
# valid bars in the range?
|
||||||
|
# - we want to avoid overrunning the underlying shm array buffer and
|
||||||
|
# we should probably calc the number of calls to make depending on
|
||||||
|
# that until we have the `marketstore` daemon in place in which case
|
||||||
|
# the shm size will be driven by user config and available sys
|
||||||
|
# memory.
|
||||||
|
|
||||||
async with open_data_client() as proxy:
|
async with open_data_client() as proxy:
|
||||||
|
|
||||||
|
max_timeout: float = 2.
|
||||||
|
mean: float = 0
|
||||||
|
count: int = 0
|
||||||
|
|
||||||
|
head_dt: None | datetime = None
|
||||||
|
if (
|
||||||
|
# fx cons seem to not provide this endpoint?
|
||||||
|
'idealpro' not in fqsn
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
head_dt = await proxy.get_head_time(fqsn=fqsn)
|
||||||
|
except RequestError:
|
||||||
|
head_dt = None
|
||||||
|
|
||||||
async def get_hist(
|
async def get_hist(
|
||||||
|
timeframe: float,
|
||||||
end_dt: Optional[datetime] = None,
|
end_dt: Optional[datetime] = None,
|
||||||
start_dt: Optional[datetime] = None,
|
start_dt: Optional[datetime] = None,
|
||||||
|
|
||||||
) -> tuple[np.ndarray, str]:
|
) -> tuple[np.ndarray, str]:
|
||||||
|
nonlocal max_timeout, mean, count
|
||||||
|
|
||||||
out, fails = await get_bars(proxy, symbol, end_dt=end_dt)
|
query_start = time.time()
|
||||||
|
out, timedout = await get_bars(
|
||||||
|
proxy,
|
||||||
|
fqsn,
|
||||||
|
timeframe,
|
||||||
|
end_dt=end_dt,
|
||||||
|
)
|
||||||
|
latency = time.time() - query_start
|
||||||
|
if (
|
||||||
|
not timedout
|
||||||
|
# and latency <= max_timeout
|
||||||
|
):
|
||||||
|
count += 1
|
||||||
|
mean += latency / count
|
||||||
|
print(
|
||||||
|
f'HISTORY FRAME QUERY LATENCY: {latency}\n'
|
||||||
|
f'mean: {mean}'
|
||||||
|
)
|
||||||
|
|
||||||
# TODO: add logic here to handle tradable hours and only grab
|
if (
|
||||||
# valid bars in the range
|
out is None
|
||||||
if out is None:
|
):
|
||||||
# could be trying to retreive bars over weekend
|
# could be trying to retreive bars over weekend
|
||||||
log.error(f"Can't grab bars starting at {end_dt}!?!?")
|
log.error(f"Can't grab bars starting at {end_dt}!?!?")
|
||||||
raise NoData(
|
raise NoData(
|
||||||
f'{end_dt}',
|
f'{end_dt}',
|
||||||
frame_size=2000,
|
# frame_size=2000,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
end_dt
|
||||||
|
and head_dt
|
||||||
|
and end_dt <= head_dt
|
||||||
|
):
|
||||||
|
raise DataUnavailable(f'First timestamp is {head_dt}')
|
||||||
|
|
||||||
bars, bars_array, first_dt, last_dt = out
|
bars, bars_array, first_dt, last_dt = out
|
||||||
|
|
||||||
# volume cleaning since there's -ve entries,
|
# volume cleaning since there's -ve entries,
|
||||||
|
@ -144,7 +198,7 @@ async def open_history_client(
|
||||||
# quite sure why.. needs some tinkering and probably
|
# quite sure why.. needs some tinkering and probably
|
||||||
# a lookthrough of the ``ib_insync`` machinery, for eg. maybe
|
# a lookthrough of the ``ib_insync`` machinery, for eg. maybe
|
||||||
# we have to do the batch queries on the `asyncio` side?
|
# we have to do the batch queries on the `asyncio` side?
|
||||||
yield get_hist, {'erlangs': 1, 'rate': 6}
|
yield get_hist, {'erlangs': 1, 'rate': 3}
|
||||||
|
|
||||||
|
|
||||||
_pacing: str = (
|
_pacing: str = (
|
||||||
|
@ -153,96 +207,19 @@ _pacing: str = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def get_bars(
|
async def wait_on_data_reset(
|
||||||
|
|
||||||
proxy: MethodProxy,
|
proxy: MethodProxy,
|
||||||
fqsn: str,
|
reset_type: str = 'data',
|
||||||
|
timeout: float = 16,
|
||||||
|
|
||||||
# blank to start which tells ib to look up the latest datum
|
task_status: TaskStatus[
|
||||||
end_dt: str = '',
|
tuple[
|
||||||
|
trio.CancelScope,
|
||||||
|
trio.Event,
|
||||||
|
]
|
||||||
|
] = trio.TASK_STATUS_IGNORED,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
) -> (dict, np.ndarray):
|
|
||||||
'''
|
|
||||||
Retrieve historical data from a ``trio``-side task using
|
|
||||||
a ``MethoProxy``.
|
|
||||||
|
|
||||||
'''
|
|
||||||
fails = 0
|
|
||||||
bars: Optional[list] = None
|
|
||||||
first_dt: datetime = None
|
|
||||||
last_dt: datetime = None
|
|
||||||
|
|
||||||
if end_dt:
|
|
||||||
last_dt = pendulum.from_timestamp(end_dt.timestamp())
|
|
||||||
|
|
||||||
for _ in range(10):
|
|
||||||
try:
|
|
||||||
out = await proxy.bars(
|
|
||||||
fqsn=fqsn,
|
|
||||||
end_dt=end_dt,
|
|
||||||
)
|
|
||||||
if out:
|
|
||||||
bars, bars_array = out
|
|
||||||
|
|
||||||
else:
|
|
||||||
await tractor.breakpoint()
|
|
||||||
|
|
||||||
if bars_array is None:
|
|
||||||
raise SymbolNotFound(fqsn)
|
|
||||||
|
|
||||||
first_dt = pendulum.from_timestamp(
|
|
||||||
bars[0].date.timestamp())
|
|
||||||
|
|
||||||
last_dt = pendulum.from_timestamp(
|
|
||||||
bars[-1].date.timestamp())
|
|
||||||
|
|
||||||
time = bars_array['time']
|
|
||||||
assert time[-1] == last_dt.timestamp()
|
|
||||||
assert time[0] == first_dt.timestamp()
|
|
||||||
log.info(
|
|
||||||
f'{len(bars)} bars retreived for {first_dt} -> {last_dt}'
|
|
||||||
)
|
|
||||||
|
|
||||||
return (bars, bars_array, first_dt, last_dt), fails
|
|
||||||
|
|
||||||
except RequestError as err:
|
|
||||||
msg = err.message
|
|
||||||
# why do we always need to rebind this?
|
|
||||||
# _err = err
|
|
||||||
|
|
||||||
if 'No market data permissions for' in msg:
|
|
||||||
# TODO: signalling for no permissions searches
|
|
||||||
raise NoData(
|
|
||||||
f'Symbol: {fqsn}',
|
|
||||||
)
|
|
||||||
|
|
||||||
elif (
|
|
||||||
err.code == 162
|
|
||||||
and 'HMDS query returned no data' in err.message
|
|
||||||
):
|
|
||||||
# XXX: this is now done in the storage mgmt layer
|
|
||||||
# and we shouldn't implicitly decrement the frame dt
|
|
||||||
# index since the upper layer may be doing so
|
|
||||||
# concurrently and we don't want to be delivering frames
|
|
||||||
# that weren't asked for.
|
|
||||||
log.warning(
|
|
||||||
f'NO DATA found ending @ {end_dt}\n'
|
|
||||||
)
|
|
||||||
|
|
||||||
# try to decrement start point and look further back
|
|
||||||
# end_dt = last_dt = last_dt.subtract(seconds=2000)
|
|
||||||
|
|
||||||
raise NoData(
|
|
||||||
f'Symbol: {fqsn}',
|
|
||||||
frame_size=2000,
|
|
||||||
)
|
|
||||||
|
|
||||||
elif _pacing in msg:
|
|
||||||
|
|
||||||
log.warning(
|
|
||||||
'History throttle rate reached!\n'
|
|
||||||
'Resetting farms with `ctrl-alt-f` hack\n'
|
|
||||||
)
|
|
||||||
# TODO: we might have to put a task lock around this
|
# TODO: we might have to put a task lock around this
|
||||||
# method..
|
# method..
|
||||||
hist_ev = proxy.status_event(
|
hist_ev = proxy.status_event(
|
||||||
|
@ -258,144 +235,259 @@ async def get_bars(
|
||||||
# live_ev = proxy.status_event(
|
# live_ev = proxy.status_event(
|
||||||
# 'Market data farm connection is OK:usfuture'
|
# 'Market data farm connection is OK:usfuture'
|
||||||
# )
|
# )
|
||||||
|
|
||||||
# try to wait on the reset event(s) to arrive, a timeout
|
# try to wait on the reset event(s) to arrive, a timeout
|
||||||
# will trigger a retry up to 6 times (for now).
|
# will trigger a retry up to 6 times (for now).
|
||||||
tries: int = 2
|
|
||||||
timeout: float = 10
|
|
||||||
|
|
||||||
# try 3 time with a data reset then fail over to
|
done = trio.Event()
|
||||||
# a connection reset.
|
with trio.move_on_after(timeout) as cs:
|
||||||
for i in range(1, tries):
|
|
||||||
|
task_status.started((cs, done))
|
||||||
|
|
||||||
log.warning('Sending DATA RESET request')
|
log.warning('Sending DATA RESET request')
|
||||||
await data_reset_hack(reset_type='data')
|
res = await data_reset_hack(reset_type=reset_type)
|
||||||
|
|
||||||
|
if not res:
|
||||||
|
log.warning(
|
||||||
|
'NO VNC DETECTED!\n'
|
||||||
|
'Manually press ctrl-alt-f on your IB java app'
|
||||||
|
)
|
||||||
|
done.set()
|
||||||
|
return False
|
||||||
|
|
||||||
with trio.move_on_after(timeout) as cs:
|
|
||||||
for name, ev in [
|
|
||||||
# TODO: not sure if waiting on other events
|
# TODO: not sure if waiting on other events
|
||||||
# is all that useful here or not. in theory
|
# is all that useful here or not.
|
||||||
# you could wait on one of the ones above
|
# - in theory you could wait on one of the ones above first
|
||||||
# first to verify the reset request was
|
# to verify the reset request was sent?
|
||||||
# sent?
|
# - we need the same for real-time quote feeds which can
|
||||||
|
# sometimes flake out and stop delivering..
|
||||||
|
for name, ev in [
|
||||||
('history', hist_ev),
|
('history', hist_ev),
|
||||||
]:
|
]:
|
||||||
await ev.wait()
|
await ev.wait()
|
||||||
log.info(f"{name} DATA RESET")
|
log.info(f"{name} DATA RESET")
|
||||||
break
|
done.set()
|
||||||
|
return True
|
||||||
|
|
||||||
if cs.cancelled_caught:
|
if cs.cancel_called:
|
||||||
fails += 1
|
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Data reset {name} timeout, retrying {i}.'
|
'Data reset task canceled?'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
done.set()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
_data_resetter_task: trio.Task | None = None
|
||||||
|
|
||||||
|
|
||||||
|
async def get_bars(
|
||||||
|
|
||||||
|
proxy: MethodProxy,
|
||||||
|
fqsn: str,
|
||||||
|
timeframe: int,
|
||||||
|
|
||||||
|
# blank to start which tells ib to look up the latest datum
|
||||||
|
end_dt: str = '',
|
||||||
|
|
||||||
|
# TODO: make this more dynamic based on measured frame rx latency?
|
||||||
|
# how long before we trigger a feed reset (seconds)
|
||||||
|
feed_reset_timeout: float = 3,
|
||||||
|
|
||||||
|
# how many days to subtract before giving up on further
|
||||||
|
# history queries for instrument, presuming that most don't
|
||||||
|
# not trade for a week XD
|
||||||
|
max_nodatas: int = 6,
|
||||||
|
|
||||||
|
task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
|
) -> (dict, np.ndarray):
|
||||||
|
'''
|
||||||
|
Retrieve historical data from a ``trio``-side task using
|
||||||
|
a ``MethoProxy``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
global _data_resetter_task
|
||||||
|
nodatas_count: int = 0
|
||||||
|
|
||||||
|
data_cs: trio.CancelScope | None = None
|
||||||
|
result: tuple[
|
||||||
|
ibis.objects.BarDataList,
|
||||||
|
np.ndarray,
|
||||||
|
datetime,
|
||||||
|
datetime,
|
||||||
|
] | None = None
|
||||||
|
result_ready = trio.Event()
|
||||||
|
|
||||||
|
async def query():
|
||||||
|
nonlocal result, data_cs, end_dt, nodatas_count
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
out = await proxy.bars(
|
||||||
|
fqsn=fqsn,
|
||||||
|
end_dt=end_dt,
|
||||||
|
sample_period_s=timeframe,
|
||||||
|
|
||||||
|
# ideally we cancel the request just before we
|
||||||
|
# cancel on the ``trio``-side and trigger a data
|
||||||
|
# reset hack.. the problem is there's no way (with
|
||||||
|
# current impl) to detect a cancel case.
|
||||||
|
# timeout=timeout,
|
||||||
|
)
|
||||||
|
if out is None:
|
||||||
|
raise NoData(f'{end_dt}')
|
||||||
|
|
||||||
|
bars, bars_array, dt_duration = out
|
||||||
|
|
||||||
|
if not bars:
|
||||||
|
log.warning(
|
||||||
|
f'History is blank for {dt_duration} from {end_dt}'
|
||||||
|
)
|
||||||
|
end_dt -= dt_duration
|
||||||
continue
|
continue
|
||||||
else:
|
|
||||||
|
|
||||||
log.warning('Sending CONNECTION RESET')
|
if bars_array is None:
|
||||||
await data_reset_hack(reset_type='connection')
|
raise SymbolNotFound(fqsn)
|
||||||
|
|
||||||
with trio.move_on_after(timeout) as cs:
|
first_dt = pendulum.from_timestamp(
|
||||||
for name, ev in [
|
bars[0].date.timestamp())
|
||||||
# TODO: not sure if waiting on other events
|
|
||||||
# is all that useful here or not. in theory
|
|
||||||
# you could wait on one of the ones above
|
|
||||||
# first to verify the reset request was
|
|
||||||
# sent?
|
|
||||||
('history', hist_ev),
|
|
||||||
]:
|
|
||||||
await ev.wait()
|
|
||||||
log.info(f"{name} DATA RESET")
|
|
||||||
|
|
||||||
if cs.cancelled_caught:
|
last_dt = pendulum.from_timestamp(
|
||||||
fails += 1
|
bars[-1].date.timestamp())
|
||||||
log.warning('Data CONNECTION RESET timeout!?')
|
|
||||||
|
time = bars_array['time']
|
||||||
|
assert time[-1] == last_dt.timestamp()
|
||||||
|
assert time[0] == first_dt.timestamp()
|
||||||
|
log.info(
|
||||||
|
f'{len(bars)} bars retreived {first_dt} -> {last_dt}'
|
||||||
|
)
|
||||||
|
|
||||||
|
if data_cs:
|
||||||
|
data_cs.cancel()
|
||||||
|
|
||||||
|
result = (bars, bars_array, first_dt, last_dt)
|
||||||
|
|
||||||
|
# signal data reset loop parent task
|
||||||
|
result_ready.set()
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
except RequestError as err:
|
||||||
|
msg = err.message
|
||||||
|
|
||||||
|
if 'No market data permissions for' in msg:
|
||||||
|
# TODO: signalling for no permissions searches
|
||||||
|
raise NoData(
|
||||||
|
f'Symbol: {fqsn}',
|
||||||
|
)
|
||||||
|
|
||||||
|
elif err.code == 162:
|
||||||
|
if (
|
||||||
|
'HMDS query returned no data' in msg
|
||||||
|
):
|
||||||
|
# XXX: this is now done in the storage mgmt
|
||||||
|
# layer and we shouldn't implicitly decrement
|
||||||
|
# the frame dt index since the upper layer may
|
||||||
|
# be doing so concurrently and we don't want to
|
||||||
|
# be delivering frames that weren't asked for.
|
||||||
|
# try to decrement start point and look further back
|
||||||
|
# end_dt = end_dt.subtract(seconds=2000)
|
||||||
|
logmsg = "SUBTRACTING DAY from DT index"
|
||||||
|
if end_dt is not None:
|
||||||
|
end_dt = end_dt.subtract(days=1)
|
||||||
|
elif end_dt is None:
|
||||||
|
end_dt = pendulum.now().subtract(days=1)
|
||||||
|
|
||||||
|
log.warning(
|
||||||
|
f'NO DATA found ending @ {end_dt}\n'
|
||||||
|
+ logmsg
|
||||||
|
)
|
||||||
|
|
||||||
|
if nodatas_count >= max_nodatas:
|
||||||
|
raise DataUnavailable(
|
||||||
|
f'Presuming {fqsn} has no further history '
|
||||||
|
f'after {max_nodatas} tries..'
|
||||||
|
)
|
||||||
|
|
||||||
|
nodatas_count += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
elif 'API historical data query cancelled' in err.message:
|
||||||
|
log.warning(
|
||||||
|
'Query cancelled by IB (:eyeroll:):\n'
|
||||||
|
f'{err.message}'
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
elif (
|
||||||
|
'Trading TWS session is connected from a different IP'
|
||||||
|
in err.message
|
||||||
|
):
|
||||||
|
log.warning("ignoring ip address warning")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# XXX: more or less same as above timeout case
|
||||||
|
elif _pacing in msg:
|
||||||
|
log.warning(
|
||||||
|
'History throttle rate reached!\n'
|
||||||
|
'Resetting farms with `ctrl-alt-f` hack\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
# cancel any existing reset task
|
||||||
|
if data_cs:
|
||||||
|
data_cs.cancel()
|
||||||
|
|
||||||
|
# spawn new data reset task
|
||||||
|
data_cs, reset_done = await nurse.start(
|
||||||
|
partial(
|
||||||
|
wait_on_data_reset,
|
||||||
|
proxy,
|
||||||
|
timeout=float('inf'),
|
||||||
|
reset_type='connection'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
return None, None
|
# TODO: make this global across all history task/requests
|
||||||
# else: # throttle wasn't fixed so error out immediately
|
# such that simultaneous symbol queries don't try data resettingn
|
||||||
# raise _err
|
# too fast..
|
||||||
|
unset_resetter: bool = False
|
||||||
|
async with trio.open_nursery() as nurse:
|
||||||
|
|
||||||
|
# start history request that we allow
|
||||||
|
# to run indefinitely until a result is acquired
|
||||||
|
nurse.start_soon(query)
|
||||||
|
|
||||||
async def backfill_bars(
|
# start history reset loop which waits up to the timeout
|
||||||
|
# for a result before triggering a data feed reset.
|
||||||
|
while not result_ready.is_set():
|
||||||
|
|
||||||
fqsn: str,
|
with trio.move_on_after(feed_reset_timeout):
|
||||||
shm: ShmArray, # type: ignore # noqa
|
await result_ready.wait()
|
||||||
|
break
|
||||||
|
|
||||||
# TODO: we want to avoid overrunning the underlying shm array buffer
|
if _data_resetter_task:
|
||||||
# and we should probably calc the number of calls to make depending
|
# don't double invoke the reset hack if another
|
||||||
# on that until we have the `marketstore` daemon in place in which
|
# requester task already has it covered.
|
||||||
# case the shm size will be driven by user config and available sys
|
|
||||||
# memory.
|
|
||||||
count: int = 16,
|
|
||||||
|
|
||||||
task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''
|
|
||||||
Fill historical bars into shared mem / storage afap.
|
|
||||||
|
|
||||||
TODO: avoid pacing constraints:
|
|
||||||
https://github.com/pikers/piker/issues/128
|
|
||||||
|
|
||||||
'''
|
|
||||||
# last_dt1 = None
|
|
||||||
last_dt = None
|
|
||||||
|
|
||||||
with trio.CancelScope() as cs:
|
|
||||||
|
|
||||||
async with open_data_client() as proxy:
|
|
||||||
|
|
||||||
out, fails = await get_bars(proxy, fqsn)
|
|
||||||
|
|
||||||
if out is None:
|
|
||||||
raise RuntimeError("Could not pull currrent history?!")
|
|
||||||
|
|
||||||
(first_bars, bars_array, first_dt, last_dt) = out
|
|
||||||
vlm = bars_array['volume']
|
|
||||||
vlm[vlm < 0] = 0
|
|
||||||
last_dt = first_dt
|
|
||||||
|
|
||||||
# write historical data to buffer
|
|
||||||
shm.push(bars_array)
|
|
||||||
|
|
||||||
task_status.started(cs)
|
|
||||||
|
|
||||||
i = 0
|
|
||||||
while i < count:
|
|
||||||
|
|
||||||
out, fails = await get_bars(proxy, fqsn, end_dt=first_dt)
|
|
||||||
|
|
||||||
if out is None:
|
|
||||||
# could be trying to retreive bars over weekend
|
|
||||||
# TODO: add logic here to handle tradable hours and
|
|
||||||
# only grab valid bars in the range
|
|
||||||
log.error(f"Can't grab bars starting at {first_dt}!?!?")
|
|
||||||
|
|
||||||
# XXX: get_bars() should internally decrement dt by
|
|
||||||
# 2k seconds and try again.
|
|
||||||
continue
|
continue
|
||||||
|
else:
|
||||||
|
_data_resetter_task = trio.lowlevel.current_task()
|
||||||
|
unset_resetter = True
|
||||||
|
|
||||||
(first_bars, bars_array, first_dt, last_dt) = out
|
# spawn new data reset task
|
||||||
# last_dt1 = last_dt
|
data_cs, reset_done = await nurse.start(
|
||||||
# last_dt = first_dt
|
partial(
|
||||||
|
wait_on_data_reset,
|
||||||
|
proxy,
|
||||||
|
timeout=float('inf'),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# sync wait on reset to complete
|
||||||
|
await reset_done.wait()
|
||||||
|
|
||||||
# volume cleaning since there's -ve entries,
|
_data_resetter_task = None if unset_resetter else _data_resetter_task
|
||||||
# wood luv to know what crookery that is..
|
return result, data_cs is not None
|
||||||
vlm = bars_array['volume']
|
|
||||||
vlm[vlm < 0] = 0
|
|
||||||
|
|
||||||
# TODO we should probably dig into forums to see what peeps
|
|
||||||
# think this data "means" and then use it as an indicator of
|
|
||||||
# sorts? dinkus has mentioned that $vlms for the day dont'
|
|
||||||
# match other platforms nor the summary stat tws shows in
|
|
||||||
# the monitor - it's probably worth investigating.
|
|
||||||
|
|
||||||
shm.push(bars_array, prepend=True)
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
|
|
||||||
asset_type_map = {
|
asset_type_map = {
|
||||||
|
@ -413,6 +505,7 @@ asset_type_map = {
|
||||||
'WAR': 'warrant',
|
'WAR': 'warrant',
|
||||||
'IOPT': 'warran',
|
'IOPT': 'warran',
|
||||||
'BAG': 'bag',
|
'BAG': 'bag',
|
||||||
|
'CRYPTO': 'crypto', # bc it's diff then fiat?
|
||||||
# 'NEWS': 'news',
|
# 'NEWS': 'news',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,7 +545,9 @@ async def _setup_quote_stream(
|
||||||
|
|
||||||
to_trio.send_nowait(None)
|
to_trio.send_nowait(None)
|
||||||
|
|
||||||
async with load_aio_clients() as accts2clients:
|
async with load_aio_clients(
|
||||||
|
disconnect_on_exit=False,
|
||||||
|
) as accts2clients:
|
||||||
caccount_name, client = get_preferred_data_client(accts2clients)
|
caccount_name, client = get_preferred_data_client(accts2clients)
|
||||||
contract = contract or (await client.find_contract(symbol))
|
contract = contract or (await client.find_contract(symbol))
|
||||||
ticker: Ticker = client.ib.reqMktData(contract, ','.join(opts))
|
ticker: Ticker = client.ib.reqMktData(contract, ','.join(opts))
|
||||||
|
@ -498,10 +593,11 @@ async def _setup_quote_stream(
|
||||||
# Manually do the dereg ourselves.
|
# Manually do the dereg ourselves.
|
||||||
teardown()
|
teardown()
|
||||||
except trio.WouldBlock:
|
except trio.WouldBlock:
|
||||||
log.warning(
|
# log.warning(
|
||||||
f'channel is blocking symbol feed for {symbol}?'
|
# f'channel is blocking symbol feed for {symbol}?'
|
||||||
f'\n{to_trio.statistics}'
|
# f'\n{to_trio.statistics}'
|
||||||
)
|
# )
|
||||||
|
pass
|
||||||
|
|
||||||
# except trio.WouldBlock:
|
# except trio.WouldBlock:
|
||||||
# # for slow debugging purposes to avoid clobbering prompt
|
# # for slow debugging purposes to avoid clobbering prompt
|
||||||
|
@ -531,7 +627,8 @@ async def open_aio_quote_stream(
|
||||||
from_aio = _quote_streams.get(symbol)
|
from_aio = _quote_streams.get(symbol)
|
||||||
if from_aio:
|
if from_aio:
|
||||||
|
|
||||||
# if we already have a cached feed deliver a rx side clone to consumer
|
# if we already have a cached feed deliver a rx side clone
|
||||||
|
# to consumer
|
||||||
async with broadcast_receiver(
|
async with broadcast_receiver(
|
||||||
from_aio,
|
from_aio,
|
||||||
2**6,
|
2**6,
|
||||||
|
@ -553,38 +650,17 @@ async def open_aio_quote_stream(
|
||||||
|
|
||||||
|
|
||||||
# TODO: cython/mypyc/numba this!
|
# TODO: cython/mypyc/numba this!
|
||||||
|
# or we can at least cache a majority of the values
|
||||||
|
# except for the ones we expect to change?..
|
||||||
def normalize(
|
def normalize(
|
||||||
ticker: Ticker,
|
ticker: Ticker,
|
||||||
calc_price: bool = False
|
calc_price: bool = False
|
||||||
|
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
|
||||||
# should be real volume for this contract by default
|
|
||||||
calc_price = False
|
|
||||||
|
|
||||||
# check for special contract types
|
# check for special contract types
|
||||||
con = ticker.contract
|
con = ticker.contract
|
||||||
if type(con) in (
|
fqsn, calc_price = con2fqsn(con)
|
||||||
ibis.Commodity,
|
|
||||||
ibis.Forex,
|
|
||||||
):
|
|
||||||
# commodities and forex don't have an exchange name and
|
|
||||||
# no real volume so we have to calculate the price
|
|
||||||
suffix = con.secType
|
|
||||||
# no real volume on this tract
|
|
||||||
calc_price = True
|
|
||||||
|
|
||||||
else:
|
|
||||||
suffix = con.primaryExchange
|
|
||||||
if not suffix:
|
|
||||||
suffix = con.exchange
|
|
||||||
|
|
||||||
# append a `.<suffix>` to the returned symbol
|
|
||||||
# key for derivatives that normally is the expiry
|
|
||||||
# date key.
|
|
||||||
expiry = con.lastTradeDateOrContractMonth
|
|
||||||
if expiry:
|
|
||||||
suffix += f'.{expiry}'
|
|
||||||
|
|
||||||
# convert named tuples to dicts so we send usable keys
|
# convert named tuples to dicts so we send usable keys
|
||||||
new_ticks = []
|
new_ticks = []
|
||||||
|
@ -616,9 +692,7 @@ def normalize(
|
||||||
|
|
||||||
# generate fqsn with possible specialized suffix
|
# generate fqsn with possible specialized suffix
|
||||||
# for derivatives, note the lowercase.
|
# for derivatives, note the lowercase.
|
||||||
data['symbol'] = data['fqsn'] = '.'.join(
|
data['symbol'] = data['fqsn'] = fqsn
|
||||||
(con.symbol, suffix)
|
|
||||||
).lower()
|
|
||||||
|
|
||||||
# convert named tuples to dicts for transport
|
# convert named tuples to dicts for transport
|
||||||
tbts = data.get('tickByTicks')
|
tbts = data.get('tickByTicks')
|
||||||
|
@ -683,6 +757,13 @@ async def stream_quotes(
|
||||||
# TODO: more consistent field translation
|
# TODO: more consistent field translation
|
||||||
atype = syminfo['asset_type'] = asset_type_map[syminfo['secType']]
|
atype = syminfo['asset_type'] = asset_type_map[syminfo['secType']]
|
||||||
|
|
||||||
|
if atype in {
|
||||||
|
'forex',
|
||||||
|
'index',
|
||||||
|
'commodity',
|
||||||
|
}:
|
||||||
|
syminfo['no_vlm'] = True
|
||||||
|
|
||||||
# for stocks it seems TWS reports too small a tick size
|
# for stocks it seems TWS reports too small a tick size
|
||||||
# such that you can't submit orders with that granularity?
|
# such that you can't submit orders with that granularity?
|
||||||
min_tick = 0.01 if atype == 'stock' else 0
|
min_tick = 0.01 if atype == 'stock' else 0
|
||||||
|
@ -709,9 +790,9 @@ async def stream_quotes(
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
return init_msgs
|
return init_msgs, syminfo
|
||||||
|
|
||||||
init_msgs = mk_init_msgs()
|
init_msgs, syminfo = mk_init_msgs()
|
||||||
|
|
||||||
# TODO: we should instead spawn a task that waits on a feed to start
|
# TODO: we should instead spawn a task that waits on a feed to start
|
||||||
# and let it wait indefinitely..instead of this hard coded stuff.
|
# and let it wait indefinitely..instead of this hard coded stuff.
|
||||||
|
@ -720,7 +801,14 @@ async def stream_quotes(
|
||||||
|
|
||||||
# it might be outside regular trading hours so see if we can at
|
# it might be outside regular trading hours so see if we can at
|
||||||
# least grab history.
|
# least grab history.
|
||||||
if isnan(first_ticker.last):
|
if (
|
||||||
|
isnan(first_ticker.last)
|
||||||
|
and type(first_ticker.contract) not in (
|
||||||
|
ibis.Commodity,
|
||||||
|
ibis.Forex,
|
||||||
|
ibis.Crypto,
|
||||||
|
)
|
||||||
|
):
|
||||||
task_status.started((init_msgs, first_quote))
|
task_status.started((init_msgs, first_quote))
|
||||||
|
|
||||||
# it's not really live but this will unblock
|
# it's not really live but this will unblock
|
||||||
|
@ -731,41 +819,77 @@ async def stream_quotes(
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
return # we never expect feed to come up?
|
return # we never expect feed to come up?
|
||||||
|
|
||||||
async with open_aio_quote_stream(
|
cs: Optional[trio.CancelScope] = None
|
||||||
|
startup: bool = True
|
||||||
|
while (
|
||||||
|
startup
|
||||||
|
or cs.cancel_called
|
||||||
|
):
|
||||||
|
with trio.CancelScope() as cs:
|
||||||
|
async with (
|
||||||
|
trio.open_nursery() as nurse,
|
||||||
|
open_aio_quote_stream(
|
||||||
symbol=sym,
|
symbol=sym,
|
||||||
contract=con,
|
contract=con,
|
||||||
) as stream:
|
) as stream,
|
||||||
|
):
|
||||||
# ugh, clear ticks since we've consumed them
|
# ugh, clear ticks since we've consumed them
|
||||||
# (ahem, ib_insync is stateful trash)
|
# (ahem, ib_insync is stateful trash)
|
||||||
first_ticker.ticks = []
|
first_ticker.ticks = []
|
||||||
|
|
||||||
|
# only on first entry at feed boot up
|
||||||
|
if startup:
|
||||||
|
startup = False
|
||||||
task_status.started((init_msgs, first_quote))
|
task_status.started((init_msgs, first_quote))
|
||||||
|
|
||||||
|
# start a stream restarter task which monitors the
|
||||||
|
# data feed event.
|
||||||
|
async def reset_on_feed():
|
||||||
|
|
||||||
|
# TODO: this seems to be surpressed from the
|
||||||
|
# traceback in ``tractor``?
|
||||||
|
# assert 0
|
||||||
|
|
||||||
|
rt_ev = proxy.status_event(
|
||||||
|
'Market data farm connection is OK:usfarm'
|
||||||
|
)
|
||||||
|
await rt_ev.wait()
|
||||||
|
cs.cancel() # cancel called should now be set
|
||||||
|
|
||||||
|
nurse.start_soon(reset_on_feed)
|
||||||
|
|
||||||
async with aclosing(stream):
|
async with aclosing(stream):
|
||||||
if type(first_ticker.contract) not in (
|
if syminfo.get('no_vlm', False):
|
||||||
ibis.Commodity,
|
|
||||||
ibis.Forex
|
# generally speaking these feeds don't
|
||||||
):
|
# include vlm data.
|
||||||
# wait for real volume on feed (trading might be closed)
|
atype = syminfo['asset_type']
|
||||||
|
log.info(
|
||||||
|
f'No-vlm {sym}@{atype}, skipping quote poll'
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# wait for real volume on feed (trading might be
|
||||||
|
# closed)
|
||||||
while True:
|
while True:
|
||||||
ticker = await stream.receive()
|
ticker = await stream.receive()
|
||||||
|
|
||||||
# for a real volume contract we rait for the first
|
# for a real volume contract we rait for
|
||||||
# "real" trade to take place
|
# the first "real" trade to take place
|
||||||
if (
|
if (
|
||||||
# not calc_price
|
# not calc_price
|
||||||
# and not ticker.rtTime
|
# and not ticker.rtTime
|
||||||
not ticker.rtTime
|
not ticker.rtTime
|
||||||
):
|
):
|
||||||
# spin consuming tickers until we get a real
|
# spin consuming tickers until we
|
||||||
# market datum
|
# get a real market datum
|
||||||
log.debug(f"New unsent ticker: {ticker}")
|
log.debug(f"New unsent ticker: {ticker}")
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
log.debug("Received first real volume tick")
|
log.debug("Received first volume tick")
|
||||||
# ugh, clear ticks since we've consumed them
|
# ugh, clear ticks since we've
|
||||||
# (ahem, ib_insync is truly stateful trash)
|
# consumed them (ahem, ib_insync is
|
||||||
|
# truly stateful trash)
|
||||||
ticker.ticks = []
|
ticker.ticks = []
|
||||||
|
|
||||||
# XXX: this works because we don't use
|
# XXX: this works because we don't use
|
||||||
|
@ -781,7 +905,9 @@ async def stream_quotes(
|
||||||
# last = time.time()
|
# last = time.time()
|
||||||
async for ticker in stream:
|
async for ticker in stream:
|
||||||
quote = normalize(ticker)
|
quote = normalize(ticker)
|
||||||
await send_chan.send({quote['fqsn']: quote})
|
fqsn = quote['fqsn']
|
||||||
|
# print(f'sending {fqsn}:\n{quote}')
|
||||||
|
await send_chan.send({fqsn: quote})
|
||||||
|
|
||||||
# ugh, clear ticks since we've consumed them
|
# ugh, clear ticks since we've consumed them
|
||||||
ticker.ticks = []
|
ticker.ticks = []
|
||||||
|
@ -805,6 +931,9 @@ async def data_reset_hack(
|
||||||
successful.
|
successful.
|
||||||
- other OS support?
|
- other OS support?
|
||||||
- integration with ``ib-gw`` run in docker + Xorg?
|
- integration with ``ib-gw`` run in docker + Xorg?
|
||||||
|
- is it possible to offer a local server that can be accessed by
|
||||||
|
a client? Would be sure be handy for running native java blobs
|
||||||
|
that need to be wrangle.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -835,7 +964,10 @@ async def data_reset_hack(
|
||||||
client.mouse.click()
|
client.mouse.click()
|
||||||
client.keyboard.press('Ctrl', 'Alt', key) # keys are stacked
|
client.keyboard.press('Ctrl', 'Alt', key) # keys are stacked
|
||||||
|
|
||||||
|
try:
|
||||||
await tractor.to_asyncio.run_task(vnc_click_hack)
|
await tractor.to_asyncio.run_task(vnc_click_hack)
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
|
||||||
# we don't really need the ``xdotool`` approach any more B)
|
# we don't really need the ``xdotool`` approach any more B)
|
||||||
return True
|
return True
|
||||||
|
@ -850,15 +982,31 @@ async def open_symbol_search(
|
||||||
# TODO: load user defined symbol set locally for fast search?
|
# TODO: load user defined symbol set locally for fast search?
|
||||||
await ctx.started({})
|
await ctx.started({})
|
||||||
|
|
||||||
async with open_data_client() as proxy:
|
async with (
|
||||||
|
open_client_proxies() as (proxies, clients),
|
||||||
|
open_data_client() as data_proxy,
|
||||||
|
):
|
||||||
async with ctx.open_stream() as stream:
|
async with ctx.open_stream() as stream:
|
||||||
|
|
||||||
last = time.time()
|
# select a non-history client for symbol search to lighten
|
||||||
|
# the load in the main data node.
|
||||||
|
proxy = data_proxy
|
||||||
|
for name, proxy in proxies.items():
|
||||||
|
if proxy is data_proxy:
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
|
||||||
|
ib_client = proxy._aio_ns.ib
|
||||||
|
log.info(f'Using {ib_client} for symbol search')
|
||||||
|
|
||||||
|
last = time.time()
|
||||||
async for pattern in stream:
|
async for pattern in stream:
|
||||||
log.debug(f'received {pattern}')
|
log.info(f'received {pattern}')
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|
||||||
|
# this causes tractor hang...
|
||||||
|
# assert 0
|
||||||
|
|
||||||
assert pattern, 'IB can not accept blank search pattern'
|
assert pattern, 'IB can not accept blank search pattern'
|
||||||
|
|
||||||
# throttle search requests to no faster then 1Hz
|
# throttle search requests to no faster then 1Hz
|
||||||
|
@ -871,7 +1019,14 @@ async def open_symbol_search(
|
||||||
except trio.WouldBlock:
|
except trio.WouldBlock:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not pattern or pattern.isspace():
|
if (
|
||||||
|
not pattern
|
||||||
|
or pattern.isspace()
|
||||||
|
|
||||||
|
# XXX: not sure if this is a bad assumption but it
|
||||||
|
# seems to make search snappier?
|
||||||
|
or len(pattern) < 1
|
||||||
|
):
|
||||||
log.warning('empty pattern received, skipping..')
|
log.warning('empty pattern received, skipping..')
|
||||||
|
|
||||||
# TODO: *BUG* if nothing is returned here the client
|
# TODO: *BUG* if nothing is returned here the client
|
||||||
|
@ -886,7 +1041,7 @@ async def open_symbol_search(
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
log.debug(f'searching for {pattern}')
|
log.info(f'searching for {pattern}')
|
||||||
|
|
||||||
last = time.time()
|
last = time.time()
|
||||||
|
|
||||||
|
@ -895,8 +1050,16 @@ async def open_symbol_search(
|
||||||
stock_results = []
|
stock_results = []
|
||||||
|
|
||||||
async def stash_results(target: Awaitable[list]):
|
async def stash_results(target: Awaitable[list]):
|
||||||
stock_results.extend(await target)
|
try:
|
||||||
|
results = await target
|
||||||
|
except tractor.trionics.Lagged:
|
||||||
|
print("IB SYM-SEARCH OVERRUN?!?")
|
||||||
|
return
|
||||||
|
|
||||||
|
stock_results.extend(results)
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
with trio.move_on_after(3) as cs:
|
||||||
async with trio.open_nursery() as sn:
|
async with trio.open_nursery() as sn:
|
||||||
sn.start_soon(
|
sn.start_soon(
|
||||||
stash_results,
|
stash_results,
|
||||||
|
@ -909,17 +1072,26 @@ async def open_symbol_search(
|
||||||
# trigger async request
|
# trigger async request
|
||||||
await trio.sleep(0)
|
await trio.sleep(0)
|
||||||
|
|
||||||
# match against our ad-hoc set immediately
|
if cs.cancelled_caught:
|
||||||
adhoc_matches = fuzzy.extractBests(
|
log.warning(
|
||||||
pattern,
|
f'Search timeout? {proxy._aio_ns.ib.client}'
|
||||||
list(_adhoc_futes_set),
|
|
||||||
score_cutoff=90,
|
|
||||||
)
|
)
|
||||||
log.info(f'fuzzy matched adhocs: {adhoc_matches}')
|
continue
|
||||||
adhoc_match_results = {}
|
else:
|
||||||
if adhoc_matches:
|
break
|
||||||
# TODO: do we need to pull contract details?
|
|
||||||
adhoc_match_results = {i[0]: {} for i in adhoc_matches}
|
# # match against our ad-hoc set immediately
|
||||||
|
# adhoc_matches = fuzzy.extractBests(
|
||||||
|
# pattern,
|
||||||
|
# list(_adhoc_futes_set),
|
||||||
|
# score_cutoff=90,
|
||||||
|
# )
|
||||||
|
# log.info(f'fuzzy matched adhocs: {adhoc_matches}')
|
||||||
|
# adhoc_match_results = {}
|
||||||
|
# if adhoc_matches:
|
||||||
|
# # TODO: do we need to pull contract details?
|
||||||
|
# adhoc_match_results = {i[0]: {} for i in
|
||||||
|
# adhoc_matches}
|
||||||
|
|
||||||
log.debug(f'fuzzy matching stocks {stock_results}')
|
log.debug(f'fuzzy matching stocks {stock_results}')
|
||||||
stock_matches = fuzzy.extractBests(
|
stock_matches = fuzzy.extractBests(
|
||||||
|
@ -928,7 +1100,8 @@ async def open_symbol_search(
|
||||||
score_cutoff=50,
|
score_cutoff=50,
|
||||||
)
|
)
|
||||||
|
|
||||||
matches = adhoc_match_results | {
|
# matches = adhoc_match_results | {
|
||||||
|
matches = {
|
||||||
item[0]: {} for item in stock_matches
|
item[0]: {} for item in stock_matches
|
||||||
}
|
}
|
||||||
# TODO: we used to deliver contract details
|
# TODO: we used to deliver contract details
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,64 @@
|
||||||
|
``kraken`` backend
|
||||||
|
------------------
|
||||||
|
though they don't have the most liquidity of all the cexes they sure are
|
||||||
|
accommodating to those of us who appreciate a little ``xmr``.
|
||||||
|
|
||||||
|
status
|
||||||
|
******
|
||||||
|
current support is *production grade* and both real-time data and order
|
||||||
|
management should be correct and fast. this backend is used by core devs
|
||||||
|
for live trading.
|
||||||
|
|
||||||
|
|
||||||
|
config
|
||||||
|
******
|
||||||
|
In order to get order mode support your ``brokers.toml``
|
||||||
|
needs to have something like the following:
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[kraken]
|
||||||
|
accounts.spot = 'spot'
|
||||||
|
key_descr = "spot"
|
||||||
|
api_key = "69696969696969696696969696969696969696969696969696969696"
|
||||||
|
secret = "BOOBSBOOBSBOOBSBOOBSBOOBSSMBZ69696969696969669969696969696"
|
||||||
|
|
||||||
|
|
||||||
|
If everything works correctly you should see any current positions
|
||||||
|
loaded in the pps pane on chart load and you should also be able to
|
||||||
|
check your trade records in the file::
|
||||||
|
|
||||||
|
<pikerk_conf_dir>/ledgers/trades_kraken_spot.toml
|
||||||
|
|
||||||
|
|
||||||
|
An example ledger file will have entries written verbatim from the
|
||||||
|
trade events schema:
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[TFJBKK-SMBZS-VJ4UWS]
|
||||||
|
ordertxid = "SMBZSA-7CNQU-3HWLNJ"
|
||||||
|
postxid = "SMBZSE-M7IF5-CFI7LT"
|
||||||
|
pair = "XXMRZEUR"
|
||||||
|
time = 1655691993.4133966
|
||||||
|
type = "buy"
|
||||||
|
ordertype = "limit"
|
||||||
|
price = "103.97000000"
|
||||||
|
cost = "499.99999977"
|
||||||
|
fee = "0.80000000"
|
||||||
|
vol = "4.80907954"
|
||||||
|
margin = "0.00000000"
|
||||||
|
misc = ""
|
||||||
|
|
||||||
|
|
||||||
|
your ``pps.toml`` file will have position entries like,
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[kraken.spot."xmreur.kraken"]
|
||||||
|
size = 4.80907954
|
||||||
|
ppu = 103.97000000
|
||||||
|
bsuid = "XXMRZEUR"
|
||||||
|
clears = [
|
||||||
|
{ tid = "TFJBKK-SMBZS-VJ4UWS", cost = 0.8, price = 103.97, size = 4.80907954, dt = "2022-05-20T02:26:33.413397+00:00" },
|
||||||
|
]
|
|
@ -0,0 +1,61 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Kraken backend.
|
||||||
|
|
||||||
|
Sub-modules within break into the core functionalities:
|
||||||
|
|
||||||
|
- ``broker.py`` part for orders / trading endpoints
|
||||||
|
- ``feed.py`` for real-time data feed endpoints
|
||||||
|
- ``api.py`` for the core API machinery which is ``trio``-ized
|
||||||
|
wrapping around ``ib_insync``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
from piker.log import get_logger
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
from .api import (
|
||||||
|
get_client,
|
||||||
|
)
|
||||||
|
from .feed import (
|
||||||
|
open_history_client,
|
||||||
|
open_symbol_search,
|
||||||
|
stream_quotes,
|
||||||
|
)
|
||||||
|
from .broker import (
|
||||||
|
trades_dialogue,
|
||||||
|
norm_trade_records,
|
||||||
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'get_client',
|
||||||
|
'trades_dialogue',
|
||||||
|
'open_history_client',
|
||||||
|
'open_symbol_search',
|
||||||
|
'stream_quotes',
|
||||||
|
'norm_trade_records',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# tractor RPC enable arg
|
||||||
|
__enable_modules__: list[str] = [
|
||||||
|
'api',
|
||||||
|
'feed',
|
||||||
|
'broker',
|
||||||
|
]
|
|
@ -0,0 +1,536 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Kraken web API wrapping.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
|
from datetime import datetime
|
||||||
|
import itertools
|
||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Optional,
|
||||||
|
Union,
|
||||||
|
)
|
||||||
|
import time
|
||||||
|
|
||||||
|
from bidict import bidict
|
||||||
|
import pendulum
|
||||||
|
import asks
|
||||||
|
from fuzzywuzzy import process as fuzzy
|
||||||
|
import numpy as np
|
||||||
|
import urllib.parse
|
||||||
|
import hashlib
|
||||||
|
import hmac
|
||||||
|
import base64
|
||||||
|
import trio
|
||||||
|
|
||||||
|
from piker import config
|
||||||
|
from piker.brokers._util import (
|
||||||
|
resproc,
|
||||||
|
SymbolNotFound,
|
||||||
|
BrokerError,
|
||||||
|
DataThrottle,
|
||||||
|
)
|
||||||
|
from piker.pp import Transaction
|
||||||
|
from . import log
|
||||||
|
|
||||||
|
# <uri>/<version>/
|
||||||
|
_url = 'https://api.kraken.com/0'
|
||||||
|
|
||||||
|
|
||||||
|
# Broker specific ohlc schema which includes a vwap field
|
||||||
|
_ohlc_dtype = [
|
||||||
|
('index', int),
|
||||||
|
('time', int),
|
||||||
|
('open', float),
|
||||||
|
('high', float),
|
||||||
|
('low', float),
|
||||||
|
('close', float),
|
||||||
|
('volume', float),
|
||||||
|
('count', int),
|
||||||
|
('bar_wap', float),
|
||||||
|
]
|
||||||
|
|
||||||
|
# UI components allow this to be declared such that additional
|
||||||
|
# (historical) fields can be exposed.
|
||||||
|
ohlc_dtype = np.dtype(_ohlc_dtype)
|
||||||
|
|
||||||
|
_show_wap_in_history = True
|
||||||
|
_symbol_info_translation: dict[str, str] = {
|
||||||
|
'tick_decimals': 'pair_decimals',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_config() -> dict[str, Any]:
|
||||||
|
|
||||||
|
conf, path = config.load()
|
||||||
|
section = conf.get('kraken')
|
||||||
|
|
||||||
|
if section is None:
|
||||||
|
log.warning(f'No config section found for kraken in {path}')
|
||||||
|
return {}
|
||||||
|
|
||||||
|
return section
|
||||||
|
|
||||||
|
|
||||||
|
def get_kraken_signature(
|
||||||
|
urlpath: str,
|
||||||
|
data: dict[str, Any],
|
||||||
|
secret: str
|
||||||
|
) -> str:
|
||||||
|
postdata = urllib.parse.urlencode(data)
|
||||||
|
encoded = (str(data['nonce']) + postdata).encode()
|
||||||
|
message = urlpath.encode() + hashlib.sha256(encoded).digest()
|
||||||
|
|
||||||
|
mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
|
||||||
|
sigdigest = base64.b64encode(mac.digest())
|
||||||
|
return sigdigest.decode()
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidKey(ValueError):
|
||||||
|
'''
|
||||||
|
EAPI:Invalid key
|
||||||
|
This error is returned when the API key used for the call is
|
||||||
|
either expired or disabled, please review the API key in your
|
||||||
|
Settings -> API tab of account management or generate a new one
|
||||||
|
and update your application.
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class Client:
|
||||||
|
|
||||||
|
# global symbol normalization table
|
||||||
|
_ntable: dict[str, str] = {}
|
||||||
|
_atable: bidict[str, str] = bidict()
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: dict[str, str],
|
||||||
|
name: str = '',
|
||||||
|
api_key: str = '',
|
||||||
|
secret: str = ''
|
||||||
|
) -> None:
|
||||||
|
self._sesh = asks.Session(connections=4)
|
||||||
|
self._sesh.base_location = _url
|
||||||
|
self._sesh.headers.update({
|
||||||
|
'User-Agent':
|
||||||
|
'krakenex/2.1.0 (+https://github.com/veox/python3-krakenex)'
|
||||||
|
})
|
||||||
|
self.conf: dict[str, str] = config
|
||||||
|
self._pairs: list[str] = []
|
||||||
|
self._name = name
|
||||||
|
self._api_key = api_key
|
||||||
|
self._secret = secret
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pairs(self) -> dict[str, Any]:
|
||||||
|
if self._pairs is None:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Make sure to run `cache_symbols()` on startup!"
|
||||||
|
)
|
||||||
|
# retreive and cache all symbols
|
||||||
|
|
||||||
|
return self._pairs
|
||||||
|
|
||||||
|
async def _public(
|
||||||
|
self,
|
||||||
|
method: str,
|
||||||
|
data: dict,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
resp = await self._sesh.post(
|
||||||
|
path=f'/public/{method}',
|
||||||
|
json=data,
|
||||||
|
timeout=float('inf')
|
||||||
|
)
|
||||||
|
return resproc(resp, log)
|
||||||
|
|
||||||
|
async def _private(
|
||||||
|
self,
|
||||||
|
method: str,
|
||||||
|
data: dict,
|
||||||
|
uri_path: str
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
headers = {
|
||||||
|
'Content-Type':
|
||||||
|
'application/x-www-form-urlencoded',
|
||||||
|
'API-Key':
|
||||||
|
self._api_key,
|
||||||
|
'API-Sign':
|
||||||
|
get_kraken_signature(uri_path, data, self._secret)
|
||||||
|
}
|
||||||
|
resp = await self._sesh.post(
|
||||||
|
path=f'/private/{method}',
|
||||||
|
data=data,
|
||||||
|
headers=headers,
|
||||||
|
timeout=float('inf')
|
||||||
|
)
|
||||||
|
return resproc(resp, log)
|
||||||
|
|
||||||
|
async def endpoint(
|
||||||
|
self,
|
||||||
|
method: str,
|
||||||
|
data: dict[str, Any]
|
||||||
|
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
uri_path = f'/0/private/{method}'
|
||||||
|
data['nonce'] = str(int(1000*time.time()))
|
||||||
|
return await self._private(method, data, uri_path)
|
||||||
|
|
||||||
|
async def get_balances(
|
||||||
|
self,
|
||||||
|
) -> dict[str, float]:
|
||||||
|
'''
|
||||||
|
Return the set of asset balances for this account
|
||||||
|
by symbol.
|
||||||
|
|
||||||
|
'''
|
||||||
|
resp = await self.endpoint(
|
||||||
|
'Balance',
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
by_bsuid = resp['result']
|
||||||
|
return {
|
||||||
|
self._atable[sym].lower(): float(bal)
|
||||||
|
for sym, bal in by_bsuid.items()
|
||||||
|
}
|
||||||
|
|
||||||
|
async def get_assets(self) -> dict[str, dict]:
|
||||||
|
resp = await self._public('Assets', {})
|
||||||
|
return resp['result']
|
||||||
|
|
||||||
|
async def cache_assets(self) -> None:
|
||||||
|
assets = self.assets = await self.get_assets()
|
||||||
|
for bsuid, info in assets.items():
|
||||||
|
self._atable[bsuid] = info['altname']
|
||||||
|
|
||||||
|
async def get_trades(
|
||||||
|
self,
|
||||||
|
fetch_limit: int = 10,
|
||||||
|
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
'''
|
||||||
|
Get the trades (aka cleared orders) history from the rest endpoint:
|
||||||
|
https://docs.kraken.com/rest/#operation/getTradeHistory
|
||||||
|
|
||||||
|
'''
|
||||||
|
ofs = 0
|
||||||
|
trades_by_id: dict[str, Any] = {}
|
||||||
|
|
||||||
|
for i in itertools.count():
|
||||||
|
if i >= fetch_limit:
|
||||||
|
break
|
||||||
|
|
||||||
|
# increment 'ofs' pagination offset
|
||||||
|
ofs = i*50
|
||||||
|
|
||||||
|
resp = await self.endpoint(
|
||||||
|
'TradesHistory',
|
||||||
|
{'ofs': ofs},
|
||||||
|
)
|
||||||
|
by_id = resp['result']['trades']
|
||||||
|
trades_by_id.update(by_id)
|
||||||
|
|
||||||
|
# we can get up to 50 results per query
|
||||||
|
if (
|
||||||
|
len(by_id) < 50
|
||||||
|
):
|
||||||
|
err = resp.get('error')
|
||||||
|
if err:
|
||||||
|
raise BrokerError(err)
|
||||||
|
|
||||||
|
# we know we received the max amount of
|
||||||
|
# trade results so there may be more history.
|
||||||
|
# catch the end of the trades
|
||||||
|
count = resp['result']['count']
|
||||||
|
break
|
||||||
|
|
||||||
|
# santity check on update
|
||||||
|
assert count == len(trades_by_id.values())
|
||||||
|
return trades_by_id
|
||||||
|
|
||||||
|
async def get_xfers(
|
||||||
|
self,
|
||||||
|
asset: str,
|
||||||
|
src_asset: str = '',
|
||||||
|
|
||||||
|
) -> dict[str, Transaction]:
|
||||||
|
'''
|
||||||
|
Get asset balance transfer transactions.
|
||||||
|
|
||||||
|
Currently only withdrawals are supported.
|
||||||
|
|
||||||
|
'''
|
||||||
|
xfers: list[dict] = (await self.endpoint(
|
||||||
|
'WithdrawStatus',
|
||||||
|
{'asset': asset},
|
||||||
|
))['result']
|
||||||
|
|
||||||
|
# eg. resp schema:
|
||||||
|
# 'result': [{'method': 'Bitcoin', 'aclass': 'currency', 'asset':
|
||||||
|
# 'XXBT', 'refid': 'AGBJRMB-JHD2M4-NDI3NR', 'txid':
|
||||||
|
# 'b95d66d3bb6fd76cbccb93f7639f99a505cb20752c62ea0acc093a0e46547c44',
|
||||||
|
# 'info': 'bc1qc8enqjekwppmw3g80p56z5ns7ze3wraqk5rl9z',
|
||||||
|
# 'amount': '0.00300726', 'fee': '0.00001000', 'time':
|
||||||
|
# 1658347714, 'status': 'Success'}]}
|
||||||
|
|
||||||
|
trans: dict[str, Transaction] = {}
|
||||||
|
for entry in xfers:
|
||||||
|
# look up the normalized name
|
||||||
|
asset = self._atable[entry['asset']].lower()
|
||||||
|
|
||||||
|
# XXX: this is in the asset units (likely) so it isn't
|
||||||
|
# quite the same as a commisions cost necessarily..)
|
||||||
|
cost = float(entry['fee'])
|
||||||
|
|
||||||
|
tran = Transaction(
|
||||||
|
fqsn=asset + '.kraken',
|
||||||
|
tid=entry['txid'],
|
||||||
|
dt=pendulum.from_timestamp(entry['time']),
|
||||||
|
bsuid=f'{asset}{src_asset}',
|
||||||
|
size=-1*(
|
||||||
|
float(entry['amount'])
|
||||||
|
+
|
||||||
|
cost
|
||||||
|
),
|
||||||
|
# since this will be treated as a "sell" it
|
||||||
|
# shouldn't be needed to compute the be price.
|
||||||
|
price='NaN',
|
||||||
|
|
||||||
|
# XXX: see note above
|
||||||
|
cost=0,
|
||||||
|
)
|
||||||
|
trans[tran.tid] = tran
|
||||||
|
|
||||||
|
return trans
|
||||||
|
|
||||||
|
async def submit_limit(
|
||||||
|
self,
|
||||||
|
symbol: str,
|
||||||
|
price: float,
|
||||||
|
action: str,
|
||||||
|
size: float,
|
||||||
|
reqid: str = None,
|
||||||
|
validate: bool = False # set True test call without a real submission
|
||||||
|
|
||||||
|
) -> dict:
|
||||||
|
'''
|
||||||
|
Place an order and return integer request id provided by client.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# Build common data dict for common keys from both endpoints
|
||||||
|
data = {
|
||||||
|
"pair": symbol,
|
||||||
|
"price": str(price),
|
||||||
|
"validate": validate
|
||||||
|
}
|
||||||
|
if reqid is None:
|
||||||
|
# Build order data for kraken api
|
||||||
|
data |= {
|
||||||
|
"ordertype": "limit",
|
||||||
|
"type": action,
|
||||||
|
"volume": str(size),
|
||||||
|
}
|
||||||
|
return await self.endpoint('AddOrder', data)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Edit order data for kraken api
|
||||||
|
data["txid"] = reqid
|
||||||
|
return await self.endpoint('EditOrder', data)
|
||||||
|
|
||||||
|
async def submit_cancel(
|
||||||
|
self,
|
||||||
|
reqid: str,
|
||||||
|
) -> dict:
|
||||||
|
'''
|
||||||
|
Send cancel request for order id ``reqid``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# txid is a transaction id given by kraken
|
||||||
|
return await self.endpoint('CancelOrder', {"txid": reqid})
|
||||||
|
|
||||||
|
async def symbol_info(
|
||||||
|
self,
|
||||||
|
pair: Optional[str] = None,
|
||||||
|
|
||||||
|
) -> dict[str, dict[str, str]]:
|
||||||
|
|
||||||
|
if pair is not None:
|
||||||
|
pairs = {'pair': pair}
|
||||||
|
else:
|
||||||
|
pairs = None # get all pairs
|
||||||
|
|
||||||
|
resp = await self._public('AssetPairs', pairs)
|
||||||
|
err = resp['error']
|
||||||
|
if err:
|
||||||
|
symbolname = pairs['pair'] if pair else None
|
||||||
|
raise SymbolNotFound(f'{symbolname}.kraken')
|
||||||
|
|
||||||
|
pairs = resp['result']
|
||||||
|
|
||||||
|
if pair is not None:
|
||||||
|
_, data = next(iter(pairs.items()))
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
return pairs
|
||||||
|
|
||||||
|
async def cache_symbols(
|
||||||
|
self,
|
||||||
|
) -> dict:
|
||||||
|
if not self._pairs:
|
||||||
|
self._pairs = await self.symbol_info()
|
||||||
|
|
||||||
|
ntable = {}
|
||||||
|
for restapikey, info in self._pairs.items():
|
||||||
|
ntable[restapikey] = ntable[info['wsname']] = info['altname']
|
||||||
|
|
||||||
|
self._ntable.update(ntable)
|
||||||
|
|
||||||
|
return self._pairs
|
||||||
|
|
||||||
|
async def search_symbols(
|
||||||
|
self,
|
||||||
|
pattern: str,
|
||||||
|
limit: int = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
if self._pairs is not None:
|
||||||
|
data = self._pairs
|
||||||
|
else:
|
||||||
|
data = await self.symbol_info()
|
||||||
|
|
||||||
|
matches = fuzzy.extractBests(
|
||||||
|
pattern,
|
||||||
|
data,
|
||||||
|
score_cutoff=50,
|
||||||
|
)
|
||||||
|
# repack in dict form
|
||||||
|
return {item[0]['altname']: item[0] for item in matches}
|
||||||
|
|
||||||
|
async def bars(
|
||||||
|
self,
|
||||||
|
symbol: str = 'XBTUSD',
|
||||||
|
|
||||||
|
# UTC 2017-07-02 12:53:20
|
||||||
|
since: Optional[Union[int, datetime]] = None,
|
||||||
|
count: int = 720, # <- max allowed per query
|
||||||
|
as_np: bool = True,
|
||||||
|
|
||||||
|
) -> dict:
|
||||||
|
|
||||||
|
if since is None:
|
||||||
|
since = pendulum.now('UTC').start_of('minute').subtract(
|
||||||
|
minutes=count).timestamp()
|
||||||
|
|
||||||
|
elif isinstance(since, int):
|
||||||
|
since = pendulum.from_timestamp(since).timestamp()
|
||||||
|
|
||||||
|
else: # presumably a pendulum datetime
|
||||||
|
since = since.timestamp()
|
||||||
|
|
||||||
|
# UTC 2017-07-02 12:53:20 is oldest seconds value
|
||||||
|
since = str(max(1499000000, int(since)))
|
||||||
|
json = await self._public(
|
||||||
|
'OHLC',
|
||||||
|
data={
|
||||||
|
'pair': symbol,
|
||||||
|
'since': since,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
res = json['result']
|
||||||
|
res.pop('last')
|
||||||
|
bars = next(iter(res.values()))
|
||||||
|
|
||||||
|
new_bars = []
|
||||||
|
|
||||||
|
first = bars[0]
|
||||||
|
last_nz_vwap = first[-3]
|
||||||
|
if last_nz_vwap == 0:
|
||||||
|
# use close if vwap is zero
|
||||||
|
last_nz_vwap = first[-4]
|
||||||
|
|
||||||
|
# convert all fields to native types
|
||||||
|
for i, bar in enumerate(bars):
|
||||||
|
# normalize weird zero-ed vwap values..cmon kraken..
|
||||||
|
# indicates vwap didn't change since last bar
|
||||||
|
vwap = float(bar.pop(-3))
|
||||||
|
if vwap != 0:
|
||||||
|
last_nz_vwap = vwap
|
||||||
|
if vwap == 0:
|
||||||
|
vwap = last_nz_vwap
|
||||||
|
|
||||||
|
# re-insert vwap as the last of the fields
|
||||||
|
bar.append(vwap)
|
||||||
|
|
||||||
|
new_bars.append(
|
||||||
|
(i,) + tuple(
|
||||||
|
ftype(bar[j]) for j, (name, ftype) in enumerate(
|
||||||
|
_ohlc_dtype[1:]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
array = np.array(new_bars, dtype=_ohlc_dtype) if as_np else bars
|
||||||
|
return array
|
||||||
|
except KeyError:
|
||||||
|
errmsg = json['error'][0]
|
||||||
|
|
||||||
|
if 'not found' in errmsg:
|
||||||
|
raise SymbolNotFound(errmsg + f': {symbol}')
|
||||||
|
|
||||||
|
elif 'Too many requests' in errmsg:
|
||||||
|
raise DataThrottle(f'{symbol}')
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise BrokerError(errmsg)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def normalize_symbol(
|
||||||
|
cls,
|
||||||
|
ticker: str
|
||||||
|
) -> str:
|
||||||
|
'''
|
||||||
|
Normalize symbol names to to a 3x3 pair from the global
|
||||||
|
definition map which we build out from the data retreived from
|
||||||
|
the 'AssetPairs' endpoint, see methods above.
|
||||||
|
|
||||||
|
'''
|
||||||
|
ticker = cls._ntable[ticker]
|
||||||
|
return ticker.lower()
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def get_client() -> Client:
|
||||||
|
|
||||||
|
conf = get_config()
|
||||||
|
if conf:
|
||||||
|
client = Client(
|
||||||
|
conf,
|
||||||
|
name=conf['key_descr'],
|
||||||
|
api_key=conf['api_key'],
|
||||||
|
secret=conf['secret']
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
client = Client({})
|
||||||
|
|
||||||
|
# at startup, load all symbols, and asset info in
|
||||||
|
# batch requests.
|
||||||
|
async with trio.open_nursery() as nurse:
|
||||||
|
nurse.start_soon(client.cache_assets)
|
||||||
|
await client.cache_symbols()
|
||||||
|
|
||||||
|
yield client
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,500 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Real-time and historical data feed endpoints.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Optional,
|
||||||
|
Callable,
|
||||||
|
)
|
||||||
|
import time
|
||||||
|
|
||||||
|
from async_generator import aclosing
|
||||||
|
from fuzzywuzzy import process as fuzzy
|
||||||
|
import numpy as np
|
||||||
|
import pendulum
|
||||||
|
from trio_typing import TaskStatus
|
||||||
|
import tractor
|
||||||
|
import trio
|
||||||
|
|
||||||
|
from piker._cacheables import open_cached_client
|
||||||
|
from piker.brokers._util import (
|
||||||
|
BrokerError,
|
||||||
|
DataThrottle,
|
||||||
|
DataUnavailable,
|
||||||
|
)
|
||||||
|
from piker.log import get_console_log
|
||||||
|
from piker.data import ShmArray
|
||||||
|
from piker.data.types import Struct
|
||||||
|
from piker.data._web_bs import open_autorecon_ws, NoBsWs
|
||||||
|
from . import log
|
||||||
|
from .api import (
|
||||||
|
Client,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# https://www.kraken.com/features/api#get-tradable-pairs
|
||||||
|
class Pair(Struct):
|
||||||
|
altname: str # alternate pair name
|
||||||
|
wsname: str # WebSocket pair name (if available)
|
||||||
|
aclass_base: str # asset class of base component
|
||||||
|
base: str # asset id of base component
|
||||||
|
aclass_quote: str # asset class of quote component
|
||||||
|
quote: str # asset id of quote component
|
||||||
|
lot: str # volume lot size
|
||||||
|
|
||||||
|
cost_decimals: int
|
||||||
|
costmin: float
|
||||||
|
pair_decimals: int # scaling decimal places for pair
|
||||||
|
lot_decimals: int # scaling decimal places for volume
|
||||||
|
|
||||||
|
# amount to multiply lot volume by to get currency volume
|
||||||
|
lot_multiplier: float
|
||||||
|
|
||||||
|
# array of leverage amounts available when buying
|
||||||
|
leverage_buy: list[int]
|
||||||
|
# array of leverage amounts available when selling
|
||||||
|
leverage_sell: list[int]
|
||||||
|
|
||||||
|
# fee schedule array in [volume, percent fee] tuples
|
||||||
|
fees: list[tuple[int, float]]
|
||||||
|
|
||||||
|
# maker fee schedule array in [volume, percent fee] tuples (if on
|
||||||
|
# maker/taker)
|
||||||
|
fees_maker: list[tuple[int, float]]
|
||||||
|
|
||||||
|
fee_volume_currency: str # volume discount currency
|
||||||
|
margin_call: str # margin call level
|
||||||
|
margin_stop: str # stop-out/liquidation margin level
|
||||||
|
ordermin: float # minimum order volume for pair
|
||||||
|
tick_size: float # min price step size
|
||||||
|
status: str
|
||||||
|
|
||||||
|
short_position_limit: float
|
||||||
|
long_position_limit: float
|
||||||
|
|
||||||
|
|
||||||
|
class OHLC(Struct):
|
||||||
|
'''
|
||||||
|
Description of the flattened OHLC quote format.
|
||||||
|
|
||||||
|
For schema details see:
|
||||||
|
https://docs.kraken.com/websockets/#message-ohlc
|
||||||
|
|
||||||
|
'''
|
||||||
|
chan_id: int # internal kraken id
|
||||||
|
chan_name: str # eg. ohlc-1 (name-interval)
|
||||||
|
pair: str # fx pair
|
||||||
|
time: float # Begin time of interval, in seconds since epoch
|
||||||
|
etime: float # End time of interval, in seconds since epoch
|
||||||
|
open: float # Open price of interval
|
||||||
|
high: float # High price within interval
|
||||||
|
low: float # Low price within interval
|
||||||
|
close: float # Close price of interval
|
||||||
|
vwap: float # Volume weighted average price within interval
|
||||||
|
volume: float # Accumulated volume **within interval**
|
||||||
|
count: int # Number of trades within interval
|
||||||
|
# (sampled) generated tick data
|
||||||
|
ticks: list[Any] = []
|
||||||
|
|
||||||
|
|
||||||
|
async def stream_messages(
|
||||||
|
ws: NoBsWs,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Message stream parser and heartbeat handler.
|
||||||
|
|
||||||
|
Deliver ws subscription messages as well as handle heartbeat logic
|
||||||
|
though a single async generator.
|
||||||
|
|
||||||
|
'''
|
||||||
|
too_slow_count = last_hb = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
with trio.move_on_after(5) as cs:
|
||||||
|
msg = await ws.recv_msg()
|
||||||
|
|
||||||
|
# trigger reconnection if heartbeat is laggy
|
||||||
|
if cs.cancelled_caught:
|
||||||
|
|
||||||
|
too_slow_count += 1
|
||||||
|
|
||||||
|
if too_slow_count > 20:
|
||||||
|
log.warning(
|
||||||
|
"Heartbeat is too slow, resetting ws connection")
|
||||||
|
|
||||||
|
await ws._connect()
|
||||||
|
too_slow_count = 0
|
||||||
|
continue
|
||||||
|
|
||||||
|
match msg:
|
||||||
|
case {'event': 'heartbeat'}:
|
||||||
|
now = time.time()
|
||||||
|
delay = now - last_hb
|
||||||
|
last_hb = now
|
||||||
|
|
||||||
|
# XXX: why tf is this not printing without --tl flag?
|
||||||
|
log.debug(f"Heartbeat after {delay}")
|
||||||
|
# print(f"Heartbeat after {delay}")
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
|
case _:
|
||||||
|
# passthrough sub msgs
|
||||||
|
yield msg
|
||||||
|
|
||||||
|
|
||||||
|
async def process_data_feed_msgs(
|
||||||
|
ws: NoBsWs,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Parse and pack data feed messages.
|
||||||
|
|
||||||
|
'''
|
||||||
|
async for msg in stream_messages(ws):
|
||||||
|
match msg:
|
||||||
|
case {
|
||||||
|
'errorMessage': errmsg
|
||||||
|
}:
|
||||||
|
raise BrokerError(errmsg)
|
||||||
|
|
||||||
|
case {
|
||||||
|
'event': 'subscriptionStatus',
|
||||||
|
} as sub:
|
||||||
|
log.info(
|
||||||
|
'WS subscription is active:\n'
|
||||||
|
f'{sub}'
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
case [
|
||||||
|
chan_id,
|
||||||
|
*payload_array,
|
||||||
|
chan_name,
|
||||||
|
pair
|
||||||
|
]:
|
||||||
|
if 'ohlc' in chan_name:
|
||||||
|
ohlc = OHLC(
|
||||||
|
chan_id,
|
||||||
|
chan_name,
|
||||||
|
pair,
|
||||||
|
*payload_array[0]
|
||||||
|
)
|
||||||
|
ohlc.typecast()
|
||||||
|
yield 'ohlc', ohlc
|
||||||
|
|
||||||
|
elif 'spread' in chan_name:
|
||||||
|
|
||||||
|
bid, ask, ts, bsize, asize = map(
|
||||||
|
float, payload_array[0])
|
||||||
|
|
||||||
|
# TODO: really makes you think IB has a horrible API...
|
||||||
|
quote = {
|
||||||
|
'symbol': pair.replace('/', ''),
|
||||||
|
'ticks': [
|
||||||
|
{'type': 'bid', 'price': bid, 'size': bsize},
|
||||||
|
{'type': 'bsize', 'price': bid, 'size': bsize},
|
||||||
|
|
||||||
|
{'type': 'ask', 'price': ask, 'size': asize},
|
||||||
|
{'type': 'asize', 'price': ask, 'size': asize},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
yield 'l1', quote
|
||||||
|
|
||||||
|
# elif 'book' in msg[-2]:
|
||||||
|
# chan_id, *payload_array, chan_name, pair = msg
|
||||||
|
# print(msg)
|
||||||
|
|
||||||
|
case _:
|
||||||
|
print(f'UNHANDLED MSG: {msg}')
|
||||||
|
# yield msg
|
||||||
|
|
||||||
|
|
||||||
|
def normalize(
|
||||||
|
ohlc: OHLC,
|
||||||
|
|
||||||
|
) -> dict:
|
||||||
|
quote = ohlc.to_dict()
|
||||||
|
quote['broker_ts'] = quote['time']
|
||||||
|
quote['brokerd_ts'] = time.time()
|
||||||
|
quote['symbol'] = quote['pair'] = quote['pair'].replace('/', '')
|
||||||
|
quote['last'] = quote['close']
|
||||||
|
quote['bar_wap'] = ohlc.vwap
|
||||||
|
|
||||||
|
# seriously eh? what's with this non-symmetry everywhere
|
||||||
|
# in subscription systems...
|
||||||
|
# XXX: piker style is always lowercases symbols.
|
||||||
|
topic = quote['pair'].replace('/', '').lower()
|
||||||
|
|
||||||
|
# print(quote)
|
||||||
|
return topic, quote
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_history_client(
|
||||||
|
symbol: str,
|
||||||
|
|
||||||
|
) -> tuple[Callable, int]:
|
||||||
|
|
||||||
|
# TODO implement history getter for the new storage layer.
|
||||||
|
async with open_cached_client('kraken') as client:
|
||||||
|
|
||||||
|
# lol, kraken won't send any more then the "last"
|
||||||
|
# 720 1m bars.. so we have to just ignore further
|
||||||
|
# requests of this type..
|
||||||
|
queries: int = 0
|
||||||
|
|
||||||
|
async def get_ohlc(
|
||||||
|
timeframe: float,
|
||||||
|
end_dt: Optional[datetime] = None,
|
||||||
|
start_dt: Optional[datetime] = None,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
datetime, # start
|
||||||
|
datetime, # end
|
||||||
|
]:
|
||||||
|
|
||||||
|
nonlocal queries
|
||||||
|
if (
|
||||||
|
queries > 0
|
||||||
|
or timeframe != 60
|
||||||
|
):
|
||||||
|
raise DataUnavailable(
|
||||||
|
'Only a single query for 1m bars supported')
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
while count <= 3:
|
||||||
|
try:
|
||||||
|
array = await client.bars(
|
||||||
|
symbol,
|
||||||
|
since=end_dt,
|
||||||
|
)
|
||||||
|
count += 1
|
||||||
|
queries += 1
|
||||||
|
break
|
||||||
|
except DataThrottle:
|
||||||
|
log.warning(f'kraken OHLC throttle for {symbol}')
|
||||||
|
await trio.sleep(1)
|
||||||
|
|
||||||
|
start_dt = pendulum.from_timestamp(array[0]['time'])
|
||||||
|
end_dt = pendulum.from_timestamp(array[-1]['time'])
|
||||||
|
return array, start_dt, end_dt
|
||||||
|
|
||||||
|
yield get_ohlc, {'erlangs': 1, 'rate': 1}
|
||||||
|
|
||||||
|
|
||||||
|
async def stream_quotes(
|
||||||
|
|
||||||
|
send_chan: trio.abc.SendChannel,
|
||||||
|
symbols: list[str],
|
||||||
|
feed_is_live: trio.Event,
|
||||||
|
loglevel: str = None,
|
||||||
|
|
||||||
|
# backend specific
|
||||||
|
sub_type: str = 'ohlc',
|
||||||
|
|
||||||
|
# startup sync
|
||||||
|
task_status: TaskStatus[tuple[dict, dict]] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Subscribe for ohlc stream of quotes for ``pairs``.
|
||||||
|
|
||||||
|
``pairs`` must be formatted <crypto_symbol>/<fiat_symbol>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# XXX: required to propagate ``tractor`` loglevel to piker logging
|
||||||
|
get_console_log(loglevel or tractor.current_actor().loglevel)
|
||||||
|
|
||||||
|
ws_pairs = {}
|
||||||
|
sym_infos = {}
|
||||||
|
|
||||||
|
async with open_cached_client('kraken') as client, send_chan as send_chan:
|
||||||
|
|
||||||
|
# keep client cached for real-time section
|
||||||
|
for sym in symbols:
|
||||||
|
|
||||||
|
# transform to upper since piker style is always lower
|
||||||
|
sym = sym.upper()
|
||||||
|
sym_info = await client.symbol_info(sym)
|
||||||
|
try:
|
||||||
|
si = Pair(**sym_info) # validation
|
||||||
|
except TypeError:
|
||||||
|
fields_diff = set(sym_info) - set(Pair.__struct_fields__)
|
||||||
|
raise TypeError(
|
||||||
|
f'Missing msg fields {fields_diff}'
|
||||||
|
)
|
||||||
|
syminfo = si.to_dict()
|
||||||
|
syminfo['price_tick_size'] = 1 / 10**si.pair_decimals
|
||||||
|
syminfo['lot_tick_size'] = 1 / 10**si.lot_decimals
|
||||||
|
syminfo['asset_type'] = 'crypto'
|
||||||
|
sym_infos[sym] = syminfo
|
||||||
|
ws_pairs[sym] = si.wsname
|
||||||
|
|
||||||
|
symbol = symbols[0].lower()
|
||||||
|
|
||||||
|
init_msgs = {
|
||||||
|
# pass back token, and bool, signalling if we're the writer
|
||||||
|
# and that history has been written
|
||||||
|
symbol: {
|
||||||
|
'symbol_info': sym_infos[sym],
|
||||||
|
'shm_write_opts': {'sum_tick_vml': False},
|
||||||
|
'fqsn': sym,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def subscribe(ws: NoBsWs):
|
||||||
|
|
||||||
|
# XXX: setup subs
|
||||||
|
# https://docs.kraken.com/websockets/#message-subscribe
|
||||||
|
# specific logic for this in kraken's sync client:
|
||||||
|
# https://github.com/krakenfx/kraken-wsclient-py/blob/master/kraken_wsclient_py/kraken_wsclient_py.py#L188
|
||||||
|
ohlc_sub = {
|
||||||
|
'event': 'subscribe',
|
||||||
|
'pair': list(ws_pairs.values()),
|
||||||
|
'subscription': {
|
||||||
|
'name': 'ohlc',
|
||||||
|
'interval': 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
# TODO: we want to eventually allow unsubs which should
|
||||||
|
# be completely fine to request from a separate task
|
||||||
|
# since internally the ws methods appear to be FIFO
|
||||||
|
# locked.
|
||||||
|
await ws.send_msg(ohlc_sub)
|
||||||
|
|
||||||
|
# trade data (aka L1)
|
||||||
|
l1_sub = {
|
||||||
|
'event': 'subscribe',
|
||||||
|
'pair': list(ws_pairs.values()),
|
||||||
|
'subscription': {
|
||||||
|
'name': 'spread',
|
||||||
|
# 'depth': 10}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
# pull a first quote and deliver
|
||||||
|
await ws.send_msg(l1_sub)
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
# unsub from all pairs on teardown
|
||||||
|
if ws.connected():
|
||||||
|
await ws.send_msg({
|
||||||
|
'pair': list(ws_pairs.values()),
|
||||||
|
'event': 'unsubscribe',
|
||||||
|
'subscription': ['ohlc', 'spread'],
|
||||||
|
})
|
||||||
|
|
||||||
|
# XXX: do we need to ack the unsub?
|
||||||
|
# await ws.recv_msg()
|
||||||
|
|
||||||
|
# see the tips on reconnection logic:
|
||||||
|
# https://support.kraken.com/hc/en-us/articles/360044504011-WebSocket-API-unexpected-disconnections-from-market-data-feeds
|
||||||
|
ws: NoBsWs
|
||||||
|
async with (
|
||||||
|
open_autorecon_ws(
|
||||||
|
'wss://ws.kraken.com/',
|
||||||
|
fixture=subscribe,
|
||||||
|
) as ws,
|
||||||
|
aclosing(process_data_feed_msgs(ws)) as msg_gen,
|
||||||
|
):
|
||||||
|
# pull a first quote and deliver
|
||||||
|
typ, ohlc_last = await anext(msg_gen)
|
||||||
|
topic, quote = normalize(ohlc_last)
|
||||||
|
|
||||||
|
task_status.started((init_msgs, quote))
|
||||||
|
|
||||||
|
# lol, only "closes" when they're margin squeezing clients ;P
|
||||||
|
feed_is_live.set()
|
||||||
|
|
||||||
|
# keep start of last interval for volume tracking
|
||||||
|
last_interval_start = ohlc_last.etime
|
||||||
|
|
||||||
|
# start streaming
|
||||||
|
async for typ, ohlc in msg_gen:
|
||||||
|
|
||||||
|
if typ == 'ohlc':
|
||||||
|
|
||||||
|
# TODO: can get rid of all this by using
|
||||||
|
# ``trades`` subscription...
|
||||||
|
|
||||||
|
# generate tick values to match time & sales pane:
|
||||||
|
# https://trade.kraken.com/charts/KRAKEN:BTC-USD?period=1m
|
||||||
|
volume = ohlc.volume
|
||||||
|
|
||||||
|
# new OHLC sample interval
|
||||||
|
if ohlc.etime > last_interval_start:
|
||||||
|
last_interval_start = ohlc.etime
|
||||||
|
tick_volume = volume
|
||||||
|
|
||||||
|
else:
|
||||||
|
# this is the tick volume *within the interval*
|
||||||
|
tick_volume = volume - ohlc_last.volume
|
||||||
|
|
||||||
|
ohlc_last = ohlc
|
||||||
|
last = ohlc.close
|
||||||
|
|
||||||
|
if tick_volume:
|
||||||
|
ohlc.ticks.append({
|
||||||
|
'type': 'trade',
|
||||||
|
'price': last,
|
||||||
|
'size': tick_volume,
|
||||||
|
})
|
||||||
|
|
||||||
|
topic, quote = normalize(ohlc)
|
||||||
|
|
||||||
|
elif typ == 'l1':
|
||||||
|
quote = ohlc
|
||||||
|
topic = quote['symbol'].lower()
|
||||||
|
|
||||||
|
await send_chan.send({topic: quote})
|
||||||
|
|
||||||
|
|
||||||
|
@tractor.context
|
||||||
|
async def open_symbol_search(
|
||||||
|
ctx: tractor.Context,
|
||||||
|
|
||||||
|
) -> Client:
|
||||||
|
async with open_cached_client('kraken') as client:
|
||||||
|
|
||||||
|
# load all symbols locally for fast search
|
||||||
|
cache = await client.cache_symbols()
|
||||||
|
await ctx.started(cache)
|
||||||
|
|
||||||
|
async with ctx.open_stream() as stream:
|
||||||
|
|
||||||
|
async for pattern in stream:
|
||||||
|
|
||||||
|
matches = fuzzy.extractBests(
|
||||||
|
pattern,
|
||||||
|
cache,
|
||||||
|
score_cutoff=50,
|
||||||
|
)
|
||||||
|
# repack in dict form
|
||||||
|
await stream.send(
|
||||||
|
{item[0]['altname']: item[0]
|
||||||
|
for item in matches}
|
||||||
|
)
|
|
@ -18,3 +18,9 @@
|
||||||
Market machinery for order executions, book, management.
|
Market machinery for order executions, book, management.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from ._client import open_ems
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'open_ems',
|
||||||
|
]
|
||||||
|
|
|
@ -22,54 +22,10 @@ from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from bidict import bidict
|
from bidict import bidict
|
||||||
from pydantic import BaseModel, validator
|
|
||||||
|
|
||||||
from ..data._source import Symbol
|
from ..data._source import Symbol
|
||||||
from ._messages import BrokerdPosition, Status
|
from ..data.types import Struct
|
||||||
|
from ..pp import Position
|
||||||
|
|
||||||
class Position(BaseModel):
|
|
||||||
'''
|
|
||||||
Basic pp (personal position) model with attached fills history.
|
|
||||||
|
|
||||||
This type should be IPC wire ready?
|
|
||||||
|
|
||||||
'''
|
|
||||||
symbol: Symbol
|
|
||||||
|
|
||||||
# last size and avg entry price
|
|
||||||
size: float
|
|
||||||
avg_price: float # TODO: contextual pricing
|
|
||||||
|
|
||||||
# ordered record of known constituent trade messages
|
|
||||||
fills: list[Status] = []
|
|
||||||
|
|
||||||
def update_from_msg(
|
|
||||||
self,
|
|
||||||
msg: BrokerdPosition,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
# XXX: better place to do this?
|
|
||||||
symbol = self.symbol
|
|
||||||
|
|
||||||
lot_size_digits = symbol.lot_size_digits
|
|
||||||
avg_price, size = (
|
|
||||||
round(msg['avg_price'], ndigits=symbol.tick_size_digits),
|
|
||||||
round(msg['size'], ndigits=lot_size_digits),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.avg_price = avg_price
|
|
||||||
self.size = size
|
|
||||||
|
|
||||||
@property
|
|
||||||
def dsize(self) -> float:
|
|
||||||
'''
|
|
||||||
The "dollar" size of the pp, normally in trading (fiat) unit
|
|
||||||
terms.
|
|
||||||
|
|
||||||
'''
|
|
||||||
return self.avg_price * self.size
|
|
||||||
|
|
||||||
|
|
||||||
_size_units = bidict({
|
_size_units = bidict({
|
||||||
|
@ -84,34 +40,9 @@ SizeUnit = Enum(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Allocator(BaseModel):
|
class Allocator(Struct):
|
||||||
|
|
||||||
class Config:
|
|
||||||
validate_assignment = True
|
|
||||||
copy_on_model_validation = False
|
|
||||||
arbitrary_types_allowed = True
|
|
||||||
|
|
||||||
# required to get the account validator lookup working?
|
|
||||||
extra = 'allow'
|
|
||||||
underscore_attrs_are_private = False
|
|
||||||
|
|
||||||
symbol: Symbol
|
symbol: Symbol
|
||||||
account: Optional[str] = 'paper'
|
|
||||||
# TODO: for enums this clearly doesn't fucking work, you can't set
|
|
||||||
# a default at startup by passing in a `dict` but yet you can set
|
|
||||||
# that value through assignment..for wtv cucked reason.. honestly, pure
|
|
||||||
# unintuitive garbage.
|
|
||||||
size_unit: str = 'currency'
|
|
||||||
_size_units: dict[str, Optional[str]] = _size_units
|
|
||||||
|
|
||||||
@validator('size_unit', pre=True)
|
|
||||||
def maybe_lookup_key(cls, v):
|
|
||||||
# apply the corresponding enum key for the text "description" value
|
|
||||||
if v not in _size_units:
|
|
||||||
return _size_units.inverse[v]
|
|
||||||
|
|
||||||
assert v in _size_units
|
|
||||||
return v
|
|
||||||
|
|
||||||
# TODO: if we ever want ot support non-uniform entry-slot-proportion
|
# TODO: if we ever want ot support non-uniform entry-slot-proportion
|
||||||
# "sizes"
|
# "sizes"
|
||||||
|
@ -120,6 +51,28 @@ class Allocator(BaseModel):
|
||||||
units_limit: float
|
units_limit: float
|
||||||
currency_limit: float
|
currency_limit: float
|
||||||
slots: int
|
slots: int
|
||||||
|
account: Optional[str] = 'paper'
|
||||||
|
|
||||||
|
_size_units: bidict[str, Optional[str]] = _size_units
|
||||||
|
|
||||||
|
# TODO: for enums this clearly doesn't fucking work, you can't set
|
||||||
|
# a default at startup by passing in a `dict` but yet you can set
|
||||||
|
# that value through assignment..for wtv cucked reason.. honestly, pure
|
||||||
|
# unintuitive garbage.
|
||||||
|
_size_unit: str = 'currency'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def size_unit(self) -> str:
|
||||||
|
return self._size_unit
|
||||||
|
|
||||||
|
@size_unit.setter
|
||||||
|
def size_unit(self, v: str) -> Optional[str]:
|
||||||
|
if v not in _size_units:
|
||||||
|
v = _size_units.inverse[v]
|
||||||
|
|
||||||
|
assert v in _size_units
|
||||||
|
self._size_unit = v
|
||||||
|
return v
|
||||||
|
|
||||||
def step_sizes(
|
def step_sizes(
|
||||||
self,
|
self,
|
||||||
|
@ -140,10 +93,13 @@ class Allocator(BaseModel):
|
||||||
else:
|
else:
|
||||||
return self.units_limit
|
return self.units_limit
|
||||||
|
|
||||||
|
def limit_info(self) -> tuple[str, float]:
|
||||||
|
return self.size_unit, self.limit()
|
||||||
|
|
||||||
def next_order_info(
|
def next_order_info(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
# we only need a startup size for exit calcs, we can the
|
# we only need a startup size for exit calcs, we can then
|
||||||
# determine how large slots should be if the initial pp size was
|
# determine how large slots should be if the initial pp size was
|
||||||
# larger then the current live one, and the live one is smaller
|
# larger then the current live one, and the live one is smaller
|
||||||
# then the initial config settings.
|
# then the initial config settings.
|
||||||
|
@ -173,7 +129,7 @@ class Allocator(BaseModel):
|
||||||
l_sub_pp = self.units_limit - abs_live_size
|
l_sub_pp = self.units_limit - abs_live_size
|
||||||
|
|
||||||
elif size_unit == 'currency':
|
elif size_unit == 'currency':
|
||||||
live_cost_basis = abs_live_size * live_pp.avg_price
|
live_cost_basis = abs_live_size * live_pp.ppu
|
||||||
slot_size = currency_per_slot / price
|
slot_size = currency_per_slot / price
|
||||||
l_sub_pp = (self.currency_limit - live_cost_basis) / price
|
l_sub_pp = (self.currency_limit - live_cost_basis) / price
|
||||||
|
|
||||||
|
@ -184,12 +140,14 @@ class Allocator(BaseModel):
|
||||||
|
|
||||||
# an entry (adding-to or starting a pp)
|
# an entry (adding-to or starting a pp)
|
||||||
if (
|
if (
|
||||||
action == 'buy' and live_size > 0 or
|
|
||||||
action == 'sell' and live_size < 0 or
|
|
||||||
live_size == 0
|
live_size == 0
|
||||||
|
or (action == 'buy' and live_size > 0)
|
||||||
|
or action == 'sell' and live_size < 0
|
||||||
):
|
):
|
||||||
|
order_size = min(
|
||||||
order_size = min(slot_size, l_sub_pp)
|
slot_size,
|
||||||
|
max(l_sub_pp, 0),
|
||||||
|
)
|
||||||
|
|
||||||
# an exit (removing-from or going to net-zero pp)
|
# an exit (removing-from or going to net-zero pp)
|
||||||
else:
|
else:
|
||||||
|
@ -205,7 +163,7 @@ class Allocator(BaseModel):
|
||||||
if size_unit == 'currency':
|
if size_unit == 'currency':
|
||||||
# compute the "projected" limit's worth of units at the
|
# compute the "projected" limit's worth of units at the
|
||||||
# current pp (weighted) price:
|
# current pp (weighted) price:
|
||||||
slot_size = currency_per_slot / live_pp.avg_price
|
slot_size = currency_per_slot / live_pp.ppu
|
||||||
|
|
||||||
else:
|
else:
|
||||||
slot_size = u_per_slot
|
slot_size = u_per_slot
|
||||||
|
@ -244,7 +202,12 @@ class Allocator(BaseModel):
|
||||||
if order_size < slot_size:
|
if order_size < slot_size:
|
||||||
# compute a fractional slots size to display
|
# compute a fractional slots size to display
|
||||||
slots_used = self.slots_used(
|
slots_used = self.slots_used(
|
||||||
Position(symbol=sym, size=order_size, avg_price=price)
|
Position(
|
||||||
|
symbol=sym,
|
||||||
|
size=order_size,
|
||||||
|
ppu=price,
|
||||||
|
bsuid=sym,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -271,8 +234,8 @@ class Allocator(BaseModel):
|
||||||
abs_pp_size = abs(pp.size)
|
abs_pp_size = abs(pp.size)
|
||||||
|
|
||||||
if self.size_unit == 'currency':
|
if self.size_unit == 'currency':
|
||||||
# live_currency_size = size or (abs_pp_size * pp.avg_price)
|
# live_currency_size = size or (abs_pp_size * pp.ppu)
|
||||||
live_currency_size = abs_pp_size * pp.avg_price
|
live_currency_size = abs_pp_size * pp.ppu
|
||||||
prop = live_currency_size / self.currency_limit
|
prop = live_currency_size / self.currency_limit
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -284,14 +247,6 @@ class Allocator(BaseModel):
|
||||||
return round(prop * self.slots)
|
return round(prop * self.slots)
|
||||||
|
|
||||||
|
|
||||||
_derivs = (
|
|
||||||
'future',
|
|
||||||
'continuous_future',
|
|
||||||
'option',
|
|
||||||
'futures_option',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def mk_allocator(
|
def mk_allocator(
|
||||||
|
|
||||||
symbol: Symbol,
|
symbol: Symbol,
|
||||||
|
@ -300,7 +255,7 @@ def mk_allocator(
|
||||||
# default allocation settings
|
# default allocation settings
|
||||||
defaults: dict[str, float] = {
|
defaults: dict[str, float] = {
|
||||||
'account': None, # select paper by default
|
'account': None, # select paper by default
|
||||||
'size_unit': 'currency',
|
# 'size_unit': 'currency',
|
||||||
'units_limit': 400,
|
'units_limit': 400,
|
||||||
'currency_limit': 5e3,
|
'currency_limit': 5e3,
|
||||||
'slots': 4,
|
'slots': 4,
|
||||||
|
@ -318,42 +273,9 @@ def mk_allocator(
|
||||||
'currency_limit': 6e3,
|
'currency_limit': 6e3,
|
||||||
'slots': 6,
|
'slots': 6,
|
||||||
}
|
}
|
||||||
|
|
||||||
defaults.update(user_def)
|
defaults.update(user_def)
|
||||||
|
|
||||||
alloc = Allocator(
|
return Allocator(
|
||||||
symbol=symbol,
|
symbol=symbol,
|
||||||
**defaults,
|
**defaults,
|
||||||
)
|
)
|
||||||
|
|
||||||
asset_type = symbol.type_key
|
|
||||||
|
|
||||||
# specific configs by asset class / type
|
|
||||||
|
|
||||||
if asset_type in _derivs:
|
|
||||||
# since it's harder to know how currency "applies" in this case
|
|
||||||
# given leverage properties
|
|
||||||
alloc.size_unit = '# units'
|
|
||||||
|
|
||||||
# set units limit to slots size thus making make the next
|
|
||||||
# entry step 1.0
|
|
||||||
alloc.units_limit = alloc.slots
|
|
||||||
|
|
||||||
# if the current position is already greater then the limit
|
|
||||||
# settings, increase the limit to the current position
|
|
||||||
if alloc.size_unit == 'currency':
|
|
||||||
startup_size = startup_pp.size * startup_pp.avg_price
|
|
||||||
|
|
||||||
if startup_size > alloc.currency_limit:
|
|
||||||
alloc.currency_limit = round(startup_size, ndigits=2)
|
|
||||||
|
|
||||||
else:
|
|
||||||
startup_size = abs(startup_pp.size)
|
|
||||||
|
|
||||||
if startup_size > alloc.units_limit:
|
|
||||||
alloc.units_limit = startup_size
|
|
||||||
|
|
||||||
if asset_type in _derivs:
|
|
||||||
alloc.slots = alloc.units_limit
|
|
||||||
|
|
||||||
return alloc
|
|
||||||
|
|
|
@ -18,26 +18,32 @@
|
||||||
Orders and execution client API.
|
Orders and execution client API.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from contextlib import asynccontextmanager as acm
|
from contextlib import asynccontextmanager as acm
|
||||||
from typing import Dict
|
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from dataclasses import dataclass, field
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
import tractor
|
import tractor
|
||||||
from tractor.trionics import broadcast_receiver
|
from tractor.trionics import broadcast_receiver
|
||||||
|
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
from ._ems import _emsd_main
|
from ..data.types import Struct
|
||||||
from .._daemon import maybe_open_emsd
|
from .._daemon import maybe_open_emsd
|
||||||
from ._messages import Order, Cancel
|
from ._messages import Order, Cancel
|
||||||
|
from ..brokers import get_brokermod
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._messages import (
|
||||||
|
BrokerdPosition,
|
||||||
|
Status,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class OrderBook(Struct):
|
||||||
class OrderBook:
|
|
||||||
'''EMS-client-side order book ctl and tracking.
|
'''EMS-client-side order book ctl and tracking.
|
||||||
|
|
||||||
A style similar to "model-view" is used here where this api is
|
A style similar to "model-view" is used here where this api is
|
||||||
|
@ -52,20 +58,18 @@ class OrderBook:
|
||||||
# mem channels used to relay order requests to the EMS daemon
|
# mem channels used to relay order requests to the EMS daemon
|
||||||
_to_ems: trio.abc.SendChannel
|
_to_ems: trio.abc.SendChannel
|
||||||
_from_order_book: trio.abc.ReceiveChannel
|
_from_order_book: trio.abc.ReceiveChannel
|
||||||
|
_sent_orders: dict[str, Order] = {}
|
||||||
_sent_orders: Dict[str, Order] = field(default_factory=dict)
|
|
||||||
_ready_to_receive: trio.Event = trio.Event()
|
|
||||||
|
|
||||||
def send(
|
def send(
|
||||||
self,
|
self,
|
||||||
msg: Order,
|
msg: Order | dict,
|
||||||
|
|
||||||
) -> dict:
|
) -> dict:
|
||||||
self._sent_orders[msg.oid] = msg
|
self._sent_orders[msg.oid] = msg
|
||||||
self._to_ems.send_nowait(msg.dict())
|
self._to_ems.send_nowait(msg)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def update(
|
def send_update(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
uuid: str,
|
uuid: str,
|
||||||
|
@ -73,9 +77,8 @@ class OrderBook:
|
||||||
|
|
||||||
) -> dict:
|
) -> dict:
|
||||||
cmd = self._sent_orders[uuid]
|
cmd = self._sent_orders[uuid]
|
||||||
msg = cmd.dict()
|
msg = cmd.copy(update=data)
|
||||||
msg.update(data)
|
self._sent_orders[uuid] = msg
|
||||||
self._sent_orders[uuid] = Order(**msg)
|
|
||||||
self._to_ems.send_nowait(msg)
|
self._to_ems.send_nowait(msg)
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
|
@ -83,12 +86,18 @@ class OrderBook:
|
||||||
"""Cancel an order (or alert) in the EMS.
|
"""Cancel an order (or alert) in the EMS.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
cmd = self._sent_orders[uuid]
|
cmd = self._sent_orders.get(uuid)
|
||||||
|
if not cmd:
|
||||||
|
log.error(
|
||||||
|
f'Unknown order {uuid}!?\n'
|
||||||
|
f'Maybe there is a stale entry or line?\n'
|
||||||
|
f'You should report this as a bug!'
|
||||||
|
)
|
||||||
msg = Cancel(
|
msg = Cancel(
|
||||||
oid=uuid,
|
oid=uuid,
|
||||||
symbol=cmd.symbol,
|
symbol=cmd.symbol,
|
||||||
)
|
)
|
||||||
self._to_ems.send_nowait(msg.dict())
|
self._to_ems.send_nowait(msg)
|
||||||
|
|
||||||
|
|
||||||
_orders: OrderBook = None
|
_orders: OrderBook = None
|
||||||
|
@ -149,21 +158,35 @@ async def relay_order_cmds_from_sync_code(
|
||||||
book = get_orders()
|
book = get_orders()
|
||||||
async with book._from_order_book.subscribe() as orders_stream:
|
async with book._from_order_book.subscribe() as orders_stream:
|
||||||
async for cmd in orders_stream:
|
async for cmd in orders_stream:
|
||||||
if cmd['symbol'] == symbol_key:
|
sym = cmd.symbol
|
||||||
log.info(f'Send order cmd:\n{pformat(cmd)}')
|
msg = pformat(cmd)
|
||||||
|
if sym == symbol_key:
|
||||||
|
log.info(f'Send order cmd:\n{msg}')
|
||||||
# send msg over IPC / wire
|
# send msg over IPC / wire
|
||||||
await to_ems_stream.send(cmd)
|
await to_ems_stream.send(cmd)
|
||||||
|
else:
|
||||||
|
log.warning(
|
||||||
|
f'Ignoring unmatched order cmd for {sym} != {symbol_key}:'
|
||||||
|
f'\n{msg}'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def open_ems(
|
async def open_ems(
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
|
mode: str = 'live',
|
||||||
|
|
||||||
) -> (
|
) -> tuple[
|
||||||
OrderBook,
|
OrderBook,
|
||||||
tractor.MsgStream,
|
tractor.MsgStream,
|
||||||
dict,
|
dict[
|
||||||
):
|
# brokername, acctid
|
||||||
|
tuple[str, str],
|
||||||
|
list[BrokerdPosition],
|
||||||
|
],
|
||||||
|
list[str],
|
||||||
|
dict[str, Status],
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Spawn an EMS daemon and begin sending orders and receiving
|
Spawn an EMS daemon and begin sending orders and receiving
|
||||||
alerts.
|
alerts.
|
||||||
|
@ -206,18 +229,35 @@ async def open_ems(
|
||||||
|
|
||||||
async with maybe_open_emsd(broker) as portal:
|
async with maybe_open_emsd(broker) as portal:
|
||||||
|
|
||||||
|
mod = get_brokermod(broker)
|
||||||
|
if (
|
||||||
|
not getattr(mod, 'trades_dialogue', None)
|
||||||
|
or mode == 'paper'
|
||||||
|
):
|
||||||
|
mode = 'paper'
|
||||||
|
|
||||||
|
from ._ems import _emsd_main
|
||||||
async with (
|
async with (
|
||||||
# connect to emsd
|
# connect to emsd
|
||||||
portal.open_context(
|
portal.open_context(
|
||||||
|
|
||||||
_emsd_main,
|
_emsd_main,
|
||||||
fqsn=fqsn,
|
fqsn=fqsn,
|
||||||
|
exec_mode=mode,
|
||||||
|
|
||||||
) as (ctx, (positions, accounts)),
|
) as (
|
||||||
|
ctx,
|
||||||
|
(
|
||||||
|
positions,
|
||||||
|
accounts,
|
||||||
|
dialogs,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
# open 2-way trade command stream
|
# open 2-way trade command stream
|
||||||
ctx.open_stream() as trades_stream,
|
ctx.open_stream() as trades_stream,
|
||||||
):
|
):
|
||||||
|
# start sync code order msg delivery task
|
||||||
async with trio.open_nursery() as n:
|
async with trio.open_nursery() as n:
|
||||||
n.start_soon(
|
n.start_soon(
|
||||||
relay_order_cmds_from_sync_code,
|
relay_order_cmds_from_sync_code,
|
||||||
|
@ -225,4 +265,10 @@ async def open_ems(
|
||||||
trades_stream
|
trades_stream
|
||||||
)
|
)
|
||||||
|
|
||||||
yield book, trades_stream, positions, accounts
|
yield (
|
||||||
|
book,
|
||||||
|
trades_stream,
|
||||||
|
positions,
|
||||||
|
accounts,
|
||||||
|
dialogs,
|
||||||
|
)
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,5 +1,5 @@
|
||||||
# piker: trading gear for hackers
|
# piker: trading gear for hackers
|
||||||
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
@ -15,108 +15,162 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Clearing system messagingn types and protocols.
|
Clearing sub-system message and protocols.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from typing import Optional, Union
|
# from collections import (
|
||||||
|
# ChainMap,
|
||||||
|
# deque,
|
||||||
|
# )
|
||||||
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
Literal,
|
||||||
|
)
|
||||||
|
|
||||||
# TODO: try out just encoding/send direction for now?
|
from msgspec import field
|
||||||
# import msgspec
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
from ..data._source import Symbol
|
from ..data._source import Symbol
|
||||||
|
from ..data.types import Struct
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: a composite for tracking msg flow on 2-legged
|
||||||
|
# dialogs.
|
||||||
|
# class Dialog(ChainMap):
|
||||||
|
# '''
|
||||||
|
# Msg collection abstraction to easily track the state changes of
|
||||||
|
# a msg flow in one high level, query-able and immutable construct.
|
||||||
|
|
||||||
|
# The main use case is to query data from a (long-running)
|
||||||
|
# msg-transaction-sequence
|
||||||
|
|
||||||
|
|
||||||
|
# '''
|
||||||
|
# def update(
|
||||||
|
# self,
|
||||||
|
# msg,
|
||||||
|
# ) -> None:
|
||||||
|
# self.maps.insert(0, msg.to_dict())
|
||||||
|
|
||||||
|
# def flatten(self) -> dict:
|
||||||
|
# return dict(self)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: ``msgspec`` stuff worth paying attention to:
|
||||||
|
# - schema evolution:
|
||||||
|
# https://jcristharif.com/msgspec/usage.html#schema-evolution
|
||||||
|
# - for eg. ``BrokerdStatus``, instead just have separate messages?
|
||||||
|
# - use literals for a common msg determined by diff keys?
|
||||||
|
# - https://jcristharif.com/msgspec/usage.html#literal
|
||||||
|
|
||||||
|
# --------------
|
||||||
# Client -> emsd
|
# Client -> emsd
|
||||||
|
# --------------
|
||||||
|
|
||||||
|
class Order(Struct):
|
||||||
|
|
||||||
class Cancel(BaseModel):
|
# TODO: ideally we can combine these 2 fields into
|
||||||
'''Cancel msg for removing a dark (ems triggered) or
|
# 1 and just use the size polarity to determine a buy/sell.
|
||||||
broker-submitted (live) trigger/order.
|
# i would like to see this become more like
|
||||||
|
# https://jcristharif.com/msgspec/usage.html#literal
|
||||||
'''
|
# action: Literal[
|
||||||
action: str = 'cancel'
|
# 'live',
|
||||||
oid: str # uuid4
|
# 'dark',
|
||||||
symbol: str
|
# 'alert',
|
||||||
|
# ]
|
||||||
|
|
||||||
class Order(BaseModel):
|
|
||||||
|
|
||||||
action: str # {'buy', 'sell', 'alert'}
|
|
||||||
# internal ``emdsd`` unique "order id"
|
|
||||||
oid: str # uuid4
|
|
||||||
symbol: Union[str, Symbol]
|
|
||||||
account: str # should we set a default as '' ?
|
|
||||||
|
|
||||||
price: float
|
|
||||||
size: float
|
|
||||||
brokers: list[str]
|
|
||||||
|
|
||||||
# Assigned once initial ack is received
|
|
||||||
# ack_time_ns: Optional[int] = None
|
|
||||||
|
|
||||||
|
action: Literal[
|
||||||
|
'buy',
|
||||||
|
'sell',
|
||||||
|
'alert',
|
||||||
|
]
|
||||||
# determines whether the create execution
|
# determines whether the create execution
|
||||||
# will be submitted to the ems or directly to
|
# will be submitted to the ems or directly to
|
||||||
# the backend broker
|
# the backend broker
|
||||||
exec_mode: str # {'dark', 'live', 'paper'}
|
exec_mode: Literal[
|
||||||
|
'dark',
|
||||||
|
'live',
|
||||||
|
# 'paper', no right?
|
||||||
|
]
|
||||||
|
|
||||||
class Config:
|
# internal ``emdsd`` unique "order id"
|
||||||
# just for pre-loading a ``Symbol`` when used
|
oid: str # uuid4
|
||||||
# in the order mode staging process
|
symbol: str | Symbol
|
||||||
arbitrary_types_allowed = True
|
account: str # should we set a default as '' ?
|
||||||
# don't copy this model instance when used in
|
|
||||||
# a recursive model
|
|
||||||
copy_on_model_validation = False
|
|
||||||
|
|
||||||
|
price: float
|
||||||
|
size: float # -ve is "sell", +ve is "buy"
|
||||||
|
|
||||||
|
brokers: Optional[list[str]] = []
|
||||||
|
|
||||||
|
|
||||||
|
class Cancel(Struct):
|
||||||
|
'''
|
||||||
|
Cancel msg for removing a dark (ems triggered) or
|
||||||
|
broker-submitted (live) trigger/order.
|
||||||
|
|
||||||
|
'''
|
||||||
|
oid: str # uuid4
|
||||||
|
symbol: str
|
||||||
|
action: str = 'cancel'
|
||||||
|
|
||||||
|
|
||||||
|
# --------------
|
||||||
# Client <- emsd
|
# Client <- emsd
|
||||||
|
# --------------
|
||||||
# update msgs from ems which relay state change info
|
# update msgs from ems which relay state change info
|
||||||
# from the active clearing engine.
|
# from the active clearing engine.
|
||||||
|
|
||||||
|
class Status(Struct):
|
||||||
|
|
||||||
class Status(BaseModel):
|
time_ns: int
|
||||||
|
oid: str # uuid4 ems-order dialog id
|
||||||
|
|
||||||
|
resp: Literal[
|
||||||
|
'pending', # acked by broker but not yet open
|
||||||
|
'open',
|
||||||
|
'dark_open', # dark/algo triggered order is open in ems clearing loop
|
||||||
|
'triggered', # above triggered order sent to brokerd, or an alert closed
|
||||||
|
'closed', # fully cleared all size/units
|
||||||
|
'fill', # partial execution
|
||||||
|
'canceled',
|
||||||
|
'error',
|
||||||
|
]
|
||||||
|
|
||||||
name: str = 'status'
|
name: str = 'status'
|
||||||
oid: str # uuid4
|
|
||||||
time_ns: int
|
|
||||||
|
|
||||||
# {
|
|
||||||
# 'dark_submitted',
|
|
||||||
# 'dark_cancelled',
|
|
||||||
# 'dark_triggered',
|
|
||||||
|
|
||||||
# 'broker_submitted',
|
|
||||||
# 'broker_cancelled',
|
|
||||||
# 'broker_executed',
|
|
||||||
# 'broker_filled',
|
|
||||||
# 'broker_errored',
|
|
||||||
|
|
||||||
# 'alert_submitted',
|
|
||||||
# 'alert_triggered',
|
|
||||||
|
|
||||||
# }
|
|
||||||
resp: str # "response", see above
|
|
||||||
|
|
||||||
# symbol: str
|
|
||||||
|
|
||||||
# trigger info
|
|
||||||
trigger_price: Optional[float] = None
|
|
||||||
# price: float
|
|
||||||
|
|
||||||
# broker: Optional[str] = None
|
|
||||||
|
|
||||||
# this maps normally to the ``BrokerdOrder.reqid`` below, an id
|
# this maps normally to the ``BrokerdOrder.reqid`` below, an id
|
||||||
# normally allocated internally by the backend broker routing system
|
# normally allocated internally by the backend broker routing system
|
||||||
broker_reqid: Optional[Union[int, str]] = None
|
reqid: Optional[int | str] = None
|
||||||
|
|
||||||
# for relaying backend msg data "through" the ems layer
|
# the (last) source order/request msg if provided
|
||||||
|
# (eg. the Order/Cancel which causes this msg) and
|
||||||
|
# acts as a back-reference to the corresponding
|
||||||
|
# request message which was the source of this msg.
|
||||||
|
req: Order | None = None
|
||||||
|
|
||||||
|
# XXX: better design/name here?
|
||||||
|
# flag that can be set to indicate a message for an order
|
||||||
|
# event that wasn't originated by piker's emsd (eg. some external
|
||||||
|
# trading system which does it's own order control but that you
|
||||||
|
# might want to "track" using piker UIs/systems).
|
||||||
|
src: Optional[str] = None
|
||||||
|
|
||||||
|
# set when a cancel request msg was set for this order flow dialog
|
||||||
|
# but the brokerd dialog isn't yet in a cancelled state.
|
||||||
|
cancel_called: bool = False
|
||||||
|
|
||||||
|
# for relaying a boxed brokerd-dialog-side msg data "through" the
|
||||||
|
# ems layer to clients.
|
||||||
brokerd_msg: dict = {}
|
brokerd_msg: dict = {}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------
|
||||||
# emsd -> brokerd
|
# emsd -> brokerd
|
||||||
|
# ---------------
|
||||||
# requests *sent* from ems to respective backend broker daemon
|
# requests *sent* from ems to respective backend broker daemon
|
||||||
|
|
||||||
class BrokerdCancel(BaseModel):
|
class BrokerdCancel(Struct):
|
||||||
|
|
||||||
action: str = 'cancel'
|
|
||||||
oid: str # piker emsd order id
|
oid: str # piker emsd order id
|
||||||
time_ns: int
|
time_ns: int
|
||||||
|
|
||||||
|
@ -127,34 +181,39 @@ class BrokerdCancel(BaseModel):
|
||||||
# for setting a unique order id then this value will be relayed back
|
# for setting a unique order id then this value will be relayed back
|
||||||
# on the emsd order request stream as the ``BrokerdOrderAck.reqid``
|
# on the emsd order request stream as the ``BrokerdOrderAck.reqid``
|
||||||
# field
|
# field
|
||||||
reqid: Optional[Union[int, str]] = None
|
reqid: Optional[int | str] = None
|
||||||
|
action: str = 'cancel'
|
||||||
|
|
||||||
|
|
||||||
class BrokerdOrder(BaseModel):
|
class BrokerdOrder(Struct):
|
||||||
|
|
||||||
action: str # {buy, sell}
|
|
||||||
oid: str
|
oid: str
|
||||||
account: str
|
account: str
|
||||||
time_ns: int
|
time_ns: int
|
||||||
|
|
||||||
|
symbol: str # fqsn
|
||||||
|
price: float
|
||||||
|
size: float
|
||||||
|
|
||||||
|
# TODO: if we instead rely on a +ve/-ve size to determine
|
||||||
|
# the action we more or less don't need this field right?
|
||||||
|
action: str = '' # {buy, sell}
|
||||||
|
|
||||||
# "broker request id": broker specific/internal order id if this is
|
# "broker request id": broker specific/internal order id if this is
|
||||||
# None, creates a new order otherwise if the id is valid the backend
|
# None, creates a new order otherwise if the id is valid the backend
|
||||||
# api must modify the existing matching order. If the broker allows
|
# api must modify the existing matching order. If the broker allows
|
||||||
# for setting a unique order id then this value will be relayed back
|
# for setting a unique order id then this value will be relayed back
|
||||||
# on the emsd order request stream as the ``BrokerdOrderAck.reqid``
|
# on the emsd order request stream as the ``BrokerdOrderAck.reqid``
|
||||||
# field
|
# field
|
||||||
reqid: Optional[Union[int, str]] = None
|
reqid: Optional[int | str] = None
|
||||||
|
|
||||||
symbol: str # symbol.<providername> ?
|
|
||||||
price: float
|
|
||||||
size: float
|
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------
|
||||||
# emsd <- brokerd
|
# emsd <- brokerd
|
||||||
|
# ---------------
|
||||||
# requests *received* to ems from broker backend
|
# requests *received* to ems from broker backend
|
||||||
|
|
||||||
|
class BrokerdOrderAck(Struct):
|
||||||
class BrokerdOrderAck(BaseModel):
|
|
||||||
'''
|
'''
|
||||||
Immediate reponse to a brokerd order request providing the broker
|
Immediate reponse to a brokerd order request providing the broker
|
||||||
specific unique order id so that the EMS can associate this
|
specific unique order id so that the EMS can associate this
|
||||||
|
@ -162,102 +221,93 @@ class BrokerdOrderAck(BaseModel):
|
||||||
``.oid`` (which is a uuid4).
|
``.oid`` (which is a uuid4).
|
||||||
|
|
||||||
'''
|
'''
|
||||||
name: str = 'ack'
|
|
||||||
|
|
||||||
# defined and provided by backend
|
# defined and provided by backend
|
||||||
reqid: Union[int, str]
|
reqid: int | str
|
||||||
|
|
||||||
# emsd id originally sent in matching request msg
|
# emsd id originally sent in matching request msg
|
||||||
oid: str
|
oid: str
|
||||||
account: str = ''
|
account: str = ''
|
||||||
|
name: str = 'ack'
|
||||||
|
|
||||||
|
|
||||||
class BrokerdStatus(BaseModel):
|
class BrokerdStatus(Struct):
|
||||||
|
|
||||||
name: str = 'status'
|
reqid: int | str
|
||||||
reqid: Union[int, str]
|
|
||||||
time_ns: int
|
time_ns: int
|
||||||
|
status: Literal[
|
||||||
|
'open',
|
||||||
|
'canceled',
|
||||||
|
'fill',
|
||||||
|
'pending',
|
||||||
|
'error',
|
||||||
|
]
|
||||||
|
|
||||||
# XXX: should be best effort set for every update
|
account: str
|
||||||
account: str = ''
|
name: str = 'status'
|
||||||
|
|
||||||
# {
|
|
||||||
# 'submitted',
|
|
||||||
# 'cancelled',
|
|
||||||
# 'filled',
|
|
||||||
# }
|
|
||||||
status: str
|
|
||||||
|
|
||||||
filled: float = 0.0
|
filled: float = 0.0
|
||||||
reason: str = ''
|
reason: str = ''
|
||||||
remaining: float = 0.0
|
remaining: float = 0.0
|
||||||
|
|
||||||
# XXX: better design/name here?
|
# external: bool = False
|
||||||
# flag that can be set to indicate a message for an order
|
|
||||||
# event that wasn't originated by piker's emsd (eg. some external
|
|
||||||
# trading system which does it's own order control but that you
|
|
||||||
# might want to "track" using piker UIs/systems).
|
|
||||||
external: bool = False
|
|
||||||
|
|
||||||
# XXX: not required schema as of yet
|
# XXX: not required schema as of yet
|
||||||
broker_details: dict = {
|
broker_details: dict = field(default_factory=lambda: {
|
||||||
'name': '',
|
'name': '',
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
class BrokerdFill(BaseModel):
|
class BrokerdFill(Struct):
|
||||||
'''
|
'''
|
||||||
A single message indicating a "fill-details" event from the broker
|
A single message indicating a "fill-details" event from the broker
|
||||||
if avaiable.
|
if avaiable.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
name: str = 'fill'
|
|
||||||
reqid: Union[int, str]
|
|
||||||
time_ns: int
|
|
||||||
|
|
||||||
# order exeuction related
|
|
||||||
action: str
|
|
||||||
size: float
|
|
||||||
price: float
|
|
||||||
|
|
||||||
broker_details: dict = {} # meta-data (eg. commisions etc.)
|
|
||||||
|
|
||||||
# brokerd timestamp required for order mode arrow placement on x-axis
|
# brokerd timestamp required for order mode arrow placement on x-axis
|
||||||
|
|
||||||
# TODO: maybe int if we force ns?
|
# TODO: maybe int if we force ns?
|
||||||
# we need to normalize this somehow since backends will use their
|
# we need to normalize this somehow since backends will use their
|
||||||
# own format and likely across many disparate epoch clocks...
|
# own format and likely across many disparate epoch clocks...
|
||||||
broker_time: float
|
broker_time: float
|
||||||
|
reqid: int | str
|
||||||
|
time_ns: int
|
||||||
|
|
||||||
|
# order exeuction related
|
||||||
|
size: float
|
||||||
|
price: float
|
||||||
|
|
||||||
|
name: str = 'fill'
|
||||||
|
action: Optional[str] = None
|
||||||
|
broker_details: dict = {} # meta-data (eg. commisions etc.)
|
||||||
|
|
||||||
|
|
||||||
class BrokerdError(BaseModel):
|
class BrokerdError(Struct):
|
||||||
'''
|
'''
|
||||||
Optional error type that can be relayed to emsd for error handling.
|
Optional error type that can be relayed to emsd for error handling.
|
||||||
|
|
||||||
This is still a TODO thing since we're not sure how to employ it yet.
|
This is still a TODO thing since we're not sure how to employ it yet.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
name: str = 'error'
|
|
||||||
oid: str
|
oid: str
|
||||||
|
symbol: str
|
||||||
|
reason: str
|
||||||
|
|
||||||
# if no brokerd order request was actually submitted (eg. we errored
|
# if no brokerd order request was actually submitted (eg. we errored
|
||||||
# at the ``pikerd`` layer) then there will be ``reqid`` allocated.
|
# at the ``pikerd`` layer) then there will be ``reqid`` allocated.
|
||||||
reqid: Optional[Union[int, str]] = None
|
reqid: Optional[int | str] = None
|
||||||
|
|
||||||
symbol: str
|
name: str = 'error'
|
||||||
reason: str
|
|
||||||
broker_details: dict = {}
|
broker_details: dict = {}
|
||||||
|
|
||||||
|
|
||||||
class BrokerdPosition(BaseModel):
|
class BrokerdPosition(Struct):
|
||||||
'''Position update event from brokerd.
|
'''Position update event from brokerd.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
name: str = 'position'
|
|
||||||
|
|
||||||
broker: str
|
broker: str
|
||||||
account: str
|
account: str
|
||||||
symbol: str
|
symbol: str
|
||||||
currency: str
|
|
||||||
size: float
|
size: float
|
||||||
avg_price: float
|
avg_price: float
|
||||||
|
currency: str = ''
|
||||||
|
name: str = 'position'
|
||||||
|
|
|
@ -18,54 +18,71 @@
|
||||||
Fake trading for forward testing.
|
Fake trading for forward testing.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from collections import defaultdict
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
import itertools
|
||||||
import time
|
import time
|
||||||
from typing import Tuple, Optional, Callable
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Optional,
|
||||||
|
Callable,
|
||||||
|
)
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from bidict import bidict
|
from bidict import bidict
|
||||||
|
import pendulum
|
||||||
import trio
|
import trio
|
||||||
import tractor
|
import tractor
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
from .. import data
|
from .. import data
|
||||||
|
from ..data._source import Symbol
|
||||||
|
from ..data.types import Struct
|
||||||
|
from ..pp import (
|
||||||
|
Position,
|
||||||
|
Transaction,
|
||||||
|
)
|
||||||
from ..data._normalize import iterticks
|
from ..data._normalize import iterticks
|
||||||
from ..data._source import unpack_fqsn
|
from ..data._source import unpack_fqsn
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
from ._messages import (
|
from ._messages import (
|
||||||
BrokerdCancel, BrokerdOrder, BrokerdOrderAck, BrokerdStatus,
|
BrokerdCancel,
|
||||||
BrokerdFill, BrokerdPosition, BrokerdError
|
BrokerdOrder,
|
||||||
|
BrokerdOrderAck,
|
||||||
|
BrokerdStatus,
|
||||||
|
BrokerdFill,
|
||||||
|
BrokerdPosition,
|
||||||
|
BrokerdError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class PaperBoi(Struct):
|
||||||
class PaperBoi:
|
'''
|
||||||
"""
|
Emulates a broker order client providing approximately the same API
|
||||||
Emulates a broker order client providing the same API and
|
and delivering an order-event response stream but with methods for
|
||||||
delivering an order-event response stream but with methods for
|
|
||||||
triggering desired events based on forward testing engine
|
triggering desired events based on forward testing engine
|
||||||
requirements.
|
requirements (eg open, closed, fill msgs).
|
||||||
|
|
||||||
"""
|
'''
|
||||||
broker: str
|
broker: str
|
||||||
|
|
||||||
ems_trades_stream: tractor.MsgStream
|
ems_trades_stream: tractor.MsgStream
|
||||||
|
|
||||||
# map of paper "live" orders which be used
|
# map of paper "live" orders which be used
|
||||||
# to simulate fills based on paper engine settings
|
# to simulate fills based on paper engine settings
|
||||||
_buys: bidict
|
_buys: defaultdict[str, bidict]
|
||||||
_sells: bidict
|
_sells: defaultdict[str, bidict]
|
||||||
_reqids: bidict
|
_reqids: bidict
|
||||||
_positions: dict[str, BrokerdPosition]
|
_positions: dict[str, Position]
|
||||||
|
_trade_ledger: dict[str, Any]
|
||||||
|
|
||||||
# init edge case L1 spread
|
# init edge case L1 spread
|
||||||
last_ask: Tuple[float, float] = (float('inf'), 0) # price, size
|
last_ask: tuple[float, float] = (float('inf'), 0) # price, size
|
||||||
last_bid: Tuple[float, float] = (0, 0)
|
last_bid: tuple[float, float] = (0, 0)
|
||||||
|
|
||||||
async def submit_limit(
|
async def submit_limit(
|
||||||
self,
|
self,
|
||||||
|
@ -75,27 +92,24 @@ class PaperBoi:
|
||||||
action: str,
|
action: str,
|
||||||
size: float,
|
size: float,
|
||||||
reqid: Optional[str],
|
reqid: Optional[str],
|
||||||
|
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Place an order and return integer request id provided by client.
|
'''
|
||||||
|
Place an order and return integer request id provided by client.
|
||||||
"""
|
|
||||||
is_modify: bool = False
|
|
||||||
if reqid is None:
|
|
||||||
reqid = str(uuid.uuid4())
|
|
||||||
|
|
||||||
else:
|
|
||||||
# order is already existing, this is a modify
|
|
||||||
(oid, symbol, action, old_price) = self._reqids[reqid]
|
|
||||||
assert old_price != price
|
|
||||||
is_modify = True
|
|
||||||
|
|
||||||
# register order internally
|
|
||||||
self._reqids[reqid] = (oid, symbol, action, price)
|
|
||||||
|
|
||||||
|
'''
|
||||||
if action == 'alert':
|
if action == 'alert':
|
||||||
# bypass all fill simulation
|
# bypass all fill simulation
|
||||||
return reqid
|
return reqid
|
||||||
|
|
||||||
|
entry = self._reqids.get(reqid)
|
||||||
|
if entry:
|
||||||
|
# order is already existing, this is a modify
|
||||||
|
(oid, symbol, action, old_price) = entry
|
||||||
|
else:
|
||||||
|
# register order internally
|
||||||
|
self._reqids[reqid] = (oid, symbol, action, price)
|
||||||
|
|
||||||
# TODO: net latency model
|
# TODO: net latency model
|
||||||
# we checkpoint here quickly particulalry
|
# we checkpoint here quickly particulalry
|
||||||
# for dark orders since we want the dark_executed
|
# for dark orders since we want the dark_executed
|
||||||
|
@ -107,15 +121,18 @@ class PaperBoi:
|
||||||
size = -size
|
size = -size
|
||||||
|
|
||||||
msg = BrokerdStatus(
|
msg = BrokerdStatus(
|
||||||
status='submitted',
|
status='open',
|
||||||
|
# account=f'paper_{self.broker}',
|
||||||
|
account='paper',
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
broker=self.broker,
|
|
||||||
time_ns=time.time_ns(),
|
time_ns=time.time_ns(),
|
||||||
filled=0.0,
|
filled=0.0,
|
||||||
reason='paper_trigger',
|
reason='paper_trigger',
|
||||||
remaining=size,
|
remaining=size,
|
||||||
|
|
||||||
|
broker_details={'name': 'paperboi'},
|
||||||
)
|
)
|
||||||
await self.ems_trades_stream.send(msg.dict())
|
await self.ems_trades_stream.send(msg)
|
||||||
|
|
||||||
# if we're already a clearing price simulate an immediate fill
|
# if we're already a clearing price simulate an immediate fill
|
||||||
if (
|
if (
|
||||||
|
@ -123,28 +140,28 @@ class PaperBoi:
|
||||||
) or (
|
) or (
|
||||||
action == 'sell' and (clear_price := self.last_bid[0]) >= price
|
action == 'sell' and (clear_price := self.last_bid[0]) >= price
|
||||||
):
|
):
|
||||||
await self.fake_fill(symbol, clear_price, size, action, reqid, oid)
|
await self.fake_fill(
|
||||||
|
symbol,
|
||||||
|
clear_price,
|
||||||
|
size,
|
||||||
|
action,
|
||||||
|
reqid,
|
||||||
|
oid,
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
|
||||||
# register this submissions as a paper live order
|
# register this submissions as a paper live order
|
||||||
|
else:
|
||||||
# submit order to book simulation fill loop
|
# set the simulated order in the respective table for lookup
|
||||||
|
# and trigger by the simulated clearing task normally
|
||||||
|
# running ``simulate_fills()``.
|
||||||
if action == 'buy':
|
if action == 'buy':
|
||||||
orders = self._buys
|
orders = self._buys
|
||||||
|
|
||||||
elif action == 'sell':
|
elif action == 'sell':
|
||||||
orders = self._sells
|
orders = self._sells
|
||||||
|
|
||||||
# set the simulated order in the respective table for lookup
|
# {symbol -> bidict[oid, (<price data>)]}
|
||||||
# and trigger by the simulated clearing task normally
|
orders[symbol][oid] = (price, size, reqid, action)
|
||||||
# running ``simulate_fills()``.
|
|
||||||
|
|
||||||
if is_modify:
|
|
||||||
# remove any existing order for the old price
|
|
||||||
orders[symbol].pop((oid, old_price))
|
|
||||||
|
|
||||||
# buys/sells: (symbol -> (price -> order))
|
|
||||||
orders.setdefault(symbol, {})[(oid, price)] = (size, reqid, action)
|
|
||||||
|
|
||||||
return reqid
|
return reqid
|
||||||
|
|
||||||
|
@ -157,26 +174,26 @@ class PaperBoi:
|
||||||
oid, symbol, action, price = self._reqids[reqid]
|
oid, symbol, action, price = self._reqids[reqid]
|
||||||
|
|
||||||
if action == 'buy':
|
if action == 'buy':
|
||||||
self._buys[symbol].pop((oid, price))
|
self._buys[symbol].pop(oid, None)
|
||||||
elif action == 'sell':
|
elif action == 'sell':
|
||||||
self._sells[symbol].pop((oid, price))
|
self._sells[symbol].pop(oid, None)
|
||||||
|
|
||||||
# TODO: net latency model
|
# TODO: net latency model
|
||||||
await trio.sleep(0.05)
|
await trio.sleep(0.05)
|
||||||
|
|
||||||
msg = BrokerdStatus(
|
msg = BrokerdStatus(
|
||||||
status='cancelled',
|
status='canceled',
|
||||||
oid=oid,
|
account='paper',
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
broker=self.broker,
|
|
||||||
time_ns=time.time_ns(),
|
time_ns=time.time_ns(),
|
||||||
|
broker_details={'name': 'paperboi'},
|
||||||
)
|
)
|
||||||
await self.ems_trades_stream.send(msg.dict())
|
await self.ems_trades_stream.send(msg)
|
||||||
|
|
||||||
async def fake_fill(
|
async def fake_fill(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
symbol: str,
|
fqsn: str,
|
||||||
price: float,
|
price: float,
|
||||||
size: float,
|
size: float,
|
||||||
action: str, # one of {'buy', 'sell'}
|
action: str, # one of {'buy', 'sell'}
|
||||||
|
@ -190,21 +207,21 @@ class PaperBoi:
|
||||||
remaining: float = 0,
|
remaining: float = 0,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Pretend to fill a broker order @ price and size.
|
'''
|
||||||
|
Pretend to fill a broker order @ price and size.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
# TODO: net latency model
|
# TODO: net latency model
|
||||||
await trio.sleep(0.05)
|
await trio.sleep(0.05)
|
||||||
|
fill_time_ns = time.time_ns()
|
||||||
|
fill_time_s = time.time()
|
||||||
|
|
||||||
msg = BrokerdFill(
|
fill_msg = BrokerdFill(
|
||||||
|
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
time_ns=time.time_ns(),
|
time_ns=fill_time_ns,
|
||||||
|
|
||||||
action=action,
|
action=action,
|
||||||
size=size,
|
size=size,
|
||||||
price=price,
|
price=price,
|
||||||
|
|
||||||
broker_time=datetime.now().timestamp(),
|
broker_time=datetime.now().timestamp(),
|
||||||
broker_details={
|
broker_details={
|
||||||
'paper_info': {
|
'paper_info': {
|
||||||
|
@ -214,79 +231,67 @@ class PaperBoi:
|
||||||
'name': self.broker + '_paper',
|
'name': self.broker + '_paper',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
await self.ems_trades_stream.send(msg.dict())
|
log.info(f'Fake filling order:\n{fill_msg}')
|
||||||
|
await self.ems_trades_stream.send(fill_msg)
|
||||||
|
|
||||||
|
self._trade_ledger.update(fill_msg.to_dict())
|
||||||
|
|
||||||
if order_complete:
|
if order_complete:
|
||||||
|
|
||||||
msg = BrokerdStatus(
|
msg = BrokerdStatus(
|
||||||
|
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
time_ns=time.time_ns(),
|
time_ns=time.time_ns(),
|
||||||
|
# account=f'paper_{self.broker}',
|
||||||
status='filled',
|
account='paper',
|
||||||
|
status='closed',
|
||||||
filled=size,
|
filled=size,
|
||||||
remaining=0 if order_complete else remaining,
|
remaining=0 if order_complete else remaining,
|
||||||
|
|
||||||
action=action,
|
|
||||||
size=size,
|
|
||||||
price=price,
|
|
||||||
|
|
||||||
broker_details={
|
|
||||||
'paper_info': {
|
|
||||||
'oid': oid,
|
|
||||||
},
|
|
||||||
'name': self.broker,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
await self.ems_trades_stream.send(msg.dict())
|
await self.ems_trades_stream.send(msg)
|
||||||
|
|
||||||
# lookup any existing position
|
# lookup any existing position
|
||||||
token = f'{symbol}.{self.broker}'
|
key = fqsn.rstrip(f'.{self.broker}')
|
||||||
pp_msg = self._positions.setdefault(
|
pp = self._positions.setdefault(
|
||||||
token,
|
fqsn,
|
||||||
BrokerdPosition(
|
Position(
|
||||||
|
Symbol(
|
||||||
|
key=key,
|
||||||
|
broker_info={self.broker: {}},
|
||||||
|
),
|
||||||
|
size=size,
|
||||||
|
ppu=price,
|
||||||
|
bsuid=key,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
t = Transaction(
|
||||||
|
fqsn=fqsn,
|
||||||
|
tid=oid,
|
||||||
|
size=size,
|
||||||
|
price=price,
|
||||||
|
cost=0, # TODO: cost model
|
||||||
|
dt=pendulum.from_timestamp(fill_time_s),
|
||||||
|
bsuid=key,
|
||||||
|
)
|
||||||
|
pp.add_clear(t)
|
||||||
|
|
||||||
|
pp_msg = BrokerdPosition(
|
||||||
broker=self.broker,
|
broker=self.broker,
|
||||||
account='paper',
|
account='paper',
|
||||||
symbol=symbol,
|
symbol=fqsn,
|
||||||
# TODO: we need to look up the asset currency from
|
# TODO: we need to look up the asset currency from
|
||||||
# broker info. i guess for crypto this can be
|
# broker info. i guess for crypto this can be
|
||||||
# inferred from the pair?
|
# inferred from the pair?
|
||||||
currency='',
|
currency='',
|
||||||
size=0.0,
|
size=pp.size,
|
||||||
avg_price=0,
|
avg_price=pp.ppu,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# "avg position price" calcs
|
await self.ems_trades_stream.send(pp_msg)
|
||||||
# TODO: eventually it'd be nice to have a small set of routines
|
|
||||||
# to do this stuff from a sequence of cleared orders to enable
|
|
||||||
# so called "contextual positions".
|
|
||||||
new_size = size + pp_msg.size
|
|
||||||
|
|
||||||
# old size minus the new size gives us size differential with
|
|
||||||
# +ve -> increase in pp size
|
|
||||||
# -ve -> decrease in pp size
|
|
||||||
size_diff = abs(new_size) - abs(pp_msg.size)
|
|
||||||
|
|
||||||
if new_size == 0:
|
|
||||||
pp_msg.avg_price = 0
|
|
||||||
|
|
||||||
elif size_diff > 0:
|
|
||||||
# only update the "average position price" when the position
|
|
||||||
# size increases not when it decreases (i.e. the position is
|
|
||||||
# being made smaller)
|
|
||||||
pp_msg.avg_price = (
|
|
||||||
abs(size) * price + pp_msg.avg_price * abs(pp_msg.size)
|
|
||||||
) / abs(new_size)
|
|
||||||
|
|
||||||
pp_msg.size = new_size
|
|
||||||
|
|
||||||
await self.ems_trades_stream.send(pp_msg.dict())
|
|
||||||
|
|
||||||
|
|
||||||
async def simulate_fills(
|
async def simulate_fills(
|
||||||
quote_stream: 'tractor.ReceiveStream', # noqa
|
quote_stream: tractor.MsgStream, # noqa
|
||||||
client: PaperBoi,
|
client: PaperBoi,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
# TODO: more machinery to better simulate real-world market things:
|
# TODO: more machinery to better simulate real-world market things:
|
||||||
|
@ -306,61 +311,116 @@ async def simulate_fills(
|
||||||
|
|
||||||
# this stream may eventually contain multiple symbols
|
# this stream may eventually contain multiple symbols
|
||||||
async for quotes in quote_stream:
|
async for quotes in quote_stream:
|
||||||
|
|
||||||
for sym, quote in quotes.items():
|
for sym, quote in quotes.items():
|
||||||
|
|
||||||
for tick in iterticks(
|
for tick in iterticks(
|
||||||
quote,
|
quote,
|
||||||
# dark order price filter(s)
|
# dark order price filter(s)
|
||||||
types=('ask', 'bid', 'trade', 'last')
|
types=('ask', 'bid', 'trade', 'last')
|
||||||
):
|
):
|
||||||
# print(tick)
|
tick_price = tick['price']
|
||||||
tick_price = tick.get('price')
|
|
||||||
ttype = tick['type']
|
|
||||||
|
|
||||||
if ttype in ('ask',):
|
buys: bidict[str, tuple] = client._buys[sym]
|
||||||
|
iter_buys = reversed(sorted(
|
||||||
|
buys.values(),
|
||||||
|
key=itemgetter(0),
|
||||||
|
))
|
||||||
|
|
||||||
|
def buy_on_ask(our_price):
|
||||||
|
return tick_price <= our_price
|
||||||
|
|
||||||
|
sells: bidict[str, tuple] = client._sells[sym]
|
||||||
|
iter_sells = sorted(
|
||||||
|
sells.values(),
|
||||||
|
key=itemgetter(0)
|
||||||
|
)
|
||||||
|
|
||||||
|
def sell_on_bid(our_price):
|
||||||
|
return tick_price >= our_price
|
||||||
|
|
||||||
|
match tick:
|
||||||
|
|
||||||
|
# on an ask queue tick, only clear buy entries
|
||||||
|
case {
|
||||||
|
'price': tick_price,
|
||||||
|
'type': 'ask',
|
||||||
|
}:
|
||||||
client.last_ask = (
|
client.last_ask = (
|
||||||
tick_price,
|
tick_price,
|
||||||
tick.get('size', client.last_ask[1]),
|
tick.get('size', client.last_ask[1]),
|
||||||
)
|
)
|
||||||
|
|
||||||
orders = client._buys.get(sym, {})
|
iter_entries = zip(
|
||||||
|
iter_buys,
|
||||||
book_sequence = reversed(
|
itertools.repeat(buy_on_ask)
|
||||||
sorted(orders.keys(), key=itemgetter(1)))
|
)
|
||||||
|
|
||||||
def pred(our_price):
|
|
||||||
return tick_price < our_price
|
|
||||||
|
|
||||||
elif ttype in ('bid',):
|
|
||||||
|
|
||||||
|
# on a bid queue tick, only clear sell entries
|
||||||
|
case {
|
||||||
|
'price': tick_price,
|
||||||
|
'type': 'bid',
|
||||||
|
}:
|
||||||
client.last_bid = (
|
client.last_bid = (
|
||||||
tick_price,
|
tick_price,
|
||||||
tick.get('size', client.last_bid[1]),
|
tick.get('size', client.last_bid[1]),
|
||||||
)
|
)
|
||||||
|
|
||||||
orders = client._sells.get(sym, {})
|
iter_entries = zip(
|
||||||
book_sequence = sorted(orders.keys(), key=itemgetter(1))
|
iter_sells,
|
||||||
|
itertools.repeat(sell_on_bid)
|
||||||
|
)
|
||||||
|
|
||||||
def pred(our_price):
|
# TODO: fix this block, though it definitely
|
||||||
return tick_price > our_price
|
# costs a lot more CPU-wise
|
||||||
|
# - doesn't seem like clears are happening still on
|
||||||
|
# "resting" limit orders?
|
||||||
|
case {
|
||||||
|
'price': tick_price,
|
||||||
|
'type': ('trade' | 'last'),
|
||||||
|
}:
|
||||||
|
# in the clearing price / last price case we
|
||||||
|
# want to iterate both sides of our book for
|
||||||
|
# clears since we don't know which direction the
|
||||||
|
# price is going to move (especially with HFT)
|
||||||
|
# and thus we simply interleave both sides (buys
|
||||||
|
# and sells) until one side clears and then
|
||||||
|
# break until the next tick?
|
||||||
|
def interleave():
|
||||||
|
for pair in zip(
|
||||||
|
iter_buys,
|
||||||
|
iter_sells,
|
||||||
|
):
|
||||||
|
for order_info, pred in zip(
|
||||||
|
pair,
|
||||||
|
itertools.cycle([buy_on_ask, sell_on_bid]),
|
||||||
|
):
|
||||||
|
yield order_info, pred
|
||||||
|
|
||||||
elif ttype in ('trade', 'last'):
|
iter_entries = interleave()
|
||||||
# TODO: simulate actual book queues and our orders
|
|
||||||
# place in it, might require full L2 data?
|
# NOTE: all other (non-clearable) tick event types
|
||||||
|
# - we don't want to sping the simulated clear loop
|
||||||
|
# below unecessarily and further don't want to pop
|
||||||
|
# simulated live orders prematurely.
|
||||||
|
case _:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# iterate book prices descending
|
# iterate all potentially clearable book prices
|
||||||
for oid, our_price in book_sequence:
|
# in FIFO order per side.
|
||||||
if pred(our_price):
|
for order_info, pred in iter_entries:
|
||||||
|
(our_price, size, reqid, action) = order_info
|
||||||
|
|
||||||
# retreive order info
|
# print(order_info)
|
||||||
(size, reqid, action) = orders.pop((oid, our_price))
|
clearable = pred(our_price)
|
||||||
|
if clearable:
|
||||||
|
# pop and retreive order info
|
||||||
|
oid = {
|
||||||
|
'buy': buys,
|
||||||
|
'sell': sells
|
||||||
|
}[action].inverse.pop(order_info)
|
||||||
|
|
||||||
# clearing price would have filled entirely
|
# clearing price would have filled entirely
|
||||||
await client.fake_fill(
|
await client.fake_fill(
|
||||||
symbol=sym,
|
fqsn=sym,
|
||||||
# todo slippage to determine fill price
|
# todo slippage to determine fill price
|
||||||
price=tick_price,
|
price=tick_price,
|
||||||
size=size,
|
size=size,
|
||||||
|
@ -368,9 +428,6 @@ async def simulate_fills(
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
oid=oid,
|
oid=oid,
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
# prices are iterated in sorted order so we're done
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
async def handle_order_requests(
|
async def handle_order_requests(
|
||||||
|
@ -380,68 +437,83 @@ async def handle_order_requests(
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
# order_request: dict
|
request_msg: dict
|
||||||
async for request_msg in ems_order_stream:
|
async for request_msg in ems_order_stream:
|
||||||
|
match request_msg:
|
||||||
action = request_msg['action']
|
case {'action': ('buy' | 'sell')}:
|
||||||
|
|
||||||
if action in {'buy', 'sell'}:
|
|
||||||
|
|
||||||
account = request_msg['account']
|
|
||||||
if account != 'paper':
|
|
||||||
log.error(
|
|
||||||
'This is a paper account, only a `paper` selection is valid'
|
|
||||||
)
|
|
||||||
await ems_order_stream.send(BrokerdError(
|
|
||||||
oid=request_msg['oid'],
|
|
||||||
symbol=request_msg['symbol'],
|
|
||||||
reason=f'Paper only. No account found: `{account}` ?',
|
|
||||||
).dict())
|
|
||||||
continue
|
|
||||||
|
|
||||||
# validate
|
|
||||||
order = BrokerdOrder(**request_msg)
|
order = BrokerdOrder(**request_msg)
|
||||||
|
account = order.account
|
||||||
|
|
||||||
# call our client api to submit the order
|
# error on bad inputs
|
||||||
reqid = await client.submit_limit(
|
reason = None
|
||||||
|
if account != 'paper':
|
||||||
|
reason = f'No account found:`{account}` (paper only)?'
|
||||||
|
|
||||||
|
elif order.size == 0:
|
||||||
|
reason = 'Invalid size: 0'
|
||||||
|
|
||||||
|
if reason:
|
||||||
|
log.error(reason)
|
||||||
|
await ems_order_stream.send(BrokerdError(
|
||||||
oid=order.oid,
|
oid=order.oid,
|
||||||
symbol=order.symbol,
|
symbol=order.symbol,
|
||||||
price=order.price,
|
reason=reason,
|
||||||
action=order.action,
|
))
|
||||||
size=order.size,
|
continue
|
||||||
|
|
||||||
# XXX: by default 0 tells ``ib_insync`` methods that
|
reqid = order.reqid or str(uuid.uuid4())
|
||||||
# there is no existing order so ask the client to create
|
|
||||||
# a new one (which it seems to do by allocating an int
|
|
||||||
# counter - collision prone..)
|
|
||||||
reqid=order.reqid,
|
|
||||||
)
|
|
||||||
|
|
||||||
# deliver ack that order has been submitted to broker routing
|
# deliver ack that order has been submitted to broker routing
|
||||||
await ems_order_stream.send(
|
await ems_order_stream.send(
|
||||||
BrokerdOrderAck(
|
BrokerdOrderAck(
|
||||||
|
|
||||||
# ems order request id
|
|
||||||
oid=order.oid,
|
oid=order.oid,
|
||||||
|
|
||||||
# broker specific request id
|
|
||||||
reqid=reqid,
|
reqid=reqid,
|
||||||
|
)
|
||||||
).dict()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
elif action == 'cancel':
|
# call our client api to submit the order
|
||||||
msg = BrokerdCancel(**request_msg)
|
reqid = await client.submit_limit(
|
||||||
|
oid=order.oid,
|
||||||
|
symbol=f'{order.symbol}.{client.broker}',
|
||||||
|
price=order.price,
|
||||||
|
action=order.action,
|
||||||
|
size=order.size,
|
||||||
|
# XXX: by default 0 tells ``ib_insync`` methods that
|
||||||
|
# there is no existing order so ask the client to create
|
||||||
|
# a new one (which it seems to do by allocating an int
|
||||||
|
# counter - collision prone..)
|
||||||
|
reqid=reqid,
|
||||||
|
)
|
||||||
|
log.info(f'Submitted paper LIMIT {reqid}:\n{order}')
|
||||||
|
|
||||||
|
case {'action': 'cancel'}:
|
||||||
|
msg = BrokerdCancel(**request_msg)
|
||||||
await client.submit_cancel(
|
await client.submit_cancel(
|
||||||
reqid=msg.reqid
|
reqid=msg.reqid
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
case _:
|
||||||
log.error(f'Unknown order command: {request_msg}')
|
log.error(f'Unknown order command: {request_msg}')
|
||||||
|
|
||||||
|
|
||||||
|
_reqids: bidict[str, tuple] = {}
|
||||||
|
_buys: defaultdict[
|
||||||
|
str, # symbol
|
||||||
|
bidict[
|
||||||
|
str, # oid
|
||||||
|
tuple[float, float, str, str], # order info
|
||||||
|
]
|
||||||
|
] = defaultdict(bidict)
|
||||||
|
_sells: defaultdict[
|
||||||
|
str, # symbol
|
||||||
|
bidict[
|
||||||
|
str, # oid
|
||||||
|
tuple[float, float, str, str], # order info
|
||||||
|
]
|
||||||
|
] = defaultdict(bidict)
|
||||||
|
_positions: dict[str, Position] = {}
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def trades_dialogue(
|
async def trades_dialogue(
|
||||||
|
|
||||||
|
@ -451,42 +523,62 @@ async def trades_dialogue(
|
||||||
loglevel: str = None,
|
loglevel: str = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
tractor.log.get_console_log(loglevel)
|
tractor.log.get_console_log(loglevel)
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
|
|
||||||
data.open_feed(
|
data.open_feed(
|
||||||
[fqsn],
|
[fqsn],
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
) as feed,
|
) as feed,
|
||||||
|
|
||||||
):
|
):
|
||||||
|
pp_msgs: list[BrokerdPosition] = []
|
||||||
|
pos: Position
|
||||||
|
token: str # f'{symbol}.{self.broker}'
|
||||||
|
for token, pos in _positions.items():
|
||||||
|
pp_msgs.append(BrokerdPosition(
|
||||||
|
broker=broker,
|
||||||
|
account='paper',
|
||||||
|
symbol=pos.symbol.front_fqsn(),
|
||||||
|
size=pos.size,
|
||||||
|
avg_price=pos.ppu,
|
||||||
|
))
|
||||||
|
|
||||||
# TODO: load paper positions per broker from .toml config file
|
# TODO: load paper positions per broker from .toml config file
|
||||||
# and pass as symbol to position data mapping: ``dict[str, dict]``
|
# and pass as symbol to position data mapping: ``dict[str, dict]``
|
||||||
# await ctx.started(all_positions)
|
await ctx.started((
|
||||||
await ctx.started(({}, {'paper',}))
|
pp_msgs,
|
||||||
|
['paper'],
|
||||||
|
))
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
ctx.open_stream() as ems_stream,
|
ctx.open_stream() as ems_stream,
|
||||||
trio.open_nursery() as n,
|
trio.open_nursery() as n,
|
||||||
):
|
):
|
||||||
|
|
||||||
client = PaperBoi(
|
client = PaperBoi(
|
||||||
broker,
|
broker,
|
||||||
ems_stream,
|
ems_stream,
|
||||||
_buys={},
|
_buys=_buys,
|
||||||
_sells={},
|
_sells=_sells,
|
||||||
|
|
||||||
_reqids={},
|
_reqids=_reqids,
|
||||||
|
|
||||||
# TODO: load paper positions from ``positions.toml``
|
# TODO: load paper positions from ``positions.toml``
|
||||||
_positions={},
|
_positions=_positions,
|
||||||
|
|
||||||
|
# TODO: load postions from ledger file
|
||||||
|
_trade_ledger={},
|
||||||
)
|
)
|
||||||
|
|
||||||
n.start_soon(handle_order_requests, client, ems_stream)
|
n.start_soon(
|
||||||
|
handle_order_requests,
|
||||||
|
client,
|
||||||
|
ems_stream,
|
||||||
|
)
|
||||||
|
|
||||||
# paper engine simulator clearing task
|
# paper engine simulator clearing task
|
||||||
await simulate_fills(feed.stream, client)
|
await simulate_fills(feed.streams[broker], client)
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
|
@ -511,6 +603,7 @@ async def open_paperboi(
|
||||||
# (we likely don't need more then one proc for basic
|
# (we likely don't need more then one proc for basic
|
||||||
# simulated order clearing)
|
# simulated order clearing)
|
||||||
if portal is None:
|
if portal is None:
|
||||||
|
log.info('Starting new paper-engine actor')
|
||||||
portal = await tn.start_actor(
|
portal = await tn.start_actor(
|
||||||
service_name,
|
service_name,
|
||||||
enable_modules=[__name__]
|
enable_modules=[__name__]
|
||||||
|
@ -523,5 +616,4 @@ async def open_paperboi(
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
|
|
||||||
) as (ctx, first):
|
) as (ctx, first):
|
||||||
|
|
||||||
yield ctx, first
|
yield ctx, first
|
||||||
|
|
|
@ -27,25 +27,35 @@ import tractor
|
||||||
|
|
||||||
from ..log import get_console_log, get_logger, colorize_json
|
from ..log import get_console_log, get_logger, colorize_json
|
||||||
from ..brokers import get_brokermod
|
from ..brokers import get_brokermod
|
||||||
from .._daemon import _tractor_kwargs
|
from .._daemon import (
|
||||||
|
_default_registry_host,
|
||||||
|
_default_registry_port,
|
||||||
|
)
|
||||||
from .. import config
|
from .. import config
|
||||||
|
|
||||||
|
|
||||||
log = get_logger('cli')
|
log = get_logger('cli')
|
||||||
DEFAULT_BROKER = 'questrade'
|
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
||||||
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
||||||
@click.option('--pdb', is_flag=True, help='Enable tractor debug mode')
|
@click.option('--pdb', is_flag=True, help='Enable tractor debug mode')
|
||||||
@click.option('--host', '-h', default='127.0.0.1', help='Host address to bind')
|
@click.option('--host', '-h', default=None, help='Host addr to bind')
|
||||||
|
@click.option('--port', '-p', default=None, help='Port number to bind')
|
||||||
@click.option(
|
@click.option(
|
||||||
'--tsdb',
|
'--tsdb',
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
help='Enable local ``marketstore`` instance'
|
help='Enable local ``marketstore`` instance'
|
||||||
)
|
)
|
||||||
def pikerd(loglevel, host, tl, pdb, tsdb):
|
def pikerd(
|
||||||
|
loglevel: str,
|
||||||
|
host: str,
|
||||||
|
port: int,
|
||||||
|
tl: bool,
|
||||||
|
pdb: bool,
|
||||||
|
tsdb: bool,
|
||||||
|
):
|
||||||
'''
|
'''
|
||||||
Spawn the piker broker-daemon.
|
Spawn the piker broker-daemon.
|
||||||
|
|
||||||
|
@ -62,12 +72,21 @@ def pikerd(loglevel, host, tl, pdb, tsdb):
|
||||||
"\n"
|
"\n"
|
||||||
))
|
))
|
||||||
|
|
||||||
|
reg_addr: None | tuple[str, int] = None
|
||||||
|
if host or port:
|
||||||
|
reg_addr = (
|
||||||
|
host or _default_registry_host,
|
||||||
|
int(port) or _default_registry_port,
|
||||||
|
)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
open_pikerd(
|
open_pikerd(
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
debug_mode=pdb,
|
debug_mode=pdb,
|
||||||
|
registry_addr=reg_addr,
|
||||||
|
|
||||||
), # normally delivers a ``Services`` handle
|
), # normally delivers a ``Services`` handle
|
||||||
trio.open_nursery() as n,
|
trio.open_nursery() as n,
|
||||||
):
|
):
|
||||||
|
@ -83,9 +102,9 @@ def pikerd(loglevel, host, tl, pdb, tsdb):
|
||||||
|
|
||||||
)
|
)
|
||||||
log.info(
|
log.info(
|
||||||
f'`marketstore` up!\n'
|
f'`marketstored` up!\n'
|
||||||
f'`marketstored` pid: {pid}\n'
|
f'pid: {pid}\n'
|
||||||
f'docker container id: {cid}\n'
|
f'container id: {cid[:12]}\n'
|
||||||
f'config: {pformat(config)}'
|
f'config: {pformat(config)}'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -97,25 +116,46 @@ def pikerd(loglevel, host, tl, pdb, tsdb):
|
||||||
@click.group(context_settings=config._context_defaults)
|
@click.group(context_settings=config._context_defaults)
|
||||||
@click.option(
|
@click.option(
|
||||||
'--brokers', '-b',
|
'--brokers', '-b',
|
||||||
default=[DEFAULT_BROKER],
|
default=None,
|
||||||
multiple=True,
|
multiple=True,
|
||||||
help='Broker backend to use'
|
help='Broker backend to use'
|
||||||
)
|
)
|
||||||
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
||||||
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
||||||
@click.option('--configdir', '-c', help='Configuration directory')
|
@click.option('--configdir', '-c', help='Configuration directory')
|
||||||
|
@click.option('--host', '-h', default=None, help='Host addr to bind')
|
||||||
|
@click.option('--port', '-p', default=None, help='Port number to bind')
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def cli(ctx, brokers, loglevel, tl, configdir):
|
def cli(
|
||||||
|
ctx: click.Context,
|
||||||
|
brokers: list[str],
|
||||||
|
loglevel: str,
|
||||||
|
tl: bool,
|
||||||
|
configdir: str,
|
||||||
|
host: str,
|
||||||
|
port: int,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
if configdir is not None:
|
if configdir is not None:
|
||||||
assert os.path.isdir(configdir), f"`{configdir}` is not a valid path"
|
assert os.path.isdir(configdir), f"`{configdir}` is not a valid path"
|
||||||
config._override_config_dir(configdir)
|
config._override_config_dir(configdir)
|
||||||
|
|
||||||
ctx.ensure_object(dict)
|
ctx.ensure_object(dict)
|
||||||
|
|
||||||
if len(brokers) == 1:
|
if not brokers:
|
||||||
brokermods = [get_brokermod(brokers[0])]
|
# (try to) load all (supposedly) supported data/broker backends
|
||||||
else:
|
from piker.brokers import __brokers__
|
||||||
|
brokers = __brokers__
|
||||||
|
|
||||||
brokermods = [get_brokermod(broker) for broker in brokers]
|
brokermods = [get_brokermod(broker) for broker in brokers]
|
||||||
|
assert brokermods
|
||||||
|
|
||||||
|
reg_addr: None | tuple[str, int] = None
|
||||||
|
if host or port:
|
||||||
|
reg_addr = (
|
||||||
|
host or _default_registry_host,
|
||||||
|
int(port) or _default_registry_port,
|
||||||
|
)
|
||||||
|
|
||||||
ctx.obj.update({
|
ctx.obj.update({
|
||||||
'brokers': brokers,
|
'brokers': brokers,
|
||||||
|
@ -125,6 +165,7 @@ def cli(ctx, brokers, loglevel, tl, configdir):
|
||||||
'log': get_console_log(loglevel),
|
'log': get_console_log(loglevel),
|
||||||
'confdir': config._config_dir,
|
'confdir': config._config_dir,
|
||||||
'wl_path': config._watchlists_data_path,
|
'wl_path': config._watchlists_data_path,
|
||||||
|
'registry_addr': reg_addr,
|
||||||
})
|
})
|
||||||
|
|
||||||
# allow enabling same loglevel in ``tractor`` machinery
|
# allow enabling same loglevel in ``tractor`` machinery
|
||||||
|
@ -134,29 +175,40 @@ def cli(ctx, brokers, loglevel, tl, configdir):
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
||||||
@click.argument('names', nargs=-1, required=False)
|
@click.argument('ports', nargs=-1, required=False)
|
||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
def services(config, tl, names):
|
def services(config, tl, ports):
|
||||||
|
|
||||||
|
from .._daemon import (
|
||||||
|
open_piker_runtime,
|
||||||
|
_default_registry_port,
|
||||||
|
_default_registry_host,
|
||||||
|
)
|
||||||
|
|
||||||
|
host = _default_registry_host
|
||||||
|
if not ports:
|
||||||
|
ports = [_default_registry_port]
|
||||||
|
|
||||||
async def list_services():
|
async def list_services():
|
||||||
|
nonlocal host
|
||||||
async with tractor.get_arbiter(
|
async with (
|
||||||
*_tractor_kwargs['arbiter_addr']
|
open_piker_runtime(
|
||||||
) as portal:
|
name='service_query',
|
||||||
|
loglevel=config['loglevel'] if tl else None,
|
||||||
|
),
|
||||||
|
tractor.get_arbiter(
|
||||||
|
host=host,
|
||||||
|
port=ports[0]
|
||||||
|
) as portal
|
||||||
|
):
|
||||||
registry = await portal.run_from_ns('self', 'get_registry')
|
registry = await portal.run_from_ns('self', 'get_registry')
|
||||||
json_d = {}
|
json_d = {}
|
||||||
for key, socket in registry.items():
|
for key, socket in registry.items():
|
||||||
# name, uuid = uid
|
|
||||||
host, port = socket
|
host, port = socket
|
||||||
json_d[key] = f'{host}:{port}'
|
json_d[key] = f'{host}:{port}'
|
||||||
click.echo(f"{colorize_json(json_d)}")
|
click.echo(f"{colorize_json(json_d)}")
|
||||||
|
|
||||||
tractor.run(
|
trio.run(list_services)
|
||||||
list_services,
|
|
||||||
name='service_query',
|
|
||||||
loglevel=config['loglevel'] if tl else None,
|
|
||||||
arbiter_addr=_tractor_kwargs['arbiter_addr'],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _load_clis() -> None:
|
def _load_clis() -> None:
|
||||||
|
|
|
@ -21,6 +21,7 @@ Broker configuration mgmt.
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
from os import path
|
||||||
from os.path import dirname
|
from os.path import dirname
|
||||||
import shutil
|
import shutil
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
@ -111,6 +112,7 @@ if _parent_user:
|
||||||
|
|
||||||
_conf_names: set[str] = {
|
_conf_names: set[str] = {
|
||||||
'brokers',
|
'brokers',
|
||||||
|
'pps',
|
||||||
'trades',
|
'trades',
|
||||||
'watchlists',
|
'watchlists',
|
||||||
}
|
}
|
||||||
|
@ -147,19 +149,21 @@ def get_conf_path(
|
||||||
conf_name: str = 'brokers',
|
conf_name: str = 'brokers',
|
||||||
|
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Return the default config path normally under
|
'''
|
||||||
``~/.config/piker`` on linux.
|
Return the top-level default config path normally under
|
||||||
|
``~/.config/piker`` on linux for a given ``conf_name``, the config
|
||||||
|
name.
|
||||||
|
|
||||||
Contains files such as:
|
Contains files such as:
|
||||||
- brokers.toml
|
- brokers.toml
|
||||||
|
- pp.toml
|
||||||
- watchlists.toml
|
- watchlists.toml
|
||||||
- trades.toml
|
|
||||||
|
|
||||||
# maybe coming soon ;)
|
# maybe coming soon ;)
|
||||||
- signals.toml
|
- signals.toml
|
||||||
- strats.toml
|
- strats.toml
|
||||||
|
|
||||||
"""
|
'''
|
||||||
assert conf_name in _conf_names
|
assert conf_name in _conf_names
|
||||||
fn = _conf_fn_w_ext(conf_name)
|
fn = _conf_fn_w_ext(conf_name)
|
||||||
return os.path.join(
|
return os.path.join(
|
||||||
|
@ -173,7 +177,7 @@ def repodir():
|
||||||
Return the abspath to the repo directory.
|
Return the abspath to the repo directory.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
dirpath = os.path.abspath(
|
dirpath = path.abspath(
|
||||||
# we're 3 levels down in **this** module file
|
# we're 3 levels down in **this** module file
|
||||||
dirname(dirname(os.path.realpath(__file__)))
|
dirname(dirname(os.path.realpath(__file__)))
|
||||||
)
|
)
|
||||||
|
@ -182,7 +186,9 @@ def repodir():
|
||||||
|
|
||||||
def load(
|
def load(
|
||||||
conf_name: str = 'brokers',
|
conf_name: str = 'brokers',
|
||||||
path: str = None
|
path: str = None,
|
||||||
|
|
||||||
|
**tomlkws,
|
||||||
|
|
||||||
) -> (dict, str):
|
) -> (dict, str):
|
||||||
'''
|
'''
|
||||||
|
@ -190,6 +196,10 @@ def load(
|
||||||
|
|
||||||
'''
|
'''
|
||||||
path = path or get_conf_path(conf_name)
|
path = path or get_conf_path(conf_name)
|
||||||
|
|
||||||
|
if not os.path.isdir(_config_dir):
|
||||||
|
os.mkdir(_config_dir)
|
||||||
|
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
fn = _conf_fn_w_ext(conf_name)
|
fn = _conf_fn_w_ext(conf_name)
|
||||||
|
|
||||||
|
@ -202,8 +212,11 @@ def load(
|
||||||
# if one exists.
|
# if one exists.
|
||||||
if os.path.isfile(template):
|
if os.path.isfile(template):
|
||||||
shutil.copyfile(template, path)
|
shutil.copyfile(template, path)
|
||||||
|
else:
|
||||||
|
with open(path, 'r'):
|
||||||
|
pass # touch it
|
||||||
|
|
||||||
config = toml.load(path)
|
config = toml.load(path, **tomlkws)
|
||||||
log.debug(f"Read config file {path}")
|
log.debug(f"Read config file {path}")
|
||||||
return config, path
|
return config, path
|
||||||
|
|
||||||
|
@ -212,6 +225,7 @@ def write(
|
||||||
config: dict, # toml config as dict
|
config: dict, # toml config as dict
|
||||||
name: str = 'brokers',
|
name: str = 'brokers',
|
||||||
path: str = None,
|
path: str = None,
|
||||||
|
**toml_kwargs,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
''''
|
''''
|
||||||
|
@ -235,11 +249,14 @@ def write(
|
||||||
f"{path}"
|
f"{path}"
|
||||||
)
|
)
|
||||||
with open(path, 'w') as cf:
|
with open(path, 'w') as cf:
|
||||||
return toml.dump(config, cf)
|
return toml.dump(
|
||||||
|
config,
|
||||||
|
cf,
|
||||||
|
**toml_kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def load_accounts(
|
def load_accounts(
|
||||||
|
|
||||||
providers: Optional[list[str]] = None
|
providers: Optional[list[str]] = None
|
||||||
|
|
||||||
) -> bidict[str, Optional[str]]:
|
) -> bidict[str, Optional[str]]:
|
||||||
|
|
|
@ -22,6 +22,12 @@ and storing data from your brokers as well as
|
||||||
sharing live streams over a network.
|
sharing live streams over a network.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
import tractor
|
||||||
|
import trio
|
||||||
|
|
||||||
|
from ..log import (
|
||||||
|
get_console_log,
|
||||||
|
)
|
||||||
from ._normalize import iterticks
|
from ._normalize import iterticks
|
||||||
from ._sharedmem import (
|
from ._sharedmem import (
|
||||||
maybe_open_shm_array,
|
maybe_open_shm_array,
|
||||||
|
@ -32,7 +38,6 @@ from ._sharedmem import (
|
||||||
)
|
)
|
||||||
from .feed import (
|
from .feed import (
|
||||||
open_feed,
|
open_feed,
|
||||||
_setup_persistent_brokerd,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,5 +49,40 @@ __all__ = [
|
||||||
'attach_shm_array',
|
'attach_shm_array',
|
||||||
'open_shm_array',
|
'open_shm_array',
|
||||||
'get_shm_token',
|
'get_shm_token',
|
||||||
'_setup_persistent_brokerd',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@tractor.context
|
||||||
|
async def _setup_persistent_brokerd(
|
||||||
|
ctx: tractor.Context,
|
||||||
|
brokername: str,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Allocate a actor-wide service nursery in ``brokerd``
|
||||||
|
such that feeds can be run in the background persistently by
|
||||||
|
the broker backend as needed.
|
||||||
|
|
||||||
|
'''
|
||||||
|
get_console_log(tractor.current_actor().loglevel)
|
||||||
|
|
||||||
|
from .feed import (
|
||||||
|
_bus,
|
||||||
|
get_feed_bus,
|
||||||
|
)
|
||||||
|
global _bus
|
||||||
|
assert not _bus
|
||||||
|
|
||||||
|
async with trio.open_nursery() as service_nursery:
|
||||||
|
# assign a nursery to the feeds bus for spawning
|
||||||
|
# background tasks from clients
|
||||||
|
get_feed_bus(brokername, service_nursery)
|
||||||
|
|
||||||
|
# unblock caller
|
||||||
|
await ctx.started()
|
||||||
|
|
||||||
|
# we pin this task to keep the feeds manager active until the
|
||||||
|
# parent actor decides to tear it down
|
||||||
|
await trio.sleep_forever()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -37,8 +37,13 @@ from docker.models.containers import Container as DockerContainer
|
||||||
from docker.errors import (
|
from docker.errors import (
|
||||||
DockerException,
|
DockerException,
|
||||||
APIError,
|
APIError,
|
||||||
|
# ContainerError,
|
||||||
|
)
|
||||||
|
import requests
|
||||||
|
from requests.exceptions import (
|
||||||
|
ConnectionError,
|
||||||
|
ReadTimeout,
|
||||||
)
|
)
|
||||||
from requests.exceptions import ConnectionError, ReadTimeout
|
|
||||||
|
|
||||||
from ..log import get_logger, get_console_log
|
from ..log import get_logger, get_console_log
|
||||||
from .. import config
|
from .. import config
|
||||||
|
@ -50,8 +55,8 @@ class DockerNotStarted(Exception):
|
||||||
'Prolly you dint start da daemon bruh'
|
'Prolly you dint start da daemon bruh'
|
||||||
|
|
||||||
|
|
||||||
class ContainerError(RuntimeError):
|
class ApplicationLogError(Exception):
|
||||||
'Error reported via app-container logging level'
|
'App in container reported an error in logs'
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
|
@ -96,9 +101,9 @@ async def open_docker(
|
||||||
# not perms?
|
# not perms?
|
||||||
raise
|
raise
|
||||||
|
|
||||||
finally:
|
# finally:
|
||||||
if client:
|
# if client:
|
||||||
client.close()
|
# client.close()
|
||||||
|
|
||||||
|
|
||||||
class Container:
|
class Container:
|
||||||
|
@ -156,7 +161,7 @@ class Container:
|
||||||
|
|
||||||
# print(f'level: {level}')
|
# print(f'level: {level}')
|
||||||
if level in ('error', 'fatal'):
|
if level in ('error', 'fatal'):
|
||||||
raise ContainerError(msg)
|
raise ApplicationLogError(msg)
|
||||||
|
|
||||||
if patt in msg:
|
if patt in msg:
|
||||||
return True
|
return True
|
||||||
|
@ -185,12 +190,29 @@ class Container:
|
||||||
if 'is not running' in err.explanation:
|
if 'is not running' in err.explanation:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def hard_kill(self, start: float) -> None:
|
||||||
|
delay = time.time() - start
|
||||||
|
# get out the big guns, bc apparently marketstore
|
||||||
|
# doesn't actually know how to terminate gracefully
|
||||||
|
# :eyeroll:...
|
||||||
|
log.error(
|
||||||
|
f'SIGKILL-ing: {self.cntr.id} after {delay}s\n'
|
||||||
|
)
|
||||||
|
self.try_signal('SIGKILL')
|
||||||
|
self.cntr.wait(
|
||||||
|
timeout=3,
|
||||||
|
condition='not-running',
|
||||||
|
)
|
||||||
|
|
||||||
async def cancel(
|
async def cancel(
|
||||||
self,
|
self,
|
||||||
stop_msg: str,
|
stop_msg: str,
|
||||||
|
hard_kill: bool = False,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
cid = self.cntr.id
|
cid = self.cntr.id
|
||||||
|
|
||||||
# first try a graceful cancel
|
# first try a graceful cancel
|
||||||
log.cancel(
|
log.cancel(
|
||||||
f'SIGINT cancelling container: {cid}\n'
|
f'SIGINT cancelling container: {cid}\n'
|
||||||
|
@ -199,16 +221,26 @@ class Container:
|
||||||
self.try_signal('SIGINT')
|
self.try_signal('SIGINT')
|
||||||
|
|
||||||
start = time.time()
|
start = time.time()
|
||||||
for _ in range(30):
|
for _ in range(6):
|
||||||
|
|
||||||
with trio.move_on_after(0.5) as cs:
|
with trio.move_on_after(0.5) as cs:
|
||||||
cs.shield = True
|
log.cancel('polling for CNTR logs...')
|
||||||
await self.process_logs_until(stop_msg)
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
await self.process_logs_until(stop_msg)
|
||||||
|
except ApplicationLogError:
|
||||||
|
hard_kill = True
|
||||||
|
else:
|
||||||
# if we aren't cancelled on above checkpoint then we
|
# if we aren't cancelled on above checkpoint then we
|
||||||
# assume we read the expected stop msg and terminated.
|
# assume we read the expected stop msg and
|
||||||
|
# terminated.
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if cs.cancelled_caught:
|
||||||
|
# on timeout just try a hard kill after
|
||||||
|
# a quick container sync-wait.
|
||||||
|
hard_kill = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
log.info(f'Polling for container shutdown:\n{cid}')
|
log.info(f'Polling for container shutdown:\n{cid}')
|
||||||
|
|
||||||
|
@ -218,6 +250,7 @@ class Container:
|
||||||
condition='not-running',
|
condition='not-running',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# graceful exit if we didn't time out
|
||||||
break
|
break
|
||||||
|
|
||||||
except (
|
except (
|
||||||
|
@ -229,24 +262,22 @@ class Container:
|
||||||
except (
|
except (
|
||||||
docker.errors.APIError,
|
docker.errors.APIError,
|
||||||
ConnectionError,
|
ConnectionError,
|
||||||
|
requests.exceptions.ConnectionError,
|
||||||
|
trio.Cancelled,
|
||||||
):
|
):
|
||||||
log.exception('Docker connection failure')
|
log.exception('Docker connection failure')
|
||||||
break
|
self.hard_kill(start)
|
||||||
else:
|
raise
|
||||||
delay = time.time() - start
|
|
||||||
log.error(
|
|
||||||
f'Failed to kill container {cid} after {delay}s\n'
|
|
||||||
'sending SIGKILL..'
|
|
||||||
)
|
|
||||||
# get out the big guns, bc apparently marketstore
|
|
||||||
# doesn't actually know how to terminate gracefully
|
|
||||||
# :eyeroll:...
|
|
||||||
self.try_signal('SIGKILL')
|
|
||||||
self.cntr.wait(
|
|
||||||
timeout=3,
|
|
||||||
condition='not-running',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
except trio.Cancelled:
|
||||||
|
log.exception('trio cancelled...')
|
||||||
|
self.hard_kill(start)
|
||||||
|
else:
|
||||||
|
hard_kill = True
|
||||||
|
|
||||||
|
if hard_kill:
|
||||||
|
self.hard_kill(start)
|
||||||
|
else:
|
||||||
log.cancel(f'Container stopped: {cid}')
|
log.cancel(f'Container stopped: {cid}')
|
||||||
|
|
||||||
|
|
||||||
|
@ -289,14 +320,12 @@ async def open_ahabd(
|
||||||
))
|
))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# TODO: we might eventually want a proxy-style msg-prot here
|
# TODO: we might eventually want a proxy-style msg-prot here
|
||||||
# to allow remote control of containers without needing
|
# to allow remote control of containers without needing
|
||||||
# callers to have root perms?
|
# callers to have root perms?
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
with trio.CancelScope(shield=True):
|
|
||||||
await cntr.cancel(stop_msg)
|
await cntr.cancel(stop_msg)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,846 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
Pre-(path)-graphics formatted x/y nd/1d rendering subsystem.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
|
import msgspec
|
||||||
|
from msgspec import field
|
||||||
|
import numpy as np
|
||||||
|
from numpy.lib import recfunctions as rfn
|
||||||
|
|
||||||
|
from ._sharedmem import (
|
||||||
|
ShmArray,
|
||||||
|
)
|
||||||
|
from ._pathops import (
|
||||||
|
path_arrays_from_ohlc,
|
||||||
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._dataviz import (
|
||||||
|
Viz,
|
||||||
|
)
|
||||||
|
from .._profile import Profiler
|
||||||
|
|
||||||
|
|
||||||
|
class IncrementalFormatter(msgspec.Struct):
|
||||||
|
'''
|
||||||
|
Incrementally updating, pre-path-graphics tracking, formatter.
|
||||||
|
|
||||||
|
Allows tracking source data state in an updateable pre-graphics
|
||||||
|
``np.ndarray`` format (in local process memory) as well as
|
||||||
|
incrementally rendering from that format **to** 1d x/y for path
|
||||||
|
generation using ``pg.functions.arrayToQPath()``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
shm: ShmArray
|
||||||
|
viz: Viz
|
||||||
|
|
||||||
|
@property
|
||||||
|
def index_field(self) -> 'str':
|
||||||
|
'''
|
||||||
|
Value (``str``) used to look up the "index series" from the
|
||||||
|
underlying source ``numpy`` struct-array; delegate directly to
|
||||||
|
the managing ``Viz``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.viz.index_field
|
||||||
|
|
||||||
|
# Incrementally updated xy ndarray formatted data, a pre-1d
|
||||||
|
# format which is updated and cached independently of the final
|
||||||
|
# pre-graphics-path 1d format.
|
||||||
|
x_nd: Optional[np.ndarray] = None
|
||||||
|
y_nd: Optional[np.ndarray] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def xy_nd(self) -> tuple[np.ndarray, np.ndarray]:
|
||||||
|
return (
|
||||||
|
self.x_nd[self.xy_slice],
|
||||||
|
self.y_nd[self.xy_slice],
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def xy_slice(self) -> slice:
|
||||||
|
return slice(
|
||||||
|
self.xy_nd_start,
|
||||||
|
self.xy_nd_stop,
|
||||||
|
)
|
||||||
|
|
||||||
|
# indexes which slice into the above arrays (which are allocated
|
||||||
|
# based on source data shm input size) and allow retrieving
|
||||||
|
# incrementally updated data.
|
||||||
|
xy_nd_start: int | None = None
|
||||||
|
xy_nd_stop: int | None = None
|
||||||
|
|
||||||
|
# TODO: eventually incrementally update 1d-pre-graphics path data?
|
||||||
|
# x_1d: Optional[np.ndarray] = None
|
||||||
|
# y_1d: Optional[np.ndarray] = None
|
||||||
|
|
||||||
|
# incremental view-change state(s) tracking
|
||||||
|
_last_vr: tuple[float, float] | None = None
|
||||||
|
_last_ivdr: tuple[float, float] | None = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def index_step_size(self) -> float:
|
||||||
|
'''
|
||||||
|
Readonly value computed on first ``.diff()`` call.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.viz.index_step()
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
msg = (
|
||||||
|
f'{type(self)}: ->\n\n'
|
||||||
|
f'fqsn={self.viz.name}\n'
|
||||||
|
f'shm_name={self.shm.token["shm_name"]}\n\n'
|
||||||
|
|
||||||
|
f'last_vr={self._last_vr}\n'
|
||||||
|
f'last_ivdr={self._last_ivdr}\n\n'
|
||||||
|
|
||||||
|
f'xy_slice={self.xy_slice}\n'
|
||||||
|
# f'xy_nd_stop={self.xy_nd_stop}\n\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
x_nd_len = 0
|
||||||
|
y_nd_len = 0
|
||||||
|
if self.x_nd is not None:
|
||||||
|
x_nd_len = len(self.x_nd)
|
||||||
|
y_nd_len = len(self.y_nd)
|
||||||
|
|
||||||
|
msg += (
|
||||||
|
f'x_nd_len={x_nd_len}\n'
|
||||||
|
f'y_nd_len={y_nd_len}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
return msg
|
||||||
|
|
||||||
|
def diff(
|
||||||
|
self,
|
||||||
|
new_read: tuple[np.ndarray],
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
]:
|
||||||
|
# TODO:
|
||||||
|
# - can the renderer just call ``Viz.read()`` directly? unpack
|
||||||
|
# latest source data read
|
||||||
|
# - eventually maybe we can implement some kind of
|
||||||
|
# transform on the ``QPainterPath`` that will more or less
|
||||||
|
# detect the diff in "elements" terms? update diff state since
|
||||||
|
# we've now rendered paths.
|
||||||
|
(
|
||||||
|
xfirst,
|
||||||
|
xlast,
|
||||||
|
array,
|
||||||
|
ivl,
|
||||||
|
ivr,
|
||||||
|
in_view,
|
||||||
|
) = new_read
|
||||||
|
|
||||||
|
index = array['index']
|
||||||
|
|
||||||
|
# if the first index in the read array is 0 then
|
||||||
|
# it means the source buffer has bee completely backfilled to
|
||||||
|
# available space.
|
||||||
|
src_start = index[0]
|
||||||
|
src_stop = index[-1] + 1
|
||||||
|
|
||||||
|
# these are the "formatted output data" indices
|
||||||
|
# for the pre-graphics arrays.
|
||||||
|
nd_start = self.xy_nd_start
|
||||||
|
nd_stop = self.xy_nd_stop
|
||||||
|
|
||||||
|
if (
|
||||||
|
nd_start is None
|
||||||
|
):
|
||||||
|
assert nd_stop is None
|
||||||
|
|
||||||
|
# setup to do a prepend of all existing src history
|
||||||
|
nd_start = self.xy_nd_start = src_stop
|
||||||
|
# set us in a zero-to-append state
|
||||||
|
nd_stop = self.xy_nd_stop = src_stop
|
||||||
|
|
||||||
|
align_index = array[self.index_field]
|
||||||
|
|
||||||
|
# compute the length diffs between the first/last index entry in
|
||||||
|
# the input data and the last indexes we have on record from the
|
||||||
|
# last time we updated the curve index.
|
||||||
|
prepend_length = int(nd_start - src_start)
|
||||||
|
append_length = int(src_stop - nd_stop)
|
||||||
|
|
||||||
|
# blah blah blah
|
||||||
|
# do diffing for prepend, append and last entry
|
||||||
|
return (
|
||||||
|
slice(src_start, nd_start),
|
||||||
|
prepend_length,
|
||||||
|
append_length,
|
||||||
|
slice(nd_stop, src_stop),
|
||||||
|
)
|
||||||
|
|
||||||
|
def _track_inview_range(
|
||||||
|
self,
|
||||||
|
view_range: tuple[int, int],
|
||||||
|
|
||||||
|
) -> bool:
|
||||||
|
# if a view range is passed, plan to draw the
|
||||||
|
# source ouput that's "in view" of the chart.
|
||||||
|
vl, vr = view_range
|
||||||
|
zoom_or_append = False
|
||||||
|
last_vr = self._last_vr
|
||||||
|
|
||||||
|
# incremental in-view data update.
|
||||||
|
if last_vr:
|
||||||
|
lvl, lvr = last_vr # relative slice indices
|
||||||
|
|
||||||
|
# TODO: detecting more specifically the interaction changes
|
||||||
|
# last_ivr = self._last_ivdr or (vl, vr)
|
||||||
|
# al, ar = last_ivr # abs slice indices
|
||||||
|
# left_change = abs(x_iv[0] - al) >= 1
|
||||||
|
# right_change = abs(x_iv[-1] - ar) >= 1
|
||||||
|
|
||||||
|
# likely a zoom/pan view change or data append update
|
||||||
|
if (
|
||||||
|
(vr - lvr) > 2
|
||||||
|
or vl < lvl
|
||||||
|
|
||||||
|
# append / prepend update
|
||||||
|
# we had an append update where the view range
|
||||||
|
# didn't change but the data-viewed (shifted)
|
||||||
|
# underneath, so we need to redraw.
|
||||||
|
# or left_change and right_change and last_vr == view_range
|
||||||
|
|
||||||
|
# not (left_change and right_change) and ivr
|
||||||
|
# (
|
||||||
|
# or abs(x_iv[ivr] - livr) > 1
|
||||||
|
):
|
||||||
|
zoom_or_append = True
|
||||||
|
|
||||||
|
self._last_vr = view_range
|
||||||
|
|
||||||
|
return zoom_or_append
|
||||||
|
|
||||||
|
def format_to_1d(
|
||||||
|
self,
|
||||||
|
new_read: tuple,
|
||||||
|
array_key: str,
|
||||||
|
profiler: Profiler,
|
||||||
|
|
||||||
|
slice_to_inview: bool = True,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
]:
|
||||||
|
shm = self.shm
|
||||||
|
|
||||||
|
(
|
||||||
|
_,
|
||||||
|
_,
|
||||||
|
array,
|
||||||
|
ivl,
|
||||||
|
ivr,
|
||||||
|
in_view,
|
||||||
|
|
||||||
|
) = new_read
|
||||||
|
|
||||||
|
(
|
||||||
|
pre_slice,
|
||||||
|
prepend_len,
|
||||||
|
append_len,
|
||||||
|
post_slice,
|
||||||
|
) = self.diff(new_read)
|
||||||
|
|
||||||
|
# we first need to allocate xy data arrays
|
||||||
|
# from the source data.
|
||||||
|
if self.y_nd is None:
|
||||||
|
self.xy_nd_start = shm._first.value
|
||||||
|
self.xy_nd_stop = shm._last.value
|
||||||
|
self.x_nd, self.y_nd = self.allocate_xy_nd(
|
||||||
|
shm,
|
||||||
|
array_key,
|
||||||
|
)
|
||||||
|
profiler('allocated xy history')
|
||||||
|
|
||||||
|
# once allocated we do incremental pre/append
|
||||||
|
# updates from the diff with the source buffer.
|
||||||
|
else:
|
||||||
|
if prepend_len:
|
||||||
|
|
||||||
|
self.incr_update_xy_nd(
|
||||||
|
shm,
|
||||||
|
array_key,
|
||||||
|
|
||||||
|
# this is the pre-sliced, "normally expected"
|
||||||
|
# new data that an updater would normally be
|
||||||
|
# expected to process, however in some cases (like
|
||||||
|
# step curves) the updater routine may want to do
|
||||||
|
# the source history-data reading itself, so we pass
|
||||||
|
# both here.
|
||||||
|
shm._array[pre_slice],
|
||||||
|
pre_slice,
|
||||||
|
prepend_len,
|
||||||
|
|
||||||
|
self.xy_nd_start,
|
||||||
|
self.xy_nd_stop,
|
||||||
|
is_append=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.xy_nd_start -= prepend_len
|
||||||
|
profiler('prepended xy history: {prepend_length}')
|
||||||
|
|
||||||
|
if append_len:
|
||||||
|
self.incr_update_xy_nd(
|
||||||
|
shm,
|
||||||
|
array_key,
|
||||||
|
|
||||||
|
shm._array[post_slice],
|
||||||
|
post_slice,
|
||||||
|
append_len,
|
||||||
|
|
||||||
|
self.xy_nd_start,
|
||||||
|
self.xy_nd_stop,
|
||||||
|
is_append=True,
|
||||||
|
)
|
||||||
|
self.xy_nd_stop += append_len
|
||||||
|
profiler('appened xy history: {append_length}')
|
||||||
|
# sanity
|
||||||
|
# slice_ln = post_slice.stop - post_slice.start
|
||||||
|
# assert append_len == slice_ln
|
||||||
|
|
||||||
|
view_changed: bool = False
|
||||||
|
view_range: tuple[int, int] = (ivl, ivr)
|
||||||
|
if slice_to_inview:
|
||||||
|
view_changed = self._track_inview_range(view_range)
|
||||||
|
array = in_view
|
||||||
|
profiler(f'{self.viz.name} view range slice {view_range}')
|
||||||
|
|
||||||
|
# hist = array[:slice_to_head]
|
||||||
|
|
||||||
|
# XXX: WOA WTF TRACTOR DEBUGGING BUGGG
|
||||||
|
# assert 0
|
||||||
|
|
||||||
|
# xy-path data transform: convert source data to a format
|
||||||
|
# able to be passed to a `QPainterPath` rendering routine.
|
||||||
|
if not len(array):
|
||||||
|
# XXX: this might be why the profiler only has exits?
|
||||||
|
return
|
||||||
|
|
||||||
|
# TODO: hist here should be the pre-sliced
|
||||||
|
# x/y_data in the case where allocate_xy is
|
||||||
|
# defined?
|
||||||
|
x_1d, y_1d, connect = self.format_xy_nd_to_1d(
|
||||||
|
array,
|
||||||
|
array_key,
|
||||||
|
view_range,
|
||||||
|
)
|
||||||
|
|
||||||
|
# app_tres = None
|
||||||
|
# if append_len:
|
||||||
|
# appended = array[-append_len-1:slice_to_head]
|
||||||
|
# app_tres = self.format_xy_nd_to_1d(
|
||||||
|
# appended,
|
||||||
|
# array_key,
|
||||||
|
# (
|
||||||
|
# view_range[1] - append_len + slice_to_head,
|
||||||
|
# view_range[1]
|
||||||
|
# ),
|
||||||
|
# )
|
||||||
|
# # assert (len(appended) - 1) == append_len
|
||||||
|
# # assert len(appended) == append_len
|
||||||
|
# print(
|
||||||
|
# f'{self.viz.name} APPEND LEN: {append_len}\n'
|
||||||
|
# f'{self.viz.name} APPENDED: {appended}\n'
|
||||||
|
# f'{self.viz.name} app_tres: {app_tres}\n'
|
||||||
|
# )
|
||||||
|
|
||||||
|
# update the last "in view data range"
|
||||||
|
if len(x_1d):
|
||||||
|
self._last_ivdr = x_1d[0], x_1d[-1]
|
||||||
|
if (
|
||||||
|
self.index_field == 'time'
|
||||||
|
and (x_1d[-1] == 0.5).any()
|
||||||
|
):
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
profiler('.format_to_1d()')
|
||||||
|
|
||||||
|
return (
|
||||||
|
x_1d,
|
||||||
|
y_1d,
|
||||||
|
connect,
|
||||||
|
prepend_len,
|
||||||
|
append_len,
|
||||||
|
view_changed,
|
||||||
|
# app_tres,
|
||||||
|
)
|
||||||
|
|
||||||
|
###############################
|
||||||
|
# Sub-type override interface #
|
||||||
|
###############################
|
||||||
|
|
||||||
|
x_offset: np.ndarray = np.array([0])
|
||||||
|
|
||||||
|
# optional pre-graphics xy formatted data which
|
||||||
|
# is incrementally updated in sync with the source data.
|
||||||
|
# XXX: was ``.allocate_xy()``
|
||||||
|
def allocate_xy_nd(
|
||||||
|
self,
|
||||||
|
src_shm: ShmArray,
|
||||||
|
data_field: str,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray, # x
|
||||||
|
np.nd.array # y
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Convert the structured-array ``src_shm`` format to
|
||||||
|
a equivalently shaped (and field-less) ``np.ndarray``.
|
||||||
|
|
||||||
|
Eg. a 4 field x N struct-array => (N, 4)
|
||||||
|
|
||||||
|
'''
|
||||||
|
y_nd = src_shm._array[data_field].copy()
|
||||||
|
x_nd = (
|
||||||
|
src_shm._array[self.index_field].copy()
|
||||||
|
+
|
||||||
|
self.x_offset
|
||||||
|
)
|
||||||
|
return x_nd, y_nd
|
||||||
|
|
||||||
|
# XXX: was ``.update_xy()``
|
||||||
|
def incr_update_xy_nd(
|
||||||
|
self,
|
||||||
|
|
||||||
|
src_shm: ShmArray,
|
||||||
|
data_field: str,
|
||||||
|
|
||||||
|
new_from_src: np.ndarray, # portion of source that was updated
|
||||||
|
|
||||||
|
read_slc: slice,
|
||||||
|
ln: int, # len of updated
|
||||||
|
|
||||||
|
nd_start: int,
|
||||||
|
nd_stop: int,
|
||||||
|
|
||||||
|
is_append: bool,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
# write pushed data to flattened copy
|
||||||
|
y_nd_new = new_from_src[data_field]
|
||||||
|
self.y_nd[read_slc] = y_nd_new
|
||||||
|
|
||||||
|
x_nd_new = self.x_nd[read_slc]
|
||||||
|
x_nd_new[:] = (
|
||||||
|
new_from_src[self.index_field]
|
||||||
|
+
|
||||||
|
self.x_offset
|
||||||
|
)
|
||||||
|
|
||||||
|
# x_nd = self.x_nd[self.xy_slice]
|
||||||
|
# y_nd = self.y_nd[self.xy_slice]
|
||||||
|
# name = self.viz.name
|
||||||
|
# if 'trade_rate' == name:
|
||||||
|
# s = 4
|
||||||
|
# print(
|
||||||
|
# f'{name.upper()}:\n'
|
||||||
|
# 'NEW_FROM_SRC:\n'
|
||||||
|
# f'new_from_src: {new_from_src}\n\n'
|
||||||
|
|
||||||
|
# f'PRE self.x_nd:'
|
||||||
|
# f'\n{list(x_nd[-s:])}\n'
|
||||||
|
|
||||||
|
# f'PRE self.y_nd:\n'
|
||||||
|
# f'{list(y_nd[-s:])}\n\n'
|
||||||
|
|
||||||
|
# f'TO WRITE:\n'
|
||||||
|
|
||||||
|
# f'x_nd_new:\n'
|
||||||
|
# f'{x_nd_new[0]}\n'
|
||||||
|
|
||||||
|
# f'y_nd_new:\n'
|
||||||
|
# f'{y_nd_new}\n'
|
||||||
|
# )
|
||||||
|
|
||||||
|
# XXX: was ``.format_xy()``
|
||||||
|
def format_xy_nd_to_1d(
|
||||||
|
self,
|
||||||
|
|
||||||
|
array: np.ndarray,
|
||||||
|
array_key: str,
|
||||||
|
vr: tuple[int, int],
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray, # 1d x
|
||||||
|
np.ndarray, # 1d y
|
||||||
|
np.ndarray | str, # connection array/style
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Default xy-nd array to 1d pre-graphics-path render routine.
|
||||||
|
|
||||||
|
Return single field column data verbatim
|
||||||
|
|
||||||
|
'''
|
||||||
|
# NOTE: we don't include the very last datum which is filled in
|
||||||
|
# normally by another graphics object.
|
||||||
|
x_1d = array[self.index_field][:-1]
|
||||||
|
if (
|
||||||
|
self.index_field == 'time'
|
||||||
|
and x_1d.any()
|
||||||
|
and (x_1d[-1] == 0.5).any()
|
||||||
|
):
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
y_1d = array[array_key][:-1]
|
||||||
|
return (
|
||||||
|
x_1d,
|
||||||
|
y_1d,
|
||||||
|
|
||||||
|
# 1d connection array or style-key to
|
||||||
|
# ``pg.functions.arrayToQPath()``
|
||||||
|
'all',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OHLCBarsFmtr(IncrementalFormatter):
|
||||||
|
x_offset: np.ndarray = np.array([
|
||||||
|
-0.5,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0.5,
|
||||||
|
])
|
||||||
|
|
||||||
|
fields: list[str] = field(
|
||||||
|
default_factory=lambda: ['open', 'high', 'low', 'close']
|
||||||
|
)
|
||||||
|
|
||||||
|
def allocate_xy_nd(
|
||||||
|
self,
|
||||||
|
|
||||||
|
ohlc_shm: ShmArray,
|
||||||
|
data_field: str,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray, # x
|
||||||
|
np.nd.array # y
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Convert an input struct-array holding OHLC samples into a pair of
|
||||||
|
flattened x, y arrays with the same size (datums wise) as the source
|
||||||
|
data.
|
||||||
|
|
||||||
|
'''
|
||||||
|
y_nd = ohlc_shm.ustruct(self.fields)
|
||||||
|
|
||||||
|
# generate an flat-interpolated x-domain
|
||||||
|
x_nd = (
|
||||||
|
np.broadcast_to(
|
||||||
|
ohlc_shm._array[self.index_field][:, None],
|
||||||
|
(
|
||||||
|
ohlc_shm._array.size,
|
||||||
|
# 4, # only ohlc
|
||||||
|
y_nd.shape[1],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
+
|
||||||
|
self.x_offset
|
||||||
|
)
|
||||||
|
assert y_nd.any()
|
||||||
|
|
||||||
|
# write pushed data to flattened copy
|
||||||
|
return (
|
||||||
|
x_nd,
|
||||||
|
y_nd,
|
||||||
|
)
|
||||||
|
|
||||||
|
def incr_update_xy_nd(
|
||||||
|
self,
|
||||||
|
|
||||||
|
src_shm: ShmArray,
|
||||||
|
data_field: str,
|
||||||
|
|
||||||
|
new_from_src: np.ndarray, # portion of source that was updated
|
||||||
|
|
||||||
|
read_slc: slice,
|
||||||
|
ln: int, # len of updated
|
||||||
|
|
||||||
|
nd_start: int,
|
||||||
|
nd_stop: int,
|
||||||
|
|
||||||
|
is_append: bool,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
# write newly pushed data to flattened copy
|
||||||
|
# a struct-arr is always passed in.
|
||||||
|
new_y_nd = rfn.structured_to_unstructured(
|
||||||
|
new_from_src[self.fields]
|
||||||
|
)
|
||||||
|
self.y_nd[read_slc] = new_y_nd
|
||||||
|
|
||||||
|
# generate same-valued-per-row x support based on y shape
|
||||||
|
x_nd_new = self.x_nd[read_slc]
|
||||||
|
x_nd_new[:] = np.broadcast_to(
|
||||||
|
new_from_src[self.index_field][:, None],
|
||||||
|
new_y_nd.shape,
|
||||||
|
) + self.x_offset
|
||||||
|
|
||||||
|
# TODO: can we drop this frame and just use the above?
|
||||||
|
def format_xy_nd_to_1d(
|
||||||
|
self,
|
||||||
|
|
||||||
|
array: np.ndarray,
|
||||||
|
array_key: str,
|
||||||
|
vr: tuple[int, int],
|
||||||
|
|
||||||
|
start: int = 0, # XXX: do we need this?
|
||||||
|
# 0.5 is no overlap between arms, 1.0 is full overlap
|
||||||
|
w: float = 0.16,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
More or less direct proxy to the ``numba``-fied
|
||||||
|
``path_arrays_from_ohlc()`` (above) but with closed in kwargs
|
||||||
|
for line spacing.
|
||||||
|
|
||||||
|
'''
|
||||||
|
x, y, c = path_arrays_from_ohlc(
|
||||||
|
array,
|
||||||
|
start,
|
||||||
|
bar_w=self.index_step_size,
|
||||||
|
bar_gap=w * self.index_step_size,
|
||||||
|
|
||||||
|
# XXX: don't ask, due to a ``numba`` bug..
|
||||||
|
use_time_index=(self.index_field == 'time'),
|
||||||
|
)
|
||||||
|
return x, y, c
|
||||||
|
|
||||||
|
|
||||||
|
class OHLCBarsAsCurveFmtr(OHLCBarsFmtr):
|
||||||
|
|
||||||
|
def format_xy_nd_to_1d(
|
||||||
|
self,
|
||||||
|
|
||||||
|
array: np.ndarray,
|
||||||
|
array_key: str,
|
||||||
|
vr: tuple[int, int],
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
str,
|
||||||
|
]:
|
||||||
|
# TODO: in the case of an existing ``.update_xy()``
|
||||||
|
# should we be passing in array as an xy arrays tuple?
|
||||||
|
|
||||||
|
# 2 more datum-indexes to capture zero at end
|
||||||
|
x_flat = self.x_nd[self.xy_nd_start:self.xy_nd_stop-1]
|
||||||
|
y_flat = self.y_nd[self.xy_nd_start:self.xy_nd_stop-1]
|
||||||
|
|
||||||
|
# slice to view
|
||||||
|
ivl, ivr = vr
|
||||||
|
x_iv_flat = x_flat[ivl:ivr]
|
||||||
|
y_iv_flat = y_flat[ivl:ivr]
|
||||||
|
|
||||||
|
# reshape to 1d for graphics rendering
|
||||||
|
y_iv = y_iv_flat.reshape(-1)
|
||||||
|
x_iv = x_iv_flat.reshape(-1)
|
||||||
|
|
||||||
|
return x_iv, y_iv, 'all'
|
||||||
|
|
||||||
|
|
||||||
|
class StepCurveFmtr(IncrementalFormatter):
|
||||||
|
|
||||||
|
x_offset: np.ndarray = np.array([
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
])
|
||||||
|
|
||||||
|
def allocate_xy_nd(
|
||||||
|
self,
|
||||||
|
|
||||||
|
shm: ShmArray,
|
||||||
|
data_field: str,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray, # x
|
||||||
|
np.nd.array # y
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Convert an input 1d shm array to a "step array" format
|
||||||
|
for use by path graphics generation.
|
||||||
|
|
||||||
|
'''
|
||||||
|
i = shm._array[self.index_field].copy()
|
||||||
|
out = shm._array[data_field].copy()
|
||||||
|
|
||||||
|
x_out = (
|
||||||
|
np.broadcast_to(
|
||||||
|
i[:, None],
|
||||||
|
(i.size, 2),
|
||||||
|
)
|
||||||
|
+
|
||||||
|
self.x_offset
|
||||||
|
)
|
||||||
|
|
||||||
|
# fill out Nx2 array to hold each step's left + right vertices.
|
||||||
|
y_out = np.empty(
|
||||||
|
x_out.shape,
|
||||||
|
dtype=out.dtype,
|
||||||
|
)
|
||||||
|
# fill in (current) values from source shm buffer
|
||||||
|
y_out[:] = out[:, np.newaxis]
|
||||||
|
|
||||||
|
# TODO: pretty sure we can drop this?
|
||||||
|
# start y at origin level
|
||||||
|
# y_out[0, 0] = 0
|
||||||
|
# y_out[self.xy_nd_start] = 0
|
||||||
|
return x_out, y_out
|
||||||
|
|
||||||
|
def incr_update_xy_nd(
|
||||||
|
self,
|
||||||
|
|
||||||
|
src_shm: ShmArray,
|
||||||
|
array_key: str,
|
||||||
|
|
||||||
|
new_from_src: np.ndarray, # portion of source that was updated
|
||||||
|
read_slc: slice,
|
||||||
|
ln: int, # len of updated
|
||||||
|
|
||||||
|
nd_start: int,
|
||||||
|
nd_stop: int,
|
||||||
|
|
||||||
|
is_append: bool,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
slice,
|
||||||
|
]:
|
||||||
|
# NOTE: for a step curve we slice from one datum prior
|
||||||
|
# to the current "update slice" to get the previous
|
||||||
|
# "level".
|
||||||
|
#
|
||||||
|
# why this is needed,
|
||||||
|
# - the current new append slice will often have a zero
|
||||||
|
# value in the latest datum-step (at least for zero-on-new
|
||||||
|
# cases like vlm in the) as per configuration of the FSP
|
||||||
|
# engine.
|
||||||
|
# - we need to look back a datum to get the last level which
|
||||||
|
# will be used to terminate/complete the last step x-width
|
||||||
|
# which will be set to pair with the last x-index THIS MEANS
|
||||||
|
#
|
||||||
|
# XXX: this means WE CAN'T USE the append slice since we need to
|
||||||
|
# "look backward" one step to get the needed back-to-zero level
|
||||||
|
# and the update data in ``new_from_src`` will only contain the
|
||||||
|
# latest new data.
|
||||||
|
back_1 = slice(
|
||||||
|
read_slc.start - 1,
|
||||||
|
read_slc.stop,
|
||||||
|
)
|
||||||
|
|
||||||
|
to_write = src_shm._array[back_1]
|
||||||
|
y_nd_new = self.y_nd[back_1]
|
||||||
|
y_nd_new[:] = to_write[array_key][:, None]
|
||||||
|
|
||||||
|
x_nd_new = self.x_nd[read_slc]
|
||||||
|
x_nd_new[:] = (
|
||||||
|
new_from_src[self.index_field][:, None]
|
||||||
|
+
|
||||||
|
self.x_offset
|
||||||
|
)
|
||||||
|
|
||||||
|
# XXX: uncomment for debugging
|
||||||
|
# x_nd = self.x_nd[self.xy_slice]
|
||||||
|
# y_nd = self.y_nd[self.xy_slice]
|
||||||
|
# name = self.viz.name
|
||||||
|
# if 'dolla_vlm' in name:
|
||||||
|
# s = 4
|
||||||
|
# print(
|
||||||
|
# f'{name}:\n'
|
||||||
|
# 'NEW_FROM_SRC:\n'
|
||||||
|
# f'new_from_src: {new_from_src}\n\n'
|
||||||
|
|
||||||
|
# f'PRE self.x_nd:'
|
||||||
|
# f'\n{x_nd[-s:]}\n'
|
||||||
|
# f'PRE self.y_nd:\n'
|
||||||
|
# f'{y_nd[-s:]}\n\n'
|
||||||
|
|
||||||
|
# f'TO WRITE:\n'
|
||||||
|
# f'x_nd_new:\n'
|
||||||
|
# f'{x_nd_new}\n'
|
||||||
|
# f'y_nd_new:\n'
|
||||||
|
# f'{y_nd_new}\n'
|
||||||
|
# )
|
||||||
|
|
||||||
|
def format_xy_nd_to_1d(
|
||||||
|
self,
|
||||||
|
|
||||||
|
array: np.ndarray,
|
||||||
|
array_key: str,
|
||||||
|
vr: tuple[int, int],
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
str,
|
||||||
|
]:
|
||||||
|
last_t, last = array[-1][[self.index_field, array_key]]
|
||||||
|
|
||||||
|
start = self.xy_nd_start
|
||||||
|
stop = self.xy_nd_stop
|
||||||
|
|
||||||
|
x_step = self.x_nd[start:stop]
|
||||||
|
y_step = self.y_nd[start:stop]
|
||||||
|
|
||||||
|
# slice out in-view data
|
||||||
|
ivl, ivr = vr
|
||||||
|
|
||||||
|
# NOTE: add an extra step to get the vertical-line-down-to-zero
|
||||||
|
# adjacent to the last-datum graphic (filled rect).
|
||||||
|
x_step_iv = x_step[ivl:ivr+1]
|
||||||
|
y_step_iv = y_step[ivl:ivr+1]
|
||||||
|
|
||||||
|
# flatten to 1d
|
||||||
|
x_1d = x_step_iv.reshape(x_step_iv.size)
|
||||||
|
y_1d = y_step_iv.reshape(y_step_iv.size)
|
||||||
|
|
||||||
|
if (
|
||||||
|
self.index_field == 'time'
|
||||||
|
and x_1d.any()
|
||||||
|
and (x_1d == 0.5).any()
|
||||||
|
):
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
# debugging
|
||||||
|
# if y_1d.any():
|
||||||
|
# s = 6
|
||||||
|
# print(
|
||||||
|
# f'x_step_iv:\n{x_step_iv[-s:]}\n'
|
||||||
|
# f'y_step_iv:\n{y_step_iv[-s:]}\n\n'
|
||||||
|
# f'x_1d:\n{x_1d[-s:]}\n'
|
||||||
|
# f'y_1d:\n{y_1d[-s:]}\n'
|
||||||
|
# )
|
||||||
|
|
||||||
|
return x_1d, y_1d, 'all'
|
|
@ -15,17 +15,30 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Graphics related downsampling routines for compressing to pixel
|
Graphics downsampling using the infamous M4 algorithm.
|
||||||
limits on the display device.
|
|
||||||
|
This is one of ``piker``'s secret weapons allowing us to boss all other
|
||||||
|
charting platforms B)
|
||||||
|
|
||||||
|
(AND DON'T YOU DARE TAKE THIS CODE WITHOUT CREDIT OR WE'LL SUE UR F#&@* ASS).
|
||||||
|
|
||||||
|
NOTES: this method is a so called "visualization driven data
|
||||||
|
aggregation" approach. It gives error-free line chart
|
||||||
|
downsampling, see
|
||||||
|
further scientific paper resources:
|
||||||
|
- http://www.vldb.org/pvldb/vol7/p797-jugel.pdf
|
||||||
|
- http://www.vldb.org/2014/program/papers/demo/p997-jugel.pdf
|
||||||
|
|
||||||
|
Details on implementation of this algo are based in,
|
||||||
|
https://github.com/pikers/piker/issues/109
|
||||||
|
|
||||||
'''
|
'''
|
||||||
import math
|
import math
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from numpy.lib import recfunctions as rfn
|
|
||||||
from numba import (
|
from numba import (
|
||||||
jit,
|
njit,
|
||||||
# float64, optional, int64,
|
# float64, optional, int64,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,109 +48,6 @@ from ..log import get_logger
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def hl2mxmn(ohlc: np.ndarray) -> np.ndarray:
|
|
||||||
'''
|
|
||||||
Convert a OHLC struct-array containing 'high'/'low' columns
|
|
||||||
to a "joined" max/min 1-d array.
|
|
||||||
|
|
||||||
'''
|
|
||||||
index = ohlc['index']
|
|
||||||
hls = ohlc[[
|
|
||||||
'low',
|
|
||||||
'high',
|
|
||||||
]]
|
|
||||||
|
|
||||||
mxmn = np.empty(2*hls.size, dtype=np.float64)
|
|
||||||
x = np.empty(2*hls.size, dtype=np.float64)
|
|
||||||
trace_hl(hls, mxmn, x, index[0])
|
|
||||||
x = x + index[0]
|
|
||||||
|
|
||||||
return mxmn, x
|
|
||||||
|
|
||||||
|
|
||||||
@jit(
|
|
||||||
# TODO: the type annots..
|
|
||||||
# float64[:](float64[:],),
|
|
||||||
nopython=True,
|
|
||||||
)
|
|
||||||
def trace_hl(
|
|
||||||
hl: 'np.ndarray',
|
|
||||||
out: np.ndarray,
|
|
||||||
x: np.ndarray,
|
|
||||||
start: int,
|
|
||||||
|
|
||||||
# the "offset" values in the x-domain which
|
|
||||||
# place the 2 output points around each ``int``
|
|
||||||
# master index.
|
|
||||||
margin: float = 0.43,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''
|
|
||||||
"Trace" the outline of the high-low values of an ohlc sequence
|
|
||||||
as a line such that the maximum deviation (aka disperaion) between
|
|
||||||
bars if preserved.
|
|
||||||
|
|
||||||
This routine is expected to modify input arrays in-place.
|
|
||||||
|
|
||||||
'''
|
|
||||||
last_l = hl['low'][0]
|
|
||||||
last_h = hl['high'][0]
|
|
||||||
|
|
||||||
for i in range(hl.size):
|
|
||||||
row = hl[i]
|
|
||||||
l, h = row['low'], row['high']
|
|
||||||
|
|
||||||
up_diff = h - last_l
|
|
||||||
down_diff = last_h - l
|
|
||||||
|
|
||||||
if up_diff > down_diff:
|
|
||||||
out[2*i + 1] = h
|
|
||||||
out[2*i] = last_l
|
|
||||||
else:
|
|
||||||
out[2*i + 1] = l
|
|
||||||
out[2*i] = last_h
|
|
||||||
|
|
||||||
last_l = l
|
|
||||||
last_h = h
|
|
||||||
|
|
||||||
x[2*i] = int(i) - margin
|
|
||||||
x[2*i + 1] = int(i) + margin
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def ohlc_flatten(
|
|
||||||
ohlc: np.ndarray,
|
|
||||||
use_mxmn: bool = True,
|
|
||||||
|
|
||||||
) -> tuple[np.ndarray, np.ndarray]:
|
|
||||||
'''
|
|
||||||
Convert an OHLCV struct-array into a flat ready-for-line-plotting
|
|
||||||
1-d array that is 4 times the size with x-domain values distributed
|
|
||||||
evenly (by 0.5 steps) over each index.
|
|
||||||
|
|
||||||
'''
|
|
||||||
index = ohlc['index']
|
|
||||||
|
|
||||||
if use_mxmn:
|
|
||||||
# traces a line optimally over highs to lows
|
|
||||||
# using numba. NOTE: pretty sure this is faster
|
|
||||||
# and looks about the same as the below output.
|
|
||||||
flat, x = hl2mxmn(ohlc)
|
|
||||||
|
|
||||||
else:
|
|
||||||
flat = rfn.structured_to_unstructured(
|
|
||||||
ohlc[['open', 'high', 'low', 'close']]
|
|
||||||
).flatten()
|
|
||||||
|
|
||||||
x = np.linspace(
|
|
||||||
start=index[0] - 0.5,
|
|
||||||
stop=index[-1] + 0.5,
|
|
||||||
num=len(flat),
|
|
||||||
)
|
|
||||||
return x, flat
|
|
||||||
|
|
||||||
|
|
||||||
def ds_m4(
|
def ds_m4(
|
||||||
x: np.ndarray,
|
x: np.ndarray,
|
||||||
y: np.ndarray,
|
y: np.ndarray,
|
||||||
|
@ -160,16 +70,6 @@ def ds_m4(
|
||||||
This is more or less an OHLC style sampling of a line-style series.
|
This is more or less an OHLC style sampling of a line-style series.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# NOTE: this method is a so called "visualization driven data
|
|
||||||
# aggregation" approach. It gives error-free line chart
|
|
||||||
# downsampling, see
|
|
||||||
# further scientific paper resources:
|
|
||||||
# - http://www.vldb.org/pvldb/vol7/p797-jugel.pdf
|
|
||||||
# - http://www.vldb.org/2014/program/papers/demo/p997-jugel.pdf
|
|
||||||
|
|
||||||
# Details on implementation of this algo are based in,
|
|
||||||
# https://github.com/pikers/piker/issues/109
|
|
||||||
|
|
||||||
# XXX: from infinite on downsampling viewable graphics:
|
# XXX: from infinite on downsampling viewable graphics:
|
||||||
# "one thing i remembered about the binning - if you are
|
# "one thing i remembered about the binning - if you are
|
||||||
# picking a range within your timeseries the start and end bin
|
# picking a range within your timeseries the start and end bin
|
||||||
|
@ -223,14 +123,20 @@ def ds_m4(
|
||||||
assert frames >= (xrange / uppx)
|
assert frames >= (xrange / uppx)
|
||||||
|
|
||||||
# call into ``numba``
|
# call into ``numba``
|
||||||
nb, i_win, y_out = _m4(
|
(
|
||||||
|
nb,
|
||||||
|
x_out,
|
||||||
|
y_out,
|
||||||
|
ymn,
|
||||||
|
ymx,
|
||||||
|
) = _m4(
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
|
||||||
frames,
|
frames,
|
||||||
|
|
||||||
# TODO: see func below..
|
# TODO: see func below..
|
||||||
# i_win,
|
# x_out,
|
||||||
# y_out,
|
# y_out,
|
||||||
|
|
||||||
# first index in x data to start at
|
# first index in x data to start at
|
||||||
|
@ -243,14 +149,14 @@ def ds_m4(
|
||||||
# filter out any overshoot in the input allocation arrays by
|
# filter out any overshoot in the input allocation arrays by
|
||||||
# removing zero-ed tail entries which should start at a certain
|
# removing zero-ed tail entries which should start at a certain
|
||||||
# index.
|
# index.
|
||||||
i_win = i_win[i_win != 0]
|
x_out = x_out[x_out != 0]
|
||||||
y_out = y_out[:i_win.size]
|
y_out = y_out[:x_out.size]
|
||||||
|
|
||||||
return nb, i_win, y_out
|
# print(f'M4 output ymn, ymx: {ymn},{ymx}')
|
||||||
|
return nb, x_out, y_out, ymn, ymx
|
||||||
|
|
||||||
|
|
||||||
@jit(
|
@njit(
|
||||||
nopython=True,
|
|
||||||
nogil=True,
|
nogil=True,
|
||||||
)
|
)
|
||||||
def _m4(
|
def _m4(
|
||||||
|
@ -260,8 +166,8 @@ def _m4(
|
||||||
|
|
||||||
frames: int,
|
frames: int,
|
||||||
|
|
||||||
# TODO: using this approach by having the ``.zeros()`` alloc lines
|
# TODO: using this approach, having the ``.zeros()`` alloc lines
|
||||||
# below, in put python was causing segs faults and alloc crashes..
|
# below in pure python, there were segs faults and alloc crashes..
|
||||||
# we might need to see how it behaves with shm arrays and consider
|
# we might need to see how it behaves with shm arrays and consider
|
||||||
# allocating them once at startup?
|
# allocating them once at startup?
|
||||||
|
|
||||||
|
@ -274,14 +180,22 @@ def _m4(
|
||||||
x_start: int,
|
x_start: int,
|
||||||
step: float,
|
step: float,
|
||||||
|
|
||||||
) -> int:
|
) -> tuple[
|
||||||
# nbins = len(i_win)
|
int,
|
||||||
# count = len(xs)
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
float,
|
||||||
|
float,
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Implementation of the m4 algorithm in ``numba``:
|
||||||
|
http://www.vldb.org/pvldb/vol7/p797-jugel.pdf
|
||||||
|
|
||||||
|
'''
|
||||||
# these are pre-allocated and mutated by ``numba``
|
# these are pre-allocated and mutated by ``numba``
|
||||||
# code in-place.
|
# code in-place.
|
||||||
y_out = np.zeros((frames, 4), ys.dtype)
|
y_out = np.zeros((frames, 4), ys.dtype)
|
||||||
i_win = np.zeros(frames, xs.dtype)
|
x_out = np.zeros(frames, xs.dtype)
|
||||||
|
|
||||||
bincount = 0
|
bincount = 0
|
||||||
x_left = x_start
|
x_left = x_start
|
||||||
|
@ -295,24 +209,34 @@ def _m4(
|
||||||
|
|
||||||
# set all bins in the left-most entry to the starting left-most x value
|
# set all bins in the left-most entry to the starting left-most x value
|
||||||
# (aka a row broadcast).
|
# (aka a row broadcast).
|
||||||
i_win[bincount] = x_left
|
x_out[bincount] = x_left
|
||||||
# set all y-values to the first value passed in.
|
# set all y-values to the first value passed in.
|
||||||
y_out[bincount] = ys[0]
|
y_out[bincount] = ys[0]
|
||||||
|
|
||||||
|
# full input y-data mx and mn
|
||||||
|
mx: float = -np.inf
|
||||||
|
mn: float = np.inf
|
||||||
|
|
||||||
|
# compute OHLC style max / min values per window sized x-frame.
|
||||||
for i in range(len(xs)):
|
for i in range(len(xs)):
|
||||||
|
|
||||||
x = xs[i]
|
x = xs[i]
|
||||||
y = ys[i]
|
y = ys[i]
|
||||||
|
|
||||||
if x < x_left + step: # the current window "step" is [bin, bin+1)
|
if x < x_left + step: # the current window "step" is [bin, bin+1)
|
||||||
y_out[bincount, 1] = min(y, y_out[bincount, 1])
|
ymn = y_out[bincount, 1] = min(y, y_out[bincount, 1])
|
||||||
y_out[bincount, 2] = max(y, y_out[bincount, 2])
|
ymx = y_out[bincount, 2] = max(y, y_out[bincount, 2])
|
||||||
y_out[bincount, 3] = y
|
y_out[bincount, 3] = y
|
||||||
|
mx = max(mx, ymx)
|
||||||
|
mn = min(mn, ymn)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Find the next bin
|
# Find the next bin
|
||||||
while x >= x_left + step:
|
while x >= x_left + step:
|
||||||
x_left += step
|
x_left += step
|
||||||
|
|
||||||
bincount += 1
|
bincount += 1
|
||||||
i_win[bincount] = x_left
|
x_out[bincount] = x_left
|
||||||
y_out[bincount] = y
|
y_out[bincount] = y
|
||||||
|
|
||||||
return bincount, i_win, y_out
|
return bincount, x_out, y_out, mn, mx
|
|
@ -56,7 +56,7 @@ def iterticks(
|
||||||
sig = (
|
sig = (
|
||||||
time,
|
time,
|
||||||
tick['price'],
|
tick['price'],
|
||||||
tick['size']
|
tick.get('size')
|
||||||
)
|
)
|
||||||
|
|
||||||
if ttype == 'dark_trade':
|
if ttype == 'dark_trade':
|
||||||
|
|
|
@ -0,0 +1,432 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
Super fast ``QPainterPath`` generation related operator routines.
|
||||||
|
|
||||||
|
"""
|
||||||
|
import numpy as np
|
||||||
|
from numpy.lib import recfunctions as rfn
|
||||||
|
from numba import (
|
||||||
|
# types,
|
||||||
|
njit,
|
||||||
|
float64,
|
||||||
|
int64,
|
||||||
|
# optional,
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: for ``numba`` typing..
|
||||||
|
# from ._source import numba_ohlc_dtype
|
||||||
|
from ._m4 import ds_m4
|
||||||
|
from .._profile import (
|
||||||
|
Profiler,
|
||||||
|
pg_profile_enabled,
|
||||||
|
ms_slower_then,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def xy_downsample(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
uppx,
|
||||||
|
|
||||||
|
x_spacer: float = 0.5,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
float,
|
||||||
|
float,
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Downsample 1D (flat ``numpy.ndarray``) arrays using M4 given an input
|
||||||
|
``uppx`` (units-per-pixel) and add space between discreet datums.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# downsample whenever more then 1 pixels per datum can be shown.
|
||||||
|
# always refresh data bounds until we get diffing
|
||||||
|
# working properly, see above..
|
||||||
|
bins, x, y, ymn, ymx = ds_m4(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
uppx,
|
||||||
|
)
|
||||||
|
|
||||||
|
# flatten output to 1d arrays suitable for path-graphics generation.
|
||||||
|
x = np.broadcast_to(x[:, None], y.shape)
|
||||||
|
x = (x + np.array(
|
||||||
|
[-x_spacer, 0, 0, x_spacer]
|
||||||
|
)).flatten()
|
||||||
|
y = y.flatten()
|
||||||
|
|
||||||
|
return x, y, ymn, ymx
|
||||||
|
|
||||||
|
|
||||||
|
@njit(
|
||||||
|
# NOTE: need to construct this manually for readonly
|
||||||
|
# arrays, see https://github.com/numba/numba/issues/4511
|
||||||
|
# (
|
||||||
|
# types.Array(
|
||||||
|
# numba_ohlc_dtype,
|
||||||
|
# 1,
|
||||||
|
# 'C',
|
||||||
|
# readonly=True,
|
||||||
|
# ),
|
||||||
|
# int64,
|
||||||
|
# types.unicode_type,
|
||||||
|
# optional(float64),
|
||||||
|
# ),
|
||||||
|
nogil=True
|
||||||
|
)
|
||||||
|
def path_arrays_from_ohlc(
|
||||||
|
data: np.ndarray,
|
||||||
|
start: int64,
|
||||||
|
bar_w: float64,
|
||||||
|
bar_gap: float64 = 0.16,
|
||||||
|
use_time_index: bool = True,
|
||||||
|
|
||||||
|
# XXX: ``numba`` issue: https://github.com/numba/numba/issues/8622
|
||||||
|
# index_field: str,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
np.ndarray,
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Generate an array of lines objects from input ohlc data.
|
||||||
|
|
||||||
|
'''
|
||||||
|
size = int(data.shape[0] * 6)
|
||||||
|
|
||||||
|
# XXX: see this for why the dtype might have to be defined outside
|
||||||
|
# the routine.
|
||||||
|
# https://github.com/numba/numba/issues/4098#issuecomment-493914533
|
||||||
|
x = np.zeros(
|
||||||
|
shape=size,
|
||||||
|
dtype=float64,
|
||||||
|
)
|
||||||
|
y, c = x.copy(), x.copy()
|
||||||
|
|
||||||
|
half_w: float = bar_w/2
|
||||||
|
|
||||||
|
# TODO: report bug for assert @
|
||||||
|
# /home/goodboy/repos/piker/env/lib/python3.8/site-packages/numba/core/typing/builtins.py:991
|
||||||
|
for i, q in enumerate(data[start:], start):
|
||||||
|
|
||||||
|
open = q['open']
|
||||||
|
high = q['high']
|
||||||
|
low = q['low']
|
||||||
|
close = q['close']
|
||||||
|
|
||||||
|
if use_time_index:
|
||||||
|
index = float64(q['time'])
|
||||||
|
else:
|
||||||
|
index = float64(q['index'])
|
||||||
|
|
||||||
|
# XXX: ``numba`` issue: https://github.com/numba/numba/issues/8622
|
||||||
|
# index = float64(q[index_field])
|
||||||
|
# AND this (probably)
|
||||||
|
# open, high, low, close, index = q[
|
||||||
|
# ['open', 'high', 'low', 'close', 'index']]
|
||||||
|
|
||||||
|
istart = i * 6
|
||||||
|
istop = istart + 6
|
||||||
|
|
||||||
|
# x,y detail the 6 points which connect all vertexes of a ohlc bar
|
||||||
|
mid: float = index + half_w
|
||||||
|
x[istart:istop] = (
|
||||||
|
index + bar_gap,
|
||||||
|
mid,
|
||||||
|
mid,
|
||||||
|
mid,
|
||||||
|
mid,
|
||||||
|
index + bar_w - bar_gap,
|
||||||
|
)
|
||||||
|
y[istart:istop] = (
|
||||||
|
open,
|
||||||
|
open,
|
||||||
|
low,
|
||||||
|
high,
|
||||||
|
close,
|
||||||
|
close,
|
||||||
|
)
|
||||||
|
|
||||||
|
# specifies that the first edge is never connected to the
|
||||||
|
# prior bars last edge thus providing a small "gap"/"space"
|
||||||
|
# between bars determined by ``bar_gap``.
|
||||||
|
c[istart:istop] = (1, 1, 1, 1, 1, 0)
|
||||||
|
|
||||||
|
return x, y, c
|
||||||
|
|
||||||
|
|
||||||
|
def hl2mxmn(
|
||||||
|
ohlc: np.ndarray,
|
||||||
|
index_field: str = 'index',
|
||||||
|
|
||||||
|
) -> np.ndarray:
|
||||||
|
'''
|
||||||
|
Convert a OHLC struct-array containing 'high'/'low' columns
|
||||||
|
to a "joined" max/min 1-d array.
|
||||||
|
|
||||||
|
'''
|
||||||
|
index = ohlc[index_field]
|
||||||
|
hls = ohlc[[
|
||||||
|
'low',
|
||||||
|
'high',
|
||||||
|
]]
|
||||||
|
|
||||||
|
mxmn = np.empty(2*hls.size, dtype=np.float64)
|
||||||
|
x = np.empty(2*hls.size, dtype=np.float64)
|
||||||
|
trace_hl(hls, mxmn, x, index[0])
|
||||||
|
x = x + index[0]
|
||||||
|
|
||||||
|
return mxmn, x
|
||||||
|
|
||||||
|
|
||||||
|
@njit(
|
||||||
|
# TODO: the type annots..
|
||||||
|
# float64[:](float64[:],),
|
||||||
|
)
|
||||||
|
def trace_hl(
|
||||||
|
hl: 'np.ndarray',
|
||||||
|
out: np.ndarray,
|
||||||
|
x: np.ndarray,
|
||||||
|
start: int,
|
||||||
|
|
||||||
|
# the "offset" values in the x-domain which
|
||||||
|
# place the 2 output points around each ``int``
|
||||||
|
# master index.
|
||||||
|
margin: float = 0.43,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
"Trace" the outline of the high-low values of an ohlc sequence
|
||||||
|
as a line such that the maximum deviation (aka disperaion) between
|
||||||
|
bars if preserved.
|
||||||
|
|
||||||
|
This routine is expected to modify input arrays in-place.
|
||||||
|
|
||||||
|
'''
|
||||||
|
last_l = hl['low'][0]
|
||||||
|
last_h = hl['high'][0]
|
||||||
|
|
||||||
|
for i in range(hl.size):
|
||||||
|
row = hl[i]
|
||||||
|
l, h = row['low'], row['high']
|
||||||
|
|
||||||
|
up_diff = h - last_l
|
||||||
|
down_diff = last_h - l
|
||||||
|
|
||||||
|
if up_diff > down_diff:
|
||||||
|
out[2*i + 1] = h
|
||||||
|
out[2*i] = last_l
|
||||||
|
else:
|
||||||
|
out[2*i + 1] = l
|
||||||
|
out[2*i] = last_h
|
||||||
|
|
||||||
|
last_l = l
|
||||||
|
last_h = h
|
||||||
|
|
||||||
|
x[2*i] = int(i) - margin
|
||||||
|
x[2*i + 1] = int(i) + margin
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def ohlc_flatten(
|
||||||
|
ohlc: np.ndarray,
|
||||||
|
use_mxmn: bool = True,
|
||||||
|
index_field: str = 'index',
|
||||||
|
|
||||||
|
) -> tuple[np.ndarray, np.ndarray]:
|
||||||
|
'''
|
||||||
|
Convert an OHLCV struct-array into a flat ready-for-line-plotting
|
||||||
|
1-d array that is 4 times the size with x-domain values distributed
|
||||||
|
evenly (by 0.5 steps) over each index.
|
||||||
|
|
||||||
|
'''
|
||||||
|
index = ohlc[index_field]
|
||||||
|
|
||||||
|
if use_mxmn:
|
||||||
|
# traces a line optimally over highs to lows
|
||||||
|
# using numba. NOTE: pretty sure this is faster
|
||||||
|
# and looks about the same as the below output.
|
||||||
|
flat, x = hl2mxmn(ohlc)
|
||||||
|
|
||||||
|
else:
|
||||||
|
flat = rfn.structured_to_unstructured(
|
||||||
|
ohlc[['open', 'high', 'low', 'close']]
|
||||||
|
).flatten()
|
||||||
|
|
||||||
|
x = np.linspace(
|
||||||
|
start=index[0] - 0.5,
|
||||||
|
stop=index[-1] + 0.5,
|
||||||
|
num=len(flat),
|
||||||
|
)
|
||||||
|
return x, flat
|
||||||
|
|
||||||
|
|
||||||
|
def slice_from_time(
|
||||||
|
arr: np.ndarray,
|
||||||
|
start_t: float,
|
||||||
|
stop_t: float,
|
||||||
|
step: int | None = None,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
slice,
|
||||||
|
slice,
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Calculate array indices mapped from a time range and return them in
|
||||||
|
a slice.
|
||||||
|
|
||||||
|
Given an input array with an epoch `'time'` series entry, calculate
|
||||||
|
the indices which span the time range and return in a slice. Presume
|
||||||
|
each `'time'` step increment is uniform and when the time stamp
|
||||||
|
series contains gaps (the uniform presumption is untrue) use
|
||||||
|
``np.searchsorted()`` binary search to look up the appropriate
|
||||||
|
index.
|
||||||
|
|
||||||
|
'''
|
||||||
|
profiler = Profiler(
|
||||||
|
msg='slice_from_time()',
|
||||||
|
disabled=not pg_profile_enabled(),
|
||||||
|
ms_threshold=ms_slower_then,
|
||||||
|
)
|
||||||
|
|
||||||
|
times = arr['time']
|
||||||
|
t_first = round(times[0])
|
||||||
|
|
||||||
|
read_i_max = arr.shape[0]
|
||||||
|
|
||||||
|
if step is None:
|
||||||
|
step = round(times[-1] - times[-2])
|
||||||
|
if step == 0:
|
||||||
|
# XXX: HOW TF is this happening?
|
||||||
|
step = 1
|
||||||
|
|
||||||
|
# compute (presumed) uniform-time-step index offsets
|
||||||
|
i_start_t = round(start_t)
|
||||||
|
read_i_start = round(((i_start_t - t_first) // step)) - 1
|
||||||
|
|
||||||
|
i_stop_t = round(stop_t)
|
||||||
|
read_i_stop = round((i_stop_t - t_first) // step) + 1
|
||||||
|
|
||||||
|
# always clip outputs to array support
|
||||||
|
# for read start:
|
||||||
|
# - never allow a start < the 0 index
|
||||||
|
# - never allow an end index > the read array len
|
||||||
|
read_i_start = min(
|
||||||
|
max(0, read_i_start),
|
||||||
|
read_i_max - 1,
|
||||||
|
)
|
||||||
|
read_i_stop = max(
|
||||||
|
0,
|
||||||
|
min(read_i_stop, read_i_max),
|
||||||
|
)
|
||||||
|
|
||||||
|
# check for larger-then-latest calculated index for given start
|
||||||
|
# time, in which case we do a binary search for the correct index.
|
||||||
|
# NOTE: this is usually the result of a time series with time gaps
|
||||||
|
# where it is expected that each index step maps to a uniform step
|
||||||
|
# in the time stamp series.
|
||||||
|
t_iv_start = times[read_i_start]
|
||||||
|
if (
|
||||||
|
t_iv_start > i_start_t
|
||||||
|
):
|
||||||
|
# do a binary search for the best index mapping to ``start_t``
|
||||||
|
# given we measured an overshoot using the uniform-time-step
|
||||||
|
# calculation from above.
|
||||||
|
|
||||||
|
# TODO: once we start caching these per source-array,
|
||||||
|
# we can just overwrite ``read_i_start`` directly.
|
||||||
|
new_read_i_start = np.searchsorted(
|
||||||
|
times,
|
||||||
|
i_start_t,
|
||||||
|
side='left',
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: minimize binary search work as much as possible:
|
||||||
|
# - cache these remap values which compensate for gaps in the
|
||||||
|
# uniform time step basis where we calc a later start
|
||||||
|
# index for the given input ``start_t``.
|
||||||
|
# - can we shorten the input search sequence by heuristic?
|
||||||
|
# up_to_arith_start = index[:read_i_start]
|
||||||
|
|
||||||
|
if (
|
||||||
|
new_read_i_start < read_i_start
|
||||||
|
):
|
||||||
|
# t_diff = t_iv_start - start_t
|
||||||
|
# print(
|
||||||
|
# f"WE'RE CUTTING OUT TIME - STEP:{step}\n"
|
||||||
|
# f'start_t:{start_t} -> 0index start_t:{t_iv_start}\n'
|
||||||
|
# f'diff: {t_diff}\n'
|
||||||
|
# f'REMAPPED START i: {read_i_start} -> {new_read_i_start}\n'
|
||||||
|
# )
|
||||||
|
read_i_start = new_read_i_start - 1
|
||||||
|
|
||||||
|
t_iv_stop = times[read_i_stop - 1]
|
||||||
|
if (
|
||||||
|
t_iv_stop > i_stop_t
|
||||||
|
):
|
||||||
|
# t_diff = stop_t - t_iv_stop
|
||||||
|
# print(
|
||||||
|
# f"WE'RE CUTTING OUT TIME - STEP:{step}\n"
|
||||||
|
# f'calced iv stop:{t_iv_stop} -> stop_t:{stop_t}\n'
|
||||||
|
# f'diff: {t_diff}\n'
|
||||||
|
# # f'SHOULD REMAP STOP: {read_i_start} -> {new_read_i_start}\n'
|
||||||
|
# )
|
||||||
|
new_read_i_stop = np.searchsorted(
|
||||||
|
times[read_i_start:],
|
||||||
|
i_stop_t,
|
||||||
|
side='left',
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
new_read_i_stop < read_i_stop
|
||||||
|
):
|
||||||
|
read_i_stop = read_i_start + new_read_i_stop
|
||||||
|
|
||||||
|
# sanity checks for range size
|
||||||
|
# samples = (i_stop_t - i_start_t) // step
|
||||||
|
# index_diff = read_i_stop - read_i_start + 1
|
||||||
|
# if index_diff > (samples + 3):
|
||||||
|
# breakpoint()
|
||||||
|
|
||||||
|
# read-relative indexes: gives a slice where `shm.array[read_slc]`
|
||||||
|
# will be the data spanning the input time range `start_t` ->
|
||||||
|
# `stop_t`
|
||||||
|
read_slc = slice(
|
||||||
|
int(read_i_start),
|
||||||
|
int(read_i_stop),
|
||||||
|
)
|
||||||
|
|
||||||
|
profiler(
|
||||||
|
'slicing complete'
|
||||||
|
# f'{start_t} -> {abs_slc.start} | {read_slc.start}\n'
|
||||||
|
# f'{stop_t} -> {abs_slc.stop} | {read_slc.stop}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
# NOTE: if caller needs absolute buffer indices they can
|
||||||
|
# slice the buffer abs index like so:
|
||||||
|
# index = arr['index']
|
||||||
|
# abs_indx = index[read_slc]
|
||||||
|
# abs_slc = slice(
|
||||||
|
# int(abs_indx[0]),
|
||||||
|
# int(abs_indx[-1]),
|
||||||
|
# )
|
||||||
|
|
||||||
|
return read_slc
|
|
@ -20,53 +20,96 @@ financial data flows.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from collections import Counter
|
from collections import (
|
||||||
|
Counter,
|
||||||
|
defaultdict,
|
||||||
|
)
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING, Optional, Union
|
from typing import (
|
||||||
|
AsyncIterator,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
import tractor
|
import tractor
|
||||||
|
from tractor.trionics import (
|
||||||
|
maybe_open_nursery,
|
||||||
|
)
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
|
|
||||||
from ..log import get_logger
|
from ..log import (
|
||||||
|
get_logger,
|
||||||
|
get_console_log,
|
||||||
|
)
|
||||||
|
from .._daemon import maybe_spawn_daemon
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ._sharedmem import ShmArray
|
from ._sharedmem import (
|
||||||
|
ShmArray,
|
||||||
|
)
|
||||||
from .feed import _FeedsBus
|
from .feed import _FeedsBus
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class sampler:
|
# highest frequency sample step is 1 second by default, though in
|
||||||
|
# the future we may want to support shorter periods or a dynamic style
|
||||||
|
# tick-event stream.
|
||||||
|
_default_delay_s: float = 1.0
|
||||||
|
|
||||||
|
|
||||||
|
class Sampler:
|
||||||
'''
|
'''
|
||||||
Global sampling engine registry.
|
Global sampling engine registry.
|
||||||
|
|
||||||
Manages state for sampling events, shm incrementing and
|
Manages state for sampling events, shm incrementing and
|
||||||
sample period logic.
|
sample period logic.
|
||||||
|
|
||||||
|
This non-instantiated type is meant to be a singleton within
|
||||||
|
a `samplerd` actor-service spawned once by the user wishing to
|
||||||
|
time-step sample real-time quote feeds, see
|
||||||
|
``._daemon.maybe_open_samplerd()`` and the below
|
||||||
|
``register_with_sampler()``.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
service_nursery: None | trio.Nursery = None
|
||||||
|
|
||||||
# TODO: we could stick these in a composed type to avoid
|
# TODO: we could stick these in a composed type to avoid
|
||||||
# angering the "i hate module scoped variables crowd" (yawn).
|
# angering the "i hate module scoped variables crowd" (yawn).
|
||||||
ohlcv_shms: dict[int, list[ShmArray]] = {}
|
ohlcv_shms: dict[float, list[ShmArray]] = {}
|
||||||
|
|
||||||
# holds one-task-per-sample-period tasks which are spawned as-needed by
|
# holds one-task-per-sample-period tasks which are spawned as-needed by
|
||||||
# data feed requests with a given detected time step usually from
|
# data feed requests with a given detected time step usually from
|
||||||
# history loading.
|
# history loading.
|
||||||
incrementers: dict[int, trio.CancelScope] = {}
|
incr_task_cs: trio.CancelScope | None = None
|
||||||
|
|
||||||
# holds all the ``tractor.Context`` remote subscriptions for
|
# holds all the ``tractor.Context`` remote subscriptions for
|
||||||
# a particular sample period increment event: all subscribers are
|
# a particular sample period increment event: all subscribers are
|
||||||
# notified on a step.
|
# notified on a step.
|
||||||
subscribers: dict[int, tractor.Context] = {}
|
# subscribers: dict[int, list[tractor.MsgStream]] = {}
|
||||||
|
subscribers: defaultdict[
|
||||||
|
float,
|
||||||
|
list[
|
||||||
|
float,
|
||||||
|
set[tractor.MsgStream]
|
||||||
|
],
|
||||||
|
] = defaultdict(
|
||||||
|
lambda: [
|
||||||
|
round(time.time()),
|
||||||
|
set(),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
async def increment_ohlc_buffer(
|
async def increment_ohlc_buffer(
|
||||||
delay_s: int,
|
self,
|
||||||
|
period_s: float,
|
||||||
task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Task which inserts new bars into the provide shared memory array
|
Task which inserts new bars into the provide shared memory array
|
||||||
every ``delay_s`` seconds.
|
every ``period_s`` seconds.
|
||||||
|
|
||||||
This task fulfills 2 purposes:
|
This task fulfills 2 purposes:
|
||||||
- it takes the subscribed set of shm arrays and increments them
|
- it takes the subscribed set of shm arrays and increments them
|
||||||
|
@ -78,100 +121,143 @@ async def increment_ohlc_buffer(
|
||||||
the underlying buffers will actually be incremented.
|
the underlying buffers will actually be incremented.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# # wait for brokerd to signal we should start sampling
|
|
||||||
# await shm_incrementing(shm_token['shm_name']).wait()
|
|
||||||
|
|
||||||
# TODO: right now we'll spin printing bars if the last time stamp is
|
# TODO: right now we'll spin printing bars if the last time stamp is
|
||||||
# before a large period of no market activity. Likely the best way
|
# before a large period of no market activity. Likely the best way
|
||||||
# to solve this is to make this task aware of the instrument's
|
# to solve this is to make this task aware of the instrument's
|
||||||
# tradable hours?
|
# tradable hours?
|
||||||
|
|
||||||
# adjust delay to compensate for trio processing time
|
total_s: float = 0 # total seconds counted
|
||||||
ad = min(sampler.ohlcv_shms.keys()) - 0.001
|
ad = period_s - 0.001 # compensate for trio processing time
|
||||||
|
|
||||||
total_s = 0 # total seconds counted
|
|
||||||
lowest = min(sampler.ohlcv_shms.keys())
|
|
||||||
lowest_shm = sampler.ohlcv_shms[lowest][0]
|
|
||||||
ad = lowest - 0.001
|
|
||||||
|
|
||||||
with trio.CancelScope() as cs:
|
with trio.CancelScope() as cs:
|
||||||
|
|
||||||
# register this time period step as active
|
# register this time period step as active
|
||||||
sampler.incrementers[delay_s] = cs
|
|
||||||
task_status.started(cs)
|
task_status.started(cs)
|
||||||
|
|
||||||
|
# sample step loop:
|
||||||
|
# includes broadcasting to all connected consumers on every
|
||||||
|
# new sample step as well incrementing any registered
|
||||||
|
# buffers by registered sample period.
|
||||||
while True:
|
while True:
|
||||||
# TODO: do we want to support dynamically
|
|
||||||
# adding a "lower" lowest increment period?
|
|
||||||
await trio.sleep(ad)
|
await trio.sleep(ad)
|
||||||
total_s += lowest
|
total_s += period_s
|
||||||
|
|
||||||
# increment all subscribed shm arrays
|
# increment all subscribed shm arrays
|
||||||
# TODO:
|
# TODO:
|
||||||
# - this in ``numba``
|
# - this in ``numba``
|
||||||
# - just lookup shms for this step instead of iterating?
|
# - just lookup shms for this step instead of iterating?
|
||||||
for delay_s, shms in sampler.ohlcv_shms.items():
|
|
||||||
if total_s % delay_s != 0:
|
i_epoch = round(time.time())
|
||||||
|
broadcasted: set[float] = set()
|
||||||
|
|
||||||
|
# print(f'epoch: {i_epoch} -> REGISTRY {self.ohlcv_shms}')
|
||||||
|
for shm_period_s, shms in self.ohlcv_shms.items():
|
||||||
|
|
||||||
|
# short-circuit on any not-ready because slower sample
|
||||||
|
# rate consuming shm buffers.
|
||||||
|
if total_s % shm_period_s != 0:
|
||||||
|
# print(f'skipping `{shm_period_s}s` sample update')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# update last epoch stamp for this period group
|
||||||
|
if shm_period_s not in broadcasted:
|
||||||
|
sub_pair = self.subscribers[shm_period_s]
|
||||||
|
sub_pair[0] = i_epoch
|
||||||
|
broadcasted.add(shm_period_s)
|
||||||
|
|
||||||
# TODO: ``numba`` this!
|
# TODO: ``numba`` this!
|
||||||
for shm in shms:
|
for shm in shms:
|
||||||
# TODO: in theory we could make this faster by copying the
|
# print(f'UPDATE {shm_period_s}s STEP for {shm.token}')
|
||||||
# "last" readable value into the underlying larger buffer's
|
|
||||||
# next value and then incrementing the counter instead of
|
|
||||||
# using ``.push()``?
|
|
||||||
|
|
||||||
# append new entry to buffer thus "incrementing" the bar
|
# append new entry to buffer thus "incrementing"
|
||||||
|
# the bar
|
||||||
array = shm.array
|
array = shm.array
|
||||||
last = array[-1:][shm._write_fields].copy()
|
last = array[-1:][shm._write_fields].copy()
|
||||||
# (index, t, close) = last[0][['index', 'time', 'close']]
|
|
||||||
(t, close) = last[0][['time', 'close']]
|
|
||||||
|
|
||||||
# this copies non-std fields (eg. vwap) from the last datum
|
# guard against startup backfilling races where
|
||||||
last[
|
# the buffer has not yet been filled.
|
||||||
['time', 'volume', 'open', 'high', 'low', 'close']
|
if not last.size:
|
||||||
][0] = (t + delay_s, 0, close, close, close, close)
|
continue
|
||||||
|
|
||||||
|
(t, close) = last[0][[
|
||||||
|
'time',
|
||||||
|
'close',
|
||||||
|
]]
|
||||||
|
|
||||||
|
next_t = t + shm_period_s
|
||||||
|
|
||||||
|
if shm_period_s <= 1:
|
||||||
|
next_t = i_epoch
|
||||||
|
|
||||||
|
# this copies non-std fields (eg. vwap) from the
|
||||||
|
# last datum
|
||||||
|
last[[
|
||||||
|
'time',
|
||||||
|
|
||||||
|
'open',
|
||||||
|
'high',
|
||||||
|
'low',
|
||||||
|
'close',
|
||||||
|
|
||||||
|
'volume',
|
||||||
|
]][0] = (
|
||||||
|
# epoch timestamp
|
||||||
|
next_t,
|
||||||
|
|
||||||
|
# OHLC
|
||||||
|
close,
|
||||||
|
close,
|
||||||
|
close,
|
||||||
|
close,
|
||||||
|
|
||||||
|
0, # vlm
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: in theory we could make this faster by
|
||||||
|
# copying the "last" readable value into the
|
||||||
|
# underlying larger buffer's next value and then
|
||||||
|
# incrementing the counter instead of using
|
||||||
|
# ``.push()``?
|
||||||
|
|
||||||
# write to the buffer
|
# write to the buffer
|
||||||
shm.push(last)
|
shm.push(last)
|
||||||
|
|
||||||
await broadcast(delay_s, shm=lowest_shm)
|
# broadcast increment msg to all updated subs per period
|
||||||
|
for shm_period_s in broadcasted:
|
||||||
|
await self.broadcast(
|
||||||
|
period_s=shm_period_s,
|
||||||
|
time_stamp=i_epoch,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def broadcast(
|
||||||
|
self,
|
||||||
|
period_s: float,
|
||||||
|
time_stamp: float | None = None,
|
||||||
|
|
||||||
async def broadcast(
|
) -> None:
|
||||||
delay_s: int,
|
|
||||||
shm: Optional[ShmArray] = None,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''
|
'''
|
||||||
Broadcast the given ``shm: ShmArray``'s buffer index step to any
|
Broadcast the period size and last index step value to all
|
||||||
subscribers for a given sample period.
|
subscribers for a given sample period.
|
||||||
|
|
||||||
The sent msg will include the first and last index which slice into
|
|
||||||
the buffer's non-empty data.
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
subs = sampler.subscribers.get(delay_s, ())
|
pair = self.subscribers[period_s]
|
||||||
|
|
||||||
first = last = -1
|
last_ts, subs = pair
|
||||||
|
|
||||||
if shm is None:
|
|
||||||
periods = sampler.ohlcv_shms.keys()
|
|
||||||
# if this is an update triggered by a history update there
|
|
||||||
# might not actually be any sampling bus setup since there's
|
|
||||||
# no "live feed" active yet.
|
|
||||||
if periods:
|
|
||||||
lowest = min(periods)
|
|
||||||
shm = sampler.ohlcv_shms[lowest][0]
|
|
||||||
first = shm._first.value
|
|
||||||
last = shm._last.value
|
|
||||||
|
|
||||||
|
task = trio.lowlevel.current_task()
|
||||||
|
log.debug(
|
||||||
|
f'SUBS {self.subscribers}\n'
|
||||||
|
f'PAIR {pair}\n'
|
||||||
|
f'TASK: {task}: {id(task)}\n'
|
||||||
|
f'broadcasting {period_s} -> {last_ts}\n'
|
||||||
|
# f'consumers: {subs}'
|
||||||
|
)
|
||||||
|
borked: set[tractor.MsgStream] = set()
|
||||||
for stream in subs:
|
for stream in subs:
|
||||||
try:
|
try:
|
||||||
await stream.send({
|
await stream.send({
|
||||||
'first': first,
|
'index': time_stamp or last_ts,
|
||||||
'last': last,
|
'period': period_s,
|
||||||
'index': last,
|
|
||||||
})
|
})
|
||||||
except (
|
except (
|
||||||
trio.BrokenResourceError,
|
trio.BrokenResourceError,
|
||||||
|
@ -180,6 +266,9 @@ async def broadcast(
|
||||||
log.error(
|
log.error(
|
||||||
f'{stream._ctx.chan.uid} dropped connection'
|
f'{stream._ctx.chan.uid} dropped connection'
|
||||||
)
|
)
|
||||||
|
borked.add(stream)
|
||||||
|
|
||||||
|
for stream in borked:
|
||||||
try:
|
try:
|
||||||
subs.remove(stream)
|
subs.remove(stream)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -187,53 +276,255 @@ async def broadcast(
|
||||||
f'{stream._ctx.chan.uid} sub already removed!?'
|
f'{stream._ctx.chan.uid} sub already removed!?'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def broadcast_all(self) -> None:
|
||||||
|
for period_s in self.subscribers:
|
||||||
|
await self.broadcast(period_s)
|
||||||
|
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def iter_ohlc_periods(
|
async def register_with_sampler(
|
||||||
ctx: tractor.Context,
|
ctx: tractor.Context,
|
||||||
delay_s: int,
|
period_s: float,
|
||||||
|
shms_by_period: dict[float, dict] | None = None,
|
||||||
|
|
||||||
|
open_index_stream: bool = True, # open a 2way stream for sample step msgs?
|
||||||
|
sub_for_broadcasts: bool = True, # sampler side to send step updates?
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
|
||||||
Subscribe to OHLC sampling "step" events: when the time
|
|
||||||
aggregation period increments, this event stream emits an index
|
|
||||||
event.
|
|
||||||
|
|
||||||
'''
|
get_console_log(tractor.current_actor().loglevel)
|
||||||
# add our subscription
|
incr_was_started: bool = False
|
||||||
subs = sampler.subscribers.setdefault(delay_s, [])
|
|
||||||
await ctx.started()
|
|
||||||
async with ctx.open_stream() as stream:
|
|
||||||
subs.append(stream)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# stream and block until cancelled
|
async with maybe_open_nursery(
|
||||||
await trio.sleep_forever()
|
Sampler.service_nursery
|
||||||
finally:
|
) as service_nursery:
|
||||||
try:
|
|
||||||
subs.remove(stream)
|
# init startup, create (actor-)local service nursery and start
|
||||||
except ValueError:
|
# increment task
|
||||||
log.error(
|
Sampler.service_nursery = service_nursery
|
||||||
f'iOHLC step stream was already dropped {ctx.chan.uid}?'
|
|
||||||
|
# always ensure a period subs entry exists
|
||||||
|
last_ts, subs = Sampler.subscribers[float(period_s)]
|
||||||
|
|
||||||
|
async with trio.Lock():
|
||||||
|
if Sampler.incr_task_cs is None:
|
||||||
|
Sampler.incr_task_cs = await service_nursery.start(
|
||||||
|
Sampler.increment_ohlc_buffer,
|
||||||
|
1.,
|
||||||
)
|
)
|
||||||
|
incr_was_started = True
|
||||||
|
|
||||||
|
# insert the base 1s period (for OHLC style sampling) into
|
||||||
|
# the increment buffer set to update and shift every second.
|
||||||
|
if shms_by_period is not None:
|
||||||
|
from ._sharedmem import (
|
||||||
|
attach_shm_array,
|
||||||
|
_Token,
|
||||||
|
)
|
||||||
|
for period in shms_by_period:
|
||||||
|
|
||||||
|
# load and register shm handles
|
||||||
|
shm_token_msg = shms_by_period[period]
|
||||||
|
shm = attach_shm_array(
|
||||||
|
_Token.from_msg(shm_token_msg),
|
||||||
|
readonly=False,
|
||||||
|
)
|
||||||
|
shms_by_period[period] = shm
|
||||||
|
Sampler.ohlcv_shms.setdefault(period, []).append(shm)
|
||||||
|
|
||||||
|
assert Sampler.ohlcv_shms
|
||||||
|
|
||||||
|
# unblock caller
|
||||||
|
await ctx.started(set(Sampler.ohlcv_shms.keys()))
|
||||||
|
|
||||||
|
if open_index_stream:
|
||||||
|
try:
|
||||||
|
async with ctx.open_stream() as stream:
|
||||||
|
if sub_for_broadcasts:
|
||||||
|
subs.add(stream)
|
||||||
|
|
||||||
|
# except broadcast requests from the subscriber
|
||||||
|
async for msg in stream:
|
||||||
|
if msg == 'broadcast_all':
|
||||||
|
await Sampler.broadcast_all()
|
||||||
|
finally:
|
||||||
|
if sub_for_broadcasts:
|
||||||
|
subs.remove(stream)
|
||||||
|
else:
|
||||||
|
# if no shms are passed in we just wait until cancelled
|
||||||
|
# by caller.
|
||||||
|
await trio.sleep_forever()
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# TODO: why tf isn't this working?
|
||||||
|
if shms_by_period is not None:
|
||||||
|
for period, shm in shms_by_period.items():
|
||||||
|
Sampler.ohlcv_shms[period].remove(shm)
|
||||||
|
|
||||||
|
if incr_was_started:
|
||||||
|
Sampler.incr_task_cs.cancel()
|
||||||
|
Sampler.incr_task_cs = None
|
||||||
|
|
||||||
|
|
||||||
|
async def spawn_samplerd(
|
||||||
|
|
||||||
|
loglevel: str | None = None,
|
||||||
|
**extra_tractor_kwargs
|
||||||
|
|
||||||
|
) -> bool:
|
||||||
|
'''
|
||||||
|
Daemon-side service task: start a sampling daemon for common step
|
||||||
|
update and increment count write and stream broadcasting.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from piker._daemon import Services
|
||||||
|
|
||||||
|
dname = 'samplerd'
|
||||||
|
log.info(f'Spawning `{dname}`')
|
||||||
|
|
||||||
|
# singleton lock creation of ``samplerd`` since we only ever want
|
||||||
|
# one daemon per ``pikerd`` proc tree.
|
||||||
|
# TODO: make this built-into the service api?
|
||||||
|
async with Services.locks[dname + '_singleton']:
|
||||||
|
|
||||||
|
if dname not in Services.service_tasks:
|
||||||
|
|
||||||
|
portal = await Services.actor_n.start_actor(
|
||||||
|
dname,
|
||||||
|
enable_modules=[
|
||||||
|
'piker.data._sampling',
|
||||||
|
],
|
||||||
|
loglevel=loglevel,
|
||||||
|
debug_mode=Services.debug_mode, # set by pikerd flag
|
||||||
|
**extra_tractor_kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
await Services.start_service_task(
|
||||||
|
dname,
|
||||||
|
portal,
|
||||||
|
register_with_sampler,
|
||||||
|
period_s=1,
|
||||||
|
sub_for_broadcasts=False,
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def maybe_open_samplerd(
|
||||||
|
|
||||||
|
loglevel: str | None = None,
|
||||||
|
**kwargs,
|
||||||
|
|
||||||
|
) -> tractor._portal.Portal: # noqa
|
||||||
|
'''
|
||||||
|
Client-side helper to maybe startup the ``samplerd`` service
|
||||||
|
under the ``pikerd`` tree.
|
||||||
|
|
||||||
|
'''
|
||||||
|
dname = 'samplerd'
|
||||||
|
|
||||||
|
async with maybe_spawn_daemon(
|
||||||
|
dname,
|
||||||
|
service_task_target=spawn_samplerd,
|
||||||
|
spawn_args={'loglevel': loglevel},
|
||||||
|
loglevel=loglevel,
|
||||||
|
**kwargs,
|
||||||
|
|
||||||
|
) as portal:
|
||||||
|
yield portal
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def open_sample_stream(
|
||||||
|
period_s: float,
|
||||||
|
shms_by_period: dict[float, dict] | None = None,
|
||||||
|
open_index_stream: bool = True,
|
||||||
|
sub_for_broadcasts: bool = True,
|
||||||
|
|
||||||
|
cache_key: str | None = None,
|
||||||
|
allow_new_sampler: bool = True,
|
||||||
|
|
||||||
|
) -> AsyncIterator[dict[str, float]]:
|
||||||
|
'''
|
||||||
|
Subscribe to OHLC sampling "step" events: when the time aggregation
|
||||||
|
period increments, this event stream emits an index event.
|
||||||
|
|
||||||
|
This is a client-side endpoint that does all the work of ensuring
|
||||||
|
the `samplerd` actor is up and that mult-consumer-tasks are given
|
||||||
|
a broadcast stream when possible.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# TODO: wrap this manager with the following to make it cached
|
||||||
|
# per client-multitasks entry.
|
||||||
|
# maybe_open_context(
|
||||||
|
# acm_func=partial(
|
||||||
|
# portal.open_context,
|
||||||
|
# register_with_sampler,
|
||||||
|
# ),
|
||||||
|
# key=cache_key or period_s,
|
||||||
|
# )
|
||||||
|
# if cache_hit:
|
||||||
|
# # add a new broadcast subscription for the quote stream
|
||||||
|
# # if this feed is likely already in use
|
||||||
|
# async with istream.subscribe() as bistream:
|
||||||
|
# yield bistream
|
||||||
|
# else:
|
||||||
|
|
||||||
|
async with (
|
||||||
|
# XXX: this should be singleton on a host,
|
||||||
|
# a lone broker-daemon per provider should be
|
||||||
|
# created for all practical purposes
|
||||||
|
maybe_open_samplerd() as portal,
|
||||||
|
|
||||||
|
portal.open_context(
|
||||||
|
register_with_sampler,
|
||||||
|
**{
|
||||||
|
'period_s': period_s,
|
||||||
|
'shms_by_period': shms_by_period,
|
||||||
|
'open_index_stream': open_index_stream,
|
||||||
|
'sub_for_broadcasts': sub_for_broadcasts,
|
||||||
|
},
|
||||||
|
) as (ctx, first)
|
||||||
|
):
|
||||||
|
async with (
|
||||||
|
ctx.open_stream() as istream,
|
||||||
|
|
||||||
|
# TODO: we don't need this task-bcasting right?
|
||||||
|
# istream.subscribe() as istream,
|
||||||
|
):
|
||||||
|
yield istream
|
||||||
|
|
||||||
|
|
||||||
async def sample_and_broadcast(
|
async def sample_and_broadcast(
|
||||||
|
|
||||||
bus: _FeedsBus, # noqa
|
bus: _FeedsBus, # noqa
|
||||||
shm: ShmArray,
|
rt_shm: ShmArray,
|
||||||
|
hist_shm: ShmArray,
|
||||||
quote_stream: trio.abc.ReceiveChannel,
|
quote_stream: trio.abc.ReceiveChannel,
|
||||||
brokername: str,
|
brokername: str,
|
||||||
sum_tick_vlm: bool = True,
|
sum_tick_vlm: bool = True,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
'''
|
||||||
|
`brokerd`-side task which writes latest datum sampled data.
|
||||||
|
|
||||||
|
This task is meant to run in the same actor (mem space) as the
|
||||||
|
`brokerd` real-time quote feed which is being sampled to
|
||||||
|
a ``ShmArray`` buffer.
|
||||||
|
|
||||||
|
'''
|
||||||
log.info("Started shared mem bar writer")
|
log.info("Started shared mem bar writer")
|
||||||
|
|
||||||
overruns = Counter()
|
overruns = Counter()
|
||||||
|
|
||||||
# iterate stream delivered by broker
|
# iterate stream delivered by broker
|
||||||
async for quotes in quote_stream:
|
async for quotes in quote_stream:
|
||||||
|
# print(quotes)
|
||||||
|
|
||||||
# TODO: ``numba`` this!
|
# TODO: ``numba`` this!
|
||||||
for broker_symbol, quote in quotes.items():
|
for broker_symbol, quote in quotes.items():
|
||||||
# TODO: in theory you can send the IPC msg *before* writing
|
# TODO: in theory you can send the IPC msg *before* writing
|
||||||
|
@ -257,6 +548,9 @@ async def sample_and_broadcast(
|
||||||
|
|
||||||
last = tick['price']
|
last = tick['price']
|
||||||
|
|
||||||
|
# more compact inline-way to do this assignment
|
||||||
|
# to both buffers?
|
||||||
|
for shm in [rt_shm, hist_shm]:
|
||||||
# update last entry
|
# update last entry
|
||||||
# benchmarked in the 4-5 us range
|
# benchmarked in the 4-5 us range
|
||||||
o, high, low, v = shm.array[-1][
|
o, high, low, v = shm.array[-1][
|
||||||
|
@ -293,29 +587,29 @@ async def sample_and_broadcast(
|
||||||
volume,
|
volume,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# TODO: PUT THIS IN A ``_FeedsBus.broadcast()`` method!
|
||||||
# XXX: we need to be very cautious here that no
|
# XXX: we need to be very cautious here that no
|
||||||
# context-channel is left lingering which doesn't have
|
# context-channel is left lingering which doesn't have
|
||||||
# a far end receiver actor-task. In such a case you can
|
# a far end receiver actor-task. In such a case you can
|
||||||
# end up triggering backpressure which which will
|
# end up triggering backpressure which which will
|
||||||
# eventually block this producer end of the feed and
|
# eventually block this producer end of the feed and
|
||||||
# thus other consumers still attached.
|
# thus other consumers still attached.
|
||||||
|
sub_key: str = broker_symbol.lower()
|
||||||
subs: list[
|
subs: list[
|
||||||
tuple[
|
tuple[
|
||||||
Union[tractor.MsgStream, trio.MemorySendChannel],
|
tractor.MsgStream | trio.MemorySendChannel,
|
||||||
tractor.Context,
|
float | None, # tick throttle in Hz
|
||||||
Optional[float], # tick throttle in Hz
|
|
||||||
]
|
]
|
||||||
] = bus._subscribers[broker_symbol.lower()]
|
] = bus.get_subs(sub_key)
|
||||||
|
|
||||||
# NOTE: by default the broker backend doesn't append
|
# NOTE: by default the broker backend doesn't append
|
||||||
# it's own "name" into the fqsn schema (but maybe it
|
# it's own "name" into the fqsn schema (but maybe it
|
||||||
# should?) so we have to manually generate the correct
|
# should?) so we have to manually generate the correct
|
||||||
# key here.
|
# key here.
|
||||||
bsym = f'{broker_symbol}.{brokername}'
|
fqsn = f'{broker_symbol}.{brokername}'
|
||||||
lags: int = 0
|
lags: int = 0
|
||||||
|
|
||||||
for (stream, ctx, tick_throttle) in subs:
|
for (stream, tick_throttle) in subs.copy():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with trio.move_on_after(0.2) as cs:
|
with trio.move_on_after(0.2) as cs:
|
||||||
if tick_throttle:
|
if tick_throttle:
|
||||||
|
@ -323,47 +617,39 @@ async def sample_and_broadcast(
|
||||||
# pushes to the ``uniform_rate_send()`` below.
|
# pushes to the ``uniform_rate_send()`` below.
|
||||||
try:
|
try:
|
||||||
stream.send_nowait(
|
stream.send_nowait(
|
||||||
(bsym, quote)
|
(fqsn, quote)
|
||||||
)
|
)
|
||||||
except trio.WouldBlock:
|
except trio.WouldBlock:
|
||||||
|
overruns[sub_key] += 1
|
||||||
|
ctx = stream._ctx
|
||||||
chan = ctx.chan
|
chan = ctx.chan
|
||||||
if ctx:
|
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Feed overrun {bus.brokername} ->'
|
f'Feed OVERRUN {sub_key}'
|
||||||
f'{chan.uid} !!!'
|
'@{bus.brokername} -> \n'
|
||||||
|
f'feed @ {chan.uid}\n'
|
||||||
|
f'throttle = {tick_throttle} Hz'
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
key = id(stream)
|
if overruns[sub_key] > 6:
|
||||||
overruns[key] += 1
|
|
||||||
log.warning(
|
|
||||||
f'Feed overrun {broker_symbol}'
|
|
||||||
'@{bus.brokername} -> '
|
|
||||||
f'feed @ {tick_throttle} Hz'
|
|
||||||
)
|
|
||||||
if overruns[key] > 6:
|
|
||||||
# TODO: should we check for the
|
# TODO: should we check for the
|
||||||
# context being cancelled? this
|
# context being cancelled? this
|
||||||
# could happen but the
|
# could happen but the
|
||||||
# channel-ipc-pipe is still up.
|
# channel-ipc-pipe is still up.
|
||||||
if not chan.connected():
|
if (
|
||||||
|
not chan.connected()
|
||||||
|
or ctx._cancel_called
|
||||||
|
):
|
||||||
log.warning(
|
log.warning(
|
||||||
'Dropping broken consumer:\n'
|
'Dropping broken consumer:\n'
|
||||||
f'{broker_symbol}:'
|
f'{sub_key}:'
|
||||||
f'{ctx.cid}@{chan.uid}'
|
f'{ctx.cid}@{chan.uid}'
|
||||||
)
|
)
|
||||||
await stream.aclose()
|
await stream.aclose()
|
||||||
raise trio.BrokenResourceError
|
raise trio.BrokenResourceError
|
||||||
else:
|
|
||||||
log.warning(
|
|
||||||
'Feed getting overrun bro!\n'
|
|
||||||
f'{broker_symbol}:'
|
|
||||||
f'{ctx.cid}@{chan.uid}'
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
await stream.send(
|
await stream.send(
|
||||||
{bsym: quote}
|
{fqsn: quote}
|
||||||
)
|
)
|
||||||
|
|
||||||
if cs.cancelled_caught:
|
if cs.cancelled_caught:
|
||||||
|
@ -376,6 +662,7 @@ async def sample_and_broadcast(
|
||||||
trio.ClosedResourceError,
|
trio.ClosedResourceError,
|
||||||
trio.EndOfChannel,
|
trio.EndOfChannel,
|
||||||
):
|
):
|
||||||
|
ctx = stream._ctx
|
||||||
chan = ctx.chan
|
chan = ctx.chan
|
||||||
if ctx:
|
if ctx:
|
||||||
log.warning(
|
log.warning(
|
||||||
|
@ -391,20 +678,69 @@ async def sample_and_broadcast(
|
||||||
# so far seems like no since this should all
|
# so far seems like no since this should all
|
||||||
# be single-threaded. Doing it anyway though
|
# be single-threaded. Doing it anyway though
|
||||||
# since there seems to be some kinda race..
|
# since there seems to be some kinda race..
|
||||||
try:
|
bus.remove_subs(
|
||||||
subs.remove((stream, tick_throttle))
|
sub_key,
|
||||||
except ValueError:
|
{(stream, tick_throttle)},
|
||||||
log.error(
|
|
||||||
f'Stream was already removed from subs!?\n'
|
|
||||||
f'{broker_symbol}:'
|
|
||||||
f'{ctx.cid}@{chan.uid}'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# a working tick-type-classes template
|
||||||
|
_tick_groups = {
|
||||||
|
'clears': {'trade', 'dark_trade', 'last'},
|
||||||
|
'bids': {'bid', 'bsize'},
|
||||||
|
'asks': {'ask', 'asize'},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def frame_ticks(
|
||||||
|
first_quote: dict,
|
||||||
|
last_quote: dict,
|
||||||
|
ticks_by_type: dict,
|
||||||
|
) -> None:
|
||||||
|
# append quotes since last iteration into the last quote's
|
||||||
|
# tick array/buffer.
|
||||||
|
ticks = last_quote.get('ticks')
|
||||||
|
|
||||||
|
# TODO: once we decide to get fancy really we should
|
||||||
|
# have a shared mem tick buffer that is just
|
||||||
|
# continually filled and the UI just ready from it
|
||||||
|
# at it's display rate.
|
||||||
|
if ticks:
|
||||||
|
# TODO: do we need this any more or can we just
|
||||||
|
# expect the receiver to unwind the below
|
||||||
|
# `ticks_by_type: dict`?
|
||||||
|
# => undwinding would potentially require a
|
||||||
|
# `dict[str, set | list]` instead with an
|
||||||
|
# included `'types' field which is an (ordered)
|
||||||
|
# set of tick type fields in the order which
|
||||||
|
# types arrived?
|
||||||
|
first_quote['ticks'].extend(ticks)
|
||||||
|
|
||||||
|
# XXX: build a tick-by-type table of lists
|
||||||
|
# of tick messages. This allows for less
|
||||||
|
# iteration on the receiver side by allowing for
|
||||||
|
# a single "latest tick event" look up by
|
||||||
|
# indexing the last entry in each sub-list.
|
||||||
|
# tbt = {
|
||||||
|
# 'types': ['bid', 'asize', 'last', .. '<type_n>'],
|
||||||
|
|
||||||
|
# 'bid': [tick0, tick1, tick2, .., tickn],
|
||||||
|
# 'asize': [tick0, tick1, tick2, .., tickn],
|
||||||
|
# 'last': [tick0, tick1, tick2, .., tickn],
|
||||||
|
# ...
|
||||||
|
# '<type_n>': [tick0, tick1, tick2, .., tickn],
|
||||||
|
# }
|
||||||
|
|
||||||
|
# append in reverse FIFO order for in-order iteration on
|
||||||
|
# receiver side.
|
||||||
|
for tick in ticks:
|
||||||
|
ttype = tick['type']
|
||||||
|
ticks_by_type[ttype].append(tick)
|
||||||
|
|
||||||
|
|
||||||
# TODO: a less naive throttler, here's some snippets:
|
# TODO: a less naive throttler, here's some snippets:
|
||||||
# token bucket by njs:
|
# token bucket by njs:
|
||||||
# https://gist.github.com/njsmith/7ea44ec07e901cb78ebe1dd8dd846cb9
|
# https://gist.github.com/njsmith/7ea44ec07e901cb78ebe1dd8dd846cb9
|
||||||
|
|
||||||
async def uniform_rate_send(
|
async def uniform_rate_send(
|
||||||
|
|
||||||
rate: float,
|
rate: float,
|
||||||
|
@ -415,6 +751,9 @@ async def uniform_rate_send(
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
|
# try not to error-out on overruns of the subscribed (chart) client
|
||||||
|
stream._ctx._backpressure = True
|
||||||
|
|
||||||
# TODO: compute the approx overhead latency per cycle
|
# TODO: compute the approx overhead latency per cycle
|
||||||
left_to_sleep = throttle_period = 1/rate - 0.000616
|
left_to_sleep = throttle_period = 1/rate - 0.000616
|
||||||
|
|
||||||
|
@ -424,6 +763,12 @@ async def uniform_rate_send(
|
||||||
diff = 0
|
diff = 0
|
||||||
|
|
||||||
task_status.started()
|
task_status.started()
|
||||||
|
ticks_by_type: defaultdict[
|
||||||
|
str,
|
||||||
|
list[dict],
|
||||||
|
] = defaultdict(list)
|
||||||
|
|
||||||
|
clear_types = _tick_groups['clears']
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
|
@ -442,34 +787,17 @@ async def uniform_rate_send(
|
||||||
|
|
||||||
if not first_quote:
|
if not first_quote:
|
||||||
first_quote = last_quote
|
first_quote = last_quote
|
||||||
|
# first_quote['tbt'] = ticks_by_type
|
||||||
|
|
||||||
if (throttle_period - diff) > 0:
|
if (throttle_period - diff) > 0:
|
||||||
# received a quote but the send cycle period hasn't yet
|
# received a quote but the send cycle period hasn't yet
|
||||||
# expired we aren't supposed to send yet so append
|
# expired we aren't supposed to send yet so append
|
||||||
# to the tick frame.
|
# to the tick frame.
|
||||||
|
frame_ticks(
|
||||||
# append quotes since last iteration into the last quote's
|
first_quote,
|
||||||
# tick array/buffer.
|
last_quote,
|
||||||
ticks = last_quote.get('ticks')
|
ticks_by_type,
|
||||||
|
)
|
||||||
# XXX: idea for frame type data structure we could
|
|
||||||
# use on the wire instead of a simple list?
|
|
||||||
# frames = {
|
|
||||||
# 'index': ['type_a', 'type_c', 'type_n', 'type_n'],
|
|
||||||
|
|
||||||
# 'type_a': [tick0, tick1, tick2, .., tickn],
|
|
||||||
# 'type_b': [tick0, tick1, tick2, .., tickn],
|
|
||||||
# 'type_c': [tick0, tick1, tick2, .., tickn],
|
|
||||||
# ...
|
|
||||||
# 'type_n': [tick0, tick1, tick2, .., tickn],
|
|
||||||
# }
|
|
||||||
|
|
||||||
# TODO: once we decide to get fancy really we should
|
|
||||||
# have a shared mem tick buffer that is just
|
|
||||||
# continually filled and the UI just ready from it
|
|
||||||
# at it's display rate.
|
|
||||||
if ticks:
|
|
||||||
first_quote['ticks'].extend(ticks)
|
|
||||||
|
|
||||||
# send cycle isn't due yet so continue waiting
|
# send cycle isn't due yet so continue waiting
|
||||||
continue
|
continue
|
||||||
|
@ -486,12 +814,35 @@ async def uniform_rate_send(
|
||||||
# received quote ASAP.
|
# received quote ASAP.
|
||||||
sym, first_quote = await quote_stream.receive()
|
sym, first_quote = await quote_stream.receive()
|
||||||
|
|
||||||
|
frame_ticks(
|
||||||
|
first_quote,
|
||||||
|
first_quote,
|
||||||
|
ticks_by_type,
|
||||||
|
)
|
||||||
|
|
||||||
# we have a quote already so send it now.
|
# we have a quote already so send it now.
|
||||||
|
|
||||||
|
with trio.move_on_after(throttle_period) as cs:
|
||||||
|
while (
|
||||||
|
not set(ticks_by_type).intersection(clear_types)
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
sym, last_quote = await quote_stream.receive()
|
||||||
|
except trio.EndOfChannel:
|
||||||
|
log.exception(f"feed for {stream} ended?")
|
||||||
|
break
|
||||||
|
|
||||||
|
frame_ticks(
|
||||||
|
first_quote,
|
||||||
|
last_quote,
|
||||||
|
ticks_by_type,
|
||||||
|
)
|
||||||
|
|
||||||
# measured_rate = 1 / (time.time() - last_send)
|
# measured_rate = 1 / (time.time() - last_send)
|
||||||
# log.info(
|
# log.info(
|
||||||
# f'`{sym}` throttled send hz: {round(measured_rate, ndigits=1)}'
|
# f'`{sym}` throttled send hz: {round(measured_rate, ndigits=1)}'
|
||||||
# )
|
# )
|
||||||
|
first_quote['tbt'] = ticks_by_type
|
||||||
|
|
||||||
# TODO: now if only we could sync this to the display
|
# TODO: now if only we could sync this to the display
|
||||||
# rate timing exactly lul
|
# rate timing exactly lul
|
||||||
|
@ -517,3 +868,4 @@ async def uniform_rate_send(
|
||||||
first_quote = last_quote = None
|
first_quote = last_quote = None
|
||||||
diff = 0
|
diff = 0
|
||||||
last_send = time.time()
|
last_send = time.time()
|
||||||
|
ticks_by_type.clear()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# piker: trading gear for hackers
|
# piker: trading gear for hackers
|
||||||
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
@ -27,13 +27,14 @@ from multiprocessing.shared_memory import SharedMemory, _USE_POSIX
|
||||||
if _USE_POSIX:
|
if _USE_POSIX:
|
||||||
from _posixshmem import shm_unlink
|
from _posixshmem import shm_unlink
|
||||||
|
|
||||||
import tractor
|
# import msgspec
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pydantic import BaseModel
|
|
||||||
from numpy.lib import recfunctions as rfn
|
from numpy.lib import recfunctions as rfn
|
||||||
|
import tractor
|
||||||
|
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
from ._source import base_iohlc_dtype
|
from ._source import base_iohlc_dtype
|
||||||
|
from .types import Struct
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
@ -49,7 +50,11 @@ _rt_buffer_start = int((_days_worth - 1) * _secs_in_day)
|
||||||
|
|
||||||
|
|
||||||
def cuckoff_mantracker():
|
def cuckoff_mantracker():
|
||||||
|
'''
|
||||||
|
Disable all ``multiprocessing``` "resource tracking" machinery since
|
||||||
|
it's an absolute multi-threaded mess of non-SC madness.
|
||||||
|
|
||||||
|
'''
|
||||||
from multiprocessing import resource_tracker as mantracker
|
from multiprocessing import resource_tracker as mantracker
|
||||||
|
|
||||||
# Tell the "resource tracker" thing to fuck off.
|
# Tell the "resource tracker" thing to fuck off.
|
||||||
|
@ -107,36 +112,39 @@ class SharedInt:
|
||||||
log.warning(f'Shm for {name} already unlinked?')
|
log.warning(f'Shm for {name} already unlinked?')
|
||||||
|
|
||||||
|
|
||||||
class _Token(BaseModel):
|
class _Token(Struct, frozen=True):
|
||||||
'''
|
'''
|
||||||
Internal represenation of a shared memory "token"
|
Internal represenation of a shared memory "token"
|
||||||
which can be used to key a system wide post shm entry.
|
which can be used to key a system wide post shm entry.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
class Config:
|
|
||||||
frozen = True
|
|
||||||
|
|
||||||
shm_name: str # this servers as a "key" value
|
shm_name: str # this servers as a "key" value
|
||||||
shm_first_index_name: str
|
shm_first_index_name: str
|
||||||
shm_last_index_name: str
|
shm_last_index_name: str
|
||||||
dtype_descr: tuple
|
dtype_descr: tuple
|
||||||
|
size: int # in struct-array index / row terms
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dtype(self) -> np.dtype:
|
def dtype(self) -> np.dtype:
|
||||||
return np.dtype(list(map(tuple, self.dtype_descr))).descr
|
return np.dtype(list(map(tuple, self.dtype_descr))).descr
|
||||||
|
|
||||||
def as_msg(self):
|
def as_msg(self):
|
||||||
return self.dict()
|
return self.to_dict()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_msg(cls, msg: dict) -> _Token:
|
def from_msg(cls, msg: dict) -> _Token:
|
||||||
if isinstance(msg, _Token):
|
if isinstance(msg, _Token):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
# TODO: native struct decoding
|
||||||
|
# return _token_dec.decode(msg)
|
||||||
|
|
||||||
msg['dtype_descr'] = tuple(map(tuple, msg['dtype_descr']))
|
msg['dtype_descr'] = tuple(map(tuple, msg['dtype_descr']))
|
||||||
return _Token(**msg)
|
return _Token(**msg)
|
||||||
|
|
||||||
|
|
||||||
|
# _token_dec = msgspec.msgpack.Decoder(_Token)
|
||||||
|
|
||||||
# TODO: this api?
|
# TODO: this api?
|
||||||
# _known_tokens = tractor.ActorVar('_shm_tokens', {})
|
# _known_tokens = tractor.ActorVar('_shm_tokens', {})
|
||||||
# _known_tokens = tractor.ContextStack('_known_tokens', )
|
# _known_tokens = tractor.ContextStack('_known_tokens', )
|
||||||
|
@ -155,6 +163,7 @@ def get_shm_token(key: str) -> _Token:
|
||||||
|
|
||||||
def _make_token(
|
def _make_token(
|
||||||
key: str,
|
key: str,
|
||||||
|
size: int,
|
||||||
dtype: Optional[np.dtype] = None,
|
dtype: Optional[np.dtype] = None,
|
||||||
) -> _Token:
|
) -> _Token:
|
||||||
'''
|
'''
|
||||||
|
@ -167,7 +176,8 @@ def _make_token(
|
||||||
shm_name=key,
|
shm_name=key,
|
||||||
shm_first_index_name=key + "_first",
|
shm_first_index_name=key + "_first",
|
||||||
shm_last_index_name=key + "_last",
|
shm_last_index_name=key + "_last",
|
||||||
dtype_descr=np.dtype(dtype).descr
|
dtype_descr=tuple(np.dtype(dtype).descr),
|
||||||
|
size=size,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -219,6 +229,7 @@ class ShmArray:
|
||||||
shm_first_index_name=self._first._shm.name,
|
shm_first_index_name=self._first._shm.name,
|
||||||
shm_last_index_name=self._last._shm.name,
|
shm_last_index_name=self._last._shm.name,
|
||||||
dtype_descr=tuple(self._array.dtype.descr),
|
dtype_descr=tuple(self._array.dtype.descr),
|
||||||
|
size=self._len,
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -433,7 +444,7 @@ class ShmArray:
|
||||||
def open_shm_array(
|
def open_shm_array(
|
||||||
|
|
||||||
key: Optional[str] = None,
|
key: Optional[str] = None,
|
||||||
size: int = _default_size,
|
size: int = _default_size, # see above
|
||||||
dtype: Optional[np.dtype] = None,
|
dtype: Optional[np.dtype] = None,
|
||||||
readonly: bool = False,
|
readonly: bool = False,
|
||||||
|
|
||||||
|
@ -464,7 +475,8 @@ def open_shm_array(
|
||||||
|
|
||||||
token = _make_token(
|
token = _make_token(
|
||||||
key=key,
|
key=key,
|
||||||
dtype=dtype
|
size=size,
|
||||||
|
dtype=dtype,
|
||||||
)
|
)
|
||||||
|
|
||||||
# create single entry arrays for storing an first and last indices
|
# create single entry arrays for storing an first and last indices
|
||||||
|
@ -516,15 +528,15 @@ def open_shm_array(
|
||||||
# "unlink" created shm on process teardown by
|
# "unlink" created shm on process teardown by
|
||||||
# pushing teardown calls onto actor context stack
|
# pushing teardown calls onto actor context stack
|
||||||
|
|
||||||
tractor._actor._lifetime_stack.callback(shmarr.close)
|
stack = tractor.current_actor().lifetime_stack
|
||||||
tractor._actor._lifetime_stack.callback(shmarr.destroy)
|
stack.callback(shmarr.close)
|
||||||
|
stack.callback(shmarr.destroy)
|
||||||
|
|
||||||
return shmarr
|
return shmarr
|
||||||
|
|
||||||
|
|
||||||
def attach_shm_array(
|
def attach_shm_array(
|
||||||
token: tuple[str, str, tuple[str, str]],
|
token: tuple[str, str, tuple[str, str]],
|
||||||
size: int = _default_size,
|
|
||||||
readonly: bool = True,
|
readonly: bool = True,
|
||||||
|
|
||||||
) -> ShmArray:
|
) -> ShmArray:
|
||||||
|
@ -563,7 +575,7 @@ def attach_shm_array(
|
||||||
raise _err
|
raise _err
|
||||||
|
|
||||||
shmarr = np.ndarray(
|
shmarr = np.ndarray(
|
||||||
(size,),
|
(token.size,),
|
||||||
dtype=token.dtype,
|
dtype=token.dtype,
|
||||||
buffer=shm.buf
|
buffer=shm.buf
|
||||||
)
|
)
|
||||||
|
@ -602,8 +614,8 @@ def attach_shm_array(
|
||||||
if key not in _known_tokens:
|
if key not in _known_tokens:
|
||||||
_known_tokens[key] = token
|
_known_tokens[key] = token
|
||||||
|
|
||||||
# "close" attached shm on process teardown
|
# "close" attached shm on actor teardown
|
||||||
tractor._actor._lifetime_stack.callback(sha.close)
|
tractor.current_actor().lifetime_stack.callback(sha.close)
|
||||||
|
|
||||||
return sha
|
return sha
|
||||||
|
|
||||||
|
@ -631,6 +643,7 @@ def maybe_open_shm_array(
|
||||||
use ``attach_shm_array``.
|
use ``attach_shm_array``.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
size = kwargs.pop('size', _default_size)
|
||||||
try:
|
try:
|
||||||
# see if we already know this key
|
# see if we already know this key
|
||||||
token = _known_tokens[key]
|
token = _known_tokens[key]
|
||||||
|
@ -638,7 +651,11 @@ def maybe_open_shm_array(
|
||||||
except KeyError:
|
except KeyError:
|
||||||
log.warning(f"Could not find {key} in shms cache")
|
log.warning(f"Could not find {key} in shms cache")
|
||||||
if dtype:
|
if dtype:
|
||||||
token = _make_token(key, dtype)
|
token = _make_token(
|
||||||
|
key,
|
||||||
|
size=size,
|
||||||
|
dtype=dtype,
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
return attach_shm_array(token=token, **kwargs), False
|
return attach_shm_array(token=token, **kwargs), False
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
|
|
@ -23,7 +23,8 @@ import decimal
|
||||||
|
|
||||||
from bidict import bidict
|
from bidict import bidict
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pydantic import BaseModel
|
|
||||||
|
from .types import Struct
|
||||||
# from numba import from_dtype
|
# from numba import from_dtype
|
||||||
|
|
||||||
|
|
||||||
|
@ -126,7 +127,7 @@ def unpack_fqsn(fqsn: str) -> tuple[str, str, str]:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Symbol(BaseModel):
|
class Symbol(Struct):
|
||||||
'''
|
'''
|
||||||
I guess this is some kinda container thing for dealing with
|
I guess this is some kinda container thing for dealing with
|
||||||
all the different meta-data formats from brokers?
|
all the different meta-data formats from brokers?
|
||||||
|
@ -152,9 +153,7 @@ class Symbol(BaseModel):
|
||||||
info: dict[str, Any],
|
info: dict[str, Any],
|
||||||
suffix: str = '',
|
suffix: str = '',
|
||||||
|
|
||||||
# XXX: like wtf..
|
) -> Symbol:
|
||||||
# ) -> 'Symbol':
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
tick_size = info.get('price_tick_size', 0.01)
|
tick_size = info.get('price_tick_size', 0.01)
|
||||||
lot_tick_size = info.get('lot_tick_size', 0.0)
|
lot_tick_size = info.get('lot_tick_size', 0.0)
|
||||||
|
@ -175,9 +174,7 @@ class Symbol(BaseModel):
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
info: dict[str, Any],
|
info: dict[str, Any],
|
||||||
|
|
||||||
# XXX: like wtf..
|
) -> Symbol:
|
||||||
# ) -> 'Symbol':
|
|
||||||
) -> None:
|
|
||||||
broker, key, suffix = unpack_fqsn(fqsn)
|
broker, key, suffix = unpack_fqsn(fqsn)
|
||||||
return cls.from_broker_info(
|
return cls.from_broker_info(
|
||||||
broker,
|
broker,
|
||||||
|
@ -221,6 +218,10 @@ class Symbol(BaseModel):
|
||||||
else:
|
else:
|
||||||
return (key, broker)
|
return (key, broker)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fqsn(self) -> str:
|
||||||
|
return '.'.join(self.tokens()).lower()
|
||||||
|
|
||||||
def front_fqsn(self) -> str:
|
def front_fqsn(self) -> str:
|
||||||
'''
|
'''
|
||||||
fqsn = "fully qualified symbol name"
|
fqsn = "fully qualified symbol name"
|
||||||
|
@ -240,7 +241,7 @@ class Symbol(BaseModel):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
tokens = self.tokens()
|
tokens = self.tokens()
|
||||||
fqsn = '.'.join(tokens)
|
fqsn = '.'.join(map(str.lower, tokens))
|
||||||
return fqsn
|
return fqsn
|
||||||
|
|
||||||
def iterfqsns(self) -> list[str]:
|
def iterfqsns(self) -> list[str]:
|
||||||
|
|
|
@ -18,13 +18,24 @@
|
||||||
ToOlS fOr CoPInG wITh "tHE wEB" protocols.
|
ToOlS fOr CoPInG wITh "tHE wEB" protocols.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from contextlib import asynccontextmanager, AsyncExitStack
|
from contextlib import (
|
||||||
|
asynccontextmanager,
|
||||||
|
AsyncExitStack,
|
||||||
|
)
|
||||||
|
from itertools import count
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from typing import Any, Callable, AsyncGenerator
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Optional,
|
||||||
|
Callable,
|
||||||
|
AsyncGenerator,
|
||||||
|
Iterable,
|
||||||
|
)
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
import trio_websocket
|
import trio_websocket
|
||||||
|
from wsproto.utilities import LocalProtocolError
|
||||||
from trio_websocket._impl import (
|
from trio_websocket._impl import (
|
||||||
ConnectionClosed,
|
ConnectionClosed,
|
||||||
DisconnectionTimeout,
|
DisconnectionTimeout,
|
||||||
|
@ -35,43 +46,53 @@ from trio_websocket._impl import (
|
||||||
|
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
|
||||||
|
from .types import Struct
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class NoBsWs:
|
class NoBsWs:
|
||||||
"""Make ``trio_websocket`` sockets stay up no matter the bs.
|
'''
|
||||||
|
Make ``trio_websocket`` sockets stay up no matter the bs.
|
||||||
|
|
||||||
"""
|
You can provide a ``fixture`` async-context-manager which will be
|
||||||
|
enter/exitted around each reconnect operation.
|
||||||
|
'''
|
||||||
recon_errors = (
|
recon_errors = (
|
||||||
ConnectionClosed,
|
ConnectionClosed,
|
||||||
DisconnectionTimeout,
|
DisconnectionTimeout,
|
||||||
ConnectionRejected,
|
ConnectionRejected,
|
||||||
HandshakeError,
|
HandshakeError,
|
||||||
ConnectionTimeout,
|
ConnectionTimeout,
|
||||||
|
LocalProtocolError,
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
url: str,
|
url: str,
|
||||||
token: str,
|
|
||||||
stack: AsyncExitStack,
|
stack: AsyncExitStack,
|
||||||
fixture: Callable,
|
fixture: Optional[Callable] = None,
|
||||||
serializer: ModuleType = json,
|
serializer: ModuleType = json
|
||||||
):
|
):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.token = token
|
|
||||||
self.fixture = fixture
|
self.fixture = fixture
|
||||||
self._stack = stack
|
self._stack = stack
|
||||||
self._ws: 'WebSocketConnection' = None # noqa
|
self._ws: 'WebSocketConnection' = None # noqa
|
||||||
|
|
||||||
|
# TODO: is there some method we can call
|
||||||
|
# on the underlying `._ws` to get this?
|
||||||
|
self._connected: bool = False
|
||||||
|
|
||||||
async def _connect(
|
async def _connect(
|
||||||
self,
|
self,
|
||||||
tries: int = 1000,
|
tries: int = 1000,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
|
self._connected = False
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
await self._stack.aclose()
|
await self._stack.aclose()
|
||||||
except (DisconnectionTimeout, RuntimeError):
|
except self.recon_errors:
|
||||||
await trio.sleep(0.5)
|
await trio.sleep(0.5)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
@ -82,19 +103,18 @@ class NoBsWs:
|
||||||
self._ws = await self._stack.enter_async_context(
|
self._ws = await self._stack.enter_async_context(
|
||||||
trio_websocket.open_websocket_url(self.url)
|
trio_websocket.open_websocket_url(self.url)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.fixture is not None:
|
||||||
# rerun user code fixture
|
# rerun user code fixture
|
||||||
if self.token == '':
|
|
||||||
ret = await self._stack.enter_async_context(
|
ret = await self._stack.enter_async_context(
|
||||||
self.fixture(self)
|
self.fixture(self)
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
ret = await self._stack.enter_async_context(
|
|
||||||
self.fixture(self, self.token)
|
|
||||||
)
|
|
||||||
|
|
||||||
assert ret is None
|
assert ret is None
|
||||||
|
|
||||||
log.info(f'Connection success: {self.url}')
|
log.info(f'Connection success: {self.url}')
|
||||||
|
|
||||||
|
self._connected = True
|
||||||
return self._ws
|
return self._ws
|
||||||
|
|
||||||
except self.recon_errors as err:
|
except self.recon_errors as err:
|
||||||
|
@ -104,11 +124,15 @@ class NoBsWs:
|
||||||
f'{type(err)}...retry attempt {i}'
|
f'{type(err)}...retry attempt {i}'
|
||||||
)
|
)
|
||||||
await trio.sleep(0.5)
|
await trio.sleep(0.5)
|
||||||
|
self._connected = False
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
log.exception('ws connection fail...')
|
log.exception('ws connection fail...')
|
||||||
raise last_err
|
raise last_err
|
||||||
|
|
||||||
|
def connected(self) -> bool:
|
||||||
|
return self._connected
|
||||||
|
|
||||||
async def send_msg(
|
async def send_msg(
|
||||||
self,
|
self,
|
||||||
data: Any,
|
data: Any,
|
||||||
|
@ -128,21 +152,26 @@ class NoBsWs:
|
||||||
except self.recon_errors:
|
except self.recon_errors:
|
||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
|
def __aiter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __anext__(self):
|
||||||
|
return await self.recv_msg()
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def open_autorecon_ws(
|
async def open_autorecon_ws(
|
||||||
url: str,
|
url: str,
|
||||||
|
|
||||||
# TODO: proper type annot smh
|
# TODO: proper type cannot smh
|
||||||
fixture: Callable,
|
fixture: Optional[Callable] = None,
|
||||||
# used for authenticated websockets
|
|
||||||
token: str = '',
|
|
||||||
) -> AsyncGenerator[tuple[...], NoBsWs]:
|
) -> AsyncGenerator[tuple[...], NoBsWs]:
|
||||||
"""Apparently we can QoS for all sorts of reasons..so catch em.
|
"""Apparently we can QoS for all sorts of reasons..so catch em.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
async with AsyncExitStack() as stack:
|
async with AsyncExitStack() as stack:
|
||||||
ws = NoBsWs(url, token, stack, fixture=fixture)
|
ws = NoBsWs(url, stack, fixture=fixture)
|
||||||
await ws._connect()
|
await ws._connect()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -150,3 +179,114 @@ async def open_autorecon_ws(
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
await stack.aclose()
|
await stack.aclose()
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
JSONRPC response-request style machinery for transparent multiplexing of msgs
|
||||||
|
over a NoBsWs.
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class JSONRPCResult(Struct):
|
||||||
|
id: int
|
||||||
|
jsonrpc: str = '2.0'
|
||||||
|
result: Optional[dict] = None
|
||||||
|
error: Optional[dict] = None
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def open_jsonrpc_session(
|
||||||
|
url: str,
|
||||||
|
start_id: int = 0,
|
||||||
|
response_type: type = JSONRPCResult,
|
||||||
|
request_type: Optional[type] = None,
|
||||||
|
request_hook: Optional[Callable] = None,
|
||||||
|
error_hook: Optional[Callable] = None,
|
||||||
|
) -> Callable[[str, dict], dict]:
|
||||||
|
|
||||||
|
async with (
|
||||||
|
trio.open_nursery() as n,
|
||||||
|
open_autorecon_ws(url) as ws
|
||||||
|
):
|
||||||
|
rpc_id: Iterable = count(start_id)
|
||||||
|
rpc_results: dict[int, dict] = {}
|
||||||
|
|
||||||
|
async def json_rpc(method: str, params: dict) -> dict:
|
||||||
|
'''
|
||||||
|
perform a json rpc call and wait for the result, raise exception in
|
||||||
|
case of error field present on response
|
||||||
|
'''
|
||||||
|
msg = {
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'id': next(rpc_id),
|
||||||
|
'method': method,
|
||||||
|
'params': params
|
||||||
|
}
|
||||||
|
_id = msg['id']
|
||||||
|
|
||||||
|
rpc_results[_id] = {
|
||||||
|
'result': None,
|
||||||
|
'event': trio.Event()
|
||||||
|
}
|
||||||
|
|
||||||
|
await ws.send_msg(msg)
|
||||||
|
|
||||||
|
await rpc_results[_id]['event'].wait()
|
||||||
|
|
||||||
|
ret = rpc_results[_id]['result']
|
||||||
|
|
||||||
|
del rpc_results[_id]
|
||||||
|
|
||||||
|
if ret.error is not None:
|
||||||
|
raise Exception(json.dumps(ret.error, indent=4))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
async def recv_task():
|
||||||
|
'''
|
||||||
|
receives every ws message and stores it in its corresponding
|
||||||
|
result field, then sets the event to wakeup original sender
|
||||||
|
tasks. also recieves responses to requests originated from
|
||||||
|
the server side.
|
||||||
|
|
||||||
|
'''
|
||||||
|
async for msg in ws:
|
||||||
|
match msg:
|
||||||
|
case {
|
||||||
|
'result': _,
|
||||||
|
'id': mid,
|
||||||
|
} if res_entry := rpc_results.get(mid):
|
||||||
|
|
||||||
|
res_entry['result'] = response_type(**msg)
|
||||||
|
res_entry['event'].set()
|
||||||
|
|
||||||
|
case {
|
||||||
|
'result': _,
|
||||||
|
'id': mid,
|
||||||
|
} if not rpc_results.get(mid):
|
||||||
|
log.warning(
|
||||||
|
f'Unexpected ws msg: {json.dumps(msg, indent=4)}'
|
||||||
|
)
|
||||||
|
|
||||||
|
case {
|
||||||
|
'method': _,
|
||||||
|
'params': _,
|
||||||
|
}:
|
||||||
|
log.debug(f'Recieved\n{msg}')
|
||||||
|
if request_hook:
|
||||||
|
await request_hook(request_type(**msg))
|
||||||
|
|
||||||
|
case {
|
||||||
|
'error': error
|
||||||
|
}:
|
||||||
|
log.warning(f'Recieved\n{error}')
|
||||||
|
if error_hook:
|
||||||
|
await error_hook(response_type(**msg))
|
||||||
|
|
||||||
|
case _:
|
||||||
|
log.warning(f'Unhandled JSON-RPC msg!?\n{msg}')
|
||||||
|
|
||||||
|
n.start_soon(recv_task)
|
||||||
|
yield json_rpc
|
||||||
|
n.cancel_scope.cancel()
|
||||||
|
|
1653
piker/data/feed.py
1653
piker/data/feed.py
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,239 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
"""
|
||||||
|
abstractions for organizing, managing and generally operating-on
|
||||||
|
real-time data processing data-structures.
|
||||||
|
|
||||||
|
"Streams, flumes, cascades and flows.."
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
|
from functools import partial
|
||||||
|
from typing import (
|
||||||
|
AsyncIterator,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
|
import tractor
|
||||||
|
from tractor.trionics import (
|
||||||
|
maybe_open_context,
|
||||||
|
)
|
||||||
|
import pendulum
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from .types import Struct
|
||||||
|
from ._source import (
|
||||||
|
Symbol,
|
||||||
|
)
|
||||||
|
from ._sharedmem import (
|
||||||
|
attach_shm_array,
|
||||||
|
ShmArray,
|
||||||
|
_Token,
|
||||||
|
)
|
||||||
|
from ._sampling import (
|
||||||
|
open_sample_stream,
|
||||||
|
)
|
||||||
|
# from .._profile import (
|
||||||
|
# Profiler,
|
||||||
|
# pg_profile_enabled,
|
||||||
|
# )
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
# from pyqtgraph import PlotItem
|
||||||
|
from .feed import Feed
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: ideas for further abstractions as per
|
||||||
|
# https://github.com/pikers/piker/issues/216 and
|
||||||
|
# https://github.com/pikers/piker/issues/270:
|
||||||
|
# - a ``Cascade`` would be the minimal "connection" of 2 ``Flumes``
|
||||||
|
# as per circuit parlance:
|
||||||
|
# https://en.wikipedia.org/wiki/Two-port_network#Cascade_connection
|
||||||
|
# - could cover the combination of our `FspAdmin` and the
|
||||||
|
# backend `.fsp._engine` related machinery to "connect" one flume
|
||||||
|
# to another?
|
||||||
|
# - a (financial signal) ``Flow`` would be the a "collection" of such
|
||||||
|
# minmial cascades. Some engineering based jargon concepts:
|
||||||
|
# - https://en.wikipedia.org/wiki/Signal_chain
|
||||||
|
# - https://en.wikipedia.org/wiki/Daisy_chain_(electrical_engineering)
|
||||||
|
# - https://en.wikipedia.org/wiki/Audio_signal_flow
|
||||||
|
# - https://en.wikipedia.org/wiki/Digital_signal_processing#Implementation
|
||||||
|
# - https://en.wikipedia.org/wiki/Dataflow_programming
|
||||||
|
# - https://en.wikipedia.org/wiki/Signal_programming
|
||||||
|
# - https://en.wikipedia.org/wiki/Incremental_computing
|
||||||
|
|
||||||
|
|
||||||
|
class Flume(Struct):
|
||||||
|
'''
|
||||||
|
Composite reference type which points to all the addressing handles
|
||||||
|
and other meta-data necessary for the read, measure and management
|
||||||
|
of a set of real-time updated data flows.
|
||||||
|
|
||||||
|
Can be thought of as a "flow descriptor" or "flow frame" which
|
||||||
|
describes the high level properties of a set of data flows that can
|
||||||
|
be used seamlessly across process-memory boundaries.
|
||||||
|
|
||||||
|
Each instance's sub-components normally includes:
|
||||||
|
- a msg oriented quote stream provided via an IPC transport
|
||||||
|
- history and real-time shm buffers which are both real-time
|
||||||
|
updated and backfilled.
|
||||||
|
- associated startup indexing information related to both buffer
|
||||||
|
real-time-append and historical prepend addresses.
|
||||||
|
- low level APIs to read and measure the updated data and manage
|
||||||
|
queuing properties.
|
||||||
|
|
||||||
|
'''
|
||||||
|
symbol: Symbol
|
||||||
|
first_quote: dict
|
||||||
|
_rt_shm_token: _Token
|
||||||
|
|
||||||
|
# optional since some data flows won't have a "downsampled" history
|
||||||
|
# buffer/stream (eg. FSPs).
|
||||||
|
_hist_shm_token: _Token | None = None
|
||||||
|
|
||||||
|
# private shm refs loaded dynamically from tokens
|
||||||
|
_hist_shm: ShmArray | None = None
|
||||||
|
_rt_shm: ShmArray | None = None
|
||||||
|
|
||||||
|
stream: tractor.MsgStream | None = None
|
||||||
|
izero_hist: int = 0
|
||||||
|
izero_rt: int = 0
|
||||||
|
throttle_rate: int | None = None
|
||||||
|
|
||||||
|
# TODO: do we need this really if we can pull the `Portal` from
|
||||||
|
# ``tractor``'s internals?
|
||||||
|
feed: Feed | None = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def rt_shm(self) -> ShmArray:
|
||||||
|
|
||||||
|
if self._rt_shm is None:
|
||||||
|
self._rt_shm = attach_shm_array(
|
||||||
|
token=self._rt_shm_token,
|
||||||
|
readonly=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
return self._rt_shm
|
||||||
|
|
||||||
|
@property
|
||||||
|
def hist_shm(self) -> ShmArray:
|
||||||
|
|
||||||
|
if self._hist_shm_token is None:
|
||||||
|
raise RuntimeError(
|
||||||
|
'No shm token has been set for the history buffer?'
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
self._hist_shm is None
|
||||||
|
):
|
||||||
|
self._hist_shm = attach_shm_array(
|
||||||
|
token=self._hist_shm_token,
|
||||||
|
readonly=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
return self._hist_shm
|
||||||
|
|
||||||
|
async def receive(self) -> dict:
|
||||||
|
return await self.stream.receive()
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def index_stream(
|
||||||
|
self,
|
||||||
|
delay_s: float = 1,
|
||||||
|
|
||||||
|
) -> AsyncIterator[int]:
|
||||||
|
|
||||||
|
if not self.feed:
|
||||||
|
raise RuntimeError('This flume is not part of any ``Feed``?')
|
||||||
|
|
||||||
|
# TODO: maybe a public (property) API for this in ``tractor``?
|
||||||
|
portal = self.stream._ctx._portal
|
||||||
|
assert portal
|
||||||
|
|
||||||
|
# XXX: this should be singleton on a host,
|
||||||
|
# a lone broker-daemon per provider should be
|
||||||
|
# created for all practical purposes
|
||||||
|
async with open_sample_stream(float(delay_s)) as stream:
|
||||||
|
yield stream
|
||||||
|
|
||||||
|
def get_ds_info(
|
||||||
|
self,
|
||||||
|
) -> tuple[float, float, float]:
|
||||||
|
'''
|
||||||
|
Compute the "downsampling" ratio info between the historical shm
|
||||||
|
buffer and the real-time (HFT) one.
|
||||||
|
|
||||||
|
Return a tuple of the fast sample period, historical sample
|
||||||
|
period and ratio between them.
|
||||||
|
|
||||||
|
'''
|
||||||
|
times = self.hist_shm.array['time']
|
||||||
|
end = pendulum.from_timestamp(times[-1])
|
||||||
|
start = pendulum.from_timestamp(times[times != times[-1]][-1])
|
||||||
|
hist_step_size_s = (end - start).seconds
|
||||||
|
|
||||||
|
times = self.rt_shm.array['time']
|
||||||
|
end = pendulum.from_timestamp(times[-1])
|
||||||
|
start = pendulum.from_timestamp(times[times != times[-1]][-1])
|
||||||
|
rt_step_size_s = (end - start).seconds
|
||||||
|
|
||||||
|
ratio = hist_step_size_s / rt_step_size_s
|
||||||
|
return (
|
||||||
|
rt_step_size_s,
|
||||||
|
hist_step_size_s,
|
||||||
|
ratio,
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: get native msgspec decoding for these workinn
|
||||||
|
def to_msg(self) -> dict:
|
||||||
|
msg = self.to_dict()
|
||||||
|
msg['symbol'] = msg['symbol'].to_dict()
|
||||||
|
|
||||||
|
# can't serialize the stream or feed objects, it's expected
|
||||||
|
# you'll have a ref to it since this msg should be rxed on
|
||||||
|
# a stream on whatever far end IPC..
|
||||||
|
msg.pop('stream')
|
||||||
|
msg.pop('feed')
|
||||||
|
return msg
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_msg(cls, msg: dict) -> dict:
|
||||||
|
symbol = Symbol(**msg.pop('symbol'))
|
||||||
|
return cls(
|
||||||
|
symbol=symbol,
|
||||||
|
**msg,
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_index(
|
||||||
|
self,
|
||||||
|
time_s: float,
|
||||||
|
array: np.ndarray,
|
||||||
|
|
||||||
|
) -> int | float:
|
||||||
|
'''
|
||||||
|
Return array shm-buffer index for for epoch time.
|
||||||
|
|
||||||
|
'''
|
||||||
|
times = array['time']
|
||||||
|
first = np.searchsorted(
|
||||||
|
times,
|
||||||
|
time_s,
|
||||||
|
side='left',
|
||||||
|
)
|
||||||
|
imx = times.shape[0] - 1
|
||||||
|
return min(first, imx)
|
|
@ -37,8 +37,8 @@ import time
|
||||||
from math import isnan
|
from math import isnan
|
||||||
|
|
||||||
from bidict import bidict
|
from bidict import bidict
|
||||||
import msgpack
|
from msgspec.msgpack import encode, decode
|
||||||
import pyqtgraph as pg
|
# import pyqtgraph as pg
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tractor
|
import tractor
|
||||||
from trio_websocket import open_websocket_url
|
from trio_websocket import open_websocket_url
|
||||||
|
@ -56,6 +56,7 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
from .feed import maybe_open_feed
|
from .feed import maybe_open_feed
|
||||||
from ..log import get_logger, get_console_log
|
from ..log import get_logger, get_console_log
|
||||||
|
from .._profile import Profiler
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
@ -131,7 +132,10 @@ def start_marketstore(
|
||||||
|
|
||||||
mktsdir = os.path.join(config._config_dir, 'marketstore')
|
mktsdir = os.path.join(config._config_dir, 'marketstore')
|
||||||
|
|
||||||
# create when dne
|
# create dirs when dne
|
||||||
|
if not os.path.isdir(config._config_dir):
|
||||||
|
os.mkdir(config._config_dir)
|
||||||
|
|
||||||
if not os.path.isdir(mktsdir):
|
if not os.path.isdir(mktsdir):
|
||||||
os.mkdir(mktsdir)
|
os.mkdir(mktsdir)
|
||||||
|
|
||||||
|
@ -387,50 +391,54 @@ class Storage:
|
||||||
async def load(
|
async def load(
|
||||||
self,
|
self,
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
|
timeframe: int,
|
||||||
|
|
||||||
) -> tuple[
|
) -> tuple[
|
||||||
dict[int, np.ndarray], # timeframe (in secs) to series
|
np.ndarray, # timeframe sampled array-series
|
||||||
Optional[datetime], # first dt
|
Optional[datetime], # first dt
|
||||||
Optional[datetime], # last dt
|
Optional[datetime], # last dt
|
||||||
]:
|
]:
|
||||||
|
|
||||||
first_tsdb_dt, last_tsdb_dt = None, None
|
first_tsdb_dt, last_tsdb_dt = None, None
|
||||||
tsdb_arrays = await self.read_ohlcv(
|
hist = await self.read_ohlcv(
|
||||||
fqsn,
|
fqsn,
|
||||||
# on first load we don't need to pull the max
|
# on first load we don't need to pull the max
|
||||||
# history per request size worth.
|
# history per request size worth.
|
||||||
limit=3000,
|
limit=3000,
|
||||||
|
timeframe=timeframe,
|
||||||
)
|
)
|
||||||
log.info(f'Loaded tsdb history {tsdb_arrays}')
|
log.info(f'Loaded tsdb history {hist}')
|
||||||
|
|
||||||
if tsdb_arrays:
|
if len(hist):
|
||||||
fastest = list(tsdb_arrays.values())[0]
|
times = hist['Epoch']
|
||||||
times = fastest['Epoch']
|
|
||||||
first, last = times[0], times[-1]
|
first, last = times[0], times[-1]
|
||||||
first_tsdb_dt, last_tsdb_dt = map(
|
first_tsdb_dt, last_tsdb_dt = map(
|
||||||
pendulum.from_timestamp, [first, last]
|
pendulum.from_timestamp, [first, last]
|
||||||
)
|
)
|
||||||
|
|
||||||
return tsdb_arrays, first_tsdb_dt, last_tsdb_dt
|
return (
|
||||||
|
hist, # array-data
|
||||||
|
first_tsdb_dt, # start of query-frame
|
||||||
|
last_tsdb_dt, # most recent
|
||||||
|
)
|
||||||
|
|
||||||
async def read_ohlcv(
|
async def read_ohlcv(
|
||||||
self,
|
self,
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
timeframe: Optional[Union[int, str]] = None,
|
timeframe: int | str,
|
||||||
end: Optional[int] = None,
|
end: Optional[int] = None,
|
||||||
limit: int = int(800e3),
|
limit: int = int(800e3),
|
||||||
|
|
||||||
) -> tuple[
|
) -> np.ndarray:
|
||||||
MarketstoreClient,
|
|
||||||
Union[dict, np.ndarray]
|
|
||||||
]:
|
|
||||||
client = self.client
|
client = self.client
|
||||||
syms = await client.list_symbols()
|
syms = await client.list_symbols()
|
||||||
|
|
||||||
if fqsn not in syms:
|
if fqsn not in syms:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
tfstr = tf_in_1s[1]
|
# use the provided timeframe or 1s by default
|
||||||
|
tfstr = tf_in_1s.get(timeframe, tf_in_1s[1])
|
||||||
|
|
||||||
params = Params(
|
params = Params(
|
||||||
symbols=fqsn,
|
symbols=fqsn,
|
||||||
|
@ -444,58 +452,72 @@ class Storage:
|
||||||
limit=limit,
|
limit=limit,
|
||||||
)
|
)
|
||||||
|
|
||||||
if timeframe is None:
|
|
||||||
log.info(f'starting {fqsn} tsdb granularity scan..')
|
|
||||||
# loop through and try to find highest granularity
|
|
||||||
for tfstr in tf_in_1s.values():
|
|
||||||
try:
|
try:
|
||||||
log.info(f'querying for {tfstr}@{fqsn}')
|
|
||||||
params.set('timeframe', tfstr)
|
|
||||||
result = await client.query(params)
|
result = await client.query(params)
|
||||||
break
|
except purerpc.grpclib.exceptions.UnknownError as err:
|
||||||
|
# indicate there is no history for this timeframe
|
||||||
except purerpc.grpclib.exceptions.UnknownError:
|
log.exception(
|
||||||
# XXX: this is already logged by the container and
|
f'Unknown mkts QUERY error: {params}\n'
|
||||||
# thus shows up through `marketstored` logs relay.
|
f'{err.args}'
|
||||||
# log.warning(f'{tfstr}@{fqsn} not found')
|
)
|
||||||
continue
|
|
||||||
else:
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
else:
|
|
||||||
result = await client.query(params)
|
|
||||||
|
|
||||||
# TODO: it turns out column access on recarrays is actually slower:
|
# TODO: it turns out column access on recarrays is actually slower:
|
||||||
# https://jakevdp.github.io/PythonDataScienceHandbook/02.09-structured-data-numpy.html#RecordArrays:-Structured-Arrays-with-a-Twist
|
# https://jakevdp.github.io/PythonDataScienceHandbook/02.09-structured-data-numpy.html#RecordArrays:-Structured-Arrays-with-a-Twist
|
||||||
# it might make sense to make these structured arrays?
|
# it might make sense to make these structured arrays?
|
||||||
# Fill out a `numpy` array-results map
|
data_set = result.by_symbols()[fqsn]
|
||||||
arrays = {}
|
array = data_set.array
|
||||||
for fqsn, data_set in result.by_symbols().items():
|
|
||||||
arrays.setdefault(fqsn, {})[
|
|
||||||
tf_in_1s.inverse[data_set.timeframe]
|
|
||||||
] = data_set.array
|
|
||||||
|
|
||||||
return arrays[fqsn][timeframe] if timeframe else arrays[fqsn]
|
# XXX: ensure sample rate is as expected
|
||||||
|
time = data_set.array['Epoch']
|
||||||
|
if len(time) > 1:
|
||||||
|
time_step = time[-1] - time[-2]
|
||||||
|
ts = tf_in_1s.inverse[data_set.timeframe]
|
||||||
|
|
||||||
|
if time_step != ts:
|
||||||
|
log.warning(
|
||||||
|
f'MKTS BUG: wrong timeframe loaded: {time_step}'
|
||||||
|
'YOUR DATABASE LIKELY CONTAINS BAD DATA FROM AN OLD BUG'
|
||||||
|
f'WIPING HISTORY FOR {ts}s'
|
||||||
|
)
|
||||||
|
await self.delete_ts(fqsn, timeframe)
|
||||||
|
|
||||||
|
# try reading again..
|
||||||
|
return await self.read_ohlcv(
|
||||||
|
fqsn,
|
||||||
|
timeframe,
|
||||||
|
end,
|
||||||
|
limit,
|
||||||
|
)
|
||||||
|
|
||||||
|
return array
|
||||||
|
|
||||||
async def delete_ts(
|
async def delete_ts(
|
||||||
self,
|
self,
|
||||||
key: str,
|
key: str,
|
||||||
timeframe: Optional[Union[int, str]] = None,
|
timeframe: Optional[Union[int, str]] = None,
|
||||||
|
fmt: str = 'OHLCV',
|
||||||
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|
||||||
client = self.client
|
client = self.client
|
||||||
syms = await client.list_symbols()
|
syms = await client.list_symbols()
|
||||||
print(syms)
|
print(syms)
|
||||||
# if key not in syms:
|
if key not in syms:
|
||||||
# raise KeyError(f'`{fqsn}` table key not found?')
|
raise KeyError(f'`{key}` table key not found in\n{syms}?')
|
||||||
|
|
||||||
return await client.destroy(tbk=key)
|
tbk = mk_tbk((
|
||||||
|
key,
|
||||||
|
tf_in_1s.get(timeframe, tf_in_1s[60]),
|
||||||
|
fmt,
|
||||||
|
))
|
||||||
|
return await client.destroy(tbk=tbk)
|
||||||
|
|
||||||
async def write_ohlcv(
|
async def write_ohlcv(
|
||||||
self,
|
self,
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
ohlcv: np.ndarray,
|
ohlcv: np.ndarray,
|
||||||
|
timeframe: int,
|
||||||
append_and_duplicate: bool = True,
|
append_and_duplicate: bool = True,
|
||||||
limit: int = int(800e3),
|
limit: int = int(800e3),
|
||||||
|
|
||||||
|
@ -519,17 +541,18 @@ class Storage:
|
||||||
|
|
||||||
m, r = divmod(len(mkts_array), limit)
|
m, r = divmod(len(mkts_array), limit)
|
||||||
|
|
||||||
|
tfkey = tf_in_1s[timeframe]
|
||||||
for i in range(m, 1):
|
for i in range(m, 1):
|
||||||
to_push = mkts_array[i-1:i*limit]
|
to_push = mkts_array[i-1:i*limit]
|
||||||
|
|
||||||
# write to db
|
# write to db
|
||||||
resp = await self.client.write(
|
resp = await self.client.write(
|
||||||
to_push,
|
to_push,
|
||||||
tbk=f'{fqsn}/1Sec/OHLCV',
|
tbk=f'{fqsn}/{tfkey}/OHLCV',
|
||||||
|
|
||||||
# NOTE: will will append duplicates
|
# NOTE: will will append duplicates
|
||||||
# for the same timestamp-index.
|
# for the same timestamp-index.
|
||||||
# TODO: pre deduplicate?
|
# TODO: pre-deduplicate?
|
||||||
isvariablelength=append_and_duplicate,
|
isvariablelength=append_and_duplicate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -548,7 +571,7 @@ class Storage:
|
||||||
# write to db
|
# write to db
|
||||||
resp = await self.client.write(
|
resp = await self.client.write(
|
||||||
to_push,
|
to_push,
|
||||||
tbk=f'{fqsn}/1Sec/OHLCV',
|
tbk=f'{fqsn}/{tfkey}/OHLCV',
|
||||||
|
|
||||||
# NOTE: will will append duplicates
|
# NOTE: will will append duplicates
|
||||||
# for the same timestamp-index.
|
# for the same timestamp-index.
|
||||||
|
@ -577,6 +600,7 @@ class Storage:
|
||||||
# def delete_range(self, start_dt, end_dt) -> None:
|
# def delete_range(self, start_dt, end_dt) -> None:
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def open_storage_client(
|
async def open_storage_client(
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
|
@ -626,7 +650,7 @@ async def tsdb_history_update(
|
||||||
# * the original data feed arch blurb:
|
# * the original data feed arch blurb:
|
||||||
# - https://github.com/pikers/piker/issues/98
|
# - https://github.com/pikers/piker/issues/98
|
||||||
#
|
#
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
disabled=False, # not pg_profile_enabled(),
|
disabled=False, # not pg_profile_enabled(),
|
||||||
delayed=False,
|
delayed=False,
|
||||||
)
|
)
|
||||||
|
@ -638,34 +662,35 @@ async def tsdb_history_update(
|
||||||
[fqsn],
|
[fqsn],
|
||||||
start_stream=False,
|
start_stream=False,
|
||||||
|
|
||||||
) as (feed, stream),
|
) as feed,
|
||||||
):
|
):
|
||||||
profiler(f'opened feed for {fqsn}')
|
profiler(f'opened feed for {fqsn}')
|
||||||
|
|
||||||
to_append = feed.shm.array
|
# to_append = feed.hist_shm.array
|
||||||
to_prepend = None
|
# to_prepend = None
|
||||||
|
|
||||||
if fqsn:
|
if fqsn:
|
||||||
symbol = feed.symbols.get(fqsn)
|
flume = feed.flumes[fqsn]
|
||||||
|
symbol = flume.symbol
|
||||||
if symbol:
|
if symbol:
|
||||||
fqsn = symbol.front_fqsn()
|
fqsn = symbol.fqsn
|
||||||
|
|
||||||
# diff db history with shm and only write the missing portions
|
# diff db history with shm and only write the missing portions
|
||||||
ohlcv = feed.shm.array
|
# ohlcv = flume.hist_shm.array
|
||||||
|
|
||||||
# TODO: use pg profiler
|
# TODO: use pg profiler
|
||||||
tsdb_arrays = await storage.read_ohlcv(fqsn)
|
# for secs in (1, 60):
|
||||||
# hist diffing
|
# tsdb_array = await storage.read_ohlcv(
|
||||||
if tsdb_arrays:
|
# fqsn,
|
||||||
for secs in (1, 60):
|
# timeframe=timeframe,
|
||||||
ts = tsdb_arrays.get(secs)
|
# )
|
||||||
if ts is not None and len(ts):
|
# # hist diffing:
|
||||||
# these aren't currently used but can be referenced from
|
# # these aren't currently used but can be referenced from
|
||||||
# within the embedded ipython shell below.
|
# # within the embedded ipython shell below.
|
||||||
to_append = ohlcv[ohlcv['time'] > ts['Epoch'][-1]]
|
# to_append = ohlcv[ohlcv['time'] > ts['Epoch'][-1]]
|
||||||
to_prepend = ohlcv[ohlcv['time'] < ts['Epoch'][0]]
|
# to_prepend = ohlcv[ohlcv['time'] < ts['Epoch'][0]]
|
||||||
|
|
||||||
profiler('Finished db arrays diffs')
|
# profiler('Finished db arrays diffs')
|
||||||
|
|
||||||
syms = await storage.client.list_symbols()
|
syms = await storage.client.list_symbols()
|
||||||
log.info(f'Existing tsdb symbol set:\n{pformat(syms)}')
|
log.info(f'Existing tsdb symbol set:\n{pformat(syms)}')
|
||||||
|
@ -774,12 +799,13 @@ async def stream_quotes(
|
||||||
async with open_websocket_url(f'ws://{host}:{port}/ws') as ws:
|
async with open_websocket_url(f'ws://{host}:{port}/ws') as ws:
|
||||||
# send subs topics to server
|
# send subs topics to server
|
||||||
resp = await ws.send_message(
|
resp = await ws.send_message(
|
||||||
msgpack.dumps({'streams': list(tbks.values())})
|
|
||||||
|
encode({'streams': list(tbks.values())})
|
||||||
)
|
)
|
||||||
log.info(resp)
|
log.info(resp)
|
||||||
|
|
||||||
async def recv() -> dict[str, Any]:
|
async def recv() -> dict[str, Any]:
|
||||||
return msgpack.loads((await ws.get_message()), encoding='utf-8')
|
return decode((await ws.get_message()), encoding='utf-8')
|
||||||
|
|
||||||
streams = (await recv())['streams']
|
streams = (await recv())['streams']
|
||||||
log.info(f"Subscribed to {streams}")
|
log.info(f"Subscribed to {streams}")
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Guillermo Rodriguez (in stewardship for piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Built-in (extension) types.
|
||||||
|
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
from typing import Optional
|
||||||
|
from pprint import pformat
|
||||||
|
|
||||||
|
import msgspec
|
||||||
|
|
||||||
|
|
||||||
|
class Struct(
|
||||||
|
msgspec.Struct,
|
||||||
|
|
||||||
|
# https://jcristharif.com/msgspec/structs.html#tagged-unions
|
||||||
|
# tag='pikerstruct',
|
||||||
|
# tag=True,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
A "human friendlier" (aka repl buddy) struct subtype.
|
||||||
|
|
||||||
|
'''
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
f: getattr(self, f)
|
||||||
|
for f in self.__struct_fields__
|
||||||
|
}
|
||||||
|
|
||||||
|
# Lul, doesn't seem to work that well..
|
||||||
|
# def __repr__(self):
|
||||||
|
# # only turn on pprint when we detect a python REPL
|
||||||
|
# # at runtime B)
|
||||||
|
# if (
|
||||||
|
# hasattr(sys, 'ps1')
|
||||||
|
# # TODO: check if we're in pdb
|
||||||
|
# ):
|
||||||
|
# return self.pformat()
|
||||||
|
|
||||||
|
# return super().__repr__()
|
||||||
|
|
||||||
|
def pformat(self) -> str:
|
||||||
|
return f'Struct({pformat(self.to_dict())})'
|
||||||
|
|
||||||
|
def copy(
|
||||||
|
self,
|
||||||
|
update: Optional[dict] = None,
|
||||||
|
|
||||||
|
) -> msgspec.Struct:
|
||||||
|
'''
|
||||||
|
Validate-typecast all self defined fields, return a copy of us
|
||||||
|
with all such fields.
|
||||||
|
|
||||||
|
This is kinda like the default behaviour in `pydantic.BaseModel`.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if update:
|
||||||
|
for k, v in update.items():
|
||||||
|
setattr(self, k, v)
|
||||||
|
|
||||||
|
# roundtrip serialize to validate
|
||||||
|
return msgspec.msgpack.Decoder(
|
||||||
|
type=type(self)
|
||||||
|
).decode(
|
||||||
|
msgspec.msgpack.Encoder().encode(self)
|
||||||
|
)
|
||||||
|
|
||||||
|
def typecast(
|
||||||
|
self,
|
||||||
|
# fields: Optional[list[str]] = None,
|
||||||
|
) -> None:
|
||||||
|
for fname, ftype in self.__annotations__.items():
|
||||||
|
setattr(self, fname, ftype(getattr(self, fname)))
|
|
@ -78,7 +78,8 @@ class Fsp:
|
||||||
# + the consuming fsp *to* the consumers output
|
# + the consuming fsp *to* the consumers output
|
||||||
# shm flow.
|
# shm flow.
|
||||||
_flow_registry: dict[
|
_flow_registry: dict[
|
||||||
tuple[_Token, str], _Token,
|
tuple[_Token, str],
|
||||||
|
tuple[_Token, Optional[ShmArray]],
|
||||||
] = {}
|
] = {}
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -120,7 +121,6 @@ class Fsp:
|
||||||
):
|
):
|
||||||
return self.func(*args, **kwargs)
|
return self.func(*args, **kwargs)
|
||||||
|
|
||||||
# TODO: lru_cache this? prettty sure it'll work?
|
|
||||||
def get_shm(
|
def get_shm(
|
||||||
self,
|
self,
|
||||||
src_shm: ShmArray,
|
src_shm: ShmArray,
|
||||||
|
@ -131,12 +131,27 @@ class Fsp:
|
||||||
for this "instance" of a signal processor for
|
for this "instance" of a signal processor for
|
||||||
the given ``key``.
|
the given ``key``.
|
||||||
|
|
||||||
|
The destination shm "token" and array are cached if possible to
|
||||||
|
minimize multiple stdlib/system calls.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
dst_token = self._flow_registry[
|
dst_token, maybe_array = self._flow_registry[
|
||||||
(src_shm._token, self.name)
|
(src_shm._token, self.name)
|
||||||
]
|
]
|
||||||
shm = attach_shm_array(dst_token)
|
if maybe_array is None:
|
||||||
return shm
|
self._flow_registry[
|
||||||
|
(src_shm._token, self.name)
|
||||||
|
] = (
|
||||||
|
dst_token,
|
||||||
|
# "cache" the ``ShmArray`` such that
|
||||||
|
# we call the underlying "attach" code as few
|
||||||
|
# times as possible as per:
|
||||||
|
# - https://github.com/pikers/piker/issues/359
|
||||||
|
# - https://github.com/pikers/piker/issues/332
|
||||||
|
maybe_array := attach_shm_array(dst_token)
|
||||||
|
)
|
||||||
|
|
||||||
|
return maybe_array
|
||||||
|
|
||||||
|
|
||||||
def fsp(
|
def fsp(
|
||||||
|
@ -184,7 +199,10 @@ def maybe_mk_fsp_shm(
|
||||||
# TODO: load output types from `Fsp`
|
# TODO: load output types from `Fsp`
|
||||||
# - should `index` be a required internal field?
|
# - should `index` be a required internal field?
|
||||||
fsp_dtype = np.dtype(
|
fsp_dtype = np.dtype(
|
||||||
[('index', int)] +
|
[('index', int)]
|
||||||
|
+
|
||||||
|
[('time', float)]
|
||||||
|
+
|
||||||
[(field_name, float) for field_name in target.outputs]
|
[(field_name, float) for field_name in target.outputs]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,13 @@ core task logic for processing chains
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import (
|
from typing import (
|
||||||
AsyncIterator, Callable, Optional,
|
AsyncIterator,
|
||||||
|
Callable,
|
||||||
|
Optional,
|
||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pyqtgraph as pg
|
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
import tractor
|
import tractor
|
||||||
|
@ -35,14 +36,22 @@ from tractor.msg import NamespacePath
|
||||||
from ..log import get_logger, get_console_log
|
from ..log import get_logger, get_console_log
|
||||||
from .. import data
|
from .. import data
|
||||||
from ..data import attach_shm_array
|
from ..data import attach_shm_array
|
||||||
from ..data.feed import Feed
|
from ..data.feed import (
|
||||||
|
Flume,
|
||||||
|
Feed,
|
||||||
|
)
|
||||||
from ..data._sharedmem import ShmArray
|
from ..data._sharedmem import ShmArray
|
||||||
|
from ..data._sampling import (
|
||||||
|
_default_delay_s,
|
||||||
|
open_sample_stream,
|
||||||
|
)
|
||||||
from ..data._source import Symbol
|
from ..data._source import Symbol
|
||||||
from ._api import (
|
from ._api import (
|
||||||
Fsp,
|
Fsp,
|
||||||
_load_builtins,
|
_load_builtins,
|
||||||
_Token,
|
_Token,
|
||||||
)
|
)
|
||||||
|
from .._profile import Profiler
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
@ -77,7 +86,7 @@ async def filter_quotes_by_sym(
|
||||||
async def fsp_compute(
|
async def fsp_compute(
|
||||||
|
|
||||||
symbol: Symbol,
|
symbol: Symbol,
|
||||||
feed: Feed,
|
flume: Flume,
|
||||||
quote_stream: trio.abc.ReceiveChannel,
|
quote_stream: trio.abc.ReceiveChannel,
|
||||||
|
|
||||||
src: ShmArray,
|
src: ShmArray,
|
||||||
|
@ -90,7 +99,7 @@ async def fsp_compute(
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
delayed=False,
|
delayed=False,
|
||||||
disabled=True
|
disabled=True
|
||||||
)
|
)
|
||||||
|
@ -105,16 +114,17 @@ async def fsp_compute(
|
||||||
filter_quotes_by_sym(fqsn, quote_stream),
|
filter_quotes_by_sym(fqsn, quote_stream),
|
||||||
|
|
||||||
# XXX: currently the ``ohlcv`` arg
|
# XXX: currently the ``ohlcv`` arg
|
||||||
feed.shm,
|
flume.rt_shm,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Conduct a single iteration of fsp with historical bars input
|
# HISTORY COMPUTE PHASE
|
||||||
# and get historical output
|
# conduct a single iteration of fsp with historical bars input
|
||||||
|
# and get historical output.
|
||||||
history_output: Union[
|
history_output: Union[
|
||||||
dict[str, np.ndarray], # multi-output case
|
dict[str, np.ndarray], # multi-output case
|
||||||
np.ndarray, # single output case
|
np.ndarray, # single output case
|
||||||
]
|
]
|
||||||
history_output = await out_stream.__anext__()
|
history_output = await anext(out_stream)
|
||||||
|
|
||||||
func_name = func.__name__
|
func_name = func.__name__
|
||||||
profiler(f'{func_name} generated history')
|
profiler(f'{func_name} generated history')
|
||||||
|
@ -126,9 +136,13 @@ async def fsp_compute(
|
||||||
# each respective field.
|
# each respective field.
|
||||||
fields = getattr(dst.array.dtype, 'fields', None).copy()
|
fields = getattr(dst.array.dtype, 'fields', None).copy()
|
||||||
fields.pop('index')
|
fields.pop('index')
|
||||||
history: Optional[np.ndarray] = None # TODO: nptyping here!
|
history_by_field: Optional[np.ndarray] = None
|
||||||
|
src_time = src.array['time']
|
||||||
|
|
||||||
if fields and len(fields) > 1 and fields:
|
if (
|
||||||
|
fields and
|
||||||
|
len(fields) > 1
|
||||||
|
):
|
||||||
if not isinstance(history_output, dict):
|
if not isinstance(history_output, dict):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f'`{func_name}` is a multi-output FSP and should yield a '
|
f'`{func_name}` is a multi-output FSP and should yield a '
|
||||||
|
@ -139,7 +153,7 @@ async def fsp_compute(
|
||||||
if key in history_output:
|
if key in history_output:
|
||||||
output = history_output[key]
|
output = history_output[key]
|
||||||
|
|
||||||
if history is None:
|
if history_by_field is None:
|
||||||
|
|
||||||
if output is None:
|
if output is None:
|
||||||
length = len(src.array)
|
length = len(src.array)
|
||||||
|
@ -149,7 +163,7 @@ async def fsp_compute(
|
||||||
# using the first output, determine
|
# using the first output, determine
|
||||||
# the length of the struct-array that
|
# the length of the struct-array that
|
||||||
# will be pushed to shm.
|
# will be pushed to shm.
|
||||||
history = np.zeros(
|
history_by_field = np.zeros(
|
||||||
length,
|
length,
|
||||||
dtype=dst.array.dtype
|
dtype=dst.array.dtype
|
||||||
)
|
)
|
||||||
|
@ -157,7 +171,7 @@ async def fsp_compute(
|
||||||
if output is None:
|
if output is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
history[key] = output
|
history_by_field[key] = output
|
||||||
|
|
||||||
# single-key output stream
|
# single-key output stream
|
||||||
else:
|
else:
|
||||||
|
@ -166,11 +180,15 @@ async def fsp_compute(
|
||||||
f'`{func_name}` is a single output FSP and should yield an '
|
f'`{func_name}` is a single output FSP and should yield an '
|
||||||
'`np.ndarray` for history'
|
'`np.ndarray` for history'
|
||||||
)
|
)
|
||||||
history = np.zeros(
|
history_by_field = np.zeros(
|
||||||
len(history_output),
|
len(history_output),
|
||||||
dtype=dst.array.dtype
|
dtype=dst.array.dtype
|
||||||
)
|
)
|
||||||
history[func_name] = history_output
|
history_by_field[func_name] = history_output
|
||||||
|
|
||||||
|
history_by_field['time'] = src_time[-len(history_by_field):]
|
||||||
|
|
||||||
|
history_output['time'] = src.array['time']
|
||||||
|
|
||||||
# TODO: XXX:
|
# TODO: XXX:
|
||||||
# THERE'S A BIG BUG HERE WITH THE `index` field since we're
|
# THERE'S A BIG BUG HERE WITH THE `index` field since we're
|
||||||
|
@ -187,7 +205,10 @@ async def fsp_compute(
|
||||||
|
|
||||||
# TODO: can we use this `start` flag instead of the manual
|
# TODO: can we use this `start` flag instead of the manual
|
||||||
# setting above?
|
# setting above?
|
||||||
index = dst.push(history, start=first)
|
index = dst.push(
|
||||||
|
history_by_field,
|
||||||
|
start=first,
|
||||||
|
)
|
||||||
|
|
||||||
profiler(f'{func_name} pushed history')
|
profiler(f'{func_name} pushed history')
|
||||||
profiler.finish()
|
profiler.finish()
|
||||||
|
@ -213,8 +234,14 @@ async def fsp_compute(
|
||||||
|
|
||||||
log.debug(f"{func_name}: {processed}")
|
log.debug(f"{func_name}: {processed}")
|
||||||
key, output = processed
|
key, output = processed
|
||||||
index = src.index
|
# dst.array[-1][key] = output
|
||||||
dst.array[-1][key] = output
|
dst.array[[key, 'time']][-1] = (
|
||||||
|
output,
|
||||||
|
# TODO: what about pushing ``time.time_ns()``
|
||||||
|
# in which case we'll need to round at the graphics
|
||||||
|
# processing / sampling layer?
|
||||||
|
src.array[-1]['time']
|
||||||
|
)
|
||||||
|
|
||||||
# NOTE: for now we aren't streaming this to the consumer
|
# NOTE: for now we aren't streaming this to the consumer
|
||||||
# stream latest array index entry which basically just acts
|
# stream latest array index entry which basically just acts
|
||||||
|
@ -225,6 +252,7 @@ async def fsp_compute(
|
||||||
# N-consumers who subscribe for the real-time output,
|
# N-consumers who subscribe for the real-time output,
|
||||||
# which we'll likely want to implement using local-mem
|
# which we'll likely want to implement using local-mem
|
||||||
# chans for the fan out?
|
# chans for the fan out?
|
||||||
|
# index = src.index
|
||||||
# if attach_stream:
|
# if attach_stream:
|
||||||
# await client_stream.send(index)
|
# await client_stream.send(index)
|
||||||
|
|
||||||
|
@ -261,7 +289,7 @@ async def cascade(
|
||||||
destination shm array buffer.
|
destination shm array buffer.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
delayed=False,
|
delayed=False,
|
||||||
disabled=False
|
disabled=False
|
||||||
)
|
)
|
||||||
|
@ -284,9 +312,10 @@ async def cascade(
|
||||||
# TODO: ugh i hate this wind/unwind to list over the wire
|
# TODO: ugh i hate this wind/unwind to list over the wire
|
||||||
# but not sure how else to do it.
|
# but not sure how else to do it.
|
||||||
for (token, fsp_name, dst_token) in shm_registry:
|
for (token, fsp_name, dst_token) in shm_registry:
|
||||||
Fsp._flow_registry[
|
Fsp._flow_registry[(
|
||||||
(_Token.from_msg(token), fsp_name)
|
_Token.from_msg(token),
|
||||||
] = _Token.from_msg(dst_token)
|
fsp_name,
|
||||||
|
)] = _Token.from_msg(dst_token), None
|
||||||
|
|
||||||
fsp: Fsp = reg.get(
|
fsp: Fsp = reg.get(
|
||||||
NamespacePath(ns_path)
|
NamespacePath(ns_path)
|
||||||
|
@ -298,6 +327,7 @@ async def cascade(
|
||||||
raise ValueError(f'Unknown fsp target: {ns_path}')
|
raise ValueError(f'Unknown fsp target: {ns_path}')
|
||||||
|
|
||||||
# open a data feed stream with requested broker
|
# open a data feed stream with requested broker
|
||||||
|
feed: Feed
|
||||||
async with data.feed.maybe_open_feed(
|
async with data.feed.maybe_open_feed(
|
||||||
[fqsn],
|
[fqsn],
|
||||||
|
|
||||||
|
@ -307,14 +337,13 @@ async def cascade(
|
||||||
# needs to get throttled the ticks we generate.
|
# needs to get throttled the ticks we generate.
|
||||||
# tick_throttle=60,
|
# tick_throttle=60,
|
||||||
|
|
||||||
) as (feed, quote_stream):
|
) as feed:
|
||||||
symbol = feed.symbols[fqsn]
|
|
||||||
|
|
||||||
|
flume = feed.flumes[fqsn]
|
||||||
|
symbol = flume.symbol
|
||||||
|
assert src.token == flume.rt_shm.token
|
||||||
profiler(f'{func}: feed up')
|
profiler(f'{func}: feed up')
|
||||||
|
|
||||||
assert src.token == feed.shm.token
|
|
||||||
# last_len = new_len = len(src.array)
|
|
||||||
|
|
||||||
func_name = func.__name__
|
func_name = func.__name__
|
||||||
async with (
|
async with (
|
||||||
trio.open_nursery() as n,
|
trio.open_nursery() as n,
|
||||||
|
@ -324,8 +353,8 @@ async def cascade(
|
||||||
|
|
||||||
fsp_compute,
|
fsp_compute,
|
||||||
symbol=symbol,
|
symbol=symbol,
|
||||||
feed=feed,
|
flume=flume,
|
||||||
quote_stream=quote_stream,
|
quote_stream=flume.stream,
|
||||||
|
|
||||||
# shm
|
# shm
|
||||||
src=src,
|
src=src,
|
||||||
|
@ -361,7 +390,7 @@ async def cascade(
|
||||||
) -> tuple[TaskTracker, int]:
|
) -> tuple[TaskTracker, int]:
|
||||||
# TODO: adopt an incremental update engine/approach
|
# TODO: adopt an incremental update engine/approach
|
||||||
# where possible here eventually!
|
# where possible here eventually!
|
||||||
log.debug(f're-syncing fsp {func_name} to source')
|
log.info(f're-syncing fsp {func_name} to source')
|
||||||
tracker.cs.cancel()
|
tracker.cs.cancel()
|
||||||
await tracker.complete.wait()
|
await tracker.complete.wait()
|
||||||
tracker, index = await n.start(fsp_target)
|
tracker, index = await n.start(fsp_target)
|
||||||
|
@ -374,14 +403,16 @@ async def cascade(
|
||||||
'key': dst_shm_token,
|
'key': dst_shm_token,
|
||||||
'first': dst._first.value,
|
'first': dst._first.value,
|
||||||
'last': dst._last.value,
|
'last': dst._last.value,
|
||||||
}})
|
}
|
||||||
|
})
|
||||||
return tracker, index
|
return tracker, index
|
||||||
|
|
||||||
def is_synced(
|
def is_synced(
|
||||||
src: ShmArray,
|
src: ShmArray,
|
||||||
dst: ShmArray
|
dst: ShmArray
|
||||||
) -> tuple[bool, int, int]:
|
) -> tuple[bool, int, int]:
|
||||||
'''Predicate to dertmine if a destination FSP
|
'''
|
||||||
|
Predicate to dertmine if a destination FSP
|
||||||
output array is aligned to its source array.
|
output array is aligned to its source array.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -390,16 +421,15 @@ async def cascade(
|
||||||
return not (
|
return not (
|
||||||
# the source is likely backfilling and we must
|
# the source is likely backfilling and we must
|
||||||
# sync history calculations
|
# sync history calculations
|
||||||
len_diff > 2 or
|
len_diff > 2
|
||||||
|
|
||||||
# we aren't step synced to the source and may be
|
# we aren't step synced to the source and may be
|
||||||
# leading/lagging by a step
|
# leading/lagging by a step
|
||||||
step_diff > 1 or
|
or step_diff > 1
|
||||||
step_diff < 0
|
or step_diff < 0
|
||||||
), step_diff, len_diff
|
), step_diff, len_diff
|
||||||
|
|
||||||
async def poll_and_sync_to_step(
|
async def poll_and_sync_to_step(
|
||||||
|
|
||||||
tracker: TaskTracker,
|
tracker: TaskTracker,
|
||||||
src: ShmArray,
|
src: ShmArray,
|
||||||
dst: ShmArray,
|
dst: ShmArray,
|
||||||
|
@ -418,18 +448,23 @@ async def cascade(
|
||||||
# detect sample period step for subscription to increment
|
# detect sample period step for subscription to increment
|
||||||
# signal
|
# signal
|
||||||
times = src.array['time']
|
times = src.array['time']
|
||||||
delay_s = times[-1] - times[times != times[-1]][-1]
|
if len(times) > 1:
|
||||||
|
last_ts = times[-1]
|
||||||
|
delay_s = float(last_ts - times[times != last_ts][-1])
|
||||||
|
else:
|
||||||
|
# our default "HFT" sample rate.
|
||||||
|
delay_s = _default_delay_s
|
||||||
|
|
||||||
# Increment the underlying shared memory buffer on every
|
# sub and increment the underlying shared memory buffer
|
||||||
# "increment" msg received from the underlying data feed.
|
# on every step msg received from the global `samplerd`
|
||||||
async with feed.index_stream(
|
# service.
|
||||||
int(delay_s)
|
async with open_sample_stream(float(delay_s)) as istream:
|
||||||
) as istream:
|
|
||||||
|
|
||||||
profiler(f'{func_name}: sample stream up')
|
profiler(f'{func_name}: sample stream up')
|
||||||
profiler.finish()
|
profiler.finish()
|
||||||
|
|
||||||
async for _ in istream:
|
async for i in istream:
|
||||||
|
# print(f'FSP incrementing {i}')
|
||||||
|
|
||||||
# respawn the compute task if the source
|
# respawn the compute task if the source
|
||||||
# array has been updated such that we compute
|
# array has been updated such that we compute
|
||||||
|
@ -458,3 +493,23 @@ async def cascade(
|
||||||
last = array[-1:].copy()
|
last = array[-1:].copy()
|
||||||
|
|
||||||
dst.push(last)
|
dst.push(last)
|
||||||
|
|
||||||
|
# sync with source buffer's time step
|
||||||
|
src_l2 = src.array[-2:]
|
||||||
|
src_li, src_lt = src_l2[-1][['index', 'time']]
|
||||||
|
src_2li, src_2lt = src_l2[-2][['index', 'time']]
|
||||||
|
dst._array['time'][src_li] = src_lt
|
||||||
|
dst._array['time'][src_2li] = src_2lt
|
||||||
|
|
||||||
|
# last2 = dst.array[-2:]
|
||||||
|
# if (
|
||||||
|
# last2[-1]['index'] != src_li
|
||||||
|
# or last2[-2]['index'] != src_2li
|
||||||
|
# ):
|
||||||
|
# dstl2 = list(last2)
|
||||||
|
# srcl2 = list(src_l2)
|
||||||
|
# print(
|
||||||
|
# # f'{dst.token}\n'
|
||||||
|
# f'src: {srcl2}\n'
|
||||||
|
# f'dst: {dstl2}\n'
|
||||||
|
# )
|
||||||
|
|
|
@ -234,7 +234,7 @@ async def flow_rates(
|
||||||
# FSPs, user input, and possibly any general event stream in
|
# FSPs, user input, and possibly any general event stream in
|
||||||
# real-time. Hint: ideally implemented with caching until mutated
|
# real-time. Hint: ideally implemented with caching until mutated
|
||||||
# ;)
|
# ;)
|
||||||
period: 'Param[int]' = 6, # noqa
|
period: 'Param[int]' = 1, # noqa
|
||||||
|
|
||||||
# TODO: support other means by providing a map
|
# TODO: support other means by providing a map
|
||||||
# to weights `partial()`-ed with `wma()`?
|
# to weights `partial()`-ed with `wma()`?
|
||||||
|
@ -268,8 +268,7 @@ async def flow_rates(
|
||||||
'dark_dvlm_rate': None,
|
'dark_dvlm_rate': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: 3.10 do ``anext()``
|
quote = await anext(source)
|
||||||
quote = await source.__anext__()
|
|
||||||
|
|
||||||
# ltr = 0
|
# ltr = 0
|
||||||
# lvr = 0
|
# lvr = 0
|
||||||
|
|
|
@ -0,0 +1,998 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
'''
|
||||||
|
Personal/Private position parsing, calculating, summarizing in a way
|
||||||
|
that doesn't try to cuk most humans who prefer to not lose their moneys..
|
||||||
|
(looking at you `ib` and dirt-bird friends)
|
||||||
|
|
||||||
|
'''
|
||||||
|
from contextlib import contextmanager as cm
|
||||||
|
from pprint import pformat
|
||||||
|
import os
|
||||||
|
from os import path
|
||||||
|
from math import copysign
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Iterator,
|
||||||
|
Optional,
|
||||||
|
Union,
|
||||||
|
)
|
||||||
|
|
||||||
|
import pendulum
|
||||||
|
from pendulum import datetime, now
|
||||||
|
import tomli
|
||||||
|
import toml
|
||||||
|
|
||||||
|
from . import config
|
||||||
|
from .brokers import get_brokermod
|
||||||
|
from .clearing._messages import BrokerdPosition, Status
|
||||||
|
from .data._source import Symbol
|
||||||
|
from .log import get_logger
|
||||||
|
from .data.types import Struct
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@cm
|
||||||
|
def open_trade_ledger(
|
||||||
|
broker: str,
|
||||||
|
account: str,
|
||||||
|
|
||||||
|
) -> str:
|
||||||
|
'''
|
||||||
|
Indempotently create and read in a trade log file from the
|
||||||
|
``<configuration_dir>/ledgers/`` directory.
|
||||||
|
|
||||||
|
Files are named per broker account of the form
|
||||||
|
``<brokername>_<accountname>.toml``. The ``accountname`` here is the
|
||||||
|
name as defined in the user's ``brokers.toml`` config.
|
||||||
|
|
||||||
|
'''
|
||||||
|
ldir = path.join(config._config_dir, 'ledgers')
|
||||||
|
if not path.isdir(ldir):
|
||||||
|
os.makedirs(ldir)
|
||||||
|
|
||||||
|
fname = f'trades_{broker}_{account}.toml'
|
||||||
|
tradesfile = path.join(ldir, fname)
|
||||||
|
|
||||||
|
if not path.isfile(tradesfile):
|
||||||
|
log.info(
|
||||||
|
f'Creating new local trades ledger: {tradesfile}'
|
||||||
|
)
|
||||||
|
with open(tradesfile, 'w') as cf:
|
||||||
|
pass # touch
|
||||||
|
with open(tradesfile, 'rb') as cf:
|
||||||
|
start = time.time()
|
||||||
|
ledger = tomli.load(cf)
|
||||||
|
print(f'Ledger load took {time.time() - start}s')
|
||||||
|
cpy = ledger.copy()
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield cpy
|
||||||
|
finally:
|
||||||
|
if cpy != ledger:
|
||||||
|
# TODO: show diff output?
|
||||||
|
# https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries
|
||||||
|
print(f'Updating ledger for {tradesfile}:\n')
|
||||||
|
ledger.update(cpy)
|
||||||
|
|
||||||
|
# we write on close the mutated ledger data
|
||||||
|
with open(tradesfile, 'w') as cf:
|
||||||
|
toml.dump(ledger, cf)
|
||||||
|
|
||||||
|
|
||||||
|
class Transaction(Struct, frozen=True):
|
||||||
|
# TODO: should this be ``.to`` (see below)?
|
||||||
|
fqsn: str
|
||||||
|
|
||||||
|
tid: Union[str, int] # unique transaction id
|
||||||
|
size: float
|
||||||
|
price: float
|
||||||
|
cost: float # commisions or other additional costs
|
||||||
|
dt: datetime
|
||||||
|
expiry: Optional[datetime] = None
|
||||||
|
|
||||||
|
# optional key normally derived from the broker
|
||||||
|
# backend which ensures the instrument-symbol this record
|
||||||
|
# is for is truly unique.
|
||||||
|
bsuid: Optional[Union[str, int]] = None
|
||||||
|
|
||||||
|
# optional fqsn for the source "asset"/money symbol?
|
||||||
|
# from: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
def iter_by_dt(
|
||||||
|
clears: dict[str, Any],
|
||||||
|
) -> Iterator[tuple[str, dict]]:
|
||||||
|
'''
|
||||||
|
Iterate entries of a ``clears: dict`` table sorted by entry recorded
|
||||||
|
datetime presumably set at the ``'dt'`` field in each entry.
|
||||||
|
|
||||||
|
'''
|
||||||
|
for tid, data in sorted(
|
||||||
|
list(clears.items()),
|
||||||
|
key=lambda item: item[1]['dt'],
|
||||||
|
):
|
||||||
|
yield tid, data
|
||||||
|
|
||||||
|
|
||||||
|
class Position(Struct):
|
||||||
|
'''
|
||||||
|
Basic pp (personal/piker position) model with attached clearing
|
||||||
|
transaction history.
|
||||||
|
|
||||||
|
'''
|
||||||
|
symbol: Symbol
|
||||||
|
|
||||||
|
# can be +ve or -ve for long/short
|
||||||
|
size: float
|
||||||
|
|
||||||
|
# "breakeven price" above or below which pnl moves above and below
|
||||||
|
# zero for the entirety of the current "trade state".
|
||||||
|
ppu: float
|
||||||
|
|
||||||
|
# unique backend symbol id
|
||||||
|
bsuid: str
|
||||||
|
|
||||||
|
split_ratio: Optional[int] = None
|
||||||
|
|
||||||
|
# ordered record of known constituent trade messages
|
||||||
|
clears: dict[
|
||||||
|
Union[str, int, Status], # trade id
|
||||||
|
dict[str, Any], # transaction history summaries
|
||||||
|
] = {}
|
||||||
|
first_clear_dt: Optional[datetime] = None
|
||||||
|
|
||||||
|
expiry: Optional[datetime] = None
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
f: getattr(self, f)
|
||||||
|
for f in self.__struct_fields__
|
||||||
|
}
|
||||||
|
|
||||||
|
def to_pretoml(self) -> tuple[str, dict]:
|
||||||
|
'''
|
||||||
|
Prep this position's data contents for export to toml including
|
||||||
|
re-structuring of the ``.clears`` table to an array of
|
||||||
|
inline-subtables for better ``pps.toml`` compactness.
|
||||||
|
|
||||||
|
'''
|
||||||
|
d = self.to_dict()
|
||||||
|
clears = d.pop('clears')
|
||||||
|
expiry = d.pop('expiry')
|
||||||
|
|
||||||
|
if self.split_ratio is None:
|
||||||
|
d.pop('split_ratio')
|
||||||
|
|
||||||
|
# should be obvious from clears/event table
|
||||||
|
d.pop('first_clear_dt')
|
||||||
|
|
||||||
|
# TODO: we need to figure out how to have one top level
|
||||||
|
# listing venue here even when the backend isn't providing
|
||||||
|
# it via the trades ledger..
|
||||||
|
# drop symbol obj in serialized form
|
||||||
|
s = d.pop('symbol')
|
||||||
|
fqsn = s.front_fqsn()
|
||||||
|
|
||||||
|
if self.expiry is None:
|
||||||
|
d.pop('expiry', None)
|
||||||
|
elif expiry:
|
||||||
|
d['expiry'] = str(expiry)
|
||||||
|
|
||||||
|
toml_clears_list = []
|
||||||
|
|
||||||
|
# reverse sort so latest clears are at top of section?
|
||||||
|
for tid, data in iter_by_dt(clears):
|
||||||
|
inline_table = toml.TomlDecoder().get_empty_inline_table()
|
||||||
|
|
||||||
|
# serialize datetime to parsable `str`
|
||||||
|
inline_table['dt'] = str(data['dt'])
|
||||||
|
|
||||||
|
# insert optional clear fields in column order
|
||||||
|
for k in ['ppu', 'accum_size']:
|
||||||
|
val = data.get(k)
|
||||||
|
if val:
|
||||||
|
inline_table[k] = val
|
||||||
|
|
||||||
|
# insert required fields
|
||||||
|
for k in ['price', 'size', 'cost']:
|
||||||
|
inline_table[k] = data[k]
|
||||||
|
|
||||||
|
inline_table['tid'] = tid
|
||||||
|
toml_clears_list.append(inline_table)
|
||||||
|
|
||||||
|
d['clears'] = toml_clears_list
|
||||||
|
|
||||||
|
return fqsn, d
|
||||||
|
|
||||||
|
def ensure_state(self) -> None:
|
||||||
|
'''
|
||||||
|
Audit either the `.size` and `.ppu` local instance vars against
|
||||||
|
the clears table calculations and return the calc-ed values if
|
||||||
|
they differ and log warnings to console.
|
||||||
|
|
||||||
|
'''
|
||||||
|
clears = list(self.clears.values())
|
||||||
|
self.first_clear_dt = min(list(entry['dt'] for entry in clears))
|
||||||
|
last_clear = clears[-1]
|
||||||
|
|
||||||
|
csize = self.calc_size()
|
||||||
|
accum = last_clear['accum_size']
|
||||||
|
if not self.expired():
|
||||||
|
if (
|
||||||
|
csize != accum
|
||||||
|
and csize != round(accum * self.split_ratio or 1)
|
||||||
|
):
|
||||||
|
raise ValueError(f'Size mismatch: {csize}')
|
||||||
|
else:
|
||||||
|
assert csize == 0, 'Contract is expired but non-zero size?'
|
||||||
|
|
||||||
|
if self.size != csize:
|
||||||
|
log.warning(
|
||||||
|
'Position state mismatch:\n'
|
||||||
|
f'{self.size} => {csize}'
|
||||||
|
)
|
||||||
|
self.size = csize
|
||||||
|
|
||||||
|
cppu = self.calc_ppu()
|
||||||
|
ppu = last_clear['ppu']
|
||||||
|
if (
|
||||||
|
cppu != ppu
|
||||||
|
and self.split_ratio is not None
|
||||||
|
# handle any split info entered (for now) manually by user
|
||||||
|
and cppu != (ppu / self.split_ratio)
|
||||||
|
):
|
||||||
|
raise ValueError(f'PPU mismatch: {cppu}')
|
||||||
|
|
||||||
|
if self.ppu != cppu:
|
||||||
|
log.warning(
|
||||||
|
'Position state mismatch:\n'
|
||||||
|
f'{self.ppu} => {cppu}'
|
||||||
|
)
|
||||||
|
self.ppu = cppu
|
||||||
|
|
||||||
|
def update_from_msg(
|
||||||
|
self,
|
||||||
|
msg: BrokerdPosition,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
# XXX: better place to do this?
|
||||||
|
symbol = self.symbol
|
||||||
|
|
||||||
|
lot_size_digits = symbol.lot_size_digits
|
||||||
|
ppu, size = (
|
||||||
|
round(
|
||||||
|
msg['avg_price'],
|
||||||
|
ndigits=symbol.tick_size_digits
|
||||||
|
),
|
||||||
|
round(
|
||||||
|
msg['size'],
|
||||||
|
ndigits=lot_size_digits
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.ppu = ppu
|
||||||
|
self.size = size
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dsize(self) -> float:
|
||||||
|
'''
|
||||||
|
The "dollar" size of the pp, normally in trading (fiat) unit
|
||||||
|
terms.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self.ppu * self.size
|
||||||
|
|
||||||
|
# TODO: idea: "real LIFO" dynamic positioning.
|
||||||
|
# - when a trade takes place where the pnl for
|
||||||
|
# the (set of) trade(s) is below the breakeven price
|
||||||
|
# it may be that the trader took a +ve pnl on a short(er)
|
||||||
|
# term trade in the same account.
|
||||||
|
# - in this case we could recalc the be price to
|
||||||
|
# be reverted back to it's prior value before the nearest term
|
||||||
|
# trade was opened.?
|
||||||
|
# def lifo_price() -> float:
|
||||||
|
# ...
|
||||||
|
|
||||||
|
def iter_clears(self) -> Iterator[tuple[str, dict]]:
|
||||||
|
'''
|
||||||
|
Iterate the internally managed ``.clears: dict`` table in
|
||||||
|
datetime-stamped order.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return iter_by_dt(self.clears)
|
||||||
|
|
||||||
|
def calc_ppu(
|
||||||
|
self,
|
||||||
|
# include transaction cost in breakeven price
|
||||||
|
# and presume the worst case of the same cost
|
||||||
|
# to exit this transaction (even though in reality
|
||||||
|
# it will be dynamic based on exit stratetgy).
|
||||||
|
cost_scalar: float = 2,
|
||||||
|
|
||||||
|
) -> float:
|
||||||
|
'''
|
||||||
|
Compute the "price-per-unit" price for the given non-zero sized
|
||||||
|
rolling position.
|
||||||
|
|
||||||
|
The recurrence relation which computes this (exponential) mean
|
||||||
|
per new clear which **increases** the accumulative postiion size
|
||||||
|
is:
|
||||||
|
|
||||||
|
ppu[-1] = (
|
||||||
|
ppu[-2] * accum_size[-2]
|
||||||
|
+
|
||||||
|
ppu[-1] * size
|
||||||
|
) / accum_size[-1]
|
||||||
|
|
||||||
|
where `cost_basis` for the current step is simply the price
|
||||||
|
* size of the most recent clearing transaction.
|
||||||
|
|
||||||
|
'''
|
||||||
|
asize_h: list[float] = [] # historical accumulative size
|
||||||
|
ppu_h: list[float] = [] # historical price-per-unit
|
||||||
|
|
||||||
|
tid: str
|
||||||
|
entry: dict[str, Any]
|
||||||
|
for (tid, entry) in self.iter_clears():
|
||||||
|
clear_size = entry['size']
|
||||||
|
clear_price = entry['price']
|
||||||
|
|
||||||
|
last_accum_size = asize_h[-1] if asize_h else 0
|
||||||
|
accum_size = last_accum_size + clear_size
|
||||||
|
accum_sign = copysign(1, accum_size)
|
||||||
|
|
||||||
|
sign_change: bool = False
|
||||||
|
|
||||||
|
if accum_size == 0:
|
||||||
|
ppu_h.append(0)
|
||||||
|
asize_h.append(0)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if accum_size == 0:
|
||||||
|
ppu_h.append(0)
|
||||||
|
asize_h.append(0)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# test if the pp somehow went "passed" a net zero size state
|
||||||
|
# resulting in a change of the "sign" of the size (+ve for
|
||||||
|
# long, -ve for short).
|
||||||
|
sign_change = (
|
||||||
|
copysign(1, last_accum_size) + accum_sign == 0
|
||||||
|
and last_accum_size != 0
|
||||||
|
)
|
||||||
|
|
||||||
|
# since we passed the net-zero-size state the new size
|
||||||
|
# after sum should be the remaining size the new
|
||||||
|
# "direction" (aka, long vs. short) for this clear.
|
||||||
|
if sign_change:
|
||||||
|
clear_size = accum_size
|
||||||
|
abs_diff = abs(accum_size)
|
||||||
|
asize_h.append(0)
|
||||||
|
ppu_h.append(0)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# old size minus the new size gives us size diff with
|
||||||
|
# +ve -> increase in pp size
|
||||||
|
# -ve -> decrease in pp size
|
||||||
|
abs_diff = abs(accum_size) - abs(last_accum_size)
|
||||||
|
|
||||||
|
# XXX: LIFO breakeven price update. only an increaze in size
|
||||||
|
# of the position contributes the breakeven price,
|
||||||
|
# a decrease does not (i.e. the position is being made
|
||||||
|
# smaller).
|
||||||
|
# abs_clear_size = abs(clear_size)
|
||||||
|
abs_new_size = abs(accum_size)
|
||||||
|
|
||||||
|
if abs_diff > 0:
|
||||||
|
|
||||||
|
cost_basis = (
|
||||||
|
# cost basis for this clear
|
||||||
|
clear_price * abs(clear_size)
|
||||||
|
+
|
||||||
|
# transaction cost
|
||||||
|
accum_sign * cost_scalar * entry['cost']
|
||||||
|
)
|
||||||
|
|
||||||
|
if asize_h:
|
||||||
|
size_last = abs(asize_h[-1])
|
||||||
|
cb_last = ppu_h[-1] * size_last
|
||||||
|
ppu = (cost_basis + cb_last) / abs_new_size
|
||||||
|
|
||||||
|
else:
|
||||||
|
ppu = cost_basis / abs_new_size
|
||||||
|
|
||||||
|
ppu_h.append(ppu)
|
||||||
|
asize_h.append(accum_size)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# on "exit" clears from a given direction,
|
||||||
|
# only the size changes not the price-per-unit
|
||||||
|
# need to be updated since the ppu remains constant
|
||||||
|
# and gets weighted by the new size.
|
||||||
|
asize_h.append(accum_size)
|
||||||
|
ppu_h.append(ppu_h[-1])
|
||||||
|
|
||||||
|
final_ppu = ppu_h[-1] if ppu_h else 0
|
||||||
|
|
||||||
|
# handle any split info entered (for now) manually by user
|
||||||
|
if self.split_ratio is not None:
|
||||||
|
final_ppu /= self.split_ratio
|
||||||
|
|
||||||
|
return final_ppu
|
||||||
|
|
||||||
|
def expired(self) -> bool:
|
||||||
|
'''
|
||||||
|
Predicate which checks if the contract/instrument is past its expiry.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return bool(self.expiry) and self.expiry < now()
|
||||||
|
|
||||||
|
def calc_size(self) -> float:
|
||||||
|
'''
|
||||||
|
Calculate the unit size of this position in the destination
|
||||||
|
asset using the clears/trade event table; zero if expired.
|
||||||
|
|
||||||
|
'''
|
||||||
|
size: float = 0
|
||||||
|
|
||||||
|
# time-expired pps (normally derivatives) are "closed"
|
||||||
|
# and have a zero size.
|
||||||
|
if self.expired():
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for tid, entry in self.clears.items():
|
||||||
|
size += entry['size']
|
||||||
|
|
||||||
|
if self.split_ratio is not None:
|
||||||
|
size = round(size * self.split_ratio)
|
||||||
|
|
||||||
|
return size
|
||||||
|
|
||||||
|
def minimize_clears(
|
||||||
|
self,
|
||||||
|
|
||||||
|
) -> dict[str, dict]:
|
||||||
|
'''
|
||||||
|
Minimize the position's clears entries by removing
|
||||||
|
all transactions before the last net zero size to avoid
|
||||||
|
unecessary history irrelevant to the current pp state.
|
||||||
|
|
||||||
|
'''
|
||||||
|
size: float = 0
|
||||||
|
clears_since_zero: list[tuple(str, dict)] = []
|
||||||
|
|
||||||
|
# TODO: we might just want to always do this when iterating
|
||||||
|
# a ledger? keep a state of the last net-zero and only do the
|
||||||
|
# full iterate when no state was stashed?
|
||||||
|
|
||||||
|
# scan for the last "net zero" position by iterating
|
||||||
|
# transactions until the next net-zero size, rinse, repeat.
|
||||||
|
for tid, clear in self.clears.items():
|
||||||
|
size += clear['size']
|
||||||
|
clears_since_zero.append((tid, clear))
|
||||||
|
|
||||||
|
if size == 0:
|
||||||
|
clears_since_zero.clear()
|
||||||
|
|
||||||
|
self.clears = dict(clears_since_zero)
|
||||||
|
return self.clears
|
||||||
|
|
||||||
|
def add_clear(
|
||||||
|
self,
|
||||||
|
t: Transaction,
|
||||||
|
) -> dict:
|
||||||
|
'''
|
||||||
|
Update clearing table and populate rolling ppu and accumulative
|
||||||
|
size in both the clears entry and local attrs state.
|
||||||
|
|
||||||
|
'''
|
||||||
|
clear = self.clears[t.tid] = {
|
||||||
|
'cost': t.cost,
|
||||||
|
'price': t.price,
|
||||||
|
'size': t.size,
|
||||||
|
'dt': t.dt,
|
||||||
|
}
|
||||||
|
|
||||||
|
# TODO: compute these incrementally instead
|
||||||
|
# of re-looping through each time resulting in O(n**2)
|
||||||
|
# behaviour..?
|
||||||
|
|
||||||
|
# NOTE: we compute these **after** adding the entry in order to
|
||||||
|
# make the recurrence relation math work inside
|
||||||
|
# ``.calc_size()``.
|
||||||
|
self.size = clear['accum_size'] = self.calc_size()
|
||||||
|
self.ppu = clear['ppu'] = self.calc_ppu()
|
||||||
|
|
||||||
|
return clear
|
||||||
|
|
||||||
|
def sugest_split(self) -> float:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class PpTable(Struct):
|
||||||
|
|
||||||
|
brokername: str
|
||||||
|
acctid: str
|
||||||
|
pps: dict[str, Position]
|
||||||
|
conf: Optional[dict] = {}
|
||||||
|
|
||||||
|
def update_from_trans(
|
||||||
|
self,
|
||||||
|
trans: dict[str, Transaction],
|
||||||
|
cost_scalar: float = 2,
|
||||||
|
|
||||||
|
) -> dict[str, Position]:
|
||||||
|
|
||||||
|
pps = self.pps
|
||||||
|
updated: dict[str, Position] = {}
|
||||||
|
|
||||||
|
# lifo update all pps from records
|
||||||
|
for tid, t in trans.items():
|
||||||
|
|
||||||
|
pp = pps.setdefault(
|
||||||
|
t.bsuid,
|
||||||
|
|
||||||
|
# if no existing pp, allocate fresh one.
|
||||||
|
Position(
|
||||||
|
Symbol.from_fqsn(
|
||||||
|
t.fqsn,
|
||||||
|
info={},
|
||||||
|
),
|
||||||
|
size=0.0,
|
||||||
|
ppu=0.0,
|
||||||
|
bsuid=t.bsuid,
|
||||||
|
expiry=t.expiry,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
clears = pp.clears
|
||||||
|
if clears:
|
||||||
|
first_clear_dt = pp.first_clear_dt
|
||||||
|
|
||||||
|
# don't do updates for ledger records we already have
|
||||||
|
# included in the current pps state.
|
||||||
|
if (
|
||||||
|
t.tid in clears
|
||||||
|
or first_clear_dt and t.dt < first_clear_dt
|
||||||
|
):
|
||||||
|
# NOTE: likely you'll see repeats of the same
|
||||||
|
# ``Transaction`` passed in here if/when you are restarting
|
||||||
|
# a ``brokerd.ib`` where the API will re-report trades from
|
||||||
|
# the current session, so we need to make sure we don't
|
||||||
|
# "double count" these in pp calculations.
|
||||||
|
continue
|
||||||
|
|
||||||
|
# update clearing table
|
||||||
|
pp.add_clear(t)
|
||||||
|
updated[t.bsuid] = pp
|
||||||
|
|
||||||
|
# minimize clears tables and update sizing.
|
||||||
|
for bsuid, pp in updated.items():
|
||||||
|
pp.ensure_state()
|
||||||
|
|
||||||
|
return updated
|
||||||
|
|
||||||
|
def dump_active(
|
||||||
|
self,
|
||||||
|
) -> tuple[
|
||||||
|
dict[str, Position],
|
||||||
|
dict[str, Position]
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Iterate all tabulated positions, render active positions to
|
||||||
|
a ``dict`` format amenable to serialization (via TOML) and drop
|
||||||
|
from state (``.pps``) as well as return in a ``dict`` all
|
||||||
|
``Position``s which have recently closed.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# NOTE: newly closed position are also important to report/return
|
||||||
|
# since a consumer, like an order mode UI ;), might want to react
|
||||||
|
# based on the closure (for example removing the breakeven line
|
||||||
|
# and clearing the entry from any lists/monitors).
|
||||||
|
closed_pp_objs: dict[str, Position] = {}
|
||||||
|
open_pp_objs: dict[str, Position] = {}
|
||||||
|
|
||||||
|
pp_objs = self.pps
|
||||||
|
for bsuid in list(pp_objs):
|
||||||
|
pp = pp_objs[bsuid]
|
||||||
|
|
||||||
|
# XXX: debug hook for size mismatches
|
||||||
|
# qqqbsuid = 320227571
|
||||||
|
# if bsuid == qqqbsuid:
|
||||||
|
# breakpoint()
|
||||||
|
|
||||||
|
pp.ensure_state()
|
||||||
|
|
||||||
|
if (
|
||||||
|
# "net-zero" is a "closed" position
|
||||||
|
pp.size == 0
|
||||||
|
|
||||||
|
# time-expired pps (normally derivatives) are "closed"
|
||||||
|
or (pp.expiry and pp.expiry < now())
|
||||||
|
):
|
||||||
|
# for expired cases
|
||||||
|
pp.size = 0
|
||||||
|
|
||||||
|
# NOTE: we DO NOT pop the pp here since it can still be
|
||||||
|
# used to check for duplicate clears that may come in as
|
||||||
|
# new transaction from some backend API and need to be
|
||||||
|
# ignored; the closed positions won't be written to the
|
||||||
|
# ``pps.toml`` since ``pp_active_entries`` above is what's
|
||||||
|
# written.
|
||||||
|
closed_pp_objs[bsuid] = pp
|
||||||
|
|
||||||
|
else:
|
||||||
|
open_pp_objs[bsuid] = pp
|
||||||
|
|
||||||
|
return open_pp_objs, closed_pp_objs
|
||||||
|
|
||||||
|
def to_toml(
|
||||||
|
self,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
|
||||||
|
active, closed = self.dump_active()
|
||||||
|
|
||||||
|
# ONLY dict-serialize all active positions; those that are closed
|
||||||
|
# we don't store in the ``pps.toml``.
|
||||||
|
to_toml_dict = {}
|
||||||
|
|
||||||
|
for bsuid, pos in active.items():
|
||||||
|
|
||||||
|
# keep the minimal amount of clears that make up this
|
||||||
|
# position since the last net-zero state.
|
||||||
|
pos.minimize_clears()
|
||||||
|
pos.ensure_state()
|
||||||
|
|
||||||
|
# serialize to pre-toml form
|
||||||
|
fqsn, asdict = pos.to_pretoml()
|
||||||
|
log.info(f'Updating active pp: {fqsn}')
|
||||||
|
|
||||||
|
# XXX: ugh, it's cuz we push the section under
|
||||||
|
# the broker name.. maybe we need to rethink this?
|
||||||
|
brokerless_key = fqsn.removeprefix(f'{self.brokername}.')
|
||||||
|
to_toml_dict[brokerless_key] = asdict
|
||||||
|
|
||||||
|
return to_toml_dict
|
||||||
|
|
||||||
|
def write_config(self) -> None:
|
||||||
|
'''
|
||||||
|
Write the current position table to the user's ``pps.toml``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# TODO: show diff output?
|
||||||
|
# https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries
|
||||||
|
print(f'Updating ``pps.toml`` for {path}:\n')
|
||||||
|
|
||||||
|
# active, closed_pp_objs = table.dump_active()
|
||||||
|
pp_entries = self.to_toml()
|
||||||
|
self.conf[self.brokername][self.acctid] = pp_entries
|
||||||
|
|
||||||
|
# TODO: why tf haven't they already done this for inline
|
||||||
|
# tables smh..
|
||||||
|
enc = PpsEncoder(preserve=True)
|
||||||
|
# table_bs_type = type(toml.TomlDecoder().get_empty_inline_table())
|
||||||
|
enc.dump_funcs[
|
||||||
|
toml.decoder.InlineTableDict
|
||||||
|
] = enc.dump_inline_table
|
||||||
|
|
||||||
|
config.write(
|
||||||
|
self.conf,
|
||||||
|
'pps',
|
||||||
|
encoder=enc,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def load_pps_from_ledger(
|
||||||
|
|
||||||
|
brokername: str,
|
||||||
|
acctname: str,
|
||||||
|
|
||||||
|
# post normalization filter on ledger entries to be processed
|
||||||
|
filter_by: Optional[list[dict]] = None,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
dict[str, Transaction],
|
||||||
|
dict[str, Position],
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Open a ledger file by broker name and account and read in and
|
||||||
|
process any trade records into our normalized ``Transaction`` form
|
||||||
|
and then update the equivalent ``Pptable`` and deliver the two
|
||||||
|
bsuid-mapped dict-sets of the transactions and pps.
|
||||||
|
|
||||||
|
'''
|
||||||
|
with (
|
||||||
|
open_trade_ledger(brokername, acctname) as ledger,
|
||||||
|
open_pps(brokername, acctname) as table,
|
||||||
|
):
|
||||||
|
if not ledger:
|
||||||
|
# null case, no ledger file with content
|
||||||
|
return {}
|
||||||
|
|
||||||
|
mod = get_brokermod(brokername)
|
||||||
|
src_records: dict[str, Transaction] = mod.norm_trade_records(ledger)
|
||||||
|
|
||||||
|
if filter_by:
|
||||||
|
records = {}
|
||||||
|
bsuids = set(filter_by)
|
||||||
|
for tid, r in src_records.items():
|
||||||
|
if r.bsuid in bsuids:
|
||||||
|
records[tid] = r
|
||||||
|
else:
|
||||||
|
records = src_records
|
||||||
|
|
||||||
|
updated = table.update_from_trans(records)
|
||||||
|
|
||||||
|
return records, updated
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: instead see if we can hack tomli and tomli-w to do the same:
|
||||||
|
# - https://github.com/hukkin/tomli
|
||||||
|
# - https://github.com/hukkin/tomli-w
|
||||||
|
class PpsEncoder(toml.TomlEncoder):
|
||||||
|
'''
|
||||||
|
Special "styled" encoder that makes a ``pps.toml`` redable and
|
||||||
|
compact by putting `.clears` tables inline and everything else
|
||||||
|
flat-ish.
|
||||||
|
|
||||||
|
'''
|
||||||
|
separator = ','
|
||||||
|
|
||||||
|
def dump_list(self, v):
|
||||||
|
'''
|
||||||
|
Dump an inline list with a newline after every element and
|
||||||
|
with consideration for denoted inline table types.
|
||||||
|
|
||||||
|
'''
|
||||||
|
retval = "[\n"
|
||||||
|
for u in v:
|
||||||
|
if isinstance(u, toml.decoder.InlineTableDict):
|
||||||
|
out = self.dump_inline_table(u)
|
||||||
|
else:
|
||||||
|
out = str(self.dump_value(u))
|
||||||
|
|
||||||
|
retval += " " + out + "," + "\n"
|
||||||
|
retval += "]"
|
||||||
|
return retval
|
||||||
|
|
||||||
|
def dump_inline_table(self, section):
|
||||||
|
"""Preserve inline table in its compact syntax instead of expanding
|
||||||
|
into subsection.
|
||||||
|
https://github.com/toml-lang/toml#user-content-inline-table
|
||||||
|
"""
|
||||||
|
val_list = []
|
||||||
|
for k, v in section.items():
|
||||||
|
# if isinstance(v, toml.decoder.InlineTableDict):
|
||||||
|
if isinstance(v, dict):
|
||||||
|
val = self.dump_inline_table(v)
|
||||||
|
else:
|
||||||
|
val = str(self.dump_value(v))
|
||||||
|
|
||||||
|
val_list.append(k + " = " + val)
|
||||||
|
|
||||||
|
retval = "{ " + ", ".join(val_list) + " }"
|
||||||
|
return retval
|
||||||
|
|
||||||
|
def dump_sections(self, o, sup):
|
||||||
|
retstr = ""
|
||||||
|
if sup != "" and sup[-1] != ".":
|
||||||
|
sup += '.'
|
||||||
|
retdict = self._dict()
|
||||||
|
arraystr = ""
|
||||||
|
for section in o:
|
||||||
|
qsection = str(section)
|
||||||
|
value = o[section]
|
||||||
|
|
||||||
|
if not re.match(r'^[A-Za-z0-9_-]+$', section):
|
||||||
|
qsection = toml.encoder._dump_str(section)
|
||||||
|
|
||||||
|
# arrayoftables = False
|
||||||
|
if (
|
||||||
|
self.preserve
|
||||||
|
and isinstance(value, toml.decoder.InlineTableDict)
|
||||||
|
):
|
||||||
|
retstr += (
|
||||||
|
qsection
|
||||||
|
+
|
||||||
|
" = "
|
||||||
|
+
|
||||||
|
self.dump_inline_table(o[section])
|
||||||
|
+
|
||||||
|
'\n' # only on the final terminating left brace
|
||||||
|
)
|
||||||
|
|
||||||
|
# XXX: this code i'm pretty sure is just blatantly bad
|
||||||
|
# and/or wrong..
|
||||||
|
# if isinstance(o[section], list):
|
||||||
|
# for a in o[section]:
|
||||||
|
# if isinstance(a, dict):
|
||||||
|
# arrayoftables = True
|
||||||
|
# if arrayoftables:
|
||||||
|
# for a in o[section]:
|
||||||
|
# arraytabstr = "\n"
|
||||||
|
# arraystr += "[[" + sup + qsection + "]]\n"
|
||||||
|
# s, d = self.dump_sections(a, sup + qsection)
|
||||||
|
# if s:
|
||||||
|
# if s[0] == "[":
|
||||||
|
# arraytabstr += s
|
||||||
|
# else:
|
||||||
|
# arraystr += s
|
||||||
|
# while d:
|
||||||
|
# newd = self._dict()
|
||||||
|
# for dsec in d:
|
||||||
|
# s1, d1 = self.dump_sections(d[dsec], sup +
|
||||||
|
# qsection + "." +
|
||||||
|
# dsec)
|
||||||
|
# if s1:
|
||||||
|
# arraytabstr += ("[" + sup + qsection +
|
||||||
|
# "." + dsec + "]\n")
|
||||||
|
# arraytabstr += s1
|
||||||
|
# for s1 in d1:
|
||||||
|
# newd[dsec + "." + s1] = d1[s1]
|
||||||
|
# d = newd
|
||||||
|
# arraystr += arraytabstr
|
||||||
|
|
||||||
|
elif isinstance(value, dict):
|
||||||
|
retdict[qsection] = o[section]
|
||||||
|
|
||||||
|
elif o[section] is not None:
|
||||||
|
retstr += (
|
||||||
|
qsection
|
||||||
|
+
|
||||||
|
" = "
|
||||||
|
+
|
||||||
|
str(self.dump_value(o[section]))
|
||||||
|
)
|
||||||
|
|
||||||
|
# if not isinstance(value, dict):
|
||||||
|
if not isinstance(value, toml.decoder.InlineTableDict):
|
||||||
|
# inline tables should not contain newlines:
|
||||||
|
# https://toml.io/en/v1.0.0#inline-table
|
||||||
|
retstr += '\n'
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError(value)
|
||||||
|
|
||||||
|
retstr += arraystr
|
||||||
|
return (retstr, retdict)
|
||||||
|
|
||||||
|
|
||||||
|
@cm
|
||||||
|
def open_pps(
|
||||||
|
brokername: str,
|
||||||
|
acctid: str,
|
||||||
|
write_on_exit: bool = True,
|
||||||
|
|
||||||
|
) -> PpTable:
|
||||||
|
'''
|
||||||
|
Read out broker-specific position entries from
|
||||||
|
incremental update file: ``pps.toml``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
conf, path = config.load('pps')
|
||||||
|
brokersection = conf.setdefault(brokername, {})
|
||||||
|
pps = brokersection.setdefault(acctid, {})
|
||||||
|
|
||||||
|
# TODO: ideally we can pass in an existing
|
||||||
|
# pps state to this right? such that we
|
||||||
|
# don't have to do a ledger reload all the
|
||||||
|
# time.. a couple ideas I can think of,
|
||||||
|
# - mirror this in some client side actor which
|
||||||
|
# does the actual ledger updates (say the paper
|
||||||
|
# engine proc if we decide to always spawn it?),
|
||||||
|
# - do diffs against updates from the ledger writer
|
||||||
|
# actor and the in-mem state here?
|
||||||
|
|
||||||
|
pp_objs = {}
|
||||||
|
table = PpTable(
|
||||||
|
brokername,
|
||||||
|
acctid,
|
||||||
|
pp_objs,
|
||||||
|
conf=conf,
|
||||||
|
)
|
||||||
|
|
||||||
|
# unmarshal/load ``pps.toml`` config entries into object form
|
||||||
|
# and update `PpTable` obj entries.
|
||||||
|
for fqsn, entry in pps.items():
|
||||||
|
bsuid = entry['bsuid']
|
||||||
|
|
||||||
|
# convert clears sub-tables (only in this form
|
||||||
|
# for toml re-presentation) back into a master table.
|
||||||
|
clears_list = entry['clears']
|
||||||
|
|
||||||
|
# index clears entries in "object" form by tid in a top
|
||||||
|
# level dict instead of a list (as is presented in our
|
||||||
|
# ``pps.toml``).
|
||||||
|
clears = pp_objs.setdefault(bsuid, {})
|
||||||
|
|
||||||
|
# TODO: should be make a ``Struct`` for clear/event entries?
|
||||||
|
# convert "clear events table" from the toml config (list of
|
||||||
|
# a dicts) and load it into object form for use in position
|
||||||
|
# processing of new clear events.
|
||||||
|
trans: list[Transaction] = []
|
||||||
|
|
||||||
|
for clears_table in clears_list:
|
||||||
|
tid = clears_table.pop('tid')
|
||||||
|
dtstr = clears_table['dt']
|
||||||
|
dt = pendulum.parse(dtstr)
|
||||||
|
clears_table['dt'] = dt
|
||||||
|
trans.append(Transaction(
|
||||||
|
fqsn=bsuid,
|
||||||
|
bsuid=bsuid,
|
||||||
|
tid=tid,
|
||||||
|
size=clears_table['size'],
|
||||||
|
price=clears_table['price'],
|
||||||
|
cost=clears_table['cost'],
|
||||||
|
dt=dt,
|
||||||
|
))
|
||||||
|
clears[tid] = clears_table
|
||||||
|
|
||||||
|
size = entry['size']
|
||||||
|
|
||||||
|
# TODO: remove but, handle old field name for now
|
||||||
|
ppu = entry.get('ppu', entry.get('be_price', 0))
|
||||||
|
split_ratio = entry.get('split_ratio')
|
||||||
|
|
||||||
|
expiry = entry.get('expiry')
|
||||||
|
if expiry:
|
||||||
|
expiry = pendulum.parse(expiry)
|
||||||
|
|
||||||
|
pp = pp_objs[bsuid] = Position(
|
||||||
|
Symbol.from_fqsn(fqsn, info={}),
|
||||||
|
size=size,
|
||||||
|
ppu=ppu,
|
||||||
|
split_ratio=split_ratio,
|
||||||
|
expiry=expiry,
|
||||||
|
bsuid=entry['bsuid'],
|
||||||
|
)
|
||||||
|
|
||||||
|
# XXX: super critical, we need to be sure to include
|
||||||
|
# all pps.toml clears to avoid reusing clears that were
|
||||||
|
# already included in the current incremental update
|
||||||
|
# state, since today's records may have already been
|
||||||
|
# processed!
|
||||||
|
for t in trans:
|
||||||
|
pp.add_clear(t)
|
||||||
|
|
||||||
|
# audit entries loaded from toml
|
||||||
|
pp.ensure_state()
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield table
|
||||||
|
finally:
|
||||||
|
if write_on_exit:
|
||||||
|
table.write_config()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
|
||||||
|
args = sys.argv
|
||||||
|
assert len(args) > 1, 'Specifiy account(s) from `brokers.toml`'
|
||||||
|
args = args[1:]
|
||||||
|
for acctid in args:
|
||||||
|
broker, name = acctid.split('.')
|
||||||
|
trans, updated_pps = load_pps_from_ledger(broker, name)
|
||||||
|
print(
|
||||||
|
f'Processing transactions into pps for {broker}:{acctid}\n'
|
||||||
|
f'{pformat(trans)}\n\n'
|
||||||
|
f'{pformat(updated_pps)}'
|
||||||
|
)
|
|
@ -32,16 +32,22 @@ def mk_marker_path(
|
||||||
style: str,
|
style: str,
|
||||||
|
|
||||||
) -> QGraphicsPathItem:
|
) -> QGraphicsPathItem:
|
||||||
"""Add a marker to be displayed on the line wrapped in a ``QGraphicsPathItem``
|
'''
|
||||||
ready to be placed using scene coordinates (not view).
|
Add a marker to be displayed on the line wrapped in
|
||||||
|
a ``QGraphicsPathItem`` ready to be placed using scene coordinates
|
||||||
|
(not view).
|
||||||
|
|
||||||
**Arguments**
|
**Arguments**
|
||||||
style String indicating the style of marker to add:
|
style String indicating the style of marker to add:
|
||||||
``'<|'``, ``'|>'``, ``'>|'``, ``'|<'``, ``'<|>'``,
|
``'<|'``, ``'|>'``, ``'>|'``, ``'|<'``, ``'<|>'``,
|
||||||
``'>|<'``, ``'^'``, ``'v'``, ``'o'``
|
``'>|<'``, ``'^'``, ``'v'``, ``'o'``
|
||||||
size Size of the marker in pixels.
|
|
||||||
|
|
||||||
"""
|
This code is taken nearly verbatim from the
|
||||||
|
`InfiniteLine.addMarker()` method but does not attempt do be aware
|
||||||
|
of low(er) level graphics controls and expects for the output
|
||||||
|
polygon to be applied to a ``QGraphicsPathItem``.
|
||||||
|
|
||||||
|
'''
|
||||||
path = QtGui.QPainterPath()
|
path = QtGui.QPainterPath()
|
||||||
|
|
||||||
if style == 'o':
|
if style == 'o':
|
||||||
|
@ -87,7 +93,8 @@ def mk_marker_path(
|
||||||
|
|
||||||
|
|
||||||
class LevelMarker(QGraphicsPathItem):
|
class LevelMarker(QGraphicsPathItem):
|
||||||
'''An arrow marker path graphich which redraws itself
|
'''
|
||||||
|
An arrow marker path graphich which redraws itself
|
||||||
to the specified view coordinate level on each paint cycle.
|
to the specified view coordinate level on each paint cycle.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -104,7 +111,8 @@ class LevelMarker(QGraphicsPathItem):
|
||||||
|
|
||||||
# get polygon and scale
|
# get polygon and scale
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.scale(size, size)
|
# self.setScale(size, size)
|
||||||
|
self.setScale(size)
|
||||||
|
|
||||||
# interally generates path
|
# interally generates path
|
||||||
self._style = None
|
self._style = None
|
||||||
|
@ -114,6 +122,7 @@ class LevelMarker(QGraphicsPathItem):
|
||||||
|
|
||||||
self.get_level = get_level
|
self.get_level = get_level
|
||||||
self._on_paint = on_paint
|
self._on_paint = on_paint
|
||||||
|
|
||||||
self.scene_x = lambda: chart.marker_right_points()[1]
|
self.scene_x = lambda: chart.marker_right_points()[1]
|
||||||
self.level: float = 0
|
self.level: float = 0
|
||||||
self.keep_in_view = keep_in_view
|
self.keep_in_view = keep_in_view
|
||||||
|
@ -149,12 +158,9 @@ class LevelMarker(QGraphicsPathItem):
|
||||||
def w(self) -> float:
|
def w(self) -> float:
|
||||||
return self.path_br().width()
|
return self.path_br().width()
|
||||||
|
|
||||||
def position_in_view(
|
def position_in_view(self) -> None:
|
||||||
self,
|
'''
|
||||||
# level: float,
|
Show a pp off-screen indicator for a level label.
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''Show a pp off-screen indicator for a level label.
|
|
||||||
|
|
||||||
This is like in fps games where you have a gps "nav" indicator
|
This is like in fps games where you have a gps "nav" indicator
|
||||||
but your teammate is outside the range of view, except in 2D, on
|
but your teammate is outside the range of view, except in 2D, on
|
||||||
|
@ -162,7 +168,6 @@ class LevelMarker(QGraphicsPathItem):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
level = self.get_level()
|
level = self.get_level()
|
||||||
|
|
||||||
view = self.chart.getViewBox()
|
view = self.chart.getViewBox()
|
||||||
vr = view.state['viewRange']
|
vr = view.state['viewRange']
|
||||||
ymn, ymx = vr[1]
|
ymn, ymx = vr[1]
|
||||||
|
@ -186,7 +191,6 @@ class LevelMarker(QGraphicsPathItem):
|
||||||
)
|
)
|
||||||
|
|
||||||
elif level < ymn: # pin to bottom of view
|
elif level < ymn: # pin to bottom of view
|
||||||
|
|
||||||
self.setPos(
|
self.setPos(
|
||||||
QPointF(
|
QPointF(
|
||||||
x,
|
x,
|
||||||
|
@ -211,7 +215,8 @@ class LevelMarker(QGraphicsPathItem):
|
||||||
w: QtWidgets.QWidget
|
w: QtWidgets.QWidget
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Core paint which we override to always update
|
'''
|
||||||
|
Core paint which we override to always update
|
||||||
our marker position in scene coordinates from a
|
our marker position in scene coordinates from a
|
||||||
view cooridnate "level".
|
view cooridnate "level".
|
||||||
|
|
||||||
|
@ -235,11 +240,12 @@ def qgo_draw_markers(
|
||||||
right_offset: float,
|
right_offset: float,
|
||||||
|
|
||||||
) -> float:
|
) -> float:
|
||||||
"""Paint markers in ``pg.GraphicsItem`` style by first
|
'''
|
||||||
|
Paint markers in ``pg.GraphicsItem`` style by first
|
||||||
removing the view transform for the painter, drawing the markers
|
removing the view transform for the painter, drawing the markers
|
||||||
in scene coords, then restoring the view coords.
|
in scene coords, then restoring the view coords.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
# paint markers in native coordinate system
|
# paint markers in native coordinate system
|
||||||
orig_tr = p.transform()
|
orig_tr = p.transform()
|
||||||
|
|
||||||
|
|
|
@ -19,15 +19,16 @@ Main app startup and run.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from types import ModuleType
|
||||||
|
|
||||||
from PyQt5.QtCore import QEvent
|
from PyQt5.QtCore import QEvent
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
from .._daemon import maybe_spawn_brokerd
|
from .._daemon import maybe_spawn_brokerd
|
||||||
from ..brokers import get_brokermod
|
|
||||||
from . import _event
|
from . import _event
|
||||||
from ._exec import run_qtractor
|
from ._exec import run_qtractor
|
||||||
from ..data.feed import install_brokerd_search
|
from ..data.feed import install_brokerd_search
|
||||||
|
from ..data._source import unpack_fqsn
|
||||||
from . import _search
|
from . import _search
|
||||||
from ._chart import GodWidget
|
from ._chart import GodWidget
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
@ -36,27 +37,26 @@ log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def load_provider_search(
|
async def load_provider_search(
|
||||||
|
brokermod: str,
|
||||||
broker: str,
|
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
log.info(f'loading brokerd for {broker}..')
|
name = brokermod.name
|
||||||
|
log.info(f'loading brokerd for {name}..')
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
|
|
||||||
maybe_spawn_brokerd(
|
maybe_spawn_brokerd(
|
||||||
broker,
|
name,
|
||||||
loglevel=loglevel
|
loglevel=loglevel
|
||||||
) as portal,
|
) as portal,
|
||||||
|
|
||||||
install_brokerd_search(
|
install_brokerd_search(
|
||||||
portal,
|
portal,
|
||||||
get_brokermod(broker),
|
brokermod,
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
|
||||||
# keep search engine stream up until cancelled
|
# keep search engine stream up until cancelled
|
||||||
await trio.sleep_forever()
|
await trio.sleep_forever()
|
||||||
|
|
||||||
|
@ -66,8 +66,8 @@ async def _async_main(
|
||||||
# implicit required argument provided by ``qtractor_run()``
|
# implicit required argument provided by ``qtractor_run()``
|
||||||
main_widget: GodWidget,
|
main_widget: GodWidget,
|
||||||
|
|
||||||
sym: str,
|
syms: list[str],
|
||||||
brokernames: str,
|
brokers: dict[str, ModuleType],
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -78,6 +78,8 @@ async def _async_main(
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from . import _display
|
from . import _display
|
||||||
|
from ._pg_overrides import _do_overrides
|
||||||
|
_do_overrides()
|
||||||
|
|
||||||
godwidget = main_widget
|
godwidget = main_widget
|
||||||
|
|
||||||
|
@ -97,6 +99,11 @@ async def _async_main(
|
||||||
sbar = godwidget.window.status_bar
|
sbar = godwidget.window.status_bar
|
||||||
starting_done = sbar.open_status('starting ze sexy chartz')
|
starting_done = sbar.open_status('starting ze sexy chartz')
|
||||||
|
|
||||||
|
needed_brokermods: dict[str, ModuleType] = {}
|
||||||
|
for fqsn in syms:
|
||||||
|
brokername, *_ = unpack_fqsn(fqsn)
|
||||||
|
needed_brokermods[brokername] = brokers[brokername]
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
trio.open_nursery() as root_n,
|
trio.open_nursery() as root_n,
|
||||||
):
|
):
|
||||||
|
@ -107,18 +114,14 @@ async def _async_main(
|
||||||
# setup search widget and focus main chart view at startup
|
# setup search widget and focus main chart view at startup
|
||||||
# search widget is a singleton alongside the godwidget
|
# search widget is a singleton alongside the godwidget
|
||||||
search = _search.SearchWidget(godwidget=godwidget)
|
search = _search.SearchWidget(godwidget=godwidget)
|
||||||
search.bar.unfocus()
|
# search.bar.unfocus()
|
||||||
|
# godwidget.hbox.addWidget(search)
|
||||||
godwidget.hbox.addWidget(search)
|
|
||||||
godwidget.search = search
|
godwidget.search = search
|
||||||
|
|
||||||
symbol, _, provider = sym.rpartition('.')
|
|
||||||
|
|
||||||
# this internally starts a ``display_symbol_data()`` task above
|
# this internally starts a ``display_symbol_data()`` task above
|
||||||
order_mode_ready = await godwidget.load_symbol(
|
order_mode_ready = await godwidget.load_symbols(
|
||||||
provider,
|
fqsns=syms,
|
||||||
symbol,
|
loglevel=loglevel,
|
||||||
loglevel
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# spin up a search engine for the local cached symbol set
|
# spin up a search engine for the local cached symbol set
|
||||||
|
@ -135,8 +138,12 @@ async def _async_main(
|
||||||
):
|
):
|
||||||
# load other providers into search **after**
|
# load other providers into search **after**
|
||||||
# the chart's select cache
|
# the chart's select cache
|
||||||
for broker in brokernames:
|
for brokername, mod in needed_brokermods.items():
|
||||||
root_n.start_soon(load_provider_search, broker, loglevel)
|
root_n.start_soon(
|
||||||
|
load_provider_search,
|
||||||
|
mod,
|
||||||
|
loglevel,
|
||||||
|
)
|
||||||
|
|
||||||
await order_mode_ready.wait()
|
await order_mode_ready.wait()
|
||||||
|
|
||||||
|
@ -165,19 +172,22 @@ async def _async_main(
|
||||||
|
|
||||||
|
|
||||||
def _main(
|
def _main(
|
||||||
sym: str,
|
syms: list[str],
|
||||||
brokernames: [str],
|
brokermods: list[ModuleType],
|
||||||
piker_loglevel: str,
|
piker_loglevel: str,
|
||||||
tractor_kwargs,
|
tractor_kwargs,
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Sync entry point to start a chart: a ``tractor`` + Qt runtime
|
Sync entry point to start a chart: a ``tractor`` + Qt runtime.
|
||||||
entry point
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
run_qtractor(
|
run_qtractor(
|
||||||
func=_async_main,
|
func=_async_main,
|
||||||
args=(sym, brokernames, piker_loglevel),
|
args=(
|
||||||
main_widget=GodWidget,
|
syms,
|
||||||
|
{mod.name: mod for mod in brokermods},
|
||||||
|
piker_loglevel,
|
||||||
|
),
|
||||||
|
main_widget_type=GodWidget,
|
||||||
tractor_kwargs=tractor_kwargs,
|
tractor_kwargs=tractor_kwargs,
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
Chart axes graphics and behavior.
|
Chart axes graphics and behavior.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from typing import Optional, Callable
|
from typing import Optional, Callable
|
||||||
from math import floor
|
from math import floor
|
||||||
|
@ -27,6 +28,7 @@ import pyqtgraph as pg
|
||||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
from PyQt5.QtCore import QPointF
|
from PyQt5.QtCore import QPointF
|
||||||
|
|
||||||
|
from . import _pg_overrides as pgo
|
||||||
from ..data._source import float_digits
|
from ..data._source import float_digits
|
||||||
from ._label import Label
|
from ._label import Label
|
||||||
from ._style import DpiAwareFont, hcolor, _font
|
from ._style import DpiAwareFont, hcolor, _font
|
||||||
|
@ -39,12 +41,17 @@ class Axis(pg.AxisItem):
|
||||||
'''
|
'''
|
||||||
A better axis that sizes tick contents considering font size.
|
A better axis that sizes tick contents considering font size.
|
||||||
|
|
||||||
|
Also includes tick values lru caching originally proposed in but never
|
||||||
|
accepted upstream:
|
||||||
|
https://github.com/pyqtgraph/pyqtgraph/pull/2160
|
||||||
|
|
||||||
'''
|
'''
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
linkedsplits,
|
plotitem: pgo.PlotItem,
|
||||||
typical_max_str: str = '100 000.000',
|
typical_max_str: str = '100 000.000 ',
|
||||||
text_color: str = 'bracket',
|
text_color: str = 'bracket',
|
||||||
|
lru_cache_tick_strings: bool = True,
|
||||||
**kwargs
|
**kwargs
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -56,41 +63,78 @@ class Axis(pg.AxisItem):
|
||||||
# XXX: pretty sure this makes things slower
|
# XXX: pretty sure this makes things slower
|
||||||
# self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
# self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
||||||
|
|
||||||
self.linkedsplits = linkedsplits
|
self.pi = plotitem
|
||||||
self._dpi_font = _font
|
self._dpi_font = _font
|
||||||
|
|
||||||
self.setTickFont(_font.font)
|
self.setTickFont(_font.font)
|
||||||
font_size = self._dpi_font.font.pixelSize()
|
font_size = self._dpi_font.font.pixelSize()
|
||||||
|
|
||||||
|
style_conf = {
|
||||||
|
'textFillLimits': [(0, 0.5)],
|
||||||
|
'tickFont': self._dpi_font.font,
|
||||||
|
|
||||||
|
}
|
||||||
|
text_offset = None
|
||||||
if self.orientation in ('bottom',):
|
if self.orientation in ('bottom',):
|
||||||
text_offset = floor(0.25 * font_size)
|
text_offset = floor(0.25 * font_size)
|
||||||
|
|
||||||
elif self.orientation in ('left', 'right'):
|
elif self.orientation in ('left', 'right'):
|
||||||
text_offset = floor(font_size / 2)
|
text_offset = floor(font_size / 2)
|
||||||
|
|
||||||
self.setStyle(**{
|
if text_offset:
|
||||||
'textFillLimits': [(0, 0.5)],
|
style_conf.update({
|
||||||
'tickFont': self._dpi_font.font,
|
|
||||||
|
|
||||||
# offset of text *away from* axis line in px
|
# offset of text *away from* axis line in px
|
||||||
# use approx. half the font pixel size (height)
|
# use approx. half the font pixel size (height)
|
||||||
'tickTextOffset': text_offset,
|
'tickTextOffset': text_offset,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
self.setStyle(**style_conf)
|
||||||
self.setTickFont(_font.font)
|
self.setTickFont(_font.font)
|
||||||
|
|
||||||
# NOTE: this is for surrounding "border"
|
# NOTE: this is for surrounding "border"
|
||||||
self.setPen(_axis_pen)
|
self.setPen(_axis_pen)
|
||||||
|
|
||||||
# this is the text color
|
# this is the text color
|
||||||
# self.setTextPen(pg.mkPen(hcolor(text_color)))
|
|
||||||
self.text_color = text_color
|
self.text_color = text_color
|
||||||
|
|
||||||
|
# generate a bounding rect based on sizing to a "typical"
|
||||||
|
# maximum length-ed string defined as init default.
|
||||||
self.typical_br = _font._qfm.boundingRect(typical_max_str)
|
self.typical_br = _font._qfm.boundingRect(typical_max_str)
|
||||||
|
|
||||||
# size the pertinent axis dimension to a "typical value"
|
# size the pertinent axis dimension to a "typical value"
|
||||||
self.size_to_values()
|
self.size_to_values()
|
||||||
|
|
||||||
|
# NOTE: requires override ``.tickValues()`` method seen below.
|
||||||
|
if lru_cache_tick_strings:
|
||||||
|
self.tickStrings = lru_cache(
|
||||||
|
maxsize=2**20
|
||||||
|
)(self.tickStrings)
|
||||||
|
|
||||||
|
# axis "sticky" labels
|
||||||
|
self._stickies: dict[str, YAxisLabel] = {}
|
||||||
|
|
||||||
|
# NOTE: only overriden to cast tick values entries into tuples
|
||||||
|
# for use with the lru caching.
|
||||||
|
def tickValues(
|
||||||
|
self,
|
||||||
|
minVal: float,
|
||||||
|
maxVal: float,
|
||||||
|
size: int,
|
||||||
|
|
||||||
|
) -> list[tuple[float, tuple[str]]]:
|
||||||
|
'''
|
||||||
|
Repack tick values into tuples for lru caching.
|
||||||
|
|
||||||
|
'''
|
||||||
|
ticks = []
|
||||||
|
for scalar, values in super().tickValues(minVal, maxVal, size):
|
||||||
|
ticks.append((
|
||||||
|
scalar,
|
||||||
|
tuple(values), # this
|
||||||
|
))
|
||||||
|
|
||||||
|
return ticks
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def text_color(self) -> str:
|
def text_color(self) -> str:
|
||||||
return self._text_color
|
return self._text_color
|
||||||
|
@ -106,6 +150,38 @@ class Axis(pg.AxisItem):
|
||||||
def txt_offsets(self) -> tuple[int, int]:
|
def txt_offsets(self) -> tuple[int, int]:
|
||||||
return tuple(self.style['tickTextOffset'])
|
return tuple(self.style['tickTextOffset'])
|
||||||
|
|
||||||
|
def add_sticky(
|
||||||
|
self,
|
||||||
|
pi: pgo.PlotItem,
|
||||||
|
name: None | str = None,
|
||||||
|
digits: None | int = 2,
|
||||||
|
bg_color='default',
|
||||||
|
fg_color='black',
|
||||||
|
|
||||||
|
) -> YAxisLabel:
|
||||||
|
|
||||||
|
# if the sticky is for our symbol
|
||||||
|
# use the tick size precision for display
|
||||||
|
name = name or pi.name
|
||||||
|
digits = digits or 2
|
||||||
|
|
||||||
|
# TODO: ``._ysticks`` should really be an attr on each
|
||||||
|
# ``PlotItem`` now instead of the containing widget (because of
|
||||||
|
# overlays) ?
|
||||||
|
|
||||||
|
# add y-axis "last" value label
|
||||||
|
sticky = self._stickies[name] = YAxisLabel(
|
||||||
|
pi=pi,
|
||||||
|
parent=self,
|
||||||
|
digits=digits, # TODO: pass this from symbol data
|
||||||
|
opacity=0.9, # slight see-through
|
||||||
|
bg_color=bg_color,
|
||||||
|
fg_color=fg_color,
|
||||||
|
)
|
||||||
|
|
||||||
|
pi.sigRangeChanged.connect(sticky.update_on_resize)
|
||||||
|
return sticky
|
||||||
|
|
||||||
|
|
||||||
class PriceAxis(Axis):
|
class PriceAxis(Axis):
|
||||||
|
|
||||||
|
@ -167,7 +243,6 @@ class PriceAxis(Axis):
|
||||||
self._min_tick = size
|
self._min_tick = size
|
||||||
|
|
||||||
def size_to_values(self) -> None:
|
def size_to_values(self) -> None:
|
||||||
# self.typical_br = _font._qfm.boundingRect(typical_max_str)
|
|
||||||
self.setWidth(self.typical_br.width())
|
self.setWidth(self.typical_br.width())
|
||||||
|
|
||||||
# XXX: drop for now since it just eats up h space
|
# XXX: drop for now since it just eats up h space
|
||||||
|
@ -222,28 +297,47 @@ class DynamicDateAxis(Axis):
|
||||||
|
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
|
|
||||||
chart = self.linkedsplits.chart
|
# XX: ARGGGGG AG:LKSKDJF:LKJSDFD
|
||||||
flow = chart._flows[chart.name]
|
chart = self.pi.chart_widget
|
||||||
shm = flow.shm
|
|
||||||
bars = shm.array
|
viz = chart._vizs[chart.name]
|
||||||
|
shm = viz.shm
|
||||||
|
array = shm.array
|
||||||
|
times = array['time']
|
||||||
|
i_0, i_l = times[0], times[-1]
|
||||||
|
|
||||||
|
if (
|
||||||
|
(indexes[0] < i_0
|
||||||
|
and indexes[-1] < i_l)
|
||||||
|
or
|
||||||
|
(indexes[0] > i_0
|
||||||
|
and indexes[-1] > i_l)
|
||||||
|
):
|
||||||
|
return []
|
||||||
|
|
||||||
|
if viz.index_field == 'index':
|
||||||
|
arr_len = times.shape[0]
|
||||||
first = shm._first.value
|
first = shm._first.value
|
||||||
|
epochs = times[
|
||||||
bars_len = len(bars)
|
list(
|
||||||
times = bars['time']
|
|
||||||
|
|
||||||
epochs = times[list(
|
|
||||||
map(
|
map(
|
||||||
int,
|
int,
|
||||||
filter(
|
filter(
|
||||||
lambda i: i > 0 and i < bars_len,
|
lambda i: i > 0 and i < arr_len,
|
||||||
(i-first for i in indexes)
|
(i - first for i in indexes)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)]
|
)
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
epochs = list(map(int, indexes))
|
||||||
|
|
||||||
# TODO: **don't** have this hard coded shift to EST
|
# TODO: **don't** have this hard coded shift to EST
|
||||||
# delay = times[-1] - times[-2]
|
# delay = times[-1] - times[-2]
|
||||||
dts = np.array(epochs, dtype='datetime64[s]')
|
dts = np.array(
|
||||||
|
epochs,
|
||||||
|
dtype='datetime64[s]',
|
||||||
|
)
|
||||||
|
|
||||||
# see units listing:
|
# see units listing:
|
||||||
# https://numpy.org/devdocs/reference/arrays.datetime.html#datetime-units
|
# https://numpy.org/devdocs/reference/arrays.datetime.html#datetime-units
|
||||||
|
@ -261,24 +355,39 @@ class DynamicDateAxis(Axis):
|
||||||
spacing: float,
|
spacing: float,
|
||||||
|
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
|
|
||||||
|
return self._indexes_to_timestrs(values)
|
||||||
|
|
||||||
|
# NOTE: handy for debugging the lru cache
|
||||||
# info = self.tickStrings.cache_info()
|
# info = self.tickStrings.cache_info()
|
||||||
# print(info)
|
# print(info)
|
||||||
return self._indexes_to_timestrs(values)
|
|
||||||
|
|
||||||
|
|
||||||
class AxisLabel(pg.GraphicsObject):
|
class AxisLabel(pg.GraphicsObject):
|
||||||
|
|
||||||
_x_margin = 0
|
# relative offsets *OF* the bounding rect relative
|
||||||
_y_margin = 0
|
# to parent graphics object.
|
||||||
|
# eg. <parent>| => <_x_br_offset> => | <text> |
|
||||||
|
_x_br_offset: float = 0
|
||||||
|
_y_br_offset: float = 0
|
||||||
|
|
||||||
|
# relative offsets of text *within* bounding rect
|
||||||
|
# eg. | <_x_margin> => <text> |
|
||||||
|
_x_margin: float = 0
|
||||||
|
_y_margin: float = 0
|
||||||
|
|
||||||
|
# multiplier of the text content's height in order
|
||||||
|
# to force a larger (y-dimension) bounding rect.
|
||||||
|
_y_txt_h_scaling: float = 1
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
parent: pg.GraphicsItem,
|
parent: pg.GraphicsItem,
|
||||||
digits: int = 2,
|
digits: int = 2,
|
||||||
|
|
||||||
bg_color: str = 'bracket',
|
bg_color: str = 'default',
|
||||||
fg_color: str = 'black',
|
fg_color: str = 'black',
|
||||||
opacity: int = 1, # XXX: seriously don't set this to 0
|
opacity: int = .8, # XXX: seriously don't set this to 0
|
||||||
font_size: str = 'default',
|
font_size: str = 'default',
|
||||||
|
|
||||||
use_arrow: bool = True,
|
use_arrow: bool = True,
|
||||||
|
@ -289,6 +398,7 @@ class AxisLabel(pg.GraphicsObject):
|
||||||
self.setParentItem(parent)
|
self.setParentItem(parent)
|
||||||
|
|
||||||
self.setFlag(self.ItemIgnoresTransformations)
|
self.setFlag(self.ItemIgnoresTransformations)
|
||||||
|
self.setZValue(100)
|
||||||
|
|
||||||
# XXX: pretty sure this is faster
|
# XXX: pretty sure this is faster
|
||||||
self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
||||||
|
@ -320,14 +430,14 @@ class AxisLabel(pg.GraphicsObject):
|
||||||
p: QtGui.QPainter,
|
p: QtGui.QPainter,
|
||||||
opt: QtWidgets.QStyleOptionGraphicsItem,
|
opt: QtWidgets.QStyleOptionGraphicsItem,
|
||||||
w: QtWidgets.QWidget
|
w: QtWidgets.QWidget
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Draw a filled rectangle based on the size of ``.label_str`` text.
|
'''
|
||||||
|
Draw a filled rectangle based on the size of ``.label_str`` text.
|
||||||
|
|
||||||
Subtypes can customize further by overloading ``.draw()``.
|
Subtypes can customize further by overloading ``.draw()``.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
# p.setCompositionMode(QtWidgets.QPainter.CompositionMode_SourceOver)
|
|
||||||
|
|
||||||
if self.label_str:
|
if self.label_str:
|
||||||
|
|
||||||
# if not self.rect:
|
# if not self.rect:
|
||||||
|
@ -338,7 +448,11 @@ class AxisLabel(pg.GraphicsObject):
|
||||||
|
|
||||||
p.setFont(self._dpifont.font)
|
p.setFont(self._dpifont.font)
|
||||||
p.setPen(self.fg_color)
|
p.setPen(self.fg_color)
|
||||||
p.drawText(self.rect, self.text_flags, self.label_str)
|
p.drawText(
|
||||||
|
self.rect,
|
||||||
|
self.text_flags,
|
||||||
|
self.label_str,
|
||||||
|
)
|
||||||
|
|
||||||
def draw(
|
def draw(
|
||||||
self,
|
self,
|
||||||
|
@ -346,6 +460,8 @@ class AxisLabel(pg.GraphicsObject):
|
||||||
rect: QtCore.QRectF
|
rect: QtCore.QRectF
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
|
p.setOpacity(self.opacity)
|
||||||
|
|
||||||
if self._use_arrow:
|
if self._use_arrow:
|
||||||
if not self.path:
|
if not self.path:
|
||||||
self._draw_arrow_path()
|
self._draw_arrow_path()
|
||||||
|
@ -353,15 +469,13 @@ class AxisLabel(pg.GraphicsObject):
|
||||||
p.drawPath(self.path)
|
p.drawPath(self.path)
|
||||||
p.fillPath(self.path, pg.mkBrush(self.bg_color))
|
p.fillPath(self.path, pg.mkBrush(self.bg_color))
|
||||||
|
|
||||||
# this adds a nice black outline around the label for some odd
|
|
||||||
# reason; ok by us
|
|
||||||
p.setOpacity(self.opacity)
|
|
||||||
|
|
||||||
# this cause the L1 labels to glitch out if used in the subtype
|
# this cause the L1 labels to glitch out if used in the subtype
|
||||||
# and it will leave a small black strip with the arrow path if
|
# and it will leave a small black strip with the arrow path if
|
||||||
# done before the above
|
# done before the above
|
||||||
p.fillRect(self.rect, self.bg_color)
|
p.fillRect(
|
||||||
|
self.rect,
|
||||||
|
self.bg_color,
|
||||||
|
)
|
||||||
|
|
||||||
def boundingRect(self): # noqa
|
def boundingRect(self): # noqa
|
||||||
'''
|
'''
|
||||||
|
@ -405,15 +519,18 @@ class AxisLabel(pg.GraphicsObject):
|
||||||
txt_h, txt_w = txt_br.height(), txt_br.width()
|
txt_h, txt_w = txt_br.height(), txt_br.width()
|
||||||
# print(f'wsw: {self._dpifont.boundingRect(" ")}')
|
# print(f'wsw: {self._dpifont.boundingRect(" ")}')
|
||||||
|
|
||||||
# allow subtypes to specify a static width and height
|
# allow subtypes to override width and height
|
||||||
h, w = self.size_hint()
|
h, w = self.size_hint()
|
||||||
# print(f'axis size: {self._parent.size()}')
|
|
||||||
# print(f'axis geo: {self._parent.geometry()}')
|
|
||||||
|
|
||||||
self.rect = QtCore.QRectF(
|
self.rect = QtCore.QRectF(
|
||||||
0, 0,
|
|
||||||
|
# relative bounds offsets
|
||||||
|
self._x_br_offset,
|
||||||
|
self._y_br_offset,
|
||||||
|
|
||||||
(w or txt_w) + self._x_margin / 2,
|
(w or txt_w) + self._x_margin / 2,
|
||||||
(h or txt_h) + self._y_margin / 2,
|
|
||||||
|
(h or txt_h) * self._y_txt_h_scaling + (self._y_margin / 2),
|
||||||
)
|
)
|
||||||
# print(self.rect)
|
# print(self.rect)
|
||||||
# hb = self.path.controlPointRect()
|
# hb = self.path.controlPointRect()
|
||||||
|
@ -489,7 +606,7 @@ class XAxisLabel(AxisLabel):
|
||||||
|
|
||||||
|
|
||||||
class YAxisLabel(AxisLabel):
|
class YAxisLabel(AxisLabel):
|
||||||
_y_margin = 4
|
_y_margin: int = 4
|
||||||
|
|
||||||
text_flags = (
|
text_flags = (
|
||||||
QtCore.Qt.AlignLeft
|
QtCore.Qt.AlignLeft
|
||||||
|
@ -500,19 +617,19 @@ class YAxisLabel(AxisLabel):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
chart,
|
pi: pgo.PlotItem,
|
||||||
*args,
|
*args,
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self._chart = chart
|
self._pi = pi
|
||||||
|
pi.sigRangeChanged.connect(self.update_on_resize)
|
||||||
chart.sigRangeChanged.connect(self.update_on_resize)
|
|
||||||
|
|
||||||
self._last_datum = (None, None)
|
self._last_datum = (None, None)
|
||||||
|
|
||||||
|
self.x_offset = 0
|
||||||
# pull text offset from axis from parent axis
|
# pull text offset from axis from parent axis
|
||||||
if getattr(self._parent, 'txt_offsets', False):
|
if getattr(self._parent, 'txt_offsets', False):
|
||||||
self.x_offset, y_offset = self._parent.txt_offsets()
|
self.x_offset, y_offset = self._parent.txt_offsets()
|
||||||
|
@ -531,7 +648,8 @@ class YAxisLabel(AxisLabel):
|
||||||
value: float, # data for text
|
value: float, # data for text
|
||||||
|
|
||||||
# on odd dimension and/or adds nice black line
|
# on odd dimension and/or adds nice black line
|
||||||
x_offset: Optional[int] = None
|
x_offset: int = 0,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
# this is read inside ``.paint()``
|
# this is read inside ``.paint()``
|
||||||
|
@ -577,7 +695,7 @@ class YAxisLabel(AxisLabel):
|
||||||
self._last_datum = (index, value)
|
self._last_datum = (index, value)
|
||||||
|
|
||||||
self.update_label(
|
self.update_label(
|
||||||
self._chart.mapFromView(QPointF(index, value)),
|
self._pi.mapFromView(QPointF(index, value)),
|
||||||
value
|
value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -18,8 +18,13 @@
|
||||||
Mouse interaction graphics
|
Mouse interaction graphics
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import Optional, Callable
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
Callable,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -36,6 +41,12 @@ from ._style import (
|
||||||
from ._axes import YAxisLabel, XAxisLabel
|
from ._axes import YAxisLabel, XAxisLabel
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._chart import (
|
||||||
|
ChartPlotWidget,
|
||||||
|
LinkedSplits,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
@ -58,7 +69,7 @@ class LineDot(pg.CurvePoint):
|
||||||
curve: pg.PlotCurveItem,
|
curve: pg.PlotCurveItem,
|
||||||
index: int,
|
index: int,
|
||||||
|
|
||||||
plot: 'ChartPlotWidget', # type: ingore # noqa
|
plot: ChartPlotWidget, # type: ingore # noqa
|
||||||
pos=None,
|
pos=None,
|
||||||
color: str = 'default_light',
|
color: str = 'default_light',
|
||||||
|
|
||||||
|
@ -151,7 +162,7 @@ class ContentsLabel(pg.LabelItem):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
# chart: 'ChartPlotWidget', # noqa
|
# chart: ChartPlotWidget, # noqa
|
||||||
view: pg.ViewBox,
|
view: pg.ViewBox,
|
||||||
|
|
||||||
anchor_at: str = ('top', 'right'),
|
anchor_at: str = ('top', 'right'),
|
||||||
|
@ -244,7 +255,7 @@ class ContentsLabels:
|
||||||
'''
|
'''
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
linkedsplits: 'LinkedSplits', # type: ignore # noqa
|
linkedsplits: LinkedSplits, # type: ignore # noqa
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
|
@ -263,8 +274,8 @@ class ContentsLabels:
|
||||||
) -> None:
|
) -> None:
|
||||||
for chart, name, label, update in self._labels:
|
for chart, name, label, update in self._labels:
|
||||||
|
|
||||||
flow = chart._flows[name]
|
viz = chart.get_viz(name)
|
||||||
array = flow.shm.array
|
array = viz.shm.array
|
||||||
|
|
||||||
if not (
|
if not (
|
||||||
index >= 0
|
index >= 0
|
||||||
|
@ -289,7 +300,7 @@ class ContentsLabels:
|
||||||
def add_label(
|
def add_label(
|
||||||
|
|
||||||
self,
|
self,
|
||||||
chart: 'ChartPlotWidget', # type: ignore # noqa
|
chart: ChartPlotWidget, # type: ignore # noqa
|
||||||
name: str,
|
name: str,
|
||||||
anchor_at: tuple[str, str] = ('top', 'left'),
|
anchor_at: tuple[str, str] = ('top', 'left'),
|
||||||
update_func: Callable = ContentsLabel.update_from_value,
|
update_func: Callable = ContentsLabel.update_from_value,
|
||||||
|
@ -316,7 +327,7 @@ class Cursor(pg.GraphicsObject):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
||||||
self,
|
self,
|
||||||
linkedsplits: 'LinkedSplits', # noqa
|
linkedsplits: LinkedSplits, # noqa
|
||||||
digits: int = 0
|
digits: int = 0
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -325,6 +336,8 @@ class Cursor(pg.GraphicsObject):
|
||||||
|
|
||||||
self.linked = linkedsplits
|
self.linked = linkedsplits
|
||||||
self.graphics: dict[str, pg.GraphicsObject] = {}
|
self.graphics: dict[str, pg.GraphicsObject] = {}
|
||||||
|
self.xaxis_label: Optional[XAxisLabel] = None
|
||||||
|
self.always_show_xlabel: bool = True
|
||||||
self.plots: list['PlotChartWidget'] = [] # type: ignore # noqa
|
self.plots: list['PlotChartWidget'] = [] # type: ignore # noqa
|
||||||
self.active_plot = None
|
self.active_plot = None
|
||||||
self.digits: int = digits
|
self.digits: int = digits
|
||||||
|
@ -385,7 +398,7 @@ class Cursor(pg.GraphicsObject):
|
||||||
|
|
||||||
def add_plot(
|
def add_plot(
|
||||||
self,
|
self,
|
||||||
plot: 'ChartPlotWidget', # noqa
|
plot: ChartPlotWidget, # noqa
|
||||||
digits: int = 0,
|
digits: int = 0,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -405,7 +418,7 @@ class Cursor(pg.GraphicsObject):
|
||||||
hl.hide()
|
hl.hide()
|
||||||
|
|
||||||
yl = YAxisLabel(
|
yl = YAxisLabel(
|
||||||
chart=plot,
|
pi=plot.plotItem,
|
||||||
# parent=plot.getAxis('right'),
|
# parent=plot.getAxis('right'),
|
||||||
parent=plot.pi_overlay.get_axis(plot.plotItem, 'right'),
|
parent=plot.pi_overlay.get_axis(plot.plotItem, 'right'),
|
||||||
digits=digits or self.digits,
|
digits=digits or self.digits,
|
||||||
|
@ -469,39 +482,58 @@ class Cursor(pg.GraphicsObject):
|
||||||
|
|
||||||
def add_curve_cursor(
|
def add_curve_cursor(
|
||||||
self,
|
self,
|
||||||
plot: 'ChartPlotWidget', # noqa
|
chart: ChartPlotWidget, # noqa
|
||||||
curve: 'PlotCurveItem', # noqa
|
curve: 'PlotCurveItem', # noqa
|
||||||
|
|
||||||
) -> LineDot:
|
) -> LineDot:
|
||||||
# if this plot contains curves add line dot "cursors" to denote
|
# if this chart contains curves add line dot "cursors" to denote
|
||||||
# the current sample under the mouse
|
# the current sample under the mouse
|
||||||
main_flow = plot._flows[plot.name]
|
main_viz = chart.get_viz(chart.name)
|
||||||
|
|
||||||
# read out last index
|
# read out last index
|
||||||
i = main_flow.shm.array[-1]['index']
|
i = main_viz.shm.array[-1]['index']
|
||||||
cursor = LineDot(
|
cursor = LineDot(
|
||||||
curve,
|
curve,
|
||||||
index=i,
|
index=i,
|
||||||
plot=plot
|
plot=chart
|
||||||
)
|
)
|
||||||
plot.addItem(cursor)
|
chart.addItem(cursor)
|
||||||
self.graphics[plot].setdefault('cursors', []).append(cursor)
|
self.graphics[chart].setdefault('cursors', []).append(cursor)
|
||||||
return cursor
|
return cursor
|
||||||
|
|
||||||
def mouseAction(self, action, plot): # noqa
|
def mouseAction(
|
||||||
|
self,
|
||||||
|
action: str,
|
||||||
|
plot: ChartPlotWidget,
|
||||||
|
|
||||||
|
) -> None: # noqa
|
||||||
|
|
||||||
log.debug(f"{(action, plot.name)}")
|
log.debug(f"{(action, plot.name)}")
|
||||||
if action == 'Enter':
|
if action == 'Enter':
|
||||||
self.active_plot = plot
|
self.active_plot = plot
|
||||||
|
plot.linked.godwidget._active_cursor = self
|
||||||
|
|
||||||
# show horiz line and y-label
|
# show horiz line and y-label
|
||||||
self.graphics[plot]['hl'].show()
|
self.graphics[plot]['hl'].show()
|
||||||
self.graphics[plot]['yl'].show()
|
self.graphics[plot]['yl'].show()
|
||||||
|
|
||||||
else: # Leave
|
if (
|
||||||
|
not self.always_show_xlabel
|
||||||
|
and not self.xaxis_label.isVisible()
|
||||||
|
):
|
||||||
|
self.xaxis_label.show()
|
||||||
|
|
||||||
# hide horiz line and y-label
|
# Leave: hide horiz line and y-label
|
||||||
|
else:
|
||||||
self.graphics[plot]['hl'].hide()
|
self.graphics[plot]['hl'].hide()
|
||||||
self.graphics[plot]['yl'].hide()
|
self.graphics[plot]['yl'].hide()
|
||||||
|
|
||||||
|
if (
|
||||||
|
not self.always_show_xlabel
|
||||||
|
and self.xaxis_label.isVisible()
|
||||||
|
):
|
||||||
|
self.xaxis_label.hide()
|
||||||
|
|
||||||
def mouseMoved(
|
def mouseMoved(
|
||||||
self,
|
self,
|
||||||
coords: tuple[QPointF], # noqa
|
coords: tuple[QPointF], # noqa
|
||||||
|
@ -590,6 +622,10 @@ class Cursor(pg.GraphicsObject):
|
||||||
left_axis_width += left.width()
|
left_axis_width += left.width()
|
||||||
|
|
||||||
# map back to abs (label-local) coordinates
|
# map back to abs (label-local) coordinates
|
||||||
|
if (
|
||||||
|
self.always_show_xlabel
|
||||||
|
or self.xaxis_label.isVisible()
|
||||||
|
):
|
||||||
self.xaxis_label.update_label(
|
self.xaxis_label.update_label(
|
||||||
abs_pos=(
|
abs_pos=(
|
||||||
plot.mapFromView(QPointF(vl_x, iy)) -
|
plot.mapFromView(QPointF(vl_x, iy)) -
|
||||||
|
|
|
@ -28,10 +28,7 @@ from PyQt5.QtWidgets import QGraphicsItem
|
||||||
from PyQt5.QtCore import (
|
from PyQt5.QtCore import (
|
||||||
Qt,
|
Qt,
|
||||||
QLineF,
|
QLineF,
|
||||||
QSizeF,
|
|
||||||
QRectF,
|
QRectF,
|
||||||
# QRect,
|
|
||||||
QPointF,
|
|
||||||
)
|
)
|
||||||
from PyQt5.QtGui import (
|
from PyQt5.QtGui import (
|
||||||
QPainter,
|
QPainter,
|
||||||
|
@ -39,11 +36,8 @@ from PyQt5.QtGui import (
|
||||||
)
|
)
|
||||||
from .._profile import pg_profile_enabled, ms_slower_then
|
from .._profile import pg_profile_enabled, ms_slower_then
|
||||||
from ._style import hcolor
|
from ._style import hcolor
|
||||||
# from ._compression import (
|
|
||||||
# # ohlc_to_m4_line,
|
|
||||||
# ds_m4,
|
|
||||||
# )
|
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
from .._profile import Profiler
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
@ -57,7 +51,39 @@ _line_styles: dict[str, int] = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Curve(pg.GraphicsObject):
|
class FlowGraphic(pg.GraphicsObject):
|
||||||
|
'''
|
||||||
|
Base class with minimal interface for `QPainterPath` implemented,
|
||||||
|
real-time updated "data flow" graphics.
|
||||||
|
|
||||||
|
See subtypes below.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# sub-type customization methods
|
||||||
|
declare_paintables: Optional[Callable] = None
|
||||||
|
sub_paint: Optional[Callable] = None
|
||||||
|
|
||||||
|
# TODO: can we remove this?
|
||||||
|
# sub_br: Optional[Callable] = None
|
||||||
|
|
||||||
|
def x_uppx(self) -> int:
|
||||||
|
|
||||||
|
px_vecs = self.pixelVectors()[0]
|
||||||
|
if px_vecs:
|
||||||
|
return px_vecs.x()
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def x_last(self) -> float | None:
|
||||||
|
'''
|
||||||
|
Return the last most x value of the last line segment or if not
|
||||||
|
drawn yet, ``None``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self._last_line.x1() if self._last_line else None
|
||||||
|
|
||||||
|
|
||||||
|
class Curve(FlowGraphic):
|
||||||
'''
|
'''
|
||||||
A faster, simpler, append friendly version of
|
A faster, simpler, append friendly version of
|
||||||
``pyqtgraph.PlotCurveItem`` built for highly customizable real-time
|
``pyqtgraph.PlotCurveItem`` built for highly customizable real-time
|
||||||
|
@ -74,7 +100,7 @@ class Curve(pg.GraphicsObject):
|
||||||
lower level graphics data can be rendered in different threads and
|
lower level graphics data can be rendered in different threads and
|
||||||
then read and drawn in this main thread without having to worry
|
then read and drawn in this main thread without having to worry
|
||||||
about dealing with Qt's concurrency primitives. See
|
about dealing with Qt's concurrency primitives. See
|
||||||
``piker.ui._flows.Renderer`` for details and logic related to lower
|
``piker.ui._render.Renderer`` for details and logic related to lower
|
||||||
level path generation and incremental update. The main differences in
|
level path generation and incremental update. The main differences in
|
||||||
the path generation code include:
|
the path generation code include:
|
||||||
|
|
||||||
|
@ -87,11 +113,6 @@ class Curve(pg.GraphicsObject):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# sub-type customization methods
|
|
||||||
sub_br: Optional[Callable] = None
|
|
||||||
sub_paint: Optional[Callable] = None
|
|
||||||
declare_paintables: Optional[Callable] = None
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*args,
|
*args,
|
||||||
|
@ -101,7 +122,6 @@ class Curve(pg.GraphicsObject):
|
||||||
fill_color: Optional[str] = None,
|
fill_color: Optional[str] = None,
|
||||||
style: str = 'solid',
|
style: str = 'solid',
|
||||||
name: Optional[str] = None,
|
name: Optional[str] = None,
|
||||||
use_fpath: bool = True,
|
|
||||||
|
|
||||||
**kwargs
|
**kwargs
|
||||||
|
|
||||||
|
@ -116,11 +136,11 @@ class Curve(pg.GraphicsObject):
|
||||||
# self._last_cap: int = 0
|
# self._last_cap: int = 0
|
||||||
self.path: Optional[QPainterPath] = None
|
self.path: Optional[QPainterPath] = None
|
||||||
|
|
||||||
# additional path used for appends which tries to avoid
|
# additional path that can be optionally used for appends which
|
||||||
# triggering an update/redraw of the presumably larger
|
# tries to avoid triggering an update/redraw of the presumably
|
||||||
# historical ``.path`` above.
|
# larger historical ``.path`` above. the flag to enable
|
||||||
self.use_fpath = use_fpath
|
# this behaviour is found in `Renderer.render()`.
|
||||||
self.fast_path: Optional[QPainterPath] = None
|
self.fast_path: QPainterPath | None = None
|
||||||
|
|
||||||
# TODO: we can probably just dispense with the parent since
|
# TODO: we can probably just dispense with the parent since
|
||||||
# we're basically only using the pen setting now...
|
# we're basically only using the pen setting now...
|
||||||
|
@ -139,9 +159,7 @@ class Curve(pg.GraphicsObject):
|
||||||
# self.last_step_pen = pg.mkPen(hcolor(color), width=2)
|
# self.last_step_pen = pg.mkPen(hcolor(color), width=2)
|
||||||
self.last_step_pen = pg.mkPen(pen, width=2)
|
self.last_step_pen = pg.mkPen(pen, width=2)
|
||||||
|
|
||||||
# self._last_line: Optional[QLineF] = None
|
self._last_line: QLineF = QLineF()
|
||||||
self._last_line = QLineF()
|
|
||||||
self._last_w: float = 1
|
|
||||||
|
|
||||||
# flat-top style histogram-like discrete curve
|
# flat-top style histogram-like discrete curve
|
||||||
# self._step_mode: bool = step_mode
|
# self._step_mode: bool = step_mode
|
||||||
|
@ -162,51 +180,19 @@ class Curve(pg.GraphicsObject):
|
||||||
# endpoint (something we saw on trade rate curves)
|
# endpoint (something we saw on trade rate curves)
|
||||||
self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
|
self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
|
||||||
|
|
||||||
# XXX: see explanation for different caching modes:
|
# XXX-NOTE-XXX: graphics caching.
|
||||||
# https://stackoverflow.com/a/39410081
|
# see explanation for different caching modes:
|
||||||
# seems to only be useful if we don't re-generate the entire
|
# https://stackoverflow.com/a/39410081 seems to only be useful
|
||||||
# QPainterPath every time
|
# if we don't re-generate the entire QPainterPath every time
|
||||||
# curve.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
|
||||||
|
|
||||||
# don't ever use this - it's a colossal nightmare of artefacts
|
# don't ever use this - it's a colossal nightmare of artefacts
|
||||||
# and is disastrous for performance.
|
# and is disastrous for performance.
|
||||||
# curve.setCacheMode(QtWidgets.QGraphicsItem.ItemCoordinateCache)
|
# self.setCacheMode(QtWidgets.QGraphicsItem.ItemCoordinateCache)
|
||||||
|
|
||||||
# allow sub-type customization
|
# allow sub-type customization
|
||||||
declare = self.declare_paintables
|
declare = self.declare_paintables
|
||||||
if declare:
|
if declare:
|
||||||
declare()
|
declare()
|
||||||
|
|
||||||
# TODO: probably stick this in a new parent
|
|
||||||
# type which will contain our own version of
|
|
||||||
# what ``PlotCurveItem`` had in terms of base
|
|
||||||
# functionality? A `FlowGraphic` maybe?
|
|
||||||
def x_uppx(self) -> int:
|
|
||||||
|
|
||||||
px_vecs = self.pixelVectors()[0]
|
|
||||||
if px_vecs:
|
|
||||||
xs_in_px = px_vecs.x()
|
|
||||||
return round(xs_in_px)
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def px_width(self) -> float:
|
|
||||||
|
|
||||||
vb = self.getViewBox()
|
|
||||||
if not vb:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
vr = self.viewRect()
|
|
||||||
l, r = int(vr.left()), int(vr.right())
|
|
||||||
|
|
||||||
start, stop = self._xrange
|
|
||||||
lbar = max(l, start)
|
|
||||||
rbar = min(r, stop)
|
|
||||||
|
|
||||||
return vb.mapViewToDevice(
|
|
||||||
QLineF(lbar, 0, rbar, 0)
|
|
||||||
).length()
|
|
||||||
|
|
||||||
# XXX: lol brutal, the internals of `CurvePoint` (inherited by
|
# XXX: lol brutal, the internals of `CurvePoint` (inherited by
|
||||||
# our `LineDot`) required ``.getData()`` to work..
|
# our `LineDot`) required ``.getData()`` to work..
|
||||||
def getData(self):
|
def getData(self):
|
||||||
|
@ -230,8 +216,8 @@ class Curve(pg.GraphicsObject):
|
||||||
self.path.clear()
|
self.path.clear()
|
||||||
|
|
||||||
if self.fast_path:
|
if self.fast_path:
|
||||||
# self.fast_path.clear()
|
self.fast_path.clear()
|
||||||
self.fast_path = None
|
# self.fast_path = None
|
||||||
|
|
||||||
@cm
|
@cm
|
||||||
def reset_cache(self) -> None:
|
def reset_cache(self) -> None:
|
||||||
|
@ -251,77 +237,65 @@ class Curve(pg.GraphicsObject):
|
||||||
self.boundingRect = self._path_br
|
self.boundingRect = self._path_br
|
||||||
return self._path_br()
|
return self._path_br()
|
||||||
|
|
||||||
|
# Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
|
||||||
def _path_br(self):
|
def _path_br(self):
|
||||||
'''
|
'''
|
||||||
Post init ``.boundingRect()```.
|
Post init ``.boundingRect()```.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# hb = self.path.boundingRect()
|
# profiler = Profiler(
|
||||||
hb = self.path.controlPointRect()
|
# msg=f'Curve.boundingRect(): `{self._name}`',
|
||||||
hb_size = hb.size()
|
# disabled=not pg_profile_enabled(),
|
||||||
|
# ms_threshold=ms_slower_then,
|
||||||
fp = self.fast_path
|
|
||||||
if fp:
|
|
||||||
fhb = fp.controlPointRect()
|
|
||||||
hb_size = fhb.size() + hb_size
|
|
||||||
|
|
||||||
# print(f'hb_size: {hb_size}')
|
|
||||||
|
|
||||||
# if self._last_step_rect:
|
|
||||||
# hb_size += self._last_step_rect.size()
|
|
||||||
|
|
||||||
# if self._line:
|
|
||||||
# br = self._last_step_rect.bottomRight()
|
|
||||||
|
|
||||||
# tl = QPointF(
|
|
||||||
# # self._vr[0],
|
|
||||||
# # hb.topLeft().y(),
|
|
||||||
# # 0,
|
|
||||||
# # hb_size.height() + 1
|
|
||||||
# )
|
# )
|
||||||
|
pr = self.path.controlPointRect()
|
||||||
|
hb_tl, hb_br = (
|
||||||
|
pr.topLeft(),
|
||||||
|
pr.bottomRight(),
|
||||||
|
)
|
||||||
|
mn_y = hb_tl.y()
|
||||||
|
mx_y = hb_br.y()
|
||||||
|
most_left = hb_tl.x()
|
||||||
|
most_right = hb_br.x()
|
||||||
|
# profiler('calc path vertices')
|
||||||
|
|
||||||
# br = self._last_step_rect.bottomRight()
|
# TODO: if/when we get fast path appends working in the
|
||||||
|
# `Renderer`, then we might need to actually use this..
|
||||||
|
# fp = self.fast_path
|
||||||
|
# if fp:
|
||||||
|
# fhb = fp.controlPointRect()
|
||||||
|
# # hb_size = fhb.size() + hb_size
|
||||||
|
# br = pr.united(fhb)
|
||||||
|
|
||||||
w = hb_size.width()
|
# XXX: *was* a way to allow sub-types to extend the
|
||||||
h = hb_size.height()
|
# boundingrect calc, but in the one use case for a step curve
|
||||||
|
# doesn't seem like we need it as long as the last line segment
|
||||||
|
# is drawn as it is?
|
||||||
|
|
||||||
|
# sbr = self.sub_br
|
||||||
|
# if sbr:
|
||||||
|
# # w, h = self.sub_br(w, h)
|
||||||
|
# sub_br = sbr()
|
||||||
|
# br = br.united(sub_br)
|
||||||
|
|
||||||
sbr = self.sub_br
|
|
||||||
if sbr:
|
|
||||||
w, h = self.sub_br(w, h)
|
|
||||||
else:
|
|
||||||
# assume plain line graphic and use
|
# assume plain line graphic and use
|
||||||
# default unit step in each direction.
|
# default unit step in each direction.
|
||||||
|
ll = self._last_line
|
||||||
|
y1, y2 = ll.y1(), ll.y2()
|
||||||
|
x1, x2 = ll.x1(), ll.x2()
|
||||||
|
|
||||||
# only on a plane line do we include
|
ymn = min(y1, y2, mn_y)
|
||||||
# and extra index step's worth of width
|
ymx = max(y1, y2, mx_y)
|
||||||
# since in the step case the end of the curve
|
most_left = min(x1, x2, most_left)
|
||||||
# actually terminates earlier so we don't need
|
most_right = max(x1, x2, most_right)
|
||||||
# this for the last step.
|
# profiler('calc last line vertices')
|
||||||
w += self._last_w
|
|
||||||
# ll = self._last_line
|
|
||||||
h += 1 # ll.y2() - ll.y1()
|
|
||||||
|
|
||||||
# br = QPointF(
|
return QRectF(
|
||||||
# self._vr[-1],
|
most_left,
|
||||||
# # tl.x() + w,
|
ymn,
|
||||||
# tl.y() + h,
|
most_right - most_left + 1,
|
||||||
# )
|
ymx,
|
||||||
|
|
||||||
br = QRectF(
|
|
||||||
|
|
||||||
# top left
|
|
||||||
# hb.topLeft()
|
|
||||||
# tl,
|
|
||||||
QPointF(hb.topLeft()),
|
|
||||||
|
|
||||||
# br,
|
|
||||||
# total size
|
|
||||||
# QSizeF(hb_size)
|
|
||||||
# hb_size,
|
|
||||||
QSizeF(w, h)
|
|
||||||
)
|
)
|
||||||
# print(f'bounding rect: {br}')
|
|
||||||
return br
|
|
||||||
|
|
||||||
def paint(
|
def paint(
|
||||||
self,
|
self,
|
||||||
|
@ -331,7 +305,7 @@ class Curve(pg.GraphicsObject):
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
msg=f'Curve.paint(): `{self._name}`',
|
msg=f'Curve.paint(): `{self._name}`',
|
||||||
disabled=not pg_profile_enabled(),
|
disabled=not pg_profile_enabled(),
|
||||||
ms_threshold=ms_slower_then,
|
ms_threshold=ms_slower_then,
|
||||||
|
@ -339,7 +313,7 @@ class Curve(pg.GraphicsObject):
|
||||||
|
|
||||||
sub_paint = self.sub_paint
|
sub_paint = self.sub_paint
|
||||||
if sub_paint:
|
if sub_paint:
|
||||||
sub_paint(p, profiler)
|
sub_paint(p)
|
||||||
|
|
||||||
p.setPen(self.last_step_pen)
|
p.setPen(self.last_step_pen)
|
||||||
p.drawLine(self._last_line)
|
p.drawLine(self._last_line)
|
||||||
|
@ -373,22 +347,30 @@ class Curve(pg.GraphicsObject):
|
||||||
self,
|
self,
|
||||||
path: QPainterPath,
|
path: QPainterPath,
|
||||||
src_data: np.ndarray,
|
src_data: np.ndarray,
|
||||||
render_data: np.ndarray,
|
|
||||||
reset: bool,
|
reset: bool,
|
||||||
array_key: str,
|
array_key: str,
|
||||||
|
index_field: str,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
# default line draw last call
|
# default line draw last call
|
||||||
# with self.reset_cache():
|
# with self.reset_cache():
|
||||||
x = render_data['index']
|
x = src_data[index_field]
|
||||||
y = render_data[array_key]
|
y = src_data[array_key]
|
||||||
|
|
||||||
|
x_last = x[-1]
|
||||||
|
x_2last = x[-2]
|
||||||
|
|
||||||
# draw the "current" step graphic segment so it
|
# draw the "current" step graphic segment so it
|
||||||
# lines up with the "middle" of the current
|
# lines up with the "middle" of the current
|
||||||
# (OHLC) sample.
|
# (OHLC) sample.
|
||||||
self._last_line = QLineF(
|
self._last_line = QLineF(
|
||||||
x[-2], y[-2],
|
|
||||||
x[-1], y[-1],
|
# NOTE: currently we draw in x-domain
|
||||||
|
# from last datum to current such that
|
||||||
|
# the end of line touches the "beginning"
|
||||||
|
# of the current datum step span.
|
||||||
|
x_2last , y[-2],
|
||||||
|
x_last, y[-1],
|
||||||
)
|
)
|
||||||
|
|
||||||
return x, y
|
return x, y
|
||||||
|
@ -404,13 +386,13 @@ class FlattenedOHLC(Curve):
|
||||||
self,
|
self,
|
||||||
path: QPainterPath,
|
path: QPainterPath,
|
||||||
src_data: np.ndarray,
|
src_data: np.ndarray,
|
||||||
render_data: np.ndarray,
|
|
||||||
reset: bool,
|
reset: bool,
|
||||||
array_key: str,
|
array_key: str,
|
||||||
|
index_field: str,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
lasts = src_data[-2:]
|
lasts = src_data[-2:]
|
||||||
x = lasts['index']
|
x = lasts[index_field]
|
||||||
y = lasts['close']
|
y = lasts['close']
|
||||||
|
|
||||||
# draw the "current" step graphic segment so it
|
# draw the "current" step graphic segment so it
|
||||||
|
@ -434,9 +416,9 @@ class StepCurve(Curve):
|
||||||
self,
|
self,
|
||||||
path: QPainterPath,
|
path: QPainterPath,
|
||||||
src_data: np.ndarray,
|
src_data: np.ndarray,
|
||||||
render_data: np.ndarray,
|
|
||||||
reset: bool,
|
reset: bool,
|
||||||
array_key: str,
|
array_key: str,
|
||||||
|
index_field: str,
|
||||||
|
|
||||||
w: float = 0.5,
|
w: float = 0.5,
|
||||||
|
|
||||||
|
@ -445,40 +427,31 @@ class StepCurve(Curve):
|
||||||
# TODO: remove this and instead place all step curve
|
# TODO: remove this and instead place all step curve
|
||||||
# updating into pre-path data render callbacks.
|
# updating into pre-path data render callbacks.
|
||||||
# full input data
|
# full input data
|
||||||
x = src_data['index']
|
x = src_data[index_field]
|
||||||
y = src_data[array_key]
|
y = src_data[array_key]
|
||||||
|
|
||||||
x_last = x[-1]
|
x_last = x[-1]
|
||||||
|
x_2last = x[-2]
|
||||||
y_last = y[-1]
|
y_last = y[-1]
|
||||||
|
step_size = x_last - x_2last
|
||||||
|
|
||||||
# lol, commenting this makes step curves
|
# lol, commenting this makes step curves
|
||||||
# all "black" for me :eyeroll:..
|
# all "black" for me :eyeroll:..
|
||||||
self._last_line = QLineF(
|
self._last_line = QLineF(
|
||||||
x_last - w, 0,
|
x_2last, 0,
|
||||||
x_last + w, 0,
|
x_last, 0,
|
||||||
)
|
)
|
||||||
self._last_step_rect = QRectF(
|
self._last_step_rect = QRectF(
|
||||||
x_last - w, 0,
|
x_last, 0,
|
||||||
x_last + w, y_last,
|
step_size, y_last,
|
||||||
)
|
)
|
||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
def sub_paint(
|
def sub_paint(
|
||||||
self,
|
self,
|
||||||
p: QPainter,
|
p: QPainter,
|
||||||
profiler: pg.debug.Profiler,
|
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
# p.drawLines(*tuple(filter(bool, self._last_step_lines)))
|
# p.drawLines(*tuple(filter(bool, self._last_step_lines)))
|
||||||
# p.drawRect(self._last_step_rect)
|
# p.drawRect(self._last_step_rect)
|
||||||
p.fillRect(self._last_step_rect, self._brush)
|
p.fillRect(self._last_step_rect, self._brush)
|
||||||
profiler('.fillRect()')
|
|
||||||
|
|
||||||
def sub_br(
|
|
||||||
self,
|
|
||||||
path_w: float,
|
|
||||||
path_h: float,
|
|
||||||
|
|
||||||
) -> (float, float):
|
|
||||||
# passthrough
|
|
||||||
return path_w, path_h
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
1239
piker/ui/_display.py
1239
piker/ui/_display.py
File diff suppressed because it is too large
Load Diff
|
@ -18,11 +18,27 @@
|
||||||
Higher level annotation editors.
|
Higher level annotation editors.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from dataclasses import dataclass, field
|
from __future__ import annotations
|
||||||
from typing import Optional
|
from collections import defaultdict
|
||||||
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
TYPE_CHECKING
|
||||||
|
)
|
||||||
|
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
from pyqtgraph import ViewBox, Point, QtCore, QtGui
|
from pyqtgraph import (
|
||||||
|
ViewBox,
|
||||||
|
Point,
|
||||||
|
QtCore,
|
||||||
|
QtWidgets,
|
||||||
|
)
|
||||||
|
from PyQt5.QtGui import (
|
||||||
|
QColor,
|
||||||
|
)
|
||||||
|
from PyQt5.QtWidgets import (
|
||||||
|
QLabel,
|
||||||
|
)
|
||||||
|
|
||||||
from pyqtgraph import functions as fn
|
from pyqtgraph import functions as fn
|
||||||
from PyQt5.QtCore import QPointF
|
from PyQt5.QtCore import QPointF
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -30,28 +46,34 @@ import numpy as np
|
||||||
from ._style import hcolor, _font
|
from ._style import hcolor, _font
|
||||||
from ._lines import LevelLine
|
from ._lines import LevelLine
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
from ..data.types import Struct
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._chart import GodWidget
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class ArrowEditor(Struct):
|
||||||
class ArrowEditor:
|
|
||||||
|
|
||||||
chart: 'ChartPlotWidget' # noqa
|
godw: GodWidget = None # type: ignore # noqa
|
||||||
_arrows: field(default_factory=dict)
|
_arrows: dict[str, list[pg.ArrowItem]] = {}
|
||||||
|
|
||||||
def add(
|
def add(
|
||||||
self,
|
self,
|
||||||
|
plot: pg.PlotItem,
|
||||||
uid: str,
|
uid: str,
|
||||||
x: float,
|
x: float,
|
||||||
y: float,
|
y: float,
|
||||||
color='default',
|
color='default',
|
||||||
pointing: Optional[str] = None,
|
pointing: Optional[str] = None,
|
||||||
) -> pg.ArrowItem:
|
|
||||||
"""Add an arrow graphic to view at given (x, y).
|
|
||||||
|
|
||||||
"""
|
) -> pg.ArrowItem:
|
||||||
|
'''
|
||||||
|
Add an arrow graphic to view at given (x, y).
|
||||||
|
|
||||||
|
'''
|
||||||
angle = {
|
angle = {
|
||||||
'up': 90,
|
'up': 90,
|
||||||
'down': -90,
|
'down': -90,
|
||||||
|
@ -74,25 +96,25 @@ class ArrowEditor:
|
||||||
brush=pg.mkBrush(hcolor(color)),
|
brush=pg.mkBrush(hcolor(color)),
|
||||||
)
|
)
|
||||||
arrow.setPos(x, y)
|
arrow.setPos(x, y)
|
||||||
|
self._arrows.setdefault(uid, []).append(arrow)
|
||||||
self._arrows[uid] = arrow
|
|
||||||
|
|
||||||
# render to view
|
# render to view
|
||||||
self.chart.plotItem.addItem(arrow)
|
plot.addItem(arrow)
|
||||||
|
|
||||||
return arrow
|
return arrow
|
||||||
|
|
||||||
def remove(self, arrow) -> bool:
|
def remove(self, arrow) -> bool:
|
||||||
self.chart.plotItem.removeItem(arrow)
|
for linked in self.godw.iter_linked():
|
||||||
|
linked.chart.plotItem.removeItem(arrow)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class LineEditor(Struct):
|
||||||
class LineEditor:
|
'''
|
||||||
'''The great editor of linez.
|
The great editor of linez.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
chart: 'ChartPlotWidget' = None # type: ignore # noqa
|
godw: GodWidget = None # type: ignore # noqa
|
||||||
_order_lines: dict[str, LevelLine] = field(default_factory=dict)
|
_order_lines: defaultdict[str, LevelLine] = defaultdict(list)
|
||||||
_active_staged_line: LevelLine = None
|
_active_staged_line: LevelLine = None
|
||||||
|
|
||||||
def stage_line(
|
def stage_line(
|
||||||
|
@ -100,11 +122,11 @@ class LineEditor:
|
||||||
line: LevelLine,
|
line: LevelLine,
|
||||||
|
|
||||||
) -> LevelLine:
|
) -> LevelLine:
|
||||||
"""Stage a line at the current chart's cursor position
|
'''
|
||||||
|
Stage a line at the current chart's cursor position
|
||||||
and return it.
|
and return it.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
|
|
||||||
# add a "staged" cursor-tracking line to view
|
# add a "staged" cursor-tracking line to view
|
||||||
# and cash it in a a var
|
# and cash it in a a var
|
||||||
if self._active_staged_line:
|
if self._active_staged_line:
|
||||||
|
@ -115,17 +137,25 @@ class LineEditor:
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def unstage_line(self) -> LevelLine:
|
def unstage_line(self) -> LevelLine:
|
||||||
"""Inverse of ``.stage_line()``.
|
'''
|
||||||
|
Inverse of ``.stage_line()``.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
# chart = self.chart._cursor.active_plot
|
cursor = self.godw.get_cursor()
|
||||||
# # chart.setCursor(QtCore.Qt.ArrowCursor)
|
if not cursor:
|
||||||
cursor = self.chart.linked.cursor
|
return None
|
||||||
|
|
||||||
# delete "staged" cursor tracking line from view
|
# delete "staged" cursor tracking line from view
|
||||||
line = self._active_staged_line
|
line = self._active_staged_line
|
||||||
if line:
|
if line:
|
||||||
|
try:
|
||||||
cursor._trackers.remove(line)
|
cursor._trackers.remove(line)
|
||||||
|
except KeyError:
|
||||||
|
# when the current cursor doesn't have said line
|
||||||
|
# registered (probably means that user held order mode
|
||||||
|
# key while panning to another view) then we just
|
||||||
|
# ignore the remove error.
|
||||||
|
pass
|
||||||
line.delete()
|
line.delete()
|
||||||
|
|
||||||
self._active_staged_line = None
|
self._active_staged_line = None
|
||||||
|
@ -133,55 +163,58 @@ class LineEditor:
|
||||||
# show the crosshair y line and label
|
# show the crosshair y line and label
|
||||||
cursor.show_xhair()
|
cursor.show_xhair()
|
||||||
|
|
||||||
def submit_line(
|
def submit_lines(
|
||||||
self,
|
self,
|
||||||
line: LevelLine,
|
lines: list[LevelLine],
|
||||||
uuid: str,
|
uuid: str,
|
||||||
|
|
||||||
) -> LevelLine:
|
) -> LevelLine:
|
||||||
|
|
||||||
staged_line = self._active_staged_line
|
# staged_line = self._active_staged_line
|
||||||
if not staged_line:
|
# if not staged_line:
|
||||||
raise RuntimeError("No line is currently staged!?")
|
# raise RuntimeError("No line is currently staged!?")
|
||||||
|
|
||||||
# for now, until submission reponse arrives
|
# for now, until submission reponse arrives
|
||||||
|
for line in lines:
|
||||||
line.hide_labels()
|
line.hide_labels()
|
||||||
|
|
||||||
# register for later lookup/deletion
|
# register for later lookup/deletion
|
||||||
self._order_lines[uuid] = line
|
self._order_lines[uuid] += lines
|
||||||
|
|
||||||
return line
|
return lines
|
||||||
|
|
||||||
def commit_line(self, uuid: str) -> LevelLine:
|
def commit_line(self, uuid: str) -> list[LevelLine]:
|
||||||
"""Commit a "staged line" to view.
|
'''
|
||||||
|
Commit a "staged line" to view.
|
||||||
|
|
||||||
Submits the line graphic under the cursor as a (new) permanent
|
Submits the line graphic under the cursor as a (new) permanent
|
||||||
graphic in view.
|
graphic in view.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
try:
|
lines = self._order_lines[uuid]
|
||||||
line = self._order_lines[uuid]
|
if lines:
|
||||||
except KeyError:
|
for line in lines:
|
||||||
log.warning(f'No line for {uuid} could be found?')
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
line.show_labels()
|
line.show_labels()
|
||||||
|
line.hide_markers()
|
||||||
|
log.debug(f'Level active for level: {line.value()}')
|
||||||
# TODO: other flashy things to indicate the order is active
|
# TODO: other flashy things to indicate the order is active
|
||||||
|
|
||||||
log.debug(f'Level active for level: {line.value()}')
|
return lines
|
||||||
|
|
||||||
return line
|
|
||||||
|
|
||||||
def lines_under_cursor(self) -> list[LevelLine]:
|
def lines_under_cursor(self) -> list[LevelLine]:
|
||||||
"""Get the line(s) under the cursor position.
|
'''
|
||||||
|
Get the line(s) under the cursor position.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
# Delete any hoverable under the cursor
|
# Delete any hoverable under the cursor
|
||||||
return self.chart.linked.cursor._hovered
|
return self.godw.get_cursor()._hovered
|
||||||
|
|
||||||
def all_lines(self) -> tuple[LevelLine]:
|
def all_lines(self) -> list[LevelLine]:
|
||||||
return tuple(self._order_lines.values())
|
all_lines = []
|
||||||
|
for lines in list(self._order_lines.values()):
|
||||||
|
all_lines.extend(lines)
|
||||||
|
|
||||||
|
return all_lines
|
||||||
|
|
||||||
def remove_line(
|
def remove_line(
|
||||||
self,
|
self,
|
||||||
|
@ -196,29 +229,30 @@ class LineEditor:
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# try to look up line from our registry
|
# try to look up line from our registry
|
||||||
line = self._order_lines.pop(uuid, line)
|
lines = self._order_lines.pop(uuid, None)
|
||||||
if line:
|
if lines:
|
||||||
|
cursor = self.godw.get_cursor()
|
||||||
|
if cursor:
|
||||||
|
for line in lines:
|
||||||
# if hovered remove from cursor set
|
# if hovered remove from cursor set
|
||||||
cursor = self.chart.linked.cursor
|
|
||||||
hovered = cursor._hovered
|
hovered = cursor._hovered
|
||||||
if line in hovered:
|
if line in hovered:
|
||||||
hovered.remove(line)
|
hovered.remove(line)
|
||||||
|
|
||||||
|
log.debug(f'deleting {line} with oid: {uuid}')
|
||||||
|
line.delete()
|
||||||
|
|
||||||
# make sure the xhair doesn't get left off
|
# make sure the xhair doesn't get left off
|
||||||
# just because we never got a un-hover event
|
# just because we never got a un-hover event
|
||||||
cursor.show_xhair()
|
cursor.show_xhair()
|
||||||
|
|
||||||
log.debug(f'deleting {line} with oid: {uuid}')
|
|
||||||
line.delete()
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
log.warning(f'Could not find line for {line}')
|
log.warning(f'Could not find line for {line}')
|
||||||
|
|
||||||
return line
|
return lines
|
||||||
|
|
||||||
|
|
||||||
class SelectRect(QtGui.QGraphicsRectItem):
|
class SelectRect(QtWidgets.QGraphicsRectItem):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
@ -227,12 +261,12 @@ class SelectRect(QtGui.QGraphicsRectItem):
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(0, 0, 1, 1)
|
super().__init__(0, 0, 1, 1)
|
||||||
|
|
||||||
# self.rbScaleBox = QtGui.QGraphicsRectItem(0, 0, 1, 1)
|
# self.rbScaleBox = QGraphicsRectItem(0, 0, 1, 1)
|
||||||
self.vb = viewbox
|
self.vb = viewbox
|
||||||
self._chart: 'ChartPlotWidget' = None # noqa
|
self._chart: 'ChartPlotWidget' = None # noqa
|
||||||
|
|
||||||
# override selection box color
|
# override selection box color
|
||||||
color = QtGui.QColor(hcolor(color))
|
color = QColor(hcolor(color))
|
||||||
self.setPen(fn.mkPen(color, width=1))
|
self.setPen(fn.mkPen(color, width=1))
|
||||||
color.setAlpha(66)
|
color.setAlpha(66)
|
||||||
self.setBrush(fn.mkBrush(color))
|
self.setBrush(fn.mkBrush(color))
|
||||||
|
@ -240,7 +274,7 @@ class SelectRect(QtGui.QGraphicsRectItem):
|
||||||
self.hide()
|
self.hide()
|
||||||
self._label = None
|
self._label = None
|
||||||
|
|
||||||
label = self._label = QtGui.QLabel()
|
label = self._label = QLabel()
|
||||||
label.setTextFormat(0) # markdown
|
label.setTextFormat(0) # markdown
|
||||||
label.setFont(_font.font)
|
label.setFont(_font.font)
|
||||||
label.setMargin(0)
|
label.setMargin(0)
|
||||||
|
@ -277,8 +311,8 @@ class SelectRect(QtGui.QGraphicsRectItem):
|
||||||
# TODO: get bg color working
|
# TODO: get bg color working
|
||||||
palette.setColor(
|
palette.setColor(
|
||||||
self._label.backgroundRole(),
|
self._label.backgroundRole(),
|
||||||
# QtGui.QColor(chart.backgroundBrush()),
|
# QColor(chart.backgroundBrush()),
|
||||||
QtGui.QColor(hcolor('papas_special')),
|
QColor(hcolor('papas_special')),
|
||||||
)
|
)
|
||||||
|
|
||||||
def update_on_resize(self, vr, r):
|
def update_on_resize(self, vr, r):
|
||||||
|
@ -326,7 +360,7 @@ class SelectRect(QtGui.QGraphicsRectItem):
|
||||||
|
|
||||||
self.setPos(r.topLeft())
|
self.setPos(r.topLeft())
|
||||||
self.resetTransform()
|
self.resetTransform()
|
||||||
self.scale(r.width(), r.height())
|
self.setRect(r)
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
y1, y2 = start_pos.y(), end_pos.y()
|
y1, y2 = start_pos.y(), end_pos.y()
|
||||||
|
@ -343,7 +377,7 @@ class SelectRect(QtGui.QGraphicsRectItem):
|
||||||
nbars = ixmx - ixmn + 1
|
nbars = ixmx - ixmn + 1
|
||||||
|
|
||||||
chart = self._chart
|
chart = self._chart
|
||||||
data = chart._flows[chart.name].shm.array[ixmn:ixmx]
|
data = chart.get_viz(chart.name).shm.array[ixmn:ixmx]
|
||||||
|
|
||||||
if len(data):
|
if len(data):
|
||||||
std = data['close'].std()
|
std = data['close'].std()
|
||||||
|
|
|
@ -18,11 +18,11 @@
|
||||||
Qt event proxying and processing using ``trio`` mem chans.
|
Qt event proxying and processing using ``trio`` mem chans.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from contextlib import asynccontextmanager, AsyncExitStack
|
from contextlib import asynccontextmanager as acm
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from pydantic import BaseModel
|
|
||||||
import trio
|
import trio
|
||||||
|
from tractor.trionics import gather_contexts
|
||||||
from PyQt5 import QtCore
|
from PyQt5 import QtCore
|
||||||
from PyQt5.QtCore import QEvent, pyqtBoundSignal
|
from PyQt5.QtCore import QEvent, pyqtBoundSignal
|
||||||
from PyQt5.QtWidgets import QWidget
|
from PyQt5.QtWidgets import QWidget
|
||||||
|
@ -30,6 +30,8 @@ from PyQt5.QtWidgets import (
|
||||||
QGraphicsSceneMouseEvent as gs_mouse,
|
QGraphicsSceneMouseEvent as gs_mouse,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from ..data.types import Struct
|
||||||
|
|
||||||
|
|
||||||
MOUSE_EVENTS = {
|
MOUSE_EVENTS = {
|
||||||
gs_mouse.GraphicsSceneMousePress,
|
gs_mouse.GraphicsSceneMousePress,
|
||||||
|
@ -43,13 +45,10 @@ MOUSE_EVENTS = {
|
||||||
# TODO: maybe consider some constrained ints down the road?
|
# TODO: maybe consider some constrained ints down the road?
|
||||||
# https://pydantic-docs.helpmanual.io/usage/types/#constrained-types
|
# https://pydantic-docs.helpmanual.io/usage/types/#constrained-types
|
||||||
|
|
||||||
class KeyboardMsg(BaseModel):
|
class KeyboardMsg(Struct):
|
||||||
'''Unpacked Qt keyboard event data.
|
'''Unpacked Qt keyboard event data.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
class Config:
|
|
||||||
arbitrary_types_allowed = True
|
|
||||||
|
|
||||||
event: QEvent
|
event: QEvent
|
||||||
etype: int
|
etype: int
|
||||||
key: int
|
key: int
|
||||||
|
@ -57,16 +56,13 @@ class KeyboardMsg(BaseModel):
|
||||||
txt: str
|
txt: str
|
||||||
|
|
||||||
def to_tuple(self) -> tuple:
|
def to_tuple(self) -> tuple:
|
||||||
return tuple(self.dict().values())
|
return tuple(self.to_dict().values())
|
||||||
|
|
||||||
|
|
||||||
class MouseMsg(BaseModel):
|
class MouseMsg(Struct):
|
||||||
'''Unpacked Qt keyboard event data.
|
'''Unpacked Qt keyboard event data.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
class Config:
|
|
||||||
arbitrary_types_allowed = True
|
|
||||||
|
|
||||||
event: QEvent
|
event: QEvent
|
||||||
etype: int
|
etype: int
|
||||||
button: int
|
button: int
|
||||||
|
@ -160,7 +156,7 @@ class EventRelay(QtCore.QObject):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@acm
|
||||||
async def open_event_stream(
|
async def open_event_stream(
|
||||||
|
|
||||||
source_widget: QWidget,
|
source_widget: QWidget,
|
||||||
|
@ -186,7 +182,7 @@ async def open_event_stream(
|
||||||
source_widget.removeEventFilter(kc)
|
source_widget.removeEventFilter(kc)
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@acm
|
||||||
async def open_signal_handler(
|
async def open_signal_handler(
|
||||||
|
|
||||||
signal: pyqtBoundSignal,
|
signal: pyqtBoundSignal,
|
||||||
|
@ -211,7 +207,7 @@ async def open_signal_handler(
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@acm
|
||||||
async def open_handlers(
|
async def open_handlers(
|
||||||
|
|
||||||
source_widgets: list[QWidget],
|
source_widgets: list[QWidget],
|
||||||
|
@ -220,16 +216,14 @@ async def open_handlers(
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
trio.open_nursery() as n,
|
trio.open_nursery() as n,
|
||||||
AsyncExitStack() as stack,
|
gather_contexts([
|
||||||
):
|
|
||||||
for widget in source_widgets:
|
|
||||||
|
|
||||||
event_recv_stream = await stack.enter_async_context(
|
|
||||||
open_event_stream(widget, event_types, **kwargs)
|
open_event_stream(widget, event_types, **kwargs)
|
||||||
)
|
for widget in source_widgets
|
||||||
|
]) as streams,
|
||||||
|
):
|
||||||
|
for widget, event_recv_stream in zip(source_widgets, streams):
|
||||||
n.start_soon(async_handler, widget, event_recv_stream)
|
n.start_soon(async_handler, widget, event_recv_stream)
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -20,16 +20,24 @@ Trio - Qt integration
|
||||||
Run ``trio`` in guest mode on top of the Qt event loop.
|
Run ``trio`` in guest mode on top of the Qt event loop.
|
||||||
All global Qt runtime settings are mostly defined here.
|
All global Qt runtime settings are mostly defined here.
|
||||||
"""
|
"""
|
||||||
from typing import Tuple, Callable, Dict, Any
|
from __future__ import annotations
|
||||||
|
from typing import (
|
||||||
|
Callable,
|
||||||
|
Any,
|
||||||
|
Type,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
import platform
|
import platform
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
# Qt specific
|
# Qt specific
|
||||||
import PyQt5 # noqa
|
import PyQt5 # noqa
|
||||||
import pyqtgraph as pg
|
from PyQt5.QtWidgets import (
|
||||||
from pyqtgraph import QtGui
|
QWidget,
|
||||||
|
QMainWindow,
|
||||||
|
QApplication,
|
||||||
|
)
|
||||||
from PyQt5 import QtCore
|
from PyQt5 import QtCore
|
||||||
# from PyQt5.QtGui import QLabel, QStatusBar
|
|
||||||
from PyQt5.QtCore import (
|
from PyQt5.QtCore import (
|
||||||
pyqtRemoveInputHook,
|
pyqtRemoveInputHook,
|
||||||
Qt,
|
Qt,
|
||||||
|
@ -37,15 +45,19 @@ from PyQt5.QtCore import (
|
||||||
)
|
)
|
||||||
import qdarkstyle
|
import qdarkstyle
|
||||||
from qdarkstyle import DarkPalette
|
from qdarkstyle import DarkPalette
|
||||||
# import qdarkgraystyle
|
# import qdarkgraystyle # TODO: play with it
|
||||||
import trio
|
import trio
|
||||||
from outcome import Error
|
from outcome import Error
|
||||||
|
|
||||||
from .._daemon import maybe_open_pikerd, _tractor_kwargs
|
from .._daemon import (
|
||||||
|
maybe_open_pikerd,
|
||||||
|
get_tractor_runtime_kwargs,
|
||||||
|
)
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
from ._pg_overrides import _do_overrides
|
from ._pg_overrides import _do_overrides
|
||||||
from . import _style
|
from . import _style
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
# pyqtgraph global config
|
# pyqtgraph global config
|
||||||
|
@ -72,17 +84,18 @@ if platform.system() == "Windows":
|
||||||
|
|
||||||
def run_qtractor(
|
def run_qtractor(
|
||||||
func: Callable,
|
func: Callable,
|
||||||
args: Tuple,
|
args: tuple,
|
||||||
main_widget: QtGui.QWidget,
|
main_widget_type: Type[QWidget],
|
||||||
tractor_kwargs: Dict[str, Any] = {},
|
tractor_kwargs: dict[str, Any] = {},
|
||||||
window_type: QtGui.QMainWindow = None,
|
window_type: QMainWindow = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
# avoids annoying message when entering debugger from qt loop
|
# avoids annoying message when entering debugger from qt loop
|
||||||
pyqtRemoveInputHook()
|
pyqtRemoveInputHook()
|
||||||
|
|
||||||
app = QtGui.QApplication.instance()
|
app = QApplication.instance()
|
||||||
if app is None:
|
if app is None:
|
||||||
app = PyQt5.QtWidgets.QApplication([])
|
app = QApplication([])
|
||||||
|
|
||||||
# TODO: we might not need this if it's desired
|
# TODO: we might not need this if it's desired
|
||||||
# to cancel the tractor machinery on Qt loop
|
# to cancel the tractor machinery on Qt loop
|
||||||
|
@ -156,11 +169,11 @@ def run_qtractor(
|
||||||
# hook into app focus change events
|
# hook into app focus change events
|
||||||
app.focusChanged.connect(window.on_focus_change)
|
app.focusChanged.connect(window.on_focus_change)
|
||||||
|
|
||||||
instance = main_widget()
|
instance = main_widget_type()
|
||||||
instance.window = window
|
instance.window = window
|
||||||
|
|
||||||
# override tractor's defaults
|
# override tractor's defaults
|
||||||
tractor_kwargs.update(_tractor_kwargs)
|
tractor_kwargs.update(get_tractor_runtime_kwargs())
|
||||||
|
|
||||||
# define tractor entrypoint
|
# define tractor entrypoint
|
||||||
async def main():
|
async def main():
|
||||||
|
@ -178,7 +191,7 @@ def run_qtractor(
|
||||||
# restrict_keyboard_interrupt_to_checkpoints=True,
|
# restrict_keyboard_interrupt_to_checkpoints=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
window.main_widget = main_widget
|
window.godwidget: GodWidget = instance
|
||||||
window.setCentralWidget(instance)
|
window.setCentralWidget(instance)
|
||||||
if is_windows:
|
if is_windows:
|
||||||
window.configure_to_desktop()
|
window.configure_to_desktop()
|
||||||
|
|
1247
piker/ui/_flows.py
1247
piker/ui/_flows.py
File diff suppressed because it is too large
Load Diff
|
@ -619,7 +619,7 @@ class FillStatusBar(QProgressBar):
|
||||||
# color: #19232D;
|
# color: #19232D;
|
||||||
# width: 10px;
|
# width: 10px;
|
||||||
|
|
||||||
self.setRange(0, slots)
|
self.setRange(0, int(slots))
|
||||||
self.setValue(value)
|
self.setValue(value)
|
||||||
|
|
||||||
|
|
||||||
|
@ -644,7 +644,7 @@ def mk_fill_status_bar(
|
||||||
|
|
||||||
# TODO: calc this height from the ``ChartnPane``
|
# TODO: calc this height from the ``ChartnPane``
|
||||||
chart_h = round(parent_pane.height() * 5/8)
|
chart_h = round(parent_pane.height() * 5/8)
|
||||||
bar_h = chart_h * 0.375
|
bar_h = chart_h * 0.375*0.9
|
||||||
|
|
||||||
# TODO: once things are sized to screen
|
# TODO: once things are sized to screen
|
||||||
bar_label_font_size = label_font_size or _font.px_size - 2
|
bar_label_font_size = label_font_size or _font.px_size - 2
|
||||||
|
|
233
piker/ui/_fsp.py
233
piker/ui/_fsp.py
|
@ -27,12 +27,13 @@ from itertools import cycle
|
||||||
from typing import Optional, AsyncGenerator, Any
|
from typing import Optional, AsyncGenerator, Any
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pydantic import create_model
|
import msgspec
|
||||||
import tractor
|
import tractor
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
|
|
||||||
|
from piker.data.types import Struct
|
||||||
from ._axes import PriceAxis
|
from ._axes import PriceAxis
|
||||||
from .._cacheables import maybe_open_context
|
from .._cacheables import maybe_open_context
|
||||||
from ..calc import humanize
|
from ..calc import humanize
|
||||||
|
@ -41,6 +42,8 @@ from ..data._sharedmem import (
|
||||||
_Token,
|
_Token,
|
||||||
try_read,
|
try_read,
|
||||||
)
|
)
|
||||||
|
from ..data.feed import Flume
|
||||||
|
from ..data._source import Symbol
|
||||||
from ._chart import (
|
from ._chart import (
|
||||||
ChartPlotWidget,
|
ChartPlotWidget,
|
||||||
LinkedSplits,
|
LinkedSplits,
|
||||||
|
@ -50,14 +53,18 @@ from ._forms import (
|
||||||
mk_form,
|
mk_form,
|
||||||
open_form_input_handling,
|
open_form_input_handling,
|
||||||
)
|
)
|
||||||
from ..fsp._api import maybe_mk_fsp_shm, Fsp
|
from ..fsp._api import (
|
||||||
|
maybe_mk_fsp_shm,
|
||||||
|
Fsp,
|
||||||
|
)
|
||||||
from ..fsp import cascade
|
from ..fsp import cascade
|
||||||
from ..fsp._volume import (
|
from ..fsp._volume import (
|
||||||
tina_vwap,
|
# tina_vwap,
|
||||||
dolla_vlm,
|
dolla_vlm,
|
||||||
flow_rates,
|
flow_rates,
|
||||||
)
|
)
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
from .._profile import Profiler
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
@ -72,14 +79,14 @@ def has_vlm(ohlcv: ShmArray) -> bool:
|
||||||
|
|
||||||
def update_fsp_chart(
|
def update_fsp_chart(
|
||||||
chart: ChartPlotWidget,
|
chart: ChartPlotWidget,
|
||||||
flow,
|
viz,
|
||||||
graphics_name: str,
|
graphics_name: str,
|
||||||
array_key: Optional[str],
|
array_key: Optional[str],
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
shm = flow.shm
|
shm = viz.shm
|
||||||
if not shm:
|
if not shm:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -105,7 +112,8 @@ def update_fsp_chart(
|
||||||
# sub-charts reference it under different 'named charts'.
|
# sub-charts reference it under different 'named charts'.
|
||||||
|
|
||||||
# read from last calculated value and update any label
|
# read from last calculated value and update any label
|
||||||
last_val_sticky = chart._ysticks.get(graphics_name)
|
last_val_sticky = chart.plotItem.getAxis(
|
||||||
|
'right')._stickies.get(graphics_name)
|
||||||
if last_val_sticky:
|
if last_val_sticky:
|
||||||
last = last_row[array_key]
|
last = last_row[array_key]
|
||||||
last_val_sticky.update_from_data(-1, last)
|
last_val_sticky.update_from_data(-1, last)
|
||||||
|
@ -153,12 +161,13 @@ async def open_fsp_sidepane(
|
||||||
)
|
)
|
||||||
|
|
||||||
# https://pydantic-docs.helpmanual.io/usage/models/#dynamic-model-creation
|
# https://pydantic-docs.helpmanual.io/usage/models/#dynamic-model-creation
|
||||||
FspConfig = create_model(
|
FspConfig = msgspec.defstruct(
|
||||||
'FspConfig',
|
"Point",
|
||||||
name=name,
|
[('name', name)] + list(params.items()),
|
||||||
**params,
|
bases=(Struct,),
|
||||||
)
|
)
|
||||||
sidepane.model = FspConfig()
|
model = FspConfig(name=name, **params)
|
||||||
|
sidepane.model = model
|
||||||
|
|
||||||
# just a logger for now until we get fsp configs up and running.
|
# just a logger for now until we get fsp configs up and running.
|
||||||
async def settings_change(
|
async def settings_change(
|
||||||
|
@ -188,7 +197,7 @@ async def open_fsp_actor_cluster(
|
||||||
|
|
||||||
from tractor._clustering import open_actor_cluster
|
from tractor._clustering import open_actor_cluster
|
||||||
|
|
||||||
# profiler = pg.debug.Profiler(
|
# profiler = Profiler(
|
||||||
# delayed=False,
|
# delayed=False,
|
||||||
# disabled=False
|
# disabled=False
|
||||||
# )
|
# )
|
||||||
|
@ -205,12 +214,12 @@ async def open_fsp_actor_cluster(
|
||||||
async def run_fsp_ui(
|
async def run_fsp_ui(
|
||||||
|
|
||||||
linkedsplits: LinkedSplits,
|
linkedsplits: LinkedSplits,
|
||||||
shm: ShmArray,
|
flume: Flume,
|
||||||
started: trio.Event,
|
started: trio.Event,
|
||||||
target: Fsp,
|
target: Fsp,
|
||||||
conf: dict[str, dict],
|
conf: dict[str, dict],
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
# profiler: pg.debug.Profiler,
|
# profiler: Profiler,
|
||||||
# _quote_throttle_rate: int = 58,
|
# _quote_throttle_rate: int = 58,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -242,9 +251,11 @@ async def run_fsp_ui(
|
||||||
else:
|
else:
|
||||||
chart = linkedsplits.subplots[overlay_with]
|
chart = linkedsplits.subplots[overlay_with]
|
||||||
|
|
||||||
|
shm = flume.rt_shm
|
||||||
chart.draw_curve(
|
chart.draw_curve(
|
||||||
name=name,
|
name,
|
||||||
shm=shm,
|
shm,
|
||||||
|
flume,
|
||||||
overlay=True,
|
overlay=True,
|
||||||
color='default_light',
|
color='default_light',
|
||||||
array_key=name,
|
array_key=name,
|
||||||
|
@ -254,8 +265,9 @@ async def run_fsp_ui(
|
||||||
else:
|
else:
|
||||||
# create a new sub-chart widget for this fsp
|
# create a new sub-chart widget for this fsp
|
||||||
chart = linkedsplits.add_plot(
|
chart = linkedsplits.add_plot(
|
||||||
name=name,
|
name,
|
||||||
shm=shm,
|
shm,
|
||||||
|
flume,
|
||||||
|
|
||||||
array_key=name,
|
array_key=name,
|
||||||
sidepane=sidepane,
|
sidepane=sidepane,
|
||||||
|
@ -277,7 +289,7 @@ async def run_fsp_ui(
|
||||||
# first UI update, usually from shm pushed history
|
# first UI update, usually from shm pushed history
|
||||||
update_fsp_chart(
|
update_fsp_chart(
|
||||||
chart,
|
chart,
|
||||||
chart._flows[array_key],
|
chart.get_viz(array_key),
|
||||||
name,
|
name,
|
||||||
array_key=array_key,
|
array_key=array_key,
|
||||||
)
|
)
|
||||||
|
@ -345,6 +357,9 @@ async def run_fsp_ui(
|
||||||
# last = time.time()
|
# last = time.time()
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: maybe this should be our ``Viz`` type since it maps
|
||||||
|
# one flume to the next? The machinery for task/actor mgmt should
|
||||||
|
# be part of the instantiation API?
|
||||||
class FspAdmin:
|
class FspAdmin:
|
||||||
'''
|
'''
|
||||||
Client API for orchestrating FSP actors and displaying
|
Client API for orchestrating FSP actors and displaying
|
||||||
|
@ -356,7 +371,7 @@ class FspAdmin:
|
||||||
tn: trio.Nursery,
|
tn: trio.Nursery,
|
||||||
cluster: dict[str, tractor.Portal],
|
cluster: dict[str, tractor.Portal],
|
||||||
linked: LinkedSplits,
|
linked: LinkedSplits,
|
||||||
src_shm: ShmArray,
|
flume: Flume,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
self.tn = tn
|
self.tn = tn
|
||||||
|
@ -368,7 +383,11 @@ class FspAdmin:
|
||||||
tuple[tractor.MsgStream, ShmArray]
|
tuple[tractor.MsgStream, ShmArray]
|
||||||
] = {}
|
] = {}
|
||||||
self._flow_registry: dict[_Token, str] = {}
|
self._flow_registry: dict[_Token, str] = {}
|
||||||
self.src_shm = src_shm
|
|
||||||
|
# TODO: make this a `.src_flume` and add
|
||||||
|
# a `dst_flume`?
|
||||||
|
# (=> but then wouldn't this be the most basic `Viz`?)
|
||||||
|
self.flume = flume
|
||||||
|
|
||||||
def rr_next_portal(self) -> tractor.Portal:
|
def rr_next_portal(self) -> tractor.Portal:
|
||||||
name, portal = next(self._rr_next_actor)
|
name, portal = next(self._rr_next_actor)
|
||||||
|
@ -381,7 +400,7 @@ class FspAdmin:
|
||||||
complete: trio.Event,
|
complete: trio.Event,
|
||||||
started: trio.Event,
|
started: trio.Event,
|
||||||
fqsn: str,
|
fqsn: str,
|
||||||
dst_shm: ShmArray,
|
dst_fsp_flume: Flume,
|
||||||
conf: dict,
|
conf: dict,
|
||||||
target: Fsp,
|
target: Fsp,
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
|
@ -402,9 +421,10 @@ class FspAdmin:
|
||||||
# data feed key
|
# data feed key
|
||||||
fqsn=fqsn,
|
fqsn=fqsn,
|
||||||
|
|
||||||
|
# TODO: pass `Flume.to_msg()`s here?
|
||||||
# mems
|
# mems
|
||||||
src_shm_token=self.src_shm.token,
|
src_shm_token=self.flume.rt_shm.token,
|
||||||
dst_shm_token=dst_shm.token,
|
dst_shm_token=dst_fsp_flume.rt_shm.token,
|
||||||
|
|
||||||
# target
|
# target
|
||||||
ns_path=ns_path,
|
ns_path=ns_path,
|
||||||
|
@ -421,12 +441,14 @@ class FspAdmin:
|
||||||
ctx.open_stream() as stream,
|
ctx.open_stream() as stream,
|
||||||
):
|
):
|
||||||
|
|
||||||
|
dst_fsp_flume.stream: tractor.MsgStream = stream
|
||||||
|
|
||||||
# register output data
|
# register output data
|
||||||
self._registry[
|
self._registry[
|
||||||
(fqsn, ns_path)
|
(fqsn, ns_path)
|
||||||
] = (
|
] = (
|
||||||
stream,
|
stream,
|
||||||
dst_shm,
|
dst_fsp_flume.rt_shm,
|
||||||
complete
|
complete
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -440,7 +462,9 @@ class FspAdmin:
|
||||||
# if the chart isn't hidden try to update
|
# if the chart isn't hidden try to update
|
||||||
# the data on screen.
|
# the data on screen.
|
||||||
if not self.linked.isHidden():
|
if not self.linked.isHidden():
|
||||||
log.debug(f'Re-syncing graphics for fsp: {ns_path}')
|
log.debug(
|
||||||
|
f'Re-syncing graphics for fsp: {ns_path}'
|
||||||
|
)
|
||||||
self.linked.graphics_cycle(
|
self.linked.graphics_cycle(
|
||||||
trigger_all=True,
|
trigger_all=True,
|
||||||
prepend_update_index=info['first'],
|
prepend_update_index=info['first'],
|
||||||
|
@ -459,9 +483,9 @@ class FspAdmin:
|
||||||
worker_name: Optional[str] = None,
|
worker_name: Optional[str] = None,
|
||||||
loglevel: str = 'info',
|
loglevel: str = 'info',
|
||||||
|
|
||||||
) -> (ShmArray, trio.Event):
|
) -> (Flume, trio.Event):
|
||||||
|
|
||||||
fqsn = self.linked.symbol.front_fqsn()
|
fqsn = self.flume.symbol.fqsn
|
||||||
|
|
||||||
# allocate an output shm array
|
# allocate an output shm array
|
||||||
key, dst_shm, opened = maybe_mk_fsp_shm(
|
key, dst_shm, opened = maybe_mk_fsp_shm(
|
||||||
|
@ -469,16 +493,36 @@ class FspAdmin:
|
||||||
target=target,
|
target=target,
|
||||||
readonly=True,
|
readonly=True,
|
||||||
)
|
)
|
||||||
self._flow_registry[
|
|
||||||
(self.src_shm._token, target.name)
|
portal = self.cluster.get(worker_name) or self.rr_next_portal()
|
||||||
] = dst_shm._token
|
provider_tag = portal.channel.uid
|
||||||
|
|
||||||
|
symbol = Symbol(
|
||||||
|
key=key,
|
||||||
|
broker_info={
|
||||||
|
provider_tag: {'asset_type': 'fsp'},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
dst_fsp_flume = Flume(
|
||||||
|
symbol=symbol,
|
||||||
|
_rt_shm_token=dst_shm.token,
|
||||||
|
first_quote={},
|
||||||
|
|
||||||
|
# set to 0 presuming for now that we can't load
|
||||||
|
# FSP history (though we should eventually).
|
||||||
|
izero_hist=0,
|
||||||
|
izero_rt=0,
|
||||||
|
)
|
||||||
|
self._flow_registry[(
|
||||||
|
self.flume.rt_shm._token,
|
||||||
|
target.name
|
||||||
|
)] = dst_shm._token
|
||||||
|
|
||||||
# if not opened:
|
# if not opened:
|
||||||
# raise RuntimeError(
|
# raise RuntimeError(
|
||||||
# f'Already started FSP `{fqsn}:{func_name}`'
|
# f'Already started FSP `{fqsn}:{func_name}`'
|
||||||
# )
|
# )
|
||||||
|
|
||||||
portal = self.cluster.get(worker_name) or self.rr_next_portal()
|
|
||||||
complete = trio.Event()
|
complete = trio.Event()
|
||||||
started = trio.Event()
|
started = trio.Event()
|
||||||
self.tn.start_soon(
|
self.tn.start_soon(
|
||||||
|
@ -487,13 +531,13 @@ class FspAdmin:
|
||||||
complete,
|
complete,
|
||||||
started,
|
started,
|
||||||
fqsn,
|
fqsn,
|
||||||
dst_shm,
|
dst_fsp_flume,
|
||||||
conf,
|
conf,
|
||||||
target,
|
target,
|
||||||
loglevel,
|
loglevel,
|
||||||
)
|
)
|
||||||
|
|
||||||
return dst_shm, started
|
return dst_fsp_flume, started
|
||||||
|
|
||||||
async def open_fsp_chart(
|
async def open_fsp_chart(
|
||||||
self,
|
self,
|
||||||
|
@ -505,7 +549,7 @@ class FspAdmin:
|
||||||
|
|
||||||
) -> (trio.Event, ChartPlotWidget):
|
) -> (trio.Event, ChartPlotWidget):
|
||||||
|
|
||||||
shm, started = await self.start_engine_task(
|
flume, started = await self.start_engine_task(
|
||||||
target,
|
target,
|
||||||
conf,
|
conf,
|
||||||
loglevel,
|
loglevel,
|
||||||
|
@ -517,7 +561,7 @@ class FspAdmin:
|
||||||
run_fsp_ui,
|
run_fsp_ui,
|
||||||
|
|
||||||
self.linked,
|
self.linked,
|
||||||
shm,
|
flume,
|
||||||
started,
|
started,
|
||||||
target,
|
target,
|
||||||
|
|
||||||
|
@ -531,7 +575,7 @@ class FspAdmin:
|
||||||
@acm
|
@acm
|
||||||
async def open_fsp_admin(
|
async def open_fsp_admin(
|
||||||
linked: LinkedSplits,
|
linked: LinkedSplits,
|
||||||
src_shm: ShmArray,
|
flume: Flume,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> AsyncGenerator[dict, dict[str, tractor.Portal]]:
|
) -> AsyncGenerator[dict, dict[str, tractor.Portal]]:
|
||||||
|
@ -552,7 +596,7 @@ async def open_fsp_admin(
|
||||||
tn,
|
tn,
|
||||||
cluster_map,
|
cluster_map,
|
||||||
linked,
|
linked,
|
||||||
src_shm,
|
flume,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
yield admin
|
yield admin
|
||||||
|
@ -566,7 +610,7 @@ async def open_fsp_admin(
|
||||||
async def open_vlm_displays(
|
async def open_vlm_displays(
|
||||||
|
|
||||||
linked: LinkedSplits,
|
linked: LinkedSplits,
|
||||||
ohlcv: ShmArray,
|
flume: Flume,
|
||||||
dvlm: bool = True,
|
dvlm: bool = True,
|
||||||
|
|
||||||
task_status: TaskStatus[ChartPlotWidget] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus[ChartPlotWidget] = trio.TASK_STATUS_IGNORED,
|
||||||
|
@ -588,6 +632,8 @@ async def open_vlm_displays(
|
||||||
sig = inspect.signature(flow_rates.func)
|
sig = inspect.signature(flow_rates.func)
|
||||||
params = sig.parameters
|
params = sig.parameters
|
||||||
|
|
||||||
|
ohlcv: ShmArray = flume.rt_shm
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
open_fsp_sidepane(
|
open_fsp_sidepane(
|
||||||
linked, {
|
linked, {
|
||||||
|
@ -607,7 +653,7 @@ async def open_vlm_displays(
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
) as sidepane,
|
) as sidepane,
|
||||||
open_fsp_admin(linked, ohlcv) as admin,
|
open_fsp_admin(linked, flume) as admin,
|
||||||
):
|
):
|
||||||
# TODO: support updates
|
# TODO: support updates
|
||||||
# period_field = sidepane.fields['period']
|
# period_field = sidepane.fields['period']
|
||||||
|
@ -618,9 +664,12 @@ async def open_vlm_displays(
|
||||||
# built-in vlm which we plot ASAP since it's
|
# built-in vlm which we plot ASAP since it's
|
||||||
# usually data provided directly with OHLC history.
|
# usually data provided directly with OHLC history.
|
||||||
shm = ohlcv
|
shm = ohlcv
|
||||||
chart = linked.add_plot(
|
ohlc_chart = linked.chart
|
||||||
|
|
||||||
|
vlm_chart = linked.add_plot(
|
||||||
name='volume',
|
name='volume',
|
||||||
shm=shm,
|
shm=shm,
|
||||||
|
flume=flume,
|
||||||
|
|
||||||
array_key='volume',
|
array_key='volume',
|
||||||
sidepane=sidepane,
|
sidepane=sidepane,
|
||||||
|
@ -633,63 +682,75 @@ async def open_vlm_displays(
|
||||||
# the curve item internals are pretty convoluted.
|
# the curve item internals are pretty convoluted.
|
||||||
style='step',
|
style='step',
|
||||||
)
|
)
|
||||||
|
vlm_chart.view.enable_auto_yrange()
|
||||||
|
|
||||||
|
# back-link the volume chart to trigger y-autoranging
|
||||||
|
# in the ohlc (parent) chart.
|
||||||
|
ohlc_chart.view.enable_auto_yrange(
|
||||||
|
src_vb=vlm_chart.view,
|
||||||
|
)
|
||||||
|
|
||||||
# force 0 to always be in view
|
# force 0 to always be in view
|
||||||
def multi_maxmin(
|
def multi_maxmin(
|
||||||
names: list[str],
|
names: list[str],
|
||||||
|
|
||||||
) -> tuple[float, float]:
|
) -> tuple[float, float]:
|
||||||
|
'''
|
||||||
|
Viz "group" maxmin loop; assumes all named flows
|
||||||
|
are in the same co-domain and thus can be sorted
|
||||||
|
as one set.
|
||||||
|
|
||||||
|
Iterates all the named flows and calls the chart
|
||||||
|
api to find their range values and return.
|
||||||
|
|
||||||
|
TODO: really we should probably have a more built-in API
|
||||||
|
for this?
|
||||||
|
|
||||||
|
'''
|
||||||
mx = 0
|
mx = 0
|
||||||
for name in names:
|
for name in names:
|
||||||
|
ymn, ymx = vlm_chart.maxmin(name=name)
|
||||||
mxmn = chart.maxmin(name=name)
|
mx = max(mx, ymx)
|
||||||
if mxmn:
|
|
||||||
ymax = mxmn[1]
|
|
||||||
if ymax > mx:
|
|
||||||
mx = ymax
|
|
||||||
|
|
||||||
return 0, mx
|
return 0, mx
|
||||||
|
|
||||||
chart.view.maxmin = partial(multi_maxmin, names=['volume'])
|
|
||||||
|
|
||||||
# TODO: fix the x-axis label issue where if you put
|
# TODO: fix the x-axis label issue where if you put
|
||||||
# the axis on the left it's totally not lined up...
|
# the axis on the left it's totally not lined up...
|
||||||
# show volume units value on LHS (for dinkus)
|
# show volume units value on LHS (for dinkus)
|
||||||
# chart.hideAxis('right')
|
# vlm_chart.hideAxis('right')
|
||||||
# chart.showAxis('left')
|
# vlm_chart.showAxis('left')
|
||||||
|
|
||||||
# send back new chart to caller
|
# send back new chart to caller
|
||||||
task_status.started(chart)
|
task_status.started(vlm_chart)
|
||||||
|
|
||||||
# should **not** be the same sub-chart widget
|
# should **not** be the same sub-chart widget
|
||||||
assert chart.name != linked.chart.name
|
assert vlm_chart.name != linked.chart.name
|
||||||
|
|
||||||
# sticky only on sub-charts atm
|
# sticky only on sub-charts atm
|
||||||
last_val_sticky = chart._ysticks[chart.name]
|
last_val_sticky = vlm_chart.plotItem.getAxis(
|
||||||
|
'right')._stickies.get(vlm_chart.name)
|
||||||
|
|
||||||
# read from last calculated value
|
# read from last calculated value
|
||||||
value = shm.array['volume'][-1]
|
value = shm.array['volume'][-1]
|
||||||
|
|
||||||
last_val_sticky.update_from_data(-1, value)
|
last_val_sticky.update_from_data(-1, value)
|
||||||
|
|
||||||
vlm_curve = chart.update_graphics_from_flow(
|
vlm_curve = vlm_chart.update_graphics_from_flow(
|
||||||
'volume',
|
'volume',
|
||||||
# shm.array,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# size view to data once at outset
|
# size view to data once at outset
|
||||||
chart.view._set_yrange()
|
vlm_chart.view._set_yrange()
|
||||||
|
|
||||||
# add axis title
|
# add axis title
|
||||||
axis = chart.getAxis('right')
|
axis = vlm_chart.getAxis('right')
|
||||||
axis.set_title(' vlm')
|
axis.set_title(' vlm')
|
||||||
|
|
||||||
if dvlm:
|
if dvlm:
|
||||||
|
|
||||||
tasks_ready = []
|
tasks_ready = []
|
||||||
# spawn and overlay $ vlm on the same subchart
|
# spawn and overlay $ vlm on the same subchart
|
||||||
dvlm_shm, started = await admin.start_engine_task(
|
dvlm_flume, started = await admin.start_engine_task(
|
||||||
dolla_vlm,
|
dolla_vlm,
|
||||||
|
|
||||||
{ # fsp engine conf
|
{ # fsp engine conf
|
||||||
|
@ -722,7 +783,7 @@ async def open_vlm_displays(
|
||||||
# XXX: the main chart already contains a vlm "units" axis
|
# XXX: the main chart already contains a vlm "units" axis
|
||||||
# so here we add an overlay wth a y-range in
|
# so here we add an overlay wth a y-range in
|
||||||
# $ liquidity-value units (normally a fiat like USD).
|
# $ liquidity-value units (normally a fiat like USD).
|
||||||
dvlm_pi = chart.overlay_plotitem(
|
dvlm_pi = vlm_chart.overlay_plotitem(
|
||||||
'dolla_vlm',
|
'dolla_vlm',
|
||||||
index=0, # place axis on inside (nearest to chart)
|
index=0, # place axis on inside (nearest to chart)
|
||||||
axis_title=' $vlm',
|
axis_title=' $vlm',
|
||||||
|
@ -736,6 +797,8 @@ async def open_vlm_displays(
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
dvlm_pi.hideAxis('left')
|
||||||
|
dvlm_pi.hideAxis('bottom')
|
||||||
# all to be overlayed curve names
|
# all to be overlayed curve names
|
||||||
fields = [
|
fields = [
|
||||||
'dolla_vlm',
|
'dolla_vlm',
|
||||||
|
@ -771,11 +834,13 @@ async def open_vlm_displays(
|
||||||
names: list[str],
|
names: list[str],
|
||||||
pi: pg.PlotItem,
|
pi: pg.PlotItem,
|
||||||
shm: ShmArray,
|
shm: ShmArray,
|
||||||
|
flume: Flume,
|
||||||
step_mode: bool = False,
|
step_mode: bool = False,
|
||||||
style: str = 'solid',
|
style: str = 'solid',
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
for name in names:
|
for name in names:
|
||||||
|
|
||||||
if 'dark' in name:
|
if 'dark' in name:
|
||||||
color = dark_vlm_color
|
color = dark_vlm_color
|
||||||
elif 'rate' in name:
|
elif 'rate' in name:
|
||||||
|
@ -783,9 +848,13 @@ async def open_vlm_displays(
|
||||||
else:
|
else:
|
||||||
color = 'bracket'
|
color = 'bracket'
|
||||||
|
|
||||||
curve, _ = chart.draw_curve(
|
assert isinstance(shm, ShmArray)
|
||||||
name=name,
|
assert isinstance(flume, Flume)
|
||||||
shm=shm,
|
|
||||||
|
viz = vlm_chart.draw_curve(
|
||||||
|
name,
|
||||||
|
shm,
|
||||||
|
flume,
|
||||||
array_key=name,
|
array_key=name,
|
||||||
overlay=pi,
|
overlay=pi,
|
||||||
color=color,
|
color=color,
|
||||||
|
@ -793,25 +862,20 @@ async def open_vlm_displays(
|
||||||
style=style,
|
style=style,
|
||||||
pi=pi,
|
pi=pi,
|
||||||
)
|
)
|
||||||
|
assert viz.plot is pi
|
||||||
# TODO: we need a better API to do this..
|
|
||||||
# specially store ref to shm for lookup in display loop
|
|
||||||
# since only a placeholder of `None` is entered in
|
|
||||||
# ``.draw_curve()``.
|
|
||||||
flow = chart._flows[name]
|
|
||||||
assert flow.plot is pi
|
|
||||||
|
|
||||||
chart_curves(
|
chart_curves(
|
||||||
fields,
|
fields,
|
||||||
dvlm_pi,
|
dvlm_pi,
|
||||||
dvlm_shm,
|
dvlm_flume.rt_shm,
|
||||||
|
dvlm_flume,
|
||||||
step_mode=True,
|
step_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# spawn flow rates fsp **ONLY AFTER** the 'dolla_vlm' fsp is
|
# spawn flow rates fsp **ONLY AFTER** the 'dolla_vlm' fsp is
|
||||||
# up since this one depends on it.
|
# up since this one depends on it.
|
||||||
|
|
||||||
fr_shm, started = await admin.start_engine_task(
|
fr_flume, started = await admin.start_engine_task(
|
||||||
flow_rates,
|
flow_rates,
|
||||||
{ # fsp engine conf
|
{ # fsp engine conf
|
||||||
'func_name': 'flow_rates',
|
'func_name': 'flow_rates',
|
||||||
|
@ -824,7 +888,7 @@ async def open_vlm_displays(
|
||||||
# chart_curves(
|
# chart_curves(
|
||||||
# dvlm_rate_fields,
|
# dvlm_rate_fields,
|
||||||
# dvlm_pi,
|
# dvlm_pi,
|
||||||
# fr_shm,
|
# fr_flume.rt_shm,
|
||||||
# )
|
# )
|
||||||
|
|
||||||
# TODO: is there a way to "sync" the dual axes such that only
|
# TODO: is there a way to "sync" the dual axes such that only
|
||||||
|
@ -833,17 +897,17 @@ async def open_vlm_displays(
|
||||||
# displayed and the curves are effectively the same minus
|
# displayed and the curves are effectively the same minus
|
||||||
# liquidity events (well at least on low OHLC periods - 1s).
|
# liquidity events (well at least on low OHLC periods - 1s).
|
||||||
vlm_curve.hide()
|
vlm_curve.hide()
|
||||||
chart.removeItem(vlm_curve)
|
vlm_chart.removeItem(vlm_curve)
|
||||||
vflow = chart._flows['volume']
|
vlm_viz = vlm_chart._vizs['volume']
|
||||||
vflow.render = False
|
vlm_viz.render = False
|
||||||
|
|
||||||
# avoid range sorting on volume once disabled
|
# avoid range sorting on volume once disabled
|
||||||
chart.view.disable_auto_yrange()
|
vlm_chart.view.disable_auto_yrange()
|
||||||
|
|
||||||
# Trade rate overlay
|
# Trade rate overlay
|
||||||
# XXX: requires an additional overlay for
|
# XXX: requires an additional overlay for
|
||||||
# a trades-per-period (time) y-range.
|
# a trades-per-period (time) y-range.
|
||||||
tr_pi = chart.overlay_plotitem(
|
tr_pi = vlm_chart.overlay_plotitem(
|
||||||
'trade_rates',
|
'trade_rates',
|
||||||
|
|
||||||
# TODO: dynamically update period (and thus this axis?)
|
# TODO: dynamically update period (and thus this axis?)
|
||||||
|
@ -867,11 +931,13 @@ async def open_vlm_displays(
|
||||||
# keep both regular and dark vlm in view
|
# keep both regular and dark vlm in view
|
||||||
names=trade_rate_fields,
|
names=trade_rate_fields,
|
||||||
)
|
)
|
||||||
|
tr_pi.hideAxis('bottom')
|
||||||
|
|
||||||
chart_curves(
|
chart_curves(
|
||||||
trade_rate_fields,
|
trade_rate_fields,
|
||||||
tr_pi,
|
tr_pi,
|
||||||
fr_shm,
|
fr_flume.rt_shm,
|
||||||
|
fr_flume,
|
||||||
# step_mode=True,
|
# step_mode=True,
|
||||||
|
|
||||||
# dashed line to represent "individual trades" being
|
# dashed line to represent "individual trades" being
|
||||||
|
@ -905,7 +971,7 @@ async def open_vlm_displays(
|
||||||
async def start_fsp_displays(
|
async def start_fsp_displays(
|
||||||
|
|
||||||
linked: LinkedSplits,
|
linked: LinkedSplits,
|
||||||
ohlcv: ShmArray,
|
flume: Flume,
|
||||||
group_status_key: str,
|
group_status_key: str,
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
|
|
||||||
|
@ -940,7 +1006,7 @@ async def start_fsp_displays(
|
||||||
# },
|
# },
|
||||||
# },
|
# },
|
||||||
}
|
}
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
delayed=False,
|
delayed=False,
|
||||||
disabled=False
|
disabled=False
|
||||||
)
|
)
|
||||||
|
@ -948,7 +1014,10 @@ async def start_fsp_displays(
|
||||||
async with (
|
async with (
|
||||||
|
|
||||||
# NOTE: this admin internally opens an actor cluster
|
# NOTE: this admin internally opens an actor cluster
|
||||||
open_fsp_admin(linked, ohlcv) as admin,
|
open_fsp_admin(
|
||||||
|
linked,
|
||||||
|
flume,
|
||||||
|
) as admin,
|
||||||
):
|
):
|
||||||
statuses = []
|
statuses = []
|
||||||
for target, conf in fsp_conf.items():
|
for target, conf in fsp_conf.items():
|
||||||
|
|
|
@ -33,6 +33,7 @@ import numpy as np
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
from .._profile import Profiler
|
||||||
from .._profile import pg_profile_enabled, ms_slower_then
|
from .._profile import pg_profile_enabled, ms_slower_then
|
||||||
# from ._style import _min_points_to_show
|
# from ._style import _min_points_to_show
|
||||||
from ._editors import SelectRect
|
from ._editors import SelectRect
|
||||||
|
@ -75,7 +76,6 @@ async def handle_viewmode_kb_inputs(
|
||||||
pressed: set[str] = set()
|
pressed: set[str] = set()
|
||||||
|
|
||||||
last = time.time()
|
last = time.time()
|
||||||
trigger_mode: str
|
|
||||||
action: str
|
action: str
|
||||||
|
|
||||||
on_next_release: Optional[Callable] = None
|
on_next_release: Optional[Callable] = None
|
||||||
|
@ -141,13 +141,16 @@ async def handle_viewmode_kb_inputs(
|
||||||
Qt.Key_Space,
|
Qt.Key_Space,
|
||||||
}
|
}
|
||||||
):
|
):
|
||||||
view._chart.linked.godwidget.search.focus()
|
godw = view._chart.linked.godwidget
|
||||||
|
godw.hist_linked.resize_sidepanes(from_linked=godw.rt_linked)
|
||||||
|
godw.search.focus()
|
||||||
|
|
||||||
# esc and ctrl-c
|
# esc and ctrl-c
|
||||||
if key == Qt.Key_Escape or (ctrl and key == Qt.Key_C):
|
if key == Qt.Key_Escape or (ctrl and key == Qt.Key_C):
|
||||||
# ctrl-c as cancel
|
# ctrl-c as cancel
|
||||||
# https://forum.qt.io/topic/532/how-to-catch-ctrl-c-on-a-widget/9
|
# https://forum.qt.io/topic/532/how-to-catch-ctrl-c-on-a-widget/9
|
||||||
view.select_box.clear()
|
view.select_box.clear()
|
||||||
|
view.linked.focus()
|
||||||
|
|
||||||
# cancel order or clear graphics
|
# cancel order or clear graphics
|
||||||
if key == Qt.Key_C or key == Qt.Key_Delete:
|
if key == Qt.Key_C or key == Qt.Key_Delete:
|
||||||
|
@ -178,17 +181,17 @@ async def handle_viewmode_kb_inputs(
|
||||||
if key in pressed:
|
if key in pressed:
|
||||||
pressed.remove(key)
|
pressed.remove(key)
|
||||||
|
|
||||||
# QUERY/QUOTE MODE #
|
# QUERY/QUOTE MODE
|
||||||
|
# ----------------
|
||||||
if {Qt.Key_Q}.intersection(pressed):
|
if {Qt.Key_Q}.intersection(pressed):
|
||||||
|
|
||||||
view.linkedsplits.cursor.in_query_mode = True
|
view.linked.cursor.in_query_mode = True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
view.linkedsplits.cursor.in_query_mode = False
|
view.linked.cursor.in_query_mode = False
|
||||||
|
|
||||||
# SELECTION MODE
|
# SELECTION MODE
|
||||||
# --------------
|
# --------------
|
||||||
|
|
||||||
if shift:
|
if shift:
|
||||||
if view.state['mouseMode'] == ViewBox.PanMode:
|
if view.state['mouseMode'] == ViewBox.PanMode:
|
||||||
view.setMouseMode(ViewBox.RectMode)
|
view.setMouseMode(ViewBox.RectMode)
|
||||||
|
@ -209,18 +212,27 @@ async def handle_viewmode_kb_inputs(
|
||||||
|
|
||||||
# ORDER MODE
|
# ORDER MODE
|
||||||
# ----------
|
# ----------
|
||||||
|
|
||||||
# live vs. dark trigger + an action {buy, sell, alert}
|
# live vs. dark trigger + an action {buy, sell, alert}
|
||||||
order_keys_pressed = ORDER_MODE.intersection(pressed)
|
order_keys_pressed = ORDER_MODE.intersection(pressed)
|
||||||
|
|
||||||
if order_keys_pressed:
|
if order_keys_pressed:
|
||||||
|
|
||||||
# show the pp size label
|
# TODO: it seems like maybe the composition should be
|
||||||
order_mode.current_pp.show()
|
# reversed here? Like, maybe we should have the nav have
|
||||||
|
# access to the pos state and then make encapsulated logic
|
||||||
|
# that shows the right stuff on screen instead or order mode
|
||||||
|
# and position-related abstractions doing this?
|
||||||
|
|
||||||
|
# show the pp size label only if there is
|
||||||
|
# a non-zero pos existing
|
||||||
|
tracker = order_mode.current_pp
|
||||||
|
if tracker.live_pp.size:
|
||||||
|
tracker.nav.show()
|
||||||
|
|
||||||
# TODO: show pp config mini-params in status bar widget
|
# TODO: show pp config mini-params in status bar widget
|
||||||
# mode.pp_config.show()
|
# mode.pp_config.show()
|
||||||
|
|
||||||
|
trigger_type: str = 'dark'
|
||||||
if (
|
if (
|
||||||
# 's' for "submit" to activate "live" order
|
# 's' for "submit" to activate "live" order
|
||||||
Qt.Key_S in pressed or
|
Qt.Key_S in pressed or
|
||||||
|
@ -228,9 +240,6 @@ async def handle_viewmode_kb_inputs(
|
||||||
):
|
):
|
||||||
trigger_type: str = 'live'
|
trigger_type: str = 'live'
|
||||||
|
|
||||||
else:
|
|
||||||
trigger_type: str = 'dark'
|
|
||||||
|
|
||||||
# order mode trigger "actions"
|
# order mode trigger "actions"
|
||||||
if Qt.Key_D in pressed: # for "damp eet"
|
if Qt.Key_D in pressed: # for "damp eet"
|
||||||
action = 'sell'
|
action = 'sell'
|
||||||
|
@ -259,8 +268,8 @@ async def handle_viewmode_kb_inputs(
|
||||||
Qt.Key_S in pressed or
|
Qt.Key_S in pressed or
|
||||||
order_keys_pressed or
|
order_keys_pressed or
|
||||||
Qt.Key_O in pressed
|
Qt.Key_O in pressed
|
||||||
) and
|
)
|
||||||
key in NUMBER_LINE
|
and key in NUMBER_LINE
|
||||||
):
|
):
|
||||||
# hot key to set order slots size.
|
# hot key to set order slots size.
|
||||||
# change edit field to current number line value,
|
# change edit field to current number line value,
|
||||||
|
@ -278,7 +287,7 @@ async def handle_viewmode_kb_inputs(
|
||||||
else: # none active
|
else: # none active
|
||||||
|
|
||||||
# hide pp label
|
# hide pp label
|
||||||
order_mode.current_pp.hide_info()
|
order_mode.current_pp.nav.hide_info()
|
||||||
|
|
||||||
# if none are pressed, remove "staged" level
|
# if none are pressed, remove "staged" level
|
||||||
# line under cursor position
|
# line under cursor position
|
||||||
|
@ -319,7 +328,6 @@ async def handle_viewmode_mouse(
|
||||||
):
|
):
|
||||||
# when in order mode, submit execution
|
# when in order mode, submit execution
|
||||||
# msg.event.accept()
|
# msg.event.accept()
|
||||||
# breakpoint()
|
|
||||||
view.order_mode.submit_order()
|
view.order_mode.submit_order()
|
||||||
|
|
||||||
|
|
||||||
|
@ -336,16 +344,6 @@ class ChartView(ViewBox):
|
||||||
'''
|
'''
|
||||||
mode_name: str = 'view'
|
mode_name: str = 'view'
|
||||||
|
|
||||||
# "relay events" for making overlaid views work.
|
|
||||||
# NOTE: these MUST be defined here (and can't be monkey patched
|
|
||||||
# on later) due to signal construction requiring refs to be
|
|
||||||
# in place during the run of meta-class machinery.
|
|
||||||
mouseDragEventRelay = QtCore.Signal(object, object, object)
|
|
||||||
wheelEventRelay = QtCore.Signal(object, object, object)
|
|
||||||
|
|
||||||
event_relay_source: 'Optional[ViewBox]' = None
|
|
||||||
relays: dict[str, QtCore.Signal] = {}
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
|
@ -375,7 +373,7 @@ class ChartView(ViewBox):
|
||||||
y=True,
|
y=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.linkedsplits = None
|
self.linked = None
|
||||||
self._chart: 'ChartPlotWidget' = None # noqa
|
self._chart: 'ChartPlotWidget' = None # noqa
|
||||||
|
|
||||||
# add our selection box annotator
|
# add our selection box annotator
|
||||||
|
@ -397,8 +395,11 @@ class ChartView(ViewBox):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if self._ic is None:
|
if self._ic is None:
|
||||||
|
try:
|
||||||
self.chart.pause_all_feeds()
|
self.chart.pause_all_feeds()
|
||||||
self._ic = trio.Event()
|
self._ic = trio.Event()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
def signal_ic(
|
def signal_ic(
|
||||||
self,
|
self,
|
||||||
|
@ -411,9 +412,12 @@ class ChartView(ViewBox):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if self._ic:
|
if self._ic:
|
||||||
|
try:
|
||||||
self._ic.set()
|
self._ic.set()
|
||||||
self._ic = None
|
self._ic = None
|
||||||
self.chart.resume_all_feeds()
|
self.chart.resume_all_feeds()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def open_async_input_handler(
|
async def open_async_input_handler(
|
||||||
|
@ -463,7 +467,7 @@ class ChartView(ViewBox):
|
||||||
self,
|
self,
|
||||||
ev,
|
ev,
|
||||||
axis=None,
|
axis=None,
|
||||||
relayed_from: ChartView = None,
|
# relayed_from: ChartView = None,
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Override "center-point" location for scrolling.
|
Override "center-point" location for scrolling.
|
||||||
|
@ -474,16 +478,23 @@ class ChartView(ViewBox):
|
||||||
TODO: PR a method into ``pyqtgraph`` to make this configurable
|
TODO: PR a method into ``pyqtgraph`` to make this configurable
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
linked = self.linked
|
||||||
|
if (
|
||||||
|
not linked
|
||||||
|
):
|
||||||
|
# print(f'{self.name} not linked but relay from {relayed_from.name}')
|
||||||
|
return
|
||||||
|
|
||||||
if axis in (0, 1):
|
if axis in (0, 1):
|
||||||
mask = [False, False]
|
mask = [False, False]
|
||||||
mask[axis] = self.state['mouseEnabled'][axis]
|
mask[axis] = self.state['mouseEnabled'][axis]
|
||||||
else:
|
else:
|
||||||
mask = self.state['mouseEnabled'][:]
|
mask = self.state['mouseEnabled'][:]
|
||||||
|
|
||||||
chart = self.linkedsplits.chart
|
chart = self.linked.chart
|
||||||
|
|
||||||
# don't zoom more then the min points setting
|
# don't zoom more then the min points setting
|
||||||
l, lbar, rbar, r = chart.bars_range()
|
out = l, lbar, rbar, r = chart.get_viz(chart.name).bars_range()
|
||||||
# vl = r - l
|
# vl = r - l
|
||||||
|
|
||||||
# if ev.delta() > 0 and vl <= _min_points_to_show:
|
# if ev.delta() > 0 and vl <= _min_points_to_show:
|
||||||
|
@ -492,7 +503,7 @@ class ChartView(ViewBox):
|
||||||
|
|
||||||
# if (
|
# if (
|
||||||
# ev.delta() < 0
|
# ev.delta() < 0
|
||||||
# and vl >= len(chart._flows[chart.name].shm.array) + 666
|
# and vl >= len(chart._vizs[chart.name].shm.array) + 666
|
||||||
# ):
|
# ):
|
||||||
# log.debug("Min zoom bruh...")
|
# log.debug("Min zoom bruh...")
|
||||||
# return
|
# return
|
||||||
|
@ -593,9 +604,20 @@ class ChartView(ViewBox):
|
||||||
self,
|
self,
|
||||||
ev,
|
ev,
|
||||||
axis: Optional[int] = None,
|
axis: Optional[int] = None,
|
||||||
relayed_from: ChartView = None,
|
# relayed_from: ChartView = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
# if relayed_from:
|
||||||
|
# print(f'PAN: {self.name} -> RELAYED FROM: {relayed_from.name}')
|
||||||
|
|
||||||
|
# NOTE since in the overlay case axes are already
|
||||||
|
# "linked" any x-range change will already be mirrored
|
||||||
|
# in all overlaid ``PlotItems``, so we need to simply
|
||||||
|
# ignore the signal here since otherwise we get N-calls
|
||||||
|
# from N-overlays resulting in an "accelerated" feeling
|
||||||
|
# panning motion instead of the expect linear shift.
|
||||||
|
# if relayed_from:
|
||||||
|
# return
|
||||||
|
|
||||||
pos = ev.pos()
|
pos = ev.pos()
|
||||||
lastPos = ev.lastPos()
|
lastPos = ev.lastPos()
|
||||||
|
@ -669,7 +691,10 @@ class ChartView(ViewBox):
|
||||||
# XXX: WHY
|
# XXX: WHY
|
||||||
ev.accept()
|
ev.accept()
|
||||||
|
|
||||||
|
try:
|
||||||
self.start_ic()
|
self.start_ic()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
# if self._ic is None:
|
# if self._ic is None:
|
||||||
# self.chart.pause_all_feeds()
|
# self.chart.pause_all_feeds()
|
||||||
# self._ic = trio.Event()
|
# self._ic = trio.Event()
|
||||||
|
@ -761,7 +786,7 @@ class ChartView(ViewBox):
|
||||||
'''
|
'''
|
||||||
name = self.name
|
name = self.name
|
||||||
# print(f'YRANGE ON {name}')
|
# print(f'YRANGE ON {name}')
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
msg=f'`ChartView._set_yrange()`: `{name}`',
|
msg=f'`ChartView._set_yrange()`: `{name}`',
|
||||||
disabled=not pg_profile_enabled(),
|
disabled=not pg_profile_enabled(),
|
||||||
ms_threshold=ms_slower_then,
|
ms_threshold=ms_slower_then,
|
||||||
|
@ -795,7 +820,7 @@ class ChartView(ViewBox):
|
||||||
# XXX: only compute the mxmn range
|
# XXX: only compute the mxmn range
|
||||||
# if none is provided as input!
|
# if none is provided as input!
|
||||||
if not yrange:
|
if not yrange:
|
||||||
# flow = chart._flows[name]
|
# flow = chart._vizs[name]
|
||||||
yrange = self._maxmin()
|
yrange = self._maxmin()
|
||||||
|
|
||||||
if yrange is None:
|
if yrange is None:
|
||||||
|
@ -830,29 +855,33 @@ class ChartView(ViewBox):
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Assign callback for rescaling y-axis automatically
|
Assign callbacks for rescaling and resampling y-axis data
|
||||||
based on data contents and ``ViewBox`` state.
|
automatically based on data contents and ``ViewBox`` state.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if src_vb is None:
|
if src_vb is None:
|
||||||
src_vb = self
|
src_vb = self
|
||||||
|
|
||||||
# splitter(s) resizing
|
# widget-UIs/splitter(s) resizing
|
||||||
src_vb.sigResized.connect(self._set_yrange)
|
src_vb.sigResized.connect(self._set_yrange)
|
||||||
|
|
||||||
|
# re-sampling trigger:
|
||||||
# TODO: a smarter way to avoid calling this needlessly?
|
# TODO: a smarter way to avoid calling this needlessly?
|
||||||
# 2 things i can think of:
|
# 2 things i can think of:
|
||||||
# - register downsample-able graphics specially and only
|
# - register downsample-able graphics specially and only
|
||||||
# iterate those.
|
# iterate those.
|
||||||
# - only register this when certain downsampleable graphics are
|
# - only register this when certain downsample-able graphics are
|
||||||
# "added to scene".
|
# "added to scene".
|
||||||
src_vb.sigRangeChangedManually.connect(
|
src_vb.sigRangeChangedManually.connect(
|
||||||
self.maybe_downsample_graphics
|
self.maybe_downsample_graphics
|
||||||
)
|
)
|
||||||
|
|
||||||
# mouse wheel doesn't emit XRangeChanged
|
# mouse wheel doesn't emit XRangeChanged
|
||||||
src_vb.sigRangeChangedManually.connect(self._set_yrange)
|
src_vb.sigRangeChangedManually.connect(self._set_yrange)
|
||||||
|
|
||||||
|
# XXX: enabling these will cause "jittery"-ness
|
||||||
|
# on zoom where sharp diffs in the y-range will
|
||||||
|
# not re-size right away until a new sample update?
|
||||||
|
# if src_vb is not self:
|
||||||
# src_vb.sigXRangeChanged.connect(self._set_yrange)
|
# src_vb.sigXRangeChanged.connect(self._set_yrange)
|
||||||
# src_vb.sigXRangeChanged.connect(
|
# src_vb.sigXRangeChanged.connect(
|
||||||
# self.maybe_downsample_graphics
|
# self.maybe_downsample_graphics
|
||||||
|
@ -882,7 +911,7 @@ class ChartView(ViewBox):
|
||||||
graphics items which are our children.
|
graphics items which are our children.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
graphics = [f.graphics for f in self._chart._flows.values()]
|
graphics = [f.graphics for f in self._chart._vizs.values()]
|
||||||
if not graphics:
|
if not graphics:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -897,8 +926,7 @@ class ChartView(ViewBox):
|
||||||
self,
|
self,
|
||||||
autoscale_overlays: bool = True,
|
autoscale_overlays: bool = True,
|
||||||
):
|
):
|
||||||
|
profiler = Profiler(
|
||||||
profiler = pg.debug.Profiler(
|
|
||||||
msg=f'ChartView.maybe_downsample_graphics() for {self.name}',
|
msg=f'ChartView.maybe_downsample_graphics() for {self.name}',
|
||||||
disabled=not pg_profile_enabled(),
|
disabled=not pg_profile_enabled(),
|
||||||
|
|
||||||
|
@ -912,10 +940,14 @@ class ChartView(ViewBox):
|
||||||
|
|
||||||
# TODO: a faster single-loop-iterator way of doing this XD
|
# TODO: a faster single-loop-iterator way of doing this XD
|
||||||
chart = self._chart
|
chart = self._chart
|
||||||
linked = self.linkedsplits
|
plots = {chart.name: chart}
|
||||||
plots = linked.subplots | {chart.name: chart}
|
|
||||||
|
linked = self.linked
|
||||||
|
if linked:
|
||||||
|
plots |= linked.subplots
|
||||||
|
|
||||||
for chart_name, chart in plots.items():
|
for chart_name, chart in plots.items():
|
||||||
for name, flow in chart._flows.items():
|
for name, flow in chart._vizs.items():
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not flow.render
|
not flow.render
|
||||||
|
@ -923,6 +955,7 @@ class ChartView(ViewBox):
|
||||||
# XXX: super important to be aware of this.
|
# XXX: super important to be aware of this.
|
||||||
# or not flow.graphics.isVisible()
|
# or not flow.graphics.isVisible()
|
||||||
):
|
):
|
||||||
|
# print(f'skipping {flow.name}')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# pass in no array which will read and render from the last
|
# pass in no array which will read and render from the last
|
||||||
|
|
|
@ -26,6 +26,7 @@ from PyQt5.QtCore import QPointF
|
||||||
|
|
||||||
from ._axes import YAxisLabel
|
from ._axes import YAxisLabel
|
||||||
from ._style import hcolor
|
from ._style import hcolor
|
||||||
|
from ._pg_overrides import PlotItem
|
||||||
|
|
||||||
|
|
||||||
class LevelLabel(YAxisLabel):
|
class LevelLabel(YAxisLabel):
|
||||||
|
@ -132,7 +133,7 @@ class LevelLabel(YAxisLabel):
|
||||||
level = self.fields['level']
|
level = self.fields['level']
|
||||||
|
|
||||||
# map "level" to local coords
|
# map "level" to local coords
|
||||||
abs_xy = self._chart.mapFromView(QPointF(0, level))
|
abs_xy = self._pi.mapFromView(QPointF(0, level))
|
||||||
|
|
||||||
self.update_label(
|
self.update_label(
|
||||||
abs_xy,
|
abs_xy,
|
||||||
|
@ -149,7 +150,7 @@ class LevelLabel(YAxisLabel):
|
||||||
h, w = self.set_label_str(fields)
|
h, w = self.set_label_str(fields)
|
||||||
|
|
||||||
if self._adjust_to_l1:
|
if self._adjust_to_l1:
|
||||||
self._x_offset = self._chart._max_l1_line_len
|
self._x_offset = self._pi.chart_widget._max_l1_line_len
|
||||||
|
|
||||||
self.setPos(QPointF(
|
self.setPos(QPointF(
|
||||||
self._h_shift * (w + self._x_offset),
|
self._h_shift * (w + self._x_offset),
|
||||||
|
@ -236,10 +237,10 @@ class L1Label(LevelLabel):
|
||||||
# Set a global "max L1 label length" so we can
|
# Set a global "max L1 label length" so we can
|
||||||
# look it up on order lines and adjust their
|
# look it up on order lines and adjust their
|
||||||
# labels not to overlap with it.
|
# labels not to overlap with it.
|
||||||
chart = self._chart
|
chart = self._pi.chart_widget
|
||||||
chart._max_l1_line_len: float = max(
|
chart._max_l1_line_len: float = max(
|
||||||
chart._max_l1_line_len,
|
chart._max_l1_line_len,
|
||||||
w
|
w,
|
||||||
)
|
)
|
||||||
|
|
||||||
return h, w
|
return h, w
|
||||||
|
@ -251,17 +252,17 @@ class L1Labels:
|
||||||
"""
|
"""
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
chart: 'ChartPlotWidget', # noqa
|
plotitem: PlotItem,
|
||||||
digits: int = 2,
|
digits: int = 2,
|
||||||
size_digits: int = 3,
|
size_digits: int = 3,
|
||||||
font_size: str = 'small',
|
font_size: str = 'small',
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
self.chart = chart
|
chart = self.chart = plotitem.chart_widget
|
||||||
|
|
||||||
raxis = chart.getAxis('right')
|
raxis = plotitem.getAxis('right')
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'chart': chart,
|
'chart': plotitem,
|
||||||
'parent': raxis,
|
'parent': raxis,
|
||||||
|
|
||||||
'opacity': 1,
|
'opacity': 1,
|
||||||
|
|
|
@ -18,9 +18,14 @@
|
||||||
Lines for orders, alerts, L2.
|
Lines for orders, alerts, L2.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from math import floor
|
from math import floor
|
||||||
from typing import Optional, Callable
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
Callable,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
from pyqtgraph import Point, functions as fn
|
from pyqtgraph import Point, functions as fn
|
||||||
|
@ -37,6 +42,9 @@ from ..calc import humanize
|
||||||
from ._label import Label
|
from ._label import Label
|
||||||
from ._style import hcolor, _font
|
from ._style import hcolor, _font
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._cursor import Cursor
|
||||||
|
|
||||||
|
|
||||||
# TODO: probably worth investigating if we can
|
# TODO: probably worth investigating if we can
|
||||||
# make .boundingRect() faster:
|
# make .boundingRect() faster:
|
||||||
|
@ -84,7 +92,7 @@ class LevelLine(pg.InfiniteLine):
|
||||||
|
|
||||||
self._marker = None
|
self._marker = None
|
||||||
self.only_show_markers_on_hover = only_show_markers_on_hover
|
self.only_show_markers_on_hover = only_show_markers_on_hover
|
||||||
self.show_markers: bool = True # presuming the line is hovered at init
|
self.track_marker_pos: bool = False
|
||||||
|
|
||||||
# should line go all the way to far end or leave a "margin"
|
# should line go all the way to far end or leave a "margin"
|
||||||
# space for other graphics (eg. L1 book)
|
# space for other graphics (eg. L1 book)
|
||||||
|
@ -122,6 +130,9 @@ class LevelLine(pg.InfiniteLine):
|
||||||
self._y_incr_mult = 1 / chart.linked.symbol.tick_size
|
self._y_incr_mult = 1 / chart.linked.symbol.tick_size
|
||||||
self._right_end_sc: float = 0
|
self._right_end_sc: float = 0
|
||||||
|
|
||||||
|
# use px caching
|
||||||
|
self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
||||||
|
|
||||||
def txt_offsets(self) -> tuple[int, int]:
|
def txt_offsets(self) -> tuple[int, int]:
|
||||||
return 0, 0
|
return 0, 0
|
||||||
|
|
||||||
|
@ -216,20 +227,23 @@ class LevelLine(pg.InfiniteLine):
|
||||||
y: float
|
y: float
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Chart coordinates cursor tracking callback.
|
'''
|
||||||
|
Chart coordinates cursor tracking callback.
|
||||||
|
|
||||||
this is called by our ``Cursor`` type once this line is set to
|
this is called by our ``Cursor`` type once this line is set to
|
||||||
track the cursor: for every movement this callback is invoked to
|
track the cursor: for every movement this callback is invoked to
|
||||||
reposition the line with the current view coordinates.
|
reposition the line with the current view coordinates.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
self.movable = True
|
self.movable = True
|
||||||
self.set_level(y) # implictly calls reposition handler
|
self.set_level(y) # implictly calls reposition handler
|
||||||
|
|
||||||
def mouseDragEvent(self, ev):
|
def mouseDragEvent(self, ev):
|
||||||
"""Override the ``InfiniteLine`` handler since we need more
|
'''
|
||||||
|
Override the ``InfiniteLine`` handler since we need more
|
||||||
detailed control and start end signalling.
|
detailed control and start end signalling.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
cursor = self._chart.linked.cursor
|
cursor = self._chart.linked.cursor
|
||||||
|
|
||||||
# hide y-crosshair
|
# hide y-crosshair
|
||||||
|
@ -281,10 +295,20 @@ class LevelLine(pg.InfiniteLine):
|
||||||
# show y-crosshair again
|
# show y-crosshair again
|
||||||
cursor.show_xhair()
|
cursor.show_xhair()
|
||||||
|
|
||||||
def delete(self) -> None:
|
def get_cursor(self) -> Optional[Cursor]:
|
||||||
"""Remove this line from containing chart/view/scene.
|
|
||||||
|
|
||||||
"""
|
chart = self._chart
|
||||||
|
cur = chart.linked.cursor
|
||||||
|
if self in cur._hovered:
|
||||||
|
return cur
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def delete(self) -> None:
|
||||||
|
'''
|
||||||
|
Remove this line from containing chart/view/scene.
|
||||||
|
|
||||||
|
'''
|
||||||
scene = self.scene()
|
scene = self.scene()
|
||||||
if scene:
|
if scene:
|
||||||
for label in self._labels:
|
for label in self._labels:
|
||||||
|
@ -298,9 +322,8 @@ class LevelLine(pg.InfiniteLine):
|
||||||
|
|
||||||
# remove from chart/cursor states
|
# remove from chart/cursor states
|
||||||
chart = self._chart
|
chart = self._chart
|
||||||
cur = chart.linked.cursor
|
cur = self.get_cursor()
|
||||||
|
if cur:
|
||||||
if self in cur._hovered:
|
|
||||||
cur._hovered.remove(self)
|
cur._hovered.remove(self)
|
||||||
|
|
||||||
chart.plotItem.removeItem(self)
|
chart.plotItem.removeItem(self)
|
||||||
|
@ -308,8 +331,8 @@ class LevelLine(pg.InfiniteLine):
|
||||||
def mouseDoubleClickEvent(
|
def mouseDoubleClickEvent(
|
||||||
self,
|
self,
|
||||||
ev: QtGui.QMouseEvent,
|
ev: QtGui.QMouseEvent,
|
||||||
) -> None:
|
|
||||||
|
|
||||||
|
) -> None:
|
||||||
# TODO: enter labels edit mode
|
# TODO: enter labels edit mode
|
||||||
print(f'double click {ev}')
|
print(f'double click {ev}')
|
||||||
|
|
||||||
|
@ -334,30 +357,22 @@ class LevelLine(pg.InfiniteLine):
|
||||||
|
|
||||||
line_end, marker_right, r_axis_x = self._chart.marker_right_points()
|
line_end, marker_right, r_axis_x = self._chart.marker_right_points()
|
||||||
|
|
||||||
if self.show_markers and self.markers:
|
# (legacy) NOTE: at one point this seemed slower when moving around
|
||||||
|
# order lines.. not sure if that's still true or why but we've
|
||||||
p.setPen(self.pen)
|
# dropped the original hacky `.pain()` transform stuff for inf
|
||||||
qgo_draw_markers(
|
# line markers now - check the git history if it needs to be
|
||||||
self.markers,
|
# reverted.
|
||||||
self.pen.color(),
|
if self._marker:
|
||||||
p,
|
if self.track_marker_pos:
|
||||||
vb_left,
|
# make the line end at the marker's x pos
|
||||||
vb_right,
|
line_end = marker_right = self._marker.pos().x()
|
||||||
marker_right,
|
|
||||||
)
|
|
||||||
# marker_size = self.markers[0][2]
|
|
||||||
self._maxMarkerSize = max([m[2] / 2. for m in self.markers])
|
|
||||||
|
|
||||||
# this seems slower when moving around
|
|
||||||
# order lines.. not sure wtf is up with that.
|
|
||||||
# for now we're just using it on the position line.
|
|
||||||
elif self._marker:
|
|
||||||
|
|
||||||
# TODO: make this label update part of a scene-aware-marker
|
# TODO: make this label update part of a scene-aware-marker
|
||||||
# composed annotation
|
# composed annotation
|
||||||
self._marker.setPos(
|
self._marker.setPos(
|
||||||
QPointF(marker_right, self.scene_y())
|
QPointF(marker_right, self.scene_y())
|
||||||
)
|
)
|
||||||
|
|
||||||
if hasattr(self._marker, 'label'):
|
if hasattr(self._marker, 'label'):
|
||||||
self._marker.label.update()
|
self._marker.label.update()
|
||||||
|
|
||||||
|
@ -379,16 +394,14 @@ class LevelLine(pg.InfiniteLine):
|
||||||
|
|
||||||
def hide(self) -> None:
|
def hide(self) -> None:
|
||||||
super().hide()
|
super().hide()
|
||||||
if self._marker:
|
mkr = self._marker
|
||||||
self._marker.hide()
|
if mkr:
|
||||||
# needed for ``order_line()`` lines currently
|
mkr.hide()
|
||||||
self._marker.label.hide()
|
|
||||||
|
|
||||||
def show(self) -> None:
|
def show(self) -> None:
|
||||||
super().show()
|
super().show()
|
||||||
if self._marker:
|
if self._marker:
|
||||||
self._marker.show()
|
self._marker.show()
|
||||||
# self._marker.label.show()
|
|
||||||
|
|
||||||
def scene_y(self) -> float:
|
def scene_y(self) -> float:
|
||||||
return self.getViewBox().mapFromView(
|
return self.getViewBox().mapFromView(
|
||||||
|
@ -421,6 +434,10 @@ class LevelLine(pg.InfiniteLine):
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
@property
|
||||||
|
def marker(self) -> LevelMarker:
|
||||||
|
return self._marker
|
||||||
|
|
||||||
def hoverEvent(self, ev):
|
def hoverEvent(self, ev):
|
||||||
'''
|
'''
|
||||||
Mouse hover callback.
|
Mouse hover callback.
|
||||||
|
@ -429,17 +446,16 @@ class LevelLine(pg.InfiniteLine):
|
||||||
cur = self._chart.linked.cursor
|
cur = self._chart.linked.cursor
|
||||||
|
|
||||||
# hovered
|
# hovered
|
||||||
if (not ev.isExit()) and ev.acceptDrags(QtCore.Qt.LeftButton):
|
if (
|
||||||
|
not ev.isExit()
|
||||||
|
and ev.acceptDrags(QtCore.Qt.LeftButton)
|
||||||
|
):
|
||||||
# if already hovered we don't need to run again
|
# if already hovered we don't need to run again
|
||||||
if self.mouseHovering is True:
|
if self.mouseHovering is True:
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.only_show_markers_on_hover:
|
if self.only_show_markers_on_hover:
|
||||||
self.show_markers = True
|
self.show_markers()
|
||||||
|
|
||||||
if self._marker:
|
|
||||||
self._marker.show()
|
|
||||||
|
|
||||||
# highlight if so configured
|
# highlight if so configured
|
||||||
if self.highlight_on_hover:
|
if self.highlight_on_hover:
|
||||||
|
@ -482,11 +498,7 @@ class LevelLine(pg.InfiniteLine):
|
||||||
cur._hovered.remove(self)
|
cur._hovered.remove(self)
|
||||||
|
|
||||||
if self.only_show_markers_on_hover:
|
if self.only_show_markers_on_hover:
|
||||||
self.show_markers = False
|
self.hide_markers()
|
||||||
|
|
||||||
if self._marker:
|
|
||||||
self._marker.hide()
|
|
||||||
self._marker.label.hide()
|
|
||||||
|
|
||||||
if self not in cur._trackers:
|
if self not in cur._trackers:
|
||||||
cur.show_xhair(y_label_level=self.value())
|
cur.show_xhair(y_label_level=self.value())
|
||||||
|
@ -498,6 +510,15 @@ class LevelLine(pg.InfiniteLine):
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
def hide_markers(self) -> None:
|
||||||
|
if self._marker:
|
||||||
|
self._marker.hide()
|
||||||
|
self._marker.label.hide()
|
||||||
|
|
||||||
|
def show_markers(self) -> None:
|
||||||
|
if self._marker:
|
||||||
|
self._marker.show()
|
||||||
|
|
||||||
|
|
||||||
def level_line(
|
def level_line(
|
||||||
|
|
||||||
|
@ -518,9 +539,10 @@ def level_line(
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> LevelLine:
|
) -> LevelLine:
|
||||||
"""Convenience routine to add a styled horizontal line to a plot.
|
'''
|
||||||
|
Convenience routine to add a styled horizontal line to a plot.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
hl_color = color + '_light' if highlight_on_hover else color
|
hl_color = color + '_light' if highlight_on_hover else color
|
||||||
|
|
||||||
line = LevelLine(
|
line = LevelLine(
|
||||||
|
@ -702,7 +724,7 @@ def order_line(
|
||||||
marker = LevelMarker(
|
marker = LevelMarker(
|
||||||
chart=chart,
|
chart=chart,
|
||||||
style=marker_style,
|
style=marker_style,
|
||||||
get_level=line.value,
|
get_level=line.value, # callback
|
||||||
size=marker_size,
|
size=marker_size,
|
||||||
keep_in_view=False,
|
keep_in_view=False,
|
||||||
)
|
)
|
||||||
|
@ -711,7 +733,8 @@ def order_line(
|
||||||
marker = line.add_marker(marker)
|
marker = line.add_marker(marker)
|
||||||
|
|
||||||
# XXX: DON'T COMMENT THIS!
|
# XXX: DON'T COMMENT THIS!
|
||||||
# this fixes it the artifact issue! .. of course, bounding rect stuff
|
# this fixes it the artifact issue!
|
||||||
|
# .. of course, bounding rect stuff
|
||||||
line._maxMarkerSize = marker_size
|
line._maxMarkerSize = marker_size
|
||||||
|
|
||||||
assert line._marker is marker
|
assert line._marker is marker
|
||||||
|
@ -732,7 +755,8 @@ def order_line(
|
||||||
|
|
||||||
if action != 'alert':
|
if action != 'alert':
|
||||||
|
|
||||||
# add a partial position label if we also added a level marker
|
# add a partial position label if we also added a level
|
||||||
|
# marker
|
||||||
pp_size_label = Label(
|
pp_size_label = Label(
|
||||||
view=view,
|
view=view,
|
||||||
color=line.color,
|
color=line.color,
|
||||||
|
@ -766,9 +790,9 @@ def order_line(
|
||||||
# XXX: without this the pp proportion label next the marker
|
# XXX: without this the pp proportion label next the marker
|
||||||
# seems to lag? this is the same issue we had with position
|
# seems to lag? this is the same issue we had with position
|
||||||
# lines which we handle with ``.update_graphcis()``.
|
# lines which we handle with ``.update_graphcis()``.
|
||||||
# marker._on_paint=lambda marker: pp_size_label.update()
|
|
||||||
marker._on_paint = lambda marker: pp_size_label.update()
|
marker._on_paint = lambda marker: pp_size_label.update()
|
||||||
|
|
||||||
|
# XXX: THIS IS AN UNTYPED MONKEY PATCH!?!?!
|
||||||
marker.label = label
|
marker.label = label
|
||||||
|
|
||||||
# sanity check
|
# sanity check
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Notifications utils.
|
||||||
|
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import subprocess
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import trio
|
||||||
|
|
||||||
|
from ..log import get_logger
|
||||||
|
from ..clearing._messages import (
|
||||||
|
Status,
|
||||||
|
)
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
_dbus_uid: Optional[str] = ''
|
||||||
|
|
||||||
|
|
||||||
|
async def notify_from_ems_status_msg(
|
||||||
|
msg: Status,
|
||||||
|
duration: int = 3000,
|
||||||
|
is_subproc: bool = False,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Send a linux desktop notification.
|
||||||
|
|
||||||
|
Handle subprocesses by discovering the dbus user id
|
||||||
|
on first call.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if platform.system() != "Linux":
|
||||||
|
return
|
||||||
|
|
||||||
|
# TODO: this in another task?
|
||||||
|
# not sure if this will ever be a bottleneck,
|
||||||
|
# we probably could do graphics stuff first tho?
|
||||||
|
|
||||||
|
if is_subproc:
|
||||||
|
global _dbus_uid
|
||||||
|
su = os.environ.get('SUDO_USER')
|
||||||
|
if (
|
||||||
|
not _dbus_uid
|
||||||
|
and su
|
||||||
|
):
|
||||||
|
|
||||||
|
# TODO: use `trio` but we need to use nursery.start()
|
||||||
|
# to use pipes?
|
||||||
|
# result = await trio.run_process(
|
||||||
|
result = subprocess.run(
|
||||||
|
[
|
||||||
|
'id',
|
||||||
|
'-u',
|
||||||
|
su,
|
||||||
|
],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
# check=True
|
||||||
|
)
|
||||||
|
_dbus_uid = result.stdout.decode("utf-8").replace('\n', '')
|
||||||
|
|
||||||
|
os.environ['DBUS_SESSION_BUS_ADDRESS'] = (
|
||||||
|
f'unix:path=/run/user/{_dbus_uid}/bus'
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await trio.run_process(
|
||||||
|
[
|
||||||
|
'notify-send',
|
||||||
|
'-u', 'normal',
|
||||||
|
'-t', f'{duration}',
|
||||||
|
'piker',
|
||||||
|
|
||||||
|
# TODO: add in standard fill/exec info that maybe we
|
||||||
|
# pack in a broker independent way?
|
||||||
|
f"'{msg.pformat()}'",
|
||||||
|
],
|
||||||
|
capture_stdout=True,
|
||||||
|
capture_stderr=True,
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
if result.returncode != 0:
|
||||||
|
log.warn(f'No notification daemon installed stderr: {result.stderr}')
|
||||||
|
|
||||||
|
log.runtime(result)
|
|
@ -25,13 +25,22 @@ from typing import (
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
from PyQt5 import (
|
||||||
from PyQt5.QtCore import QLineF, QPointF
|
QtGui,
|
||||||
|
QtWidgets,
|
||||||
|
)
|
||||||
|
from PyQt5.QtCore import (
|
||||||
|
QLineF,
|
||||||
|
QRectF,
|
||||||
|
)
|
||||||
|
|
||||||
from PyQt5.QtGui import QPainterPath
|
from PyQt5.QtGui import QPainterPath
|
||||||
|
|
||||||
|
from ._curve import FlowGraphic
|
||||||
from .._profile import pg_profile_enabled, ms_slower_then
|
from .._profile import pg_profile_enabled, ms_slower_then
|
||||||
from ._style import hcolor
|
from ._style import hcolor
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
from .._profile import Profiler
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ._chart import LinkedSplits
|
from ._chart import LinkedSplits
|
||||||
|
@ -43,7 +52,8 @@ log = get_logger(__name__)
|
||||||
def bar_from_ohlc_row(
|
def bar_from_ohlc_row(
|
||||||
row: np.ndarray,
|
row: np.ndarray,
|
||||||
# 0.5 is no overlap between arms, 1.0 is full overlap
|
# 0.5 is no overlap between arms, 1.0 is full overlap
|
||||||
w: float = 0.43
|
bar_w: float,
|
||||||
|
bar_gap: float = 0.16
|
||||||
|
|
||||||
) -> tuple[QLineF]:
|
) -> tuple[QLineF]:
|
||||||
'''
|
'''
|
||||||
|
@ -51,8 +61,7 @@ def bar_from_ohlc_row(
|
||||||
OHLC "bar" for use in the "last datum" of a series.
|
OHLC "bar" for use in the "last datum" of a series.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
open, high, low, close, index = row[
|
open, high, low, close, index = row
|
||||||
['open', 'high', 'low', 'close', 'index']]
|
|
||||||
|
|
||||||
# TODO: maybe consider using `QGraphicsLineItem` ??
|
# TODO: maybe consider using `QGraphicsLineItem` ??
|
||||||
# gives us a ``.boundingRect()`` on the objects which may make
|
# gives us a ``.boundingRect()`` on the objects which may make
|
||||||
|
@ -60,9 +69,11 @@ def bar_from_ohlc_row(
|
||||||
# history path faster since it's done in C++:
|
# history path faster since it's done in C++:
|
||||||
# https://doc.qt.io/qt-5/qgraphicslineitem.html
|
# https://doc.qt.io/qt-5/qgraphicslineitem.html
|
||||||
|
|
||||||
|
mid: float = (bar_w / 2) + index
|
||||||
|
|
||||||
# high -> low vertical (body) line
|
# high -> low vertical (body) line
|
||||||
if low != high:
|
if low != high:
|
||||||
hl = QLineF(index, low, index, high)
|
hl = QLineF(mid, low, mid, high)
|
||||||
else:
|
else:
|
||||||
# XXX: if we don't do it renders a weird rectangle?
|
# XXX: if we don't do it renders a weird rectangle?
|
||||||
# see below for filtering this later...
|
# see below for filtering this later...
|
||||||
|
@ -73,15 +84,18 @@ def bar_from_ohlc_row(
|
||||||
# the index's range according to the view mapping coordinates.
|
# the index's range according to the view mapping coordinates.
|
||||||
|
|
||||||
# open line
|
# open line
|
||||||
o = QLineF(index - w, open, index, open)
|
o = QLineF(index + bar_gap, open, mid, open)
|
||||||
|
|
||||||
# close line
|
# close line
|
||||||
c = QLineF(index, close, index + w, close)
|
c = QLineF(
|
||||||
|
mid, close,
|
||||||
|
index + bar_w - bar_gap, close,
|
||||||
|
)
|
||||||
|
|
||||||
return [hl, o, c]
|
return [hl, o, c]
|
||||||
|
|
||||||
|
|
||||||
class BarItems(pg.GraphicsObject):
|
class BarItems(FlowGraphic):
|
||||||
'''
|
'''
|
||||||
"Price range" bars graphics rendered from a OHLC sampled sequence.
|
"Price range" bars graphics rendered from a OHLC sampled sequence.
|
||||||
|
|
||||||
|
@ -90,8 +104,8 @@ class BarItems(pg.GraphicsObject):
|
||||||
self,
|
self,
|
||||||
linked: LinkedSplits,
|
linked: LinkedSplits,
|
||||||
plotitem: 'pg.PlotItem', # noqa
|
plotitem: 'pg.PlotItem', # noqa
|
||||||
pen_color: str = 'bracket',
|
color: str = 'bracket',
|
||||||
last_bar_color: str = 'bracket',
|
last_bar_color: str = 'original',
|
||||||
|
|
||||||
name: Optional[str] = None,
|
name: Optional[str] = None,
|
||||||
|
|
||||||
|
@ -100,21 +114,37 @@ class BarItems(pg.GraphicsObject):
|
||||||
self.linked = linked
|
self.linked = linked
|
||||||
# XXX: for the mega-lulz increasing width here increases draw
|
# XXX: for the mega-lulz increasing width here increases draw
|
||||||
# latency... so probably don't do it until we figure that out.
|
# latency... so probably don't do it until we figure that out.
|
||||||
self._color = pen_color
|
self._color = color
|
||||||
self.bars_pen = pg.mkPen(hcolor(pen_color), width=1)
|
self.bars_pen = pg.mkPen(hcolor(color), width=1)
|
||||||
self.last_bar_pen = pg.mkPen(hcolor(last_bar_color), width=2)
|
self.last_bar_pen = pg.mkPen(hcolor(last_bar_color), width=2)
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|
||||||
self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
# XXX: causes this weird jitter bug when click-drag panning
|
||||||
|
# where the path curve will awkwardly flicker back and forth?
|
||||||
|
# self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
||||||
|
|
||||||
self.path = QPainterPath()
|
self.path = QPainterPath()
|
||||||
self._last_bar_lines: Optional[tuple[QLineF, ...]] = None
|
self._last_bar_lines: tuple[QLineF, ...] | None = None
|
||||||
|
|
||||||
def x_uppx(self) -> int:
|
def x_last(self) -> None | float:
|
||||||
# we expect the downsample curve report this.
|
'''
|
||||||
return 0
|
Return the last most x value of the close line segment
|
||||||
|
or if not drawn yet, ``None``.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if self._last_bar_lines:
|
||||||
|
close_arm_line = self._last_bar_lines[-1]
|
||||||
|
return close_arm_line.x2() if close_arm_line else None
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def boundingRect(self):
|
|
||||||
# Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
|
# Qt docs: https://doc.qt.io/qt-5/qgraphicsitem.html#boundingRect
|
||||||
|
def boundingRect(self):
|
||||||
|
# profiler = Profiler(
|
||||||
|
# msg=f'BarItems.boundingRect(): `{self._name}`',
|
||||||
|
# disabled=not pg_profile_enabled(),
|
||||||
|
# ms_threshold=ms_slower_then,
|
||||||
|
# )
|
||||||
|
|
||||||
# TODO: Can we do rect caching to make this faster
|
# TODO: Can we do rect caching to make this faster
|
||||||
# like `pg.PlotCurveItem` does? In theory it's just
|
# like `pg.PlotCurveItem` does? In theory it's just
|
||||||
|
@ -134,32 +164,37 @@ class BarItems(pg.GraphicsObject):
|
||||||
hb.topLeft(),
|
hb.topLeft(),
|
||||||
hb.bottomRight(),
|
hb.bottomRight(),
|
||||||
)
|
)
|
||||||
|
mn_y = hb_tl.y()
|
||||||
|
mx_y = hb_br.y()
|
||||||
|
most_left = hb_tl.x()
|
||||||
|
most_right = hb_br.x()
|
||||||
|
# profiler('calc path vertices')
|
||||||
|
|
||||||
# need to include last bar height or BR will be off
|
# need to include last bar height or BR will be off
|
||||||
mx_y = hb_br.y()
|
# OHLC line segments: [hl, o, c]
|
||||||
mn_y = hb_tl.y()
|
last_lines: tuple[QLineF] | None = self._last_bar_lines
|
||||||
|
|
||||||
last_lines = self._last_bar_lines
|
|
||||||
if last_lines:
|
if last_lines:
|
||||||
body_line = self._last_bar_lines[0]
|
(
|
||||||
if body_line:
|
hl,
|
||||||
mx_y = max(mx_y, max(body_line.y1(), body_line.y2()))
|
o,
|
||||||
mn_y = min(mn_y, min(body_line.y1(), body_line.y2()))
|
c,
|
||||||
|
) = last_lines
|
||||||
|
most_right = c.x2() + 1
|
||||||
|
ymx = ymn = c.y2()
|
||||||
|
|
||||||
return QtCore.QRectF(
|
if hl:
|
||||||
|
y1, y2 = hl.y1(), hl.y2()
|
||||||
|
ymn = min(y1, y2)
|
||||||
|
ymx = max(y1, y2)
|
||||||
|
mx_y = max(ymx, mx_y)
|
||||||
|
mn_y = min(ymn, mn_y)
|
||||||
|
# profiler('calc last bar vertices')
|
||||||
|
|
||||||
# top left
|
return QRectF(
|
||||||
QPointF(
|
most_left,
|
||||||
hb_tl.x(),
|
|
||||||
mn_y,
|
mn_y,
|
||||||
),
|
most_right - most_left + 1,
|
||||||
|
mx_y - mn_y,
|
||||||
# bottom right
|
|
||||||
QPointF(
|
|
||||||
hb_br.x() + 1,
|
|
||||||
mx_y,
|
|
||||||
)
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def paint(
|
def paint(
|
||||||
|
@ -170,7 +205,7 @@ class BarItems(pg.GraphicsObject):
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
profiler = pg.debug.Profiler(
|
profiler = Profiler(
|
||||||
disabled=not pg_profile_enabled(),
|
disabled=not pg_profile_enabled(),
|
||||||
ms_threshold=ms_slower_then,
|
ms_threshold=ms_slower_then,
|
||||||
)
|
)
|
||||||
|
@ -196,29 +231,40 @@ class BarItems(pg.GraphicsObject):
|
||||||
self,
|
self,
|
||||||
path: QPainterPath,
|
path: QPainterPath,
|
||||||
src_data: np.ndarray,
|
src_data: np.ndarray,
|
||||||
render_data: np.ndarray,
|
|
||||||
reset: bool,
|
reset: bool,
|
||||||
array_key: str,
|
array_key: str,
|
||||||
|
index_field: str,
|
||||||
fields: list[str] = [
|
|
||||||
'index',
|
|
||||||
'open',
|
|
||||||
'high',
|
|
||||||
'low',
|
|
||||||
'close',
|
|
||||||
],
|
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
# relevant fields
|
# relevant fields
|
||||||
|
fields: list[str] = [
|
||||||
|
'open',
|
||||||
|
'high',
|
||||||
|
'low',
|
||||||
|
'close',
|
||||||
|
index_field,
|
||||||
|
]
|
||||||
ohlc = src_data[fields]
|
ohlc = src_data[fields]
|
||||||
last_row = ohlc[-1:]
|
# last_row = ohlc[-1:]
|
||||||
|
|
||||||
# individual values
|
# individual values
|
||||||
last_row = i, o, h, l, last = ohlc[-1]
|
last_row = o, h, l, last, i = ohlc[-1]
|
||||||
|
|
||||||
|
# times = src_data['time']
|
||||||
|
# if times[-1] - times[-2]:
|
||||||
|
# breakpoint()
|
||||||
|
|
||||||
|
index = src_data[index_field]
|
||||||
|
step_size = index[-1] - index[-2]
|
||||||
|
|
||||||
# generate new lines objects for updatable "current bar"
|
# generate new lines objects for updatable "current bar"
|
||||||
self._last_bar_lines = bar_from_ohlc_row(last_row)
|
bg: float = 0.16 * step_size
|
||||||
|
self._last_bar_lines = bar_from_ohlc_row(
|
||||||
|
last_row,
|
||||||
|
bar_w=step_size,
|
||||||
|
bar_gap=bg,
|
||||||
|
)
|
||||||
|
|
||||||
# assert i == graphics.start_index - 1
|
# assert i == graphics.start_index - 1
|
||||||
# assert i == last_index
|
# assert i == last_index
|
||||||
|
@ -233,10 +279,16 @@ class BarItems(pg.GraphicsObject):
|
||||||
if l != h: # noqa
|
if l != h: # noqa
|
||||||
|
|
||||||
if body is None:
|
if body is None:
|
||||||
body = self._last_bar_lines[0] = QLineF(i, l, i, h)
|
body = self._last_bar_lines[0] = QLineF(
|
||||||
|
i + bg, l,
|
||||||
|
i + step_size - bg, h,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# update body
|
# update body
|
||||||
body.setLine(i, l, i, h)
|
body.setLine(
|
||||||
|
body.x1(), l,
|
||||||
|
body.x2(), h,
|
||||||
|
)
|
||||||
|
|
||||||
# XXX: pretty sure this is causing an issue where the
|
# XXX: pretty sure this is causing an issue where the
|
||||||
# bar has a large upward move right before the next
|
# bar has a large upward move right before the next
|
||||||
|
@ -247,4 +299,5 @@ class BarItems(pg.GraphicsObject):
|
||||||
# date / from some previous sample. It's weird though
|
# date / from some previous sample. It's weird though
|
||||||
# because i've seen it do this to bars i - 3 back?
|
# because i've seen it do this to bars i - 3 back?
|
||||||
|
|
||||||
return ohlc['index'], ohlc['close']
|
# return ohlc['time'], ohlc['close']
|
||||||
|
return ohlc[index_field], ohlc['close']
|
||||||
|
|
|
@ -22,12 +22,9 @@ from __future__ import annotations
|
||||||
from typing import (
|
from typing import (
|
||||||
Optional, Generic,
|
Optional, Generic,
|
||||||
TypeVar, Callable,
|
TypeVar, Callable,
|
||||||
Literal,
|
|
||||||
)
|
)
|
||||||
import enum
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from pydantic import BaseModel, validator
|
# from pydantic import BaseModel, validator
|
||||||
from pydantic.generics import GenericModel
|
from pydantic.generics import GenericModel
|
||||||
from PyQt5.QtWidgets import (
|
from PyQt5.QtWidgets import (
|
||||||
QWidget,
|
QWidget,
|
||||||
|
@ -38,6 +35,7 @@ from ._forms import (
|
||||||
# FontScaledDelegate,
|
# FontScaledDelegate,
|
||||||
Edit,
|
Edit,
|
||||||
)
|
)
|
||||||
|
from ..data.types import Struct
|
||||||
|
|
||||||
|
|
||||||
DataType = TypeVar('DataType')
|
DataType = TypeVar('DataType')
|
||||||
|
@ -62,7 +60,7 @@ class Selection(Field[DataType], Generic[DataType]):
|
||||||
options: dict[str, DataType]
|
options: dict[str, DataType]
|
||||||
# value: DataType = None
|
# value: DataType = None
|
||||||
|
|
||||||
@validator('value') # , always=True)
|
# @validator('value') # , always=True)
|
||||||
def set_value_first(
|
def set_value_first(
|
||||||
cls,
|
cls,
|
||||||
|
|
||||||
|
@ -100,7 +98,7 @@ class Edit(Field[DataType], Generic[DataType]):
|
||||||
widget_factory = Edit
|
widget_factory = Edit
|
||||||
|
|
||||||
|
|
||||||
class AllocatorPane(BaseModel):
|
class AllocatorPane(Struct):
|
||||||
|
|
||||||
account = Selection[str](
|
account = Selection[str](
|
||||||
options=dict.fromkeys(
|
options=dict.fromkeys(
|
||||||
|
|
|
@ -18,23 +18,27 @@
|
||||||
Charting overlay helpers.
|
Charting overlay helpers.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from typing import Callable, Optional
|
from collections import defaultdict
|
||||||
|
from functools import partial
|
||||||
from pyqtgraph.Qt.QtCore import (
|
from typing import (
|
||||||
# QObject,
|
Callable,
|
||||||
# Signal,
|
Optional,
|
||||||
Qt,
|
|
||||||
# QEvent,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from pyqtgraph.graphicsItems.AxisItem import AxisItem
|
from pyqtgraph.graphicsItems.AxisItem import AxisItem
|
||||||
from pyqtgraph.graphicsItems.ViewBox import ViewBox
|
from pyqtgraph.graphicsItems.ViewBox import ViewBox
|
||||||
from pyqtgraph.graphicsItems.GraphicsWidget import GraphicsWidget
|
# from pyqtgraph.graphicsItems.GraphicsWidget import GraphicsWidget
|
||||||
from pyqtgraph.graphicsItems.PlotItem.PlotItem import PlotItem
|
from pyqtgraph.graphicsItems.PlotItem.PlotItem import PlotItem
|
||||||
from pyqtgraph.Qt.QtCore import QObject, Signal, QEvent
|
from pyqtgraph.Qt.QtCore import (
|
||||||
from pyqtgraph.Qt.QtWidgets import QGraphicsGridLayout, QGraphicsLinearLayout
|
QObject,
|
||||||
|
Signal,
|
||||||
from ._interaction import ChartView
|
QEvent,
|
||||||
|
Qt,
|
||||||
|
)
|
||||||
|
from pyqtgraph.Qt.QtWidgets import (
|
||||||
|
# QGraphicsGridLayout,
|
||||||
|
QGraphicsLinearLayout,
|
||||||
|
)
|
||||||
|
|
||||||
__all__ = ["PlotItemOverlay"]
|
__all__ = ["PlotItemOverlay"]
|
||||||
|
|
||||||
|
@ -80,8 +84,8 @@ class ComposedGridLayout:
|
||||||
``<axis_name>i`` in the layout.
|
``<axis_name>i`` in the layout.
|
||||||
|
|
||||||
The ``item: PlotItem`` passed to the constructor's grid layout is
|
The ``item: PlotItem`` passed to the constructor's grid layout is
|
||||||
used verbatim as the "main plot" who's view box is give precedence
|
used verbatim as the "main plot" who's view box is given precedence
|
||||||
for input handling. The main plot's axes are removed from it's
|
for input handling. The main plot's axes are removed from its
|
||||||
layout and placed in the surrounding exterior layouts to allow for
|
layout and placed in the surrounding exterior layouts to allow for
|
||||||
re-ordering if desired.
|
re-ordering if desired.
|
||||||
|
|
||||||
|
@ -89,16 +93,11 @@ class ComposedGridLayout:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
item: PlotItem,
|
item: PlotItem,
|
||||||
grid: QGraphicsGridLayout,
|
|
||||||
reverse: bool = False, # insert items to the "center"
|
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
self.items: list[PlotItem] = []
|
|
||||||
# self.grid = grid
|
|
||||||
self.reverse = reverse
|
|
||||||
|
|
||||||
# TODO: use a ``bidict`` here?
|
self.items: list[PlotItem] = []
|
||||||
self._pi2axes: dict[
|
self._pi2axes: dict[ # TODO: use a ``bidict`` here?
|
||||||
int,
|
int,
|
||||||
dict[str, AxisItem],
|
dict[str, AxisItem],
|
||||||
] = {}
|
] = {}
|
||||||
|
@ -120,12 +119,13 @@ class ComposedGridLayout:
|
||||||
|
|
||||||
if name in ('top', 'bottom'):
|
if name in ('top', 'bottom'):
|
||||||
orient = Qt.Vertical
|
orient = Qt.Vertical
|
||||||
|
|
||||||
elif name in ('left', 'right'):
|
elif name in ('left', 'right'):
|
||||||
orient = Qt.Horizontal
|
orient = Qt.Horizontal
|
||||||
|
|
||||||
layout.setOrientation(orient)
|
layout.setOrientation(orient)
|
||||||
|
|
||||||
self.insert(0, item)
|
self.insert_plotitem(0, item)
|
||||||
|
|
||||||
# insert surrounding linear layouts into the parent pi's layout
|
# insert surrounding linear layouts into the parent pi's layout
|
||||||
# such that additional axes can be appended arbitrarily without
|
# such that additional axes can be appended arbitrarily without
|
||||||
|
@ -159,7 +159,7 @@ class ComposedGridLayout:
|
||||||
# enter plot into list for index tracking
|
# enter plot into list for index tracking
|
||||||
self.items.insert(index, plotitem)
|
self.items.insert(index, plotitem)
|
||||||
|
|
||||||
def insert(
|
def insert_plotitem(
|
||||||
self,
|
self,
|
||||||
index: int,
|
index: int,
|
||||||
plotitem: PlotItem,
|
plotitem: PlotItem,
|
||||||
|
@ -171,7 +171,9 @@ class ComposedGridLayout:
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if index < 0:
|
if index < 0:
|
||||||
raise ValueError('`insert()` only supports an index >= 0')
|
raise ValueError(
|
||||||
|
'`.insert_plotitem()` only supports an index >= 0'
|
||||||
|
)
|
||||||
|
|
||||||
# add plot's axes in sequence to the embedded linear layouts
|
# add plot's axes in sequence to the embedded linear layouts
|
||||||
# for each "side" thus avoiding graphics collisions.
|
# for each "side" thus avoiding graphics collisions.
|
||||||
|
@ -220,7 +222,7 @@ class ComposedGridLayout:
|
||||||
|
|
||||||
return index
|
return index
|
||||||
|
|
||||||
def append(
|
def append_plotitem(
|
||||||
self,
|
self,
|
||||||
item: PlotItem,
|
item: PlotItem,
|
||||||
|
|
||||||
|
@ -232,7 +234,7 @@ class ComposedGridLayout:
|
||||||
'''
|
'''
|
||||||
# for left and bottom axes we have to first remove
|
# for left and bottom axes we have to first remove
|
||||||
# items and re-insert to maintain a list-order.
|
# items and re-insert to maintain a list-order.
|
||||||
return self.insert(len(self.items), item)
|
return self.insert_plotitem(len(self.items), item)
|
||||||
|
|
||||||
def get_axis(
|
def get_axis(
|
||||||
self,
|
self,
|
||||||
|
@ -249,16 +251,16 @@ class ComposedGridLayout:
|
||||||
named = self._pi2axes[name]
|
named = self._pi2axes[name]
|
||||||
return named.get(index)
|
return named.get(index)
|
||||||
|
|
||||||
def pop(
|
# def pop(
|
||||||
self,
|
# self,
|
||||||
item: PlotItem,
|
# item: PlotItem,
|
||||||
|
|
||||||
) -> PlotItem:
|
# ) -> PlotItem:
|
||||||
'''
|
# '''
|
||||||
Remove item and restack all axes in list-order.
|
# Remove item and restack all axes in list-order.
|
||||||
|
|
||||||
'''
|
# '''
|
||||||
raise NotImplementedError
|
# raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
# Unimplemented features TODO:
|
# Unimplemented features TODO:
|
||||||
|
@ -279,194 +281,6 @@ class ComposedGridLayout:
|
||||||
# axis?
|
# axis?
|
||||||
|
|
||||||
|
|
||||||
# TODO: we might want to enabled some kind of manual flag to disable
|
|
||||||
# this method wrapping during type creation? As example a user could
|
|
||||||
# definitively decide **not** to enable broadcasting support by
|
|
||||||
# setting something like ``ViewBox.disable_relays = True``?
|
|
||||||
def mk_relay_method(
|
|
||||||
|
|
||||||
signame: str,
|
|
||||||
slot: Callable[
|
|
||||||
[ViewBox,
|
|
||||||
'QEvent',
|
|
||||||
Optional[AxisItem]],
|
|
||||||
None,
|
|
||||||
],
|
|
||||||
|
|
||||||
) -> Callable[
|
|
||||||
[
|
|
||||||
ViewBox,
|
|
||||||
# lol, there isn't really a generic type thanks
|
|
||||||
# to the rewrite of Qt's event system XD
|
|
||||||
'QEvent',
|
|
||||||
|
|
||||||
'Optional[AxisItem]',
|
|
||||||
'Optional[ViewBox]', # the ``relayed_from`` arg we provide
|
|
||||||
],
|
|
||||||
None,
|
|
||||||
]:
|
|
||||||
|
|
||||||
def maybe_broadcast(
|
|
||||||
vb: 'ViewBox',
|
|
||||||
ev: 'QEvent',
|
|
||||||
axis: 'Optional[int]' = None,
|
|
||||||
relayed_from: 'ViewBox' = None,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''
|
|
||||||
(soon to be) Decorator which makes an event handler
|
|
||||||
"broadcastable" to overlayed ``GraphicsWidget``s.
|
|
||||||
|
|
||||||
Adds relay signals based on the decorated handler's name
|
|
||||||
and conducts a signal broadcast of the relay signal if there
|
|
||||||
are consumers registered.
|
|
||||||
|
|
||||||
'''
|
|
||||||
# When no relay source has been set just bypass all
|
|
||||||
# the broadcast machinery.
|
|
||||||
if vb.event_relay_source is None:
|
|
||||||
ev.accept()
|
|
||||||
return slot(
|
|
||||||
vb,
|
|
||||||
ev,
|
|
||||||
axis=axis,
|
|
||||||
)
|
|
||||||
|
|
||||||
if relayed_from:
|
|
||||||
assert axis is None
|
|
||||||
|
|
||||||
# this is a relayed event and should be ignored (so it does not
|
|
||||||
# halt/short circuit the graphicscene loop). Further the
|
|
||||||
# surrounding handler for this signal must be allowed to execute
|
|
||||||
# and get processed by **this consumer**.
|
|
||||||
# print(f'{vb.name} rx relayed from {relayed_from.name}')
|
|
||||||
ev.ignore()
|
|
||||||
|
|
||||||
return slot(
|
|
||||||
vb,
|
|
||||||
ev,
|
|
||||||
axis=axis,
|
|
||||||
)
|
|
||||||
|
|
||||||
if axis is not None:
|
|
||||||
# print(f'{vb.name} handling axis event:\n{str(ev)}')
|
|
||||||
ev.accept()
|
|
||||||
return slot(
|
|
||||||
vb,
|
|
||||||
ev,
|
|
||||||
axis=axis,
|
|
||||||
)
|
|
||||||
|
|
||||||
elif (
|
|
||||||
relayed_from is None
|
|
||||||
and vb.event_relay_source is vb # we are the broadcaster
|
|
||||||
and axis is None
|
|
||||||
):
|
|
||||||
# Broadcast case: this is a source event which will be
|
|
||||||
# relayed to attached consumers and accepted after all
|
|
||||||
# consumers complete their own handling followed by this
|
|
||||||
# routine's processing. Sequence is,
|
|
||||||
# - pre-relay to all consumers *first* - ``.emit()`` blocks
|
|
||||||
# until all downstream relay handlers have run.
|
|
||||||
# - run the source handler for **this** event and accept
|
|
||||||
# the event
|
|
||||||
|
|
||||||
# Access the "bound signal" that is created
|
|
||||||
# on the widget type as part of instantiation.
|
|
||||||
signal = getattr(vb, signame)
|
|
||||||
# print(f'{vb.name} emitting {signame}')
|
|
||||||
|
|
||||||
# TODO/NOTE: we could also just bypass a "relay" signal
|
|
||||||
# entirely and instead call the handlers manually in
|
|
||||||
# a loop? This probably is a lot simpler and also doesn't
|
|
||||||
# have any downside, and allows not touching target widget
|
|
||||||
# internals.
|
|
||||||
signal.emit(
|
|
||||||
ev,
|
|
||||||
axis,
|
|
||||||
# passing this demarks a broadcasted/relayed event
|
|
||||||
vb,
|
|
||||||
)
|
|
||||||
# accept event so no more relays are fired.
|
|
||||||
ev.accept()
|
|
||||||
|
|
||||||
# call underlying wrapped method with an extra
|
|
||||||
# ``relayed_from`` value to denote that this is a relayed
|
|
||||||
# event handling case.
|
|
||||||
return slot(
|
|
||||||
vb,
|
|
||||||
ev,
|
|
||||||
axis=axis,
|
|
||||||
)
|
|
||||||
|
|
||||||
return maybe_broadcast
|
|
||||||
|
|
||||||
|
|
||||||
# XXX: :( can't define signals **after** class compile time
|
|
||||||
# so this is not really useful.
|
|
||||||
# def mk_relay_signal(
|
|
||||||
# func,
|
|
||||||
# name: str = None,
|
|
||||||
|
|
||||||
# ) -> Signal:
|
|
||||||
# (
|
|
||||||
# args,
|
|
||||||
# varargs,
|
|
||||||
# varkw,
|
|
||||||
# defaults,
|
|
||||||
# kwonlyargs,
|
|
||||||
# kwonlydefaults,
|
|
||||||
# annotations
|
|
||||||
# ) = inspect.getfullargspec(func)
|
|
||||||
|
|
||||||
# # XXX: generate a relay signal with 1 extra
|
|
||||||
# # argument for a ``relayed_from`` kwarg. Since
|
|
||||||
# # ``'self'`` is already ignored by signals we just need
|
|
||||||
# # to count the arguments since we're adding only 1 (and
|
|
||||||
# # ``args`` will capture that).
|
|
||||||
# numargs = len(args + list(defaults))
|
|
||||||
# signal = Signal(*tuple(numargs * [object]))
|
|
||||||
# signame = name or func.__name__ + 'Relay'
|
|
||||||
# return signame, signal
|
|
||||||
|
|
||||||
|
|
||||||
def enable_relays(
|
|
||||||
widget: GraphicsWidget,
|
|
||||||
handler_names: list[str],
|
|
||||||
|
|
||||||
) -> list[Signal]:
|
|
||||||
'''
|
|
||||||
Method override helper which enables relay of a particular
|
|
||||||
``Signal`` from some chosen broadcaster widget to a set of
|
|
||||||
consumer widgets which should operate their event handlers normally
|
|
||||||
but instead of signals "relayed" from the broadcaster.
|
|
||||||
|
|
||||||
Mostly useful for overlaying widgets that handle user input
|
|
||||||
that you want to overlay graphically. The target ``widget`` type must
|
|
||||||
define ``QtCore.Signal``s each with a `'Relay'` suffix for each
|
|
||||||
name provided in ``handler_names: list[str]``.
|
|
||||||
|
|
||||||
'''
|
|
||||||
signals = []
|
|
||||||
for name in handler_names:
|
|
||||||
handler = getattr(widget, name)
|
|
||||||
signame = name + 'Relay'
|
|
||||||
# ensure the target widget defines a relay signal
|
|
||||||
relay = getattr(widget, signame)
|
|
||||||
widget.relays[signame] = name
|
|
||||||
signals.append(relay)
|
|
||||||
method = mk_relay_method(signame, handler)
|
|
||||||
setattr(widget, name, method)
|
|
||||||
|
|
||||||
return signals
|
|
||||||
|
|
||||||
|
|
||||||
enable_relays(
|
|
||||||
ChartView,
|
|
||||||
['wheelEvent', 'mouseDragEvent']
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class PlotItemOverlay:
|
class PlotItemOverlay:
|
||||||
'''
|
'''
|
||||||
A composite for managing overlaid ``PlotItem`` instances such that
|
A composite for managing overlaid ``PlotItem`` instances such that
|
||||||
|
@ -482,16 +296,18 @@ class PlotItemOverlay:
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
self.root_plotitem: PlotItem = root_plotitem
|
self.root_plotitem: PlotItem = root_plotitem
|
||||||
|
self.relay_handlers: defaultdict[
|
||||||
|
str,
|
||||||
|
list[Callable],
|
||||||
|
] = defaultdict(list)
|
||||||
|
|
||||||
vb = root_plotitem.vb
|
# NOTE: required for scene layering/relaying; this guarantees
|
||||||
vb.event_relay_source = vb # TODO: maybe change name?
|
# the "root" plot receives priority for interaction
|
||||||
vb.setZValue(1000) # XXX: critical for scene layering/relaying
|
# events/signals.
|
||||||
|
root_plotitem.vb.setZValue(10)
|
||||||
|
|
||||||
self.overlays: list[PlotItem] = []
|
self.overlays: list[PlotItem] = []
|
||||||
self.layout = ComposedGridLayout(
|
self.layout = ComposedGridLayout(root_plotitem)
|
||||||
root_plotitem,
|
|
||||||
root_plotitem.layout,
|
|
||||||
)
|
|
||||||
self._relays: dict[str, Signal] = {}
|
self._relays: dict[str, Signal] = {}
|
||||||
|
|
||||||
def add_plotitem(
|
def add_plotitem(
|
||||||
|
@ -499,8 +315,10 @@ class PlotItemOverlay:
|
||||||
plotitem: PlotItem,
|
plotitem: PlotItem,
|
||||||
index: Optional[int] = None,
|
index: Optional[int] = None,
|
||||||
|
|
||||||
# TODO: we could also put the ``ViewBox.XAxis``
|
# event/signal names which will be broadcasted to all added
|
||||||
# style enum here?
|
# (relayee) ``PlotItem``s (eg. ``ViewBox.mouseDragEvent``).
|
||||||
|
relay_events: list[str] = [],
|
||||||
|
|
||||||
# (0,), # link x
|
# (0,), # link x
|
||||||
# (1,), # link y
|
# (1,), # link y
|
||||||
# (0, 1), # link both
|
# (0, 1), # link both
|
||||||
|
@ -510,58 +328,155 @@ class PlotItemOverlay:
|
||||||
|
|
||||||
index = index or len(self.overlays)
|
index = index or len(self.overlays)
|
||||||
root = self.root_plotitem
|
root = self.root_plotitem
|
||||||
# layout: QGraphicsGridLayout = root.layout
|
|
||||||
self.overlays.insert(index, plotitem)
|
self.overlays.insert(index, plotitem)
|
||||||
vb: ViewBox = plotitem.vb
|
vb: ViewBox = plotitem.vb
|
||||||
|
|
||||||
# mark this consumer overlay as ready to expect relayed events
|
|
||||||
# from the root plotitem.
|
|
||||||
vb.event_relay_source = root.vb
|
|
||||||
|
|
||||||
# TODO: some sane way to allow menu event broadcast XD
|
# TODO: some sane way to allow menu event broadcast XD
|
||||||
# vb.setMenuEnabled(False)
|
# vb.setMenuEnabled(False)
|
||||||
|
|
||||||
# TODO: inside the `maybe_broadcast()` (soon to be) decorator
|
# wire up any relay signal(s) from the source plot to added
|
||||||
# we need have checks that consumers have been attached to
|
# "overlays". We use a plain loop instead of mucking with
|
||||||
# these relay signals.
|
# re-connecting signal/slots which tends to be more invasive and
|
||||||
if link_axes != (0, 1):
|
# harder to implement and provides no measurable performance
|
||||||
|
# gain.
|
||||||
|
if relay_events:
|
||||||
|
for ev_name in relay_events:
|
||||||
|
relayee_handler: Callable[
|
||||||
|
[
|
||||||
|
ViewBox,
|
||||||
|
# lol, there isn't really a generic type thanks
|
||||||
|
# to the rewrite of Qt's event system XD
|
||||||
|
QEvent,
|
||||||
|
|
||||||
# wire up relay signals
|
AxisItem | None,
|
||||||
for relay_signal_name, handler_name in vb.relays.items():
|
],
|
||||||
# print(handler_name)
|
None,
|
||||||
# XXX: Signal class attrs are bound after instantiation
|
] = getattr(vb, ev_name)
|
||||||
# of the defining type, so we need to access that bound
|
|
||||||
# version here.
|
sub_handlers: list[Callable] = self.relay_handlers[ev_name]
|
||||||
signal = getattr(root.vb, relay_signal_name)
|
|
||||||
handler = getattr(vb, handler_name)
|
# on the first registry of a relayed event we pop the
|
||||||
signal.connect(handler)
|
# root's handler and override it to a custom broadcaster
|
||||||
|
# routine.
|
||||||
|
if not sub_handlers:
|
||||||
|
|
||||||
|
src_handler = getattr(
|
||||||
|
root.vb,
|
||||||
|
ev_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def broadcast(
|
||||||
|
ev: 'QEvent',
|
||||||
|
|
||||||
|
# TODO: drop this viewbox specific input and
|
||||||
|
# allow a predicate to be passed in by user.
|
||||||
|
axis: 'Optional[int]' = None,
|
||||||
|
|
||||||
|
*,
|
||||||
|
|
||||||
|
# these are bound in by the ``partial`` below
|
||||||
|
# and ensure a unique broadcaster per event.
|
||||||
|
ev_name: str = None,
|
||||||
|
src_handler: Callable = None,
|
||||||
|
relayed_from: 'ViewBox' = None,
|
||||||
|
|
||||||
|
# remaining inputs the source handler expects
|
||||||
|
**kwargs,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Broadcast signal or event: this is a source
|
||||||
|
event which will be relayed to attached
|
||||||
|
"relayee" plot item consumers.
|
||||||
|
|
||||||
|
The event is accepted halting any further
|
||||||
|
handlers from being triggered.
|
||||||
|
|
||||||
|
Sequence is,
|
||||||
|
- pre-relay to all consumers *first* - exactly
|
||||||
|
like how a ``Signal.emit()`` blocks until all
|
||||||
|
downstream relay handlers have run.
|
||||||
|
- run the event's source handler event
|
||||||
|
|
||||||
|
'''
|
||||||
|
ev.accept()
|
||||||
|
|
||||||
|
# broadcast first to relayees *first*. trigger
|
||||||
|
# relay of event to all consumers **before**
|
||||||
|
# processing/consumption in the source handler.
|
||||||
|
relayed_handlers = self.relay_handlers[ev_name]
|
||||||
|
|
||||||
|
assert getattr(vb, ev_name).__name__ == ev_name
|
||||||
|
|
||||||
|
# TODO: generalize as an input predicate
|
||||||
|
if axis is None:
|
||||||
|
for handler in relayed_handlers:
|
||||||
|
handler(
|
||||||
|
ev,
|
||||||
|
axis=axis,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
# run "source" widget's handler last
|
||||||
|
src_handler(
|
||||||
|
ev,
|
||||||
|
axis=axis,
|
||||||
|
)
|
||||||
|
|
||||||
|
# dynamic handler override on the publisher plot
|
||||||
|
setattr(
|
||||||
|
root.vb,
|
||||||
|
ev_name,
|
||||||
|
partial(
|
||||||
|
broadcast,
|
||||||
|
ev_name=ev_name,
|
||||||
|
src_handler=src_handler
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
assert getattr(root.vb, ev_name)
|
||||||
|
assert relayee_handler not in sub_handlers
|
||||||
|
|
||||||
|
# append relayed-to widget's handler to relay table
|
||||||
|
sub_handlers.append(relayee_handler)
|
||||||
|
|
||||||
# link dim-axes to root if requested by user.
|
# link dim-axes to root if requested by user.
|
||||||
# TODO: solve more-then-wanted scaled panning on click drag
|
|
||||||
# which seems to be due to broadcast. So we probably need to
|
|
||||||
# disable broadcast when axes are linked in a particular
|
|
||||||
# dimension?
|
|
||||||
for dim in link_axes:
|
for dim in link_axes:
|
||||||
# link x and y axes to new view box such that the top level
|
# link x and y axes to new view box such that the top level
|
||||||
# viewbox propagates to the root (and whatever other
|
# viewbox propagates to the root (and whatever other
|
||||||
# plotitem overlays that have been added).
|
# plotitem overlays that have been added).
|
||||||
vb.linkView(dim, root.vb)
|
vb.linkView(dim, root.vb)
|
||||||
|
|
||||||
# make overlaid viewbox impossible to focus since the top
|
# => NOTE: in order to prevent "more-then-linear" scaled
|
||||||
# level should handle all input and relay to overlays.
|
# panning moves on (for eg. click-drag) certain range change
|
||||||
# NOTE: this was solved with the `setZValue()` above!
|
# signals (i.e. ``.sigXRangeChanged``), the user needs to be
|
||||||
|
# careful that any broadcasted ``relay_events`` are are short
|
||||||
|
# circuited in sub-handlers (aka relayee's) implementations. As
|
||||||
|
# an example if a ``ViewBox.mouseDragEvent`` is broadcasted, the
|
||||||
|
# overlayed implementations need to be sure they either don't
|
||||||
|
# also link the x-axes (by not providing ``link_axes=(0,)``
|
||||||
|
# above) or that the relayee ``.mouseDragEvent()`` handlers are
|
||||||
|
# ready to "``return`` early" in the case that
|
||||||
|
# ``.sigXRangeChanged`` is emitted as part of linked axes.
|
||||||
|
# For more details on such signalling mechanics peek in
|
||||||
|
# ``ViewBox.linkView()``.
|
||||||
|
|
||||||
# TODO: we will probably want to add a "focus" api such that
|
# make overlaid viewbox impossible to focus since the top level
|
||||||
# a new "top level" ``PlotItem`` can be selected dynamically
|
# should handle all input and relay to overlays. Note that the
|
||||||
# (and presumably the axes dynamically sorted to match).
|
# "root" plot item gettingn interaction priority is configured
|
||||||
|
# with the ``.setZValue()`` during init.
|
||||||
vb.setFlag(
|
vb.setFlag(
|
||||||
vb.GraphicsItemFlag.ItemIsFocusable,
|
vb.GraphicsItemFlag.ItemIsFocusable,
|
||||||
False
|
False
|
||||||
)
|
)
|
||||||
vb.setFocusPolicy(Qt.NoFocus)
|
vb.setFocusPolicy(Qt.NoFocus)
|
||||||
|
|
||||||
|
# => TODO: add a "focus" api for switching the "top level"
|
||||||
|
# ``PlotItem`` dynamically.
|
||||||
|
|
||||||
# append-compose into the layout all axes from this plot
|
# append-compose into the layout all axes from this plot
|
||||||
self.layout.insert(index, plotitem)
|
self.layout.insert_plotitem(index, plotitem)
|
||||||
|
|
||||||
plotitem.setGeometry(root.vb.sceneBoundingRect())
|
plotitem.setGeometry(root.vb.sceneBoundingRect())
|
||||||
|
|
||||||
|
@ -579,24 +494,7 @@ class PlotItemOverlay:
|
||||||
root.vb.setFocus()
|
root.vb.setFocus()
|
||||||
assert root.vb.focusWidget()
|
assert root.vb.focusWidget()
|
||||||
|
|
||||||
# XXX: do we need this? Why would you build then destroy?
|
vb.setZValue(100)
|
||||||
def remove_plotitem(self, plotItem: PlotItem) -> None:
|
|
||||||
'''
|
|
||||||
Remove this ``PlotItem`` from the overlayed set making not shown
|
|
||||||
and unable to accept input.
|
|
||||||
|
|
||||||
'''
|
|
||||||
...
|
|
||||||
|
|
||||||
# TODO: i think this would be super hot B)
|
|
||||||
def focus_item(self, plotitem: PlotItem) -> PlotItem:
|
|
||||||
'''
|
|
||||||
Apply focus to a contained PlotItem thus making it the "top level"
|
|
||||||
item in the overlay able to accept peripheral's input from the user
|
|
||||||
and responsible for zoom and panning control via its ``ViewBox``.
|
|
||||||
|
|
||||||
'''
|
|
||||||
...
|
|
||||||
|
|
||||||
def get_axis(
|
def get_axis(
|
||||||
self,
|
self,
|
||||||
|
@ -630,8 +528,9 @@ class PlotItemOverlay:
|
||||||
|
|
||||||
return axes
|
return axes
|
||||||
|
|
||||||
# TODO: i guess we need this if you want to detach existing plots
|
# XXX: untested as of now.
|
||||||
# dynamically? XXX: untested as of now.
|
# TODO: need this as part of selecting a different root/source
|
||||||
|
# plot to rewire interaction event broadcast dynamically.
|
||||||
def _disconnect_all(
|
def _disconnect_all(
|
||||||
self,
|
self,
|
||||||
plotitem: PlotItem,
|
plotitem: PlotItem,
|
||||||
|
@ -646,3 +545,22 @@ class PlotItemOverlay:
|
||||||
disconnected.append(sig)
|
disconnected.append(sig)
|
||||||
|
|
||||||
return disconnected
|
return disconnected
|
||||||
|
|
||||||
|
# XXX: do we need this? Why would you build then destroy?
|
||||||
|
# def remove_plotitem(self, plotItem: PlotItem) -> None:
|
||||||
|
# '''
|
||||||
|
# Remove this ``PlotItem`` from the overlayed set making not shown
|
||||||
|
# and unable to accept input.
|
||||||
|
|
||||||
|
# '''
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# TODO: i think this would be super hot B)
|
||||||
|
# def focus_plotitem(self, plotitem: PlotItem) -> PlotItem:
|
||||||
|
# '''
|
||||||
|
# Apply focus to a contained PlotItem thus making it the "top level"
|
||||||
|
# item in the overlay able to accept peripheral's input from the user
|
||||||
|
# and responsible for zoom and panning control via its ``ViewBox``.
|
||||||
|
|
||||||
|
# '''
|
||||||
|
# ...
|
||||||
|
|
|
@ -1,236 +0,0 @@
|
||||||
# piker: trading gear for hackers
|
|
||||||
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
|
||||||
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU Affero General Public License for more details.
|
|
||||||
|
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
"""
|
|
||||||
Super fast ``QPainterPath`` generation related operator routines.
|
|
||||||
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
from typing import (
|
|
||||||
# Optional,
|
|
||||||
TYPE_CHECKING,
|
|
||||||
)
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
from numpy.lib import recfunctions as rfn
|
|
||||||
from numba import njit, float64, int64 # , optional
|
|
||||||
# import pyqtgraph as pg
|
|
||||||
from PyQt5 import QtGui
|
|
||||||
# from PyQt5.QtCore import QLineF, QPointF
|
|
||||||
|
|
||||||
from ..data._sharedmem import (
|
|
||||||
ShmArray,
|
|
||||||
)
|
|
||||||
# from .._profile import pg_profile_enabled, ms_slower_then
|
|
||||||
from ._compression import (
|
|
||||||
ds_m4,
|
|
||||||
)
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from ._flows import Renderer
|
|
||||||
|
|
||||||
|
|
||||||
def xy_downsample(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
uppx,
|
|
||||||
|
|
||||||
x_spacer: float = 0.5,
|
|
||||||
|
|
||||||
) -> tuple[np.ndarray, np.ndarray]:
|
|
||||||
|
|
||||||
# downsample whenever more then 1 pixels per datum can be shown.
|
|
||||||
# always refresh data bounds until we get diffing
|
|
||||||
# working properly, see above..
|
|
||||||
bins, x, y = ds_m4(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
uppx,
|
|
||||||
)
|
|
||||||
|
|
||||||
# flatten output to 1d arrays suitable for path-graphics generation.
|
|
||||||
x = np.broadcast_to(x[:, None], y.shape)
|
|
||||||
x = (x + np.array(
|
|
||||||
[-x_spacer, 0, 0, x_spacer]
|
|
||||||
)).flatten()
|
|
||||||
y = y.flatten()
|
|
||||||
|
|
||||||
return x, y
|
|
||||||
|
|
||||||
|
|
||||||
@njit(
|
|
||||||
# TODO: for now need to construct this manually for readonly arrays, see
|
|
||||||
# https://github.com/numba/numba/issues/4511
|
|
||||||
# ntypes.tuple((float64[:], float64[:], float64[:]))(
|
|
||||||
# numba_ohlc_dtype[::1], # contiguous
|
|
||||||
# int64,
|
|
||||||
# optional(float64),
|
|
||||||
# ),
|
|
||||||
nogil=True
|
|
||||||
)
|
|
||||||
def path_arrays_from_ohlc(
|
|
||||||
data: np.ndarray,
|
|
||||||
start: int64,
|
|
||||||
bar_gap: float64 = 0.43,
|
|
||||||
|
|
||||||
) -> np.ndarray:
|
|
||||||
'''
|
|
||||||
Generate an array of lines objects from input ohlc data.
|
|
||||||
|
|
||||||
'''
|
|
||||||
size = int(data.shape[0] * 6)
|
|
||||||
|
|
||||||
x = np.zeros(
|
|
||||||
# data,
|
|
||||||
shape=size,
|
|
||||||
dtype=float64,
|
|
||||||
)
|
|
||||||
y, c = x.copy(), x.copy()
|
|
||||||
|
|
||||||
# TODO: report bug for assert @
|
|
||||||
# /home/goodboy/repos/piker/env/lib/python3.8/site-packages/numba/core/typing/builtins.py:991
|
|
||||||
for i, q in enumerate(data[start:], start):
|
|
||||||
|
|
||||||
# TODO: ask numba why this doesn't work..
|
|
||||||
# open, high, low, close, index = q[
|
|
||||||
# ['open', 'high', 'low', 'close', 'index']]
|
|
||||||
|
|
||||||
open = q['open']
|
|
||||||
high = q['high']
|
|
||||||
low = q['low']
|
|
||||||
close = q['close']
|
|
||||||
index = float64(q['index'])
|
|
||||||
|
|
||||||
istart = i * 6
|
|
||||||
istop = istart + 6
|
|
||||||
|
|
||||||
# x,y detail the 6 points which connect all vertexes of a ohlc bar
|
|
||||||
x[istart:istop] = (
|
|
||||||
index - bar_gap,
|
|
||||||
index,
|
|
||||||
index,
|
|
||||||
index,
|
|
||||||
index,
|
|
||||||
index + bar_gap,
|
|
||||||
)
|
|
||||||
y[istart:istop] = (
|
|
||||||
open,
|
|
||||||
open,
|
|
||||||
low,
|
|
||||||
high,
|
|
||||||
close,
|
|
||||||
close,
|
|
||||||
)
|
|
||||||
|
|
||||||
# specifies that the first edge is never connected to the
|
|
||||||
# prior bars last edge thus providing a small "gap"/"space"
|
|
||||||
# between bars determined by ``bar_gap``.
|
|
||||||
c[istart:istop] = (1, 1, 1, 1, 1, 0)
|
|
||||||
|
|
||||||
return x, y, c
|
|
||||||
|
|
||||||
|
|
||||||
def gen_ohlc_qpath(
|
|
||||||
r: Renderer,
|
|
||||||
data: np.ndarray,
|
|
||||||
array_key: str, # we ignore this
|
|
||||||
vr: tuple[int, int],
|
|
||||||
|
|
||||||
start: int = 0, # XXX: do we need this?
|
|
||||||
# 0.5 is no overlap between arms, 1.0 is full overlap
|
|
||||||
w: float = 0.43,
|
|
||||||
|
|
||||||
) -> QtGui.QPainterPath:
|
|
||||||
'''
|
|
||||||
More or less direct proxy to ``path_arrays_from_ohlc()``
|
|
||||||
but with closed in kwargs for line spacing.
|
|
||||||
|
|
||||||
'''
|
|
||||||
x, y, c = path_arrays_from_ohlc(
|
|
||||||
data,
|
|
||||||
start,
|
|
||||||
bar_gap=w,
|
|
||||||
)
|
|
||||||
return x, y, c
|
|
||||||
|
|
||||||
|
|
||||||
def ohlc_to_line(
|
|
||||||
ohlc_shm: ShmArray,
|
|
||||||
data_field: str,
|
|
||||||
fields: list[str] = ['open', 'high', 'low', 'close']
|
|
||||||
|
|
||||||
) -> tuple[
|
|
||||||
np.ndarray,
|
|
||||||
np.ndarray,
|
|
||||||
]:
|
|
||||||
'''
|
|
||||||
Convert an input struct-array holding OHLC samples into a pair of
|
|
||||||
flattened x, y arrays with the same size (datums wise) as the source
|
|
||||||
data.
|
|
||||||
|
|
||||||
'''
|
|
||||||
y_out = ohlc_shm.ustruct(fields)
|
|
||||||
first = ohlc_shm._first.value
|
|
||||||
last = ohlc_shm._last.value
|
|
||||||
|
|
||||||
# write pushed data to flattened copy
|
|
||||||
y_out[first:last] = rfn.structured_to_unstructured(
|
|
||||||
ohlc_shm.array[fields]
|
|
||||||
)
|
|
||||||
|
|
||||||
# generate an flat-interpolated x-domain
|
|
||||||
x_out = (
|
|
||||||
np.broadcast_to(
|
|
||||||
ohlc_shm._array['index'][:, None],
|
|
||||||
(
|
|
||||||
ohlc_shm._array.size,
|
|
||||||
# 4, # only ohlc
|
|
||||||
y_out.shape[1],
|
|
||||||
),
|
|
||||||
) + np.array([-0.5, 0, 0, 0.5])
|
|
||||||
)
|
|
||||||
assert y_out.any()
|
|
||||||
|
|
||||||
return (
|
|
||||||
x_out,
|
|
||||||
y_out,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def to_step_format(
|
|
||||||
shm: ShmArray,
|
|
||||||
data_field: str,
|
|
||||||
index_field: str = 'index',
|
|
||||||
|
|
||||||
) -> tuple[int, np.ndarray, np.ndarray]:
|
|
||||||
'''
|
|
||||||
Convert an input 1d shm array to a "step array" format
|
|
||||||
for use by path graphics generation.
|
|
||||||
|
|
||||||
'''
|
|
||||||
i = shm._array['index'].copy()
|
|
||||||
out = shm._array[data_field].copy()
|
|
||||||
|
|
||||||
x_out = np.broadcast_to(
|
|
||||||
i[:, None],
|
|
||||||
(i.size, 2),
|
|
||||||
) + np.array([-0.5, 0.5])
|
|
||||||
|
|
||||||
y_out = np.empty((len(out), 2), dtype=out.dtype)
|
|
||||||
y_out[:] = out[:, np.newaxis]
|
|
||||||
|
|
||||||
# start y at origin level
|
|
||||||
y_out[0, 0] = 0
|
|
||||||
return x_out, y_out
|
|
|
@ -15,13 +15,19 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Customization of ``pyqtgraph`` core routines to speed up our use mostly
|
Customization of ``pyqtgraph`` core routines and various types normally
|
||||||
based on not requiring "scentific precision" for pixel perfect view
|
for speedups.
|
||||||
transforms.
|
|
||||||
|
Generally, our does not require "scentific precision" for pixel perfect
|
||||||
|
view transforms.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
|
|
||||||
|
from ._axes import Axis
|
||||||
|
|
||||||
|
|
||||||
def invertQTransform(tr):
|
def invertQTransform(tr):
|
||||||
"""Return a QTransform that is the inverse of *tr*.
|
"""Return a QTransform that is the inverse of *tr*.
|
||||||
|
@ -46,3 +52,236 @@ def _do_overrides() -> None:
|
||||||
"""
|
"""
|
||||||
# we don't care about potential fp issues inside Qt
|
# we don't care about potential fp issues inside Qt
|
||||||
pg.functions.invertQTransform = invertQTransform
|
pg.functions.invertQTransform = invertQTransform
|
||||||
|
pg.PlotItem = PlotItem
|
||||||
|
|
||||||
|
# enable "QPainterPathPrivate for faster arrayToQPath" from
|
||||||
|
# https://github.com/pyqtgraph/pyqtgraph/pull/2324
|
||||||
|
pg.setConfigOption('enableExperimental', True)
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE: the below customized type contains all our changes on a method
|
||||||
|
# by method basis as per the diff:
|
||||||
|
# https://github.com/pyqtgraph/pyqtgraph/commit/8e60bc14234b6bec1369ff4192dbfb82f8682920#diff-a2b5865955d2ba703dbc4c35ff01aa761aa28d2aeaac5e68d24e338bc82fb5b1R500
|
||||||
|
|
||||||
|
class PlotItem(pg.PlotItem):
|
||||||
|
'''
|
||||||
|
Overrides for the core plot object mostly pertaining to overlayed
|
||||||
|
multi-view management as it relates to multi-axis managment.
|
||||||
|
|
||||||
|
This object is the combination of a ``ViewBox`` and multiple
|
||||||
|
``AxisItem``s and so far we've added additional functionality and
|
||||||
|
APIs for:
|
||||||
|
- removal of axes
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
From ``pyqtgraph`` super type docs:
|
||||||
|
- Manage placement of ViewBox, AxisItems, and LabelItems
|
||||||
|
- Create and manage a list of PlotDataItems displayed inside the
|
||||||
|
ViewBox
|
||||||
|
- Implement a context menu with commonly used display and analysis
|
||||||
|
options
|
||||||
|
|
||||||
|
'''
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
parent=None,
|
||||||
|
name=None,
|
||||||
|
labels=None,
|
||||||
|
title=None,
|
||||||
|
viewBox=None,
|
||||||
|
axisItems=None,
|
||||||
|
default_axes=['left', 'bottom'],
|
||||||
|
enableMenu=True,
|
||||||
|
**kargs
|
||||||
|
):
|
||||||
|
super().__init__(
|
||||||
|
parent=parent,
|
||||||
|
name=name,
|
||||||
|
labels=labels,
|
||||||
|
title=title,
|
||||||
|
viewBox=viewBox,
|
||||||
|
axisItems=axisItems,
|
||||||
|
# default_axes=default_axes,
|
||||||
|
enableMenu=enableMenu,
|
||||||
|
kargs=kargs,
|
||||||
|
)
|
||||||
|
self.name = name
|
||||||
|
self.chart_widget = None
|
||||||
|
# self.setAxisItems(
|
||||||
|
# axisItems,
|
||||||
|
# default_axes=default_axes,
|
||||||
|
# )
|
||||||
|
|
||||||
|
# NOTE: this is an entirely new method not in upstream.
|
||||||
|
def removeAxis(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
unlink: bool = True,
|
||||||
|
|
||||||
|
) -> Optional[pg.AxisItem]:
|
||||||
|
"""
|
||||||
|
Remove an axis from the contained axis items
|
||||||
|
by ```name: str```.
|
||||||
|
|
||||||
|
This means the axis graphics object will be removed
|
||||||
|
from the ``.layout: QGraphicsGridLayout`` as well as unlinked
|
||||||
|
from the underlying associated ``ViewBox``.
|
||||||
|
|
||||||
|
If the ``unlink: bool`` is set to ``False`` then the axis will
|
||||||
|
stay linked to its view and will only be removed from the
|
||||||
|
layoutonly be removed from the layout.
|
||||||
|
|
||||||
|
If no axis with ``name: str`` is found then this is a noop.
|
||||||
|
|
||||||
|
Return the axis instance that was removed.
|
||||||
|
|
||||||
|
"""
|
||||||
|
entry = self.axes.pop(name, None)
|
||||||
|
|
||||||
|
if not entry:
|
||||||
|
return
|
||||||
|
|
||||||
|
axis = entry['item']
|
||||||
|
self.layout.removeItem(axis)
|
||||||
|
axis.scene().removeItem(axis)
|
||||||
|
if unlink:
|
||||||
|
axis.unlinkFromView()
|
||||||
|
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
return axis
|
||||||
|
|
||||||
|
# Why do we need to always have all axes created?
|
||||||
|
#
|
||||||
|
# I don't understand this at all.
|
||||||
|
#
|
||||||
|
# Everything seems to work if you just always apply the
|
||||||
|
# set passed to this method **EXCEPT** for some super weird reason
|
||||||
|
# the view box geometry still computes as though the space for the
|
||||||
|
# `'bottom'` axis is always there **UNLESS** you always add that
|
||||||
|
# axis but hide it?
|
||||||
|
#
|
||||||
|
# Why in tf would this be the case!?!?
|
||||||
|
def setAxisItems(
|
||||||
|
self,
|
||||||
|
# XXX: yeah yeah, i know we can't use type annots like this yet.
|
||||||
|
axisItems: Optional[dict[str, pg.AxisItem]] = None,
|
||||||
|
add_to_layout: bool = True,
|
||||||
|
default_axes: list[str] = ['left', 'bottom'],
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Override axis item setting to only
|
||||||
|
|
||||||
|
"""
|
||||||
|
axisItems = axisItems or {}
|
||||||
|
|
||||||
|
# XXX: wth is is this even saying?!?
|
||||||
|
# Array containing visible axis items
|
||||||
|
# Also containing potentially hidden axes, but they are not
|
||||||
|
# touched so it does not matter
|
||||||
|
# visibleAxes = ['left', 'bottom']
|
||||||
|
# Note that it does not matter that this adds
|
||||||
|
# some values to visibleAxes a second time
|
||||||
|
|
||||||
|
# XXX: uhhh wat^ ..?
|
||||||
|
|
||||||
|
visibleAxes = list(default_axes) + list(axisItems.keys())
|
||||||
|
|
||||||
|
# TODO: we should probably invert the loop here to not loop the
|
||||||
|
# predefined "axis name set" and instead loop the `axisItems`
|
||||||
|
# input and lookup indices from a predefined map.
|
||||||
|
for name, pos in (
|
||||||
|
('top', (1, 1)),
|
||||||
|
('bottom', (3, 1)),
|
||||||
|
('left', (2, 0)),
|
||||||
|
('right', (2, 2))
|
||||||
|
):
|
||||||
|
if (
|
||||||
|
name in self.axes and
|
||||||
|
name in axisItems
|
||||||
|
):
|
||||||
|
# we already have an axis entry for this name
|
||||||
|
# so remove the existing entry.
|
||||||
|
self.removeAxis(name)
|
||||||
|
|
||||||
|
# elif name not in axisItems:
|
||||||
|
# # this axis entry is not provided in this call
|
||||||
|
# # so remove any old/existing entry.
|
||||||
|
# self.removeAxis(name)
|
||||||
|
|
||||||
|
# Create new axis
|
||||||
|
if name in axisItems:
|
||||||
|
axis = axisItems[name]
|
||||||
|
if axis.scene() is not None:
|
||||||
|
if (
|
||||||
|
name not in self.axes
|
||||||
|
or axis != self.axes[name]["item"]
|
||||||
|
):
|
||||||
|
raise RuntimeError(
|
||||||
|
"Can't add an axis to multiple plots. Shared axes"
|
||||||
|
" can be achieved with multiple AxisItem instances"
|
||||||
|
" and set[X/Y]Link.")
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Set up new axis
|
||||||
|
|
||||||
|
# XXX: ok but why do we want to add axes for all entries
|
||||||
|
# if not desired by the user? The only reason I can see
|
||||||
|
# adding this is without it there's some weird
|
||||||
|
# ``ViewBox`` geometry bug.. where a gap for the
|
||||||
|
# 'bottom' axis is somehow left in?
|
||||||
|
# axis = pg.AxisItem(orientation=name, parent=self)
|
||||||
|
axis = Axis(
|
||||||
|
self,
|
||||||
|
orientation=name,
|
||||||
|
parent=self,
|
||||||
|
)
|
||||||
|
|
||||||
|
axis.linkToView(self.vb)
|
||||||
|
|
||||||
|
# XXX: shouldn't you already know the ``pos`` from the name?
|
||||||
|
# Oh right instead of a global map that would let you
|
||||||
|
# reasily look that up it's redefined over and over and over
|
||||||
|
# again in methods..
|
||||||
|
self.axes[name] = {'item': axis, 'pos': pos}
|
||||||
|
|
||||||
|
# NOTE: in the overlay case the axis may be added to some
|
||||||
|
# other layout and should not be added here.
|
||||||
|
if add_to_layout:
|
||||||
|
self.layout.addItem(axis, *pos)
|
||||||
|
|
||||||
|
# place axis above images at z=0, items that want to draw
|
||||||
|
# over the axes should be placed at z>=1:
|
||||||
|
axis.setZValue(0.5)
|
||||||
|
axis.setFlag(
|
||||||
|
axis.GraphicsItemFlag.ItemNegativeZStacksBehindParent
|
||||||
|
)
|
||||||
|
if name in visibleAxes:
|
||||||
|
self.showAxis(name, True)
|
||||||
|
else:
|
||||||
|
# why do we need to insert all axes to ``.axes`` and
|
||||||
|
# only hide the ones the user doesn't specify? It all
|
||||||
|
# seems to work fine without doing this except for this
|
||||||
|
# weird gap for the 'bottom' axis that always shows up
|
||||||
|
# in the view box geometry??
|
||||||
|
self.hideAxis(name)
|
||||||
|
|
||||||
|
def updateGrid(
|
||||||
|
self,
|
||||||
|
*args,
|
||||||
|
):
|
||||||
|
alpha = self.ctrl.gridAlphaSlider.value()
|
||||||
|
x = alpha if self.ctrl.xGridCheck.isChecked() else False
|
||||||
|
y = alpha if self.ctrl.yGridCheck.isChecked() else False
|
||||||
|
for name, dim in (
|
||||||
|
('top', x),
|
||||||
|
('bottom', x),
|
||||||
|
('left', y),
|
||||||
|
('right', y)
|
||||||
|
):
|
||||||
|
if name in self.axes:
|
||||||
|
self.getAxis(name).setGrid(dim)
|
||||||
|
# self.getAxis('bottom').setGrid(x)
|
||||||
|
# self.getAxis('left').setGrid(y)
|
||||||
|
# self.getAxis('right').setGrid(y)
|
||||||
|
|
|
@ -19,10 +19,15 @@ Position info and display
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from copy import copy
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from math import floor, copysign
|
from math import floor, copysign
|
||||||
from typing import Optional
|
from typing import (
|
||||||
|
Callable,
|
||||||
|
Optional,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# from PyQt5.QtWidgets import QStyle
|
# from PyQt5.QtWidgets import QStyle
|
||||||
|
@ -36,37 +41,52 @@ from ._anchors import (
|
||||||
pp_tight_and_right, # wanna keep it straight in the long run
|
pp_tight_and_right, # wanna keep it straight in the long run
|
||||||
gpath_pin,
|
gpath_pin,
|
||||||
)
|
)
|
||||||
from ..calc import humanize, pnl, puterize
|
from ..calc import (
|
||||||
from ..clearing._allocate import Allocator, Position
|
humanize,
|
||||||
|
pnl,
|
||||||
|
puterize,
|
||||||
|
)
|
||||||
|
from ..clearing._allocate import Allocator
|
||||||
|
from ..pp import Position
|
||||||
from ..data._normalize import iterticks
|
from ..data._normalize import iterticks
|
||||||
from ..data.feed import Feed
|
from ..data.feed import (
|
||||||
|
Feed,
|
||||||
|
Flume,
|
||||||
|
)
|
||||||
|
from ..data.types import Struct
|
||||||
from ._label import Label
|
from ._label import Label
|
||||||
from ._lines import LevelLine, order_line
|
from ._lines import LevelLine, order_line
|
||||||
from ._style import _font
|
from ._style import _font
|
||||||
from ._forms import FieldsForm, FillStatusBar, QLabel
|
from ._forms import FieldsForm, FillStatusBar, QLabel
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._chart import (
|
||||||
|
ChartPlotWidget,
|
||||||
|
)
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
_pnl_tasks: dict[str, bool] = {}
|
_pnl_tasks: dict[str, bool] = {}
|
||||||
|
|
||||||
|
|
||||||
async def update_pnl_from_feed(
|
async def update_pnl_from_feed(
|
||||||
|
|
||||||
feed: Feed,
|
flume: Flume,
|
||||||
order_mode: OrderMode, # noqa
|
order_mode: OrderMode, # noqa
|
||||||
tracker: PositionTracker,
|
tracker: PositionTracker,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Real-time display the current pp's PnL in the appropriate label.
|
'''
|
||||||
|
Real-time display the current pp's PnL in the appropriate label.
|
||||||
|
|
||||||
``ValueError`` if this task is spawned where there is a net-zero pp.
|
``ValueError`` if this task is spawned where there is a net-zero pp.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
global _pnl_tasks
|
global _pnl_tasks
|
||||||
|
|
||||||
pp = order_mode.current_pp
|
pp: PositionTracker = order_mode.current_pp
|
||||||
live = pp.live_pp
|
live: Position = pp.live_pp
|
||||||
key = live.symbol.key
|
key: str = live.symbol.front_fqsn()
|
||||||
|
|
||||||
log.info(f'Starting pnl display for {pp.alloc.account}')
|
log.info(f'Starting pnl display for {pp.alloc.account}')
|
||||||
|
|
||||||
|
@ -82,14 +102,25 @@ async def update_pnl_from_feed(
|
||||||
|
|
||||||
# real-time update pnl on the status pane
|
# real-time update pnl on the status pane
|
||||||
try:
|
try:
|
||||||
async with feed.stream.subscribe() as bstream:
|
async with flume.stream.subscribe() as bstream:
|
||||||
# last_tick = time.time()
|
# last_tick = time.time()
|
||||||
async for quotes in bstream:
|
async for quotes in bstream:
|
||||||
|
|
||||||
# now = time.time()
|
# now = time.time()
|
||||||
# period = now - last_tick
|
# period = now - last_tick
|
||||||
|
|
||||||
for sym, quote in quotes.items():
|
for sym, quote in quotes.items():
|
||||||
|
# print(f'{key} PnL: sym:{sym}')
|
||||||
|
|
||||||
|
# TODO: uggggh we probably want a better state
|
||||||
|
# management then this sincce we want to enable
|
||||||
|
# updating whatever the current symbol is in
|
||||||
|
# real-time right?
|
||||||
|
if sym != key:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# watch out for wrong quote msg-data if you muck
|
||||||
|
# with backend feed subs code..
|
||||||
|
# assert sym == quote['fqsn']
|
||||||
|
|
||||||
for tick in iterticks(quote, types):
|
for tick in iterticks(quote, types):
|
||||||
# print(f'{1/period} Hz')
|
# print(f'{1/period} Hz')
|
||||||
|
@ -103,13 +134,17 @@ async def update_pnl_from_feed(
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# compute and display pnl status
|
# compute and display pnl status
|
||||||
order_mode.pane.pnl_label.format(
|
pnl_val = (
|
||||||
pnl=copysign(1, size) * pnl(
|
copysign(1, size)
|
||||||
# live.avg_price,
|
*
|
||||||
order_mode.current_pp.live_pp.avg_price,
|
pnl(
|
||||||
|
# live.ppu,
|
||||||
|
order_mode.current_pp.live_pp.ppu,
|
||||||
tick['price'],
|
tick['price'],
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
# print(f'formatting PNL {sym} => {pnl_val}')
|
||||||
|
order_mode.pane.pnl_label.format(pnl=pnl_val)
|
||||||
|
|
||||||
# last_tick = time.time()
|
# last_tick = time.time()
|
||||||
finally:
|
finally:
|
||||||
|
@ -165,19 +200,36 @@ class SettingsPane:
|
||||||
key: str,
|
key: str,
|
||||||
value: str,
|
value: str,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Try to apply some input setting (by the user), revert to
|
||||||
|
previous setting if it fails display new value if applied.
|
||||||
|
|
||||||
|
'''
|
||||||
|
self.apply_setting(key, value)
|
||||||
|
self.update_status_ui(self.order_mode.current_pp)
|
||||||
|
|
||||||
|
def apply_setting(
|
||||||
|
self,
|
||||||
|
|
||||||
|
key: str,
|
||||||
|
value: str,
|
||||||
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
'''
|
'''
|
||||||
Called on any order pane edit field value change.
|
Called on any order pane edit field value change.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
mode = self.order_mode
|
mode = self.order_mode
|
||||||
|
tracker = mode.current_pp
|
||||||
|
alloc = tracker.alloc
|
||||||
|
|
||||||
# an account switch request
|
# an account switch request
|
||||||
if key == 'account':
|
if key == 'account':
|
||||||
|
|
||||||
# hide details on the old selection
|
# hide details on the old selection
|
||||||
old_tracker = mode.current_pp
|
old_tracker = mode.current_pp
|
||||||
old_tracker.hide_info()
|
old_tracker.nav.hide_info()
|
||||||
|
|
||||||
# re-assign the order mode tracker
|
# re-assign the order mode tracker
|
||||||
account_name = value
|
account_name = value
|
||||||
|
@ -187,7 +239,7 @@ class SettingsPane:
|
||||||
# a ``brokerd`) then error and switch back to the last
|
# a ``brokerd`) then error and switch back to the last
|
||||||
# selection.
|
# selection.
|
||||||
if tracker is None:
|
if tracker is None:
|
||||||
sym = old_tracker.chart.linked.symbol.key
|
sym = old_tracker.charts[0].linked.symbol.key
|
||||||
log.error(
|
log.error(
|
||||||
f'Account `{account_name}` can not be set for {sym}'
|
f'Account `{account_name}` can not be set for {sym}'
|
||||||
)
|
)
|
||||||
|
@ -198,39 +250,44 @@ class SettingsPane:
|
||||||
self.order_mode.current_pp = tracker
|
self.order_mode.current_pp = tracker
|
||||||
assert tracker.alloc.account == account_name
|
assert tracker.alloc.account == account_name
|
||||||
self.form.fields['account'].setCurrentText(account_name)
|
self.form.fields['account'].setCurrentText(account_name)
|
||||||
tracker.show()
|
tracker.nav.show()
|
||||||
tracker.hide_info()
|
tracker.nav.hide_info()
|
||||||
|
|
||||||
self.display_pnl(tracker)
|
self.display_pnl(tracker)
|
||||||
|
|
||||||
# load the new account's allocator
|
# load the new account's allocator
|
||||||
alloc = tracker.alloc
|
alloc = tracker.alloc
|
||||||
|
|
||||||
else:
|
|
||||||
tracker = mode.current_pp
|
|
||||||
alloc = tracker.alloc
|
|
||||||
|
|
||||||
size_unit = alloc.size_unit
|
|
||||||
|
|
||||||
# WRITE any settings to current pp's allocator
|
# WRITE any settings to current pp's allocator
|
||||||
try:
|
|
||||||
if key == 'size_unit':
|
if key == 'size_unit':
|
||||||
# implicit re-write of value if input
|
# implicit re-write of value if input
|
||||||
# is the "text name" of the units.
|
# is the "text name" of the units.
|
||||||
# yah yah, i know this is badd..
|
# yah yah, i know this is badd..
|
||||||
alloc.size_unit = value
|
alloc.size_unit = value
|
||||||
else:
|
|
||||||
|
elif key != 'account': # numeric fields entry
|
||||||
|
try:
|
||||||
value = puterize(value)
|
value = puterize(value)
|
||||||
|
except ValueError as err:
|
||||||
|
log.error(err.args[0])
|
||||||
|
return False
|
||||||
|
|
||||||
if key == 'limit':
|
if key == 'limit':
|
||||||
|
if value <= 0:
|
||||||
|
log.error('limit must be > 0')
|
||||||
|
return False
|
||||||
|
|
||||||
pp = mode.current_pp.live_pp
|
pp = mode.current_pp.live_pp
|
||||||
|
|
||||||
if size_unit == 'currency':
|
if alloc.size_unit == 'currency':
|
||||||
dsize = pp.dsize
|
dsize = pp.dsize
|
||||||
if dsize > value:
|
if dsize > value:
|
||||||
log.error(
|
log.error(
|
||||||
f'limit must > then current pp: {dsize}'
|
f'limit must > then current pp: {dsize}'
|
||||||
)
|
)
|
||||||
raise ValueError
|
# reset position size value
|
||||||
|
alloc.currency_limit = dsize
|
||||||
|
return False
|
||||||
|
|
||||||
alloc.currency_limit = value
|
alloc.currency_limit = value
|
||||||
|
|
||||||
|
@ -246,30 +303,50 @@ class SettingsPane:
|
||||||
|
|
||||||
elif key == 'slots':
|
elif key == 'slots':
|
||||||
if value <= 0:
|
if value <= 0:
|
||||||
raise ValueError('slots must be > 0')
|
# raise ValueError('slots must be > 0')
|
||||||
|
log.error('limit must be > 0')
|
||||||
|
return False
|
||||||
|
|
||||||
alloc.slots = int(value)
|
alloc.slots = int(value)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
log.error(f'Unknown setting {key}')
|
log.error(f'Unknown setting {key}')
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
log.info(f'settings change: {key}: {value}')
|
# don't log account "change" case since it'll be submitted
|
||||||
|
# on every mouse interaction.
|
||||||
|
log.runtime(f'settings change: {key}: {value}')
|
||||||
|
|
||||||
except ValueError:
|
# TODO: maybe return a diff of settings so if we can an error we
|
||||||
log.error(f'Invalid value for `{key}`: {value}')
|
# can have general input handling code to report it through the
|
||||||
|
# UI in some way?
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_status_ui(
|
||||||
|
self,
|
||||||
|
tracker: PositionTracker,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
alloc = tracker.alloc
|
||||||
|
slots = alloc.slots
|
||||||
|
used = alloc.slots_used(tracker.live_pp)
|
||||||
|
size = tracker.live_pp.size
|
||||||
|
dsize = tracker.live_pp.dsize
|
||||||
|
|
||||||
# READ out settings and update the status UI / settings widgets
|
# READ out settings and update the status UI / settings widgets
|
||||||
suffix = {'currency': ' $', 'units': ' u'}[size_unit]
|
suffix = {'currency': ' $', 'units': ' u'}[alloc.size_unit]
|
||||||
limit = alloc.limit()
|
size_unit, limit = alloc.limit_info()
|
||||||
|
|
||||||
# TODO: a reverse look up from the position to the equivalent
|
|
||||||
# account(s), if none then look to user config for default?
|
|
||||||
self.update_status_ui(pp=tracker)
|
|
||||||
|
|
||||||
step_size, currency_per_slot = alloc.step_sizes()
|
step_size, currency_per_slot = alloc.step_sizes()
|
||||||
|
|
||||||
if size_unit == 'currency':
|
if alloc.size_unit == 'currency':
|
||||||
step_size = currency_per_slot
|
step_size = currency_per_slot
|
||||||
|
if dsize >= limit:
|
||||||
|
self.apply_setting('limit', limit)
|
||||||
|
|
||||||
|
elif size >= limit:
|
||||||
|
self.apply_setting('limit', limit)
|
||||||
|
|
||||||
self.step_label.format(
|
self.step_label.format(
|
||||||
step_size=str(humanize(step_size)) + suffix
|
step_size=str(humanize(step_size)) + suffix
|
||||||
|
@ -288,22 +365,6 @@ class SettingsPane:
|
||||||
# update of level marker size label based on any new settings
|
# update of level marker size label based on any new settings
|
||||||
tracker.update_from_pp()
|
tracker.update_from_pp()
|
||||||
|
|
||||||
# TODO: maybe return a diff of settings so if we can an error we
|
|
||||||
# can have general input handling code to report it through the
|
|
||||||
# UI in some way?
|
|
||||||
return True
|
|
||||||
|
|
||||||
def update_status_ui(
|
|
||||||
self,
|
|
||||||
|
|
||||||
pp: PositionTracker,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
alloc = pp.alloc
|
|
||||||
slots = alloc.slots
|
|
||||||
used = alloc.slots_used(pp.live_pp)
|
|
||||||
|
|
||||||
# calculate proportion of position size limit
|
# calculate proportion of position size limit
|
||||||
# that exists and display in fill bar
|
# that exists and display in fill bar
|
||||||
# TODO: what should we do for fractional slot pps?
|
# TODO: what should we do for fractional slot pps?
|
||||||
|
@ -314,7 +375,7 @@ class SettingsPane:
|
||||||
# min(round(prop * slots), slots)
|
# min(round(prop * slots), slots)
|
||||||
min(used, slots)
|
min(used, slots)
|
||||||
)
|
)
|
||||||
self.update_account_icons({alloc.account: pp.live_pp})
|
self.update_account_icons({alloc.account: tracker.live_pp})
|
||||||
|
|
||||||
def update_account_icons(
|
def update_account_icons(
|
||||||
self,
|
self,
|
||||||
|
@ -340,7 +401,9 @@ class SettingsPane:
|
||||||
tracker: PositionTracker,
|
tracker: PositionTracker,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Display the PnL for the current symbol and personal positioning (pp).
|
'''
|
||||||
|
Display the PnL for the current symbol and personal positioning
|
||||||
|
(pp).
|
||||||
|
|
||||||
If a position is open start a background task which will
|
If a position is open start a background task which will
|
||||||
real-time update the pnl label in the settings pane.
|
real-time update the pnl label in the settings pane.
|
||||||
|
@ -349,24 +412,25 @@ class SettingsPane:
|
||||||
mode = self.order_mode
|
mode = self.order_mode
|
||||||
sym = mode.chart.linked.symbol
|
sym = mode.chart.linked.symbol
|
||||||
size = tracker.live_pp.size
|
size = tracker.live_pp.size
|
||||||
feed = mode.quote_feed
|
flume: Feed = mode.feed.flumes[sym.fqsn]
|
||||||
pnl_value = 0
|
pnl_value = 0
|
||||||
|
|
||||||
if size:
|
if size:
|
||||||
# last historical close price
|
# last historical close price
|
||||||
last = feed.shm.array[-1][['close']][0]
|
last = flume.rt_shm.array[-1][['close']][0]
|
||||||
pnl_value = copysign(1, size) * pnl(
|
pnl_value = copysign(1, size) * pnl(
|
||||||
tracker.live_pp.avg_price,
|
tracker.live_pp.ppu,
|
||||||
last,
|
last,
|
||||||
)
|
)
|
||||||
|
|
||||||
# maybe start update task
|
# maybe start update task
|
||||||
global _pnl_tasks
|
global _pnl_tasks
|
||||||
if sym.key not in _pnl_tasks:
|
fqsn = sym.front_fqsn()
|
||||||
_pnl_tasks[sym.key] = True
|
if fqsn not in _pnl_tasks:
|
||||||
|
_pnl_tasks[fqsn] = True
|
||||||
self.order_mode.nursery.start_soon(
|
self.order_mode.nursery.start_soon(
|
||||||
update_pnl_from_feed,
|
update_pnl_from_feed,
|
||||||
feed,
|
flume,
|
||||||
mode,
|
mode,
|
||||||
tracker,
|
tracker,
|
||||||
)
|
)
|
||||||
|
@ -375,15 +439,15 @@ class SettingsPane:
|
||||||
self.pnl_label.format(pnl=pnl_value)
|
self.pnl_label.format(pnl=pnl_value)
|
||||||
|
|
||||||
|
|
||||||
def position_line(
|
def pp_line(
|
||||||
|
|
||||||
chart: 'ChartPlotWidget', # noqa
|
chart: ChartPlotWidget, # noqa
|
||||||
size: float,
|
size: float,
|
||||||
level: float,
|
level: float,
|
||||||
color: str,
|
color: str,
|
||||||
|
marker: LevelMarker,
|
||||||
|
|
||||||
orient_v: str = 'bottom',
|
orient_v: str = 'bottom',
|
||||||
marker: Optional[LevelMarker] = None,
|
|
||||||
|
|
||||||
) -> LevelLine:
|
) -> LevelLine:
|
||||||
'''
|
'''
|
||||||
|
@ -414,16 +478,7 @@ def position_line(
|
||||||
show_markers=False,
|
show_markers=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
if marker:
|
# TODO: use `LevelLine.add_marker()`` for this instead?
|
||||||
# configure marker to position data
|
|
||||||
|
|
||||||
if size > 0: # long
|
|
||||||
style = '|<' # point "up to" the line
|
|
||||||
elif size < 0: # short
|
|
||||||
style = '>|' # point "down to" the line
|
|
||||||
|
|
||||||
marker.style = style
|
|
||||||
|
|
||||||
# set marker color to same as line
|
# set marker color to same as line
|
||||||
marker.setPen(line.currentPen)
|
marker.setPen(line.currentPen)
|
||||||
marker.setBrush(fn.mkBrush(line.currentPen.color()))
|
marker.setBrush(fn.mkBrush(line.currentPen.color()))
|
||||||
|
@ -431,77 +486,331 @@ def position_line(
|
||||||
marker.update()
|
marker.update()
|
||||||
marker.show()
|
marker.show()
|
||||||
|
|
||||||
|
line._marker = marker
|
||||||
|
line.track_marker_pos = True
|
||||||
|
|
||||||
# show position marker on view "edge" when out of view
|
# show position marker on view "edge" when out of view
|
||||||
vb = line.getViewBox()
|
vb = line.getViewBox()
|
||||||
vb.sigRangeChanged.connect(marker.position_in_view)
|
vb.sigRangeChanged.connect(marker.position_in_view)
|
||||||
|
|
||||||
line.set_level(level)
|
|
||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
_derivs = (
|
||||||
|
'future',
|
||||||
|
'continuous_future',
|
||||||
|
'option',
|
||||||
|
'futures_option',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: move into annoate module?
|
||||||
|
def mk_level_marker(
|
||||||
|
chart: ChartPlotWidget,
|
||||||
|
size: float,
|
||||||
|
level: float,
|
||||||
|
on_paint: Callable,
|
||||||
|
|
||||||
|
) -> LevelMarker:
|
||||||
|
'''
|
||||||
|
Allocate and return nan arrow graphics element.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# scale marker size with dpi-aware font size
|
||||||
|
font_size = _font.font.pixelSize()
|
||||||
|
arrow_size = floor(1.375 * font_size)
|
||||||
|
arrow = LevelMarker(
|
||||||
|
chart=chart,
|
||||||
|
style='|<', # actual style is set by caller based on size
|
||||||
|
get_level=level,
|
||||||
|
size=arrow_size,
|
||||||
|
on_paint=on_paint,
|
||||||
|
)
|
||||||
|
arrow.show()
|
||||||
|
return arrow
|
||||||
|
|
||||||
|
|
||||||
|
class Nav(Struct):
|
||||||
|
'''
|
||||||
|
Composite for holding a set of charts and respective (by order)
|
||||||
|
graphics-elements which display position information acting as sort
|
||||||
|
of "navigation" system for a position.
|
||||||
|
|
||||||
|
'''
|
||||||
|
charts: dict[int, ChartPlotWidget]
|
||||||
|
pp_labels: dict[str, Label] = {}
|
||||||
|
size_labels: dict[str, Label] = {}
|
||||||
|
lines: dict[str, Optional[LevelLine]] = {}
|
||||||
|
level_markers: dict[str, Optional[LevelMarker]] = {}
|
||||||
|
color: str = 'default_lightest'
|
||||||
|
|
||||||
|
def update_ui(
|
||||||
|
self,
|
||||||
|
account: str,
|
||||||
|
price: float,
|
||||||
|
size: float,
|
||||||
|
slots_used: float,
|
||||||
|
size_digits: Optional[int] = None,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Update personal position level line.
|
||||||
|
|
||||||
|
'''
|
||||||
|
for key, chart in self.charts.items():
|
||||||
|
size_digits = size_digits or chart.linked.symbol.lot_size_digits
|
||||||
|
line = self.lines.get(key)
|
||||||
|
level_marker = self.level_markers[key]
|
||||||
|
pp_label = self.pp_labels[key]
|
||||||
|
|
||||||
|
if size:
|
||||||
|
# create and show a pp line if none yet exists
|
||||||
|
if line is None:
|
||||||
|
arrow = self.level_markers[key]
|
||||||
|
line = pp_line(
|
||||||
|
chart=chart,
|
||||||
|
level=price,
|
||||||
|
size=size,
|
||||||
|
color=self.color,
|
||||||
|
marker=arrow,
|
||||||
|
)
|
||||||
|
self.lines[key] = line
|
||||||
|
|
||||||
|
# modify existing indicator line
|
||||||
|
line.set_level(price)
|
||||||
|
|
||||||
|
# update LHS sizing label
|
||||||
|
line.update_labels({
|
||||||
|
'size': size,
|
||||||
|
'size_digits': size_digits,
|
||||||
|
'fiat_size': round(price * size, ndigits=2),
|
||||||
|
|
||||||
|
# TODO: per account lines on a single (or very
|
||||||
|
# related) symbol
|
||||||
|
'account': account,
|
||||||
|
})
|
||||||
|
line.show()
|
||||||
|
|
||||||
|
# always show arrow-marker when a non-zero
|
||||||
|
# pos size.
|
||||||
|
level_marker.show()
|
||||||
|
|
||||||
|
# configure marker to position data
|
||||||
|
if size > 0: # long
|
||||||
|
# point "up to" the line
|
||||||
|
level_marker.style = '|<'
|
||||||
|
|
||||||
|
elif size < 0: # short
|
||||||
|
# point "down to" the line
|
||||||
|
level_marker.style = '>|'
|
||||||
|
|
||||||
|
# remove line from view for a net-zero pos
|
||||||
|
else:
|
||||||
|
self.hide()
|
||||||
|
|
||||||
|
# label updates
|
||||||
|
size_label = self.size_labels[key]
|
||||||
|
size_label.fields['slots_used'] = slots_used
|
||||||
|
size_label.render()
|
||||||
|
|
||||||
|
# set arrow marker to correct level
|
||||||
|
level_marker.level = price
|
||||||
|
|
||||||
|
# these updates are critical to avoid lag on view/scene changes
|
||||||
|
# TODO: couldn't we integrate this into
|
||||||
|
# a ``.inter_ui_elements_and_update()``?
|
||||||
|
level_marker.update() # trigger paint
|
||||||
|
pp_label.update()
|
||||||
|
size_label.update()
|
||||||
|
|
||||||
|
def level(self) -> float:
|
||||||
|
'''
|
||||||
|
Return the "level" value from the underlying ``LevelLine`` which tracks
|
||||||
|
the "average position" price defined the represented position instance.
|
||||||
|
|
||||||
|
'''
|
||||||
|
if self.lines:
|
||||||
|
for key, line in self.lines.items():
|
||||||
|
if line:
|
||||||
|
return line.value()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def iter_ui_elements(self) -> tuple[
|
||||||
|
Label,
|
||||||
|
Label,
|
||||||
|
LevelLine,
|
||||||
|
LevelMarker,
|
||||||
|
]:
|
||||||
|
for key, chart in self.charts.items():
|
||||||
|
yield (
|
||||||
|
self.pp_labels[key],
|
||||||
|
self.size_labels[key],
|
||||||
|
self.lines.get(key),
|
||||||
|
self.level_markers[key],
|
||||||
|
)
|
||||||
|
|
||||||
|
def show(self) -> None:
|
||||||
|
'''
|
||||||
|
Show all UI elements on all managed charts.
|
||||||
|
|
||||||
|
'''
|
||||||
|
for (
|
||||||
|
pp_label,
|
||||||
|
size_label,
|
||||||
|
line,
|
||||||
|
level_marker,
|
||||||
|
) in self.iter_ui_elements():
|
||||||
|
|
||||||
|
# NOTE: be sure to re-trigger arrow/label placement in case
|
||||||
|
# a new sidepane or other widget (like the search bar) was
|
||||||
|
# dynamically swapped into the chart-row-widget-space in
|
||||||
|
# which case we want to reposition in the view but including
|
||||||
|
# the new x-distance added by that sidepane. See details in
|
||||||
|
# ``LevelMarker.position_in_view()`` but more less ``.
|
||||||
|
# ``ChartPlotWidget.self.marker_right_points()`` gets called
|
||||||
|
# which itself eventually calls `.getAxis.pos().x()` and
|
||||||
|
# it's THIS that needs to be called **AFTER** the sidepane
|
||||||
|
# has been added..
|
||||||
|
level_marker.show()
|
||||||
|
level_marker.position_in_view()
|
||||||
|
|
||||||
|
# labels
|
||||||
|
pp_label.show()
|
||||||
|
size_label.show()
|
||||||
|
|
||||||
|
if line:
|
||||||
|
line.show()
|
||||||
|
line.show_labels()
|
||||||
|
|
||||||
|
def hide(self) -> None:
|
||||||
|
for (
|
||||||
|
pp_label,
|
||||||
|
size_label,
|
||||||
|
line,
|
||||||
|
level_marker,
|
||||||
|
) in self.iter_ui_elements():
|
||||||
|
pp_label.hide()
|
||||||
|
level_marker.hide()
|
||||||
|
size_label.hide()
|
||||||
|
if line:
|
||||||
|
line.hide()
|
||||||
|
|
||||||
|
def update_graphics(
|
||||||
|
self,
|
||||||
|
marker: LevelMarker,
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Update all labels callback.
|
||||||
|
|
||||||
|
Meant to be called from the marker ``.paint()``
|
||||||
|
for immediate, lag free label draws.
|
||||||
|
|
||||||
|
'''
|
||||||
|
for (
|
||||||
|
pp_label,
|
||||||
|
size_label,
|
||||||
|
line,
|
||||||
|
level_marker,
|
||||||
|
) in self.iter_ui_elements():
|
||||||
|
|
||||||
|
pp_label.update()
|
||||||
|
size_label.update()
|
||||||
|
|
||||||
|
# XXX: can't call this because it causes a recursive paint/render
|
||||||
|
# level_marker.update()
|
||||||
|
|
||||||
|
def hide_info(self) -> None:
|
||||||
|
'''
|
||||||
|
Hide details (just size label?) of position nav elements.
|
||||||
|
|
||||||
|
'''
|
||||||
|
for (
|
||||||
|
pp_label,
|
||||||
|
size_label,
|
||||||
|
line,
|
||||||
|
level_marker,
|
||||||
|
) in self.iter_ui_elements():
|
||||||
|
|
||||||
|
size_label.hide()
|
||||||
|
if line:
|
||||||
|
line.hide_labels()
|
||||||
|
|
||||||
|
|
||||||
class PositionTracker:
|
class PositionTracker:
|
||||||
'''
|
'''
|
||||||
Track and display real-time positions for a single symbol
|
Track and display real-time positions for a single asset-symbol
|
||||||
over multiple accounts on a single chart.
|
held in a single account, normally shown on a single chart.
|
||||||
|
|
||||||
Graphically composed of a level line and marker as well as labels
|
Graphically composed of a level line and marker as well as labels
|
||||||
for indcating current position information. Updates are made to the
|
for indcating current position information. Updates are made to the
|
||||||
corresponding "settings pane" for the chart's "order mode" UX.
|
corresponding "settings pane" for the chart's "order mode" UX.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# inputs
|
|
||||||
chart: 'ChartPlotWidget' # noqa
|
|
||||||
|
|
||||||
alloc: Allocator
|
alloc: Allocator
|
||||||
startup_pp: Position
|
startup_pp: Position
|
||||||
live_pp: Position
|
live_pp: Position
|
||||||
|
nav: Nav # holds all UI elements across all charts
|
||||||
# allocated
|
|
||||||
pp_label: Label
|
|
||||||
size_label: Label
|
|
||||||
line: Optional[LevelLine] = None
|
|
||||||
|
|
||||||
_color: str = 'default_lightest'
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
chart: 'ChartPlotWidget', # noqa
|
charts: list[ChartPlotWidget],
|
||||||
alloc: Allocator,
|
alloc: Allocator,
|
||||||
startup_pp: Position,
|
startup_pp: Position,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
self.chart = chart
|
nav = self.nav = Nav(charts={id(chart): chart for chart in charts})
|
||||||
|
|
||||||
self.alloc = alloc
|
self.alloc = alloc
|
||||||
self.startup_pp = startup_pp
|
self.startup_pp = startup_pp
|
||||||
self.live_pp = startup_pp.copy()
|
self.live_pp = copy(startup_pp)
|
||||||
|
|
||||||
|
# TODO: maybe add this as a method ``Nav.add_chart()``
|
||||||
|
# init all UI elements
|
||||||
|
for key, chart in nav.charts.items():
|
||||||
view = chart.getViewBox()
|
view = chart.getViewBox()
|
||||||
|
|
||||||
# literally the 'pp' (pee pee) label that's always in view
|
arrow = mk_level_marker(
|
||||||
self.pp_label = pp_label = Label(
|
chart=chart,
|
||||||
|
size=1,
|
||||||
|
level=nav.level,
|
||||||
|
on_paint=nav.update_graphics,
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: we really need some kinda "spacing" manager for all
|
||||||
|
# this stuff...
|
||||||
|
def offset_from_yaxis() -> float:
|
||||||
|
'''
|
||||||
|
If no L1 labels are present beside the x-axis place
|
||||||
|
the line label offset from the y-axis just enough to avoid
|
||||||
|
label overlap with any sticky labels.
|
||||||
|
|
||||||
|
'''
|
||||||
|
x = chart.marker_right_points()[1]
|
||||||
|
if chart._max_l1_line_len == 0:
|
||||||
|
mkw = pp_label.txt.boundingRect().width()
|
||||||
|
x -= 1.5 * mkw
|
||||||
|
|
||||||
|
return x
|
||||||
|
|
||||||
|
arrow.scene_x = offset_from_yaxis
|
||||||
|
view.scene().addItem(arrow)
|
||||||
|
arrow.hide() # never show on startup
|
||||||
|
nav.level_markers[key] = arrow
|
||||||
|
|
||||||
|
# literally the 'pp' (pee pee) "position price" label that's
|
||||||
|
# always in view
|
||||||
|
pp_label = Label(
|
||||||
view=view,
|
view=view,
|
||||||
fmt_str='pp',
|
fmt_str='pp',
|
||||||
color=self._color,
|
color=nav.color,
|
||||||
update_on_range_change=False,
|
update_on_range_change=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# create placeholder 'up' level arrow
|
|
||||||
self._level_marker = None
|
|
||||||
self._level_marker = self.level_marker(size=1)
|
|
||||||
|
|
||||||
pp_label.scene_anchor = partial(
|
|
||||||
gpath_pin,
|
|
||||||
gpath=self._level_marker,
|
|
||||||
label=pp_label,
|
|
||||||
)
|
|
||||||
pp_label.render()
|
pp_label.render()
|
||||||
|
nav.pp_labels[key] = pp_label
|
||||||
|
|
||||||
self.size_label = size_label = Label(
|
size_label = Label(
|
||||||
view=view,
|
view=view,
|
||||||
color=self._color,
|
color=self.nav.color,
|
||||||
|
|
||||||
# this is "static" label
|
# this is "static" label
|
||||||
# update_on_range_change=False,
|
# update_on_range_change=False,
|
||||||
|
@ -514,11 +823,19 @@ class PositionTracker:
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
size_label.render()
|
size_label.render()
|
||||||
|
|
||||||
size_label.scene_anchor = partial(
|
size_label.scene_anchor = partial(
|
||||||
pp_tight_and_right,
|
pp_tight_and_right,
|
||||||
label=self.pp_label,
|
label=pp_label,
|
||||||
)
|
)
|
||||||
|
nav.size_labels[key] = size_label
|
||||||
|
|
||||||
|
pp_label.scene_anchor = partial(
|
||||||
|
gpath_pin,
|
||||||
|
gpath=arrow,
|
||||||
|
label=pp_label,
|
||||||
|
)
|
||||||
|
|
||||||
|
nav.show()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pane(self) -> FieldsForm:
|
def pane(self) -> FieldsForm:
|
||||||
|
@ -528,169 +845,74 @@ class PositionTracker:
|
||||||
'''
|
'''
|
||||||
return self.chart.linked.godwidget.pp_pane
|
return self.chart.linked.godwidget.pp_pane
|
||||||
|
|
||||||
def update_graphics(
|
|
||||||
self,
|
|
||||||
marker: LevelMarker
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''
|
|
||||||
Update all labels.
|
|
||||||
|
|
||||||
Meant to be called from the maker ``.paint()``
|
|
||||||
for immediate, lag free label draws.
|
|
||||||
|
|
||||||
'''
|
|
||||||
self.pp_label.update()
|
|
||||||
self.size_label.update()
|
|
||||||
|
|
||||||
def update_from_pp(
|
def update_from_pp(
|
||||||
self,
|
self,
|
||||||
position: Optional[Position] = None,
|
position: Optional[Position] = None,
|
||||||
|
set_as_startup: bool = False,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Update graphics and data from average price and size passed in our
|
'''
|
||||||
EMS ``BrokerdPosition`` msg.
|
Update graphics and data from average price and size passed in
|
||||||
|
our EMS ``BrokerdPosition`` msg.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# live pp updates
|
# live pp updates
|
||||||
pp = position or self.live_pp
|
pp = position or self.live_pp
|
||||||
|
if set_as_startup:
|
||||||
|
startup_pp = pp
|
||||||
|
else:
|
||||||
|
startup_pp = self.startup_pp
|
||||||
|
alloc = self.alloc
|
||||||
|
|
||||||
self.update_line(
|
# update allocator settings
|
||||||
pp.avg_price,
|
asset_type = pp.symbol.type_key
|
||||||
pp.size,
|
|
||||||
self.chart.linked.symbol.lot_size_digits,
|
|
||||||
)
|
|
||||||
|
|
||||||
# label updates
|
# specific configs by asset class / type
|
||||||
self.size_label.fields['slots_used'] = round(
|
if asset_type in _derivs:
|
||||||
self.alloc.slots_used(pp), ndigits=1)
|
# since it's harder to know how currency "applies" in this case
|
||||||
self.size_label.render()
|
# given leverage properties
|
||||||
|
alloc.size_unit = '# units'
|
||||||
|
|
||||||
if pp.size == 0:
|
# set units limit to slots size thus making make the next
|
||||||
self.hide()
|
# entry step 1.0
|
||||||
|
alloc.units_limit = alloc.slots
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._level_marker.level = pp.avg_price
|
alloc.size_unit = 'currency'
|
||||||
|
|
||||||
# these updates are critical to avoid lag on view/scene changes
|
# if the current position is already greater then the limit
|
||||||
self._level_marker.update() # trigger paint
|
# settings, increase the limit to the current position
|
||||||
self.pp_label.update()
|
if alloc.size_unit == 'currency':
|
||||||
self.size_label.update()
|
startup_size = self.startup_pp.size * startup_pp.ppu
|
||||||
|
|
||||||
self.show()
|
if startup_size > alloc.currency_limit:
|
||||||
|
alloc.currency_limit = round(startup_size, ndigits=2)
|
||||||
|
|
||||||
|
else:
|
||||||
|
startup_size = abs(startup_pp.size)
|
||||||
|
|
||||||
|
if startup_size > alloc.units_limit:
|
||||||
|
alloc.units_limit = startup_size
|
||||||
|
|
||||||
|
if asset_type in _derivs:
|
||||||
|
alloc.slots = alloc.units_limit
|
||||||
|
|
||||||
|
self.nav.update_ui(
|
||||||
|
self.alloc.account,
|
||||||
|
pp.ppu,
|
||||||
|
pp.size,
|
||||||
|
round(alloc.slots_used(pp), ndigits=1), # slots used
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.live_pp.size:
|
||||||
|
# print("SHOWING NAV")
|
||||||
|
self.nav.show()
|
||||||
|
|
||||||
|
# if pp.size == 0:
|
||||||
|
else:
|
||||||
|
# print("HIDING NAV")
|
||||||
|
self.nav.hide()
|
||||||
|
|
||||||
# don't show side and status widgets unless
|
# don't show side and status widgets unless
|
||||||
# order mode is "engaged" (which done via input controls)
|
# order mode is "engaged" (which done via input controls)
|
||||||
self.hide_info()
|
self.nav.hide_info()
|
||||||
|
|
||||||
def level(self) -> float:
|
|
||||||
if self.line:
|
|
||||||
return self.line.value()
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def show(self) -> None:
|
|
||||||
if self.live_pp.size:
|
|
||||||
self.line.show()
|
|
||||||
self.line.show_labels()
|
|
||||||
|
|
||||||
self._level_marker.show()
|
|
||||||
self.pp_label.show()
|
|
||||||
self.size_label.show()
|
|
||||||
|
|
||||||
def hide(self) -> None:
|
|
||||||
self.pp_label.hide()
|
|
||||||
self._level_marker.hide()
|
|
||||||
self.size_label.hide()
|
|
||||||
if self.line:
|
|
||||||
self.line.hide()
|
|
||||||
|
|
||||||
def hide_info(self) -> None:
|
|
||||||
'''Hide details (right now just size label?) of position.
|
|
||||||
|
|
||||||
'''
|
|
||||||
self.size_label.hide()
|
|
||||||
if self.line:
|
|
||||||
self.line.hide_labels()
|
|
||||||
|
|
||||||
# TODO: move into annoate module
|
|
||||||
def level_marker(
|
|
||||||
self,
|
|
||||||
size: float,
|
|
||||||
|
|
||||||
) -> LevelMarker:
|
|
||||||
|
|
||||||
if self._level_marker:
|
|
||||||
self._level_marker.delete()
|
|
||||||
|
|
||||||
# arrow marker
|
|
||||||
# scale marker size with dpi-aware font size
|
|
||||||
font_size = _font.font.pixelSize()
|
|
||||||
|
|
||||||
# scale marker size with dpi-aware font size
|
|
||||||
arrow_size = floor(1.375 * font_size)
|
|
||||||
|
|
||||||
if size > 0:
|
|
||||||
style = '|<'
|
|
||||||
|
|
||||||
elif size < 0:
|
|
||||||
style = '>|'
|
|
||||||
|
|
||||||
arrow = LevelMarker(
|
|
||||||
chart=self.chart,
|
|
||||||
style=style,
|
|
||||||
get_level=self.level,
|
|
||||||
size=arrow_size,
|
|
||||||
on_paint=self.update_graphics,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.chart.getViewBox().scene().addItem(arrow)
|
|
||||||
arrow.show()
|
|
||||||
|
|
||||||
return arrow
|
|
||||||
|
|
||||||
def update_line(
|
|
||||||
self,
|
|
||||||
price: float,
|
|
||||||
size: float,
|
|
||||||
size_digits: int,
|
|
||||||
|
|
||||||
) -> None:
|
|
||||||
'''Update personal position level line.
|
|
||||||
|
|
||||||
'''
|
|
||||||
# do line update
|
|
||||||
line = self.line
|
|
||||||
|
|
||||||
if size:
|
|
||||||
if line is None:
|
|
||||||
|
|
||||||
# create and show a pp line
|
|
||||||
line = self.line = position_line(
|
|
||||||
chart=self.chart,
|
|
||||||
level=price,
|
|
||||||
size=size,
|
|
||||||
color=self._color,
|
|
||||||
marker=self._level_marker,
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
line.set_level(price)
|
|
||||||
self._level_marker.level = price
|
|
||||||
self._level_marker.update()
|
|
||||||
|
|
||||||
# update LHS sizing label
|
|
||||||
line.update_labels({
|
|
||||||
'size': size,
|
|
||||||
'size_digits': size_digits,
|
|
||||||
'fiat_size': round(price * size, ndigits=2),
|
|
||||||
|
|
||||||
# TODO: per account lines on a single (or very related) symbol
|
|
||||||
'account': self.alloc.account,
|
|
||||||
})
|
|
||||||
line.show()
|
|
||||||
|
|
||||||
elif line: # remove pp line from view if it exists on a net-zero pp
|
|
||||||
line.delete()
|
|
||||||
self.line = None
|
|
||||||
|
|
|
@ -0,0 +1,332 @@
|
||||||
|
# piker: trading gear for hackers
|
||||||
|
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||||
|
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
High level streaming graphics primitives.
|
||||||
|
|
||||||
|
This is an intermediate layer which associates real-time low latency
|
||||||
|
graphics primitives with underlying stream/flow related data structures
|
||||||
|
for fast incremental update.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import (
|
||||||
|
Optional,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
|
import msgspec
|
||||||
|
import numpy as np
|
||||||
|
import pyqtgraph as pg
|
||||||
|
from PyQt5.QtGui import QPainterPath
|
||||||
|
|
||||||
|
from ..data._formatters import (
|
||||||
|
IncrementalFormatter,
|
||||||
|
)
|
||||||
|
from ..data._pathops import (
|
||||||
|
xy_downsample,
|
||||||
|
)
|
||||||
|
from ..log import get_logger
|
||||||
|
from .._profile import (
|
||||||
|
Profiler,
|
||||||
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._dataviz import Viz
|
||||||
|
|
||||||
|
|
||||||
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Renderer(msgspec.Struct):
|
||||||
|
|
||||||
|
viz: Viz
|
||||||
|
fmtr: IncrementalFormatter
|
||||||
|
|
||||||
|
# output graphics rendering, the main object
|
||||||
|
# processed in ``QGraphicsObject.paint()``
|
||||||
|
path: Optional[QPainterPath] = None
|
||||||
|
fast_path: Optional[QPainterPath] = None
|
||||||
|
|
||||||
|
# XXX: just ideas..
|
||||||
|
# called on the final data (transform) output to convert
|
||||||
|
# to "graphical data form" a format that can be passed to
|
||||||
|
# the ``.draw()`` implementation.
|
||||||
|
# graphics_t: Optional[Callable[ShmArray, np.ndarray]] = None
|
||||||
|
# graphics_t_shm: Optional[ShmArray] = None
|
||||||
|
|
||||||
|
# path graphics update implementation methods
|
||||||
|
# prepend_fn: Optional[Callable[QPainterPath, QPainterPath]] = None
|
||||||
|
# append_fn: Optional[Callable[QPainterPath, QPainterPath]] = None
|
||||||
|
|
||||||
|
# downsampling state
|
||||||
|
_last_uppx: float = 0
|
||||||
|
_in_ds: bool = False
|
||||||
|
|
||||||
|
def draw_path(
|
||||||
|
self,
|
||||||
|
x: np.ndarray,
|
||||||
|
y: np.ndarray,
|
||||||
|
connect: str | np.ndarray = 'all',
|
||||||
|
path: Optional[QPainterPath] = None,
|
||||||
|
redraw: bool = False,
|
||||||
|
|
||||||
|
) -> QPainterPath:
|
||||||
|
|
||||||
|
path_was_none = path is None
|
||||||
|
|
||||||
|
if redraw and path:
|
||||||
|
path.clear()
|
||||||
|
|
||||||
|
# TODO: avoid this?
|
||||||
|
if self.fast_path:
|
||||||
|
self.fast_path.clear()
|
||||||
|
|
||||||
|
path = pg.functions.arrayToQPath(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
connect=connect,
|
||||||
|
finiteCheck=False,
|
||||||
|
|
||||||
|
# reserve mem allocs see:
|
||||||
|
# - https://doc.qt.io/qt-5/qpainterpath.html#reserve
|
||||||
|
# - https://doc.qt.io/qt-5/qpainterpath.html#capacity
|
||||||
|
# - https://doc.qt.io/qt-5/qpainterpath.html#clear
|
||||||
|
# XXX: right now this is based on had hoc checks on a
|
||||||
|
# hidpi 3840x2160 4k monitor but we should optimize for
|
||||||
|
# the target display(s) on the sys.
|
||||||
|
# if no_path_yet:
|
||||||
|
# graphics.path.reserve(int(500e3))
|
||||||
|
# path=path, # path re-use / reserving
|
||||||
|
)
|
||||||
|
|
||||||
|
# avoid mem allocs if possible
|
||||||
|
if path_was_none:
|
||||||
|
path.reserve(path.capacity())
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
|
def render(
|
||||||
|
self,
|
||||||
|
|
||||||
|
new_read,
|
||||||
|
array_key: str,
|
||||||
|
profiler: Profiler,
|
||||||
|
uppx: float = 1,
|
||||||
|
|
||||||
|
# redraw and ds flags
|
||||||
|
should_redraw: bool = False,
|
||||||
|
new_sample_rate: bool = False,
|
||||||
|
should_ds: bool = False,
|
||||||
|
showing_src_data: bool = True,
|
||||||
|
|
||||||
|
do_append: bool = True,
|
||||||
|
use_fpath: bool = True,
|
||||||
|
|
||||||
|
# only render datums "in view" of the ``ChartView``
|
||||||
|
use_vr: bool = True,
|
||||||
|
|
||||||
|
) -> tuple[QPainterPath, bool]:
|
||||||
|
'''
|
||||||
|
Render the current graphics path(s)
|
||||||
|
|
||||||
|
There are (at least) 3 stages from source data to graphics data:
|
||||||
|
- a data transform (which can be stored in additional shm)
|
||||||
|
- a graphics transform which converts discrete basis data to
|
||||||
|
a `float`-basis view-coords graphics basis. (eg. ``ohlc_flatten()``,
|
||||||
|
``step_path_arrays_from_1d()``, etc.)
|
||||||
|
|
||||||
|
- blah blah blah (from notes)
|
||||||
|
|
||||||
|
'''
|
||||||
|
# TODO: can the renderer just call ``Viz.read()`` directly?
|
||||||
|
# unpack latest source data read
|
||||||
|
fmtr = self.fmtr
|
||||||
|
|
||||||
|
(
|
||||||
|
_,
|
||||||
|
_,
|
||||||
|
array,
|
||||||
|
ivl,
|
||||||
|
ivr,
|
||||||
|
in_view,
|
||||||
|
) = new_read
|
||||||
|
|
||||||
|
# xy-path data transform: convert source data to a format
|
||||||
|
# able to be passed to a `QPainterPath` rendering routine.
|
||||||
|
fmt_out = fmtr.format_to_1d(
|
||||||
|
new_read,
|
||||||
|
array_key,
|
||||||
|
profiler,
|
||||||
|
|
||||||
|
slice_to_inview=use_vr,
|
||||||
|
)
|
||||||
|
|
||||||
|
# no history in view case
|
||||||
|
if not fmt_out:
|
||||||
|
# XXX: this might be why the profiler only has exits?
|
||||||
|
return
|
||||||
|
|
||||||
|
(
|
||||||
|
x_1d,
|
||||||
|
y_1d,
|
||||||
|
connect,
|
||||||
|
prepend_length,
|
||||||
|
append_length,
|
||||||
|
view_changed,
|
||||||
|
# append_tres,
|
||||||
|
|
||||||
|
) = fmt_out
|
||||||
|
|
||||||
|
# redraw conditions
|
||||||
|
if (
|
||||||
|
prepend_length > 0
|
||||||
|
or new_sample_rate
|
||||||
|
or view_changed
|
||||||
|
|
||||||
|
# NOTE: comment this to try and make "append paths"
|
||||||
|
# work below..
|
||||||
|
or append_length > 0
|
||||||
|
):
|
||||||
|
should_redraw = True
|
||||||
|
|
||||||
|
path: QPainterPath = self.path
|
||||||
|
fast_path: QPainterPath = self.fast_path
|
||||||
|
reset: bool = False
|
||||||
|
|
||||||
|
self.viz.yrange = None
|
||||||
|
|
||||||
|
# redraw the entire source data if we have either of:
|
||||||
|
# - no prior path graphic rendered or,
|
||||||
|
# - we always intend to re-render the data only in view
|
||||||
|
if (
|
||||||
|
path is None
|
||||||
|
or should_redraw
|
||||||
|
):
|
||||||
|
# print(f"{self.viz.name} -> REDRAWING BRUH")
|
||||||
|
if new_sample_rate and showing_src_data:
|
||||||
|
log.info(f'DEDOWN -> {array_key}')
|
||||||
|
self._in_ds = False
|
||||||
|
|
||||||
|
elif should_ds and uppx > 1:
|
||||||
|
|
||||||
|
x_1d, y_1d, ymn, ymx = xy_downsample(
|
||||||
|
x_1d,
|
||||||
|
y_1d,
|
||||||
|
uppx,
|
||||||
|
)
|
||||||
|
self.viz.yrange = ymn, ymx
|
||||||
|
# print(f'{self.viz.name} post ds: ymn, ymx: {ymn},{ymx}')
|
||||||
|
|
||||||
|
reset = True
|
||||||
|
profiler(f'FULL PATH downsample redraw={should_ds}')
|
||||||
|
self._in_ds = True
|
||||||
|
|
||||||
|
path = self.draw_path(
|
||||||
|
x=x_1d,
|
||||||
|
y=y_1d,
|
||||||
|
connect=connect,
|
||||||
|
path=path,
|
||||||
|
redraw=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
profiler(
|
||||||
|
'generated fresh path. '
|
||||||
|
f'(should_redraw: {should_redraw} '
|
||||||
|
f'should_ds: {should_ds} new_sample_rate: {new_sample_rate})'
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO: get this piecewise prepend working - right now it's
|
||||||
|
# giving heck on vwap...
|
||||||
|
# elif prepend_length:
|
||||||
|
|
||||||
|
# prepend_path = pg.functions.arrayToQPath(
|
||||||
|
# x[0:prepend_length],
|
||||||
|
# y[0:prepend_length],
|
||||||
|
# connect='all'
|
||||||
|
# )
|
||||||
|
|
||||||
|
# # swap prepend path in "front"
|
||||||
|
# old_path = graphics.path
|
||||||
|
# graphics.path = prepend_path
|
||||||
|
# # graphics.path.moveTo(new_x[0], new_y[0])
|
||||||
|
# graphics.path.connectPath(old_path)
|
||||||
|
|
||||||
|
elif (
|
||||||
|
append_length > 0
|
||||||
|
and do_append
|
||||||
|
):
|
||||||
|
print(f'{array_key} append len: {append_length}')
|
||||||
|
# new_x = x_1d[-append_length - 2:] # slice_to_head]
|
||||||
|
# new_y = y_1d[-append_length - 2:] # slice_to_head]
|
||||||
|
profiler('sliced append path')
|
||||||
|
# (
|
||||||
|
# x_1d,
|
||||||
|
# y_1d,
|
||||||
|
# connect,
|
||||||
|
# ) = append_tres
|
||||||
|
|
||||||
|
profiler(
|
||||||
|
f'diffed array input, append_length={append_length}'
|
||||||
|
)
|
||||||
|
|
||||||
|
# if should_ds and uppx > 1:
|
||||||
|
# new_x, new_y = xy_downsample(
|
||||||
|
# new_x,
|
||||||
|
# new_y,
|
||||||
|
# uppx,
|
||||||
|
# )
|
||||||
|
# profiler(f'fast path downsample redraw={should_ds}')
|
||||||
|
|
||||||
|
append_path = self.draw_path(
|
||||||
|
x=x_1d,
|
||||||
|
y=y_1d,
|
||||||
|
connect=connect,
|
||||||
|
path=fast_path,
|
||||||
|
)
|
||||||
|
profiler('generated append qpath')
|
||||||
|
|
||||||
|
if use_fpath:
|
||||||
|
# print(f'{self.viz.name}: FAST PATH')
|
||||||
|
# an attempt at trying to make append-updates faster..
|
||||||
|
if fast_path is None:
|
||||||
|
fast_path = append_path
|
||||||
|
# fast_path.reserve(int(6e3))
|
||||||
|
else:
|
||||||
|
fast_path.connectPath(append_path)
|
||||||
|
size = fast_path.capacity()
|
||||||
|
profiler(f'connected fast path w size: {size}')
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"append_path br: {append_path.boundingRect()}\n"
|
||||||
|
f"path size: {size}\n"
|
||||||
|
f"append_path len: {append_path.length()}\n"
|
||||||
|
f"fast_path len: {fast_path.length()}\n"
|
||||||
|
)
|
||||||
|
# graphics.path.moveTo(new_x[0], new_y[0])
|
||||||
|
# path.connectPath(append_path)
|
||||||
|
|
||||||
|
# XXX: lol this causes a hang..
|
||||||
|
# graphics.path = graphics.path.simplified()
|
||||||
|
else:
|
||||||
|
size = path.capacity()
|
||||||
|
profiler(f'connected history path w size: {size}')
|
||||||
|
path.connectPath(append_path)
|
||||||
|
|
||||||
|
self.path = path
|
||||||
|
self.fast_path = fast_path
|
||||||
|
|
||||||
|
return self.path, reset
|
|
@ -35,9 +35,13 @@ from collections import defaultdict
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import (
|
from typing import (
|
||||||
Optional, Callable,
|
Optional,
|
||||||
Awaitable, Sequence,
|
Callable,
|
||||||
Any, AsyncIterator
|
Awaitable,
|
||||||
|
Sequence,
|
||||||
|
Any,
|
||||||
|
AsyncIterator,
|
||||||
|
Iterator,
|
||||||
)
|
)
|
||||||
import time
|
import time
|
||||||
# from pprint import pformat
|
# from pprint import pformat
|
||||||
|
@ -119,7 +123,7 @@ class CompleterView(QTreeView):
|
||||||
# TODO: size this based on DPI font
|
# TODO: size this based on DPI font
|
||||||
self.setIndentation(_font.px_size)
|
self.setIndentation(_font.px_size)
|
||||||
|
|
||||||
# self.setUniformRowHeights(True)
|
self.setUniformRowHeights(True)
|
||||||
# self.setColumnWidth(0, 3)
|
# self.setColumnWidth(0, 3)
|
||||||
# self.setVerticalBarPolicy(Qt.ScrollBarAlwaysOff)
|
# self.setVerticalBarPolicy(Qt.ScrollBarAlwaysOff)
|
||||||
# self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustIgnored)
|
# self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustIgnored)
|
||||||
|
@ -138,13 +142,15 @@ class CompleterView(QTreeView):
|
||||||
model.setHorizontalHeaderLabels(labels)
|
model.setHorizontalHeaderLabels(labels)
|
||||||
|
|
||||||
self._font_size: int = 0 # pixels
|
self._font_size: int = 0 # pixels
|
||||||
|
self._init: bool = False
|
||||||
|
|
||||||
async def on_pressed(self, idx: QModelIndex) -> None:
|
async def on_pressed(self, idx: QModelIndex) -> None:
|
||||||
'''Mouse pressed on view handler.
|
'''
|
||||||
|
Mouse pressed on view handler.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
search = self.parent()
|
search = self.parent()
|
||||||
await search.chart_current_item(clear_to_cache=False)
|
await search.chart_current_item()
|
||||||
search.focus()
|
search.focus()
|
||||||
|
|
||||||
def set_font_size(self, size: int = 18):
|
def set_font_size(self, size: int = 18):
|
||||||
|
@ -156,56 +162,64 @@ class CompleterView(QTreeView):
|
||||||
|
|
||||||
self.setStyleSheet(f"font: {size}px")
|
self.setStyleSheet(f"font: {size}px")
|
||||||
|
|
||||||
# def resizeEvent(self, event: 'QEvent') -> None:
|
def resize_to_results(
|
||||||
# event.accept()
|
self,
|
||||||
# super().resizeEvent(event)
|
w: Optional[float] = 0,
|
||||||
|
h: Optional[float] = None,
|
||||||
|
|
||||||
def on_resize(self) -> None:
|
) -> None:
|
||||||
'''
|
|
||||||
Resize relay event from god.
|
|
||||||
|
|
||||||
'''
|
|
||||||
self.resize_to_results()
|
|
||||||
|
|
||||||
def resize_to_results(self):
|
|
||||||
model = self.model()
|
model = self.model()
|
||||||
cols = model.columnCount()
|
cols = model.columnCount()
|
||||||
# rows = model.rowCount()
|
cidx = self.selectionModel().currentIndex()
|
||||||
|
rows = model.rowCount()
|
||||||
|
self.expandAll()
|
||||||
|
|
||||||
|
# compute the approx height in pixels needed to include
|
||||||
|
# all result rows in view.
|
||||||
|
row_h = rows_h = self.rowHeight(cidx) * (rows + 1)
|
||||||
|
for idx, item in self.iter_df_rows():
|
||||||
|
row_h = self.rowHeight(idx)
|
||||||
|
rows_h += row_h
|
||||||
|
# print(f'row_h: {row_h}\nrows_h: {rows_h}')
|
||||||
|
|
||||||
|
# TODO: could we just break early here on detection
|
||||||
|
# of ``rows_h >= h``?
|
||||||
|
|
||||||
col_w_tot = 0
|
col_w_tot = 0
|
||||||
for i in range(cols):
|
for i in range(cols):
|
||||||
|
# only slap in a rows's height's worth
|
||||||
|
# of padding once at startup.. no idea
|
||||||
|
if (
|
||||||
|
not self._init
|
||||||
|
and row_h
|
||||||
|
):
|
||||||
|
col_w_tot = row_h
|
||||||
|
self._init = True
|
||||||
|
|
||||||
self.resizeColumnToContents(i)
|
self.resizeColumnToContents(i)
|
||||||
col_w_tot += self.columnWidth(i)
|
col_w_tot += self.columnWidth(i)
|
||||||
|
|
||||||
win = self.window()
|
# NOTE: if the heigh `h` set here is **too large** then the
|
||||||
win_h = win.height()
|
# resize event will perpetually trigger as the window causes
|
||||||
edit_h = self.parent().bar.height()
|
# some kind of recompute of callbacks.. so we have to ensure
|
||||||
sb_h = win.statusBar().height()
|
# it's limited.
|
||||||
|
if h:
|
||||||
|
h: int = round(h)
|
||||||
|
abs_mx = round(0.91 * h)
|
||||||
|
self.setMaximumHeight(abs_mx)
|
||||||
|
|
||||||
# TODO: probably make this more general / less hacky
|
if rows_h <= abs_mx:
|
||||||
# we should figure out the exact number of rows to allow
|
# self.setMinimumHeight(rows_h)
|
||||||
# inclusive of search bar and header "rows", in pixel terms.
|
self.setMinimumHeight(rows_h)
|
||||||
# Eventually when we have an "info" widget below the results we
|
# self.setFixedHeight(rows_h)
|
||||||
# will want space for it and likely terminating the results-view
|
|
||||||
# space **exactly on a row** would be ideal.
|
|
||||||
# if row_px > 0:
|
|
||||||
# rows = ceil(window_h / row_px) - 4
|
|
||||||
# else:
|
|
||||||
# rows = 16
|
|
||||||
# self.setFixedHeight(rows * row_px)
|
|
||||||
# self.resize(self.width(), rows * row_px)
|
|
||||||
|
|
||||||
# NOTE: if the heigh set here is **too large** then the resize
|
else:
|
||||||
# event will perpetually trigger as the window causes some kind
|
self.setMinimumHeight(abs_mx)
|
||||||
# of recompute of callbacks.. so we have to ensure it's limited.
|
|
||||||
h = win_h - (edit_h + 1.666*sb_h)
|
|
||||||
assert h > 0
|
|
||||||
self.setFixedHeight(round(h))
|
|
||||||
|
|
||||||
# size to width of longest result seen thus far
|
# dyncamically size to width of longest result seen
|
||||||
# TODO: should we always dynamically scale to longest result?
|
curr_w = self.width()
|
||||||
if self.width() < col_w_tot:
|
if curr_w < col_w_tot:
|
||||||
self.setFixedWidth(col_w_tot)
|
self.setMinimumWidth(col_w_tot)
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
@ -331,6 +345,23 @@ class CompleterView(QTreeView):
|
||||||
item = model.itemFromIndex(idx)
|
item = model.itemFromIndex(idx)
|
||||||
yield idx, item
|
yield idx, item
|
||||||
|
|
||||||
|
def iter_df_rows(
|
||||||
|
self,
|
||||||
|
iparent: QModelIndex = QModelIndex(),
|
||||||
|
|
||||||
|
) -> Iterator[tuple[QModelIndex, QStandardItem]]:
|
||||||
|
|
||||||
|
model = self.model()
|
||||||
|
isections = model.rowCount(iparent)
|
||||||
|
for i in range(isections):
|
||||||
|
idx = model.index(i, 0, iparent)
|
||||||
|
item = model.itemFromIndex(idx)
|
||||||
|
yield idx, item
|
||||||
|
|
||||||
|
if model.hasChildren(idx):
|
||||||
|
# recursively yield child items depth-first
|
||||||
|
yield from self.iter_df_rows(idx)
|
||||||
|
|
||||||
def find_section(
|
def find_section(
|
||||||
self,
|
self,
|
||||||
section: str,
|
section: str,
|
||||||
|
@ -354,7 +385,8 @@ class CompleterView(QTreeView):
|
||||||
status_field: str = None,
|
status_field: str = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Clear all result-rows from under the depth = 1 section.
|
'''
|
||||||
|
Clear all result-rows from under the depth = 1 section.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
idx = self.find_section(section)
|
idx = self.find_section(section)
|
||||||
|
@ -375,8 +407,6 @@ class CompleterView(QTreeView):
|
||||||
else:
|
else:
|
||||||
model.setItem(idx.row(), 1, QStandardItem())
|
model.setItem(idx.row(), 1, QStandardItem())
|
||||||
|
|
||||||
self.resize_to_results()
|
|
||||||
|
|
||||||
return idx
|
return idx
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
@ -386,12 +416,26 @@ class CompleterView(QTreeView):
|
||||||
section: str,
|
section: str,
|
||||||
values: Sequence[str],
|
values: Sequence[str],
|
||||||
clear_all: bool = False,
|
clear_all: bool = False,
|
||||||
|
reverse: bool = False,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Set result-rows for depth = 1 tree section ``section``.
|
Set result-rows for depth = 1 tree section ``section``.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
if (
|
||||||
|
values
|
||||||
|
and not isinstance(values[0], str)
|
||||||
|
):
|
||||||
|
flattened: list[str] = []
|
||||||
|
for val in values:
|
||||||
|
flattened.extend(val)
|
||||||
|
|
||||||
|
values = flattened
|
||||||
|
|
||||||
|
if reverse:
|
||||||
|
values = reversed(values)
|
||||||
|
|
||||||
model = self.model()
|
model = self.model()
|
||||||
if clear_all:
|
if clear_all:
|
||||||
# XXX: rewrite the model from scratch if caller requests it
|
# XXX: rewrite the model from scratch if caller requests it
|
||||||
|
@ -444,9 +488,22 @@ class CompleterView(QTreeView):
|
||||||
|
|
||||||
self.show_matches()
|
self.show_matches()
|
||||||
|
|
||||||
def show_matches(self) -> None:
|
def show_matches(
|
||||||
|
self,
|
||||||
|
wh: Optional[tuple[float, float]] = None,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
if wh:
|
||||||
|
self.resize_to_results(*wh)
|
||||||
|
else:
|
||||||
|
# case where it's just an update from results and *NOT*
|
||||||
|
# a resize of some higher level parent-container widget.
|
||||||
|
search = self.parent()
|
||||||
|
w, h = search.space_dims()
|
||||||
|
self.resize_to_results(w=w, h=h)
|
||||||
|
|
||||||
self.show()
|
self.show()
|
||||||
self.resize_to_results()
|
|
||||||
|
|
||||||
|
|
||||||
class SearchBar(Edit):
|
class SearchBar(Edit):
|
||||||
|
@ -466,18 +523,15 @@ class SearchBar(Edit):
|
||||||
self.godwidget = godwidget
|
self.godwidget = godwidget
|
||||||
super().__init__(parent, **kwargs)
|
super().__init__(parent, **kwargs)
|
||||||
self.view: CompleterView = view
|
self.view: CompleterView = view
|
||||||
godwidget._widgets[view.mode_name] = view
|
|
||||||
|
|
||||||
def show(self) -> None:
|
|
||||||
super().show()
|
|
||||||
self.view.show_matches()
|
|
||||||
|
|
||||||
def unfocus(self) -> None:
|
def unfocus(self) -> None:
|
||||||
self.parent().hide()
|
self.parent().hide()
|
||||||
self.clearFocus()
|
self.clearFocus()
|
||||||
|
|
||||||
|
def hide(self) -> None:
|
||||||
if self.view:
|
if self.view:
|
||||||
self.view.hide()
|
self.view.hide()
|
||||||
|
super().hide()
|
||||||
|
|
||||||
|
|
||||||
class SearchWidget(QtWidgets.QWidget):
|
class SearchWidget(QtWidgets.QWidget):
|
||||||
|
@ -496,15 +550,16 @@ class SearchWidget(QtWidgets.QWidget):
|
||||||
parent=None,
|
parent=None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(parent or godwidget)
|
super().__init__(parent)
|
||||||
|
|
||||||
# size it as we specify
|
# size it as we specify
|
||||||
self.setSizePolicy(
|
self.setSizePolicy(
|
||||||
QtWidgets.QSizePolicy.Fixed,
|
QtWidgets.QSizePolicy.Fixed,
|
||||||
QtWidgets.QSizePolicy.Expanding,
|
QtWidgets.QSizePolicy.Fixed,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.godwidget = godwidget
|
self.godwidget = godwidget
|
||||||
|
godwidget.reg_for_resize(self)
|
||||||
|
|
||||||
self.vbox = QtWidgets.QVBoxLayout(self)
|
self.vbox = QtWidgets.QVBoxLayout(self)
|
||||||
self.vbox.setContentsMargins(0, 4, 4, 0)
|
self.vbox.setContentsMargins(0, 4, 4, 0)
|
||||||
|
@ -554,20 +609,37 @@ class SearchWidget(QtWidgets.QWidget):
|
||||||
self.vbox.setAlignment(self.view, Qt.AlignTop | Qt.AlignLeft)
|
self.vbox.setAlignment(self.view, Qt.AlignTop | Qt.AlignLeft)
|
||||||
|
|
||||||
def focus(self) -> None:
|
def focus(self) -> None:
|
||||||
|
self.show()
|
||||||
|
self.bar.focus()
|
||||||
|
|
||||||
|
def show_cache_entries(
|
||||||
|
self,
|
||||||
|
only: bool = False,
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Clear the search results view and show only cached (aka recently
|
||||||
|
loaded with active data) feeds in the results section.
|
||||||
|
|
||||||
|
'''
|
||||||
|
godw = self.godwidget
|
||||||
|
|
||||||
|
# first entry in the cache is the current symbol(s)
|
||||||
|
fqsns = []
|
||||||
|
|
||||||
|
for multi_fqsns in list(godw._chart_cache):
|
||||||
|
fqsns.extend(list(multi_fqsns))
|
||||||
|
|
||||||
if self.view.model().rowCount(QModelIndex()) == 0:
|
|
||||||
# fill cache list if nothing existing
|
|
||||||
self.view.set_section_entries(
|
self.view.set_section_entries(
|
||||||
'cache',
|
'cache',
|
||||||
list(reversed(self.godwidget._chart_cache)),
|
list(fqsns),
|
||||||
clear_all=True,
|
# remove all other completion results except for cache
|
||||||
|
clear_all=only,
|
||||||
|
reverse=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.bar.focus()
|
|
||||||
self.show()
|
|
||||||
|
|
||||||
def get_current_item(self) -> Optional[tuple[str, str]]:
|
def get_current_item(self) -> Optional[tuple[str, str]]:
|
||||||
'''Return the current completer tree selection as
|
'''
|
||||||
|
Return the current completer tree selection as
|
||||||
a tuple ``(parent: str, child: str)`` if valid, else ``None``.
|
a tuple ``(parent: str, child: str)`` if valid, else ``None``.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -603,7 +675,8 @@ class SearchWidget(QtWidgets.QWidget):
|
||||||
clear_to_cache: bool = True,
|
clear_to_cache: bool = True,
|
||||||
|
|
||||||
) -> Optional[str]:
|
) -> Optional[str]:
|
||||||
'''Attempt to load and switch the current selected
|
'''
|
||||||
|
Attempt to load and switch the current selected
|
||||||
completion result to the affiliated chart app.
|
completion result to the affiliated chart app.
|
||||||
|
|
||||||
Return any loaded symbol.
|
Return any loaded symbol.
|
||||||
|
@ -614,14 +687,15 @@ class SearchWidget(QtWidgets.QWidget):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
provider, symbol = value
|
provider, symbol = value
|
||||||
chart = self.godwidget
|
godw = self.godwidget
|
||||||
|
|
||||||
log.info(f'Requesting symbol: {symbol}.{provider}')
|
fqsn = f'{symbol}.{provider}'
|
||||||
|
log.info(f'Requesting symbol: {fqsn}')
|
||||||
|
|
||||||
await chart.load_symbol(
|
# assert provider in symbol
|
||||||
provider,
|
await godw.load_symbols(
|
||||||
symbol,
|
fqsns=[fqsn],
|
||||||
'info',
|
loglevel='info',
|
||||||
)
|
)
|
||||||
|
|
||||||
# fully qualified symbol name (SNS i guess is what we're
|
# fully qualified symbol name (SNS i guess is what we're
|
||||||
|
@ -635,18 +709,46 @@ class SearchWidget(QtWidgets.QWidget):
|
||||||
# Re-order the symbol cache on the chart to display in
|
# Re-order the symbol cache on the chart to display in
|
||||||
# LIFO order. this is normally only done internally by
|
# LIFO order. this is normally only done internally by
|
||||||
# the chart on new symbols being loaded into memory
|
# the chart on new symbols being loaded into memory
|
||||||
chart.set_chart_symbol(fqsn, chart.linkedsplits)
|
godw.set_chart_symbols(
|
||||||
|
(fqsn,), (
|
||||||
self.view.set_section_entries(
|
godw.hist_linked,
|
||||||
'cache',
|
godw.rt_linked,
|
||||||
values=list(reversed(chart._chart_cache)),
|
|
||||||
|
|
||||||
# remove all other completion results except for cache
|
|
||||||
clear_all=True,
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
self.show_cache_entries(only=True)
|
||||||
|
|
||||||
|
self.bar.focus()
|
||||||
return fqsn
|
return fqsn
|
||||||
|
|
||||||
|
def space_dims(self) -> tuple[float, float]:
|
||||||
|
'''
|
||||||
|
Compute and return the "available space dimentions" for this
|
||||||
|
search widget in terms of px space for results by return the
|
||||||
|
pair of width and height.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# XXX: dun need dis rite?
|
||||||
|
# win = self.window()
|
||||||
|
# win_h = win.height()
|
||||||
|
# sb_h = win.statusBar().height()
|
||||||
|
godw = self.godwidget
|
||||||
|
hl = godw.hist_linked
|
||||||
|
edit_h = self.bar.height()
|
||||||
|
h = hl.height() - edit_h
|
||||||
|
w = hl.width()
|
||||||
|
return w, h
|
||||||
|
|
||||||
|
def on_resize(self) -> None:
|
||||||
|
'''
|
||||||
|
Resize relay event from god, resize all child widgets.
|
||||||
|
|
||||||
|
Right now this is just view to contents and/or the fast chart
|
||||||
|
height.
|
||||||
|
|
||||||
|
'''
|
||||||
|
w, h = self.space_dims()
|
||||||
|
self.bar.view.show_matches(wh=(w, h))
|
||||||
|
|
||||||
|
|
||||||
_search_active: trio.Event = trio.Event()
|
_search_active: trio.Event = trio.Event()
|
||||||
_search_enabled: bool = False
|
_search_enabled: bool = False
|
||||||
|
@ -682,9 +784,10 @@ async def pack_matches(
|
||||||
with trio.CancelScope() as cs:
|
with trio.CancelScope() as cs:
|
||||||
task_status.started(cs)
|
task_status.started(cs)
|
||||||
# ensure ^ status is updated
|
# ensure ^ status is updated
|
||||||
results = await search(pattern)
|
results = list(await search(pattern))
|
||||||
|
|
||||||
if provider != 'cache': # XXX: don't cache the cache results xD
|
# XXX: don't cache the cache results xD
|
||||||
|
if provider != 'cache':
|
||||||
matches[(provider, pattern)] = results
|
matches[(provider, pattern)] = results
|
||||||
|
|
||||||
# print(f'results from {provider}: {results}')
|
# print(f'results from {provider}: {results}')
|
||||||
|
@ -712,10 +815,11 @@ async def fill_results(
|
||||||
max_pause_time: float = 6/16 + 0.001,
|
max_pause_time: float = 6/16 + 0.001,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Task to search through providers and fill in possible
|
'''
|
||||||
|
Task to search through providers and fill in possible
|
||||||
completion results.
|
completion results.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
global _search_active, _search_enabled, _searcher_cache
|
global _search_active, _search_enabled, _searcher_cache
|
||||||
|
|
||||||
bar = search.bar
|
bar = search.bar
|
||||||
|
@ -729,6 +833,10 @@ async def fill_results(
|
||||||
matches = defaultdict(list)
|
matches = defaultdict(list)
|
||||||
has_results: defaultdict[str, set[str]] = defaultdict(set)
|
has_results: defaultdict[str, set[str]] = defaultdict(set)
|
||||||
|
|
||||||
|
# show cached feed list at startup
|
||||||
|
search.show_cache_entries()
|
||||||
|
search.on_resize()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
await _search_active.wait()
|
await _search_active.wait()
|
||||||
period = None
|
period = None
|
||||||
|
@ -742,7 +850,7 @@ async def fill_results(
|
||||||
pattern = await recv_chan.receive()
|
pattern = await recv_chan.receive()
|
||||||
|
|
||||||
period = time.time() - wait_start
|
period = time.time() - wait_start
|
||||||
print(f'{pattern} after {period}')
|
log.debug(f'{pattern} after {period}')
|
||||||
|
|
||||||
# during fast multiple key inputs, wait until a pause
|
# during fast multiple key inputs, wait until a pause
|
||||||
# (in typing) to initiate search
|
# (in typing) to initiate search
|
||||||
|
@ -780,8 +888,9 @@ async def fill_results(
|
||||||
# it hasn't already been searched with the current
|
# it hasn't already been searched with the current
|
||||||
# input pattern (in which case just look up the old
|
# input pattern (in which case just look up the old
|
||||||
# results).
|
# results).
|
||||||
if (period >= pause) and (
|
if (
|
||||||
provider not in already_has_results
|
period >= pause
|
||||||
|
and provider not in already_has_results
|
||||||
):
|
):
|
||||||
|
|
||||||
# TODO: it may make more sense TO NOT search the
|
# TODO: it may make more sense TO NOT search the
|
||||||
|
@ -789,7 +898,9 @@ async def fill_results(
|
||||||
# cpu-bound.
|
# cpu-bound.
|
||||||
if provider != 'cache':
|
if provider != 'cache':
|
||||||
view.clear_section(
|
view.clear_section(
|
||||||
provider, status_field='-> searchin..')
|
provider,
|
||||||
|
status_field='-> searchin..',
|
||||||
|
)
|
||||||
|
|
||||||
await n.start(
|
await n.start(
|
||||||
pack_matches,
|
pack_matches,
|
||||||
|
@ -810,11 +921,20 @@ async def fill_results(
|
||||||
# re-searching it's ``dict`` since it's easier
|
# re-searching it's ``dict`` since it's easier
|
||||||
# but it also causes it to be slower then cached
|
# but it also causes it to be slower then cached
|
||||||
# results from other providers on occasion.
|
# results from other providers on occasion.
|
||||||
if results and provider != 'cache':
|
if (
|
||||||
|
results
|
||||||
|
):
|
||||||
|
if provider != 'cache':
|
||||||
view.set_section_entries(
|
view.set_section_entries(
|
||||||
section=provider,
|
section=provider,
|
||||||
values=results,
|
values=results,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
# if provider == 'cache':
|
||||||
|
# for the cache just show what we got
|
||||||
|
# that matches
|
||||||
|
search.show_cache_entries()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
view.clear_section(provider)
|
view.clear_section(provider)
|
||||||
|
|
||||||
|
@ -841,8 +961,7 @@ async def handle_keyboard_input(
|
||||||
godwidget = search.godwidget
|
godwidget = search.godwidget
|
||||||
view = bar.view
|
view = bar.view
|
||||||
view.set_font_size(bar.dpi_font.px_size)
|
view.set_font_size(bar.dpi_font.px_size)
|
||||||
|
send, recv = trio.open_memory_channel(616)
|
||||||
send, recv = trio.open_memory_channel(16)
|
|
||||||
|
|
||||||
async with trio.open_nursery() as n:
|
async with trio.open_nursery() as n:
|
||||||
|
|
||||||
|
@ -857,6 +976,10 @@ async def handle_keyboard_input(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
bar.focus()
|
||||||
|
search.show_cache_entries()
|
||||||
|
await trio.sleep(0)
|
||||||
|
|
||||||
async for kbmsg in recv_chan:
|
async for kbmsg in recv_chan:
|
||||||
event, etype, key, mods, txt = kbmsg.to_tuple()
|
event, etype, key, mods, txt = kbmsg.to_tuple()
|
||||||
|
|
||||||
|
@ -866,19 +989,21 @@ async def handle_keyboard_input(
|
||||||
if mods == Qt.ControlModifier:
|
if mods == Qt.ControlModifier:
|
||||||
ctl = True
|
ctl = True
|
||||||
|
|
||||||
if key in (Qt.Key_Enter, Qt.Key_Return):
|
if key in (
|
||||||
|
Qt.Key_Enter,
|
||||||
await search.chart_current_item(clear_to_cache=True)
|
Qt.Key_Return
|
||||||
|
):
|
||||||
_search_enabled = False
|
_search_enabled = False
|
||||||
continue
|
await search.chart_current_item(clear_to_cache=True)
|
||||||
|
search.show_cache_entries(only=True)
|
||||||
|
view.show_matches()
|
||||||
|
search.focus()
|
||||||
|
|
||||||
elif not ctl and not bar.text():
|
elif not ctl and not bar.text():
|
||||||
# if nothing in search text show the cache
|
|
||||||
view.set_section_entries(
|
# TODO: really should factor this somewhere..bc
|
||||||
'cache',
|
# we're doin it in another spot as well..
|
||||||
list(reversed(godwidget._chart_cache)),
|
search.show_cache_entries(only=True)
|
||||||
clear_all=True,
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# cancel and close
|
# cancel and close
|
||||||
|
@ -887,7 +1012,7 @@ async def handle_keyboard_input(
|
||||||
Qt.Key_Space, # i feel like this is the "native" one
|
Qt.Key_Space, # i feel like this is the "native" one
|
||||||
Qt.Key_Alt,
|
Qt.Key_Alt,
|
||||||
}:
|
}:
|
||||||
search.bar.unfocus()
|
bar.unfocus()
|
||||||
|
|
||||||
# kill the search and focus back on main chart
|
# kill the search and focus back on main chart
|
||||||
if godwidget:
|
if godwidget:
|
||||||
|
@ -935,11 +1060,14 @@ async def handle_keyboard_input(
|
||||||
if item:
|
if item:
|
||||||
parent_item = item.parent()
|
parent_item = item.parent()
|
||||||
|
|
||||||
|
# if we're in the cache section and thus the next
|
||||||
|
# selection is a cache item, switch and show it
|
||||||
|
# immediately since it should be very fast.
|
||||||
if parent_item and parent_item.text() == 'cache':
|
if parent_item and parent_item.text() == 'cache':
|
||||||
|
|
||||||
# if it's a cache item, switch and show it immediately
|
|
||||||
await search.chart_current_item(clear_to_cache=False)
|
await search.chart_current_item(clear_to_cache=False)
|
||||||
|
|
||||||
|
# ACTUAL SEARCH BLOCK #
|
||||||
|
# where we fuzzy complete and fill out sections.
|
||||||
elif not ctl:
|
elif not ctl:
|
||||||
# relay to completer task
|
# relay to completer task
|
||||||
_search_enabled = True
|
_search_enabled = True
|
||||||
|
@ -950,13 +1078,21 @@ async def handle_keyboard_input(
|
||||||
async def search_simple_dict(
|
async def search_simple_dict(
|
||||||
text: str,
|
text: str,
|
||||||
source: dict,
|
source: dict,
|
||||||
|
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
|
|
||||||
|
tokens = []
|
||||||
|
for key in source:
|
||||||
|
if not isinstance(key, str):
|
||||||
|
tokens.extend(key)
|
||||||
|
else:
|
||||||
|
tokens.append(key)
|
||||||
|
|
||||||
# search routine can be specified as a function such
|
# search routine can be specified as a function such
|
||||||
# as in the case of the current app's local symbol cache
|
# as in the case of the current app's local symbol cache
|
||||||
matches = fuzzy.extractBests(
|
matches = fuzzy.extractBests(
|
||||||
text,
|
text,
|
||||||
source.keys(),
|
tokens,
|
||||||
score_cutoff=90,
|
score_cutoff=90,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -240,12 +240,12 @@ def hcolor(name: str) -> str:
|
||||||
'gunmetal': '#91A3B0',
|
'gunmetal': '#91A3B0',
|
||||||
'battleship': '#848482',
|
'battleship': '#848482',
|
||||||
|
|
||||||
|
# default ohlc-bars/curve gray
|
||||||
|
'bracket': '#666666', # like the logo
|
||||||
|
|
||||||
# bluish
|
# bluish
|
||||||
'charcoal': '#36454F',
|
'charcoal': '#36454F',
|
||||||
|
|
||||||
# default bars
|
|
||||||
'bracket': '#666666', # like the logo
|
|
||||||
|
|
||||||
# work well for filled polygons which want a 'bracket' feel
|
# work well for filled polygons which want a 'bracket' feel
|
||||||
# going light to dark
|
# going light to dark
|
||||||
'davies': '#555555',
|
'davies': '#555555',
|
||||||
|
|
|
@ -21,15 +21,29 @@ Qt main window singletons and stuff.
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import time
|
import time
|
||||||
from typing import Callable, Optional, Union
|
from typing import (
|
||||||
|
Callable,
|
||||||
|
Optional,
|
||||||
|
Union,
|
||||||
|
)
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from pyqtgraph import QtGui
|
|
||||||
from PyQt5 import QtCore
|
from PyQt5 import QtCore
|
||||||
from PyQt5.QtWidgets import QLabel, QStatusBar
|
from PyQt5.QtWidgets import (
|
||||||
|
QWidget,
|
||||||
|
QMainWindow,
|
||||||
|
QApplication,
|
||||||
|
QLabel,
|
||||||
|
QStatusBar,
|
||||||
|
)
|
||||||
|
|
||||||
|
from PyQt5.QtGui import (
|
||||||
|
QScreen,
|
||||||
|
QCloseEvent,
|
||||||
|
)
|
||||||
from ..log import get_logger
|
from ..log import get_logger
|
||||||
from ._style import _font_small, hcolor
|
from ._style import _font_small, hcolor
|
||||||
|
from ._chart import GodWidget
|
||||||
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
@ -148,12 +162,13 @@ class MultiStatus:
|
||||||
self.bar.clearMessage()
|
self.bar.clearMessage()
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QtGui.QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
|
|
||||||
# XXX: for tiling wms this should scale
|
# XXX: for tiling wms this should scale
|
||||||
# with the alloted window size.
|
# with the alloted window size.
|
||||||
# TODO: detect for tiling and if untrue set some size?
|
# TODO: detect for tiling and if untrue set some size?
|
||||||
size = (300, 500)
|
# size = (300, 500)
|
||||||
|
godwidget: GodWidget
|
||||||
|
|
||||||
title = 'piker chart (ur symbol is loading bby)'
|
title = 'piker chart (ur symbol is loading bby)'
|
||||||
|
|
||||||
|
@ -162,17 +177,20 @@ class MainWindow(QtGui.QMainWindow):
|
||||||
# self.setMinimumSize(*self.size)
|
# self.setMinimumSize(*self.size)
|
||||||
self.setWindowTitle(self.title)
|
self.setWindowTitle(self.title)
|
||||||
|
|
||||||
|
# set by runtime after `trio` is engaged.
|
||||||
|
self.godwidget: Optional[GodWidget] = None
|
||||||
|
|
||||||
self._status_bar: QStatusBar = None
|
self._status_bar: QStatusBar = None
|
||||||
self._status_label: QLabel = None
|
self._status_label: QLabel = None
|
||||||
self._size: Optional[tuple[int, int]] = None
|
self._size: Optional[tuple[int, int]] = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def mode_label(self) -> QtGui.QLabel:
|
def mode_label(self) -> QLabel:
|
||||||
|
|
||||||
# init mode label
|
# init mode label
|
||||||
if not self._status_label:
|
if not self._status_label:
|
||||||
|
|
||||||
self._status_label = label = QtGui.QLabel()
|
self._status_label = label = QLabel()
|
||||||
label.setStyleSheet(
|
label.setStyleSheet(
|
||||||
f"""QLabel {{
|
f"""QLabel {{
|
||||||
color : {hcolor('gunmetal')};
|
color : {hcolor('gunmetal')};
|
||||||
|
@ -194,8 +212,7 @@ class MainWindow(QtGui.QMainWindow):
|
||||||
|
|
||||||
def closeEvent(
|
def closeEvent(
|
||||||
self,
|
self,
|
||||||
|
event: QCloseEvent,
|
||||||
event: QtGui.QCloseEvent,
|
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''Cancel the root actor asap.
|
'''Cancel the root actor asap.
|
||||||
|
@ -235,8 +252,8 @@ class MainWindow(QtGui.QMainWindow):
|
||||||
def on_focus_change(
|
def on_focus_change(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
last: QtGui.QWidget,
|
last: QWidget,
|
||||||
current: QtGui.QWidget,
|
current: QWidget,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
|
@ -247,11 +264,12 @@ class MainWindow(QtGui.QMainWindow):
|
||||||
name = getattr(current, 'mode_name', '')
|
name = getattr(current, 'mode_name', '')
|
||||||
self.set_mode_name(name)
|
self.set_mode_name(name)
|
||||||
|
|
||||||
def current_screen(self) -> QtGui.QScreen:
|
def current_screen(self) -> QScreen:
|
||||||
"""Get a frickin screen (if we can, gawd).
|
'''
|
||||||
|
Get a frickin screen (if we can, gawd).
|
||||||
|
|
||||||
"""
|
'''
|
||||||
app = QtGui.QApplication.instance()
|
app = QApplication.instance()
|
||||||
|
|
||||||
for _ in range(3):
|
for _ in range(3):
|
||||||
screen = app.screenAt(self.pos())
|
screen = app.screenAt(self.pos())
|
||||||
|
@ -284,7 +302,7 @@ class MainWindow(QtGui.QMainWindow):
|
||||||
'''
|
'''
|
||||||
# https://stackoverflow.com/a/18975846
|
# https://stackoverflow.com/a/18975846
|
||||||
if not size and not self._size:
|
if not size and not self._size:
|
||||||
app = QtGui.QApplication.instance()
|
# app = QApplication.instance()
|
||||||
geo = self.current_screen().geometry()
|
geo = self.current_screen().geometry()
|
||||||
h, w = geo.height(), geo.width()
|
h, w = geo.height(), geo.width()
|
||||||
# use approx 1/3 of the area of the screen by default
|
# use approx 1/3 of the area of the screen by default
|
||||||
|
@ -292,9 +310,36 @@ class MainWindow(QtGui.QMainWindow):
|
||||||
|
|
||||||
self.resize(*size or self._size)
|
self.resize(*size or self._size)
|
||||||
|
|
||||||
|
def resizeEvent(self, event: QtCore.QEvent) -> None:
|
||||||
|
if (
|
||||||
|
# event.spontaneous()
|
||||||
|
event.oldSize().height == event.size().height
|
||||||
|
):
|
||||||
|
event.ignore()
|
||||||
|
return
|
||||||
|
|
||||||
|
# XXX: uncomment for debugging..
|
||||||
|
# attrs = {}
|
||||||
|
# for key in dir(event):
|
||||||
|
# if key == '__dir__':
|
||||||
|
# continue
|
||||||
|
# attr = getattr(event, key)
|
||||||
|
# try:
|
||||||
|
# attrs[key] = attr()
|
||||||
|
# except TypeError:
|
||||||
|
# attrs[key] = attr
|
||||||
|
|
||||||
|
# from pprint import pformat
|
||||||
|
# print(
|
||||||
|
# f'{pformat(attrs)}\n'
|
||||||
|
# f'WINDOW RESIZE: {self.size()}\n\n'
|
||||||
|
# )
|
||||||
|
self.godwidget.on_win_resize(event)
|
||||||
|
event.accept()
|
||||||
|
|
||||||
|
|
||||||
# singleton app per actor
|
# singleton app per actor
|
||||||
_qt_win: QtGui.QMainWindow = None
|
_qt_win: QMainWindow = None
|
||||||
|
|
||||||
|
|
||||||
def main_window() -> MainWindow:
|
def main_window() -> MainWindow:
|
||||||
|
|
|
@ -46,8 +46,10 @@ def _kivy_import_hack():
|
||||||
@click.argument('name', nargs=1, required=True)
|
@click.argument('name', nargs=1, required=True)
|
||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
def monitor(config, rate, name, dhost, test, tl):
|
def monitor(config, rate, name, dhost, test, tl):
|
||||||
"""Start a real-time watchlist UI
|
'''
|
||||||
"""
|
Start a real-time watchlist UI
|
||||||
|
|
||||||
|
'''
|
||||||
# global opts
|
# global opts
|
||||||
brokermod = config['brokermods'][0]
|
brokermod = config['brokermods'][0]
|
||||||
loglevel = config['loglevel']
|
loglevel = config['loglevel']
|
||||||
|
@ -70,8 +72,12 @@ def monitor(config, rate, name, dhost, test, tl):
|
||||||
) as portal:
|
) as portal:
|
||||||
# run app "main"
|
# run app "main"
|
||||||
await _async_main(
|
await _async_main(
|
||||||
name, portal, tickers,
|
name,
|
||||||
brokermod, rate, test=test,
|
portal,
|
||||||
|
tickers,
|
||||||
|
brokermod,
|
||||||
|
rate,
|
||||||
|
test=test,
|
||||||
)
|
)
|
||||||
|
|
||||||
tractor.run(
|
tractor.run(
|
||||||
|
@ -122,7 +128,7 @@ def optschain(config, symbol, date, rate, test):
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option(
|
@click.option(
|
||||||
'--profile',
|
'--profile',
|
||||||
'-p',
|
# '-p',
|
||||||
default=None,
|
default=None,
|
||||||
help='Enable pyqtgraph profiling'
|
help='Enable pyqtgraph profiling'
|
||||||
)
|
)
|
||||||
|
@ -131,9 +137,14 @@ def optschain(config, symbol, date, rate, test):
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
help='Enable tractor debug mode'
|
help='Enable tractor debug mode'
|
||||||
)
|
)
|
||||||
@click.argument('symbol', required=True)
|
@click.argument('symbols', nargs=-1, required=True)
|
||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
def chart(config, symbol, profile, pdb):
|
def chart(
|
||||||
|
config,
|
||||||
|
symbols: list[str],
|
||||||
|
profile,
|
||||||
|
pdb: bool,
|
||||||
|
):
|
||||||
'''
|
'''
|
||||||
Start a real-time chartng UI
|
Start a real-time chartng UI
|
||||||
|
|
||||||
|
@ -144,8 +155,10 @@ def chart(config, symbol, profile, pdb):
|
||||||
_profile._pg_profile = True
|
_profile._pg_profile = True
|
||||||
_profile.ms_slower_then = float(profile)
|
_profile.ms_slower_then = float(profile)
|
||||||
|
|
||||||
|
# Qt UI entrypoint
|
||||||
from ._app import _main
|
from ._app import _main
|
||||||
|
|
||||||
|
for symbol in symbols:
|
||||||
if '.' not in symbol:
|
if '.' not in symbol:
|
||||||
click.echo(click.style(
|
click.echo(click.style(
|
||||||
f'symbol: {symbol} must have a {symbol}.<provider> suffix',
|
f'symbol: {symbol} must have a {symbol}.<provider> suffix',
|
||||||
|
@ -153,15 +166,16 @@ def chart(config, symbol, profile, pdb):
|
||||||
))
|
))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
# global opts
|
# global opts
|
||||||
brokernames = config['brokers']
|
brokernames = config['brokers']
|
||||||
|
brokermods = config['brokermods']
|
||||||
|
assert brokermods
|
||||||
tractorloglevel = config['tractorloglevel']
|
tractorloglevel = config['tractorloglevel']
|
||||||
pikerloglevel = config['loglevel']
|
pikerloglevel = config['loglevel']
|
||||||
|
|
||||||
_main(
|
_main(
|
||||||
sym=symbol,
|
syms=symbols,
|
||||||
brokernames=brokernames,
|
brokermods=brokermods,
|
||||||
piker_loglevel=pikerloglevel,
|
piker_loglevel=pikerloglevel,
|
||||||
tractor_kwargs={
|
tractor_kwargs={
|
||||||
'debug_mode': pdb,
|
'debug_mode': pdb,
|
||||||
|
@ -170,5 +184,6 @@ def chart(config, symbol, profile, pdb):
|
||||||
'enable_modules': [
|
'enable_modules': [
|
||||||
'piker.clearing._client'
|
'piker.clearing._client'
|
||||||
],
|
],
|
||||||
|
'registry_addr': config.get('registry_addr'),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,3 +0,0 @@
|
||||||
"""
|
|
||||||
Super hawt Qt UI components
|
|
||||||
"""
|
|
|
@ -1,67 +0,0 @@
|
||||||
import sys
|
|
||||||
|
|
||||||
from PySide2.QtCharts import QtCharts
|
|
||||||
from PySide2.QtWidgets import QApplication, QMainWindow
|
|
||||||
from PySide2.QtCore import Qt, QPointF
|
|
||||||
from PySide2 import QtGui
|
|
||||||
import qdarkstyle
|
|
||||||
|
|
||||||
data = ((1, 7380, 7520, 7380, 7510, 7324),
|
|
||||||
(2, 7520, 7580, 7410, 7440, 7372),
|
|
||||||
(3, 7440, 7650, 7310, 7520, 7434),
|
|
||||||
(4, 7450, 7640, 7450, 7550, 7480),
|
|
||||||
(5, 7510, 7590, 7460, 7490, 7502),
|
|
||||||
(6, 7500, 7590, 7480, 7560, 7512),
|
|
||||||
(7, 7560, 7830, 7540, 7800, 7584))
|
|
||||||
|
|
||||||
|
|
||||||
app = QApplication([])
|
|
||||||
# set dark stylesheet
|
|
||||||
# import pdb; pdb.set_trace()
|
|
||||||
app.setStyleSheet(qdarkstyle.load_stylesheet_pyside())
|
|
||||||
|
|
||||||
series = QtCharts.QCandlestickSeries()
|
|
||||||
series.setDecreasingColor(Qt.darkRed)
|
|
||||||
series.setIncreasingColor(Qt.darkGreen)
|
|
||||||
|
|
||||||
ma5 = QtCharts.QLineSeries() # 5-days average data line
|
|
||||||
tm = [] # stores str type data
|
|
||||||
|
|
||||||
# in a loop, series and ma5 append corresponding data
|
|
||||||
for num, o, h, l, c, m in data:
|
|
||||||
candle = QtCharts.QCandlestickSet(o, h, l, c)
|
|
||||||
series.append(candle)
|
|
||||||
ma5.append(QPointF(num, m))
|
|
||||||
tm.append(str(num))
|
|
||||||
|
|
||||||
pen = candle.pen()
|
|
||||||
# import pdb; pdb.set_trace()
|
|
||||||
|
|
||||||
chart = QtCharts.QChart()
|
|
||||||
|
|
||||||
# import pdb; pdb.set_trace()
|
|
||||||
series.setBodyOutlineVisible(False)
|
|
||||||
series.setCapsVisible(False)
|
|
||||||
# brush = QtGui.QBrush()
|
|
||||||
# brush.setColor(Qt.green)
|
|
||||||
# series.setBrush(brush)
|
|
||||||
chart.addSeries(series) # candle
|
|
||||||
chart.addSeries(ma5) # ma5 line
|
|
||||||
|
|
||||||
chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations)
|
|
||||||
chart.createDefaultAxes()
|
|
||||||
chart.legend().hide()
|
|
||||||
|
|
||||||
chart.axisX(series).setCategories(tm)
|
|
||||||
chart.axisX(ma5).setVisible(False)
|
|
||||||
|
|
||||||
view = QtCharts.QChartView(chart)
|
|
||||||
view.chart().setTheme(QtCharts.QChart.ChartTheme.ChartThemeDark)
|
|
||||||
view.setRubberBand(QtCharts.QChartView.HorizontalRubberBand)
|
|
||||||
# chartview.chart().setTheme(QtCharts.QChart.ChartTheme.ChartThemeBlueCerulean)
|
|
||||||
|
|
||||||
ui = QMainWindow()
|
|
||||||
# ui.setGeometry(50, 50, 500, 300)
|
|
||||||
ui.setCentralWidget(view)
|
|
||||||
ui.show()
|
|
||||||
sys.exit(app.exec_())
|
|
|
@ -1,13 +1,12 @@
|
||||||
# we require a pinned dev branch to get some edge features that
|
# we require a pinned dev branch to get some edge features that
|
||||||
# are often untested in tractor's CI and/or being tested by us
|
# are often untested in tractor's CI and/or being tested by us
|
||||||
# first before committing as core features in tractor's base.
|
# first before committing as core features in tractor's base.
|
||||||
-e git+https://github.com/goodboy/tractor.git@master#egg=tractor
|
-e git+https://github.com/goodboy/tractor.git@piker_pin#egg=tractor
|
||||||
|
|
||||||
# `pyqtgraph` peeps keep breaking, fixing, improving so might as well
|
# `pyqtgraph` peeps keep breaking, fixing, improving so might as well
|
||||||
# pin this to a dev branch that we have more control over especially
|
# pin this to a dev branch that we have more control over especially
|
||||||
# as more graphics stuff gets hashed out.
|
# as more graphics stuff gets hashed out.
|
||||||
-e git+https://github.com/pikers/pyqtgraph.git@piker_pin#egg=pyqtgraph
|
-e git+https://github.com/pikers/pyqtgraph.git@master#egg=pyqtgraph
|
||||||
|
|
||||||
|
|
||||||
# our async client for ``marketstore`` (the tsdb)
|
# our async client for ``marketstore`` (the tsdb)
|
||||||
-e git+https://github.com/pikers/anyio-marketstore.git@master#egg=anyio-marketstore
|
-e git+https://github.com/pikers/anyio-marketstore.git@master#egg=anyio-marketstore
|
||||||
|
@ -18,4 +17,7 @@
|
||||||
|
|
||||||
|
|
||||||
# ``asyncvnc`` for sending interactions to ib-gw inside docker
|
# ``asyncvnc`` for sending interactions to ib-gw inside docker
|
||||||
-e git+https://github.com/pikers/asyncvnc.git@vid_passthrough#egg=asyncvnc
|
-e git+https://github.com/pikers/asyncvnc.git@main#egg=asyncvnc
|
||||||
|
|
||||||
|
# ``cryptofeed`` for connecting to various crypto exchanges + custom fixes
|
||||||
|
-e git+https://github.com/pikers/cryptofeed.git@date_parsing#egg=cryptofeed
|
||||||
|
|
8
setup.py
8
setup.py
|
@ -41,26 +41,28 @@ setup(
|
||||||
},
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'toml',
|
'toml',
|
||||||
|
'tomli', # fastest pure py reader
|
||||||
'click',
|
'click',
|
||||||
'colorlog',
|
'colorlog',
|
||||||
'attrs',
|
'attrs',
|
||||||
'pygments',
|
'pygments',
|
||||||
'colorama', # numba traceback coloring
|
'colorama', # numba traceback coloring
|
||||||
'pydantic', # structured data
|
'msgspec', # performant IPC messaging and structs
|
||||||
|
'protobuf',
|
||||||
|
|
||||||
# async
|
# async
|
||||||
'trio',
|
'trio',
|
||||||
'trio-websocket',
|
'trio-websocket',
|
||||||
'msgspec', # performant IPC messaging
|
|
||||||
'async_generator',
|
'async_generator',
|
||||||
|
|
||||||
# from github currently (see requirements.txt)
|
# from github currently (see requirements.txt)
|
||||||
# 'trimeter', # not released yet..
|
# 'trimeter', # not released yet..
|
||||||
# 'tractor',
|
# 'tractor',
|
||||||
# asyncvnc,
|
# asyncvnc,
|
||||||
|
# 'cryptofeed',
|
||||||
|
|
||||||
# brokers
|
# brokers
|
||||||
'asks==2.4.8',
|
'asks',
|
||||||
'ib_insync',
|
'ib_insync',
|
||||||
|
|
||||||
# numerics
|
# numerics
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import tractor
|
import tractor
|
||||||
import trio
|
from piker import (
|
||||||
from piker import log, config
|
# log,
|
||||||
from piker.brokers import questrade
|
config,
|
||||||
|
)
|
||||||
|
from piker._daemon import (
|
||||||
|
Services,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
|
@ -14,15 +19,6 @@ def pytest_addoption(parser):
|
||||||
help="Use a practice API account")
|
help="Use a practice API account")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session', autouse=True)
|
|
||||||
def loglevel(request):
|
|
||||||
orig = tractor.log._default_loglevel
|
|
||||||
level = tractor.log._default_loglevel = request.config.option.loglevel
|
|
||||||
log.get_console_log(level)
|
|
||||||
yield level
|
|
||||||
tractor.log._default_loglevel = orig
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def test_config():
|
def test_config():
|
||||||
dirname = os.path.dirname
|
dirname = os.path.dirname
|
||||||
|
@ -37,9 +33,11 @@ def test_config():
|
||||||
|
|
||||||
@pytest.fixture(scope='session', autouse=True)
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
def confdir(request, test_config):
|
def confdir(request, test_config):
|
||||||
"""If the `--confdir` flag is not passed use the
|
'''
|
||||||
|
If the `--confdir` flag is not passed use the
|
||||||
broker config file found in that dir.
|
broker config file found in that dir.
|
||||||
"""
|
|
||||||
|
'''
|
||||||
confdir = request.config.option.confdir
|
confdir = request.config.option.confdir
|
||||||
if confdir is not None:
|
if confdir is not None:
|
||||||
config._override_config_dir(confdir)
|
config._override_config_dir(confdir)
|
||||||
|
@ -47,49 +45,61 @@ def confdir(request, test_config):
|
||||||
return confdir
|
return confdir
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session', autouse=True)
|
# @pytest.fixture(scope='session', autouse=True)
|
||||||
def travis(confdir):
|
# def travis(confdir):
|
||||||
is_travis = os.environ.get('TRAVIS', False)
|
# is_travis = os.environ.get('TRAVIS', False)
|
||||||
if is_travis:
|
# if is_travis:
|
||||||
# this directory is cached, see .travis.yaml
|
# # this directory is cached, see .travis.yaml
|
||||||
conf_file = config.get_broker_conf_path()
|
# conf_file = config.get_broker_conf_path()
|
||||||
refresh_token = os.environ['QT_REFRESH_TOKEN']
|
# refresh_token = os.environ['QT_REFRESH_TOKEN']
|
||||||
|
|
||||||
def write_with_token(token):
|
# def write_with_token(token):
|
||||||
# XXX don't pass the dir path here since may be
|
# # XXX don't pass the dir path here since may be
|
||||||
# written behind the scenes in the `confdir fixture`
|
# # written behind the scenes in the `confdir fixture`
|
||||||
if not os.path.isfile(conf_file):
|
# if not os.path.isfile(conf_file):
|
||||||
open(conf_file, 'w').close()
|
# open(conf_file, 'w').close()
|
||||||
conf, path = config.load()
|
# conf, path = config.load()
|
||||||
conf.setdefault('questrade', {}).update(
|
# conf.setdefault('questrade', {}).update(
|
||||||
{'refresh_token': token,
|
# {'refresh_token': token,
|
||||||
'is_practice': 'True'}
|
# 'is_practice': 'True'}
|
||||||
)
|
# )
|
||||||
config.write(conf, path)
|
# config.write(conf, path)
|
||||||
|
|
||||||
async def ensure_config():
|
# async def ensure_config():
|
||||||
# try to refresh current token using cached brokers config
|
# # try to refresh current token using cached brokers config
|
||||||
# if it fails fail try using the refresh token provided by the
|
# # if it fails fail try using the refresh token provided by the
|
||||||
# env var and if that fails stop the test run here.
|
# # env var and if that fails stop the test run here.
|
||||||
try:
|
# try:
|
||||||
async with questrade.get_client(ask_user=False):
|
# async with questrade.get_client(ask_user=False):
|
||||||
pass
|
# pass
|
||||||
except (
|
# except (
|
||||||
FileNotFoundError, ValueError,
|
# FileNotFoundError, ValueError,
|
||||||
questrade.BrokerError, questrade.QuestradeError,
|
# questrade.BrokerError, questrade.QuestradeError,
|
||||||
trio.MultiError,
|
# trio.MultiError,
|
||||||
):
|
# ):
|
||||||
# 3 cases:
|
# # 3 cases:
|
||||||
# - config doesn't have a ``refresh_token`` k/v
|
# # - config doesn't have a ``refresh_token`` k/v
|
||||||
# - cache dir does not exist yet
|
# # - cache dir does not exist yet
|
||||||
# - current token is expired; take it form env var
|
# # - current token is expired; take it form env var
|
||||||
write_with_token(refresh_token)
|
# write_with_token(refresh_token)
|
||||||
|
|
||||||
async with questrade.get_client(ask_user=False):
|
# async with questrade.get_client(ask_user=False):
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
# XXX ``pytest_trio`` doesn't support scope or autouse
|
# # XXX ``pytest_trio`` doesn't support scope or autouse
|
||||||
trio.run(ensure_config)
|
# trio.run(ensure_config)
|
||||||
|
|
||||||
|
|
||||||
|
_ci_env: bool = os.environ.get('CI', False)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session')
|
||||||
|
def ci_env() -> bool:
|
||||||
|
'''
|
||||||
|
Detect CI envoirment.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return _ci_env
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -105,3 +115,61 @@ def tmx_symbols():
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def cse_symbols():
|
def cse_symbols():
|
||||||
return ['TRUL.CN', 'CWEB.CN', 'SNN.CN']
|
return ['TRUL.CN', 'CWEB.CN', 'SNN.CN']
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def _open_test_pikerd(
|
||||||
|
reg_addr: tuple[str, int] | None = None,
|
||||||
|
**kwargs,
|
||||||
|
|
||||||
|
) -> tuple[
|
||||||
|
str,
|
||||||
|
int,
|
||||||
|
tractor.Portal
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Testing helper to startup the service tree and runtime on
|
||||||
|
a different port then the default to allow testing alongside
|
||||||
|
a running stack.
|
||||||
|
|
||||||
|
'''
|
||||||
|
import random
|
||||||
|
from piker._daemon import maybe_open_pikerd
|
||||||
|
|
||||||
|
if reg_addr is None:
|
||||||
|
port = random.randint(6e3, 7e3)
|
||||||
|
reg_addr = ('127.0.0.1', port)
|
||||||
|
|
||||||
|
# try:
|
||||||
|
async with (
|
||||||
|
maybe_open_pikerd(
|
||||||
|
registry_addr=reg_addr,
|
||||||
|
**kwargs,
|
||||||
|
) as service_manager,
|
||||||
|
):
|
||||||
|
# this proc/actor is the pikerd
|
||||||
|
assert service_manager is Services
|
||||||
|
|
||||||
|
async with tractor.wait_for_actor(
|
||||||
|
'pikerd',
|
||||||
|
arbiter_sockaddr=reg_addr,
|
||||||
|
) as portal:
|
||||||
|
raddr = portal.channel.raddr
|
||||||
|
assert raddr == reg_addr
|
||||||
|
yield (
|
||||||
|
raddr[0],
|
||||||
|
raddr[1],
|
||||||
|
portal,
|
||||||
|
service_manager,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def open_test_pikerd():
|
||||||
|
|
||||||
|
yield _open_test_pikerd
|
||||||
|
|
||||||
|
# TODO: teardown checks such as,
|
||||||
|
# - no leaked subprocs or shm buffers
|
||||||
|
# - all requested container service are torn down
|
||||||
|
# - certain ``tractor`` runtime state?
|
||||||
|
|
|
@ -0,0 +1,128 @@
|
||||||
|
'''
|
||||||
|
Data feed layer APIs, performance, msg throttling.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from collections import Counter
|
||||||
|
from pprint import pprint
|
||||||
|
from typing import AsyncContextManager
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
# import tractor
|
||||||
|
import trio
|
||||||
|
from piker.data import (
|
||||||
|
ShmArray,
|
||||||
|
open_feed,
|
||||||
|
)
|
||||||
|
from piker.data._source import (
|
||||||
|
unpack_fqsn,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'fqsns',
|
||||||
|
[
|
||||||
|
# binance
|
||||||
|
(100, {'btcusdt.binance', 'ethusdt.binance'}, False),
|
||||||
|
|
||||||
|
# kraken
|
||||||
|
(20, {'ethusdt.kraken', 'xbtusd.kraken'}, True),
|
||||||
|
|
||||||
|
# binance + kraken
|
||||||
|
(100, {'btcusdt.binance', 'xbtusd.kraken'}, False),
|
||||||
|
],
|
||||||
|
ids=lambda param: f'quotes={param[0]}@fqsns={param[1]}',
|
||||||
|
)
|
||||||
|
def test_multi_fqsn_feed(
|
||||||
|
open_test_pikerd: AsyncContextManager,
|
||||||
|
fqsns: set[str],
|
||||||
|
ci_env: bool
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Start a real-time data feed for provided fqsn and pull
|
||||||
|
a few quotes then simply shut down.
|
||||||
|
|
||||||
|
'''
|
||||||
|
max_quotes, fqsns, run_in_ci = fqsns
|
||||||
|
|
||||||
|
if (
|
||||||
|
ci_env
|
||||||
|
and not run_in_ci
|
||||||
|
):
|
||||||
|
pytest.skip('Skipping CI disabled test due to feed restrictions')
|
||||||
|
|
||||||
|
brokers = set()
|
||||||
|
for fqsn in fqsns:
|
||||||
|
brokername, key, suffix = unpack_fqsn(fqsn)
|
||||||
|
brokers.add(brokername)
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with (
|
||||||
|
open_test_pikerd(),
|
||||||
|
open_feed(
|
||||||
|
fqsns,
|
||||||
|
loglevel='info',
|
||||||
|
|
||||||
|
# TODO: ensure throttle rate is applied
|
||||||
|
# limit to at least display's FPS
|
||||||
|
# avoiding needless Qt-in-guest-mode context switches
|
||||||
|
# tick_throttle=_quote_throttle_rate,
|
||||||
|
|
||||||
|
) as feed
|
||||||
|
):
|
||||||
|
# verify shm buffers exist
|
||||||
|
for fqin in fqsns:
|
||||||
|
flume = feed.flumes[fqin]
|
||||||
|
ohlcv: ShmArray = flume.rt_shm
|
||||||
|
hist_ohlcv: ShmArray = flume.hist_shm
|
||||||
|
|
||||||
|
async with feed.open_multi_stream(brokers) as stream:
|
||||||
|
|
||||||
|
# pull the first startup quotes, one for each fqsn, and
|
||||||
|
# ensure they match each flume's startup quote value.
|
||||||
|
fqsns_copy = fqsns.copy()
|
||||||
|
with trio.fail_after(0.5):
|
||||||
|
for _ in range(1):
|
||||||
|
first_quotes = await stream.receive()
|
||||||
|
for fqsn, quote in first_quotes.items():
|
||||||
|
|
||||||
|
# XXX: TODO: WTF apparently this error will get
|
||||||
|
# supressed and only show up in the teardown
|
||||||
|
# excgroup if we don't have the fix from
|
||||||
|
# <tractorbugurl>
|
||||||
|
# assert 0
|
||||||
|
|
||||||
|
fqsns_copy.remove(fqsn)
|
||||||
|
flume = feed.flumes[fqsn]
|
||||||
|
assert quote['last'] == flume.first_quote['last']
|
||||||
|
|
||||||
|
cntr = Counter()
|
||||||
|
with trio.fail_after(6):
|
||||||
|
async for quotes in stream:
|
||||||
|
for fqsn, quote in quotes.items():
|
||||||
|
cntr[fqsn] += 1
|
||||||
|
|
||||||
|
# await tractor.breakpoint()
|
||||||
|
flume = feed.flumes[fqsn]
|
||||||
|
ohlcv: ShmArray = flume.rt_shm
|
||||||
|
hist_ohlcv: ShmArray = flume.hist_shm
|
||||||
|
|
||||||
|
# print quote msg, rt and history
|
||||||
|
# buffer values on console.
|
||||||
|
rt_row = ohlcv.array[-1]
|
||||||
|
hist_row = hist_ohlcv.array[-1]
|
||||||
|
# last = quote['last']
|
||||||
|
|
||||||
|
# assert last == rt_row['close']
|
||||||
|
# assert last == hist_row['close']
|
||||||
|
pprint(
|
||||||
|
f'{fqsn}: {quote}\n'
|
||||||
|
f'rt_ohlc: {rt_row}\n'
|
||||||
|
f'hist_ohlc: {hist_row}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
if cntr.total() >= max_quotes:
|
||||||
|
break
|
||||||
|
|
||||||
|
assert set(cntr.keys()) == fqsns
|
||||||
|
|
||||||
|
trio.run(main)
|
|
@ -8,7 +8,6 @@ from trio.testing import trio_test
|
||||||
from piker.brokers import questrade as qt
|
from piker.brokers import questrade as qt
|
||||||
import pytest
|
import pytest
|
||||||
import tractor
|
import tractor
|
||||||
from tractor.testing import tractor_test
|
|
||||||
|
|
||||||
import piker
|
import piker
|
||||||
from piker.brokers import get_brokermod
|
from piker.brokers import get_brokermod
|
||||||
|
@ -23,6 +22,12 @@ pytestmark = pytest.mark.skipif(
|
||||||
reason="questrade tests can only be run locally with an API key",
|
reason="questrade tests can only be run locally with an API key",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# TODO: this module was removed from tractor into it's
|
||||||
|
# tests/conftest.py, we need to rewrite the below tests
|
||||||
|
# to use the `open_pikerd_runtime()` to make these work again
|
||||||
|
# (if we're not just gonna junk em).
|
||||||
|
# from tractor.testing import tractor_test
|
||||||
|
|
||||||
|
|
||||||
# stock quote
|
# stock quote
|
||||||
_ex_quotes = {
|
_ex_quotes = {
|
||||||
|
@ -106,7 +111,7 @@ def match_packet(symbols, quotes, feed_type='stock'):
|
||||||
assert not quotes
|
assert not quotes
|
||||||
|
|
||||||
|
|
||||||
@tractor_test
|
# @tractor_test
|
||||||
async def test_concurrent_tokens_refresh(us_symbols, loglevel):
|
async def test_concurrent_tokens_refresh(us_symbols, loglevel):
|
||||||
"""Verify that concurrent requests from mulitple tasks work alongside
|
"""Verify that concurrent requests from mulitple tasks work alongside
|
||||||
random token refreshing which simulates an access token expiry + refresh
|
random token refreshing which simulates an access token expiry + refresh
|
||||||
|
@ -337,7 +342,7 @@ async def stream_stocks(feed, symbols):
|
||||||
'options_and_options',
|
'options_and_options',
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@tractor_test
|
# @tractor_test
|
||||||
async def test_quote_streaming(tmx_symbols, loglevel, stream_what):
|
async def test_quote_streaming(tmx_symbols, loglevel, stream_what):
|
||||||
"""Set up option streaming using the broker daemon.
|
"""Set up option streaming using the broker daemon.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
'''
|
||||||
|
Actor tree daemon sub-service verifications
|
||||||
|
|
||||||
|
'''
|
||||||
|
from typing import AsyncContextManager
|
||||||
|
from contextlib import asynccontextmanager as acm
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import trio
|
||||||
|
import tractor
|
||||||
|
|
||||||
|
from piker._daemon import (
|
||||||
|
find_service,
|
||||||
|
check_for_service,
|
||||||
|
Services,
|
||||||
|
)
|
||||||
|
from piker.data import (
|
||||||
|
open_feed,
|
||||||
|
)
|
||||||
|
from piker.clearing import (
|
||||||
|
open_ems,
|
||||||
|
)
|
||||||
|
from piker.clearing._messages import (
|
||||||
|
BrokerdPosition,
|
||||||
|
Status,
|
||||||
|
)
|
||||||
|
from piker.clearing._client import (
|
||||||
|
OrderBook,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_runtime_boot(
|
||||||
|
open_test_pikerd: AsyncContextManager
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Verify we can boot the `pikerd` service stack using the
|
||||||
|
`open_test_pikerd` fixture helper and that registry address details
|
||||||
|
match up.
|
||||||
|
|
||||||
|
'''
|
||||||
|
async def main():
|
||||||
|
port = 6666
|
||||||
|
daemon_addr = ('127.0.0.1', port)
|
||||||
|
services: Services
|
||||||
|
|
||||||
|
async with (
|
||||||
|
open_test_pikerd(
|
||||||
|
reg_addr=daemon_addr,
|
||||||
|
) as (_, _, pikerd_portal, services),
|
||||||
|
|
||||||
|
tractor.wait_for_actor(
|
||||||
|
'pikerd',
|
||||||
|
arbiter_sockaddr=daemon_addr,
|
||||||
|
) as portal,
|
||||||
|
):
|
||||||
|
assert pikerd_portal.channel.raddr == daemon_addr
|
||||||
|
assert pikerd_portal.channel.raddr == portal.channel.raddr
|
||||||
|
|
||||||
|
trio.run(main)
|
||||||
|
|
||||||
|
|
||||||
|
@acm
|
||||||
|
async def ensure_service(
|
||||||
|
name: str,
|
||||||
|
sockaddr: tuple[str, int] | None = None,
|
||||||
|
) -> None:
|
||||||
|
async with find_service(name) as portal:
|
||||||
|
remote_sockaddr = portal.channel.raddr
|
||||||
|
print(f'FOUND `{name}` @ {remote_sockaddr}')
|
||||||
|
|
||||||
|
if sockaddr:
|
||||||
|
assert remote_sockaddr == sockaddr
|
||||||
|
|
||||||
|
yield portal
|
||||||
|
|
||||||
|
|
||||||
|
def test_ensure_datafeed_actors(
|
||||||
|
open_test_pikerd: AsyncContextManager
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Verify that booting a data feed starts a `brokerd`
|
||||||
|
actor and a singleton global `samplerd` and opening
|
||||||
|
an order mode in paper opens the `paperboi` service.
|
||||||
|
|
||||||
|
'''
|
||||||
|
actor_name: str = 'brokerd'
|
||||||
|
backend: str = 'kraken'
|
||||||
|
brokerd_name: str = f'{actor_name}.{backend}'
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with (
|
||||||
|
open_test_pikerd(),
|
||||||
|
open_feed(
|
||||||
|
['xbtusdt.kraken'],
|
||||||
|
loglevel='info',
|
||||||
|
) as feed
|
||||||
|
):
|
||||||
|
# halt rt quote streams since we aren't testing them
|
||||||
|
await feed.pause()
|
||||||
|
|
||||||
|
async with (
|
||||||
|
ensure_service(brokerd_name),
|
||||||
|
ensure_service('samplerd'),
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|
||||||
|
trio.run(main)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ensure_ems_in_paper_actors(
|
||||||
|
open_test_pikerd: AsyncContextManager
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
actor_name: str = 'brokerd'
|
||||||
|
backend: str = 'kraken'
|
||||||
|
brokerd_name: str = f'{actor_name}.{backend}'
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
|
||||||
|
# type declares
|
||||||
|
book: OrderBook
|
||||||
|
trades_stream: tractor.MsgStream
|
||||||
|
pps: dict[str, list[BrokerdPosition]]
|
||||||
|
accounts: list[str]
|
||||||
|
dialogs: dict[str, Status]
|
||||||
|
|
||||||
|
# ensure we timeout after is startup is too slow.
|
||||||
|
# TODO: something like this should be our start point for
|
||||||
|
# benchmarking end-to-end startup B)
|
||||||
|
with trio.fail_after(9):
|
||||||
|
async with (
|
||||||
|
open_test_pikerd() as (_, _, _, services),
|
||||||
|
|
||||||
|
open_ems(
|
||||||
|
'xbtusdt.kraken',
|
||||||
|
mode='paper',
|
||||||
|
) as (
|
||||||
|
book,
|
||||||
|
trades_stream,
|
||||||
|
pps,
|
||||||
|
accounts,
|
||||||
|
dialogs,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
# there should be no on-going positions,
|
||||||
|
# TODO: though eventually we'll want to validate against
|
||||||
|
# local ledger and `pps.toml` state ;)
|
||||||
|
assert not pps
|
||||||
|
assert not dialogs
|
||||||
|
|
||||||
|
pikerd_subservices = ['emsd', 'samplerd']
|
||||||
|
|
||||||
|
async with (
|
||||||
|
ensure_service('emsd'),
|
||||||
|
ensure_service(brokerd_name),
|
||||||
|
ensure_service(f'paperboi.{backend}'),
|
||||||
|
):
|
||||||
|
for name in pikerd_subservices:
|
||||||
|
assert name in services.service_tasks
|
||||||
|
|
||||||
|
# brokerd.kraken actor should have been started
|
||||||
|
# implicitly by the ems.
|
||||||
|
assert brokerd_name in services.service_tasks
|
||||||
|
|
||||||
|
print('ALL SERVICES STARTED, terminating..')
|
||||||
|
await services.cancel_service('emsd')
|
||||||
|
|
||||||
|
with pytest.raises(
|
||||||
|
tractor._exceptions.ContextCancelled,
|
||||||
|
) as exc_info:
|
||||||
|
trio.run(main)
|
||||||
|
|
||||||
|
cancel_msg: str = '`_emsd_main()` was remotely cancelled by its caller'
|
||||||
|
assert cancel_msg in exc_info.value.args[0]
|
Loading…
Reference in New Issue