├── .gitignore ├── LICENSE ├── README.md ├── cmd ├── MCSellSideSim │ └── main.go ├── MCSimulator │ └── main.go └── OrderRouter │ └── main.go ├── core ├── account │ └── todo ├── algo │ └── todo ├── compliance │ └── todo ├── logger │ ├── handlers │ │ └── handleES.go │ ├── init.go │ ├── logger.go │ └── logger_test.go ├── messagebus │ ├── config.go │ ├── messagebus.go │ └── test │ │ └── testBroker.go ├── order │ ├── order.go │ └── order_test.go ├── pricing │ └── todo ├── security │ └── todo ├── service │ ├── config.go │ ├── config_test.go │ ├── init.go │ ├── service.go │ └── service_test.go └── static │ └── todo ├── database ├── memstore │ ├── copy.go │ └── orderstore.go ├── orderstore.go └── pgsql │ ├── procs │ ├── execution.sql │ └── order.sql │ └── tables │ ├── 1.enums.sql │ ├── 2.users.sql │ └── 3.ems.sql ├── doc.go ├── proto ├── Makefile ├── order │ ├── order.pb.go │ ├── order.proto │ └── orderpb_test.go ├── pricing │ ├── pricing.pb.go │ ├── pricing.proto │ └── pricingpb_test.go └── service │ ├── service.pb.go │ ├── service.proto │ └── servicepb_test.go ├── services ├── marketconnectors │ ├── common │ │ ├── alloc │ │ │ └── todo │ │ ├── config.go │ │ ├── equity │ │ │ └── todo │ │ ├── future │ │ │ └── todo │ │ ├── fx │ │ │ └── todo │ │ ├── limit │ │ │ └── todo │ │ ├── options │ │ │ └── todo │ │ ├── order │ │ │ ├── execution_fix44.go │ │ │ ├── fixclient.go │ │ │ └── ordercxlreject_fix44.go │ │ └── trs │ │ │ └── todo │ ├── sellsidesim │ │ ├── config.go │ │ ├── executor.go │ │ ├── orderbook │ │ │ └── orderbook.go │ │ ├── sellsidesim.go │ │ └── sellsidesim_test.go │ ├── simulator │ │ ├── simulator.go │ │ └── simulator_test.go │ └── util.go └── orderrouter │ ├── config.go │ ├── orderrouter.go │ └── orderrouter_test.go └── test ├── benchmark └── client2fix_test.go └── order └── mockorder.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | .idea 10 | *.iml 11 | 12 | # Architecture specific extensions/prefixes 13 | *.[568vq] 14 | [568vq].out 15 | 16 | *.cgo1.go 17 | *.cgo2.c 18 | _cgo_defun.c 19 | _cgo_gotypes.go 20 | _cgo_export.* 21 | 22 | _testmain.go 23 | 24 | *.exe 25 | *.test 26 | *.prof 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GoTrade 2 | 3 | > GoTrade is a **FIX** protocol electronic trading and order management system written in Golang, structured for typical multi-asset instituional use 4 | 5 |

6 | 7 |

8 | 9 | [![GoDoc](https://godoc.org/github.com/cyanly/gotrade?status.png)](https://godoc.org/github.com/cyanly/gotrade) [![Build Status](https://travis-ci.org/quickfixgo/quickfix.svg?branch=master)](https://travis-ci.org/quickfixgo/quickfix) 10 | 11 | ## Status 12 | This project is currently more of a proof of concept. It is no where near in completeness of a commerical product. This public repo serves as mostly for the purpose of experimenting and share of ideas. 13 | 14 | ## Getting Started 15 | ``` 16 | $ go get -u github.com/cyanly/gotrade 17 | 18 | ``` 19 | 20 | ## Features 21 | 22 | - [x] Trade in real-time via FIX through the broker-neutral API. 23 | - [x] Normalized FIX order flow behavior across multiple FIX versions and asset classes. 24 | - [x] Pure Go. 25 | - [x] Platform neutral: write once, build for any operating systems and arch (Linux/Windows/OSX etc). 26 | - [x] Native code performance. 27 | - [x] Ease of deployment. 28 | - [x] Lack of OOP verbosity, works for small and big teams. 29 | - [x] Protobuf. 30 | - [x] Binary encoding format, efficient yet extensible. 31 | - [x] Easy Language Interoperability (C++, Python, Java, C#, Javascript, etc). 32 | - [x] Protocol backward compatibility. 33 | 34 | ## Design 35 | ``` 36 | └─ gotrade/ 37 | ├─ core/ -> The low-level API that gives consumers all the knobs they need 38 | │ ├─ order/ 39 | │ │ └─ execution/ 40 | │ ├─ service/ 41 | └─ proto/... -> Protobuf messaging protocol of various entities 42 | └─ services/ -> Core services managing multi-asset order flow 43 | │ ├─ orderrouter/ -> Centralized management facility for multi-asset global order flow 44 | │ ├─ marketconnectors/ -> Managing FIX connection to each trading venue, also performs pre-trade risk checks 45 | │ 46 | └─cmd/... -> Command-line executables, binary build targets 47 | 48 | ``` 49 | 50 | 51 | ## Examples 52 | 53 | The best way to see goTrade in action is to take a look at tests (see Benchmark section below): 54 | 55 | Pre-Requisites: 56 | - Go 1.4 or higher 57 | - get dependencies: 58 | ``` 59 | $ cd $GOPATH/src/github.com/cyanly/gotrade 60 | $ go get -u -t ./... 61 | ``` 62 | 63 | **OrderRouter** and **MarketConnector** test cases will mock a testdb and messaging bus for end-to-end, message to message test. 64 | ``` 65 | $ cd $GOPATH/src/github.com/cyanly/gotrade/services/orderrouter 66 | $ go test -v 67 | 68 | $ cd $GOPATH/src/github.com/cyanly/gotrade/services/marketconnectors/simulator 69 | $ go test -v 70 | ``` 71 | 72 |

73 | 74 |

75 | 76 | ## Benchmark 77 | 78 | Machine: `Intel Core i5 CPU @ 2.80GHz` + `Ubuntu 14.04 Desktop x86_64` 79 | 80 | - `test/benchmark/client2fix_test.go` 81 | - **CL ⇒ OR**:
*Client send order protobuf to OrderRouter(OR)* 82 | - **OR ⇒ MC**:
*OrderRouter process order and dispatch persisted order entity to target MarketConnector* 83 | - **MC ⇒ FIX**:
*MarketConnector translate into NewOrderSingle FIX message based on the session with its counterparty* 84 | - **FIX ⇒ MC**:
*MarketConnector received FIX message on its order, here Simulator sending a fully FILL execution* 85 | - **EXE ⇒ CL**:
*MarketConnector publish processed and persisted Execution onto messaging bus, here our Client will listen to* 86 | 87 | Included: 88 | - from order to FIX to a fully fill execution message to execution protobuf published back 89 | - serialsing/deserialsing mock order into protobuf messages 90 | - Request/Publish and Response/Subscribe via NATS.io message bus 91 | - Time spent in the Linux TCP/IP stack 92 | - Decode FIX messages and reply by a simulated broker 93 | 94 | Excluded: 95 | - Database transaction time (hard-wired to an inline mock DB driver) 96 | 97 | Result: 98 | - **`0.176ms per op, 5670 order+fill pairs per sec`** 99 |

100 | 101 |

102 | 103 | 104 | ## Limitations 105 | 106 | 107 | ## Contributing 108 | 109 | **GoTrade** © 2016+, Chao Yan. Released under the [GNU] General Public License.
110 | Authored and maintained by Chao Yan with help from contributors ([list][contributors]).
111 | Contributions are welcome. 112 | 113 | > [cyan.ly](http://cyan.ly)  ·  114 | > GitHub [@cyanly](https://github.com/cyanly)  ·  115 | 116 | [GNU]: http://www.gnu.org/licenses/gpl-3.0.en.html 117 | [contributors]: http://github.com/cyanly/gotrade/contributors 118 | -------------------------------------------------------------------------------- /cmd/MCSellSideSim/main.go: -------------------------------------------------------------------------------- 1 | // Command line executable entry package for SellSideSimulator 2 | package main 3 | 4 | import ( 5 | "github.com/cyanly/gotrade/core/service" 6 | "github.com/cyanly/gotrade/services/marketconnectors/sellsidesim" 7 | ) 8 | 9 | func main() { 10 | // Load configurations 11 | 12 | // Initialise Service Infrastructure 13 | sc := service.NewConfig() 14 | sc.ServiceName = "OrderRouter" 15 | svc := service.NewService(sc) 16 | 17 | // Initialise Component 18 | orsvc := sellsidesim.NewSellSideSimulator("console") 19 | orsvc.Start() 20 | defer orsvc.Close() 21 | 22 | // Go 23 | <-svc.Start() 24 | } 25 | -------------------------------------------------------------------------------- /cmd/MCSimulator/main.go: -------------------------------------------------------------------------------- 1 | // Command line executable entry package for FIX buy side simulator 2 | package main 3 | 4 | import ( 5 | "github.com/cyanly/gotrade/core/service" 6 | _ "github.com/cyanly/gotrade/database/memstore" 7 | "github.com/cyanly/gotrade/services/marketconnectors/common" 8 | "github.com/cyanly/gotrade/services/marketconnectors/simulator" 9 | ) 10 | 11 | func main() { 12 | 13 | // Load configurations 14 | 15 | // Initialise Service Infrastructure 16 | sc := service.NewConfig() 17 | sc.ServiceName = "Simulator" 18 | svc := service.NewService(sc) 19 | 20 | // Initialise Component 21 | orc := common.NewConfig() 22 | orc.MessageBusURL = sc.MessageBusURL 23 | 24 | // Initialise Database Connection 25 | orc.DatabaseDriver = "memstore" 26 | 27 | orsvc := simulator.NewMarketConnector(orc) 28 | orsvc.Start() 29 | defer orsvc.Close() 30 | 31 | // Go 32 | <-svc.Start() 33 | } 34 | -------------------------------------------------------------------------------- /cmd/OrderRouter/main.go: -------------------------------------------------------------------------------- 1 | // Command line executable entry package for Order Router 2 | package main 3 | 4 | import ( 5 | "github.com/cyanly/gotrade/core/service" 6 | "github.com/cyanly/gotrade/services/orderrouter" 7 | _ "github.com/cyanly/gotrade/database/memstore" 8 | ) 9 | 10 | func main() { 11 | 12 | // Load configurations 13 | 14 | // Initialise Service Infrastructure 15 | sc := service.NewConfig() 16 | sc.ServiceName = "OrderRouter" 17 | svc := service.NewService(sc) 18 | 19 | // Initialise OrderRouter 20 | orc := orderrouter.NewConfig() 21 | orc.ServiceMessageBusURL = sc.MessageBusURL 22 | 23 | // Initialise Database Connection 24 | orc.DatabaseDriver = "memstore" 25 | 26 | orsvc := orderrouter.NewOrderRouter(orc) 27 | orsvc.Start() 28 | defer orsvc.Close() 29 | 30 | // Go 31 | <-svc.Start() 32 | } 33 | -------------------------------------------------------------------------------- /core/account/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/core/account/todo -------------------------------------------------------------------------------- /core/algo/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/core/algo/todo -------------------------------------------------------------------------------- /core/compliance/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/core/compliance/todo -------------------------------------------------------------------------------- /core/logger/handlers/handleES.go: -------------------------------------------------------------------------------- 1 | // Package es implements an Elasticsearch batch handler. Currently this implementation 2 | // assumes the index format of "logs-YY-MM-DD". 3 | package es 4 | 5 | import ( 6 | "io" 7 | stdlog "log" 8 | "sync" 9 | "time" 10 | 11 | "github.com/tj/go-elastic/batch" 12 | 13 | "github.com/apex/log" 14 | "os" 15 | ) 16 | 17 | var ( 18 | hostname string 19 | pid int 20 | ) 21 | 22 | // TODO(tj): allow index configuration 23 | // TODO(tj): allow dumping logs to stderr on timeout 24 | // TODO(tj): allow custom format that does not include .fields etc 25 | // TODO(tj): allow interval flushes 26 | // TODO(tj): allow explicit Flush() (for Lambda where you have to flush at the end of function) 27 | 28 | // index for the current time. 29 | func index() string { 30 | return time.Now().Format("logs-06-01-02") 31 | } 32 | 33 | // Elasticsearch interface. 34 | type Elasticsearch interface { 35 | Bulk(io.Reader) error 36 | } 37 | 38 | // Config for handler. 39 | type Config struct { 40 | BufferSize int // BufferSize is the number of logs to buffer before flush (default: 100) 41 | Client Elasticsearch // Client for ES 42 | } 43 | 44 | // defaults applies defaults to the config. 45 | func (c *Config) defaults() { 46 | if c.BufferSize == 0 { 47 | c.BufferSize = 100 48 | } 49 | } 50 | 51 | // Handler implementation. 52 | type Handler struct { 53 | *Config 54 | 55 | mu sync.Mutex 56 | batch *batch.Batch 57 | } 58 | 59 | // New handler with BufferSize 60 | func New(config *Config) *Handler { 61 | config.defaults() 62 | 63 | // system info 64 | hn, _ := os.Hostname() 65 | hostname = hn 66 | pid = os.Getpid() 67 | 68 | return &Handler{ 69 | Config: config, 70 | } 71 | } 72 | 73 | // HandleLog implements log.Handler. 74 | func (h *Handler) HandleLog(e *log.Entry) error { 75 | h.mu.Lock() 76 | defer h.mu.Unlock() 77 | 78 | if h.batch == nil { 79 | h.batch = &batch.Batch{ 80 | Elastic: h.Client, 81 | Index: index(), 82 | Type: "log", 83 | } 84 | } 85 | 86 | // append system info 87 | e.WithFields(log.Fields{ 88 | "host": hostname, 89 | "pid": pid, 90 | }) 91 | 92 | h.batch.Add(e) 93 | 94 | if h.batch.Size() >= h.BufferSize { 95 | go h.flush(h.batch) 96 | h.batch = nil 97 | } 98 | 99 | return nil 100 | } 101 | 102 | // flush the given `batch` asynchronously. 103 | func (h *Handler) flush(batch *batch.Batch) { 104 | size := batch.Size() 105 | start := time.Now() 106 | stdlog.Printf("log/elastic: flushing %d logs", size) 107 | 108 | if err := batch.Flush(); err != nil { 109 | stdlog.Printf("log/elastic: failed to flush %d logs: %s", size, err) 110 | } 111 | 112 | stdlog.Printf("log/elastic: flushed %d logs in %s", size, time.Since(start)) 113 | } 114 | -------------------------------------------------------------------------------- /core/logger/init.go: -------------------------------------------------------------------------------- 1 | package logger 2 | 3 | import ( 4 | "net/http" 5 | "os" 6 | "time" 7 | 8 | apexlog "github.com/apex/log" 9 | "github.com/apex/log/handlers/cli" 10 | "github.com/apex/log/handlers/multi" 11 | "github.com/apex/log/handlers/text" 12 | "github.com/tj/go-elastic" 13 | 14 | es "github.com/cyanly/gotrade/core/logger/handlers" 15 | ) 16 | 17 | func init() { 18 | //Default to console logging 19 | apexlog.SetHandler(cli.Default) 20 | 21 | //Upgrade to ElasticSearch if defined in ENV 22 | if os.Getenv("ES_ADDR") != "" { 23 | esClient := elastic.New(os.Getenv("ES_ADDR")) //"http://192.168.99.101:9200" 24 | esClient.HTTPClient = &http.Client{ 25 | Timeout: 5 * time.Second, 26 | } 27 | 28 | e := es.New(&es.Config{ 29 | Client: esClient, 30 | BufferSize: 100, 31 | }) 32 | 33 | t := text.New(os.Stderr) 34 | 35 | apexlog.SetHandler(multi.New(e, t)) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /core/logger/logger.go: -------------------------------------------------------------------------------- 1 | package logger 2 | 3 | import ( 4 | apexlog "github.com/apex/log" 5 | "github.com/apex/log/handlers/discard" 6 | ) 7 | 8 | // Fields represents a map of entry level data used for structured logging. 9 | type Fields map[string]interface{} 10 | 11 | // WithField returns a new entry with the `key` and `value` set. 12 | func WithField(key string, value interface{}) *apexlog.Entry { 13 | return apexlog.WithField(key, value) 14 | } 15 | 16 | // WithError returns a new entry with the "error" set to `err`. 17 | func WithError(err error) *apexlog.Entry { 18 | return apexlog.WithError(err) 19 | } 20 | 21 | // Debug level message. 22 | func Debug(msg string) { 23 | apexlog.Debug(msg) 24 | } 25 | 26 | // Info level message. 27 | func Info(msg string) { 28 | apexlog.Info(msg) 29 | } 30 | 31 | // Warn level message. 32 | func Warn(msg string) { 33 | apexlog.Warn(msg) 34 | } 35 | 36 | // Error level message. 37 | func Error(msg string) { 38 | apexlog.Error(msg) 39 | } 40 | 41 | // Fatal level message, followed by an exit. 42 | func Fatal(msg string) { 43 | apexlog.Fatal(msg) 44 | } 45 | 46 | // Debugf level formatted message. 47 | func Debugf(msg string, v ...interface{}) { 48 | apexlog.Debugf(msg, v...) 49 | } 50 | 51 | // Infof level formatted message. 52 | func Infof(msg string, v ...interface{}) { 53 | apexlog.Infof(msg, v...) 54 | } 55 | 56 | // Warnf level formatted message. 57 | func Warnf(msg string, v ...interface{}) { 58 | apexlog.Warnf(msg, v...) 59 | } 60 | 61 | // Errorf level formatted message. 62 | func Errorf(msg string, v ...interface{}) { 63 | apexlog.Errorf(msg, v...) 64 | } 65 | 66 | // Fatalf level formatted message, followed by an exit. 67 | func Fatalf(msg string, v ...interface{}) { 68 | apexlog.Fatalf(msg, v...) 69 | } 70 | 71 | // Trace returns a new entry with a Stop method to fire off 72 | // a corresponding completion log, useful with defer. 73 | func Trace(msg string) *apexlog.Entry { 74 | return apexlog.Trace(msg) 75 | } 76 | 77 | // Discard all log entries, for benchmarking purposes 78 | func Discard() { 79 | apexlog.SetHandler(discard.Default) 80 | } 81 | -------------------------------------------------------------------------------- /core/logger/logger_test.go: -------------------------------------------------------------------------------- 1 | package logger_test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | l "github.com/cyanly/gotrade/core/logger" 8 | ) 9 | 10 | func TestLogger_printf(t *testing.T) { 11 | l.Infof("logged in %s", "Chao") 12 | } 13 | 14 | func TestLogger_levels(t *testing.T) { 15 | l.Debug("uploading") 16 | l.Info("upload complete") 17 | } 18 | 19 | func TestLogger_WithFields(t *testing.T) { 20 | ctx := l.WithField("file", "1.png") 21 | ctx.Debug("uploading") 22 | ctx.Info("upload complete") 23 | 24 | ctx = l.WithField("payload", l.Fields{ 25 | "file": "medium.png", 26 | "type": "image/png", 27 | "size": 1 << 20, 28 | }) 29 | ctx.Debug("uploading") 30 | ctx.Info("upload complete") 31 | } 32 | 33 | func TestLogger_WithField(t *testing.T) { 34 | ctx := l.WithField("file", "3.png").WithField("user", "Chao") 35 | ctx.Debug("uploading") 36 | ctx.Info("upload complete") 37 | } 38 | 39 | func TestLogger_Trace_info(t *testing.T) { 40 | func() (err error) { 41 | defer l.WithField("file", "info.png").Trace("upload").Stop(&err) 42 | return nil 43 | }() 44 | } 45 | 46 | func TestLogger_Trace_error(t *testing.T) { 47 | func() (err error) { 48 | defer l.WithField("file", "error.png").Trace("upload").Stop(&err) 49 | return fmt.Errorf("boom") 50 | }() 51 | 52 | } 53 | 54 | func BenchmarkLogger_small(b *testing.B) { 55 | for i := 0; i < b.N; i++ { 56 | l.Info("login") 57 | } 58 | } 59 | 60 | func BenchmarkLogger_medium(b *testing.B) { 61 | for i := 0; i < b.N; i++ { 62 | l.WithField("payload", l.Fields{ 63 | "file": "medium.png", 64 | "type": "image/png", 65 | "size": 1 << 20, 66 | }).Info("upload") 67 | } 68 | } 69 | 70 | //func BenchmarkLogger_large(b *testing.B) { 71 | // err := fmt.Errorf("boom") 72 | // 73 | // for i := 0; i < b.N; i++ { 74 | // l.WithFields(l.Fields{ 75 | // "file": "large.png", 76 | // "type": "image/png", 77 | // "size": 1 << 20, 78 | // }).WithFields(l.Fields{ 79 | // "some": "more", 80 | // "data": "here", 81 | // "whatever": "blah blah", 82 | // "more": "stuff", 83 | // "context": "such useful", 84 | // "much": "fun", 85 | // }).WithError(err).Error("upload failed") 86 | // } 87 | //} 88 | -------------------------------------------------------------------------------- /core/messagebus/config.go: -------------------------------------------------------------------------------- 1 | package messagebus 2 | 3 | import "github.com/nats-io/nats" 4 | 5 | // Config represents the configuration. 6 | type Config struct { 7 | Url string 8 | } 9 | 10 | const DefaultUrl = nats.DefaultURL 11 | 12 | // NewConfig returns an instance of Config with defaults. 13 | func NewConfig(url string) *Config { 14 | 15 | var config = &Config{ 16 | Url: url, 17 | } 18 | return config 19 | } 20 | -------------------------------------------------------------------------------- /core/messagebus/messagebus.go: -------------------------------------------------------------------------------- 1 | package messagebus 2 | 3 | import ( 4 | "github.com/nats-io/nats" 5 | ) 6 | 7 | type Msg nats.Msg 8 | 9 | type MessageBus struct { 10 | Config *Config 11 | Connection *nats.Conn 12 | } 13 | 14 | // Create new message bus wrapper, for this repo NATS is hardcoded. 15 | // This module should serve as message routing across multiple protocols (RMQ, 0MQ, Tibrv etc) 16 | func NewMessageBus(config *Config) (*MessageBus, error) { 17 | if conn, err := nats.Connect(config.Url); err != nil { 18 | return nil, err 19 | } else { 20 | m := &MessageBus{ 21 | Config: config, 22 | Connection: conn, 23 | } 24 | return m, nil 25 | } 26 | } 27 | 28 | // Close underlying messaging bus driver 29 | func (m *MessageBus) Close() { 30 | if m.Connection != nil { 31 | m.Connection.Close() 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /core/messagebus/test/testBroker.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "github.com/nats-io/gnatsd/server" 5 | "github.com/nats-io/gnatsd/test" 6 | ) 7 | 8 | // Create a local messaging broker, usually for testing purpose 9 | func RunDefaultServer() *server.Server { 10 | test.DefaultTestOptions.Port = 22222 11 | return test.RunDefaultServer() 12 | } 13 | -------------------------------------------------------------------------------- /core/order/order.go: -------------------------------------------------------------------------------- 1 | // Core order APIs 2 | package order 3 | 4 | import ( 5 | 6 | "errors" 7 | "fmt" 8 | 9 | messagebus "github.com/nats-io/nats" 10 | proto "github.com/cyanly/gotrade/proto/order" 11 | ) 12 | 13 | var ( 14 | MessageBus *messagebus.Conn 15 | ) 16 | 17 | // Order struct extends protobuf Order /proto/order/order.proto 18 | type Order struct { 19 | *proto.Order 20 | } 21 | 22 | func (m *Order) IsCompleted() bool { 23 | switch m.OrderStatus { 24 | case proto.OrderStatus_CANCELLED, 25 | proto.OrderStatus_REJECTED, 26 | proto.OrderStatus_FILLED, 27 | proto.OrderStatus_DONE_FOR_DAY, 28 | proto.OrderStatus_EXPIRED: 29 | return true 30 | 31 | default: 32 | return false 33 | } 34 | } 35 | 36 | // Status that Order can be cancelled 37 | func (m *Order) CanCancel() bool { 38 | switch m.OrderStatus { 39 | case proto.OrderStatus_NEW, 40 | proto.OrderStatus_PARTIALLY_FILLED, 41 | proto.OrderStatus_REPLACED: 42 | return true 43 | case proto.OrderStatus_REJECTED: 44 | switch m.MessageType { 45 | case proto.Order_NEW: 46 | return false 47 | default: 48 | return true 49 | } 50 | 51 | default: 52 | return false 53 | } 54 | } 55 | 56 | // Status that Order can be replaced 57 | func (m *Order) CanReplace() bool { 58 | return m.CanCancel() 59 | } 60 | 61 | // Basic validation of Order, usually unmarshaled off message bus 62 | func (m *Order) Validate() error { 63 | if m.OrderId <= 0 { 64 | return errors.New("Invalid OrderId") 65 | } 66 | 67 | return nil 68 | } 69 | 70 | // Pretty print order, usually for logging 71 | func (m *Order) String() string { 72 | return fmt.Sprintf("(%s) %s %v %s -> %s", m.Trader, m.Side, m.Quantity, m.Symbol, m.MarketConnector) 73 | } 74 | -------------------------------------------------------------------------------- /core/order/order_test.go: -------------------------------------------------------------------------------- 1 | package order 2 | 3 | import ( 4 | "encoding/json" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func TestOrderJson_Parse(t *testing.T) { 10 | // Parse JSON 11 | var o Order 12 | if err := json.Unmarshal([]byte(`{ 13 | "order_id": 123, 14 | "order_key": 0, 15 | "version": 0, 16 | "client_guid": "1234-5678-abcd-3210", 17 | "instruction": "NEW", 18 | "side": "BUY", 19 | "quantity": 100, 20 | "symbol": "AAPL", 21 | "order_type": "MARKET", 22 | "timeinforce": "DAY", 23 | "handle_inst": "AUTOMATED_EXECUTION_ORDER_PRIVATE", 24 | "exchange": "CME", 25 | "description": "Apple Inc.", 26 | "account_id": 1, 27 | "market_connector": "Simulator", 28 | "source": "mock test", 29 | "trader": "trader1", 30 | "trader_id": 1, 31 | "machine": "CYAN", 32 | "memo": "Super Secret", 33 | "create_datetime": "`+time.Now().UTC().Format(time.RFC3339Nano)+`", 34 | "-": "###" 35 | }`), &o); err != nil { 36 | t.Fatal(err) 37 | } 38 | 39 | // Validate configuration. 40 | if o.OrderId != 123 { 41 | t.Fatalf("unexpected order_id: %v, expecting 123", o.OrderId) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /core/pricing/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/core/pricing/todo -------------------------------------------------------------------------------- /core/security/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/core/security/todo -------------------------------------------------------------------------------- /core/service/config.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "github.com/cyanly/gotrade/core/messagebus" 5 | ) 6 | 7 | const ( 8 | // DefaultMessageBusURL is the default MessageBus URL for service communications. 9 | DefaultMessageBusURL = messagebus.DefaultUrl 10 | // DefaultHeartbeatFreq is the period between service heartbeats in seconds. 11 | DefaultHeartbeatFreq = int(3) 12 | ) 13 | 14 | // Config represents the configuration for base service. 15 | type Config struct { 16 | MessageBusURL string `json:"service_message_bus"` 17 | ServiceName string `json:"service_name"` 18 | HeartbeatFreq int `json:"service_heartbeat_freq"` 19 | } 20 | 21 | // NewConfig returns an instance of Config with defaults. 22 | func NewConfig() Config { 23 | return Config{ 24 | ServiceName: "Unamed Service", 25 | MessageBusURL: DefaultMessageBusURL, 26 | HeartbeatFreq: DefaultHeartbeatFreq, 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /core/service/config_test.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "encoding/json" 5 | "testing" 6 | ) 7 | 8 | func TestConfig_Parse(t *testing.T) { 9 | // Parse configuration. 10 | var c Config 11 | if err := json.Unmarshal([]byte(`{ 12 | "service_name": "test_service name", 13 | "service_heartbeat_freq": 3, 14 | "service_message_bus": "some_url", 15 | "entry_not_in_use": "#####" 16 | }`), &c); err != nil { 17 | t.Fatal(err) 18 | } 19 | 20 | // Validate configuration. 21 | if c.ServiceName != "test_service name" { 22 | t.Fatalf("unexpected service_name: %v", c.ServiceName) 23 | } else if c.HeartbeatFreq != 3 { 24 | t.Fatalf("unexpected service_heartbeat_freq: %v", c.HeartbeatFreq) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /core/service/init.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import "os" 4 | 5 | var ( 6 | uuid string 7 | hostname string 8 | pid int 9 | ) 10 | 11 | func init() { 12 | hn, _ := os.Hostname() 13 | hostname = hn 14 | 15 | pid = os.Getpid() 16 | } 17 | -------------------------------------------------------------------------------- /core/service/service.go: -------------------------------------------------------------------------------- 1 | // Core service infrastructure for servicing starting/stopping/SIGTERM, and heartbeating etc 2 | package service 3 | 4 | import ( 5 | "fmt" 6 | "os" 7 | "os/signal" 8 | "syscall" 9 | "time" 10 | 11 | log "github.com/cyanly/gotrade/core/logger" 12 | proto "github.com/cyanly/gotrade/proto/service" 13 | messagebus "github.com/nats-io/nats" 14 | ) 15 | 16 | type Service struct { 17 | Config Config 18 | Status proto.Heartbeat_Status 19 | 20 | shutdownChannel chan bool 21 | messageBus *messagebus.Conn 22 | lastHBMsg *proto.Heartbeat 23 | publishAddress string 24 | } 25 | 26 | func NewService(c Config) *Service { 27 | 28 | // Hardware Info 29 | uuid = fmt.Sprint(hostname, ":", pid) 30 | log.Infof("Service [%v] starting @ %v", c.ServiceName, uuid) 31 | 32 | // Service handle 33 | svc := &Service{ 34 | Config: c, 35 | Status: proto.STARTING, 36 | shutdownChannel: make(chan bool), 37 | } 38 | 39 | // Messaging bus 40 | messageBus, err := messagebus.Connect(svc.Config.MessageBusURL) 41 | svc.messageBus = messageBus 42 | if err != nil { 43 | log.WithField("MessageBusURL", svc.Config.MessageBusURL).Fatal("error: Cannot connect to message bus") 44 | } 45 | 46 | //Heartbeating 47 | currDateTime := time.Now().UTC().Format(time.RFC3339) 48 | hbMsg := &proto.Heartbeat{ 49 | Name: svc.Config.ServiceName, 50 | Id: uuid, 51 | Status: proto.STARTING, 52 | Machine: hostname, 53 | CreationDatetime: currDateTime, 54 | CurrentDatetime: currDateTime, 55 | } 56 | svc.lastHBMsg = hbMsg 57 | hbTicker := time.NewTicker(time.Second * time.Duration(svc.Config.HeartbeatFreq)) 58 | go func(shutdownChannel chan bool) { 59 | publish_address := "service.Heartbeat." + svc.Config.ServiceName 60 | 61 | for range hbTicker.C { 62 | hbMsg.CurrentDatetime = time.Now().UTC().Format(time.RFC3339) 63 | hbMsg.Status = svc.Status 64 | 65 | if data, _ := hbMsg.Marshal(); data != nil { 66 | messageBus.Publish(publish_address, data) 67 | } 68 | 69 | select { 70 | case <-shutdownChannel: 71 | hbTicker.Stop() 72 | 73 | //Publish Stop heartbeat 74 | if svc.Status != proto.ERROR { 75 | svc.Status = proto.STOPPED 76 | } 77 | hbMsg.CurrentDatetime = time.Now().UTC().Format(time.RFC3339) 78 | hbMsg.Status = svc.Status 79 | if data, _ := hbMsg.Marshal(); data != nil { 80 | messageBus.Publish(publish_address, data) 81 | } 82 | 83 | messageBus.Close() 84 | 85 | log.Info("Server Terminated") 86 | return 87 | } 88 | } 89 | }(svc.shutdownChannel) 90 | 91 | return svc 92 | } 93 | 94 | func (self *Service) Start() chan bool { 95 | //SIGINT or SIGTERM is caught 96 | quitChannel := make(chan os.Signal) 97 | signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM) 98 | shutdownCallerChannel := make(chan bool) 99 | go func() { 100 | <-quitChannel 101 | self.shutdownChannel <- true 102 | shutdownCallerChannel <- true 103 | }() 104 | 105 | self.Status = proto.RUNNING 106 | 107 | // Immediately publish heartbeat 108 | self.lastHBMsg.CurrentDatetime = time.Now().UTC().Format(time.RFC3339) 109 | self.lastHBMsg.Status = self.Status 110 | if data, _ := self.lastHBMsg.Marshal(); data != nil { 111 | self.messageBus.Publish("service.Heartbeat."+self.Config.ServiceName, data) 112 | } 113 | 114 | log.Infof("Service [%v] Started", self.Config.ServiceName) 115 | return shutdownCallerChannel 116 | } 117 | 118 | func (self *Service) Stop() { 119 | self.shutdownChannel <- true 120 | } -------------------------------------------------------------------------------- /core/service/service_test.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | 7 | "github.com/cyanly/gotrade/core/messagebus/test" 8 | proto "github.com/cyanly/gotrade/proto/service" 9 | ) 10 | 11 | func TestServiceStartAndStop(t *testing.T) { 12 | ts := test.RunDefaultServer() 13 | defer ts.Shutdown() 14 | 15 | sc := NewConfig() 16 | sc.ServiceName = "Test Service" 17 | sc.MessageBusURL = "nats://localhost:22222" 18 | 19 | svc := NewService(sc) 20 | if svc.Status != proto.STARTING { 21 | t.Fatalf("unexpected service status: %s , expecting STARTING", svc.Status) 22 | } 23 | 24 | svc.Start() 25 | if svc.Status != proto.RUNNING { 26 | t.Fatalf("unexpected service status: %s, expecting RUNNING", svc.Status) 27 | } 28 | 29 | time.Sleep(500 * time.Millisecond) 30 | svc.shutdownChannel <- true 31 | time.Sleep(100 * time.Millisecond) 32 | 33 | if svc.Status != proto.STOPPED { 34 | t.Fatalf("unexpected service status: %s, expecting STOPPED", svc.Status) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /core/static/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/core/static/todo -------------------------------------------------------------------------------- /database/memstore/copy.go: -------------------------------------------------------------------------------- 1 | package memstore 2 | 3 | import "reflect" 4 | 5 | func Copy(toValue interface{}, fromValue interface{}) (err error) { 6 | var ( 7 | isSlice bool 8 | fromType reflect.Type 9 | isFromPtr bool 10 | toType reflect.Type 11 | isToPtr bool 12 | amount int 13 | ) 14 | 15 | from := reflect.Indirect(reflect.ValueOf(fromValue)) 16 | to := reflect.Indirect(reflect.ValueOf(toValue)) 17 | 18 | if to.Kind() == reflect.Slice { 19 | isSlice = true 20 | if from.Kind() == reflect.Slice { 21 | fromType = from.Type().Elem() 22 | if fromType.Kind() == reflect.Ptr { 23 | fromType = fromType.Elem() 24 | isFromPtr = true 25 | } 26 | amount = from.Len() 27 | } else { 28 | fromType = from.Type() 29 | amount = 1 30 | } 31 | 32 | toType = to.Type().Elem() 33 | if toType.Kind() == reflect.Ptr { 34 | toType = toType.Elem() 35 | isToPtr = true 36 | } 37 | } else { 38 | fromType = from.Type() 39 | toType = to.Type() 40 | amount = 1 41 | } 42 | 43 | for e := 0; e < amount; e++ { 44 | var dest, source reflect.Value 45 | if isSlice { 46 | if from.Kind() == reflect.Slice { 47 | source = from.Index(e) 48 | if isFromPtr { 49 | source = source.Elem() 50 | } 51 | } else { 52 | source = from 53 | } 54 | } else { 55 | source = from 56 | } 57 | 58 | if isSlice { 59 | dest = reflect.New(toType).Elem() 60 | } else { 61 | dest = to 62 | } 63 | 64 | for _, field := range deepFields(fromType) { 65 | if !field.Anonymous { 66 | name := field.Name 67 | fromField := source.FieldByName(name) 68 | toField := dest.FieldByName(name) 69 | toMethod := dest.Addr().MethodByName(name) 70 | if fromField.IsValid() && toField.IsValid() && toField.CanSet() { 71 | toField.Set(fromField) 72 | } 73 | 74 | if fromField.IsValid() && toMethod.IsValid() { 75 | toMethod.Call([]reflect.Value{fromField}) 76 | } 77 | } 78 | } 79 | 80 | for i := 0; i < toType.NumField(); i++ { 81 | field := toType.Field(i) 82 | if !field.Anonymous { 83 | name := field.Name 84 | fromMethod := source.Addr().MethodByName(name) 85 | toField := dest.FieldByName(name) 86 | 87 | if fromMethod.IsValid() && toField.IsValid() && toField.CanSet() { 88 | values := fromMethod.Call([]reflect.Value{}) 89 | if len(values) >= 1 { 90 | toField.Set(values[0]) 91 | } 92 | } 93 | } 94 | } 95 | 96 | if isSlice { 97 | if isToPtr { 98 | to.Set(reflect.Append(to, dest.Addr())) 99 | } else { 100 | to.Set(reflect.Append(to, dest)) 101 | } 102 | } 103 | } 104 | return 105 | } 106 | 107 | func deepFields(ifaceType reflect.Type) []reflect.StructField { 108 | var fields []reflect.StructField 109 | 110 | for i := 0; i < ifaceType.NumField(); i++ { 111 | v := ifaceType.Field(i) 112 | if v.Anonymous && v.Type.Kind() == reflect.Struct { 113 | fields = append(fields, deepFields(v.Type)...) 114 | } else { 115 | fields = append(fields, v) 116 | } 117 | } 118 | 119 | return fields 120 | } 121 | -------------------------------------------------------------------------------- /database/memstore/orderstore.go: -------------------------------------------------------------------------------- 1 | package memstore 2 | 3 | import ( 4 | "errors" 5 | 6 | "fmt" 7 | "github.com/cyanly/gotrade/database" 8 | proto "github.com/cyanly/gotrade/proto/order" 9 | ) 10 | 11 | const StoreType = "memstore" 12 | 13 | func init() { 14 | database.RegisterOrderStore(StoreType, database.OrderStoreRegistration{ 15 | DialFunc: newOrderStore, 16 | }) 17 | } 18 | 19 | type OrderStore struct { 20 | nextOrderID int32 21 | nextOrderKey int32 22 | nextExecutionID int32 23 | orderMap map[int32]*proto.Order 24 | executionMap map[int32]*proto.Execution 25 | } 26 | 27 | func newOrderStore(url string, options database.Options) (database.OrderStore, error) { 28 | return &OrderStore{ 29 | orderMap: make(map[int32]*proto.Order), 30 | executionMap: make(map[int32]*proto.Execution), 31 | nextOrderID: 1, 32 | nextOrderKey: 1, 33 | nextExecutionID: 1, 34 | }, nil 35 | } 36 | 37 | // Interface Implementations ======================================================== 38 | 39 | // Returns an order for OrderId 40 | func (store *OrderStore) OrderGet(orderId int32) (*proto.Order, error) { 41 | if order, ok := store.orderMap[orderId]; ok { 42 | orderClone := &proto.Order{} 43 | Copy(orderClone, order) 44 | return orderClone, nil 45 | } else { 46 | return nil, errors.New(fmt.Sprintf("OrderStore: OrderID %v not found", orderId)) 47 | } 48 | } 49 | 50 | // Returns an order for OrderId 51 | func (store *OrderStore) OrderGetByOrderKey(orderKey int32) (*proto.Order, error) { 52 | for _, o := range store.orderMap { 53 | if o.OrderKey == orderKey { 54 | orderClone := &proto.Order{} 55 | Copy(orderClone, o) 56 | return orderClone, nil 57 | } 58 | } 59 | 60 | return nil, errors.New(fmt.Sprintf("OrderStore: OrderKey %v not found", orderKey)) 61 | } 62 | 63 | // Save order as new entry 64 | func (store *OrderStore) OrderCreate(order *proto.Order) error { 65 | clone := order 66 | //&proto.Order{} Copy(clone, order) 67 | clone.OrderId = store.nextOrderID 68 | store.nextOrderID++ 69 | if clone.OrderKey == 0 { 70 | clone.OrderKey = store.nextOrderKey 71 | store.nextOrderKey++ 72 | } else { 73 | clone.Version++ 74 | } 75 | store.orderMap[clone.OrderId] = clone 76 | 77 | return nil 78 | } 79 | 80 | // Create an Execution from entity 81 | func (store *OrderStore) ExecutionCreate(er *proto.Execution) error { 82 | if o, ok := store.orderMap[er.OrderId]; ok == false { 83 | return errors.New(fmt.Sprintf("OrderStore: OrderID %v does not exists", er.OrderId)) 84 | } else { 85 | clone := er 86 | //&proto.Execution{} Copy(clone, er) 87 | clone.ExecutionId = store.nextExecutionID 88 | store.nextExecutionID++ 89 | store.executionMap[clone.ExecutionId] = clone 90 | 91 | // Update Order status, this should be in single transaction 92 | o.FilledQuantity = er.CalcCumQuantity 93 | o.FilledAvgPrice = er.CalcAvgPrice 94 | o.OrderStatus = er.OrderStatus 95 | switch o.OrderStatus { 96 | case proto.OrderStatus_CANCELLED, 97 | proto.OrderStatus_REJECTED, 98 | proto.OrderStatus_FILLED, 99 | proto.OrderStatus_DONE_FOR_DAY, 100 | proto.OrderStatus_EXPIRED: 101 | o.IsComplete = true 102 | 103 | default: 104 | o.IsComplete = false 105 | } 106 | 107 | } 108 | 109 | return nil 110 | } 111 | 112 | func (store *OrderStore) Close() {} 113 | -------------------------------------------------------------------------------- /database/orderstore.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "errors" 5 | 6 | proto "github.com/cyanly/gotrade/proto/order" 7 | ) 8 | 9 | // FOR CLIENT ================================================================= 10 | 11 | // Defines the OrderStore interface. 12 | // Every backing store must implement at least this interface. 13 | type OrderStore interface { 14 | // Returns an order row for OrderId, excludes Execution/Allocations etc 15 | OrderGet(int32) (*proto.Order, error) 16 | // Returns an order row for OrderKey, excludes Execution/Allocations etc 17 | OrderGetByOrderKey(int32) (*proto.Order, error) 18 | // Save order as new entry 19 | OrderCreate(*proto.Order) error 20 | 21 | // Create an Execution from entity 22 | ExecutionCreate(*proto.Execution) error 23 | 24 | // Close the store and clean up. (Flush to disk, cleanly sever connections, etc) 25 | Close() 26 | } 27 | 28 | // Datastore options to be passed by client 29 | type Options map[string]interface{} 30 | 31 | func NewOrderStore(name, dbpath string, opts Options) (OrderStore, error) { 32 | r, registered := storeRegistry[name] 33 | if !registered { 34 | return nil, errors.New("OrderStore: name '" + name + "' is not registered") 35 | } 36 | return r.DialFunc(dbpath, opts) 37 | } 38 | 39 | // FOR Storage Implementations ===================================================== 40 | 41 | type OrderStoreRegistration struct { 42 | DialFunc func(string, Options) (OrderStore, error) 43 | } 44 | 45 | var storeRegistry = make(map[string]OrderStoreRegistration) 46 | 47 | func RegisterOrderStore(name string, register OrderStoreRegistration) { 48 | if _, found := storeRegistry[name]; found { 49 | panic("already registered OrderStore " + name) 50 | } 51 | storeRegistry[name] = register 52 | } 53 | -------------------------------------------------------------------------------- /database/pgsql/procs/execution.sql: -------------------------------------------------------------------------------- 1 | -- !InsertOne InsertExecution 2 | -- $1: exe *Execution 3 | -- $ret: $1.ExecutionId 4 | INSERT INTO execution ( 5 | order_id, 6 | order_status_id, 7 | broker_execution_time, 8 | qty, 9 | cum_qty, 10 | price, 11 | avg_price, 12 | broker_order_id, 13 | broker_exec_id, 14 | previous_broker_exec_id, 15 | calc_cum_qty, 16 | calc_avg_price, 17 | exec_type_id, 18 | last_mkt, 19 | last_liquidity_ind_id, 20 | text, 21 | exec_broker, 22 | cancel_replace_by_exececution_id 23 | ) 24 | VALUES ( 25 | $1.OrderId, 26 | $1.OrderStatus, 27 | TIMESTAMP WITH TIME ZONE '$1.BrokerExecDatetime', 28 | $1.Quantity, 29 | $1.CumQuantity, 30 | $1.Price, 31 | $1.AvgPrice, 32 | $1.BrokerOrderId, 33 | $1.BrokerExecId, 34 | $1.PrevBrokerExecId, 35 | $1.CalcCumQuantity, 36 | $1.CalcAvgPrice, 37 | $1.ExecType, 38 | $1.Lastmkt, 39 | $1.LastLiquidity, 40 | $1.Text, 41 | $1.ExecBroker, 42 | $1.CancelReplaceByExececutionId 43 | ) 44 | RETURNING execution_id; -------------------------------------------------------------------------------- /database/pgsql/procs/order.sql: -------------------------------------------------------------------------------- 1 | -- !Select GetOrderByOrderId 2 | -- $1: orderId int32 3 | -- $ret: *Order(OrderId,ClientGuid,OrderKey,Version,SubmitDatetime,Instruction,MarketConnector,OrderType,Timeinforce,HandleInst,Symbol,Exchange,Side,Quantity,LimitPrice,FilledQuantity,FilledAvgPrice,OrderStatus,IsComplete,IsExpired,TradeBookingId,TraderId,AccountId,BrokerUserid,BrokerAccount,Description,Source,OpenClose,Algo) 4 | SELECT 5 | order_id, 6 | client_guid, 7 | order_key, 8 | order_key_version, 9 | to_char(order_submitted_time at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'), 10 | instruction, 11 | market_connector, 12 | order_type, 13 | time_in_force, 14 | handl_inst, 15 | symbol, 16 | exchange, 17 | side, 18 | qty, 19 | limit_price, 20 | filled_qty, 21 | filled_avg_price, 22 | order_status_id, 23 | is_complete, 24 | is_booked, 25 | is_expired, 26 | trade_booking_id, 27 | trader_id, 28 | account, 29 | broker_user_id, 30 | broker_account, 31 | description, 32 | source, 33 | open_close, 34 | algo 35 | from orders WHERE order_id = $1; 36 | 37 | 38 | -- !Execute UpdateOrderStatus 39 | -- $1: order *Order 40 | UPDATE orders SET 41 | filled_qty = $1.FilledQuantity, 42 | filled_avg_price = $1.FilledAvgPrice, 43 | order_status_id = $1.OrderStatus, 44 | is_complete = $1.IsComplete, 45 | is_booked = COALESCE($1.IsBooked, 0), 46 | is_expired = COALESCE($1.IsExpired, 0), 47 | trade_booking_id = $1.TradeBookingId 48 | where order_id = $1.OrderId; 49 | 50 | -- !SelectOne GetNextOrderKey 51 | -- $ret: nextOrderKey int32 52 | SELECT nextval('orderkeysequence')::INT; 53 | 54 | -- !InsertOne InsertOrder 55 | -- $1: order *Order 56 | -- $ret: $1.OrderId 57 | INSERT INTO orders ( 58 | client_guid, 59 | order_key, 60 | order_key_version, 61 | order_submitted_time, 62 | instruction, 63 | market_connector, 64 | order_type, 65 | time_in_force, 66 | handl_inst, 67 | symbol, 68 | exchange, 69 | side, 70 | qty, 71 | limit_price, 72 | filled_qty, 73 | filled_avg_price, 74 | order_status_id, 75 | is_complete, 76 | is_booked, 77 | is_expired, 78 | trade_booking_id, 79 | trader_id, 80 | account, 81 | broker_user_id, 82 | broker_account, 83 | description, 84 | source, 85 | open_close, 86 | algo ) 87 | VALUES ( 88 | $1.ClientGuid, 89 | $1.OrderKey, 90 | $1.Version, 91 | TIMESTAMP WITH TIME ZONE '$1.SubmitDatetime', 92 | $1.Instruction, 93 | $1.MarketConnector, 94 | $1.OrderType, 95 | $1.Timeinforce, 96 | $1.HandleInst, 97 | $1.Symbol, 98 | $1.Exchange, 99 | $1.Side, 100 | $1.Quantity, 101 | $1.LimitPrice, 102 | $1.FilledQuantity, 103 | $1.FilledAvgPrice, 104 | $1.OrderStatus, 105 | $1.IsComplete, 106 | $1.IsBooked, 107 | $1.IsExpired, 108 | $1.TradeBookingId, 109 | $1.TraderId, 110 | $1.AccountId, 111 | $1.BrokerUserid, 112 | $1.BrokerAccount, 113 | $1.Description, 114 | $1.Source, 115 | $1.OpenClose, 116 | $1.Algo 117 | ) 118 | RETURNING order_id; 119 | 120 | -- !SelectOne GetOrderIdentsByClientOrdId 121 | -- $1: clOrdId string 122 | -- $ret: ordKey int32, ordId int32 123 | SELECT order_key, order_id FROM orders 124 | WHERE order_key = $1 AND order_key_version = $2; -------------------------------------------------------------------------------- /database/pgsql/tables/1.enums.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS order_status 2 | ( 3 | order_status_id SERIAL, 4 | description TEXT UNIQUE NOT NULL, 5 | name CHAR(3) UNIQUE NOT NULL, 6 | fix42value CHAR(1), 7 | fix44value CHAR(1), 8 | fix50value CHAR(1), 9 | 10 | PRIMARY KEY (order_status_id) 11 | ); 12 | 13 | --===================== 14 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (0, 'NEW', 'O ', '0', '0', '0'); 15 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (1, 'PARTIALLY_FILLED', 'PF ', '1', '1', '1'); 16 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (2, 'FILLED', 'F ', '2', '2', '2'); 17 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (3, 'DONE_FOR_DAY', 'DD ', '3', '3', '3'); 18 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (4, 'CANCELED', 'X ', '4', '4', '4'); 19 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (5, 'REPLACED', 'RP ', '5', null, '5'); 20 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (6, 'PENDING_CANCEL', 'PC ', '6', '6', '6'); 21 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (7, 'STOPPED', 'ST ', '7', '7', '7'); 22 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (8, 'REJECTED', 'R ', '8', '8', '8'); 23 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (9, 'SUSPENDED', 'SP ', '9', '9', '9'); 24 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (10, 'PENDING_NEW', 'PN ', 'A', 'A', 'A'); 25 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (11, 'CALCULATED', 'CD ', 'B', 'B', 'B'); 26 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (12, 'EXPIRED', 'EX ', 'C', 'C', null); 27 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (13, 'ACCEPTED_FOR_BIDDING', 'AB ', 'D', 'D', 'D'); 28 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (14, 'PENDING_REPLACE', 'PR ', 'E', 'E', 'E'); 29 | 30 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (100, 'ORDER_RECEIVED', 'OR ', null, null, null); 31 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (101, 'ORDER_SENT', 'OS ', null, null, null); 32 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (102, 'MC_ACK_ORDER', 'OA ', null, null, null); 33 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (103, 'MC_SENT_ORDER', 'SO ', null, null, null); 34 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (110, 'CANCEL_RECEIVED', 'CR ', null, null, null); 35 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (111, 'CANCEL_SENT', 'CS ', null, null, null); 36 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (112, 'MC_ACK_CANCEL', 'CA ', null, null, null); 37 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (113, 'MC_SENT_CANCEL', 'CB ', null, null, null); 38 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (120, 'REPLACE_RECEIVED', 'RR ', null, null, null); 39 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (121, 'REPLACE_SENT', 'RS ', null, null, null); 40 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (122, 'MC_ACK_REPLACE', 'RA ', null, null, null); 41 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (123, 'MC_SENT_REPLACE', 'RB ', null, null, null); 42 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (125, 'EXECUTION_REPLACED', 'ER ', null, null, null); 43 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (126, 'EXECUTION_CANCELLED', 'EC ', null, null, null); 44 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (130, 'BOOKABLE', 'BA ', null, null, null); 45 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (131, 'TRADE_PUMP_RECEIVED', 'TPR', null, null, null); 46 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (132, 'TRADE_PUMP_PROCESSED', 'TPP', null, null, null); 47 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (134, 'SENT_PMS', 'TS ', null, null, null); 48 | INSERT INTO order_status(order_status_id, description, name, fix42value, fix44value, fix50value) VALUES (135, 'BOOKED', 'BK ', null, null, null); 49 | --===================== 50 | 51 | 52 | CREATE TABLE IF NOT EXISTS exec_type 53 | ( 54 | exec_type_id SERIAL, 55 | name TEXT UNIQUE NOT NULL, 56 | fix42value CHAR(1), 57 | fix44value CHAR(1), 58 | fix50value CHAR(1), 59 | 60 | PRIMARY KEY (exec_type_id) 61 | ); 62 | 63 | 64 | --===================== 65 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (0, 'NEW', '0', '0', '0'); 66 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (1, 'PARTIAL_FILL', '1', null, null); 67 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (2, 'FILL', null, null, '2'); 68 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (3, 'DONE_FOR_DAY', '3', '3', '3'); 69 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (4, 'CANCELED', '4', '4', '4'); 70 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (5, 'REPLACE', '5', '5', '5'); 71 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (6, 'PENDING_CANCEL', '6', '6', '6'); 72 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (7, 'STOPPED', '7', '7', '7'); 73 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (8, 'REJECTED', '8', '8', '8'); 74 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (9, 'SUSPENDED', '9', '9', '9'); 75 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (10, 'PENDING_NEW', 'A', 'A', 'A'); 76 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (11, 'CALCULATED', 'B', 'B', 'B'); 77 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (12, 'EXPIRED', 'C', 'C', 'C'); 78 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (13, 'RESTATED', 'D', 'D', 'D'); 79 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (14, 'PENDING_REPLACE', 'E', 'E', 'E'); 80 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (15, 'TRADE', null, 'F', 'F'); 81 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (16, 'TRADE_CORRECT', null, 'G', 'G'); 82 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (17, 'TRADE_CANCEL', null, 'H', 'H'); 83 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (18, 'ORDER_STATUS', null, 'I', 'I'); 84 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (19, 'CLEARING_HOLD', null, null, 'J'); 85 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (20, 'RELEASED_TO_CLEARING', null, null, 'K'); 86 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (21, 'SYSTEM', null, null, 'L'); 87 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (22, 'LOCKED', null, null, 'M'); 88 | INSERT INTO exec_type(exec_type_id, name, fix42value, fix44value, fix50value) VALUES (23, 'RELEASED', null, null, 'N'); 89 | --===================== 90 | 91 | 92 | -------------------------------------------------------------------------------- /database/pgsql/tables/2.users.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS person 2 | ( 3 | person_id SERIAL, 4 | user_id TEXT NOT NULL, 5 | organisation_id INT, 6 | first_name TEXT NOT NULL, 7 | last_name TEXT NOT NULL, 8 | title TEXT NOT NULL, 9 | role_type TEXT NOT NULL, 10 | 11 | PRIMARY KEY (person_id) 12 | ); 13 | 14 | CREATE TABLE IF NOT EXISTS organisation 15 | ( 16 | organisation_id SERIAL, 17 | name TEXT UNIQUE NOT NULL, 18 | marketing_name TEXT NOT NULL, 19 | organisation_type TEXT NOT NULL, 20 | 21 | PRIMARY KEY (organisation_id) 22 | ); 23 | 24 | -------------------------------------------------------------------------------- /database/pgsql/tables/3.ems.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS orders 2 | ( 3 | order_id SERIAL, 4 | client_guid TEXT, 5 | order_key INT NOT NULL, 6 | order_key_version INT NOT NULL, 7 | order_submitted_time TIMESTAMP NOT NULL, 8 | instruction TEXT NOT NULL, 9 | market_connector TEXT NOT NULL, 10 | order_type TEXT NOT NULL, 11 | time_in_force TEXT NOT NULL, 12 | handl_inst INT NOT NULL, 13 | symbol TEXT NOT NULL, 14 | exchange TEXT NOT NULL, 15 | side TEXT NOT NULL, 16 | qty DOUBLE PRECISION NOT NULL, 17 | limit_price DOUBLE PRECISION, 18 | filled_qty DOUBLE PRECISION, 19 | filled_avg_price DOUBLE PRECISION, 20 | order_status_id INT NOT NULL, 21 | is_complete BOOLEAN NOT NULL, 22 | is_booked BOOLEAN NOT NULL, 23 | is_expired BOOLEAN NOT NULL, 24 | trade_booking_id INT, 25 | trader_id INT NOT NULL, 26 | account INT NOT NULL, 27 | broker_user_id TEXT NOT NULL, 28 | broker_account TEXT NOT NULL, 29 | description TEXT, 30 | source TEXT NOT NULL, 31 | open_close CHAR(1) NOT NULL, 32 | algo TEXT, 33 | 34 | PRIMARY KEY (order_key, order_key_version), 35 | FOREIGN KEY (order_status_id) REFERENCES order_status (order_status_id), 36 | FOREIGN KEY (trader_id) REFERENCES person (person_id), 37 | UNIQUE (order_id) 38 | ); 39 | 40 | 41 | CREATE TABLE IF NOT EXISTS execution 42 | ( 43 | execution_id SERIAL, 44 | order_id INT NOT NULL, 45 | order_status_id INT NOT NULL, 46 | broker_execution_time TIMESTAMP, 47 | qty DOUBLE PRECISION, 48 | cum_qty DOUBLE PRECISION, 49 | price DOUBLE PRECISION, 50 | avg_price DOUBLE PRECISION, 51 | broker_order_id TEXT, 52 | broker_exec_id TEXT, 53 | previous_broker_exec_id TEXT, 54 | calc_cum_qty DOUBLE PRECISION, 55 | calc_avg_price DOUBLE PRECISION, 56 | exec_type_id INT, 57 | last_mkt TEXT, 58 | last_liquidity_ind_id INT, 59 | text TEXT, 60 | exec_broker TEXT, 61 | cancel_replace_by_execution_id INT, 62 | inserted TIMESTAMP DEFAULT (now() AT TIME ZONE 'utc'), 63 | 64 | PRIMARY KEY (execution_id), 65 | FOREIGN KEY (order_status_id) REFERENCES order_status (order_status_id), 66 | FOREIGN KEY (order_id) REFERENCES orders (order_id), 67 | FOREIGN KEY (exec_type_id) REFERENCES exec_type (exec_type_id) 68 | ); -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2016 Chao Yan. All rights reserved. 2 | 3 | /* 4 | GoTrade is a FIX protocol electronic trading and order management system 5 | written in Golang, structured for typical multi-asset institutional use. 6 | 7 | Dependencies: 8 | gogo/protobuf 9 | A fork of golang/protobuf with tweaks and extras 10 | quickfixgo/quickfix 11 | FIX Engine in Golang 12 | nats-io/nats 13 | Performant messaging bus in Golang, seems resemblance of TibRV 14 | 15 | There are sub-packages within the gotrade package for various components: 16 | core/...: 17 | The low-level API that gives consumers all the knobs they need 18 | proto/...: 19 | The messaging protocol of various entities, in Protobuf format. 20 | services/...: 21 | Core services managing multi-asset order flow 22 | database/...: (not provided yet) 23 | SQL scripts and data layer APIs (PostgreSQL here but the idea is to 24 | support different storage engines without breaking everything) 25 | 26 | Then on top of the core packages, we have: 27 | cmd/...: 28 | Command-line executables, a.k.a final products 29 | test/...: 30 | Integration tests, etc. 31 | 32 | To avoid cyclic imports, imports should never pull in higher-level 33 | APIs into a lower-level package. For example, you could import all of 34 | core and shell from cmd/... or test/..., but you couldn't import any 35 | of shell from core/.... 36 | 37 | */ 38 | package gotrade 39 | 40 | import ( 41 | _ "github.com/cyanly/gotrade/services/marketconnectors/simulator" 42 | _ "github.com/cyanly/gotrade/services/orderrouter" 43 | ) 44 | -------------------------------------------------------------------------------- /proto/Makefile: -------------------------------------------------------------------------------- 1 | PROTO_PATH := ${GOPATH}/src:. 2 | PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/gogo/protobuf/protobuf 3 | PROTO_PATH := ${PROTO_PATH}:${GOPATH}/src/github.com/gogo/protobuf/gogoproto 4 | 5 | .PHONY: all 6 | 7 | all: 8 | protoc --proto_path=${PROTO_PATH} --gogo_out=. ./service/*.proto 9 | protoc --proto_path=${PROTO_PATH} --gogo_out=. ./order/*.proto 10 | protoc --proto_path=${PROTO_PATH} --gogo_out=. ./pricing/*.proto 11 | -------------------------------------------------------------------------------- /proto/order/order.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package proto.order; 4 | 5 | // CODE GEN FLAGS =================================== 6 | 7 | option go_package = "order"; 8 | 9 | import "github.com/gogo/protobuf/gogoproto/gogo.proto"; 10 | option (gogoproto.gostring_all) = true; 11 | option (gogoproto.equal_all) = true; 12 | option (gogoproto.verbose_equal_all) = true; 13 | option (gogoproto.goproto_stringer_all) = false; 14 | option (gogoproto.stringer_all) = true; 15 | option (gogoproto.marshaler_all) = true; 16 | option (gogoproto.unmarshaler_all) = true; 17 | option (gogoproto.sizer_all) = true; 18 | option (gogoproto.populate_all) = true; 19 | option (gogoproto.testgen_all) = true; 20 | option (gogoproto.benchgen_all) = true; 21 | option (gogoproto.goproto_enum_prefix_all) = true; 22 | 23 | // ENTITIES ========================================== 24 | 25 | message Order { 26 | // Idents 27 | required string client_guid = 1 [(gogoproto.nullable) = false]; 28 | required int32 order_id = 2 [(gogoproto.nullable) = false]; 29 | required int32 order_key = 3 [(gogoproto.nullable) = false]; 30 | required int32 version = 4 [(gogoproto.nullable) = false]; 31 | enum OrderMessageType { 32 | NEW = 0; 33 | CANCEL = 1; 34 | REPLACE = 2; 35 | } 36 | optional OrderMessageType message_type = 5 [(gogoproto.nullable) = false]; 37 | 38 | // basic 39 | required Side side = 6 [(gogoproto.nullable) = false]; 40 | required double quantity = 7 [(gogoproto.nullable) = false]; 41 | required string symbol = 8 [(gogoproto.nullable) = false]; 42 | 43 | // execution specific 44 | required OrderType order_type = 16 [(gogoproto.nullable) = false]; 45 | required TimeInForce timeinforce = 17 [(gogoproto.nullable) = false]; 46 | optional double limit_price = 18 [(gogoproto.nullable) = false]; 47 | optional string exchange = 19 [(gogoproto.nullable) = false]; 48 | optional string description = 20 [(gogoproto.nullable) = false]; 49 | optional double filled_quantity = 21 [(gogoproto.nullable) = false]; 50 | optional double filled_avg_price = 22 [(gogoproto.nullable) = false]; 51 | optional OrderStatus order_status = 23 [(gogoproto.nullable) = false]; 52 | 53 | // accounts 54 | required int32 account_id = 30 [(gogoproto.nullable) = false]; 55 | optional string broker_userid = 31 [(gogoproto.nullable) = false]; 56 | optional string broker_account = 32 [(gogoproto.nullable) = false]; 57 | optional string market_connector = 33 [(gogoproto.nullable) = false]; 58 | 59 | // execution extra 60 | optional string settl_ccy = 40 [(gogoproto.nullable) = false]; 61 | required HandlInst handle_inst = 41 [(gogoproto.nullable) = false]; 62 | optional string algo = 42 [(gogoproto.nullable) = false]; 63 | optional bool is_complete = 43 [(gogoproto.nullable) = false]; 64 | optional bool is_booked = 44 [(gogoproto.nullable) = false]; 65 | optional bool is_expired = 45 [(gogoproto.nullable) = false]; 66 | optional int32 trade_booking_id = 46 [(gogoproto.nullable) = false]; 67 | optional string open_close = 47 [(gogoproto.nullable) = false]; 68 | 69 | // source 70 | optional string source = 50 [(gogoproto.nullable) = false]; 71 | optional string trader = 51 [(gogoproto.nullable) = false]; 72 | required int32 trader_id = 52 [(gogoproto.nullable) = false]; 73 | optional string machine = 53 [(gogoproto.nullable) = false]; 74 | optional string memo = 54 [(gogoproto.nullable) = false]; 75 | 76 | // timing 77 | optional string create_datetime = 60 [(gogoproto.nullable) = false]; 78 | optional string submit_datetime = 61 [(gogoproto.nullable) = false]; 79 | 80 | // array components 81 | repeated Execution executions = 70 [(gogoproto.nullable) = false]; 82 | repeated Allocation allocations = 80 [(gogoproto.nullable) = false]; 83 | } 84 | 85 | message Execution { 86 | required int32 execution_id = 1 [(gogoproto.nullable) = false]; 87 | required int32 order_id = 2 [(gogoproto.nullable) = false]; 88 | optional int32 order_key = 3 [(gogoproto.nullable) = false]; 89 | 90 | optional string client_order_id = 5 [(gogoproto.nullable) = false]; 91 | optional string broker_order_id = 6 [(gogoproto.nullable) = false]; 92 | optional string broker_exec_id = 7 [(gogoproto.nullable) = false]; 93 | optional string prev_broker_exec_id = 8 [(gogoproto.nullable) = false]; 94 | optional int32 cancel_replace_by_exececution_id=9 [(gogoproto.nullable) = false]; 95 | 96 | optional OrderStatus order_status = 10 [(gogoproto.nullable) = false]; 97 | 98 | enum ExecType { 99 | NEW = 0; 100 | PARTIAL_FILL = 1; 101 | FILL = 2; 102 | DONE_FOR_DAY = 3; 103 | CANCELED = 4; 104 | REPLACE = 5; 105 | PENDING_CANCEL = 6; 106 | STOPPED = 7; 107 | REJECTED = 8; 108 | SUSPENDED = 9; 109 | PENDING_NEW = 10; 110 | CALCULATED = 11; 111 | EXPIRED = 12; 112 | RESTATED = 13; 113 | PENDING_REPLACE = 14; 114 | TRADE = 15; 115 | TRADE_CORRECT = 16; 116 | TRADE_CANCEL = 17; 117 | ORDER_STATUS = 18; 118 | } 119 | optional ExecType exec_type = 11 [(gogoproto.nullable) = false]; 120 | 121 | optional double quantity = 20 [(gogoproto.nullable) = false]; 122 | optional double price = 21 [(gogoproto.nullable) = false]; 123 | 124 | optional string text = 30 [(gogoproto.nullable) = false]; 125 | optional string lastmkt = 31 [(gogoproto.nullable) = false]; 126 | optional string exec_broker = 32 [(gogoproto.nullable) = false]; 127 | optional LastLiquidityInd last_liquidity = 33 [(gogoproto.nullable) = false]; 128 | 129 | optional double cum_quantity = 50 [(gogoproto.nullable) = false]; 130 | optional double avg_price = 51 [(gogoproto.nullable) = false]; 131 | optional double calc_cum_quantity = 52 [(gogoproto.nullable) = false]; 132 | optional double calc_avg_price = 53 [(gogoproto.nullable) = false]; 133 | 134 | // timing 135 | optional string broker_exec_datetime = 60 [(gogoproto.nullable) = false]; 136 | optional string create_datetime = 61 [(gogoproto.nullable) = false]; 137 | } 138 | 139 | message Allocation { 140 | optional int32 allocation_id = 1 [(gogoproto.nullable) = false]; 141 | optional int32 order_key = 2 [(gogoproto.nullable) = false]; 142 | optional int32 strategy_id = 3 [(gogoproto.nullable) = false]; 143 | 144 | optional double quantity = 10 [(gogoproto.nullable) = false]; 145 | optional double ratio = 11 [(gogoproto.nullable) = false]; 146 | } 147 | 148 | // ENUMS ============================================== 149 | 150 | enum OrderStatus { 151 | NEW = 0; 152 | PARTIALLY_FILLED = 1; 153 | FILLED = 2; 154 | DONE_FOR_DAY = 3; 155 | CANCELLED = 4; 156 | REPLACED = 5; 157 | PENDING_CANCEL = 6; 158 | STOPPED = 7; 159 | REJECTED = 8; 160 | SUSPENDED = 9; 161 | PENDING_NEW = 10; 162 | CALCULATED = 11; 163 | EXPIRED = 12; 164 | ACCEPTED_FOR_BIDDING = 13; 165 | PENDING_REPLACE = 14; 166 | ORDER_RECEIVED = 100; 167 | ORDER_SENT = 101; 168 | MC_ACK_ORDER = 102; 169 | MC_SENT_ORDER = 103; 170 | CANCEL_RECEIVED = 110; 171 | CANCEL_SENT = 111; 172 | MC_ACK_CANCEL = 112; 173 | MC_SENT_CANCEL = 113; 174 | REPLACE_RECEIVED = 120; 175 | REPLACE_SENT = 121; 176 | MC_ACK_REPLACE = 122; 177 | MC_SENT_REPLACE = 123; 178 | EXECUTION_REPLACED = 125; 179 | EXECUTION_CANCELLED = 126; 180 | BOOKABLE = 130; 181 | TRADE_PUMP_RECEIVED = 131; 182 | TRADE_PUMP_PROCESSED = 132; 183 | BOOKING = 133; 184 | SENT_PMS = 134; 185 | BOOKED = 135; 186 | 187 | } 188 | 189 | enum Side { 190 | UNKNOWN_SIDE = 0; 191 | BUY = 1; 192 | SELL = 2; 193 | BUY_MINUS = 3; 194 | SELL_PLUS = 4; 195 | SELL_SHORT = 5; 196 | SELL_SHORT_EXEMPT = 6; 197 | UNDISCLOSED = 7; 198 | CROSS = 8; 199 | CROSS_SHORT = 9; 200 | CROSS_SHORT_EXEMPT = 10; 201 | AS_DEFINED = 11; 202 | OPPOSITE = 12; 203 | SUBSCRIBE = 13; 204 | REDEEM = 14; 205 | LEND = 15; 206 | BORROW = 16; 207 | } 208 | 209 | enum OrderType { 210 | UNKNOWN_ORDER_TYPE = 0; 211 | MARKET = 1; 212 | LIMIT = 2; 213 | STOP = 3; 214 | STOP_LIMIT = 4; 215 | MARKET_ON_CLOSE = 5; 216 | WITH_OR_WITHOUT = 6; 217 | LIMIT_OR_BETTER = 7; 218 | LIMIT_WITH_OR_WITHOUT = 8; 219 | ON_BASIS = 9; 220 | ON_CLOSE = 10; 221 | LIMIT_ON_CLOSE = 11; 222 | FOREX_MARKET = 12; 223 | PREVIOUSLY_QUOTED = 13; 224 | PREVIOUSLY_INDICATED = 14; 225 | FOREX_LIMIT = 15; 226 | FOREX_SWAP = 16; 227 | FOREX_PREVIOUSLY_QUOTED = 17; 228 | FUNARI = 18; 229 | MARKET_IF_TOUCHED = 19; 230 | MARKET_WITH_LEFTOVER_AS_LIMIT = 20; 231 | PREVIOUS_FUND_VALUATION_POINT = 21; 232 | NEXT_FUND_VALUATION_POINT = 22; 233 | PEGGED = 23; 234 | } 235 | 236 | enum TimeInForce { 237 | DAY = 0; 238 | GOOD_TILL_CANCEL = 1; 239 | AT_THE_OPENING = 2; 240 | IMMEDIATE_OR_CANCEL = 3; 241 | FILL_OR_KILL = 4; 242 | GOOD_TILL_CROSSING = 5; 243 | GOOD_TILL_DATE = 6; 244 | AT_THE_CLOSE = 7; 245 | } 246 | 247 | enum HandlInst { 248 | UNKNOWN_HANDL_INST = 0; 249 | AUTOMATED_EXECUTION_ORDER_PRIVATE = 1; 250 | AUTOMATED_EXECUTION_ORDER_PUBLIC = 2; 251 | MANUAL_ORDER = 3; 252 | } 253 | 254 | enum LastLiquidityInd { 255 | UNKNOWN_LAST_LIQUIDITY_IND = 0; 256 | ADDED_LIQUIDITY = 1; 257 | REMOVED_LIQUIDITY = 2; 258 | LIQUIDITY_ROUTED_OUT = 3; 259 | AUCTION = 4; 260 | } 261 | 262 | 263 | // MESSAGES ===================================== 264 | 265 | message NewOrderRequest { 266 | optional Order order = 1; 267 | } 268 | message NewOrderResponse { 269 | optional int32 error_code = 1 [(gogoproto.nullable) = false]; 270 | optional string error_message = 2; 271 | optional Order order = 10; 272 | 273 | } 274 | 275 | message CancelOrderRequest { 276 | optional string client_guid = 1 [(gogoproto.nullable) = false]; 277 | required int32 order_id = 2 [(gogoproto.nullable) = false]; 278 | optional int32 order_key = 3 [(gogoproto.nullable) = false]; 279 | optional int32 version = 4 [(gogoproto.nullable) = false]; 280 | optional string source = 50 [(gogoproto.nullable) = false]; 281 | optional string trader = 51 [(gogoproto.nullable) = false]; 282 | required int32 trader_id = 52 [(gogoproto.nullable) = false]; 283 | optional string machine = 53 [(gogoproto.nullable) = false]; 284 | } 285 | message CancelOrderResponse { 286 | optional int32 error_code = 1 [(gogoproto.nullable) = false]; 287 | optional string error_message = 2; 288 | optional Order order = 10; 289 | } 290 | 291 | message ReplaceOrderRequest { 292 | optional Order order = 1; 293 | optional string source = 50 [(gogoproto.nullable) = false]; 294 | optional string trader = 51 [(gogoproto.nullable) = false]; 295 | required int32 trader_id = 52 [(gogoproto.nullable) = false]; 296 | optional string machine = 53 [(gogoproto.nullable) = false]; 297 | } 298 | message ReplaceOrderResponse { 299 | optional int32 error_code = 1 [(gogoproto.nullable) = false]; 300 | optional string error_message = 2; 301 | optional Order order = 10; 302 | } 303 | 304 | service OrderRequestService { 305 | rpc New (NewOrderRequest) returns (NewOrderResponse); 306 | rpc Cancel (CancelOrderRequest) returns (CancelOrderResponse); 307 | rpc Replace (ReplaceOrderRequest) returns (ReplaceOrderResponse); 308 | } -------------------------------------------------------------------------------- /proto/pricing/pricing.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package proto.pricing; 4 | 5 | // CODE GEN FLAGS =================================== 6 | 7 | option go_package = "pricing"; 8 | 9 | import "github.com/gogo/protobuf/gogoproto/gogo.proto"; 10 | option (gogoproto.gostring_all) = true; 11 | option (gogoproto.equal_all) = true; 12 | option (gogoproto.verbose_equal_all) = true; 13 | option (gogoproto.goproto_stringer_all) = false; 14 | option (gogoproto.stringer_all) = true; 15 | option (gogoproto.marshaler_all) = true; 16 | option (gogoproto.unmarshaler_all) = true; 17 | option (gogoproto.sizer_all) = true; 18 | option (gogoproto.populate_all) = true; 19 | option (gogoproto.testgen_all) = true; 20 | option (gogoproto.benchgen_all) = true; 21 | //option (gogoproto.goproto_enum_prefix_all) = false; 22 | 23 | // ENTITIES ========================================== 24 | 25 | message Quote { 26 | //NOTE: Quote field name matching BloombergAPI 27 | 28 | // Reference data 29 | optional string isin = 1; 30 | optional string sedol = 2; 31 | optional string description = 10; 32 | optional string crncy = 11; 33 | optional double px_trade_lot_size = 12; 34 | optional double round_lot = 13; 35 | optional string exch_code = 14; 36 | optional string prim_exch = 15; 37 | optional string expiry = 16; 38 | optional string multiplier = 17; 39 | optional double tick_size = 18; 40 | optional int32 open_interest = 19; 41 | optional string first_notice_date = 20; 42 | optional string symbol = 21; 43 | optional string symbol_root = 22; 44 | optional double margin = 23; 45 | optional double price_scale = 24; 46 | optional string country = 25; 47 | 48 | // level 1 market data 49 | optional double last_price = 100; 50 | optional int32 size_last_trade = 101; 51 | optional double bid = 102; 52 | optional int32 bid_size = 103; 53 | optional double ask = 104; 54 | optional int32 ask_size = 105; 55 | optional double spread_ba = 106; 56 | optional double high = 107; 57 | optional double low = 108; 58 | optional double open = 109; 59 | optional double close = 110; 60 | optional int64 volume = 111; 61 | optional string bid_datetime = 112; 62 | optional string ask_datetime = 113; 63 | optional string last_datetime = 114; 64 | 65 | // level 2 market data 66 | optional double bid1 = 201; 67 | optional int32 bid1_size = 202; 68 | optional double bid2 = 203; 69 | optional int32 bid2_size = 204; 70 | optional double bid3 = 205; 71 | optional int32 bid3_size = 206; 72 | optional double bid4 = 207; 73 | optional int32 bid4_size = 208; 74 | optional double bid5 = 209; 75 | optional int32 bid5_size = 210; 76 | optional double ask1 = 211; 77 | optional int32 ask1_size = 212; 78 | optional double ask2 = 213; 79 | optional int32 ask2_size = 214; 80 | optional double ask3 = 215; 81 | optional int32 ask3_size = 216; 82 | optional double ask4 = 217; 83 | optional int32 ask4_size = 218; 84 | optional double ask5 = 219; 85 | optional int32 ask5_size = 220; 86 | } 87 | 88 | 89 | // MESSAGES ===================================== 90 | 91 | message QuoteInitialRequest { 92 | required string symbol = 1; 93 | 94 | // options 95 | optional string exchange = 5; 96 | optional bool include_option_chain = 6; 97 | // entitlements 98 | optional string source = 50; 99 | optional string user = 51; 100 | optional int32 user_id = 52; 101 | optional string machine = 53; 102 | } 103 | message QuoteInitialResponse { 104 | optional int32 error_code = 1; 105 | optional string error_message = 2; 106 | optional Quote quote = 8; 107 | } 108 | 109 | service QuoteService { 110 | rpc QuoteInitial (QuoteInitialRequest) returns (QuoteInitialResponse); 111 | } 112 | -------------------------------------------------------------------------------- /proto/pricing/pricingpb_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-gogo. 2 | // source: pricing/pricing.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package pricing is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | pricing/pricing.proto 10 | 11 | It has these top-level messages: 12 | Quote 13 | QuoteInitialRequest 14 | QuoteInitialResponse 15 | */ 16 | package pricing 17 | 18 | import testing "testing" 19 | import math_rand "math/rand" 20 | import time "time" 21 | import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" 22 | import encoding_json "encoding/json" 23 | import fmt "fmt" 24 | import go_parser "go/parser" 25 | import proto "github.com/gogo/protobuf/proto" 26 | import math "math" 27 | 28 | // discarding unused import gogoproto "github.com/gogo/protobuf/gogoproto/gogo.pb" 29 | 30 | // Reference imports to suppress errors if they are not otherwise used. 31 | var _ = proto.Marshal 32 | var _ = math.Inf 33 | 34 | func TestQuoteProto(t *testing.T) { 35 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 36 | p := NewPopulatedQuote(popr, false) 37 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 38 | if err != nil { 39 | panic(err) 40 | } 41 | msg := &Quote{} 42 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 43 | panic(err) 44 | } 45 | for i := range data { 46 | data[i] = byte(popr.Intn(256)) 47 | } 48 | if err := p.VerboseEqual(msg); err != nil { 49 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 50 | } 51 | if !p.Equal(msg) { 52 | t.Fatalf("%#v !Proto %#v", msg, p) 53 | } 54 | } 55 | 56 | func TestQuoteMarshalTo(t *testing.T) { 57 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 58 | p := NewPopulatedQuote(popr, false) 59 | size := p.Size() 60 | data := make([]byte, size) 61 | for i := range data { 62 | data[i] = byte(popr.Intn(256)) 63 | } 64 | _, err := p.MarshalTo(data) 65 | if err != nil { 66 | panic(err) 67 | } 68 | msg := &Quote{} 69 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 70 | panic(err) 71 | } 72 | for i := range data { 73 | data[i] = byte(popr.Intn(256)) 74 | } 75 | if err := p.VerboseEqual(msg); err != nil { 76 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 77 | } 78 | if !p.Equal(msg) { 79 | t.Fatalf("%#v !Proto %#v", msg, p) 80 | } 81 | } 82 | 83 | func BenchmarkQuoteProtoMarshal(b *testing.B) { 84 | popr := math_rand.New(math_rand.NewSource(616)) 85 | total := 0 86 | pops := make([]*Quote, 10000) 87 | for i := 0; i < 10000; i++ { 88 | pops[i] = NewPopulatedQuote(popr, false) 89 | } 90 | b.ResetTimer() 91 | for i := 0; i < b.N; i++ { 92 | data, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000]) 93 | if err != nil { 94 | panic(err) 95 | } 96 | total += len(data) 97 | } 98 | b.SetBytes(int64(total / b.N)) 99 | } 100 | 101 | func BenchmarkQuoteProtoUnmarshal(b *testing.B) { 102 | popr := math_rand.New(math_rand.NewSource(616)) 103 | total := 0 104 | datas := make([][]byte, 10000) 105 | for i := 0; i < 10000; i++ { 106 | data, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedQuote(popr, false)) 107 | if err != nil { 108 | panic(err) 109 | } 110 | datas[i] = data 111 | } 112 | msg := &Quote{} 113 | b.ResetTimer() 114 | for i := 0; i < b.N; i++ { 115 | total += len(datas[i%10000]) 116 | if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { 117 | panic(err) 118 | } 119 | } 120 | b.SetBytes(int64(total / b.N)) 121 | } 122 | 123 | func TestQuoteInitialRequestProto(t *testing.T) { 124 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 125 | p := NewPopulatedQuoteInitialRequest(popr, false) 126 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 127 | if err != nil { 128 | panic(err) 129 | } 130 | msg := &QuoteInitialRequest{} 131 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 132 | panic(err) 133 | } 134 | for i := range data { 135 | data[i] = byte(popr.Intn(256)) 136 | } 137 | if err := p.VerboseEqual(msg); err != nil { 138 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 139 | } 140 | if !p.Equal(msg) { 141 | t.Fatalf("%#v !Proto %#v", msg, p) 142 | } 143 | } 144 | 145 | func TestQuoteInitialRequestMarshalTo(t *testing.T) { 146 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 147 | p := NewPopulatedQuoteInitialRequest(popr, false) 148 | size := p.Size() 149 | data := make([]byte, size) 150 | for i := range data { 151 | data[i] = byte(popr.Intn(256)) 152 | } 153 | _, err := p.MarshalTo(data) 154 | if err != nil { 155 | panic(err) 156 | } 157 | msg := &QuoteInitialRequest{} 158 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 159 | panic(err) 160 | } 161 | for i := range data { 162 | data[i] = byte(popr.Intn(256)) 163 | } 164 | if err := p.VerboseEqual(msg); err != nil { 165 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 166 | } 167 | if !p.Equal(msg) { 168 | t.Fatalf("%#v !Proto %#v", msg, p) 169 | } 170 | } 171 | 172 | func BenchmarkQuoteInitialRequestProtoMarshal(b *testing.B) { 173 | popr := math_rand.New(math_rand.NewSource(616)) 174 | total := 0 175 | pops := make([]*QuoteInitialRequest, 10000) 176 | for i := 0; i < 10000; i++ { 177 | pops[i] = NewPopulatedQuoteInitialRequest(popr, false) 178 | } 179 | b.ResetTimer() 180 | for i := 0; i < b.N; i++ { 181 | data, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000]) 182 | if err != nil { 183 | panic(err) 184 | } 185 | total += len(data) 186 | } 187 | b.SetBytes(int64(total / b.N)) 188 | } 189 | 190 | func BenchmarkQuoteInitialRequestProtoUnmarshal(b *testing.B) { 191 | popr := math_rand.New(math_rand.NewSource(616)) 192 | total := 0 193 | datas := make([][]byte, 10000) 194 | for i := 0; i < 10000; i++ { 195 | data, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedQuoteInitialRequest(popr, false)) 196 | if err != nil { 197 | panic(err) 198 | } 199 | datas[i] = data 200 | } 201 | msg := &QuoteInitialRequest{} 202 | b.ResetTimer() 203 | for i := 0; i < b.N; i++ { 204 | total += len(datas[i%10000]) 205 | if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { 206 | panic(err) 207 | } 208 | } 209 | b.SetBytes(int64(total / b.N)) 210 | } 211 | 212 | func TestQuoteInitialResponseProto(t *testing.T) { 213 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 214 | p := NewPopulatedQuoteInitialResponse(popr, false) 215 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 216 | if err != nil { 217 | panic(err) 218 | } 219 | msg := &QuoteInitialResponse{} 220 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 221 | panic(err) 222 | } 223 | for i := range data { 224 | data[i] = byte(popr.Intn(256)) 225 | } 226 | if err := p.VerboseEqual(msg); err != nil { 227 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 228 | } 229 | if !p.Equal(msg) { 230 | t.Fatalf("%#v !Proto %#v", msg, p) 231 | } 232 | } 233 | 234 | func TestQuoteInitialResponseMarshalTo(t *testing.T) { 235 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 236 | p := NewPopulatedQuoteInitialResponse(popr, false) 237 | size := p.Size() 238 | data := make([]byte, size) 239 | for i := range data { 240 | data[i] = byte(popr.Intn(256)) 241 | } 242 | _, err := p.MarshalTo(data) 243 | if err != nil { 244 | panic(err) 245 | } 246 | msg := &QuoteInitialResponse{} 247 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 248 | panic(err) 249 | } 250 | for i := range data { 251 | data[i] = byte(popr.Intn(256)) 252 | } 253 | if err := p.VerboseEqual(msg); err != nil { 254 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 255 | } 256 | if !p.Equal(msg) { 257 | t.Fatalf("%#v !Proto %#v", msg, p) 258 | } 259 | } 260 | 261 | func BenchmarkQuoteInitialResponseProtoMarshal(b *testing.B) { 262 | popr := math_rand.New(math_rand.NewSource(616)) 263 | total := 0 264 | pops := make([]*QuoteInitialResponse, 10000) 265 | for i := 0; i < 10000; i++ { 266 | pops[i] = NewPopulatedQuoteInitialResponse(popr, false) 267 | } 268 | b.ResetTimer() 269 | for i := 0; i < b.N; i++ { 270 | data, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000]) 271 | if err != nil { 272 | panic(err) 273 | } 274 | total += len(data) 275 | } 276 | b.SetBytes(int64(total / b.N)) 277 | } 278 | 279 | func BenchmarkQuoteInitialResponseProtoUnmarshal(b *testing.B) { 280 | popr := math_rand.New(math_rand.NewSource(616)) 281 | total := 0 282 | datas := make([][]byte, 10000) 283 | for i := 0; i < 10000; i++ { 284 | data, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedQuoteInitialResponse(popr, false)) 285 | if err != nil { 286 | panic(err) 287 | } 288 | datas[i] = data 289 | } 290 | msg := &QuoteInitialResponse{} 291 | b.ResetTimer() 292 | for i := 0; i < b.N; i++ { 293 | total += len(datas[i%10000]) 294 | if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { 295 | panic(err) 296 | } 297 | } 298 | b.SetBytes(int64(total / b.N)) 299 | } 300 | 301 | func TestQuoteJSON(t *testing.T) { 302 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 303 | p := NewPopulatedQuote(popr, true) 304 | jsondata, err := encoding_json.Marshal(p) 305 | if err != nil { 306 | if _, ok := err.(*encoding_json.UnsupportedTypeError); ok { 307 | t.Skip(err) 308 | } 309 | t.Fatal(err) 310 | } 311 | msg := &Quote{} 312 | err = encoding_json.Unmarshal(jsondata, msg) 313 | if err != nil { 314 | t.Fatal(err) 315 | } 316 | if err := p.VerboseEqual(msg); err != nil { 317 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 318 | } 319 | if !p.Equal(msg) { 320 | t.Fatalf("%#v !Json Equal %#v", msg, p) 321 | } 322 | } 323 | func TestQuoteInitialRequestJSON(t *testing.T) { 324 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 325 | p := NewPopulatedQuoteInitialRequest(popr, true) 326 | jsondata, err := encoding_json.Marshal(p) 327 | if err != nil { 328 | if _, ok := err.(*encoding_json.UnsupportedTypeError); ok { 329 | t.Skip(err) 330 | } 331 | t.Fatal(err) 332 | } 333 | msg := &QuoteInitialRequest{} 334 | err = encoding_json.Unmarshal(jsondata, msg) 335 | if err != nil { 336 | t.Fatal(err) 337 | } 338 | if err := p.VerboseEqual(msg); err != nil { 339 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 340 | } 341 | if !p.Equal(msg) { 342 | t.Fatalf("%#v !Json Equal %#v", msg, p) 343 | } 344 | } 345 | func TestQuoteInitialResponseJSON(t *testing.T) { 346 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 347 | p := NewPopulatedQuoteInitialResponse(popr, true) 348 | jsondata, err := encoding_json.Marshal(p) 349 | if err != nil { 350 | if _, ok := err.(*encoding_json.UnsupportedTypeError); ok { 351 | t.Skip(err) 352 | } 353 | t.Fatal(err) 354 | } 355 | msg := &QuoteInitialResponse{} 356 | err = encoding_json.Unmarshal(jsondata, msg) 357 | if err != nil { 358 | t.Fatal(err) 359 | } 360 | if err := p.VerboseEqual(msg); err != nil { 361 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 362 | } 363 | if !p.Equal(msg) { 364 | t.Fatalf("%#v !Json Equal %#v", msg, p) 365 | } 366 | } 367 | func TestQuoteProtoText(t *testing.T) { 368 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 369 | p := NewPopulatedQuote(popr, true) 370 | data := github_com_gogo_protobuf_proto.MarshalTextString(p) 371 | msg := &Quote{} 372 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 373 | panic(err) 374 | } 375 | if err := p.VerboseEqual(msg); err != nil { 376 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 377 | } 378 | if !p.Equal(msg) { 379 | t.Fatalf("%#v !Proto %#v", msg, p) 380 | } 381 | } 382 | 383 | func TestQuoteProtoCompactText(t *testing.T) { 384 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 385 | p := NewPopulatedQuote(popr, true) 386 | data := github_com_gogo_protobuf_proto.CompactTextString(p) 387 | msg := &Quote{} 388 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 389 | panic(err) 390 | } 391 | if err := p.VerboseEqual(msg); err != nil { 392 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 393 | } 394 | if !p.Equal(msg) { 395 | t.Fatalf("%#v !Proto %#v", msg, p) 396 | } 397 | } 398 | 399 | func TestQuoteInitialRequestProtoText(t *testing.T) { 400 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 401 | p := NewPopulatedQuoteInitialRequest(popr, true) 402 | data := github_com_gogo_protobuf_proto.MarshalTextString(p) 403 | msg := &QuoteInitialRequest{} 404 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 405 | panic(err) 406 | } 407 | if err := p.VerboseEqual(msg); err != nil { 408 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 409 | } 410 | if !p.Equal(msg) { 411 | t.Fatalf("%#v !Proto %#v", msg, p) 412 | } 413 | } 414 | 415 | func TestQuoteInitialRequestProtoCompactText(t *testing.T) { 416 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 417 | p := NewPopulatedQuoteInitialRequest(popr, true) 418 | data := github_com_gogo_protobuf_proto.CompactTextString(p) 419 | msg := &QuoteInitialRequest{} 420 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 421 | panic(err) 422 | } 423 | if err := p.VerboseEqual(msg); err != nil { 424 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 425 | } 426 | if !p.Equal(msg) { 427 | t.Fatalf("%#v !Proto %#v", msg, p) 428 | } 429 | } 430 | 431 | func TestQuoteInitialResponseProtoText(t *testing.T) { 432 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 433 | p := NewPopulatedQuoteInitialResponse(popr, true) 434 | data := github_com_gogo_protobuf_proto.MarshalTextString(p) 435 | msg := &QuoteInitialResponse{} 436 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 437 | panic(err) 438 | } 439 | if err := p.VerboseEqual(msg); err != nil { 440 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 441 | } 442 | if !p.Equal(msg) { 443 | t.Fatalf("%#v !Proto %#v", msg, p) 444 | } 445 | } 446 | 447 | func TestQuoteInitialResponseProtoCompactText(t *testing.T) { 448 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 449 | p := NewPopulatedQuoteInitialResponse(popr, true) 450 | data := github_com_gogo_protobuf_proto.CompactTextString(p) 451 | msg := &QuoteInitialResponse{} 452 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 453 | panic(err) 454 | } 455 | if err := p.VerboseEqual(msg); err != nil { 456 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 457 | } 458 | if !p.Equal(msg) { 459 | t.Fatalf("%#v !Proto %#v", msg, p) 460 | } 461 | } 462 | 463 | func TestQuoteStringer(t *testing.T) { 464 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 465 | p := NewPopulatedQuote(popr, false) 466 | s1 := p.String() 467 | s2 := fmt.Sprintf("%v", p) 468 | if s1 != s2 { 469 | t.Fatalf("String want %v got %v", s1, s2) 470 | } 471 | } 472 | func TestQuoteInitialRequestStringer(t *testing.T) { 473 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 474 | p := NewPopulatedQuoteInitialRequest(popr, false) 475 | s1 := p.String() 476 | s2 := fmt.Sprintf("%v", p) 477 | if s1 != s2 { 478 | t.Fatalf("String want %v got %v", s1, s2) 479 | } 480 | } 481 | func TestQuoteInitialResponseStringer(t *testing.T) { 482 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 483 | p := NewPopulatedQuoteInitialResponse(popr, false) 484 | s1 := p.String() 485 | s2 := fmt.Sprintf("%v", p) 486 | if s1 != s2 { 487 | t.Fatalf("String want %v got %v", s1, s2) 488 | } 489 | } 490 | func TestQuoteSize(t *testing.T) { 491 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 492 | p := NewPopulatedQuote(popr, true) 493 | size2 := github_com_gogo_protobuf_proto.Size(p) 494 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 495 | if err != nil { 496 | panic(err) 497 | } 498 | size := p.Size() 499 | if len(data) != size { 500 | t.Errorf("size %v != marshalled size %v", size, len(data)) 501 | } 502 | if size2 != size { 503 | t.Errorf("size %v != before marshal proto.Size %v", size, size2) 504 | } 505 | size3 := github_com_gogo_protobuf_proto.Size(p) 506 | if size3 != size { 507 | t.Errorf("size %v != after marshal proto.Size %v", size, size3) 508 | } 509 | } 510 | 511 | func BenchmarkQuoteSize(b *testing.B) { 512 | popr := math_rand.New(math_rand.NewSource(616)) 513 | total := 0 514 | pops := make([]*Quote, 1000) 515 | for i := 0; i < 1000; i++ { 516 | pops[i] = NewPopulatedQuote(popr, false) 517 | } 518 | b.ResetTimer() 519 | for i := 0; i < b.N; i++ { 520 | total += pops[i%1000].Size() 521 | } 522 | b.SetBytes(int64(total / b.N)) 523 | } 524 | 525 | func TestQuoteInitialRequestSize(t *testing.T) { 526 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 527 | p := NewPopulatedQuoteInitialRequest(popr, true) 528 | size2 := github_com_gogo_protobuf_proto.Size(p) 529 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 530 | if err != nil { 531 | panic(err) 532 | } 533 | size := p.Size() 534 | if len(data) != size { 535 | t.Errorf("size %v != marshalled size %v", size, len(data)) 536 | } 537 | if size2 != size { 538 | t.Errorf("size %v != before marshal proto.Size %v", size, size2) 539 | } 540 | size3 := github_com_gogo_protobuf_proto.Size(p) 541 | if size3 != size { 542 | t.Errorf("size %v != after marshal proto.Size %v", size, size3) 543 | } 544 | } 545 | 546 | func BenchmarkQuoteInitialRequestSize(b *testing.B) { 547 | popr := math_rand.New(math_rand.NewSource(616)) 548 | total := 0 549 | pops := make([]*QuoteInitialRequest, 1000) 550 | for i := 0; i < 1000; i++ { 551 | pops[i] = NewPopulatedQuoteInitialRequest(popr, false) 552 | } 553 | b.ResetTimer() 554 | for i := 0; i < b.N; i++ { 555 | total += pops[i%1000].Size() 556 | } 557 | b.SetBytes(int64(total / b.N)) 558 | } 559 | 560 | func TestQuoteInitialResponseSize(t *testing.T) { 561 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 562 | p := NewPopulatedQuoteInitialResponse(popr, true) 563 | size2 := github_com_gogo_protobuf_proto.Size(p) 564 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 565 | if err != nil { 566 | panic(err) 567 | } 568 | size := p.Size() 569 | if len(data) != size { 570 | t.Errorf("size %v != marshalled size %v", size, len(data)) 571 | } 572 | if size2 != size { 573 | t.Errorf("size %v != before marshal proto.Size %v", size, size2) 574 | } 575 | size3 := github_com_gogo_protobuf_proto.Size(p) 576 | if size3 != size { 577 | t.Errorf("size %v != after marshal proto.Size %v", size, size3) 578 | } 579 | } 580 | 581 | func BenchmarkQuoteInitialResponseSize(b *testing.B) { 582 | popr := math_rand.New(math_rand.NewSource(616)) 583 | total := 0 584 | pops := make([]*QuoteInitialResponse, 1000) 585 | for i := 0; i < 1000; i++ { 586 | pops[i] = NewPopulatedQuoteInitialResponse(popr, false) 587 | } 588 | b.ResetTimer() 589 | for i := 0; i < b.N; i++ { 590 | total += pops[i%1000].Size() 591 | } 592 | b.SetBytes(int64(total / b.N)) 593 | } 594 | 595 | func TestQuoteGoString(t *testing.T) { 596 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 597 | p := NewPopulatedQuote(popr, false) 598 | s1 := p.GoString() 599 | s2 := fmt.Sprintf("%#v", p) 600 | if s1 != s2 { 601 | t.Fatalf("GoString want %v got %v", s1, s2) 602 | } 603 | _, err := go_parser.ParseExpr(s1) 604 | if err != nil { 605 | panic(err) 606 | } 607 | } 608 | func TestQuoteInitialRequestGoString(t *testing.T) { 609 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 610 | p := NewPopulatedQuoteInitialRequest(popr, false) 611 | s1 := p.GoString() 612 | s2 := fmt.Sprintf("%#v", p) 613 | if s1 != s2 { 614 | t.Fatalf("GoString want %v got %v", s1, s2) 615 | } 616 | _, err := go_parser.ParseExpr(s1) 617 | if err != nil { 618 | panic(err) 619 | } 620 | } 621 | func TestQuoteInitialResponseGoString(t *testing.T) { 622 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 623 | p := NewPopulatedQuoteInitialResponse(popr, false) 624 | s1 := p.GoString() 625 | s2 := fmt.Sprintf("%#v", p) 626 | if s1 != s2 { 627 | t.Fatalf("GoString want %v got %v", s1, s2) 628 | } 629 | _, err := go_parser.ParseExpr(s1) 630 | if err != nil { 631 | panic(err) 632 | } 633 | } 634 | func TestQuoteVerboseEqual(t *testing.T) { 635 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 636 | p := NewPopulatedQuote(popr, false) 637 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 638 | if err != nil { 639 | panic(err) 640 | } 641 | msg := &Quote{} 642 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 643 | panic(err) 644 | } 645 | if err := p.VerboseEqual(msg); err != nil { 646 | t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err) 647 | } 648 | } 649 | func TestQuoteInitialRequestVerboseEqual(t *testing.T) { 650 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 651 | p := NewPopulatedQuoteInitialRequest(popr, false) 652 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 653 | if err != nil { 654 | panic(err) 655 | } 656 | msg := &QuoteInitialRequest{} 657 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 658 | panic(err) 659 | } 660 | if err := p.VerboseEqual(msg); err != nil { 661 | t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err) 662 | } 663 | } 664 | func TestQuoteInitialResponseVerboseEqual(t *testing.T) { 665 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 666 | p := NewPopulatedQuoteInitialResponse(popr, false) 667 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 668 | if err != nil { 669 | panic(err) 670 | } 671 | msg := &QuoteInitialResponse{} 672 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 673 | panic(err) 674 | } 675 | if err := p.VerboseEqual(msg); err != nil { 676 | t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err) 677 | } 678 | } 679 | 680 | //These tests are generated by github.com/gogo/protobuf/plugin/testgen 681 | -------------------------------------------------------------------------------- /proto/service/service.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-gogo. 2 | // source: service/service.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package service is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | service/service.proto 10 | 11 | It has these top-level messages: 12 | Heartbeat 13 | */ 14 | package service 15 | 16 | import proto "github.com/gogo/protobuf/proto" 17 | import math "math" 18 | 19 | // discarding unused import gogoproto "github.com/gogo/protobuf/gogoproto/gogo.pb" 20 | 21 | import io "io" 22 | import fmt "fmt" 23 | import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" 24 | 25 | import strings "strings" 26 | import reflect "reflect" 27 | 28 | import sort "sort" 29 | import strconv "strconv" 30 | 31 | import bytes "bytes" 32 | 33 | // Reference imports to suppress errors if they are not otherwise used. 34 | var _ = proto.Marshal 35 | var _ = math.Inf 36 | 37 | type Heartbeat_Status int32 38 | 39 | const ( 40 | STOPPED Heartbeat_Status = 0 41 | STARTING Heartbeat_Status = 1 42 | RUNNING Heartbeat_Status = 2 43 | ERROR Heartbeat_Status = 3 44 | STOPPING Heartbeat_Status = 4 45 | ) 46 | 47 | var Heartbeat_Status_name = map[int32]string{ 48 | 0: "STOPPED", 49 | 1: "STARTING", 50 | 2: "RUNNING", 51 | 3: "ERROR", 52 | 4: "STOPPING", 53 | } 54 | var Heartbeat_Status_value = map[string]int32{ 55 | "STOPPED": 0, 56 | "STARTING": 1, 57 | "RUNNING": 2, 58 | "ERROR": 3, 59 | "STOPPING": 4, 60 | } 61 | 62 | func (x Heartbeat_Status) Enum() *Heartbeat_Status { 63 | p := new(Heartbeat_Status) 64 | *p = x 65 | return p 66 | } 67 | func (x Heartbeat_Status) String() string { 68 | return proto.EnumName(Heartbeat_Status_name, int32(x)) 69 | } 70 | func (x *Heartbeat_Status) UnmarshalJSON(data []byte) error { 71 | value, err := proto.UnmarshalJSONEnum(Heartbeat_Status_value, data, "Heartbeat_Status") 72 | if err != nil { 73 | return err 74 | } 75 | *x = Heartbeat_Status(value) 76 | return nil 77 | } 78 | 79 | type Heartbeat struct { 80 | Name string `protobuf:"bytes,1,req,name=name" json:"name"` 81 | Id string `protobuf:"bytes,2,opt,name=id" json:"id"` 82 | Machine string `protobuf:"bytes,3,opt,name=machine" json:"machine"` 83 | Status Heartbeat_Status `protobuf:"varint,4,opt,name=status,enum=proto.service.Heartbeat_Status" json:"status"` 84 | CurrentDatetime string `protobuf:"bytes,5,opt,name=current_datetime" json:"current_datetime"` 85 | CreationDatetime string `protobuf:"bytes,6,opt,name=creation_datetime" json:"creation_datetime"` 86 | Description string `protobuf:"bytes,7,opt,name=description" json:"description"` 87 | XXX_unrecognized []byte `json:"-"` 88 | } 89 | 90 | func (m *Heartbeat) Reset() { *m = Heartbeat{} } 91 | func (*Heartbeat) ProtoMessage() {} 92 | 93 | func (m *Heartbeat) GetName() string { 94 | if m != nil { 95 | return m.Name 96 | } 97 | return "" 98 | } 99 | 100 | func (m *Heartbeat) GetId() string { 101 | if m != nil { 102 | return m.Id 103 | } 104 | return "" 105 | } 106 | 107 | func (m *Heartbeat) GetMachine() string { 108 | if m != nil { 109 | return m.Machine 110 | } 111 | return "" 112 | } 113 | 114 | func (m *Heartbeat) GetStatus() Heartbeat_Status { 115 | if m != nil { 116 | return m.Status 117 | } 118 | return STOPPED 119 | } 120 | 121 | func (m *Heartbeat) GetCurrentDatetime() string { 122 | if m != nil { 123 | return m.CurrentDatetime 124 | } 125 | return "" 126 | } 127 | 128 | func (m *Heartbeat) GetCreationDatetime() string { 129 | if m != nil { 130 | return m.CreationDatetime 131 | } 132 | return "" 133 | } 134 | 135 | func (m *Heartbeat) GetDescription() string { 136 | if m != nil { 137 | return m.Description 138 | } 139 | return "" 140 | } 141 | 142 | func init() { 143 | proto.RegisterEnum("proto.service.Heartbeat_Status", Heartbeat_Status_name, Heartbeat_Status_value) 144 | } 145 | func (m *Heartbeat) Unmarshal(data []byte) error { 146 | var hasFields [1]uint64 147 | l := len(data) 148 | iNdEx := 0 149 | for iNdEx < l { 150 | var wire uint64 151 | for shift := uint(0); ; shift += 7 { 152 | if iNdEx >= l { 153 | return io.ErrUnexpectedEOF 154 | } 155 | b := data[iNdEx] 156 | iNdEx++ 157 | wire |= (uint64(b) & 0x7F) << shift 158 | if b < 0x80 { 159 | break 160 | } 161 | } 162 | fieldNum := int32(wire >> 3) 163 | wireType := int(wire & 0x7) 164 | switch fieldNum { 165 | case 1: 166 | if wireType != 2 { 167 | return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) 168 | } 169 | var stringLen uint64 170 | for shift := uint(0); ; shift += 7 { 171 | if iNdEx >= l { 172 | return io.ErrUnexpectedEOF 173 | } 174 | b := data[iNdEx] 175 | iNdEx++ 176 | stringLen |= (uint64(b) & 0x7F) << shift 177 | if b < 0x80 { 178 | break 179 | } 180 | } 181 | postIndex := iNdEx + int(stringLen) 182 | if postIndex > l { 183 | return io.ErrUnexpectedEOF 184 | } 185 | m.Name = string(data[iNdEx:postIndex]) 186 | iNdEx = postIndex 187 | hasFields[0] |= uint64(0x00000001) 188 | case 2: 189 | if wireType != 2 { 190 | return fmt.Errorf("proto: wrong wireType = %d for field Id", wireType) 191 | } 192 | var stringLen uint64 193 | for shift := uint(0); ; shift += 7 { 194 | if iNdEx >= l { 195 | return io.ErrUnexpectedEOF 196 | } 197 | b := data[iNdEx] 198 | iNdEx++ 199 | stringLen |= (uint64(b) & 0x7F) << shift 200 | if b < 0x80 { 201 | break 202 | } 203 | } 204 | postIndex := iNdEx + int(stringLen) 205 | if postIndex > l { 206 | return io.ErrUnexpectedEOF 207 | } 208 | m.Id = string(data[iNdEx:postIndex]) 209 | iNdEx = postIndex 210 | case 3: 211 | if wireType != 2 { 212 | return fmt.Errorf("proto: wrong wireType = %d for field Machine", wireType) 213 | } 214 | var stringLen uint64 215 | for shift := uint(0); ; shift += 7 { 216 | if iNdEx >= l { 217 | return io.ErrUnexpectedEOF 218 | } 219 | b := data[iNdEx] 220 | iNdEx++ 221 | stringLen |= (uint64(b) & 0x7F) << shift 222 | if b < 0x80 { 223 | break 224 | } 225 | } 226 | postIndex := iNdEx + int(stringLen) 227 | if postIndex > l { 228 | return io.ErrUnexpectedEOF 229 | } 230 | m.Machine = string(data[iNdEx:postIndex]) 231 | iNdEx = postIndex 232 | case 4: 233 | if wireType != 0 { 234 | return fmt.Errorf("proto: wrong wireType = %d for field Status", wireType) 235 | } 236 | for shift := uint(0); ; shift += 7 { 237 | if iNdEx >= l { 238 | return io.ErrUnexpectedEOF 239 | } 240 | b := data[iNdEx] 241 | iNdEx++ 242 | m.Status |= (Heartbeat_Status(b) & 0x7F) << shift 243 | if b < 0x80 { 244 | break 245 | } 246 | } 247 | case 5: 248 | if wireType != 2 { 249 | return fmt.Errorf("proto: wrong wireType = %d for field CurrentDatetime", wireType) 250 | } 251 | var stringLen uint64 252 | for shift := uint(0); ; shift += 7 { 253 | if iNdEx >= l { 254 | return io.ErrUnexpectedEOF 255 | } 256 | b := data[iNdEx] 257 | iNdEx++ 258 | stringLen |= (uint64(b) & 0x7F) << shift 259 | if b < 0x80 { 260 | break 261 | } 262 | } 263 | postIndex := iNdEx + int(stringLen) 264 | if postIndex > l { 265 | return io.ErrUnexpectedEOF 266 | } 267 | m.CurrentDatetime = string(data[iNdEx:postIndex]) 268 | iNdEx = postIndex 269 | case 6: 270 | if wireType != 2 { 271 | return fmt.Errorf("proto: wrong wireType = %d for field CreationDatetime", wireType) 272 | } 273 | var stringLen uint64 274 | for shift := uint(0); ; shift += 7 { 275 | if iNdEx >= l { 276 | return io.ErrUnexpectedEOF 277 | } 278 | b := data[iNdEx] 279 | iNdEx++ 280 | stringLen |= (uint64(b) & 0x7F) << shift 281 | if b < 0x80 { 282 | break 283 | } 284 | } 285 | postIndex := iNdEx + int(stringLen) 286 | if postIndex > l { 287 | return io.ErrUnexpectedEOF 288 | } 289 | m.CreationDatetime = string(data[iNdEx:postIndex]) 290 | iNdEx = postIndex 291 | case 7: 292 | if wireType != 2 { 293 | return fmt.Errorf("proto: wrong wireType = %d for field Description", wireType) 294 | } 295 | var stringLen uint64 296 | for shift := uint(0); ; shift += 7 { 297 | if iNdEx >= l { 298 | return io.ErrUnexpectedEOF 299 | } 300 | b := data[iNdEx] 301 | iNdEx++ 302 | stringLen |= (uint64(b) & 0x7F) << shift 303 | if b < 0x80 { 304 | break 305 | } 306 | } 307 | postIndex := iNdEx + int(stringLen) 308 | if postIndex > l { 309 | return io.ErrUnexpectedEOF 310 | } 311 | m.Description = string(data[iNdEx:postIndex]) 312 | iNdEx = postIndex 313 | default: 314 | var sizeOfWire int 315 | for { 316 | sizeOfWire++ 317 | wire >>= 7 318 | if wire == 0 { 319 | break 320 | } 321 | } 322 | iNdEx -= sizeOfWire 323 | skippy, err := skipService(data[iNdEx:]) 324 | if err != nil { 325 | return err 326 | } 327 | if (iNdEx + skippy) > l { 328 | return io.ErrUnexpectedEOF 329 | } 330 | m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...) 331 | iNdEx += skippy 332 | } 333 | } 334 | if hasFields[0]&uint64(0x00000001) == 0 { 335 | return github_com_gogo_protobuf_proto.NewRequiredNotSetError("name") 336 | } 337 | 338 | return nil 339 | } 340 | func skipService(data []byte) (n int, err error) { 341 | l := len(data) 342 | iNdEx := 0 343 | for iNdEx < l { 344 | var wire uint64 345 | for shift := uint(0); ; shift += 7 { 346 | if iNdEx >= l { 347 | return 0, io.ErrUnexpectedEOF 348 | } 349 | b := data[iNdEx] 350 | iNdEx++ 351 | wire |= (uint64(b) & 0x7F) << shift 352 | if b < 0x80 { 353 | break 354 | } 355 | } 356 | wireType := int(wire & 0x7) 357 | switch wireType { 358 | case 0: 359 | for { 360 | if iNdEx >= l { 361 | return 0, io.ErrUnexpectedEOF 362 | } 363 | iNdEx++ 364 | if data[iNdEx-1] < 0x80 { 365 | break 366 | } 367 | } 368 | return iNdEx, nil 369 | case 1: 370 | iNdEx += 8 371 | return iNdEx, nil 372 | case 2: 373 | var length int 374 | for shift := uint(0); ; shift += 7 { 375 | if iNdEx >= l { 376 | return 0, io.ErrUnexpectedEOF 377 | } 378 | b := data[iNdEx] 379 | iNdEx++ 380 | length |= (int(b) & 0x7F) << shift 381 | if b < 0x80 { 382 | break 383 | } 384 | } 385 | iNdEx += length 386 | return iNdEx, nil 387 | case 3: 388 | for { 389 | var innerWire uint64 390 | var start int = iNdEx 391 | for shift := uint(0); ; shift += 7 { 392 | if iNdEx >= l { 393 | return 0, io.ErrUnexpectedEOF 394 | } 395 | b := data[iNdEx] 396 | iNdEx++ 397 | innerWire |= (uint64(b) & 0x7F) << shift 398 | if b < 0x80 { 399 | break 400 | } 401 | } 402 | innerWireType := int(innerWire & 0x7) 403 | if innerWireType == 4 { 404 | break 405 | } 406 | next, err := skipService(data[start:]) 407 | if err != nil { 408 | return 0, err 409 | } 410 | iNdEx = start + next 411 | } 412 | return iNdEx, nil 413 | case 4: 414 | return iNdEx, nil 415 | case 5: 416 | iNdEx += 4 417 | return iNdEx, nil 418 | default: 419 | return 0, fmt.Errorf("proto: illegal wireType %d", wireType) 420 | } 421 | } 422 | panic("unreachable") 423 | } 424 | func (this *Heartbeat) String() string { 425 | if this == nil { 426 | return "nil" 427 | } 428 | s := strings.Join([]string{`&Heartbeat{`, 429 | `Name:` + fmt.Sprintf("%v", this.Name) + `,`, 430 | `Id:` + fmt.Sprintf("%v", this.Id) + `,`, 431 | `Machine:` + fmt.Sprintf("%v", this.Machine) + `,`, 432 | `Status:` + fmt.Sprintf("%v", this.Status) + `,`, 433 | `CurrentDatetime:` + fmt.Sprintf("%v", this.CurrentDatetime) + `,`, 434 | `CreationDatetime:` + fmt.Sprintf("%v", this.CreationDatetime) + `,`, 435 | `Description:` + fmt.Sprintf("%v", this.Description) + `,`, 436 | `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`, 437 | `}`, 438 | }, "") 439 | return s 440 | } 441 | func valueToStringService(v interface{}) string { 442 | rv := reflect.ValueOf(v) 443 | if rv.IsNil() { 444 | return "nil" 445 | } 446 | pv := reflect.Indirect(rv).Interface() 447 | return fmt.Sprintf("*%v", pv) 448 | } 449 | func (m *Heartbeat) Size() (n int) { 450 | var l int 451 | _ = l 452 | l = len(m.Name) 453 | n += 1 + l + sovService(uint64(l)) 454 | l = len(m.Id) 455 | n += 1 + l + sovService(uint64(l)) 456 | l = len(m.Machine) 457 | n += 1 + l + sovService(uint64(l)) 458 | n += 1 + sovService(uint64(m.Status)) 459 | l = len(m.CurrentDatetime) 460 | n += 1 + l + sovService(uint64(l)) 461 | l = len(m.CreationDatetime) 462 | n += 1 + l + sovService(uint64(l)) 463 | l = len(m.Description) 464 | n += 1 + l + sovService(uint64(l)) 465 | if m.XXX_unrecognized != nil { 466 | n += len(m.XXX_unrecognized) 467 | } 468 | return n 469 | } 470 | 471 | func sovService(x uint64) (n int) { 472 | for { 473 | n++ 474 | x >>= 7 475 | if x == 0 { 476 | break 477 | } 478 | } 479 | return n 480 | } 481 | func sozService(x uint64) (n int) { 482 | return sovService(uint64((x << 1) ^ uint64((int64(x) >> 63)))) 483 | } 484 | func NewPopulatedHeartbeat(r randyService, easy bool) *Heartbeat { 485 | this := &Heartbeat{} 486 | this.Name = randStringService(r) 487 | this.Id = randStringService(r) 488 | this.Machine = randStringService(r) 489 | this.Status = Heartbeat_Status([]int32{0, 1, 2, 3, 4}[r.Intn(5)]) 490 | this.CurrentDatetime = randStringService(r) 491 | this.CreationDatetime = randStringService(r) 492 | this.Description = randStringService(r) 493 | if !easy && r.Intn(10) != 0 { 494 | this.XXX_unrecognized = randUnrecognizedService(r, 8) 495 | } 496 | return this 497 | } 498 | 499 | type randyService interface { 500 | Float32() float32 501 | Float64() float64 502 | Int63() int64 503 | Int31() int32 504 | Uint32() uint32 505 | Intn(n int) int 506 | } 507 | 508 | func randUTF8RuneService(r randyService) rune { 509 | ru := r.Intn(62) 510 | if ru < 10 { 511 | return rune(ru + 48) 512 | } else if ru < 36 { 513 | return rune(ru + 55) 514 | } 515 | return rune(ru + 61) 516 | } 517 | func randStringService(r randyService) string { 518 | v1 := r.Intn(100) 519 | tmps := make([]rune, v1) 520 | for i := 0; i < v1; i++ { 521 | tmps[i] = randUTF8RuneService(r) 522 | } 523 | return string(tmps) 524 | } 525 | func randUnrecognizedService(r randyService, maxFieldNumber int) (data []byte) { 526 | l := r.Intn(5) 527 | for i := 0; i < l; i++ { 528 | wire := r.Intn(4) 529 | if wire == 3 { 530 | wire = 5 531 | } 532 | fieldNumber := maxFieldNumber + r.Intn(100) 533 | data = randFieldService(data, r, fieldNumber, wire) 534 | } 535 | return data 536 | } 537 | func randFieldService(data []byte, r randyService, fieldNumber int, wire int) []byte { 538 | key := uint32(fieldNumber)<<3 | uint32(wire) 539 | switch wire { 540 | case 0: 541 | data = encodeVarintPopulateService(data, uint64(key)) 542 | v2 := r.Int63() 543 | if r.Intn(2) == 0 { 544 | v2 *= -1 545 | } 546 | data = encodeVarintPopulateService(data, uint64(v2)) 547 | case 1: 548 | data = encodeVarintPopulateService(data, uint64(key)) 549 | data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256))) 550 | case 2: 551 | data = encodeVarintPopulateService(data, uint64(key)) 552 | ll := r.Intn(100) 553 | data = encodeVarintPopulateService(data, uint64(ll)) 554 | for j := 0; j < ll; j++ { 555 | data = append(data, byte(r.Intn(256))) 556 | } 557 | default: 558 | data = encodeVarintPopulateService(data, uint64(key)) 559 | data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256))) 560 | } 561 | return data 562 | } 563 | func encodeVarintPopulateService(data []byte, v uint64) []byte { 564 | for v >= 1<<7 { 565 | data = append(data, uint8(uint64(v)&0x7f|0x80)) 566 | v >>= 7 567 | } 568 | data = append(data, uint8(v)) 569 | return data 570 | } 571 | func (m *Heartbeat) Marshal() (data []byte, err error) { 572 | size := m.Size() 573 | data = make([]byte, size) 574 | n, err := m.MarshalTo(data) 575 | if err != nil { 576 | return nil, err 577 | } 578 | return data[:n], nil 579 | } 580 | 581 | func (m *Heartbeat) MarshalTo(data []byte) (n int, err error) { 582 | var i int 583 | _ = i 584 | var l int 585 | _ = l 586 | data[i] = 0xa 587 | i++ 588 | i = encodeVarintService(data, i, uint64(len(m.Name))) 589 | i += copy(data[i:], m.Name) 590 | data[i] = 0x12 591 | i++ 592 | i = encodeVarintService(data, i, uint64(len(m.Id))) 593 | i += copy(data[i:], m.Id) 594 | data[i] = 0x1a 595 | i++ 596 | i = encodeVarintService(data, i, uint64(len(m.Machine))) 597 | i += copy(data[i:], m.Machine) 598 | data[i] = 0x20 599 | i++ 600 | i = encodeVarintService(data, i, uint64(m.Status)) 601 | data[i] = 0x2a 602 | i++ 603 | i = encodeVarintService(data, i, uint64(len(m.CurrentDatetime))) 604 | i += copy(data[i:], m.CurrentDatetime) 605 | data[i] = 0x32 606 | i++ 607 | i = encodeVarintService(data, i, uint64(len(m.CreationDatetime))) 608 | i += copy(data[i:], m.CreationDatetime) 609 | data[i] = 0x3a 610 | i++ 611 | i = encodeVarintService(data, i, uint64(len(m.Description))) 612 | i += copy(data[i:], m.Description) 613 | if m.XXX_unrecognized != nil { 614 | i += copy(data[i:], m.XXX_unrecognized) 615 | } 616 | return i, nil 617 | } 618 | 619 | func encodeFixed64Service(data []byte, offset int, v uint64) int { 620 | data[offset] = uint8(v) 621 | data[offset+1] = uint8(v >> 8) 622 | data[offset+2] = uint8(v >> 16) 623 | data[offset+3] = uint8(v >> 24) 624 | data[offset+4] = uint8(v >> 32) 625 | data[offset+5] = uint8(v >> 40) 626 | data[offset+6] = uint8(v >> 48) 627 | data[offset+7] = uint8(v >> 56) 628 | return offset + 8 629 | } 630 | func encodeFixed32Service(data []byte, offset int, v uint32) int { 631 | data[offset] = uint8(v) 632 | data[offset+1] = uint8(v >> 8) 633 | data[offset+2] = uint8(v >> 16) 634 | data[offset+3] = uint8(v >> 24) 635 | return offset + 4 636 | } 637 | func encodeVarintService(data []byte, offset int, v uint64) int { 638 | for v >= 1<<7 { 639 | data[offset] = uint8(v&0x7f | 0x80) 640 | v >>= 7 641 | offset++ 642 | } 643 | data[offset] = uint8(v) 644 | return offset + 1 645 | } 646 | func (this *Heartbeat) GoString() string { 647 | if this == nil { 648 | return "nil" 649 | } 650 | s := strings.Join([]string{`&service.Heartbeat{` + 651 | `Name:` + fmt.Sprintf("%#v", this.Name), 652 | `Id:` + fmt.Sprintf("%#v", this.Id), 653 | `Machine:` + fmt.Sprintf("%#v", this.Machine), 654 | `Status:` + fmt.Sprintf("%#v", this.Status), 655 | `CurrentDatetime:` + fmt.Sprintf("%#v", this.CurrentDatetime), 656 | `CreationDatetime:` + fmt.Sprintf("%#v", this.CreationDatetime), 657 | `Description:` + fmt.Sprintf("%#v", this.Description), 658 | `XXX_unrecognized:` + fmt.Sprintf("%#v", this.XXX_unrecognized) + `}`}, ", ") 659 | return s 660 | } 661 | func valueToGoStringService(v interface{}, typ string) string { 662 | rv := reflect.ValueOf(v) 663 | if rv.IsNil() { 664 | return "nil" 665 | } 666 | pv := reflect.Indirect(rv).Interface() 667 | return fmt.Sprintf("func(v %v) *%v { return &v } ( %#v )", typ, typ, pv) 668 | } 669 | func extensionToGoStringService(e map[int32]github_com_gogo_protobuf_proto.Extension) string { 670 | if e == nil { 671 | return "nil" 672 | } 673 | s := "map[int32]proto.Extension{" 674 | keys := make([]int, 0, len(e)) 675 | for k := range e { 676 | keys = append(keys, int(k)) 677 | } 678 | sort.Ints(keys) 679 | ss := []string{} 680 | for _, k := range keys { 681 | ss = append(ss, strconv.Itoa(k)+": "+e[int32(k)].GoString()) 682 | } 683 | s += strings.Join(ss, ",") + "}" 684 | return s 685 | } 686 | func (this *Heartbeat) VerboseEqual(that interface{}) error { 687 | if that == nil { 688 | if this == nil { 689 | return nil 690 | } 691 | return fmt.Errorf("that == nil && this != nil") 692 | } 693 | 694 | that1, ok := that.(*Heartbeat) 695 | if !ok { 696 | return fmt.Errorf("that is not of type *Heartbeat") 697 | } 698 | if that1 == nil { 699 | if this == nil { 700 | return nil 701 | } 702 | return fmt.Errorf("that is type *Heartbeat but is nil && this != nil") 703 | } else if this == nil { 704 | return fmt.Errorf("that is type *Heartbeatbut is not nil && this == nil") 705 | } 706 | if this.Name != that1.Name { 707 | return fmt.Errorf("Name this(%v) Not Equal that(%v)", this.Name, that1.Name) 708 | } 709 | if this.Id != that1.Id { 710 | return fmt.Errorf("Id this(%v) Not Equal that(%v)", this.Id, that1.Id) 711 | } 712 | if this.Machine != that1.Machine { 713 | return fmt.Errorf("Machine this(%v) Not Equal that(%v)", this.Machine, that1.Machine) 714 | } 715 | if this.Status != that1.Status { 716 | return fmt.Errorf("Status this(%v) Not Equal that(%v)", this.Status, that1.Status) 717 | } 718 | if this.CurrentDatetime != that1.CurrentDatetime { 719 | return fmt.Errorf("CurrentDatetime this(%v) Not Equal that(%v)", this.CurrentDatetime, that1.CurrentDatetime) 720 | } 721 | if this.CreationDatetime != that1.CreationDatetime { 722 | return fmt.Errorf("CreationDatetime this(%v) Not Equal that(%v)", this.CreationDatetime, that1.CreationDatetime) 723 | } 724 | if this.Description != that1.Description { 725 | return fmt.Errorf("Description this(%v) Not Equal that(%v)", this.Description, that1.Description) 726 | } 727 | if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { 728 | return fmt.Errorf("XXX_unrecognized this(%v) Not Equal that(%v)", this.XXX_unrecognized, that1.XXX_unrecognized) 729 | } 730 | return nil 731 | } 732 | func (this *Heartbeat) Equal(that interface{}) bool { 733 | if that == nil { 734 | if this == nil { 735 | return true 736 | } 737 | return false 738 | } 739 | 740 | that1, ok := that.(*Heartbeat) 741 | if !ok { 742 | return false 743 | } 744 | if that1 == nil { 745 | if this == nil { 746 | return true 747 | } 748 | return false 749 | } else if this == nil { 750 | return false 751 | } 752 | if this.Name != that1.Name { 753 | return false 754 | } 755 | if this.Id != that1.Id { 756 | return false 757 | } 758 | if this.Machine != that1.Machine { 759 | return false 760 | } 761 | if this.Status != that1.Status { 762 | return false 763 | } 764 | if this.CurrentDatetime != that1.CurrentDatetime { 765 | return false 766 | } 767 | if this.CreationDatetime != that1.CreationDatetime { 768 | return false 769 | } 770 | if this.Description != that1.Description { 771 | return false 772 | } 773 | if !bytes.Equal(this.XXX_unrecognized, that1.XXX_unrecognized) { 774 | return false 775 | } 776 | return true 777 | } 778 | -------------------------------------------------------------------------------- /proto/service/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package proto.service; 4 | 5 | // CODE GEN FLAGS =================================== 6 | 7 | option go_package = "service"; 8 | 9 | import "github.com/gogo/protobuf/gogoproto/gogo.proto"; 10 | option (gogoproto.gostring_all) = true; 11 | option (gogoproto.equal_all) = true; 12 | option (gogoproto.verbose_equal_all) = true; 13 | option (gogoproto.goproto_stringer_all) = false; 14 | option (gogoproto.stringer_all) = true; 15 | option (gogoproto.marshaler_all) = true; 16 | option (gogoproto.unmarshaler_all) = true; 17 | option (gogoproto.sizer_all) = true; 18 | option (gogoproto.populate_all) = true; 19 | option (gogoproto.testgen_all) = true; 20 | option (gogoproto.benchgen_all) = true; 21 | option (gogoproto.goproto_enum_prefix_all) = false; 22 | 23 | // ENTITIES ========================================== 24 | 25 | message Heartbeat { 26 | enum Status { 27 | STOPPED = 0; 28 | STARTING = 1; 29 | RUNNING = 2; 30 | ERROR = 3; 31 | STOPPING = 4; 32 | } 33 | 34 | required string name = 1 [(gogoproto.nullable) = false]; 35 | optional string id = 2 [(gogoproto.nullable) = false]; 36 | optional string machine = 3 [(gogoproto.nullable) = false]; 37 | optional Status status = 4 [(gogoproto.nullable) = false]; 38 | optional string current_datetime = 5 [(gogoproto.nullable) = false]; 39 | optional string creation_datetime = 6 [(gogoproto.nullable) = false]; 40 | optional string description = 7 [(gogoproto.nullable) = false]; 41 | } 42 | -------------------------------------------------------------------------------- /proto/service/servicepb_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-gogo. 2 | // source: service/service.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package service is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | service/service.proto 10 | 11 | It has these top-level messages: 12 | Heartbeat 13 | */ 14 | package service 15 | 16 | import testing "testing" 17 | import math_rand "math/rand" 18 | import time "time" 19 | import github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" 20 | import encoding_json "encoding/json" 21 | import fmt "fmt" 22 | import go_parser "go/parser" 23 | import proto "github.com/gogo/protobuf/proto" 24 | import math "math" 25 | 26 | // discarding unused import gogoproto "github.com/gogo/protobuf/gogoproto/gogo.pb" 27 | 28 | // Reference imports to suppress errors if they are not otherwise used. 29 | var _ = proto.Marshal 30 | var _ = math.Inf 31 | 32 | func TestHeartbeatProto(t *testing.T) { 33 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 34 | p := NewPopulatedHeartbeat(popr, false) 35 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 36 | if err != nil { 37 | panic(err) 38 | } 39 | msg := &Heartbeat{} 40 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 41 | panic(err) 42 | } 43 | for i := range data { 44 | data[i] = byte(popr.Intn(256)) 45 | } 46 | if err := p.VerboseEqual(msg); err != nil { 47 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 48 | } 49 | if !p.Equal(msg) { 50 | t.Fatalf("%#v !Proto %#v", msg, p) 51 | } 52 | } 53 | 54 | func TestHeartbeatMarshalTo(t *testing.T) { 55 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 56 | p := NewPopulatedHeartbeat(popr, false) 57 | size := p.Size() 58 | data := make([]byte, size) 59 | for i := range data { 60 | data[i] = byte(popr.Intn(256)) 61 | } 62 | _, err := p.MarshalTo(data) 63 | if err != nil { 64 | panic(err) 65 | } 66 | msg := &Heartbeat{} 67 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 68 | panic(err) 69 | } 70 | for i := range data { 71 | data[i] = byte(popr.Intn(256)) 72 | } 73 | if err := p.VerboseEqual(msg); err != nil { 74 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 75 | } 76 | if !p.Equal(msg) { 77 | t.Fatalf("%#v !Proto %#v", msg, p) 78 | } 79 | } 80 | 81 | func BenchmarkHeartbeatProtoMarshal(b *testing.B) { 82 | popr := math_rand.New(math_rand.NewSource(616)) 83 | total := 0 84 | pops := make([]*Heartbeat, 10000) 85 | for i := 0; i < 10000; i++ { 86 | pops[i] = NewPopulatedHeartbeat(popr, false) 87 | } 88 | b.ResetTimer() 89 | for i := 0; i < b.N; i++ { 90 | data, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000]) 91 | if err != nil { 92 | panic(err) 93 | } 94 | total += len(data) 95 | } 96 | b.SetBytes(int64(total / b.N)) 97 | } 98 | 99 | func BenchmarkHeartbeatProtoUnmarshal(b *testing.B) { 100 | popr := math_rand.New(math_rand.NewSource(616)) 101 | total := 0 102 | datas := make([][]byte, 10000) 103 | for i := 0; i < 10000; i++ { 104 | data, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedHeartbeat(popr, false)) 105 | if err != nil { 106 | panic(err) 107 | } 108 | datas[i] = data 109 | } 110 | msg := &Heartbeat{} 111 | b.ResetTimer() 112 | for i := 0; i < b.N; i++ { 113 | total += len(datas[i%10000]) 114 | if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { 115 | panic(err) 116 | } 117 | } 118 | b.SetBytes(int64(total / b.N)) 119 | } 120 | 121 | func TestHeartbeatJSON(t *testing.T) { 122 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 123 | p := NewPopulatedHeartbeat(popr, true) 124 | jsondata, err := encoding_json.Marshal(p) 125 | if err != nil { 126 | if _, ok := err.(*encoding_json.UnsupportedTypeError); ok { 127 | t.Skip(err) 128 | } 129 | t.Fatal(err) 130 | } 131 | msg := &Heartbeat{} 132 | err = encoding_json.Unmarshal(jsondata, msg) 133 | if err != nil { 134 | t.Fatal(err) 135 | } 136 | if err := p.VerboseEqual(msg); err != nil { 137 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 138 | } 139 | if !p.Equal(msg) { 140 | t.Fatalf("%#v !Json Equal %#v", msg, p) 141 | } 142 | } 143 | func TestHeartbeatProtoText(t *testing.T) { 144 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 145 | p := NewPopulatedHeartbeat(popr, true) 146 | data := github_com_gogo_protobuf_proto.MarshalTextString(p) 147 | msg := &Heartbeat{} 148 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 149 | panic(err) 150 | } 151 | if err := p.VerboseEqual(msg); err != nil { 152 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 153 | } 154 | if !p.Equal(msg) { 155 | t.Fatalf("%#v !Proto %#v", msg, p) 156 | } 157 | } 158 | 159 | func TestHeartbeatProtoCompactText(t *testing.T) { 160 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 161 | p := NewPopulatedHeartbeat(popr, true) 162 | data := github_com_gogo_protobuf_proto.CompactTextString(p) 163 | msg := &Heartbeat{} 164 | if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { 165 | panic(err) 166 | } 167 | if err := p.VerboseEqual(msg); err != nil { 168 | t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) 169 | } 170 | if !p.Equal(msg) { 171 | t.Fatalf("%#v !Proto %#v", msg, p) 172 | } 173 | } 174 | 175 | func TestHeartbeatStringer(t *testing.T) { 176 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 177 | p := NewPopulatedHeartbeat(popr, false) 178 | s1 := p.String() 179 | s2 := fmt.Sprintf("%v", p) 180 | if s1 != s2 { 181 | t.Fatalf("String want %v got %v", s1, s2) 182 | } 183 | } 184 | func TestHeartbeatSize(t *testing.T) { 185 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 186 | p := NewPopulatedHeartbeat(popr, true) 187 | size2 := github_com_gogo_protobuf_proto.Size(p) 188 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 189 | if err != nil { 190 | panic(err) 191 | } 192 | size := p.Size() 193 | if len(data) != size { 194 | t.Errorf("size %v != marshalled size %v", size, len(data)) 195 | } 196 | if size2 != size { 197 | t.Errorf("size %v != before marshal proto.Size %v", size, size2) 198 | } 199 | size3 := github_com_gogo_protobuf_proto.Size(p) 200 | if size3 != size { 201 | t.Errorf("size %v != after marshal proto.Size %v", size, size3) 202 | } 203 | } 204 | 205 | func BenchmarkHeartbeatSize(b *testing.B) { 206 | popr := math_rand.New(math_rand.NewSource(616)) 207 | total := 0 208 | pops := make([]*Heartbeat, 1000) 209 | for i := 0; i < 1000; i++ { 210 | pops[i] = NewPopulatedHeartbeat(popr, false) 211 | } 212 | b.ResetTimer() 213 | for i := 0; i < b.N; i++ { 214 | total += pops[i%1000].Size() 215 | } 216 | b.SetBytes(int64(total / b.N)) 217 | } 218 | 219 | func TestHeartbeatGoString(t *testing.T) { 220 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 221 | p := NewPopulatedHeartbeat(popr, false) 222 | s1 := p.GoString() 223 | s2 := fmt.Sprintf("%#v", p) 224 | if s1 != s2 { 225 | t.Fatalf("GoString want %v got %v", s1, s2) 226 | } 227 | _, err := go_parser.ParseExpr(s1) 228 | if err != nil { 229 | panic(err) 230 | } 231 | } 232 | func TestHeartbeatVerboseEqual(t *testing.T) { 233 | popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) 234 | p := NewPopulatedHeartbeat(popr, false) 235 | data, err := github_com_gogo_protobuf_proto.Marshal(p) 236 | if err != nil { 237 | panic(err) 238 | } 239 | msg := &Heartbeat{} 240 | if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { 241 | panic(err) 242 | } 243 | if err := p.VerboseEqual(msg); err != nil { 244 | t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err) 245 | } 246 | } 247 | 248 | //These tests are generated by github.com/gogo/protobuf/plugin/testgen 249 | -------------------------------------------------------------------------------- /services/marketconnectors/common/alloc/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/alloc/todo -------------------------------------------------------------------------------- /services/marketconnectors/common/config.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | import "github.com/cyanly/gotrade/core/messagebus" 4 | 5 | const ( 6 | // DefaultMessageBusURL is the default MessageBus URL for service communications. 7 | DefaultMessageBusURL = messagebus.DefaultUrl 8 | ) 9 | 10 | // Config represents the configuration for base service. 11 | type Config struct { 12 | MessageBusURL string `json:"mc_message_bus"` 13 | DatabaseDriver string `json:"database_driver"` //database storage engine driver 14 | DatabaseUrl string `json:"database_url"` //database connection string 15 | 16 | MarketConnectorName string 17 | } 18 | 19 | // NewConfig returns an instance of Config with defaults. 20 | func NewConfig() Config { 21 | return Config{ 22 | MessageBusURL: DefaultMessageBusURL, 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /services/marketconnectors/common/equity/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/equity/todo -------------------------------------------------------------------------------- /services/marketconnectors/common/future/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/future/todo -------------------------------------------------------------------------------- /services/marketconnectors/common/fx/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/fx/todo -------------------------------------------------------------------------------- /services/marketconnectors/common/limit/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/limit/todo -------------------------------------------------------------------------------- /services/marketconnectors/common/options/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/options/todo -------------------------------------------------------------------------------- /services/marketconnectors/common/order/execution_fix44.go: -------------------------------------------------------------------------------- 1 | package order 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/quickfixgo/quickfix" 7 | fix44er "github.com/quickfixgo/quickfix/fix44/executionreport" 8 | 9 | proto "github.com/cyanly/gotrade/proto/order" 10 | util "github.com/cyanly/gotrade/services/marketconnectors" 11 | ) 12 | 13 | // Common route handler for FIX4.4 Execution Report message 14 | // this function can be sub-classed to extend with special fields if a market connector requires 15 | func (app FIXClient) onFIX44ExecutionReport(msg fix44er.Message, sessionID quickfix.SessionID) quickfix.MessageRejectError { 16 | // Construct execution report protobuf struct from FIX message 17 | er := &proto.Execution{ 18 | // required fields 19 | ClientOrderId: *msg.ClOrdID, 20 | BrokerOrderId: msg.OrderID, 21 | ExecType: proto.Execution_ExecType(util.FIXEnumToProtoEnum(msg.ExecType)), 22 | OrderStatus: proto.OrderStatus(util.FIXEnumToProtoEnum(msg.OrdStatus)), 23 | BrokerExecId: msg.ExecID, 24 | CumQuantity: msg.CumQty, 25 | AvgPrice: msg.AvgPx, 26 | 27 | // common tags 28 | Quantity: *msg.LastQty, 29 | Price: *msg.LastPx, 30 | Lastmkt: *msg.LastMkt, 31 | } 32 | 33 | if msg.TransactTime != nil { 34 | er.BrokerExecDatetime = msg.TransactTime.UTC().Format(time.RFC3339Nano) 35 | } 36 | if msg.ExecRefID != nil { 37 | er.PrevBrokerExecId = *msg.ExecRefID 38 | } 39 | if msg.Text != nil { 40 | er.Text = *msg.Text 41 | } 42 | 43 | //TODO: Check for Dups - Tags.PossDupFlag 44 | 45 | return app.processExecutionReport(er) 46 | } 47 | -------------------------------------------------------------------------------- /services/marketconnectors/common/order/fixclient.go: -------------------------------------------------------------------------------- 1 | // A common FIX Client required by QuickFIX, should work for most of market connectors. 2 | // 3 | // If a market connector has special tags etc for incoming message, only need its own NewFIXClient() to 4 | // replace common calls to its own callback routes. 5 | // If a market connector has complete different behaviour, it will need to implements its own FIXClient 6 | package order 7 | 8 | import ( 9 | "strconv" 10 | "time" 11 | 12 | messagebus "github.com/nats-io/nats" 13 | "github.com/quickfixgo/quickfix" 14 | fix44er "github.com/quickfixgo/quickfix/fix44/executionreport" 15 | fix44nos "github.com/quickfixgo/quickfix/fix44/newordersingle" 16 | fix44ocj "github.com/quickfixgo/quickfix/fix44/ordercancelreject" 17 | "github.com/quickfixgo/quickfix/tag" 18 | 19 | "strings" 20 | log "github.com/cyanly/gotrade/core/logger" 21 | "github.com/cyanly/gotrade/database" 22 | proto "github.com/cyanly/gotrade/proto/order" 23 | util "github.com/cyanly/gotrade/services/marketconnectors" 24 | "github.com/cyanly/gotrade/services/marketconnectors/common" 25 | ) 26 | 27 | type FIXClient struct { 28 | *quickfix.Initiator 29 | *quickfix.MessageRouter 30 | 31 | Session quickfix.SessionID 32 | MessageBus *messagebus.Conn 33 | OrderStore database.OrderStore 34 | 35 | marketConnectorName string 36 | } 37 | 38 | // Create a FIXClient with common routes for market connectors 39 | func NewFIXClient(c common.Config) *FIXClient { 40 | app := &FIXClient{ 41 | MessageRouter: quickfix.NewMessageRouter(), 42 | 43 | marketConnectorName: c.MarketConnectorName, 44 | } 45 | 46 | // Initiate message bus listening for requests 47 | if msgbus, err := messagebus.Connect(c.MessageBusURL); err != nil { 48 | log.Fatalf("error: Cannot connect to order message bus @ %v", c.MessageBusURL) 49 | } else { 50 | app.MessageBus = msgbus 51 | } 52 | 53 | // Connect to database storage driver 54 | if storage, err := database.NewOrderStore(c.DatabaseDriver, c.DatabaseUrl, nil); err != nil { 55 | log.Fatalf("error: Cannot connect to database driver %v @ %v", c.DatabaseDriver, c.DatabaseUrl) 56 | } else { 57 | app.OrderStore = storage 58 | } 59 | 60 | // QuickFIX settings from config 61 | appSettings := quickfix.NewSettings() 62 | var settings *quickfix.SessionSettings 63 | settings = appSettings.GlobalSettings() 64 | settings.Set("SocketConnectHost", "127.0.0.1") 65 | settings.Set("SocketConnectPort", "5001") 66 | settings.Set("HeartBtInt", "30") 67 | settings.Set("SenderCompID", "CYAN") 68 | settings.Set("TargetCompID", "CORP") 69 | settings.Set("ResetOnLogon", "Y") 70 | settings.Set("FileLogPath", "tmp") 71 | 72 | settings = quickfix.NewSessionSettings() 73 | settings.Set("BeginString", "FIX.4.4") 74 | if session, err := appSettings.AddSession(settings); err != nil { 75 | log.WithError(err).Fatal("FIX Session Error") 76 | } else { 77 | app.Session = session 78 | } 79 | 80 | // FIX routes 81 | app.AddRoute(fix44er.Route(app.onFIX44ExecutionReport)) 82 | app.AddRoute(fix44ocj.Route(app.onFIX44OrderCancelReject)) 83 | 84 | // FIX logging 85 | logFactory := quickfix.NewNullLogFactory() // quickfix.NewScreenLogFactory() 86 | 87 | // Create initiator 88 | if initiator, err := quickfix.NewInitiator(app, quickfix.NewMemoryStoreFactory(), appSettings, logFactory); err != nil { 89 | log.WithError(err).Fatal("FIX NewInitiator Error") 90 | } else { 91 | app.Initiator = initiator 92 | } 93 | 94 | return app 95 | } 96 | 97 | func (app FIXClient) OnCreate(sessionID quickfix.SessionID) { 98 | return 99 | } 100 | 101 | func (app FIXClient) OnLogon(sessionID quickfix.SessionID) { 102 | return 103 | } 104 | 105 | func (app FIXClient) OnLogout(sessionID quickfix.SessionID) { 106 | return 107 | } 108 | 109 | func (app FIXClient) FromAdmin(msg quickfix.Message, sessionID quickfix.SessionID) (reject quickfix.MessageRejectError) { 110 | return 111 | } 112 | 113 | func (app FIXClient) ToAdmin(msg quickfix.Message, sessionID quickfix.SessionID) { 114 | return 115 | } 116 | 117 | func (app FIXClient) ToApp(msg quickfix.Message, sessionID quickfix.SessionID) (err error) { 118 | return 119 | } 120 | 121 | func (app FIXClient) FromApp(msg quickfix.Message, sessionID quickfix.SessionID) (reject quickfix.MessageRejectError) { 122 | log.Infof("FIX->MC EXEC: \n%v", msg.String()) 123 | return app.Route(msg, sessionID) 124 | } 125 | 126 | func stringPtr(s string) *string { return &s } 127 | 128 | // Common Order FIX client routines serving requests from order router 129 | // if a market connector has special case it will need to implement its own start routine like below 130 | func (app FIXClient) Start() error { 131 | err := app.Initiator.Start() 132 | 133 | // Subscribe to order flow topics 134 | // NEW 135 | app.MessageBus.Subscribe("order.NewOrderRequest.MC."+app.marketConnectorName, func(m *messagebus.Msg) { 136 | request := new(proto.NewOrderRequest) 137 | if err := request.Unmarshal(m.Data); err == nil { 138 | order := request.Order 139 | 140 | //TODO: this is only prototype, migrate common tasks: instruments / limits processing 141 | 142 | fixmsg := fix44nos.Message{ 143 | ClOrdID: strconv.Itoa(int(order.OrderKey)) + "." + strconv.Itoa(int(order.Version)), 144 | Side: util.ProtoEnumToFIXEnum(int(order.Side)), 145 | TransactTime: time.Now(), 146 | OrdType: util.ProtoEnumToFIXEnum(int(order.OrderType)), 147 | } 148 | 149 | // Instrument specific 150 | fixmsg.Instrument.Symbol = &order.Symbol 151 | 152 | fixmsg.OrderQty = &order.Quantity 153 | if order.OrderType == proto.OrderType_LIMIT || order.OrderType == proto.OrderType_LIMIT_ON_CLOSE { 154 | fixmsg.Price = &order.LimitPrice 155 | } 156 | 157 | // Broker specific 158 | fixmsg.Account = &order.BrokerAccount 159 | fixmsg.HandlInst = stringPtr(util.ProtoEnumToFIXEnum(int(order.HandleInst))) 160 | 161 | // 142 SenderLocationID 162 | // Mandatory for CME exchanges. It contains a 2-character country. For the US and Canada, the state/province is included. 163 | fixmsg.SenderLocationID = stringPtr("UK") 164 | 165 | log.Info("MC->FIX FIX44NewOrderSingle") 166 | if err := quickfix.SendToTarget(fixmsg, app.Session); err != nil { 167 | log.WithError(err).Fatal("FIX quickfix.SendToTarget Error") 168 | } 169 | 170 | } 171 | }) 172 | 173 | // TODO: CANCEL 174 | 175 | // TODO: REPLACE 176 | 177 | return err 178 | } 179 | 180 | // Common behaviours to persist and publish populated Execution entity into our data layer and message bus 181 | func (app FIXClient) processExecutionReport(er *proto.Execution) quickfix.MessageRejectError { 182 | 183 | // Find the Order by using the OrderKey without the Version 184 | // Need to try to LOAD the order, if that fails then alert stating unsolicited order received 185 | keyVersion := strings.Split(er.ClientOrderId, ".") 186 | if len(keyVersion) != 2 { 187 | return quickfix.ValueIsIncorrect(tag.ClOrdID) 188 | } 189 | if oKey, err := strconv.Atoi(keyVersion[0]); err != nil { 190 | return quickfix.ValueIsIncorrect(tag.ClOrdID) 191 | } else { 192 | if ord, err := app.OrderStore.OrderGetByOrderKey(int32(oKey)); err != nil { 193 | log.WithError(err).Warnf("Received ExecutionReport with OrderKey(%v) does not exist.", oKey) 194 | return nil // TODO: ignore or quickfix.ValueIsIncorrect(tag.ClOrdID) 195 | } else { 196 | er.OrderId = ord.OrderId 197 | } 198 | } 199 | 200 | // Persist the execution report 201 | if err := app.OrderStore.ExecutionCreate(er); err == nil { 202 | //publish to message bus 203 | data, _ := er.Marshal() 204 | app.MessageBus.Publish("order.Execution", data) 205 | } else { 206 | log.WithField("execution", er).WithError(err).Error("[ MC ] ERROR Create Reject Execution") 207 | } 208 | 209 | // Do any validating and if we are good to continue 210 | 211 | // Check if we need to cancel any execution 212 | // Check if we need to correct any execution 213 | switch er.ExecType { 214 | case proto.Execution_TRADE_CANCEL: 215 | case proto.Execution_TRADE_CORRECT: 216 | //TODO: TryGetAmendedExecution 217 | case proto.Execution_RESTATED: 218 | case proto.Execution_REJECTED: 219 | } 220 | 221 | // Check if the trade is fully filled or is done for day or canceled 222 | // .. Send message to TradePump to say trade filled and already to book. 223 | 224 | return nil 225 | } 226 | -------------------------------------------------------------------------------- /services/marketconnectors/common/order/ordercxlreject_fix44.go: -------------------------------------------------------------------------------- 1 | package order 2 | 3 | import ( 4 | "github.com/quickfixgo/quickfix" 5 | fix44ocj "github.com/quickfixgo/quickfix/fix44/ordercancelreject" 6 | ) 7 | 8 | func (app FIXClient) onFIX44OrderCancelReject(msg fix44ocj.Message, sessionID quickfix.SessionID) quickfix.MessageRejectError { 9 | 10 | // TODO: not finished for prototype 11 | 12 | return nil 13 | } 14 | -------------------------------------------------------------------------------- /services/marketconnectors/common/trs/todo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyanly/gotrade/f2cd14de9f6f8c10433145306db383af9bb24ea8/services/marketconnectors/common/trs/todo -------------------------------------------------------------------------------- /services/marketconnectors/sellsidesim/config.go: -------------------------------------------------------------------------------- 1 | package sellsidesim 2 | -------------------------------------------------------------------------------- /services/marketconnectors/sellsidesim/executor.go: -------------------------------------------------------------------------------- 1 | package sellsidesim 2 | 3 | import ( 4 | "strconv" 5 | 6 | "github.com/quickfixgo/quickfix" 7 | "github.com/quickfixgo/quickfix/enum" 8 | fix44er "github.com/quickfixgo/quickfix/fix44/executionreport" 9 | fix44nos "github.com/quickfixgo/quickfix/fix44/newordersingle" 10 | 11 | "github.com/quickfixgo/quickfix/tag" 12 | log "github.com/cyanly/gotrade/core/logger" 13 | ) 14 | 15 | type Executor struct { 16 | orderID int 17 | execID int 18 | *quickfix.MessageRouter 19 | } 20 | 21 | func NewExecutor() *Executor { 22 | e := &Executor{MessageRouter: quickfix.NewMessageRouter()} 23 | //e.AddRoute(fix40nos.Route(e.OnFIX40NewOrderSingle)) 24 | //e.AddRoute(fix41nos.Route(e.OnFIX41NewOrderSingle)) 25 | //e.AddRoute(fix42nos.Route(e.OnFIX42NewOrderSingle)) 26 | //e.AddRoute(fix43nos.Route(e.OnFIX43NewOrderSingle)) 27 | e.AddRoute(fix44nos.Route(e.OnFIX44NewOrderSingle)) 28 | //e.AddRoute(fix50nos.Route(e.OnFIX50NewOrderSingle)) 29 | 30 | return e 31 | } 32 | 33 | func (e *Executor) genOrderID() string { 34 | e.orderID++ 35 | return strconv.Itoa(e.orderID) 36 | } 37 | 38 | func (e *Executor) genExecID() string { 39 | e.execID++ 40 | return strconv.Itoa(e.execID) 41 | } 42 | 43 | //quickfix.Application interface 44 | func (e Executor) OnCreate(sessionID quickfix.SessionID) { return } 45 | func (e Executor) OnLogon(sessionID quickfix.SessionID) { return } 46 | func (e Executor) OnLogout(sessionID quickfix.SessionID) { return } 47 | func (e Executor) ToAdmin(msg quickfix.Message, sessionID quickfix.SessionID) { return } 48 | func (e Executor) ToApp(msg quickfix.Message, sessionID quickfix.SessionID) error { return nil } 49 | func (e Executor) FromAdmin(msg quickfix.Message, sessionID quickfix.SessionID) quickfix.MessageRejectError { 50 | return nil 51 | } 52 | 53 | //Use Message Cracker on Incoming Application Messages 54 | func (e *Executor) FromApp(msg quickfix.Message, sessionID quickfix.SessionID) (reject quickfix.MessageRejectError) { 55 | log.Infof("FIX->SIM: FIX44NewOrderSingle \n%v", msg.String()) 56 | return e.Route(msg, sessionID) 57 | } 58 | 59 | func stringPtr(s string) *string { return &s } 60 | 61 | func (e *Executor) OnFIX44NewOrderSingle(msg fix44nos.Message, sessionID quickfix.SessionID) (err quickfix.MessageRejectError) { 62 | if msg.OrdType != enum.OrdType_LIMIT { 63 | err = quickfix.ValueIsIncorrect(tag.OrdType) 64 | return 65 | } 66 | 67 | execReport := fix44er.Message{ 68 | ClOrdID: &msg.ClOrdID, 69 | Account: msg.Account, 70 | OrderID: e.genOrderID(), 71 | ExecID: e.genExecID(), 72 | ExecType: enum.ExecType_FILL, 73 | OrdStatus: enum.OrdStatus_FILLED, 74 | Side: msg.Side, 75 | Instrument: msg.Instrument, 76 | OrderQtyData: &msg.OrderQtyData, 77 | LeavesQty: 0, 78 | LastQty: msg.OrderQtyData.OrderQty, 79 | CumQty: *msg.OrderQtyData.OrderQty, 80 | AvgPx: *msg.Price, 81 | LastPx: msg.Price, 82 | LastMkt: stringPtr("SIM"), 83 | } 84 | 85 | quickfix.SendToTarget(execReport, sessionID) 86 | 87 | return 88 | } 89 | -------------------------------------------------------------------------------- /services/marketconnectors/sellsidesim/orderbook/orderbook.go: -------------------------------------------------------------------------------- 1 | // A simulated order book bid offer matching by bloomberg pricefeed quotes 2 | // this is to give our sell side FIX engine simulate a false feel of reality, 3 | // so as to support MARKET or LIMIT orders 4 | package orderbook 5 | 6 | import ( 7 | orac "github.com/cyanly/gotrade/core/order" 8 | er "github.com/cyanly/gotrade/core/order/execution" 9 | ems "github.com/cyanly/gotrade/proto/order" 10 | marketdata "github.com/cyanly/gotrade/proto/pricing" 11 | "github.com/nats-io/nats" 12 | 13 | "fmt" 14 | "log" 15 | "math" 16 | "os" 17 | "time" 18 | ) 19 | 20 | type OrderBook struct { 21 | OrdersList map[int32]*ems.Order 22 | Symbol string 23 | Quote *marketdata.Quote 24 | Subscriber *nats.Subscription 25 | } 26 | 27 | type QuoteBand struct { 28 | price float64 29 | quantity float64 30 | next *QuoteBand 31 | } 32 | 33 | var ( 34 | //Public Variables 35 | PXMessageBus *nats.Conn 36 | OrderUpdateChan chan *ems.Order 37 | 38 | //Private Variables 39 | closed chan bool 40 | newOrderChan chan *ems.Order 41 | removeOrderChan chan *ems.Order 42 | rejectListenerChan chan *OrderBook 43 | listeningSymbols map[string]*OrderBook 44 | ) 45 | 46 | func init() { 47 | closed = make(chan bool) 48 | newOrderChan = make(chan *ems.Order) 49 | removeOrderChan = make(chan *ems.Order) 50 | rejectListenerChan = make(chan *OrderBook) 51 | listeningSymbols = make(map[string]*OrderBook) 52 | 53 | fmt.Println("BBGPX @ " + fmt.Sprint(os.Getenv("RMQ_URL")) + "/BBGPX") 54 | PXMessageBus, _ := nats.Connect(fmt.Sprint(os.Getenv("RMQ_URL")) + "/BBGPX") 55 | 56 | OrderUpdateChan = make(chan *ems.Order) 57 | 58 | //Imagine how to get below right in C# 59 | go func() { 60 | for { 61 | select { 62 | case <-closed: 63 | return 64 | case order := <-newOrderChan: 65 | 66 | if listener, exists := listeningSymbols[order.Symbol]; exists == false { 67 | quote := &marketdata.Quote{} 68 | listener := &OrderBook{ 69 | OrdersList: make(map[int32]*ems.Order), 70 | Symbol: order.Symbol, 71 | // Subscriber: subscriber, 72 | Quote: quote, 73 | } 74 | listeningSymbols[order.Symbol] = listener 75 | listener.OrdersList[order.OrderId] = order 76 | 77 | go func(order *ems.Order, listener *OrderBook) { 78 | quoteInitialReq := &marketdata.QuoteInitialRequest{ 79 | Symbol: &order.Symbol, 80 | } 81 | data, _ := quoteInitialReq.Marshal() 82 | initMsg, err := PXMessageBus.Request("Schemas.Pricefeed.QuoteInitialRequest", data, 5*time.Second) 83 | if err != nil { 84 | quoteInitialRes := &marketdata.QuoteInitialResponse{} 85 | quoteInitialRes.Unmarshal(initMsg.Data) 86 | if quoteInitialRes.Quote != nil { 87 | listener.Quote = quoteInitialRes.Quote 88 | } 89 | } else { 90 | rejectListenerChan <- listener 91 | return 92 | } 93 | 94 | if len(listener.OrdersList) == 0 { 95 | return 96 | } 97 | subscriber, _ := PXMessageBus.Subscribe(order.Symbol, func(m *nats.Msg) { 98 | //Simulate trade filling triggered only by a trade in market 99 | quote := &marketdata.Quote{} 100 | quote.Unmarshal(m.Data) 101 | if err := listener.Quote.Unmarshal(m.Data); err == nil { 102 | listener.ProcessOrderBookUpdate(quote) 103 | } 104 | 105 | }) 106 | listener.Subscriber = subscriber 107 | 108 | log.Println("SUB " + order.Symbol) 109 | }(order, listener) 110 | } else { 111 | listener.OrdersList[order.OrderId] = order 112 | } 113 | 114 | case listener := <-rejectListenerChan: 115 | first := true 116 | for _, order := range listener.OrdersList { 117 | er.NewStatusExecution(order, ems.Execution_REJECTED, "Missing Market Data") 118 | OrderUpdateChan <- order 119 | if first { 120 | delete(listeningSymbols, order.Symbol) 121 | log.Println("UNSUB " + order.Symbol) 122 | first = false 123 | } 124 | } 125 | listener.Subscriber.Unsubscribe() 126 | 127 | case order := <-removeOrderChan: 128 | if listener, exists := listeningSymbols[order.Symbol]; exists == true { 129 | if order, exists := listener.OrdersList[order.OrderId]; exists == true { 130 | delete(listener.OrdersList, order.OrderId) 131 | } 132 | if len(listener.OrdersList) == 0 { 133 | listener.Subscriber.Unsubscribe() 134 | delete(listeningSymbols, order.Symbol) 135 | log.Println("UNSUB " + order.Symbol) 136 | } 137 | } 138 | } 139 | } 140 | }() 141 | } 142 | 143 | func Close() { 144 | closed <- true 145 | PXMessageBus.Close() 146 | } 147 | 148 | func RegisterOrder(order *ems.Order) { 149 | order.FilledQuantity = float64(0) 150 | order.FilledAvgPrice = float64(0) 151 | 152 | newOrderChan <- order 153 | } 154 | 155 | func UnRegisterOrder(order *ems.Order) { 156 | removeOrderChan <- order 157 | } 158 | 159 | func (m *OrderBook) ProcessOrderBookUpdate(triggerQuote *marketdata.Quote) { 160 | bidQuotes, askQuotes := m.ConstrucOrderBookLinkedList() 161 | 162 | for _, order := range m.OrdersList { 163 | if orac.IsCompleted(order) { 164 | continue 165 | } 166 | 167 | var quotebands *QuoteBand 168 | if order.Side == ems.Side_BUY { 169 | quotebands = askQuotes 170 | } else { 171 | quotebands = bidQuotes 172 | } 173 | if quotebands == nil { 174 | continue 175 | } 176 | 177 | if order.OrderType == ems.OrderType_MARKET { 178 | for quotebands != nil && order.Quantity > order.FilledQuantity { 179 | fillQty := math.Min(quotebands.quantity, (order.Quantity - order.FilledQuantity)) 180 | DoTrade(order, fillQty, quotebands.price) 181 | quotebands.quantity -= fillQty 182 | if quotebands.quantity == 0 { 183 | quotebands = quotebands.next 184 | } 185 | } 186 | } 187 | if triggerQuote.LastPrice == nil { 188 | continue 189 | } 190 | if order.OrderType == ems.OrderType_LIMIT { 191 | for quotebands != nil && order.Quantity > order.FilledQuantity { 192 | if order.Side == ems.Side_BUY { 193 | if order.LimitPrice < quotebands.price { 194 | break 195 | } 196 | } else { 197 | if order.LimitPrice > quotebands.price { 198 | break 199 | } 200 | } 201 | fillQty := math.Min(quotebands.quantity, (order.Quantity - order.FilledQuantity)) 202 | DoTrade(order, fillQty, quotebands.price) 203 | quotebands.quantity -= fillQty 204 | if quotebands.quantity == 0 { 205 | quotebands = quotebands.next 206 | } 207 | } 208 | } 209 | } 210 | } 211 | 212 | func DoTrade(order *ems.Order, quantity float64, price float64) { 213 | log.Println(fmt.Sprintf("fill %v: %v @ %v", order.OrderId, quantity, price)) 214 | er.NewTradeExecution(order, quantity, price, "") 215 | 216 | if orac.IsCompleted(order) { 217 | UnRegisterOrder(order) 218 | OrderUpdateChan <- order 219 | } 220 | } 221 | 222 | func (m *OrderBook) ConstrucOrderBookLinkedList() (*QuoteBand, *QuoteBand) { 223 | var bidQuotes *QuoteBand 224 | if m.Quote.Bid != nil && m.Quote.BidSize != nil { 225 | quo := &QuoteBand{ 226 | price: *m.Quote.Bid, 227 | quantity: float64(*m.Quote.BidSize), 228 | } 229 | bidQuotes = quo 230 | } 231 | if m.Quote.Bid1 != nil && m.Quote.Bid1Size != nil { 232 | quo := &QuoteBand{ 233 | price: *m.Quote.Bid1, 234 | quantity: float64(*m.Quote.Bid1Size), 235 | } 236 | bidQuotes = quo 237 | } 238 | if m.Quote.Bid2 != nil && m.Quote.Bid2Size != nil { 239 | quo := &QuoteBand{ 240 | price: *m.Quote.Bid2, 241 | quantity: float64(*m.Quote.Bid2Size), 242 | } 243 | if bidQuotes == nil { 244 | bidQuotes = quo 245 | } else { 246 | bidQuotes.next = quo 247 | } 248 | } 249 | if m.Quote.Bid3 != nil && m.Quote.Bid3Size != nil { 250 | quo := &QuoteBand{ 251 | price: *m.Quote.Bid3, 252 | quantity: float64(*m.Quote.Bid3Size), 253 | } 254 | if bidQuotes == nil { 255 | bidQuotes = quo 256 | } else { 257 | bidQuotes.next = quo 258 | } 259 | } 260 | if m.Quote.Bid4 != nil && m.Quote.Bid4Size != nil { 261 | quo := &QuoteBand{ 262 | price: *m.Quote.Bid4, 263 | quantity: float64(*m.Quote.Bid4Size), 264 | } 265 | if bidQuotes == nil { 266 | bidQuotes = quo 267 | } else { 268 | bidQuotes.next = quo 269 | } 270 | } 271 | if m.Quote.Bid5 != nil && m.Quote.Bid5Size != nil { 272 | quo := &QuoteBand{ 273 | price: *m.Quote.Bid5, 274 | quantity: float64(*m.Quote.Bid5Size), 275 | } 276 | if bidQuotes == nil { 277 | bidQuotes = quo 278 | } else { 279 | bidQuotes.next = quo 280 | } 281 | } 282 | 283 | var askQuotes *QuoteBand 284 | if m.Quote.Ask != nil && m.Quote.AskSize != nil { 285 | quo := &QuoteBand{ 286 | price: *m.Quote.Ask, 287 | quantity: float64(*m.Quote.AskSize), 288 | } 289 | askQuotes = quo 290 | } 291 | if m.Quote.Ask1 != nil && m.Quote.Ask1Size != nil { 292 | quo := &QuoteBand{ 293 | price: *m.Quote.Ask1, 294 | quantity: float64(*m.Quote.Ask1Size), 295 | } 296 | askQuotes = quo 297 | } 298 | if m.Quote.Ask2 != nil && m.Quote.Ask2Size != nil { 299 | quo := &QuoteBand{ 300 | price: *m.Quote.Ask2, 301 | quantity: float64(*m.Quote.Ask2Size), 302 | } 303 | if askQuotes == nil { 304 | askQuotes = quo 305 | } else { 306 | askQuotes.next = quo 307 | } 308 | } 309 | if m.Quote.Ask3 != nil && m.Quote.Ask3Size != nil { 310 | quo := &QuoteBand{ 311 | price: *m.Quote.Ask3, 312 | quantity: float64(*m.Quote.Ask3Size), 313 | } 314 | if askQuotes == nil { 315 | askQuotes = quo 316 | } else { 317 | askQuotes.next = quo 318 | } 319 | } 320 | if m.Quote.Ask4 != nil && m.Quote.Ask4Size != nil { 321 | quo := &QuoteBand{ 322 | price: *m.Quote.Ask4, 323 | quantity: float64(*m.Quote.Ask4Size), 324 | } 325 | if askQuotes == nil { 326 | askQuotes = quo 327 | } else { 328 | askQuotes.next = quo 329 | } 330 | } 331 | if m.Quote.Ask5 != nil && m.Quote.Ask5Size != nil { 332 | quo := &QuoteBand{ 333 | price: *m.Quote.Ask5, 334 | quantity: float64(*m.Quote.Ask5Size), 335 | } 336 | if askQuotes == nil { 337 | askQuotes = quo 338 | } else { 339 | askQuotes.next = quo 340 | } 341 | } 342 | return bidQuotes, askQuotes 343 | } 344 | -------------------------------------------------------------------------------- /services/marketconnectors/sellsidesim/sellsidesim.go: -------------------------------------------------------------------------------- 1 | package sellsidesim 2 | 3 | import ( 4 | "github.com/quickfixgo/quickfix" 5 | 6 | "log" 7 | ) 8 | 9 | type SellSideSimulator struct { 10 | acceptor *quickfix.Acceptor 11 | } 12 | 13 | func NewSellSideSimulator(logger string) *SellSideSimulator { 14 | mc := &SellSideSimulator{} 15 | 16 | // QuickFIX settings 17 | appSettings := quickfix.NewSettings() 18 | var settings *quickfix.SessionSettings 19 | settings = appSettings.GlobalSettings() 20 | settings.Set("SocketAcceptPort", "5001") 21 | settings.Set("SenderCompID", "CORP") 22 | settings.Set("TargetCompID", "CYAN") 23 | settings.Set("ResetOnLogon", "Y") 24 | settings.Set("FileLogPath", "tmp") 25 | settings = quickfix.NewSessionSettings() 26 | settings.Set("BeginString", "FIX.4.4") 27 | appSettings.AddSession(settings) 28 | 29 | app := NewExecutor() 30 | 31 | var logFactory quickfix.LogFactory 32 | switch logger { 33 | case "console": 34 | logFactory = quickfix.NewScreenLogFactory() 35 | default: 36 | logFactory = quickfix.NewNullLogFactory() 37 | 38 | } 39 | 40 | acceptor, err := quickfix.NewAcceptor(app, quickfix.NewMemoryStoreFactory(), appSettings, logFactory) 41 | if err != nil { 42 | log.Panic(err) 43 | } 44 | 45 | mc.acceptor = acceptor 46 | 47 | return mc 48 | } 49 | 50 | func (m *SellSideSimulator) Start() { 51 | if err := m.acceptor.Start(); err != nil { 52 | log.Panic(err) 53 | } 54 | } 55 | 56 | func (m *SellSideSimulator) Close() { 57 | m.acceptor.Stop() 58 | } 59 | -------------------------------------------------------------------------------- /services/marketconnectors/sellsidesim/sellsidesim_test.go: -------------------------------------------------------------------------------- 1 | package sellsidesim 2 | 3 | import ( 4 | //"testing" 5 | //"time" 6 | ) 7 | 8 | //func TestNewMarketConnectorStartAndStop(t *testing.T) { 9 | // 10 | // svc := NewSellSideSimulator("") 11 | // svc.Start() 12 | // 13 | // time.Sleep(5000 * time.Millisecond) 14 | // 15 | // svc.Close() 16 | // 17 | // //if svc.msgbus.IsClosed() != true { 18 | // // t.Fatal("order router failed to shut down") 19 | // //} 20 | //} 21 | -------------------------------------------------------------------------------- /services/marketconnectors/simulator/simulator.go: -------------------------------------------------------------------------------- 1 | // MC Simulator communicates with SellSideSim service in FIX protocol as a mean to test trade life cycle 2 | package simulator 3 | 4 | import ( 5 | log "github.com/cyanly/gotrade/core/logger" 6 | proto "github.com/cyanly/gotrade/proto/order" 7 | "github.com/cyanly/gotrade/services/marketconnectors/common" 8 | "github.com/cyanly/gotrade/services/marketconnectors/common/order" 9 | ) 10 | 11 | const ( 12 | MarketConnectorName string = "Simulator" 13 | ) 14 | 15 | type MCSimulator struct { 16 | Config common.Config 17 | OrdersList map[int32]*proto.Order 18 | 19 | app *order.FIXClient 20 | } 21 | 22 | func NewMarketConnector(c common.Config) *MCSimulator { 23 | c.MarketConnectorName = MarketConnectorName 24 | 25 | mc := &MCSimulator{ 26 | Config: c, 27 | } 28 | 29 | // Simulator is an standard Order only FIX app 30 | mc.app = order.NewFIXClient(c) 31 | 32 | return mc 33 | } 34 | 35 | func (m *MCSimulator) Start() { 36 | if err := m.app.Start(); err != nil { 37 | log.WithError(err).Fatal("FIX Client Start Error") 38 | } 39 | } 40 | 41 | func (m *MCSimulator) Close() { 42 | m.app.Stop() 43 | } 44 | 45 | func (m *MCSimulator) Name() string { 46 | return MarketConnectorName 47 | } 48 | -------------------------------------------------------------------------------- /services/marketconnectors/simulator/simulator_test.go: -------------------------------------------------------------------------------- 1 | package simulator 2 | 3 | import ( 4 | messagebus "github.com/nats-io/nats" 5 | "github.com/cyanly/gotrade/core/messagebus/test" 6 | _ "github.com/cyanly/gotrade/database/memstore" 7 | proto "github.com/cyanly/gotrade/proto/order" 8 | "github.com/cyanly/gotrade/services/marketconnectors/common" 9 | "github.com/cyanly/gotrade/services/marketconnectors/sellsidesim" 10 | testOrder "github.com/cyanly/gotrade/test/order" 11 | 12 | "os" 13 | "testing" 14 | "time" 15 | ) 16 | 17 | func TestMain(m *testing.M) { 18 | //mock message bus 19 | ts := test.RunDefaultServer() 20 | defer ts.Shutdown() 21 | 22 | //simulate a sell side FIX server 23 | sellsvc := sellsidesim.NewSellSideSimulator("") 24 | sellsvc.Start() 25 | defer sellsvc.Close() 26 | 27 | code := m.Run() 28 | os.Exit(code) 29 | } 30 | 31 | func TestNewMarketConnectorStartAndStop(t *testing.T) { 32 | //config 33 | sc := common.NewConfig() 34 | sc.MessageBusURL = "nats://localhost:22222" 35 | sc.DatabaseDriver = "memstore" 36 | 37 | //start MC 38 | svc := NewMarketConnector(sc) 39 | svc.Start() 40 | 41 | //stop and disconnect MC 42 | time.Sleep(100 * time.Millisecond) 43 | svc.Close() 44 | 45 | //expect no panic 46 | } 47 | 48 | func TestNewMarketConnectorNewOrderRequest(t *testing.T) { 49 | //config 50 | sc := common.NewConfig() 51 | sc.MessageBusURL = "nats://localhost:22222" 52 | sc.DatabaseDriver = "memstore" 53 | 54 | //start MC 55 | svc := NewMarketConnector(sc) 56 | svc.Start() 57 | 58 | time.Sleep(100 * time.Millisecond) 59 | 60 | // mock a NewOrderRequest, really should come from OrderRouter 61 | request := &proto.NewOrderRequest{ 62 | Order: testOrder.MockOrder(), 63 | } 64 | request.Order.OrderStatus = proto.OrderStatus_ORDER_RECEIVED 65 | request.Order.SubmitDatetime = time.Now().UTC().Format(time.RFC3339Nano) 66 | request.Order.MessageType = proto.Order_NEW 67 | request.Order.OrderKey = int32(321) 68 | svc.app.OrderStore.OrderCreate(request.Order) 69 | 70 | // subscribe to check if we can receive a fill execution report from sim broker 71 | recvExecutionReport := false 72 | svc.app.MessageBus.Subscribe("order.Execution", func(m *messagebus.Msg) { 73 | recvExecutionReport = true 74 | exec := new(proto.Execution) 75 | if err := exec.Unmarshal(m.Data); err == nil { 76 | if exec.ClientOrderId != "321.1" { 77 | t.Fatalf("unexpected execution report ClOrdId %v, expecting 321.1", exec.ClientOrderId) 78 | } 79 | if exec.OrderId != 1 { 80 | t.Fatalf("unexpected execution report OrderId %v, expecting 1", exec.OrderId) 81 | } 82 | } else { 83 | t.Fatalf("unexpected execution report: %v", err) 84 | } 85 | }) 86 | 87 | // send above mock NewOrderRequest 88 | data, _ := request.Marshal() 89 | svc.app.MessageBus.Publish("order.NewOrderRequest.MC."+MarketConnectorName, data) 90 | 91 | time.Sleep(100 * time.Millisecond) 92 | 93 | //stop and disconnect MC 94 | svc.Close() 95 | 96 | if recvExecutionReport == false { 97 | t.Fatal("No execution report received from mock order new request") 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /services/marketconnectors/util.go: -------------------------------------------------------------------------------- 1 | package marketconnectors 2 | 3 | var protoEnumToFIXEnum = map[int]string{ 4 | 0: "0", 5 | 1: "1", 6 | 2: "2", 7 | 3: "3", 8 | 4: "4", 9 | 5: "5", 10 | 6: "6", 11 | 7: "7", 12 | 8: "8", 13 | 9: "9", 14 | 10: "A", 15 | 11: "B", 16 | 12: "C", 17 | 13: "D", 18 | 14: "E", 19 | 15: "F", 20 | 16: "G", 21 | 17: "H", 22 | 18: "I", 23 | 19: "J", 24 | 20: "K", 25 | 21: "L", 26 | 22: "M", 27 | 23: "N", 28 | 24: "O", 29 | 25: "P", 30 | 26: "Q", 31 | } 32 | var fixEnumToProtoEnum = map[string]int{} 33 | 34 | func init() { 35 | for k, v := range protoEnumToFIXEnum { 36 | fixEnumToProtoEnum[v] = k 37 | } 38 | } 39 | 40 | func ProtoEnumToFIXEnum(pbe int) string { 41 | return protoEnumToFIXEnum[pbe] 42 | } 43 | func FIXEnumToProtoEnum(fbe string) int { 44 | return fixEnumToProtoEnum[fbe] 45 | } 46 | -------------------------------------------------------------------------------- /services/orderrouter/config.go: -------------------------------------------------------------------------------- 1 | package orderrouter 2 | 3 | import "github.com/cyanly/gotrade/core/messagebus" 4 | 5 | const ( 6 | // DefaultMessageBusURL is the default MessageBus URL for service communications. 7 | DefaultMessageBusURL = messagebus.DefaultUrl 8 | DefaultServiceMessageBusURL = messagebus.DefaultUrl 9 | ) 10 | 11 | // Config represents the configuration for base service. 12 | type Config struct { 13 | MessageBusURL string `json:"or_message_bus"` //trade and order message bus URL 14 | ServiceMessageBusURL string `json:"service_message_bus"` //services message bus URL, for listening to MC heartbeats 15 | DatabaseDriver string `json:"database_driver"` //database storage engine driver 16 | DatabaseUrl string `json:"database_url"` //database connection string 17 | } 18 | 19 | // NewConfig returns an instance of Config with defaults. 20 | func NewConfig() Config { 21 | return Config{ 22 | MessageBusURL: DefaultMessageBusURL, 23 | ServiceMessageBusURL: DefaultServiceMessageBusURL, 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /services/orderrouter/orderrouter.go: -------------------------------------------------------------------------------- 1 | // Serving order New/Cancel/Replace tasks, communicates with various market connectors 2 | package orderrouter 3 | 4 | import ( 5 | messagebus "github.com/nats-io/nats" 6 | log "github.com/cyanly/gotrade/core/logger" 7 | "github.com/cyanly/gotrade/core/order" 8 | "github.com/cyanly/gotrade/database" 9 | proto "github.com/cyanly/gotrade/proto/order" 10 | "github.com/cyanly/gotrade/proto/service" 11 | 12 | "fmt" 13 | "strings" 14 | "time" 15 | ) 16 | 17 | type OrderRouter struct { 18 | Config Config 19 | 20 | orderStore database.OrderStore // order storage driver 21 | stopChan chan bool // signal stop order router processor channel 22 | msgbusService *messagebus.Conn // message bus listening to service heartbeats 23 | msgbus *messagebus.Conn // message bus listenning to order 24 | mclist map[string]time.Time // list of available market connectors 25 | mcChan chan *service.Heartbeat // channel updates market connector heartbeats 26 | reqChan chan OrderRequest // channel for order requests 27 | } 28 | 29 | type ReqType int 30 | 31 | const ( 32 | REQ_NEW ReqType = iota 33 | REQ_CANCEL 34 | REQ_REPLACE 35 | ) 36 | 37 | type OrderRequest struct { 38 | ReplyAddr string 39 | RequestType ReqType 40 | Request interface{} 41 | OrderId int32 42 | } 43 | 44 | // Initialise OrderRouter instance and set up topic subscribers 45 | func NewOrderRouter(c Config) *OrderRouter { 46 | 47 | or := &OrderRouter{ 48 | Config: c, 49 | 50 | // internal variables 51 | stopChan: make(chan bool), 52 | mclist: map[string]time.Time{}, 53 | mcChan: make(chan *service.Heartbeat), 54 | reqChan: make(chan OrderRequest), 55 | } 56 | 57 | // Connect to database storage driver 58 | if storage, err := database.NewOrderStore(c.DatabaseDriver, c.DatabaseUrl, nil); err != nil { 59 | log.Fatalf("error: Cannot connect to database driver %v @ %v", c.DatabaseDriver, c.DatabaseUrl) 60 | } else { 61 | or.orderStore = storage 62 | } 63 | 64 | // Keep a list of active MarketConnectors 65 | if ncSvc, err := messagebus.Connect(c.ServiceMessageBusURL); err != nil { 66 | log.Fatalf("error: Cannot connect to service message bus @ %v", c.ServiceMessageBusURL) 67 | } else { 68 | or.msgbusService = ncSvc 69 | ncSvc.Subscribe("service.Heartbeat.MC.>", func(m *messagebus.Msg) { 70 | hbMsg := &service.Heartbeat{} 71 | if err := hbMsg.Unmarshal(m.Data); err == nil { 72 | or.mcChan <- hbMsg 73 | } 74 | }) 75 | } 76 | 77 | // Order requests processors subscribing 78 | if msgbus, err := messagebus.Connect(c.MessageBusURL); err != nil { 79 | log.Fatalf("error: Cannot connect to order message bus @ %v", c.MessageBusURL) 80 | } else { 81 | or.msgbus = msgbus 82 | 83 | //CL->OR order NEW request 84 | msgbus.Subscribe("order.NewOrderRequest", func(m *messagebus.Msg) { 85 | request := new(proto.NewOrderRequest) 86 | if err := request.Unmarshal(m.Data); err == nil && len(m.Reply) > 0 { 87 | // validate basic fields 88 | if request.Order == nil { 89 | // empty request 90 | return 91 | } 92 | if request.Order.MarketConnector == "" { 93 | // empty market connect 94 | return 95 | } 96 | 97 | orReq := OrderRequest{ 98 | ReplyAddr: m.Reply, 99 | RequestType: REQ_NEW, 100 | Request: request, 101 | } 102 | oO := order.Order{ 103 | Order: request.Order, 104 | } 105 | log.Infof("CL->OR NEW %v", oO.String()) 106 | or.reqChan <- orReq 107 | } 108 | }) 109 | 110 | //CL->OR order CANCEL request 111 | msgbus.Subscribe("order.CancelOrderRequest", func(m *messagebus.Msg) { 112 | request := new(proto.CancelOrderRequest) 113 | if err := request.Unmarshal(m.Data); err == nil && len(m.Reply) > 0 { 114 | // validate basic fields 115 | if request.OrderId == 0 { 116 | // request without order id 117 | return 118 | } 119 | 120 | orReq := OrderRequest{ 121 | ReplyAddr: m.Reply, 122 | RequestType: REQ_CANCEL, 123 | Request: request, 124 | OrderId: request.OrderId, 125 | } 126 | 127 | log.Infof("CL->OR CXL OrderKey := %v OrderId := %v", request.OrderKey, request.OrderId) 128 | or.reqChan <- orReq 129 | } 130 | }) 131 | 132 | //CL->OR order REPLACE request 133 | msgbus.Subscribe("order.ReplaceOrderRequest", func(m *messagebus.Msg) { 134 | request := new(proto.ReplaceOrderRequest) 135 | if err := request.Unmarshal(m.Data); err == nil && len(m.Reply) > 0 { 136 | // validate basic fields 137 | if request.Order == nil { 138 | // empty request 139 | return 140 | } 141 | if request.Order.OrderId == 0 { 142 | // request without order id 143 | return 144 | } 145 | if request.Order.MarketConnector == "" { 146 | // empty market connect 147 | return 148 | } 149 | 150 | orReq := OrderRequest{ 151 | ReplyAddr: m.Reply, 152 | RequestType: REQ_REPLACE, 153 | Request: request, 154 | OrderId: request.Order.OrderId, 155 | } 156 | 157 | log.Infof("CL->OR RPL OrderKey := %v OrderId := %v", request.Order.OrderKey, request.Order.OrderId) 158 | or.reqChan <- orReq 159 | 160 | } 161 | }) 162 | } 163 | 164 | return or 165 | } 166 | 167 | // start the logic spinning code for OrderRouter, using a single for..select.. pattern so there is no need to lock resources 168 | // hence to avoid synchronisation issues 169 | func (self *OrderRouter) Start() { 170 | go func() { 171 | for { 172 | select { 173 | case <-self.stopChan: 174 | self.msgbus.Close() 175 | self.msgbusService.Close() 176 | return 177 | 178 | // update known market connector list based on their heartbeat status 179 | case hbMsg := <-self.mcChan: 180 | svcName := strings.Replace(hbMsg.Name, "MC.", "", 1) 181 | if hbMsg.Status == service.RUNNING { 182 | self.mclist[svcName] = time.Now() 183 | } else { 184 | delete(self.mclist, svcName) 185 | } 186 | // remove market connector if we have not heard from it for a while 187 | case <-time.After(6 * time.Second): 188 | for name, lastHb := range self.mclist { 189 | if time.Since(lastHb).Seconds() > 6 { 190 | delete(self.mclist, name) 191 | } 192 | } 193 | case req := <-self.reqChan: 194 | func() { // wrap in func for defer reply 195 | 196 | requestError := "" 197 | var order *proto.Order 198 | 199 | // make sure reply message sent in the end 200 | defer func() { 201 | var data []byte 202 | errCode := int32(0) 203 | if requestError != "" { 204 | errCode = int32(-1) 205 | } 206 | switch req.RequestType { 207 | case REQ_NEW: 208 | resp := &proto.NewOrderResponse{ 209 | ErrorCode: errCode, 210 | ErrorMessage: &requestError, 211 | Order: order, 212 | } 213 | data, _ = resp.Marshal() 214 | case REQ_CANCEL: 215 | resp := &proto.CancelOrderResponse{ 216 | ErrorCode: errCode, 217 | ErrorMessage: &requestError, 218 | Order: order, 219 | } 220 | data, _ = resp.Marshal() 221 | case REQ_REPLACE: 222 | resp := &proto.ReplaceOrderResponse{ 223 | ErrorCode: errCode, 224 | ErrorMessage: &requestError, 225 | Order: order, 226 | } 227 | data, _ = resp.Marshal() 228 | } 229 | self.msgbus.Publish(req.ReplyAddr, data) 230 | }() 231 | 232 | // Prepare order 233 | if req.RequestType == REQ_CANCEL || req.RequestType == REQ_REPLACE { 234 | // retrieve previous order for state check 235 | if prev_order, err := self.orderStore.OrderGet(req.OrderId); err != nil { 236 | requestError = "Order does not exists" 237 | return 238 | } else { 239 | // rule.1: working orders 240 | if prev_order.MessageType == proto.Order_NEW { 241 | if !(prev_order.OrderStatus == proto.OrderStatus_PARTIALLY_FILLED || 242 | prev_order.OrderStatus == proto.OrderStatus_NEW) { 243 | requestError = fmt.Sprintf("Disallowed due to order status: %s", prev_order.OrderStatus) 244 | return 245 | } 246 | } 247 | // rule.2: pending cancel rejected 248 | if prev_order.MessageType == proto.Order_CANCEL { 249 | if !(prev_order.OrderStatus == proto.OrderStatus_REJECTED) { 250 | requestError = "Pending cancel on order" 251 | return 252 | } 253 | } 254 | // rule.2: pending replace acked by broker 255 | if prev_order.MessageType == proto.Order_REPLACE { 256 | if !(prev_order.OrderStatus == proto.OrderStatus_NEW || 257 | prev_order.OrderStatus == proto.OrderStatus_REPLACED || 258 | prev_order.OrderStatus == proto.OrderStatus_REJECTED) { 259 | requestError = "Pending replace on order" 260 | return 261 | } 262 | } 263 | 264 | // prepare for new order entry 265 | if req.RequestType == REQ_CANCEL { 266 | pReq := req.Request.(*proto.CancelOrderRequest) 267 | order = prev_order 268 | order.OrderId = 0 // wipe order id for new id 269 | order.Version++ // bump up order key version 270 | order.SubmitDatetime = time.Now().UTC().Format(time.RFC3339Nano) // timing this cancel 271 | order.Trader = pReq.Trader // trader who tries to cancel 272 | order.TraderId = pReq.TraderId // trader who tries to cancel 273 | order.Source = pReq.Source // source of cancel 274 | order.MessageType = proto.Order_CANCEL 275 | order.OrderStatus = proto.OrderStatus_CANCEL_RECEIVED 276 | } 277 | if req.RequestType == REQ_REPLACE { 278 | pReq := req.Request.(*proto.ReplaceOrderRequest) 279 | pReq.Order.OrderKey = prev_order.OrderKey 280 | 281 | order := pReq.Order 282 | order.OrderId = 0 // wipe order id for new id 283 | order.OrderKey = prev_order.OrderKey // in case client failed to provide correct order key 284 | order.Version = prev_order.Version + 1 // bump up order key version 285 | order.SubmitDatetime = time.Now().UTC().Format(time.RFC3339Nano) // timing this replace 286 | order.Trader = pReq.Trader // trader who tries to replace 287 | order.TraderId = pReq.TraderId // trader who tries to replace 288 | order.MessageType = proto.Order_REPLACE 289 | order.OrderStatus = proto.OrderStatus_REPLACE_RECEIVED 290 | } 291 | 292 | //TODO: check ClientGUID consistency 293 | } 294 | } else { 295 | // New order 296 | pReq := req.Request.(*proto.NewOrderRequest) 297 | order = pReq.Order 298 | order.OrderId = 0 299 | order.Version = 0 300 | } 301 | 302 | // Persist order entry 303 | if err := self.orderStore.OrderCreate(order); err != nil { 304 | requestError = fmt.Sprint(err) 305 | log.WithError(err).Error("[ OR ] ERROR Create order") 306 | return 307 | } 308 | 309 | //TODO: allocations 310 | 311 | // Check target market connector is up 312 | if _, ok := self.mclist[order.MarketConnector]; ok == false { 313 | log.Warnf("OR->CL REJECT OrderKey: %v MC: %v : %v", order.OrderKey, order.MarketConnector, "LINK TO BROKER DOWN") 314 | 315 | // REJECT due to market connector down 316 | requestError = "LINK TO BROKER DOWN" 317 | 318 | // create Reject execution 319 | self.reject(order, "LINK TO BROKER DOWN") 320 | 321 | } else { 322 | log.WithField("order", order).Infof("OR->MC OrderKey: %v", order.OrderKey) 323 | 324 | // relay order with idents to its market connector 325 | // todo: special case on MC.AlgoAggregator 326 | var data []byte 327 | switch req.RequestType { 328 | case REQ_NEW: 329 | request := req.Request.(*proto.NewOrderRequest) 330 | data, _ = request.Marshal() 331 | case REQ_CANCEL: 332 | request := req.Request.(*proto.CancelOrderRequest) 333 | data, _ = request.Marshal() 334 | case REQ_REPLACE: 335 | request := req.Request.(*proto.ReplaceOrderRequest) 336 | data, _ = request.Marshal() 337 | } 338 | self.msgbus.Publish("order.NewOrderRequest.MC."+order.MarketConnector, data) 339 | } 340 | }() 341 | } 342 | } 343 | }() 344 | } 345 | 346 | func (self *OrderRouter) Close() { 347 | self.stopChan <- true 348 | } 349 | 350 | func (self *OrderRouter) reject(o *proto.Order, reason string) { 351 | 352 | execution := &proto.Execution{ 353 | OrderId: o.OrderId, 354 | OrderKey: o.OrderKey, 355 | ClientOrderId: fmt.Sprintf("%v.%v", o.OrderKey, o.Version), 356 | ExecType: proto.Execution_REJECTED, 357 | OrderStatus: proto.OrderStatus_REJECTED, 358 | BrokerExecDatetime: time.Now().UTC().Format(time.RFC3339Nano), 359 | Text: reason, 360 | } 361 | 362 | if err := self.orderStore.ExecutionCreate(execution); err == nil { 363 | //publish to message bus 364 | data, _ := execution.Marshal() 365 | self.msgbus.Publish("order.Execution", data) 366 | } else { 367 | log.WithField("execution", execution).WithError(err).Error("[ OR ] ERROR Create Reject Execution") 368 | } 369 | 370 | } 371 | -------------------------------------------------------------------------------- /services/orderrouter/orderrouter_test.go: -------------------------------------------------------------------------------- 1 | package orderrouter 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | "time" 7 | 8 | "github.com/cyanly/gotrade/core/messagebus/test" 9 | _ "github.com/cyanly/gotrade/database/memstore" 10 | proto "github.com/cyanly/gotrade/proto/order" 11 | pService "github.com/cyanly/gotrade/proto/service" 12 | testOrder "github.com/cyanly/gotrade/test/order" 13 | ) 14 | 15 | func TestMain(m *testing.M) { 16 | //mock message bus 17 | ts := test.RunDefaultServer() 18 | defer ts.Shutdown() 19 | 20 | code := m.Run() 21 | os.Exit(code) 22 | } 23 | 24 | func TestOrderRouterStartAndStop(t *testing.T) { 25 | 26 | sc := NewConfig() 27 | sc.MessageBusURL = "nats://localhost:22222" 28 | sc.ServiceMessageBusURL = "nats://localhost:22222" 29 | sc.DatabaseDriver = "memstore" 30 | 31 | svc := NewOrderRouter(sc) 32 | svc.Start() 33 | 34 | svc.Close() 35 | time.Sleep(200 * time.Millisecond) 36 | 37 | if svc.msgbus.IsClosed() != true { 38 | t.Fatal("order router failed to shut down") 39 | } 40 | } 41 | 42 | func TestOrderRouterNewOrderRequest(t *testing.T) { 43 | 44 | svc := mockOrderRouter() 45 | svc.Start() 46 | defer svc.Close() 47 | 48 | // mock new order request 49 | request := &proto.NewOrderRequest{ 50 | Order: testOrder.MockOrder(), 51 | } 52 | data, merr := request.Marshal() 53 | if merr != nil { 54 | t.Fatal(merr) 55 | } 56 | 57 | // send mock order, expecting reject due to market connector not up 58 | req := new(proto.NewOrderRequest) 59 | if err := req.Unmarshal(data); err != nil { 60 | t.Fatal(err) 61 | } 62 | if reply, err := svc.msgbus.Request("order.NewOrderRequest", data, 200*time.Millisecond); err != nil { 63 | t.Fatalf("an error '%s' was not expected when sending NewOrderRequest", err) 64 | } else { 65 | resp := new(proto.NewOrderResponse) 66 | if err := resp.Unmarshal(reply.Data); err != nil { 67 | t.Fatalf("an error '%s' was not expected when decoding NewOrderResponse", err) 68 | } 69 | 70 | if *resp.ErrorMessage != "LINK TO BROKER DOWN" { 71 | t.Fatalf("unexpected NewOrderResponse %s, expecting LINK TO BROKER DOWN", *resp.ErrorMessage) 72 | } 73 | } 74 | 75 | // mock a test market connector 76 | mcname := "MC.TestCase" 77 | hbMsg := pService.Heartbeat{ 78 | Name: mcname, 79 | Status: pService.RUNNING, 80 | } 81 | if hbMsgData, err := hbMsg.Marshal(); err != nil { 82 | t.Fatal(err) 83 | } else { 84 | svc.msgbusService.Publish("service.Heartbeat.MC.TestCase", hbMsgData) 85 | 86 | time.Sleep(100 * time.Millisecond) 87 | 88 | if reply, err := svc.msgbus.Request("order.NewOrderRequest", data, 200*time.Millisecond); err != nil { 89 | t.Fatalf("an error '%s' was not expected when sending NewOrderRequest", err) 90 | } else { 91 | resp := new(proto.NewOrderResponse) 92 | if err := resp.Unmarshal(reply.Data); err != nil { 93 | t.Fatalf("an error '%s' was not expected when decoding NewOrderResponse", err) 94 | } 95 | 96 | if resp.ErrorMessage != nil && len(*resp.ErrorMessage) > 0 { 97 | t.Fatalf("unexpected NewOrderResponse error message: %s", *resp.ErrorMessage) 98 | } 99 | 100 | if resp.Order == nil || resp.Order.OrderId != 2 { 101 | t.Fatalf("unexpected NewOrderResponse id not matching mock OrderId(%v), expecting 2") 102 | } 103 | 104 | if resp.Order == nil || resp.Order.OrderKey != 2 { 105 | t.Fatalf("unexpected NewOrderResponse OrderKey %v, expecting 2", resp.Order.OrderKey) 106 | } 107 | } 108 | } 109 | } 110 | 111 | func mockOrderRouter() *OrderRouter { 112 | 113 | sc := NewConfig() 114 | sc.MessageBusURL = "nats://localhost:22222" 115 | sc.ServiceMessageBusURL = "nats://localhost:22222" 116 | sc.DatabaseDriver = "memstore" 117 | 118 | svc := NewOrderRouter(sc) 119 | 120 | return svc 121 | } 122 | -------------------------------------------------------------------------------- /test/benchmark/client2fix_test.go: -------------------------------------------------------------------------------- 1 | package benchmark 2 | 3 | import ( 4 | "github.com/nats-io/nats" 5 | logger "github.com/cyanly/gotrade/core/logger" 6 | "github.com/cyanly/gotrade/core/messagebus/test" 7 | "github.com/cyanly/gotrade/core/service" 8 | _ "github.com/cyanly/gotrade/database/memstore" 9 | proto "github.com/cyanly/gotrade/proto/order" 10 | MCCommon "github.com/cyanly/gotrade/services/marketconnectors/common" 11 | "github.com/cyanly/gotrade/services/marketconnectors/sellsidesim" 12 | MCSimulator "github.com/cyanly/gotrade/services/marketconnectors/simulator" 13 | "github.com/cyanly/gotrade/services/orderrouter" 14 | testOrder "github.com/cyanly/gotrade/test/order" 15 | 16 | "log" 17 | "os" 18 | "testing" 19 | "time" 20 | ) 21 | 22 | var ( 23 | msgbus *nats.Conn 24 | ) 25 | 26 | func TestMain(m *testing.M) { 27 | // Start a temporary messaging broker 28 | ts := test.RunDefaultServer() 29 | defer ts.Shutdown() 30 | 31 | // Start OrderRouter service 32 | orSC := orderrouter.NewConfig() 33 | orSC.MessageBusURL = "nats://localhost:22222" 34 | orSC.ServiceMessageBusURL = "nats://localhost:22222" 35 | orSC.DatabaseDriver = "memstore" 36 | orSvc := orderrouter.NewOrderRouter(orSC) 37 | orSvc.Start() 38 | defer orSvc.Close() 39 | 40 | // Start Simulated FIX Sell Side 41 | sellsvc := sellsidesim.NewSellSideSimulator("") 42 | sellsvc.Start() 43 | defer sellsvc.Close() 44 | 45 | // Start a MarketConnector Service for simuilated market 46 | // 1. MC (implementation) 47 | mcSC := MCCommon.NewConfig() 48 | mcSC.MessageBusURL = "nats://localhost:22222" 49 | mcSC.DatabaseDriver = "memstore" 50 | mcSvc := MCSimulator.NewMarketConnector(mcSC) 51 | mcSvc.Start() 52 | defer mcSvc.Close() 53 | // 2. Service (heartbeating etc) 54 | sc := service.NewConfig() 55 | sc.MessageBusURL = "nats://localhost:22222" 56 | sc.ServiceName = "MC." + mcSvc.Name() 57 | svc := service.NewService(sc) 58 | svc.Start() 59 | defer svc.Stop() 60 | 61 | // Turn off logging to measure performance 62 | logger.Discard() 63 | 64 | time.Sleep(100 * time.Millisecond) // async 65 | 66 | // Connect messaging bus for this mock client 67 | if nc, err := nats.Connect("nats://localhost:22222"); err != nil { 68 | log.Fatal("error: Cannot connect to message bus") 69 | } else { 70 | msgbus = nc 71 | } 72 | 73 | code := m.Run() 74 | os.Exit(code) 75 | } 76 | 77 | func BenchmarkTradeflow(b *testing.B) { 78 | b.StopTimer() 79 | 80 | // Mock new order request 81 | request := &proto.NewOrderRequest{ 82 | Order: testOrder.MockOrder(), 83 | } 84 | request.Order.MarketConnector = "Simulator" 85 | data, merr := request.Marshal() 86 | if merr != nil { 87 | b.Fatal(merr) 88 | } 89 | 90 | //////// 91 | // benchmark the whole journey of: 92 | // CL->OR: Client send order protobuf to OrderRouter(OR) 93 | // OR->MC: OrderRouter process order and dispatch persisted order entity or target MarketConnector 94 | // MC->FIX: MarketConnector translate into NewOrderSingle FIX message based on the session with its counterparty 95 | // FIX->MC: MarketConnector received FIX message on its order, here Simulator sending a fully FILL execution 96 | // EXEC->CL: MarketConnector publish processed and persisted Execution onto messaging bus, here our Client will listen to 97 | /////// 98 | log.Println("benchmark count: ", b.N) 99 | b.StartTimer() 100 | for i := 0; i < b.N; i++ { 101 | if reply, err := msgbus.Request("order.NewOrderRequest", data, 2000*time.Millisecond); err != nil { 102 | b.Fatalf("an error '%s' was not expected when sending NewOrderRequest", err) 103 | } else { 104 | resp := new(proto.NewOrderResponse) 105 | if err := resp.Unmarshal(reply.Data); err != nil { 106 | b.Fatalf("an error '%s' was not expected when decoding NewOrderResponse", err) 107 | } 108 | 109 | if resp.ErrorMessage != nil && len(*resp.ErrorMessage) > 0 { 110 | b.Fatalf("unexpected NewOrderResponse error message: %s", *resp.ErrorMessage) 111 | } 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /test/order/mockorder.go: -------------------------------------------------------------------------------- 1 | package order 2 | 3 | import ( 4 | proto "github.com/cyanly/gotrade/proto/order" 5 | 6 | "encoding/json" 7 | "log" 8 | "time" 9 | ) 10 | 11 | func MockOrder() *proto.Order { 12 | var o proto.Order 13 | if err := json.Unmarshal([]byte(`{ 14 | "order_id": 0, 15 | "order_key": 0, 16 | "version": 0, 17 | "client_guid": "1234-5678-abcd-3210", 18 | "instruction": "NEW", 19 | "side": "BUY", 20 | "quantity": 100, 21 | "symbol": "AAPL", 22 | "order_type": "LIMIT", 23 | "limit_price": 100, 24 | "timeinforce": "DAY", 25 | "handle_inst": "AUTOMATED_EXECUTION_ORDER_PRIVATE", 26 | "exchange": "CME", 27 | "description": "Apple Inc.", 28 | "account_id": 1, 29 | "market_connector": "TestCase", 30 | "source": "mock test", 31 | "trader": "trader1", 32 | "trader_id": 1, 33 | "machine": "CYAN", 34 | "memo": "Super Secret", 35 | "broker_account": "billions_club", 36 | "create_datetime": "`+time.Now().UTC().Format(time.RFC3339Nano)+`", 37 | "-": "###" 38 | }`), &o); err != nil { 39 | log.Fatal("MockOrder: ", err) 40 | } 41 | 42 | return &o 43 | } 44 | --------------------------------------------------------------------------------