├── LICENSE ├── README.md ├── q.bat ├── q ├── bitcoind.q ├── btex01.q ├── btex02.q ├── btex03.q ├── btex04.q ├── q.k___download from kx .txt ├── sethdb.q ├── tick │ ├── cfmd.q │ ├── csmd.q │ ├── r.q │ ├── sym.q │ ├── tick.q │ └── u.q ├── w32 │ ├── libeay32.dll │ ├── liblz4.dll │ ├── msvcp140.dll │ ├── q.exe___download from kx .txt │ ├── qctpmd.dll │ ├── qx.dll │ ├── ssleay32.dll │ ├── thostmduserapi_se.dll │ └── vcruntime140.dll └── w64 │ ├── libeay32.dll │ ├── liblz4.dll │ ├── msvcp140.dll │ ├── q.exe___download from kx .txt │ ├── qctpmd.dll │ ├── qx.dll │ ├── ssleay32.dll │ ├── thostmduserapi_se.dll │ └── vcruntime140.dll ├── q64.bat └── tick.bat /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 kdbcn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kdb 2 | kdb+中文教程例子代码 3 | -------------------------------------------------------------------------------- /q.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q.bat -------------------------------------------------------------------------------- /q/bitcoind.q: -------------------------------------------------------------------------------- 1 | / 2 | src: https://github.com/jlucid/qbitcoind 3 | 4 | Copyright (c) 2017 Jeremy Lucid 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the "Software"), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | \ 27 | 28 | 29 | \d .bitcoind 30 | 31 | 32 | /////////////////////////////////////////////////////////// 33 | // SECTION CONTAINING SETUP LOGIC PRIOR TO LISTED APIs 34 | // - Set username and password 35 | // - Change hostname 36 | // - Function to parse function input 37 | // - Function to perform post requests 38 | ////////////////////////////////////////////////////////// 39 | 40 | 41 | userpassEnabled:0b 42 | hostLookup:(enlist `bitcoind)!(enlist `$":http://127.0.0.1:8332/") 43 | passLookup:(enlist `bitcoind)!(enlist ""); 44 | 45 | 46 | initHost:{[hostAddr] 47 | @[`.bitcoind;`hostLookup;,;(!) . enlist@'(`bitcoind;hsym `$hostAddr)]; 48 | } 49 | 50 | 51 | initPass:{[username;password] 52 | hostPass:":" sv (username;password); 53 | @[`.bitcoind;`passLookup;,;(!) . enlist@'(`bitcoind;hostPass)]; 54 | @[`.bitcoind;`userpassEnabled;:;1b]; 55 | } 56 | 57 | 58 | defaultPayload:{ 59 | (!) . (`jsonrpc`id`method`params;("1.0";0;"";())) 60 | } 61 | 62 | 63 | parseArgs:{[args;requiredArgs;optionalArgs] 64 | if[(::)~first args;args:()]; 65 | if[(0~count requiredArgs) & (0~count optionalArgs) & (0(count requiredArgs)) & not 99h~type last args;-1"parseArgs: Optional arguments must be passed as a dictionary";:`error]; 69 | optionalInput:$[(count args)>(count requiredArgs);last args;()]; 70 | requiredInput,optionalInput 71 | } 72 | 73 | 74 | base64Encode:{[String] 75 | if[not count String;:""]; 76 | remainder:count[String]mod 3; 77 | pc:count p:(0x;0x0000;0x00)remainder; 78 | b:.Q.b6 2 sv/: 6 cut raze 0b vs/: String,p; 79 | first 76 cut(neg[pc] _ b),pc#"=" 80 | } 81 | 82 | 83 | request:{[body] 84 | hostName:hostLookup[`bitcoind]; 85 | credentials:base64Encode passLookup[`bitcoind]; 86 | header:"text/plain \r\n","Authorization: Basic ",credentials; 87 | out:@[.Q.hp[hostName;header;];.j.j body;{[err] -2 "Error: request: ",err;:.j.j (enlist `error)!(enlist err)}]; 88 | @[.j.k;;{[out;err] -2 "Error: ",err," .Q.hp returned: ",out;:(enlist `error)!(enlist out)}[out;]] out 89 | } 90 | 91 | 92 | ///////////////////////////////////////////////// 93 | // HTTP JSON_RPC CALLS 94 | ///////////////////////////////////////////////// 95 | 96 | abandontransaction:('[{[args] 97 | requiredArgs:`txid; 98 | optionalArgs:(); 99 | input:parseArgs[args;requiredArgs;optionalArgs]; 100 | if[`error~input;:()]; 101 | body:.bitcoind.defaultPayload[]; 102 | body[`method]:"abandontransaction"; 103 | body[`params]:input; 104 | .bitcoind.request[body] 105 | };enlist] 106 | ) 107 | 108 | 109 | abortrescan:('[{[args] 110 | requiredArgs:(); 111 | optionalArgs:(); 112 | input:parseArgs[args;requiredArgs;optionalArgs]; 113 | if[`error~input;:()]; 114 | body:.bitcoind.defaultPayload[]; 115 | body[`method]:"abortrescan"; 116 | body[`params]:input; 117 | .bitcoind.request[body] 118 | };enlist] 119 | ) 120 | 121 | 122 | addmultisigaddress:('[{[args] 123 | requiredArgs:`nrequired`keys; 124 | optionalArgs:`label`address_type; 125 | input:parseArgs[args;requiredArgs;optionalArgs]; 126 | if[`error~input;:()]; 127 | body:.bitcoind.defaultPayload[]; 128 | body[`method]:"addmultisigaddress"; 129 | body[`params]:input; 130 | .bitcoind.request[body] 131 | };enlist] 132 | ) 133 | 134 | 135 | addnode:('[{[args] 136 | requiredArgs:`node`command; 137 | optionalArgs:(); 138 | input:parseArgs[args;requiredArgs;optionalArgs]; 139 | if[`error~input;:()]; 140 | body:.bitcoind.defaultPayload[]; 141 | body[`method]:"addnode"; 142 | body[`params]:input; 143 | .bitcoind.request[body] 144 | };enlist] 145 | ) 146 | 147 | 148 | backupwallet:('[{[args] 149 | requiredArgs:`destination; 150 | optionalArgs:(); 151 | input:parseArgs[args;requiredArgs;optionalArgs]; 152 | if[`error~input;:()]; 153 | body:.bitcoind.defaultPayload[]; 154 | body[`method]:"backupwallet"; 155 | body[`params]:input; 156 | .bitcoind.request[body] 157 | };enlist] 158 | ) 159 | 160 | 161 | bumpfee:('[{[args] 162 | requiredArgs:`txid; 163 | optionalArgs:`options; 164 | input:parseArgs[args;requiredArgs;optionalArgs]; 165 | if[`error~input;:()]; 166 | body:.bitcoind.defaultPayload[]; 167 | body[`method]:"bumpfee"; 168 | body[`params]:input; 169 | .bitcoind.request[body] 170 | };enlist] 171 | ) 172 | 173 | 174 | clearbanned:('[{[args] 175 | requiredArgs:(); 176 | optionalArgs:(); 177 | input:parseArgs[args;requiredArgs;optionalArgs]; 178 | if[`error~input;:()]; 179 | body:.bitcoind.defaultPayload[]; 180 | body[`method]:"clearbanned"; 181 | body[`params]:input; 182 | .bitcoind.request[body] 183 | };enlist] 184 | ) 185 | 186 | 187 | createmultisig:('[{[args] 188 | requiredArgs:`nrequired`keys; 189 | optionalArgs:`address_type; 190 | input:parseArgs[args;requiredArgs;optionalArgs]; 191 | if[`error~input;:()]; 192 | body:.bitcoind.defaultPayload[]; 193 | body[`method]:"createmultisig"; 194 | body[`params]:input; 195 | .bitcoind.request[body] 196 | };enlist] 197 | ) 198 | 199 | 200 | createrawtransaction:('[{[args] 201 | requiredArgs:`inputs`outputs; 202 | optionalArgs:`locktime`replaceable; 203 | input:parseArgs[args;requiredArgs;optionalArgs]; 204 | if[`error~input;:()]; 205 | body:.bitcoind.defaultPayload[]; 206 | body[`method]:"createrawtransaction"; 207 | body[`params]:input; 208 | .bitcoind.request[body] 209 | };enlist] 210 | ) 211 | 212 | 213 | createwallet:('[{[args] 214 | requiredArgs:`wallet_name; 215 | optionalArgs:`disable_private_keys`blank; 216 | input:parseArgs[args;requiredArgs;optionalArgs]; 217 | if[`error~input;:()]; 218 | body:.bitcoind.defaultPayload[]; 219 | body[`method]:"createwallet"; 220 | body[`params]:input; 221 | .bitcoind.request[body] 222 | };enlist] 223 | ) 224 | 225 | 226 | decoderawtransaction:('[{[args] 227 | requiredArgs:`hexstring; 228 | optionalArgs:`iswitness; 229 | input:parseArgs[args;requiredArgs;optionalArgs]; 230 | if[`error~input;:()]; 231 | body:.bitcoind.defaultPayload[]; 232 | body[`method]:"decoderawtransaction"; 233 | body[`params]:input; 234 | .bitcoind.request[body] 235 | };enlist] 236 | ) 237 | 238 | 239 | decodescript:('[{[args] 240 | requiredArgs:`hexstring; 241 | optionalArgs:(); 242 | input:parseArgs[args;requiredArgs;optionalArgs]; 243 | if[`error~input;:()]; 244 | body:.bitcoind.defaultPayload[]; 245 | body[`method]:"decodescript"; 246 | body[`params]:input; 247 | .bitcoind.request[body] 248 | };enlist] 249 | ) 250 | 251 | 252 | deriveaddresses:('[{[args] 253 | requiredArgs:`descriptor; 254 | optionalArgs:`range; 255 | input:parseArgs[args;requiredArgs;optionalArgs]; 256 | if[`error~input;:()]; 257 | body:.bitcoind.defaultPayload[]; 258 | body[`method]:"deriveaddresses"; 259 | body[`params]:input; 260 | .bitcoind.request[body] 261 | };enlist] 262 | ) 263 | 264 | 265 | disconnectnode:('[{[args] 266 | requiredArgs:(); 267 | optionalArgs:`address`nodeid; 268 | input:parseArgs[args;requiredArgs;optionalArgs]; 269 | if[`error~input;:()]; 270 | body:.bitcoind.defaultPayload[]; 271 | body[`method]:"disconnectnode"; 272 | body[`params]:input; 273 | .bitcoind.request[body] 274 | };enlist] 275 | ) 276 | 277 | 278 | dumpprivkey:('[{[args] 279 | requiredArgs:`address; 280 | optionalArgs:(); 281 | input:parseArgs[args;requiredArgs;optionalArgs]; 282 | if[`error~input;:()]; 283 | body:.bitcoind.defaultPayload[]; 284 | body[`method]:"dumpprivkey"; 285 | body[`params]:input; 286 | .bitcoind.request[body] 287 | };enlist] 288 | ) 289 | 290 | 291 | dumpwallet:('[{[args] 292 | requiredArgs:`filename; 293 | optionalArgs:(); 294 | input:parseArgs[args;requiredArgs;optionalArgs]; 295 | if[`error~input;:()]; 296 | body:.bitcoind.defaultPayload[]; 297 | body[`method]:"dumpwallet"; 298 | body[`params]:input; 299 | .bitcoind.request[body] 300 | };enlist] 301 | ) 302 | 303 | 304 | encryptwallet:('[{[args] 305 | requiredArgs:`passphrase; 306 | optionalArgs:(); 307 | input:parseArgs[args;requiredArgs;optionalArgs]; 308 | if[`error~input;:()]; 309 | body:.bitcoind.defaultPayload[]; 310 | body[`method]:"encryptwallet"; 311 | body[`params]:input; 312 | .bitcoind.request[body] 313 | };enlist] 314 | ) 315 | 316 | 317 | estimatesmartfee:('[{[args] 318 | requiredArgs:`conf_target; 319 | optionalArgs:`estimate_mode; 320 | input:parseArgs[args;requiredArgs;optionalArgs]; 321 | if[`error~input;:()]; 322 | body:.bitcoind.defaultPayload[]; 323 | body[`method]:"estimatesmartfee"; 324 | body[`params]:input; 325 | .bitcoind.request[body] 326 | };enlist] 327 | ) 328 | 329 | 330 | fundrawtransaction:('[{[args] 331 | requiredArgs:`hexstring; 332 | optionalArgs:`options`iswitness; 333 | input:parseArgs[args;requiredArgs;optionalArgs]; 334 | if[`error~input;:()]; 335 | body:.bitcoind.defaultPayload[]; 336 | body[`method]:"fundrawtransaction"; 337 | body[`params]:input; 338 | .bitcoind.request[body] 339 | };enlist] 340 | ) 341 | 342 | 343 | generatetoaddress:('[{[args] 344 | requiredArgs:`nblocks`address; 345 | optionalArgs:`maxtries; 346 | input:parseArgs[args;requiredArgs;optionalArgs]; 347 | if[`error~input;:()]; 348 | body:.bitcoind.defaultPayload[]; 349 | body[`method]:"generatetoaddress"; 350 | body[`params]:input; 351 | .bitcoind.request[body] 352 | };enlist] 353 | ) 354 | 355 | 356 | getaddednodeinfo:('[{[args] 357 | requiredArgs:`node; 358 | optionalArgs:(); 359 | input:parseArgs[args;requiredArgs;optionalArgs]; 360 | if[`error~input;:()]; 361 | body:.bitcoind.defaultPayload[]; 362 | body[`method]:"getaddednodeinfo"; 363 | body[`params]:input; 364 | .bitcoind.request[body] 365 | };enlist] 366 | ) 367 | 368 | 369 | getaddressesbylabel:('[{[args] 370 | requiredArgs:`label; 371 | optionalArgs:(); 372 | input:parseArgs[args;requiredArgs;optionalArgs]; 373 | if[`error~input;:()]; 374 | body:.bitcoind.defaultPayload[]; 375 | body[`method]:"getaddressesbylabel"; 376 | body[`params]:input; 377 | .bitcoind.request[body] 378 | };enlist] 379 | ) 380 | 381 | 382 | getaddressinfo:('[{[args] 383 | requiredArgs:`address; 384 | optionalArgs:(); 385 | input:parseArgs[args;requiredArgs;optionalArgs]; 386 | if[`error~input;:()]; 387 | body:.bitcoind.defaultPayload[]; 388 | body[`method]:"getaddressinfo"; 389 | body[`params]:input; 390 | .bitcoind.request[body] 391 | };enlist] 392 | ) 393 | 394 | 395 | getbalance:('[{[args] 396 | requiredArgs:(); 397 | optionalArgs:`dummy`minconf`include_watchonly; 398 | input:parseArgs[args;requiredArgs;optionalArgs]; 399 | if[`error~input;:()]; 400 | body:.bitcoind.defaultPayload[]; 401 | body[`method]:"getbalance"; 402 | body[`params]:input; 403 | .bitcoind.request[body] 404 | };enlist] 405 | ) 406 | 407 | 408 | getbestblockhash:('[{[args] 409 | requiredArgs:(); 410 | optionalArgs:(); 411 | input:parseArgs[args;requiredArgs;optionalArgs]; 412 | if[`error~input;:()]; 413 | body:.bitcoind.defaultPayload[]; 414 | body[`method]:"getbestblockhash"; 415 | body[`params]:input; 416 | .bitcoind.request[body] 417 | };enlist] 418 | ) 419 | 420 | 421 | getblock:('[{[args] 422 | requiredArgs:`blockhash; 423 | optionalArgs:`verbosity; 424 | input:parseArgs[args;requiredArgs;optionalArgs]; 425 | if[`error~input;:()]; 426 | body:.bitcoind.defaultPayload[]; 427 | body[`method]:"getblock"; 428 | body[`params]:input; 429 | .bitcoind.request[body] 430 | };enlist] 431 | ) 432 | 433 | 434 | getblockchaininfo:('[{[args] 435 | requiredArgs:(); 436 | optionalArgs:(); 437 | input:parseArgs[args;requiredArgs;optionalArgs]; 438 | if[`error~input;:()]; 439 | body:.bitcoind.defaultPayload[]; 440 | body[`method]:"getblockchaininfo"; 441 | body[`params]:input; 442 | .bitcoind.request[body] 443 | };enlist] 444 | ) 445 | 446 | 447 | getblockcount:('[{[args] 448 | requiredArgs:(); 449 | optionalArgs:(); 450 | input:parseArgs[args;requiredArgs;optionalArgs]; 451 | if[`error~input;:()]; 452 | body:.bitcoind.defaultPayload[]; 453 | body[`method]:"getblockcount"; 454 | body[`params]:input; 455 | .bitcoind.request[body] 456 | };enlist] 457 | ) 458 | 459 | 460 | getblockhash:('[{[args] 461 | requiredArgs:`height; 462 | optionalArgs:(); 463 | input:parseArgs[args;requiredArgs;optionalArgs]; 464 | if[`error~input;:()]; 465 | body:.bitcoind.defaultPayload[]; 466 | body[`method]:"getblockhash"; 467 | body[`params]:input; 468 | .bitcoind.request[body] 469 | };enlist] 470 | ) 471 | 472 | 473 | getblockheader:('[{[args] 474 | requiredArgs:`blockhash; 475 | optionalArgs:`verbose; 476 | input:parseArgs[args;requiredArgs;optionalArgs]; 477 | if[`error~input;:()]; 478 | body:.bitcoind.defaultPayload[]; 479 | body[`method]:"getblockheader"; 480 | body[`params]:input; 481 | .bitcoind.request[body] 482 | };enlist] 483 | ) 484 | 485 | 486 | getblocktemplate:('[{[args] 487 | requiredArgs:`template_request; 488 | optionalArgs:(); 489 | input:parseArgs[args;requiredArgs;optionalArgs]; 490 | if[`error~input;:()]; 491 | body:.bitcoind.defaultPayload[]; 492 | body[`method]:"getblocktemplate"; 493 | body[`params]:input; 494 | .bitcoind.request[body] 495 | };enlist] 496 | ) 497 | 498 | 499 | getchaintips:('[{[args] 500 | requiredArgs:(); 501 | optionalArgs:(); 502 | input:parseArgs[args;requiredArgs;optionalArgs]; 503 | if[`error~input;:()]; 504 | body:.bitcoind.defaultPayload[]; 505 | body[`method]:"getchaintips"; 506 | body[`params]:input; 507 | .bitcoind.request[body] 508 | };enlist] 509 | ) 510 | 511 | 512 | getchaintxstats:('[{[args] 513 | requiredArgs:(); 514 | optionalArgs:`nblocks`blockhash; 515 | input:parseArgs[args;requiredArgs;optionalArgs]; 516 | if[`error~input;:()]; 517 | body:.bitcoind.defaultPayload[]; 518 | body[`method]:"getchaintxstats"; 519 | body[`params]:input; 520 | .bitcoind.request[body] 521 | };enlist] 522 | ) 523 | 524 | 525 | getconnectioncount:('[{[args] 526 | requiredArgs:(); 527 | optionalArgs:(); 528 | input:parseArgs[args;requiredArgs;optionalArgs]; 529 | if[`error~input;:()]; 530 | body:.bitcoind.defaultPayload[]; 531 | body[`method]:"getconnectioncount"; 532 | body[`params]:input; 533 | .bitcoind.request[body] 534 | };enlist] 535 | ) 536 | 537 | 538 | getdescriptorinfo:('[{[args] 539 | requiredArgs:`descriptor; 540 | optionalArgs:(); 541 | input:parseArgs[args;requiredArgs;optionalArgs]; 542 | if[`error~input;:()]; 543 | body:.bitcoind.defaultPayload[]; 544 | body[`method]:"getdescriptorinfo"; 545 | body[`params]:input; 546 | .bitcoind.request[body] 547 | };enlist] 548 | ) 549 | 550 | 551 | getdifficulty:('[{[args] 552 | requiredArgs:(); 553 | optionalArgs:(); 554 | input:parseArgs[args;requiredArgs;optionalArgs]; 555 | if[`error~input;:()]; 556 | body:.bitcoind.defaultPayload[]; 557 | body[`method]:"getdifficulty"; 558 | body[`params]:input; 559 | .bitcoind.request[body] 560 | };enlist] 561 | ) 562 | 563 | 564 | getmemoryinfo:('[{[args] 565 | requiredArgs:(); 566 | optionalArgs:`mode; 567 | input:parseArgs[args;requiredArgs;optionalArgs]; 568 | if[`error~input;:()]; 569 | body:.bitcoind.defaultPayload[]; 570 | body[`method]:"getmemoryinfo"; 571 | body[`params]:input; 572 | .bitcoind.request[body] 573 | };enlist] 574 | ) 575 | 576 | 577 | getmempoolancestors:('[{[args] 578 | requiredArgs:`txid; 579 | optionalArgs:`verbose; 580 | input:parseArgs[args;requiredArgs;optionalArgs]; 581 | if[`error~input;:()]; 582 | body:.bitcoind.defaultPayload[]; 583 | body[`method]:"getmempoolancestors"; 584 | body[`params]:input; 585 | .bitcoind.request[body] 586 | };enlist] 587 | ) 588 | 589 | 590 | getmempooldescendants:('[{[args] 591 | requiredArgs:`txid; 592 | optionalArgs:`verbose; 593 | input:parseArgs[args;requiredArgs;optionalArgs]; 594 | if[`error~input;:()]; 595 | body:.bitcoind.defaultPayload[]; 596 | body[`method]:"getmempooldescendants"; 597 | body[`params]:input; 598 | .bitcoind.request[body] 599 | };enlist] 600 | ) 601 | 602 | 603 | getmempoolentry:('[{[args] 604 | requiredArgs:`txid; 605 | optionalArgs:(); 606 | input:parseArgs[args;requiredArgs;optionalArgs]; 607 | if[`error~input;:()]; 608 | body:.bitcoind.defaultPayload[]; 609 | body[`method]:"getmempoolentry"; 610 | body[`params]:input; 611 | .bitcoind.request[body] 612 | };enlist] 613 | ) 614 | 615 | 616 | getmempoolinfo:('[{[args] 617 | requiredArgs:(); 618 | optionalArgs:(); 619 | input:parseArgs[args;requiredArgs;optionalArgs]; 620 | if[`error~input;:()]; 621 | body:.bitcoind.defaultPayload[]; 622 | body[`method]:"getmempoolinfo"; 623 | body[`params]:input; 624 | .bitcoind.request[body] 625 | };enlist] 626 | ) 627 | 628 | 629 | getmininginfo:('[{[args] 630 | requiredArgs:(); 631 | optionalArgs:(); 632 | input:parseArgs[args;requiredArgs;optionalArgs]; 633 | if[`error~input;:()]; 634 | body:.bitcoind.defaultPayload[]; 635 | body[`method]:"getmininginfo"; 636 | body[`params]:input; 637 | .bitcoind.request[body] 638 | };enlist] 639 | ) 640 | 641 | 642 | getnettotals:('[{[args] 643 | requiredArgs:(); 644 | optionalArgs:(); 645 | input:parseArgs[args;requiredArgs;optionalArgs]; 646 | if[`error~input;:()]; 647 | body:.bitcoind.defaultPayload[]; 648 | body[`method]:"getnettotals"; 649 | body[`params]:input; 650 | .bitcoind.request[body] 651 | };enlist] 652 | ) 653 | 654 | 655 | getnetworkhashps:('[{[args] 656 | requiredArgs:(); 657 | optionalArgs:`nblocks`height; 658 | input:parseArgs[args;requiredArgs;optionalArgs]; 659 | if[`error~input;:()]; 660 | body:.bitcoind.defaultPayload[]; 661 | body[`method]:"getnetworkhashps"; 662 | body[`params]:input; 663 | .bitcoind.request[body] 664 | };enlist] 665 | ) 666 | 667 | 668 | getnetworkinfo:('[{[args] 669 | requiredArgs:(); 670 | optionalArgs:(); 671 | input:parseArgs[args;requiredArgs;optionalArgs]; 672 | if[`error~input;:()]; 673 | body:.bitcoind.defaultPayload[]; 674 | body[`method]:"getnetworkinfo"; 675 | body[`params]:input; 676 | .bitcoind.request[body] 677 | };enlist] 678 | ) 679 | 680 | 681 | getnewaddress:('[{[args] 682 | requiredArgs:(); 683 | optionalArgs:`label`address_type; 684 | input:parseArgs[args;requiredArgs;optionalArgs]; 685 | if[`error~input;:()]; 686 | body:.bitcoind.defaultPayload[]; 687 | body[`method]:"getnewaddress"; 688 | body[`params]:input; 689 | .bitcoind.request[body] 690 | };enlist] 691 | ) 692 | 693 | 694 | getnodeaddresses:('[{[args] 695 | requiredArgs:(); 696 | optionalArgs:`count; 697 | input:parseArgs[args;requiredArgs;optionalArgs]; 698 | if[`error~input;:()]; 699 | body:.bitcoind.defaultPayload[]; 700 | body[`method]:"getnodeaddresses"; 701 | body[`params]:input; 702 | .bitcoind.request[body] 703 | };enlist] 704 | ) 705 | 706 | 707 | getpeerinfo:('[{[args] 708 | requiredArgs:(); 709 | optionalArgs:(); 710 | input:parseArgs[args;requiredArgs;optionalArgs]; 711 | if[`error~input;:()]; 712 | body:.bitcoind.defaultPayload[]; 713 | body[`method]:"getpeerinfo"; 714 | body[`params]:input; 715 | .bitcoind.request[body] 716 | };enlist] 717 | ) 718 | 719 | 720 | getrawchangeaddress:('[{[args] 721 | requiredArgs:(); 722 | optionalArgs:`address_type; 723 | input:parseArgs[args;requiredArgs;optionalArgs]; 724 | if[`error~input;:()]; 725 | body:.bitcoind.defaultPayload[]; 726 | body[`method]:"getrawchangeaddress"; 727 | body[`params]:input; 728 | .bitcoind.request[body] 729 | };enlist] 730 | ) 731 | 732 | 733 | getrawmempool:('[{[args] 734 | requiredArgs:(); 735 | optionalArgs:`verbose; 736 | input:parseArgs[args;requiredArgs;optionalArgs]; 737 | if[`error~input;:()]; 738 | body:.bitcoind.defaultPayload[]; 739 | body[`method]:"getrawmempool"; 740 | body[`params]:input; 741 | .bitcoind.request[body] 742 | };enlist] 743 | ) 744 | 745 | 746 | getrawtransaction:('[{[args] 747 | requiredArgs:`txid; 748 | optionalArgs:`verbose`blockhash; 749 | input:parseArgs[args;requiredArgs;optionalArgs]; 750 | if[`error~input;:()]; 751 | body:.bitcoind.defaultPayload[]; 752 | body[`method]:"getrawtransaction"; 753 | body[`params]:input; 754 | .bitcoind.request[body] 755 | };enlist] 756 | ) 757 | 758 | 759 | batch_getrawtransactions:('[{[args] 760 | supportedArgs:`txids`verbose; 761 | optionalArgs:`verbose; 762 | if[(count supportedArgs)prev px) by sym from 7 | //L05:将pp dict转为字段方便后续处理。 8 | {(delete pp from x),'(::)each exec pp from x} //pp dict => field 9 | //L04:计算信号,权益等: pp:{[x;y]...}\[ x初始值;y序列数据] 10 | update pp: {[x;y] 11 | //L04a:初始化 12 | if[0=x`ps;x:`ps`pt`px`ca`eq!(0j;0Np;0f;x`ca;x`eq)]; 13 | //L04b:卖出 14 | if[(x[`ps]>0)&(y[`ma1]y`ma2); 20 | x[`pt]:y[`date]; x[`px]:y[`close]; 21 | x[`ps]:100*floor 0.01*x[`ca] div y[`close]*(1+y[`fee]); 22 | x[`ca]:x[`ca]-x[`ps]*y[`close]*(1+y[`fee]) ]; 23 | //L04d:更新eq 24 | x[`eq]:x[`ca]+x[`ps]*y[`close]; 25 | //L04e:返回x 26 | :x; } 27 | //L04f: ps=position,pt=date,px=price,ca=cash,eq=equity 28 | \ [`ps`pt`px`ca`eq!(0j;0Np;0f;10000000f;10000000f) ; flip`date`close`ma1`ma2`flg`fee!(date;close;ma1;ma2;flg;0.0004)] by sym from 29 | //L03:计算均线指标 30 | update ma1:mavg[20;close],ma2:mavg[100;close],flg:1002010.01.01 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /q/btex02.q: -------------------------------------------------------------------------------- 1 | //均线策略,限制最多持有只数 2 | system "l d:/kdb/hdb"; 3 | //参数:p1/p2均线参数,ca0初始资金,fee费率,dt0/dt1起止日期,max_pos_syms最多持有只数 4 | para:`p1`p2`ca0`fee`dt0`dt1`max_pos_syms!(5;10;10000000f;0.0004;2019.01.01;.z.D;10); 5 | //L04:结果 6 | select last n,last eq,last ret,last annret,last mdd by date from 7 | update ret:{-1+x%first x}[eq], 8 | annret:{[x;y]((y%first y) xexp' 365.0%(x-first x))-1}[date;eq], 9 | mdd:{1-mins x %maxs x}eq from 10 | {delete pos from (delete pp from x),'(::)each exec pp from x} 11 | //L03:计算交易信号、净值等 12 | update pp:{[x;y] 13 | /L03a:持仓,更新价格 14 | pos:x`pos;pos[y`sym;`close]:y`close; 15 | /L03b:计算当前持股只数 16 | num:0^exec count sym from pos where ps>0; 17 | /L03c:卖出 18 | if[(pos[y`sym;`ps]>0)&(y[`ma1]pos[y`sym;`pt]); 19 | x[`ca]:x[`ca]+pos[y`sym;`ps]*y[`close]*(1-para[`fee]); 20 | x[`eq]:x[`ca]+exec sum ps*close from pos;pos[y`sym;`ps]:0 ]; 21 | /L03d:若无持仓,设置相关字段 22 | if[null[pos[y`sym;`ps]] or pos[y`sym;`ps]=0; 23 | pos[y`sym;`ps`pt`px`n]:(0;0Nd;0f;0)]; 24 | /L03e:买入 25 | if[(pos[y`sym;`ps]=0)&y[`flg]&(y[`ma1]>y`ma2)&(numpara`p1; 34 | /L09e:取d[`date]的数据 35 | bar:`rnk`chg xdesc select from bars where date=d`date; 36 | /L09f:卖出 37 | bi:0;do[count bar;b:bar[bi]; 38 | if[(pos[b`sym;`ps]>0)&b[`rnk]<>9; 39 | ca:ca+pos[b`sym;`ps]*b[`close]*1-para[`fee]; 40 | pos[b`sym;`ps]:0]; 41 | bi:bi+1]; 42 | /L09g:买入;由于买入要考虑现金是否充足,先卖后买 43 | bi:0;do[count bar;b:bar[bi]; 44 | if[(pos[b`sym;`ps] in (0j;0Nj))&(b[`rnk]=9)&(b[`volume]>0)&(b[`atr]>0); 45 | bqty:100*floor 0.01*eq*para[`rf]%b`atr; 46 | if[(bqty>0)&(ca>myca:bqty*b[`close]*1+para`fee); 47 | pos[b`sym;`ps`pt`px]:(bqty;d`date;b[`close]*1+para`fee); 48 | ca:ca-myca]]; 49 | bi:bi+1]; 50 | ]; //L09d结束 51 | //L09h:更新持仓表 52 | delete from `pos where ps=0; 53 | update close:{[mydate;mysym]exec 0f^last close from bars 54 | where date=mydate,sym=mysym}[d`date]each sym from `pos; 55 | //L09i: 更新组合表 56 | por[d[`date];`pos`ca`eq]:(pos;ca;eq:ca+{$[x~();0f;x]}exec sum ps*close from pos where ps>0); 57 | //L09j: 下一交易日 58 | di:di+1 59 | ]; //L09b结束 60 | //L10:结束,显示绩效指标,可根据需要进一步处理 61 | select date,eq,ret:{-1+x%first x}[eq],annret:{[x;y]((y%first y) xexp' 365.0%(x-first x))-1}[date;eq], 62 | mdd:{1-mins x %maxs x}[eq],symcnt:{exec count i from x where ps>0}each pos from por 63 | -------------------------------------------------------------------------------- /q/btex04.q: -------------------------------------------------------------------------------- 1 | bt:{[para]:select sym,date,eq,ret,annret,mdd,trades,wins,fee:para`fee,m1:para`m1,m2:para`m2 from 2 | select by sym from update ret:{-1+x%first x} eq, annret:{[x;y]((y%first y) 3 | xexp' 365.0%(x-first x))-1} [date;eq],mdd:{1-mins x %maxs x}eq,trades:sums((ps=0)&0<0^prev ps), 4 | wins:sums (psprev px) by sym from 5 | {(delete pp from x),'(::)each exec pp from x} //pp dict => field 6 | update pp: {[x;y]if[0=x`ps;x:`ps`pt`px`ca`eq!(0j;0Np;0f;x`ca;x`eq)]; 7 | if[(x[`ps]>0)&(y[`ma1]y`ma2);x[`pt]:y[`date]; x[`px]:y[`close]; 10 | x[`ps]:100*floor 0.01*x[`ca] div y[`close]*(1+y[`fee]); 11 | x[`ca]:x[`ca]-x[`ps]*y[`close]*(1+y[`fee]) ]; 12 | x[`eq]:x[`ca]+x[`ps]*y[`close]; :x; } 13 | \ [`ps`pt`px`ca`eq!(0j;0Np;0f;10000000f;10000000f) ; 14 | flip`date`close`ma1`ma2`flg`fee!(date;close;ma1;ma2;flg;para`fee)] by sym from 15 | update ma1:mavg[para`m1;close],ma2:mavg[para`m2;close],flg:100 `xxxxxx.SH, `1xxxxxx=>`xxxxxx.SZ : necode2sym[`0600036] necode2sym[`1000001] 3 | necode2sym:{`$$["0"~first sx:string x;(1_sx),".SH";"1"~first sx;(1_sx),".SZ";sx]}; 4 | /代码格式转换:`xxxxxx.SH => `0xxxxxx, `xxxxxx.SZ => `1xxxxxx : sym2necode[`000001.SH] sym2necode[`000001.SZ] 5 | sym2necode:{`$$[".SH"~-3#sx:string x;"0",-3_sx;".SZ"~-3#sx;"1",-3_sx;sx]}; 6 | 7 | /读交易日(上证综指日线数据日期): gettrddt[.z.D-7;.z.D] 8 | gettrddt:{[startdate;enddate]`date xasc `date xcol("D ";enlist",")0:.Q.hg "http://quotes.money.163.com/service/chddata.html?code=0000001&start=",ssr[string[startdate];".";""],"&end=",ssr[string enddate;".";""],"&fields=TCLOSE"}; 9 | 10 | /读A股代码表 getcsasyms[] 11 | getcsasyms:{select sym:necode2sym each code,`$name from update `$code from {lower[cols x]xcol x}.j.k[.Q.hg "http://quotes.money.163.com/hs/service/diyrank.php?query=STYPE%3AEQA&fields=CODE,NAME&sort=SYMBOL&order=asc&count=8000&type=query"]`list}; 12 | 13 | /读指数代码表 getcsisyms[] or getcsisyms[`SH] or getcsisyms[`SZ] 14 | getcsisyms:{ /ex: getcsisyms[] or getcsisyms[`SH] or getcsisyms[`SZ] 15 | qrystr:$[null x;qs:"CODE:_in_0000001,0000016,0000300,0000905,0000906,0000852,1399001,1399005,1399006,1399106";x=`SH;"IS_INDEX:true;EXCHANGE:CNSESH";x=`SZ;"IS_INDEX:true;EXCHANGE:CNSESZ";qs]; select sym:necode2sym each`$code,`$name from {lower[cols x]xcol x}.j.k[.Q.hg"http://quotes.money.163.com/hs/service/hsindexrank.php?host=/hs/service/hsindexrank.php&page=0&query=",qrystr,"&fields=no,SYMBOL,NAME&sort=SYMBOL&order=asc&count=5000&type=query"]`list}; 16 | 17 | /读A股、指数的日行情: getcsbar[`000001.SH;.z.D-3;.z.D] 18 | getcsbar:{[mysym;startdate;enddate]`date xasc update sym:mysym,open:?[open>0;open;prevclose],high:?[high>0;high;prevclose],low:?[low>0;low;prevclose],close:?[close>0;close;prevclose],0f^mv,0f^fmv from `date`sym`prevclose`open`high`low`close`volume`amount`mv`fmv xcol("DS FFFFFFFFF";enlist",")0: .Q.hg "http://quotes.money.163.com/service/chddata.html?code=",string[sym2necode mysym],"&start=",ssr[string[startdate];".";""],"&end=",ssr[string enddate;".";""],"&fields=LCLOSE;TOPEN;HIGH;LOW;TCLOSE;VOTURNOVER;VATURNOVER;TCAP;MCAP"}; 19 | 20 | /读A股最新行情: prevclose昨收价,open开盘价,high最高价,low最低价,close最新价,volume成交量,amount成交金额,mv总市值,fmv流通市值,eps每股收益,zdf涨跌幅,zf震幅,hsl换手率,pe市盈率 21 | getcsataq:{ /ex: getcsataq[] or getcsataq `000001.SZ or getcsataq `600036.SH 22 | qrystr:$[null x;"STYPE%3AEQA";-11h=type[x];"CODE%3A",string[sym2necode x],"%3BSTYPE%3AEQA";"STYPE%3AEQA"]; : select dt:"Z"$time,sym:necode2sym each`$code,`$name,prevclose:yestclose,open:?[open>0;open;yestclose],high:?[high>0;high;yestclose],low:?[low>0;low;yestclose],close:?[price>0;price;yestclose],volume,amount:turnover,mv:tcap,fmv:mcap,eps:mfsum,zdf:percent,zf,hsl:hs,pe from {lower[cols x]xcol x} {key[d]#d:(`CODE`HIGH`HS`LOW`MCAP`MFSUM`NAME`OPEN`PE`PERCENT`PRICE`SYMBOL`TCAP`TIME`TURNOVER`VOLUME`YESTCLOSE`ZF`NO!(();0Nf;0Nf;0Nf;0Nf;0Nf;();0Nf;0Nf;0Nf;0Nf;();0Nf;();0Nf;0Nf;0Nf;0Nf;0Nf)),x} each {(.j.k[x])`list} .Q.hg"http://quotes.money.163.com/hs/service/diyrank.php?page=0&query=",qrystr,"&fields=NO%2CSYMBOL%2CNAME%2CYESTCLOSE%2CPRICE%2CPERCENT%2COPEN%2CHIGH%2CLOW%2CVOLUME%2CTURNOVER%2CHS%2CZF%2CPE%2CMFSUM%2CMCAP%2CTCAP%2CCODE%2CTIME&sort=PERCENT&order=desc&count=8000&type=query"}; 23 | 24 | /读指数最新行情: 25 | getcsitaq:{ /ex: getcsitaq[] or getcsitaq[`000001.SH] or getcsitaq[`399001.SZ] 26 | qrystr:$[null x;qs:"CODE:_in_0000001,0000016,0000300,0000905,0000906,0000852,1399001,1399005,1399006,1399106";-11h=type[x];"CODE:_in_",string[sym2necode x];qs]; select dt:"Z"$time,sym:necode2sym each`$code,name,prevclose:yestclose,open,high,low,close:price,volume,amount:turnover,mv:0f,fmv:0f,eps:0f,zdf:percent,zf:zhenfu,hsl:0f,pe:0f from {lower[cols x]xcol x}.j.k[.Q.hg"http://quotes.money.163.com/hs/service/hsindexrank.php?host=/hs/service/hsindexrank.php&page=0&query=",qrystr,"&fields=no,TIME,SYMBOL,NAME,PRICE,UPDOWN,PERCENT,zhenfu,VOLUME,TURNOVER,YESTCLOSE,OPEN,HIGH,LOW&sort=SYMBOL&order=asc&count=25&type=query"]`list}; 27 | 28 | /中金所行情 29 | getcfebar:{[mydate]if[-14h<>type mydate;:`error_para];et:flip`date`sym`open`high`low`close`volume`amount`openint!"DSFFFFFFF"$\:(); 30 | if[mydate<2010.04.16;:et];datestr:string[mydate]_/ 4 6;monthstr:6#datestr;daystr:-2#datestr; 31 | `date xcols update date:mydate from{update {`$string[x],".CFE"}each sym,0f^open,0f^high,0f^low,0f^close,amount*10000.0 from select from x where sym like "*[0-9]"} 32 | `sym`open`high`low`close xcols`sym`open`high`low`volume`amount`openint`close xcol("S",7#"F";enlist",")0: 33 | .Q.hg"http://www.cffex.com.cn/sj/hqsj/rtj/",monthstr,"/",daystr,"/",datestr,"_1.csv"}; 34 | 35 | /上期所行情 36 | getshfbar:{[mydate]if[-14h<>type mydate;:`error_para];et:flip`date`sym`open`high`low`close`volume`amount`openint!"DSFFFFFFF"$\:(); 37 | r:@[.j.k;ssr[;":\"\",";":0,"].Q.hg"http://www.shfe.com.cn/data/dailydata/kx/kx",(string[mydate]_/4 6),".dat";`]; 38 | :$[99h=type r; 39 | {select from x where sym like "*[0-9].SHF"}select date:mydate,sym:(`$(upper -2_/:trim PRODUCTID),'DELIVERYMONTH,\:".SHF"),open:OPENPRICE,high:HIGHESTPRICE,low:LOWESTPRICE,close:CLOSEPRICE,volume:VOLUME,amount:0f,openint:OPENINTEREST from select from r[`o_curinstrument] where (10h=type each DELIVERYMONTH)&10h=type each PRODUCTID; 40 | et]; 41 | }; 42 | 43 | /上期能源行情 44 | getinebar:{[mydate]if[-14h<>type mydate;:`error_para];et:flip`date`sym`open`high`low`close`volume`amount`openint!"DSFFFFFFF"$\:(); 45 | if[mydate<2018.03.26;:et]; 46 | r:@[.j.k;ssr[;":\"\",";":0,"] .Q.hg"http://www.ine.cn/data/dailydata/kx/kx",(string[mydate]_/4 6),".dat";`]; 47 | :$[99h=type r; 48 | {select from x where sym like "*[0-9].INE"}select date:mydate,sym:(`$(upper -2_/:trim PRODUCTID),'DELIVERYMONTH,\:".INE"),open:OPENPRICE,high:HIGHESTPRICE,low:LOWESTPRICE,close:CLOSEPRICE,volume:VOLUME,amount:0f,openint:OPENINTEREST from select from r[`o_curinstrument] where (10h=type each DELIVERYMONTH)&10h=type each PRODUCTID; 49 | et]; 50 | }; 51 | 52 | /大商所品种 53 | getdceprod:{[mydate]dstr:string[mydate]_/4 6;mstr:string neg[1]+`mm$mydate; 54 | distinct update{upper `$ssr[string x;"[0-9]";""]}each sym from{select from x where sym like "*[0-9]"}`name`sym xcol 55 | ("SS ";enlist"\t")0: ssr[;"%";""]ssr[;"\t\t";"\t"] 56 | .Q.hp["http://www.dce.com.cn/publicweb/businessguidelines/exportFutAndOptSettle.html";"application/x-www-form-urlencoded"] 57 | "variety=all&trade_type=0&year=",(4#dstr),"&month=",mstr,"&day=",(-2#dstr),"&exportFlag=txt"}; 58 | / 大商所行情 59 | getdcebar:{[mydate]if[-14h<>type mydate;:`error_para]; 60 | dceprod:`name xkey getdceprod[mydate]; /大商所品种及其名称 61 | r:.Q.hg"http://www.dce.com.cn/publicweb/quotesdata/exportDayQuotesChData.html?dayQuotes.variety=all&dayQuotes.trade_type=0&year=",string[`year$mydate],"&month=",string[neg[1]+`mm$mydate],"&day=",string[`dd$mydate],"&exportFlag=txt"; 62 | select date:mydate,sym:sym{`$string[x],string[y],".DCE"}'month,open,high,low,close,volume,amount*10000f,openint from 63 | lj[;dceprod]{select from x where month>0}`name`month`open`high`low`close`volume`openint`amount xcol ("SJFFFF FF F";enlist"\t")0: ssr[ssr[r;",";""];"\t\t";"\t"] }; 64 | 65 | /郑商所行情 66 | czchttp:{[path] r:`:http://www.czce.com.cn "GET ",path," HTTP/1.1\r\nHost: www.czce.com.cn\r\nAccept-Encoding: identity",$[`Cookie in key `.czce;"\r\n",.czce.Cookie;""],"\r\n\r\n"; 67 | if[not()~ck:{x where x like "Set-Cookie:*"}"\r\n" vs first["\r\n\r\n" vs r]; 68 | r:`:http://www.czce.com.cn "GET ",path," HTTP/1.1\r\nHost: www.czce.com.cn\r\nAccept-Encoding: identity\r\n",(.czce.Cookie::4_first ck),"\r\n\r\n"]; 69 | :(vs[" ";first "\r\n" vs first["\r\n\r\n" vs r]][1]; (first[ss[r;"\r\n\r\n"]]+4)_r); /(status;body) 70 | }; 71 | getczcbar:{[mydate]if[-14h<>type mydate;:`error_para];dstr:string[mydate]_/4 6; et:flip`date`sym`open`high`low`close`volume`amount`openint!"DSFFFFFFF"$\:(); 72 | :`date xcols update date:mydate from{[mydate;x]select {[d;s]s:string[s];i:0;while[(i<9)&(`month$d)>"M"$"20",yymm:string[i],(-3#s);i+:1];`$(-3_s),yymm,".CZC"}[mydate]each sym,open,high,low,close,volume,amount*10000f,openint from x where sym like "*[0-9]"}[mydate] 73 | $[mydate>2015.09.18; 74 | [r:czchttp["/cn/DFSStaticFiles/Future/",(4#dstr),"/",dstr,"/FutureDataDaily.txt"]; 75 | $[r[0]~"200";`sym`open`high`low`close`volume`openint`amount xcol("S FFFF FF F";enlist"|")0:ssr[r 1;",";""];:et] 76 | ]; 77 | mydate>=2010.08.25; 78 | [r:czchttp["http://www.czce.com.cn/cn/exchange/",(4#dstr),"/datadaily/",dstr,".txt"]; 79 | $[r[0]~"200";`sym`open`high`low`close`volume`openint`amount xcol("S FFFF FF F";enlist",")0:r 1;:et] 80 | ]; 81 | [r:czchttp["/cn/exchange/jyxx/hq/hq",dstr,".html"]; 82 | $[r[0]~"200";flip`sym`open`high`low`close`volume`openint`amount!("S FFFF FF F ";",")0: 83 | enlist ssr[;" ";""]ssr[;"\r\n";","]ssr[;",";""]ssr[;"\r\n\r\n";"\r\n"]ssr[;"";""] ssr[;"";""] 84 | {last[ss[x;"\r\n\r\n
"]]#x} {(first[ss[x;""]])_x} r 1;:et] 85 | ] 86 | ]; 87 | }; 88 | 89 | 90 | /期货当前合约 91 | gethxcfsyms:{[ex] /ex: gethxcfsyms`CFE gethxcfsyms`SHF gethxcfsyms`DCE gethxcfsyms`CZC 92 | :`sym xasc select sym:(`$sym,\:".",string[ex]) from flip `sym`id!flip last last .j.k 1_-2_ 93 | .Q.hg $[ex=`CFE;"http://webcffex.hermes.hexun.com/cffex/sortlist?block=770"; 94 | "http://webftcn.hermes.hexun.com/gb/shf/sortlist?block=",$[ex=`SHF;"430";ex=`DCE;"431";"432"] 95 | ],"&number=1000&title=15&commodityid=0&direction=0&start=0&column=code" ; 96 | }; 97 | 98 | /行情数据 99 | gethxbar:{[mysym;startdate;num;bartype] /"num:x or -x,x<=1000; bartype:`1m`5m`30m`60m`1d`1w`1m" 100 | sym2hxsym:{$[x like "*.SH";"sse",-3_string[x];x like "*.SZ";"szse",-3_string[x];x like "*.CFE";"CFFEX",-4_string[x];like[x;"[FR]U*.SHF"] or like[x;"WR*.SHF"] or like[x;"HC*.SHF"] or like[x;"RB*.SHF"] or like[x;"SP*.SHF"];"SHFE",-4_string[x];x like "A[GU]*.SHF";"SHFE2",-4_string[x];x like "*.SHF";"SHFE3",-4_string[x];x like "*.DCE";"DCE",-4_string[x];x like "*.CZC";"CZCE",-4_string[x];string[x]]}; 101 | r:.j.k -2_1_ .Q.hg URL::$[mysym like "*.S[HZ]";"http://webstock.quote.hermes.hexun.com/a";mysym like "*.CFE";"http://webcffex.hermes.hexun.com/cffex";"http://webftcn.hermes.hexun.com/shf"],"/kline?code=",sym2hxsym[mysym],"&start=",(string[startdate]_/4 6),"000000&number=",string[num],"&type=",(`1m`5m`15m`30m`60m`1d`1w`1m`!"012345675")[bartype]; 102 | rrr:flip lower[raze key each r`Kline]!flip(rr:r`Data)[0]; 103 | :$[mysym like "*.S[HZ]";select date:"D"$8#/:string[`long$time],sym:mysym,time:`timespan$"T"$-6#/:string[`long$time],prevclose:lastclose%rr[4],open%rr[4],high%rr[4],low%rr[4],close%rr[4],volume,amount,openint:0f from rrr; 104 | select date:"D"$8#/:string[`long$time],sym:mysym,time:`timespan$"T"$-6#/:string[`long$time],prevclose:lastclose%rr[4],open%rr[4],high%rr[4],low%rr[4],close%rr[4],volume,amount,openint:openinterest from rrr]; 105 | }; 106 | 107 | /下载数据并以分列表形式保存在hdb : setcsbar[] 108 | setcsbar:{0N!(.z.Z;`setcsbar.start...); 109 | startdate:2010.01.01; 110 | hdbpath:hsym `$hdbp:ssr[getenv`QHOME;"\\";"/"],"/../hdb"; /hdb路径,如果没有定义QHOME需要修改 111 | @[.Q.chk;hdbpath;`];@[system;"l ",hdbp;`]; 112 | trddt:gettrddt[2010.01.01;.z.D]; 113 | maxdt:$[`csbar in key `.;startdate^exec last date from select max date from csbar;startdate]; 114 | newdatelist:exec date from trddt where date>maxdt; 115 | if[0=count newdatelist;0N!(.z.Z;`no.upd.required.);:`no.upd.required]; 116 | dt:exec first dt from getcsitaq[`000001.SH]; 117 | if[(1=count[newdatelist])&(last[newdatelist]=`date$dt)&(15:01:00<`time$dt); 118 | t:select date:`date$dt,sym,prevclose,open,high,low,close,volume,amount,fltshr:fmv%close from getcsataq[],getcsitaq[]; 119 | if[(98h=type t)&(0lastdate;0N!(.z.Z;mysym;firstdate;lastdate;`no.upd);:()]; 126 | 0N!(.z.Z;mysym;firstdate;lastdate); 127 | (` sv (hdbpath;`csbar;`) ) upsert .Q.en[hdbpath]select date,sym,prevclose,open,high,low,close,volume,amount,fltshr:fmv%close from getcsbar[mysym;firstdate;lastdate]; 128 | }[;startdate;exec last date from trddt;hdbpath]each asc distinct exec sym from getcsisyms[],getcsasyms[]; 129 | @[` sv (hdbpath;`csbar;`);`date`sym;`g#]; 130 | 0N!(.z.Z;`setcsbar.stop.); 131 | }; 132 | 133 | /将分列表csbar保存为分区表csbarp(..p=partitioned) 134 | setcsbarp:{[startdate;enddate]0N!(.z.T;`start...); 135 | hdbpath:hsym `$hdbp:ssr[getenv`QHOME;"\\";"/"],"/../hdb"; /hdb路径,如果没有定义QHOME需要修改 136 | .Q.chk hdbpath;system "l ",hdbp; 137 | {[dt;hdbpath]0N!(.z.T;dt;hdbpath); 138 | (` sv (hdbpath;`$string dt;`csbarp;`) ) set .Q.en[hdbpath]`sym xasc select sym,prevclose,open,high,low,close,volume,amount,mv,fmv from csbar where date=dt; 139 | }[;hdbpath]each exec distinct date from csbar where date within (startdate;enddate); 140 | }; 141 | 142 | /下载期货行情数据并以分列表形式保存在hdb : setcfbar[] 143 | setcfbar:{0N!(.z.T;`start...); 144 | hdbpath:hsym `$hdbp:ssr[getenv`QHOME;"\\";"/"],"/../hdb"; /hdb路径,如果没有定义QHOME需要修改 145 | @[.Q.chk;hdbpath;`];@[system;"l ",hdbp;`]; 146 | newdatelist:exec date from gettrddt[;.z.D] 1+$[`cfbar in key `.;2010.01.01^exec last date from select max date from cfbar;2010.01.01]; 147 | if[0=count newdatelist;:`no.upd.required]; 148 | {[dt;hdbpath]0N!(.z.T;dt;hdbpath); 149 | (` sv (hdbpath;`cfbar;`) ) upsert .Q.en[hdbpath]`sym xasc select date,sym,open,high,low,close,volume,amount,openint from 150 | getcfebar[dt],getshfbar[dt],getinebar[dt],getdcebar[dt],getczcbar[dt]; 151 | }[;hdbpath]each newdatelist; 152 | @[` sv (hdbpath;`cfbar;`);`date`sym;`g#]; 153 | 0N!(.z.T;`stop.); 154 | }; 155 | -------------------------------------------------------------------------------- /q/tick/cfmd.q: -------------------------------------------------------------------------------- 1 | //kdb+CTP行情接口 感谢itfin/zhu honghai/... 2 | 3 | //1.配置行情接口信息 4 | /qctpmdinfo:`front`front2`broker`user`pwd!(`tcp://180.168.146.187:10110;`;`9999;`;`); //标准CTP(交易时间用) 5 | qctpmdinfo:`front`front2`broker`user`pwd!(`tcp://180.168.146.187:10131;`;`9999;`;`); //7*24小时环境 6 | 7 | //2.修改where条件配置要订阅的代码 8 | getcfsyms:{symsmap::1!select exsym,sym from {update {`$string[x]_2}each exsym from x where ex=`CZC} getwebfutsyms[]; 9 | .ctpmd.sub_syms::exec sym from select sym from symsmap where (sym like "RB[0-9]*.SHF")|(sym like "[IJ][0-9]*.DCE")|(sym like "AP[0-9]*.CZC"); 10 | }; 11 | 12 | 13 | //=============================期货合约代码转换公式============================= 14 | ctpexsym2sym:{[exsym]:symsmap[exsym;`sym];}; 15 | ctpsym2exsym:{[x]:exec first exsym from select exsym from symsmap where sym=x;}; 16 | //从新浪读取期货合约代码 17 | getwebfutsyms:{ht:.Q.hg`$":http://finance.sina.com.cn/iframe/futures_info_cff.js"; 18 | :{update exsym:?[ex in`DCE`SHF;lower exsym;exsym],sym:(`$string[exsym],'".",/:string[ex]) from select ex,exsym,name from delete from x where (exsym in`NULL`SHF`DCE`CZC`CFE)or(name=`$"\272\317\324\274")or(name like "*\301\254\320\370")}{update ex:fills?[exsym in`SHF`DCE`CZC`CFE;exsym;`] from x} 19 | flip`name`exsym!flip{$[x like "*new Array(*";{`$"," vs {ssr[x;"\"";""]} -2 _ (2+x ? "(")_ x} x;x like "*\311\317\306\332\313\371*";`SHF;x like "*\264\363\311\314\313\371*";`DCE;x like "*\326\243\311\314\313\371*";`CZC;x like "*\326\320\275\360\313\371*";`CFE;`NULL]}each ";" vs ht}; 20 | //============================================================================== 21 | \c 100 150 22 | .ctpmd.flow:`$ssr[getenv`qhome;"\\";"/"],"/../data/temp/"; 23 | sv[`;(hsym[.ctpmd.flow];`null)] set (); /在流文件路径写一个文件,以确保该路径已存在,否则api初始化时会出错 24 | if[not system"p";system"p 5014"]; 25 | upd:()!(); 26 | .q.showmsg:showmsg:{0N!(x;.z.Z);}; 27 | h:neg hopen `::5010;if[h=0;showmsg`tickerplant_conn_error];showmsg(`connect_to_tickerplant;h); 28 | cftaq:([sym:`$()]date:`date$();time:`timespan$();prevclose:`float$();open:`float$();high:`float$();low:`float$();close:`float$();volume:`float$();amount:`float$();openint:`float$();bid:`float$();bsize:`float$();ask:`float$();asize:`float$();upperlimit:`float$();lowerlimit:`float$()); 29 | CTPCtrl:(`u#enlist `)!(enlist ()); /保存状态 30 | L12:L11:L:(); 31 | `initctpq`freectpq`ctpqrun`userLoginQ`subscribeMarketData{x set `qctpmd 2:(x;y);}' 1 1 1 2 1; /加载 32 | CTPMDKey:`TradingDay`InstrumentID`ExchangeID`ExchangeInstID`LastPrice`PreSettlementPrice`PreClosePrice`PreOpenInterest`OpenPrice`HighestPrice`LowestPrice`Volume`Turnover`OpenInterest`ClosePrice`SettlementPrice`UpperLimitPrice`LowerLimitPrice`PreDelta`CurrDelta`UpdateTime`UpdateMillisec`BidPrice1`BidVolume1`AskPrice1`AskVolume1`BidPrice2`BidVolume2`AskPrice2`AskVolume2`BidPrice3`BidVolume3`AskPrice3`AskVolume3`BidPrice4`BidVolume4`AskPrice4`AskVolume4`BidPrice5`BidVolume5`AskPrice5`AskVolume5`AveragePrice; 33 | ctpmdlogined:0b; 34 | ctpqconn:{[x;y]CTPCtrl[`Run]:initctpq[ qctpmdinfo[`front`front2`broker`user`pwd],.ctpmd.flow,(::)];}; 35 | ctpqdisc:{[x;y]CTPCtrl[`Run]:freectpq[];upd[`FrontDisconnectQ][];}; /断开连接 36 | ctpqlogin:{[]if[(not 1b~CTPCtrl`ConnectQ)|(1b~CTPCtrl`LoginQ);:()];ctpcall[`userLoginQ;qctpmdinfo[`broker`user`pwd]];}; 37 | ctpmdsub:{$[-11h=type x;subscribeMarketData[ctpsym2exsym x];11h=type x;subscribeMarketData[`$"," sv string ctpsym2exsym each x];:`syms_error];}; /订阅行情 38 | upd[`taq1]:{ctpmdsub x`symlist;}; 39 | seq:0i;newseq:{seq::seq+1;:`int$seq;}; 40 | ctpcall:{[x;y]k:newseq[];C,:enlist (.z.T;k;x;y);(value x)[k;y,$[0h=type y;();enlist (::)]];}; /调用各种api接口方法 41 | onctpmd:{[x]{upd[x[0]][x[1]];}each x;}; // 事件分发器 onctpmd:{[x]L,:(enlist .z.T),x;upd[x[0]][x[1]];}; //onctp:{[x]L,:(enlist .z.T),/:x;{UPD[x[0]][x[1]]} each x;}; 42 | upd[`FrontConnectQ]:{[x]showmsg(`OnFrontConnected);showmsg(`ReqUserLogin;qctpmdinfo[`broker`user]);CTPCtrl[`ConnectQ`ConntimeQ]:(1b;.z.P);ctpqlogin[];}; //zzz 前置机连接事件 43 | upd[`FrontDisconnectQ]:{[x]showmsg(`OnFrontDisconnected);CTPCtrl[`ConnectQ`LoginQ`DiscReasonQ`DisctimeQ]:(0b;0b;x[0];.z.P);}; //断开连接事件 44 | upd[`UserLoginQ]:{[x]showmsg(`OnRspUserLogin);showmsg(`SubscribingMarketData;.ctpmd.sub_syms);y:x[2];if[0=count y;:()];CTPCtrl[`LoginQ`LoginTimeQ`FrontID_Q`QSessionID_Q`QMaxOrderRef_Q]:(1b;.z.P),y;ctpmdlogined::1b; ctpmdsub[.ctpmd.sub_syms];}; //用户已登录事件函数 45 | upd[`UserLogoutQ]:{ctpmdlogined::0b;}; 46 | upd[`SubMarketDataQ]:{showmsg[`$"SubMarketData OK."];}; 47 | 48 | upd[`DepthMD]:{[x]y:CTPMDKey!x;upd[`ctptaq](mysym:ctpexsym2sym[`$y`InstrumentID];mydate:.z.D^"D"$y[`TradingDay];.z.N^`timespan$("T"$y[`UpdateTime])+y[`UpdateMillisec];"f"$y[`PreClosePrice];"f"$y[`OpenPrice];"f"$y[`HighestPrice];"f"$y[`LowestPrice];"f"$y[`LastPrice];"f"$y[`Volume];"f"$y[`Turnover];"f"$y[`OpenInterest];0f^"f"$y[`BidPrice1];"f"$y[`BidVolume1];0f^"f"$y[`AskPrice1];"f"$y[`AskVolume1];"f"$y[`UpperLimitPrice];"f"$y[`LowerLimitPrice]);}; //行情事件函数 49 | upd[`ctptaq]:{d:`sym`date`time`prevclose`open`high`low`close`volume`amount`openint`bid`bsize`ask`asize`upperlimit`lowerlimit!x; 50 | if[(d[`close]<=0f)|d[`close]=0wf;d[`close]:0f];if[(d[`open]<=0f)|d[`open]=0wf;d[`open]:d[`close]]; 51 | if[(d[`high]<=0f)|d[`high]=0wf;d[`high]:d[`close]];if[(d[`low]<=0f)|d[`low]=0wf;d[`low]:d[`close]];if[d[`volume]=0nf;d[`volume]:0f]; 52 | `cftaq upsert d; h(`.u.upd;`cftaq;d[`time`sym`prevclose`open`high`low`close`volume`amount`openint`bid`bsize`ask`asize`upperlimit`lowerlimit]); / 发给tp的taq必须是list不是dict 53 | }; 54 | start:{showmsg`start...;getcfsyms[];ctpmdstarted::1b;ctpqconn[`;.z.P];}; //启动 start[]; 55 | stop: {showmsg`stop...;ctpmdstarted::0b;ctpqdisc[`;.z.P];}; //停止 stop[] 56 | 57 | showmsg(`$"start within 08:50 15:00 or 20:50 23:30,stop within 15:10 15:15 or 02:40 02:45. start[]/stop[]:start/stop now."); 58 | ctpmdstarted:0b; 59 | .z.ts:{ 60 | if[(mod[.z.D;7]>1)&(not ctpmdstarted) & ( (.z.T within 08:50 15:00) | (.z.T within 20:50 23:30));start[]]; 61 | if[ctpmdstarted&((.z.T within 15:10 15:15)|(.z.T within 02:40 02:45));stop[]]; 62 | }; 63 | \t 2000 64 | 65 | -------------------------------------------------------------------------------- /q/tick/csmd.q: -------------------------------------------------------------------------------- 1 | /本脚本仅供学习之用。 2 | 3 | /要订阅的证券代码(Wind格式);数量不能太多,否则可能被服务器禁用 4 | codes: `000001.SH`600036.SH`000001.SZ`399001.SZ; 5 | 6 | /sina代码格式转换:`shxxxxxx => `xxxxxx.SH, `szxxxxxx=>`xxxxxx.SZ : sinacode2sym[`sh600036] sinacode2sym[`sh000001] 7 | sinacode2sym:{`$$["sh"~2#sx:string x;(2_sx),".SH";"sz"~2#sx;(2_sx),".SZ";sx]}; 8 | 9 | /sina代码格式转换:`xxxxxx.SH => `shxxxxxx, `xxxxxx.SZ => `szxxxxxx : sym2sinacode[`000001.SH] sym2sinacode[`000001.SZ] 10 | sym2sinacode:{`$$[".SH"~-3#sx:string x;"sh",-3_sx;".SZ"~-3#sx;"sz",-3_sx;sx]}; 11 | 12 | /连接到sina websocket:如果采用wss协议则需要设置环境变量set SSL_VERIFY_SERVER=NO且存在libeay32.dll/ssleay32.dll 13 | conn2ws:{[codes]:(`$":wss://hq.sinajs.cn")"GET /wskt?list=",("," sv string sym2sinacode each codes)," HTTP/1.1\r\nHost: hq.sinajs.cn\r\n\r\n";}; 14 | 15 | /将接收到的字符串解析为表 16 | str2tbl:{select date,`timespan$time,{sinacode2sym`$vs["=";string x]0}each sym,prevclose,open,high,low,close,volume,amount,bid,bsize,ask,asize from 17 | flip ( `sym`open`prevclose`close`high`low`bid0`ask0`volume`amount`bsize`bid`bsize2`bid2`bsize3`bid3`bsize4`bid4`bsize5`bid5`asize`ask`asize2`ask2`asize3`ask3`asize4`ask4`asize5`ask5`date`time)!("S",(29#"F"),"DT";",")0: "\n" vs -1 _ x}; 18 | 19 | /定义行情表(主键表),用于保存最新行情 20 | cstaq:([sym:`$()]date:`date$();time:`timespan$();prevclose:`float$();open:`float$();high:`float$();low:`float$();close:`float$();volume:`float$();amount:`float$();bid:`float$();bsize:`float$();ask:`float$();asize:`float$()); 21 | 22 | /连接tickerplant 23 | h:hopen `::5010; 24 | 25 | /定议.z.ws函数,处理websocket返回的数据 26 | .z.ws:{`cstaq upsert t:str2tbl x; 27 | {neg[h](`.u.upd;`cstaq;value x)}each 28 | delete date from select from t where date=.z.D;}; 29 | 30 | /连接websocket 31 | wsh:conn2ws[codes]; 32 | 33 | /定时器,发送心跳 34 | .z.ts:{neg[wsh 0] "";}; 35 | system "t 10000"; 36 | -------------------------------------------------------------------------------- /q/tick/r.q: -------------------------------------------------------------------------------- 1 | /q tick/r.q [host]:port[:usr:pwd] [host]:port[:usr:pwd] 2 | /2008.09.09 .k ->.q 3 | 4 | if[not "w"=first string .z.o;system "sleep 1"]; 5 | 6 | upd:insert; 7 | 8 | / get the ticker plant and history ports, defaults are 5010,5012 9 | .u.x:.z.x,(count .z.x)_(":5010";":5012"); 10 | 11 | / end of day: save, clear, hdb reload 12 | .u.end:{t:tables`.;t@:where `g=attr each t@\:`sym;.Q.hdpf[`$":",.u.x 1;`:./hdb;x;`sym];@[;`sym;`g#] each t;}; 13 | 14 | / init schema and sync up from log file;cd to hdb(so client save can run) 15 | .u.rep:{(.[;();:;].)each x;if[null first y;:()];-11!y;system "cd ",1_-10_string first reverse y}; 16 | / HARDCODE \cd if other than logdir/db 17 | 18 | / connect to ticker plant for (schema;(logcount;log)) 19 | .u.rep .(hopen `$":",.u.x 0)"(.u.sub[`;`];`.u `i`L)"; 20 | 21 | -------------------------------------------------------------------------------- /q/tick/sym.q: -------------------------------------------------------------------------------- 1 | //trade & quote 2 | cstaq:([]time:`timespan$();sym:`g#`symbol$();prevclose:`float$();open:`float$();high:`float$();low:`float$();close:`float$();volume:`float$();amount:`float$();bid:`float$();bsize:`float$();ask:`float$();asize:`float$()); 3 | 4 | //cftaq 5 | cftaq:([]time:`timespan$();sym:`g#`symbol$();prevclose:`float$();open:`float$();high:`float$();low:`float$();close:`float$();volume:`float$();amount:`float$();openint:`float$();bid:`float$();bsize:`float$();ask:`float$();asize:`float$();upperlimit:`float$();lowerlimit:`float$()); 6 | 7 | //msg 8 | msg:([]time:`timespan$();sym:`g#`symbol$();src:`int$();data:()); -------------------------------------------------------------------------------- /q/tick/tick.q: -------------------------------------------------------------------------------- 1 | / q tick.q sym . -p 5001 foo 2>&1 & 2 | /2014.03.12 remove license check 3 | /2013.09.05 warn on corrupt log 4 | /2013.08.14 allow when -u is set 5 | /2012.11.09 use timestamp type rather than time. -19h/"t"/.z.Z -> -16h/"n"/.z.P 6 | /2011.02.10 i->i,j to avoid duplicate data if subscription whilst data in buffer 7 | /2009.07.30 ts day (and "d"$a instead of floor a) 8 | /2008.09.09 .k -> .q, 2.4 9 | /2008.02.03 tick/r.k allow no log 10 | /2007.09.03 check one day flip 11 | /2006.10.18 check type? 12 | /2006.07.24 pub then log 13 | /2006.02.09 fix(2005.11.28) .z.ts end-of-day 14 | /2006.01.05 @[;`sym;`g#] in tick.k load 15 | /2005.12.21 tick/r.k reset `g#sym 16 | /2005.12.11 feed can send .u.endofday 17 | /2005.11.28 zero-end-of-day 18 | /2005.10.28 allow`time on incoming 19 | /2005.10.10 zero latency 20 | "kdb+tick 2.8 2014.03.12" 21 | 22 | /q tick.q SRC [DST] [-p 5010] [-o h] 23 | system"l tick/",(src:first .z.x,enlist"sym"),".q" 24 | 25 | if[not system"p";system"p 5010"] 26 | 27 | \l tick/u.q 28 | \d .u 29 | ld:{if[not type key L::`$(-10_string L),string x;.[L;();:;()]];i::j::-11!(-2;L);if[0<=type i;-2 (string L)," is a corrupt log. Truncate to length ",(string last i)," and restart";exit 1];hopen L}; 30 | tick:{init[];if[not min(`time`sym~2#key flip value@)each t;'`timesym];@[;`sym;`g#]each t;d::.z.D;if[l::count y;L::`$":",y,"/",x,10#".";l::ld d]}; 31 | 32 | endofday:{end d;d+:1;if[l;hclose l;l::0(`.u.ld;d)]}; 33 | ts:{if[dtype first x;a,x;(enlist(count first x)#a),x]]; 39 | t insert x;if[l;l enlist (`upd;t;x);j+:1];}]; 40 | 41 | if[not system"t";system"t 1000"; 42 | .z.ts:{ts .z.D}; 43 | upd:{[t;x]ts"d"$a:.z.P; 44 | if[not -16=type first first x;a:"n"$a;x:$[0>type first x;a,x;(enlist(count first x)#a),x]]; 45 | f:key flip value t;pub[t;$[0>type first x;enlist f!x;flip f!x]];if[l;l enlist (`upd;t;x);i+:1];}]; 46 | 47 | \d . 48 | .u.tick[src;.z.x 1]; 49 | 50 | \ 51 | globals used 52 | .u.w - dictionary of tables->(handle;syms) 53 | .u.i - msg count in log file 54 | .u.j - total msg count (log file plus those held in buffer) 55 | .u.t - table names 56 | .u.L - tp log filename, e.g. `:./sym2008.09.11 57 | .u.l - handle to tp log file 58 | .u.d - date 59 | 60 | /test 61 | >q tick.q 62 | >q tick/ssl.q 63 | 64 | /run 65 | >q tick.q sym . -p 5010 /tick 66 | >q tick/r.q :5010 -p 5011 /rdb 67 | >q sym -p 5012 /hdb 68 | >q tick/ssl.q sym :5010 /feed 69 | -------------------------------------------------------------------------------- /q/tick/u.q: -------------------------------------------------------------------------------- 1 | /2019.06.17 ensure sym has g attr for schema returned to new subscriber 2 | /2008.09.09 .k -> .q 3 | /2006.05.08 add 4 | 5 | \d .u 6 | init:{w::t!(count t::tables`.)#()} 7 | 8 | del:{w[x]_:w[x;;0]?y};.z.pc:{del[;x]each t}; 9 | 10 | sel:{$[`~y;x;select from x where sym in y]} 11 | 12 | pub:{[t;x]{[t;x;w]if[count x:sel[x]w 1;(neg first w)(`upd;t;x)]}[t;x]each w t} 13 | 14 | add:{$[(count w x)>i:w[x;;0]?.z.w;.[`.u.w;(x;i;1);union;y];w[x],:enlist(.z.w;y)];(x;$[99=type v:value x;sel[v]y;@[0#v;`sym;`g#]])} 15 | 16 | sub:{if[x~`;:sub[;y]each t];if[not x in t;'x];del[x].z.w;add[x;y]} 17 | 18 | end:{(neg union/[w[;;0]])@\:(`.u.end;x)} 19 | -------------------------------------------------------------------------------- /q/w32/libeay32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/libeay32.dll -------------------------------------------------------------------------------- /q/w32/liblz4.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/liblz4.dll -------------------------------------------------------------------------------- /q/w32/msvcp140.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/msvcp140.dll -------------------------------------------------------------------------------- /q/w32/q.exe___download from kx .txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/q.exe___download from kx .txt -------------------------------------------------------------------------------- /q/w32/qctpmd.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/qctpmd.dll -------------------------------------------------------------------------------- /q/w32/qx.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/qx.dll -------------------------------------------------------------------------------- /q/w32/ssleay32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/ssleay32.dll -------------------------------------------------------------------------------- /q/w32/thostmduserapi_se.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/thostmduserapi_se.dll -------------------------------------------------------------------------------- /q/w32/vcruntime140.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w32/vcruntime140.dll -------------------------------------------------------------------------------- /q/w64/libeay32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/libeay32.dll -------------------------------------------------------------------------------- /q/w64/liblz4.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/liblz4.dll -------------------------------------------------------------------------------- /q/w64/msvcp140.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/msvcp140.dll -------------------------------------------------------------------------------- /q/w64/q.exe___download from kx .txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/q.exe___download from kx .txt -------------------------------------------------------------------------------- /q/w64/qctpmd.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/qctpmd.dll -------------------------------------------------------------------------------- /q/w64/qx.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/qx.dll -------------------------------------------------------------------------------- /q/w64/ssleay32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/ssleay32.dll -------------------------------------------------------------------------------- /q/w64/thostmduserapi_se.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/thostmduserapi_se.dll -------------------------------------------------------------------------------- /q/w64/vcruntime140.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q/w64/vcruntime140.dll -------------------------------------------------------------------------------- /q64.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/q64.bat -------------------------------------------------------------------------------- /tick.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdbcn/kdb/b6b47863202423cfd0b2c678d9a467c8a81ba817/tick.bat --------------------------------------------------------------------------------