pyswordfish 3.0.4.0__cp312-cp312-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyswordfish-3.0.4.0.dist-info/METADATA +53 -0
- pyswordfish-3.0.4.0.dist-info/RECORD +644 -0
- pyswordfish-3.0.4.0.dist-info/WHEEL +5 -0
- pyswordfish-3.0.4.0.dist-info/top_level.txt +1 -0
- swordfish/__init__.py +102 -0
- swordfish/_config.py +901 -0
- swordfish/_connection.py +1418 -0
- swordfish/_core.py +17 -0
- swordfish/_engine.py +1456 -0
- swordfish/_enums.py +142 -0
- swordfish/_function_bindings.py +23594 -0
- swordfish/_function_tools.py +124 -0
- swordfish/_helper.py +57 -0
- swordfish/_runtime.py +158 -0
- swordfish/_sqlbuilder.py +549 -0
- swordfish/_streaming.py +333 -0
- swordfish/_swordfishcpp.cp312-win_amd64.pyd +0 -0
- swordfish/_swordfishcpp.pyi +4784 -0
- swordfish/_translator.py +580 -0
- swordfish/asset/dolphindb.dos +71 -0
- swordfish/asset/tzdb/Africa/Abidjan +0 -0
- swordfish/asset/tzdb/Africa/Accra +0 -0
- swordfish/asset/tzdb/Africa/Addis_Ababa +0 -0
- swordfish/asset/tzdb/Africa/Algiers +0 -0
- swordfish/asset/tzdb/Africa/Asmara +0 -0
- swordfish/asset/tzdb/Africa/Asmera +0 -0
- swordfish/asset/tzdb/Africa/Bamako +0 -0
- swordfish/asset/tzdb/Africa/Bangui +0 -0
- swordfish/asset/tzdb/Africa/Banjul +0 -0
- swordfish/asset/tzdb/Africa/Bissau +0 -0
- swordfish/asset/tzdb/Africa/Blantyre +0 -0
- swordfish/asset/tzdb/Africa/Brazzaville +0 -0
- swordfish/asset/tzdb/Africa/Bujumbura +0 -0
- swordfish/asset/tzdb/Africa/Cairo +0 -0
- swordfish/asset/tzdb/Africa/Casablanca +0 -0
- swordfish/asset/tzdb/Africa/Ceuta +0 -0
- swordfish/asset/tzdb/Africa/Conakry +0 -0
- swordfish/asset/tzdb/Africa/Dakar +0 -0
- swordfish/asset/tzdb/Africa/Dar_es_Salaam +0 -0
- swordfish/asset/tzdb/Africa/Djibouti +0 -0
- swordfish/asset/tzdb/Africa/Douala +0 -0
- swordfish/asset/tzdb/Africa/El_Aaiun +0 -0
- swordfish/asset/tzdb/Africa/Freetown +0 -0
- swordfish/asset/tzdb/Africa/Gaborone +0 -0
- swordfish/asset/tzdb/Africa/Harare +0 -0
- swordfish/asset/tzdb/Africa/Johannesburg +0 -0
- swordfish/asset/tzdb/Africa/Juba +0 -0
- swordfish/asset/tzdb/Africa/Kampala +0 -0
- swordfish/asset/tzdb/Africa/Khartoum +0 -0
- swordfish/asset/tzdb/Africa/Kigali +0 -0
- swordfish/asset/tzdb/Africa/Kinshasa +0 -0
- swordfish/asset/tzdb/Africa/Lagos +0 -0
- swordfish/asset/tzdb/Africa/Libreville +0 -0
- swordfish/asset/tzdb/Africa/Lome +0 -0
- swordfish/asset/tzdb/Africa/Luanda +0 -0
- swordfish/asset/tzdb/Africa/Lubumbashi +0 -0
- swordfish/asset/tzdb/Africa/Lusaka +0 -0
- swordfish/asset/tzdb/Africa/Malabo +0 -0
- swordfish/asset/tzdb/Africa/Maputo +0 -0
- swordfish/asset/tzdb/Africa/Maseru +0 -0
- swordfish/asset/tzdb/Africa/Mbabane +0 -0
- swordfish/asset/tzdb/Africa/Mogadishu +0 -0
- swordfish/asset/tzdb/Africa/Monrovia +0 -0
- swordfish/asset/tzdb/Africa/Nairobi +0 -0
- swordfish/asset/tzdb/Africa/Ndjamena +0 -0
- swordfish/asset/tzdb/Africa/Niamey +0 -0
- swordfish/asset/tzdb/Africa/Nouakchott +0 -0
- swordfish/asset/tzdb/Africa/Ouagadougou +0 -0
- swordfish/asset/tzdb/Africa/Porto-Novo +0 -0
- swordfish/asset/tzdb/Africa/Sao_Tome +0 -0
- swordfish/asset/tzdb/Africa/Timbuktu +0 -0
- swordfish/asset/tzdb/Africa/Tripoli +0 -0
- swordfish/asset/tzdb/Africa/Tunis +0 -0
- swordfish/asset/tzdb/Africa/Windhoek +0 -0
- swordfish/asset/tzdb/America/Adak +0 -0
- swordfish/asset/tzdb/America/Anchorage +0 -0
- swordfish/asset/tzdb/America/Anguilla +0 -0
- swordfish/asset/tzdb/America/Antigua +0 -0
- swordfish/asset/tzdb/America/Araguaina +0 -0
- swordfish/asset/tzdb/America/Argentina/Buenos_Aires +0 -0
- swordfish/asset/tzdb/America/Argentina/Catamarca +0 -0
- swordfish/asset/tzdb/America/Argentina/ComodRivadavia +0 -0
- swordfish/asset/tzdb/America/Argentina/Cordoba +0 -0
- swordfish/asset/tzdb/America/Argentina/Jujuy +0 -0
- swordfish/asset/tzdb/America/Argentina/La_Rioja +0 -0
- swordfish/asset/tzdb/America/Argentina/Mendoza +0 -0
- swordfish/asset/tzdb/America/Argentina/Rio_Gallegos +0 -0
- swordfish/asset/tzdb/America/Argentina/Salta +0 -0
- swordfish/asset/tzdb/America/Argentina/San_Juan +0 -0
- swordfish/asset/tzdb/America/Argentina/San_Luis +0 -0
- swordfish/asset/tzdb/America/Argentina/Tucuman +0 -0
- swordfish/asset/tzdb/America/Argentina/Ushuaia +0 -0
- swordfish/asset/tzdb/America/Aruba +0 -0
- swordfish/asset/tzdb/America/Asuncion +0 -0
- swordfish/asset/tzdb/America/Atikokan +0 -0
- swordfish/asset/tzdb/America/Atka +0 -0
- swordfish/asset/tzdb/America/Bahia +0 -0
- swordfish/asset/tzdb/America/Bahia_Banderas +0 -0
- swordfish/asset/tzdb/America/Barbados +0 -0
- swordfish/asset/tzdb/America/Belem +0 -0
- swordfish/asset/tzdb/America/Belize +0 -0
- swordfish/asset/tzdb/America/Blanc-Sablon +0 -0
- swordfish/asset/tzdb/America/Boa_Vista +0 -0
- swordfish/asset/tzdb/America/Bogota +0 -0
- swordfish/asset/tzdb/America/Boise +0 -0
- swordfish/asset/tzdb/America/Buenos_Aires +0 -0
- swordfish/asset/tzdb/America/Cambridge_Bay +0 -0
- swordfish/asset/tzdb/America/Campo_Grande +0 -0
- swordfish/asset/tzdb/America/Cancun +0 -0
- swordfish/asset/tzdb/America/Caracas +0 -0
- swordfish/asset/tzdb/America/Catamarca +0 -0
- swordfish/asset/tzdb/America/Cayenne +0 -0
- swordfish/asset/tzdb/America/Cayman +0 -0
- swordfish/asset/tzdb/America/Chicago +0 -0
- swordfish/asset/tzdb/America/Chihuahua +0 -0
- swordfish/asset/tzdb/America/Coral_Harbour +0 -0
- swordfish/asset/tzdb/America/Cordoba +0 -0
- swordfish/asset/tzdb/America/Costa_Rica +0 -0
- swordfish/asset/tzdb/America/Creston +0 -0
- swordfish/asset/tzdb/America/Cuiaba +0 -0
- swordfish/asset/tzdb/America/Curacao +0 -0
- swordfish/asset/tzdb/America/Danmarkshavn +0 -0
- swordfish/asset/tzdb/America/Dawson +0 -0
- swordfish/asset/tzdb/America/Dawson_Creek +0 -0
- swordfish/asset/tzdb/America/Denver +0 -0
- swordfish/asset/tzdb/America/Detroit +0 -0
- swordfish/asset/tzdb/America/Dominica +0 -0
- swordfish/asset/tzdb/America/Edmonton +0 -0
- swordfish/asset/tzdb/America/Eirunepe +0 -0
- swordfish/asset/tzdb/America/El_Salvador +0 -0
- swordfish/asset/tzdb/America/Ensenada +0 -0
- swordfish/asset/tzdb/America/Fort_Nelson +0 -0
- swordfish/asset/tzdb/America/Fort_Wayne +0 -0
- swordfish/asset/tzdb/America/Fortaleza +0 -0
- swordfish/asset/tzdb/America/Glace_Bay +0 -0
- swordfish/asset/tzdb/America/Godthab +0 -0
- swordfish/asset/tzdb/America/Goose_Bay +0 -0
- swordfish/asset/tzdb/America/Grand_Turk +0 -0
- swordfish/asset/tzdb/America/Grenada +0 -0
- swordfish/asset/tzdb/America/Guadeloupe +0 -0
- swordfish/asset/tzdb/America/Guatemala +0 -0
- swordfish/asset/tzdb/America/Guayaquil +0 -0
- swordfish/asset/tzdb/America/Guyana +0 -0
- swordfish/asset/tzdb/America/Halifax +0 -0
- swordfish/asset/tzdb/America/Havana +0 -0
- swordfish/asset/tzdb/America/Hermosillo +0 -0
- swordfish/asset/tzdb/America/Indiana/Indianapolis +0 -0
- swordfish/asset/tzdb/America/Indiana/Knox +0 -0
- swordfish/asset/tzdb/America/Indiana/Marengo +0 -0
- swordfish/asset/tzdb/America/Indiana/Petersburg +0 -0
- swordfish/asset/tzdb/America/Indiana/Tell_City +0 -0
- swordfish/asset/tzdb/America/Indiana/Vevay +0 -0
- swordfish/asset/tzdb/America/Indiana/Vincennes +0 -0
- swordfish/asset/tzdb/America/Indiana/Winamac +0 -0
- swordfish/asset/tzdb/America/Indianapolis +0 -0
- swordfish/asset/tzdb/America/Inuvik +0 -0
- swordfish/asset/tzdb/America/Iqaluit +0 -0
- swordfish/asset/tzdb/America/Jamaica +0 -0
- swordfish/asset/tzdb/America/Jujuy +0 -0
- swordfish/asset/tzdb/America/Juneau +0 -0
- swordfish/asset/tzdb/America/Kentucky/Louisville +0 -0
- swordfish/asset/tzdb/America/Kentucky/Monticello +0 -0
- swordfish/asset/tzdb/America/Knox_IN +0 -0
- swordfish/asset/tzdb/America/Kralendijk +0 -0
- swordfish/asset/tzdb/America/La_Paz +0 -0
- swordfish/asset/tzdb/America/Lima +0 -0
- swordfish/asset/tzdb/America/Los_Angeles +0 -0
- swordfish/asset/tzdb/America/Louisville +0 -0
- swordfish/asset/tzdb/America/Lower_Princes +0 -0
- swordfish/asset/tzdb/America/Maceio +0 -0
- swordfish/asset/tzdb/America/Managua +0 -0
- swordfish/asset/tzdb/America/Manaus +0 -0
- swordfish/asset/tzdb/America/Marigot +0 -0
- swordfish/asset/tzdb/America/Martinique +0 -0
- swordfish/asset/tzdb/America/Matamoros +0 -0
- swordfish/asset/tzdb/America/Mazatlan +0 -0
- swordfish/asset/tzdb/America/Mendoza +0 -0
- swordfish/asset/tzdb/America/Menominee +0 -0
- swordfish/asset/tzdb/America/Merida +0 -0
- swordfish/asset/tzdb/America/Metlakatla +0 -0
- swordfish/asset/tzdb/America/Mexico_City +0 -0
- swordfish/asset/tzdb/America/Miquelon +0 -0
- swordfish/asset/tzdb/America/Moncton +0 -0
- swordfish/asset/tzdb/America/Monterrey +0 -0
- swordfish/asset/tzdb/America/Montevideo +0 -0
- swordfish/asset/tzdb/America/Montreal +0 -0
- swordfish/asset/tzdb/America/Montserrat +0 -0
- swordfish/asset/tzdb/America/Nassau +0 -0
- swordfish/asset/tzdb/America/New_York +0 -0
- swordfish/asset/tzdb/America/Nipigon +0 -0
- swordfish/asset/tzdb/America/Nome +0 -0
- swordfish/asset/tzdb/America/Noronha +0 -0
- swordfish/asset/tzdb/America/North_Dakota/Beulah +0 -0
- swordfish/asset/tzdb/America/North_Dakota/Center +0 -0
- swordfish/asset/tzdb/America/North_Dakota/New_Salem +0 -0
- swordfish/asset/tzdb/America/Ojinaga +0 -0
- swordfish/asset/tzdb/America/Panama +0 -0
- swordfish/asset/tzdb/America/Pangnirtung +0 -0
- swordfish/asset/tzdb/America/Paramaribo +0 -0
- swordfish/asset/tzdb/America/Phoenix +0 -0
- swordfish/asset/tzdb/America/Port-au-Prince +0 -0
- swordfish/asset/tzdb/America/Port_of_Spain +0 -0
- swordfish/asset/tzdb/America/Porto_Acre +0 -0
- swordfish/asset/tzdb/America/Porto_Velho +0 -0
- swordfish/asset/tzdb/America/Puerto_Rico +0 -0
- swordfish/asset/tzdb/America/Punta_Arenas +0 -0
- swordfish/asset/tzdb/America/Rainy_River +0 -0
- swordfish/asset/tzdb/America/Rankin_Inlet +0 -0
- swordfish/asset/tzdb/America/Recife +0 -0
- swordfish/asset/tzdb/America/Regina +0 -0
- swordfish/asset/tzdb/America/Resolute +0 -0
- swordfish/asset/tzdb/America/Rio_Branco +0 -0
- swordfish/asset/tzdb/America/Rosario +0 -0
- swordfish/asset/tzdb/America/Santa_Isabel +0 -0
- swordfish/asset/tzdb/America/Santarem +0 -0
- swordfish/asset/tzdb/America/Santiago +0 -0
- swordfish/asset/tzdb/America/Santo_Domingo +0 -0
- swordfish/asset/tzdb/America/Sao_Paulo +0 -0
- swordfish/asset/tzdb/America/Scoresbysund +0 -0
- swordfish/asset/tzdb/America/Shiprock +0 -0
- swordfish/asset/tzdb/America/Sitka +0 -0
- swordfish/asset/tzdb/America/St_Barthelemy +0 -0
- swordfish/asset/tzdb/America/St_Johns +0 -0
- swordfish/asset/tzdb/America/St_Kitts +0 -0
- swordfish/asset/tzdb/America/St_Lucia +0 -0
- swordfish/asset/tzdb/America/St_Thomas +0 -0
- swordfish/asset/tzdb/America/St_Vincent +0 -0
- swordfish/asset/tzdb/America/Swift_Current +0 -0
- swordfish/asset/tzdb/America/Tegucigalpa +0 -0
- swordfish/asset/tzdb/America/Thule +0 -0
- swordfish/asset/tzdb/America/Thunder_Bay +0 -0
- swordfish/asset/tzdb/America/Tijuana +0 -0
- swordfish/asset/tzdb/America/Toronto +0 -0
- swordfish/asset/tzdb/America/Tortola +0 -0
- swordfish/asset/tzdb/America/Vancouver +0 -0
- swordfish/asset/tzdb/America/Virgin +0 -0
- swordfish/asset/tzdb/America/Whitehorse +0 -0
- swordfish/asset/tzdb/America/Winnipeg +0 -0
- swordfish/asset/tzdb/America/Yakutat +0 -0
- swordfish/asset/tzdb/America/Yellowknife +0 -0
- swordfish/asset/tzdb/Antarctica/Casey +0 -0
- swordfish/asset/tzdb/Antarctica/Davis +0 -0
- swordfish/asset/tzdb/Antarctica/DumontDUrville +0 -0
- swordfish/asset/tzdb/Antarctica/Macquarie +0 -0
- swordfish/asset/tzdb/Antarctica/Mawson +0 -0
- swordfish/asset/tzdb/Antarctica/McMurdo +0 -0
- swordfish/asset/tzdb/Antarctica/Palmer +0 -0
- swordfish/asset/tzdb/Antarctica/Rothera +0 -0
- swordfish/asset/tzdb/Antarctica/South_Pole +0 -0
- swordfish/asset/tzdb/Antarctica/Syowa +0 -0
- swordfish/asset/tzdb/Antarctica/Troll +0 -0
- swordfish/asset/tzdb/Antarctica/Vostok +0 -0
- swordfish/asset/tzdb/Arctic/Longyearbyen +0 -0
- swordfish/asset/tzdb/Asia/Aden +0 -0
- swordfish/asset/tzdb/Asia/Almaty +0 -0
- swordfish/asset/tzdb/Asia/Amman +0 -0
- swordfish/asset/tzdb/Asia/Anadyr +0 -0
- swordfish/asset/tzdb/Asia/Aqtau +0 -0
- swordfish/asset/tzdb/Asia/Aqtobe +0 -0
- swordfish/asset/tzdb/Asia/Ashgabat +0 -0
- swordfish/asset/tzdb/Asia/Ashkhabad +0 -0
- swordfish/asset/tzdb/Asia/Atyrau +0 -0
- swordfish/asset/tzdb/Asia/Baghdad +0 -0
- swordfish/asset/tzdb/Asia/Bahrain +0 -0
- swordfish/asset/tzdb/Asia/Baku +0 -0
- swordfish/asset/tzdb/Asia/Bangkok +0 -0
- swordfish/asset/tzdb/Asia/Barnaul +0 -0
- swordfish/asset/tzdb/Asia/Beirut +0 -0
- swordfish/asset/tzdb/Asia/Bishkek +0 -0
- swordfish/asset/tzdb/Asia/Brunei +0 -0
- swordfish/asset/tzdb/Asia/Calcutta +0 -0
- swordfish/asset/tzdb/Asia/Chita +0 -0
- swordfish/asset/tzdb/Asia/Choibalsan +0 -0
- swordfish/asset/tzdb/Asia/Chongqing +0 -0
- swordfish/asset/tzdb/Asia/Chungking +0 -0
- swordfish/asset/tzdb/Asia/Colombo +0 -0
- swordfish/asset/tzdb/Asia/Dacca +0 -0
- swordfish/asset/tzdb/Asia/Damascus +0 -0
- swordfish/asset/tzdb/Asia/Dhaka +0 -0
- swordfish/asset/tzdb/Asia/Dili +0 -0
- swordfish/asset/tzdb/Asia/Dubai +0 -0
- swordfish/asset/tzdb/Asia/Dushanbe +0 -0
- swordfish/asset/tzdb/Asia/Famagusta +0 -0
- swordfish/asset/tzdb/Asia/Gaza +0 -0
- swordfish/asset/tzdb/Asia/Harbin +0 -0
- swordfish/asset/tzdb/Asia/Hebron +0 -0
- swordfish/asset/tzdb/Asia/Ho_Chi_Minh +0 -0
- swordfish/asset/tzdb/Asia/Hong_Kong +0 -0
- swordfish/asset/tzdb/Asia/Hovd +0 -0
- swordfish/asset/tzdb/Asia/Irkutsk +0 -0
- swordfish/asset/tzdb/Asia/Istanbul +0 -0
- swordfish/asset/tzdb/Asia/Jakarta +0 -0
- swordfish/asset/tzdb/Asia/Jayapura +0 -0
- swordfish/asset/tzdb/Asia/Jerusalem +0 -0
- swordfish/asset/tzdb/Asia/Kabul +0 -0
- swordfish/asset/tzdb/Asia/Kamchatka +0 -0
- swordfish/asset/tzdb/Asia/Karachi +0 -0
- swordfish/asset/tzdb/Asia/Kashgar +0 -0
- swordfish/asset/tzdb/Asia/Kathmandu +0 -0
- swordfish/asset/tzdb/Asia/Katmandu +0 -0
- swordfish/asset/tzdb/Asia/Khandyga +0 -0
- swordfish/asset/tzdb/Asia/Kolkata +0 -0
- swordfish/asset/tzdb/Asia/Krasnoyarsk +0 -0
- swordfish/asset/tzdb/Asia/Kuala_Lumpur +0 -0
- swordfish/asset/tzdb/Asia/Kuching +0 -0
- swordfish/asset/tzdb/Asia/Kuwait +0 -0
- swordfish/asset/tzdb/Asia/Macao +0 -0
- swordfish/asset/tzdb/Asia/Macau +0 -0
- swordfish/asset/tzdb/Asia/Magadan +0 -0
- swordfish/asset/tzdb/Asia/Makassar +0 -0
- swordfish/asset/tzdb/Asia/Manila +0 -0
- swordfish/asset/tzdb/Asia/Muscat +0 -0
- swordfish/asset/tzdb/Asia/Nicosia +0 -0
- swordfish/asset/tzdb/Asia/Novokuznetsk +0 -0
- swordfish/asset/tzdb/Asia/Novosibirsk +0 -0
- swordfish/asset/tzdb/Asia/Omsk +0 -0
- swordfish/asset/tzdb/Asia/Oral +0 -0
- swordfish/asset/tzdb/Asia/Phnom_Penh +0 -0
- swordfish/asset/tzdb/Asia/Pontianak +0 -0
- swordfish/asset/tzdb/Asia/Pyongyang +0 -0
- swordfish/asset/tzdb/Asia/Qatar +0 -0
- swordfish/asset/tzdb/Asia/Qyzylorda +0 -0
- swordfish/asset/tzdb/Asia/Rangoon +0 -0
- swordfish/asset/tzdb/Asia/Riyadh +0 -0
- swordfish/asset/tzdb/Asia/Saigon +0 -0
- swordfish/asset/tzdb/Asia/Sakhalin +0 -0
- swordfish/asset/tzdb/Asia/Samarkand +0 -0
- swordfish/asset/tzdb/Asia/Seoul +0 -0
- swordfish/asset/tzdb/Asia/Shanghai +0 -0
- swordfish/asset/tzdb/Asia/Singapore +0 -0
- swordfish/asset/tzdb/Asia/Srednekolymsk +0 -0
- swordfish/asset/tzdb/Asia/Taipei +0 -0
- swordfish/asset/tzdb/Asia/Tashkent +0 -0
- swordfish/asset/tzdb/Asia/Tbilisi +0 -0
- swordfish/asset/tzdb/Asia/Tehran +0 -0
- swordfish/asset/tzdb/Asia/Tel_Aviv +0 -0
- swordfish/asset/tzdb/Asia/Thimbu +0 -0
- swordfish/asset/tzdb/Asia/Thimphu +0 -0
- swordfish/asset/tzdb/Asia/Tokyo +0 -0
- swordfish/asset/tzdb/Asia/Tomsk +0 -0
- swordfish/asset/tzdb/Asia/Ujung_Pandang +0 -0
- swordfish/asset/tzdb/Asia/Ulaanbaatar +0 -0
- swordfish/asset/tzdb/Asia/Ulan_Bator +0 -0
- swordfish/asset/tzdb/Asia/Urumqi +0 -0
- swordfish/asset/tzdb/Asia/Ust-Nera +0 -0
- swordfish/asset/tzdb/Asia/Vientiane +0 -0
- swordfish/asset/tzdb/Asia/Vladivostok +0 -0
- swordfish/asset/tzdb/Asia/Yakutsk +0 -0
- swordfish/asset/tzdb/Asia/Yangon +0 -0
- swordfish/asset/tzdb/Asia/Yekaterinburg +0 -0
- swordfish/asset/tzdb/Asia/Yerevan +0 -0
- swordfish/asset/tzdb/Atlantic/Azores +0 -0
- swordfish/asset/tzdb/Atlantic/Bermuda +0 -0
- swordfish/asset/tzdb/Atlantic/Canary +0 -0
- swordfish/asset/tzdb/Atlantic/Cape_Verde +0 -0
- swordfish/asset/tzdb/Atlantic/Faeroe +0 -0
- swordfish/asset/tzdb/Atlantic/Faroe +0 -0
- swordfish/asset/tzdb/Atlantic/Jan_Mayen +0 -0
- swordfish/asset/tzdb/Atlantic/Madeira +0 -0
- swordfish/asset/tzdb/Atlantic/Reykjavik +0 -0
- swordfish/asset/tzdb/Atlantic/South_Georgia +0 -0
- swordfish/asset/tzdb/Atlantic/St_Helena +0 -0
- swordfish/asset/tzdb/Atlantic/Stanley +0 -0
- swordfish/asset/tzdb/Australia/ACT +0 -0
- swordfish/asset/tzdb/Australia/Adelaide +0 -0
- swordfish/asset/tzdb/Australia/Brisbane +0 -0
- swordfish/asset/tzdb/Australia/Broken_Hill +0 -0
- swordfish/asset/tzdb/Australia/Canberra +0 -0
- swordfish/asset/tzdb/Australia/Currie +0 -0
- swordfish/asset/tzdb/Australia/Darwin +0 -0
- swordfish/asset/tzdb/Australia/Eucla +0 -0
- swordfish/asset/tzdb/Australia/Hobart +0 -0
- swordfish/asset/tzdb/Australia/LHI +0 -0
- swordfish/asset/tzdb/Australia/Lindeman +0 -0
- swordfish/asset/tzdb/Australia/Lord_Howe +0 -0
- swordfish/asset/tzdb/Australia/Melbourne +0 -0
- swordfish/asset/tzdb/Australia/NSW +0 -0
- swordfish/asset/tzdb/Australia/North +0 -0
- swordfish/asset/tzdb/Australia/Perth +0 -0
- swordfish/asset/tzdb/Australia/Queensland +0 -0
- swordfish/asset/tzdb/Australia/South +0 -0
- swordfish/asset/tzdb/Australia/Sydney +0 -0
- swordfish/asset/tzdb/Australia/Tasmania +0 -0
- swordfish/asset/tzdb/Australia/Victoria +0 -0
- swordfish/asset/tzdb/Australia/West +0 -0
- swordfish/asset/tzdb/Australia/Yancowinna +0 -0
- swordfish/asset/tzdb/Brazil/Acre +0 -0
- swordfish/asset/tzdb/Brazil/DeNoronha +0 -0
- swordfish/asset/tzdb/Brazil/East +0 -0
- swordfish/asset/tzdb/Brazil/West +0 -0
- swordfish/asset/tzdb/CET +0 -0
- swordfish/asset/tzdb/CST6CDT +0 -0
- swordfish/asset/tzdb/Canada/Atlantic +0 -0
- swordfish/asset/tzdb/Canada/Central +0 -0
- swordfish/asset/tzdb/Canada/Eastern +0 -0
- swordfish/asset/tzdb/Canada/Mountain +0 -0
- swordfish/asset/tzdb/Canada/Newfoundland +0 -0
- swordfish/asset/tzdb/Canada/Pacific +0 -0
- swordfish/asset/tzdb/Canada/Saskatchewan +0 -0
- swordfish/asset/tzdb/Canada/Yukon +0 -0
- swordfish/asset/tzdb/Chile/Continental +0 -0
- swordfish/asset/tzdb/Chile/EasterIsland +0 -0
- swordfish/asset/tzdb/Cuba +0 -0
- swordfish/asset/tzdb/EET +0 -0
- swordfish/asset/tzdb/EST +0 -0
- swordfish/asset/tzdb/EST5EDT +0 -0
- swordfish/asset/tzdb/Egypt +0 -0
- swordfish/asset/tzdb/Eire +0 -0
- swordfish/asset/tzdb/Etc/GMT +0 -0
- swordfish/asset/tzdb/Etc/GMT+0 +0 -0
- swordfish/asset/tzdb/Etc/GMT+1 +0 -0
- swordfish/asset/tzdb/Etc/GMT+10 +0 -0
- swordfish/asset/tzdb/Etc/GMT+11 +0 -0
- swordfish/asset/tzdb/Etc/GMT+12 +0 -0
- swordfish/asset/tzdb/Etc/GMT+2 +0 -0
- swordfish/asset/tzdb/Etc/GMT+3 +0 -0
- swordfish/asset/tzdb/Etc/GMT+4 +0 -0
- swordfish/asset/tzdb/Etc/GMT+5 +0 -0
- swordfish/asset/tzdb/Etc/GMT+6 +0 -0
- swordfish/asset/tzdb/Etc/GMT+7 +0 -0
- swordfish/asset/tzdb/Etc/GMT+8 +0 -0
- swordfish/asset/tzdb/Etc/GMT+9 +0 -0
- swordfish/asset/tzdb/Etc/GMT-0 +0 -0
- swordfish/asset/tzdb/Etc/GMT-1 +0 -0
- swordfish/asset/tzdb/Etc/GMT-10 +0 -0
- swordfish/asset/tzdb/Etc/GMT-11 +0 -0
- swordfish/asset/tzdb/Etc/GMT-12 +0 -0
- swordfish/asset/tzdb/Etc/GMT-13 +0 -0
- swordfish/asset/tzdb/Etc/GMT-14 +0 -0
- swordfish/asset/tzdb/Etc/GMT-2 +0 -0
- swordfish/asset/tzdb/Etc/GMT-3 +0 -0
- swordfish/asset/tzdb/Etc/GMT-4 +0 -0
- swordfish/asset/tzdb/Etc/GMT-5 +0 -0
- swordfish/asset/tzdb/Etc/GMT-6 +0 -0
- swordfish/asset/tzdb/Etc/GMT-7 +0 -0
- swordfish/asset/tzdb/Etc/GMT-8 +0 -0
- swordfish/asset/tzdb/Etc/GMT-9 +0 -0
- swordfish/asset/tzdb/Etc/GMT0 +0 -0
- swordfish/asset/tzdb/Etc/Greenwich +0 -0
- swordfish/asset/tzdb/Etc/UCT +0 -0
- swordfish/asset/tzdb/Etc/UTC +0 -0
- swordfish/asset/tzdb/Etc/Universal +0 -0
- swordfish/asset/tzdb/Etc/Zulu +0 -0
- swordfish/asset/tzdb/Europe/Amsterdam +0 -0
- swordfish/asset/tzdb/Europe/Andorra +0 -0
- swordfish/asset/tzdb/Europe/Astrakhan +0 -0
- swordfish/asset/tzdb/Europe/Athens +0 -0
- swordfish/asset/tzdb/Europe/Belfast +0 -0
- swordfish/asset/tzdb/Europe/Belgrade +0 -0
- swordfish/asset/tzdb/Europe/Berlin +0 -0
- swordfish/asset/tzdb/Europe/Bratislava +0 -0
- swordfish/asset/tzdb/Europe/Brussels +0 -0
- swordfish/asset/tzdb/Europe/Bucharest +0 -0
- swordfish/asset/tzdb/Europe/Budapest +0 -0
- swordfish/asset/tzdb/Europe/Busingen +0 -0
- swordfish/asset/tzdb/Europe/Chisinau +0 -0
- swordfish/asset/tzdb/Europe/Copenhagen +0 -0
- swordfish/asset/tzdb/Europe/Dublin +0 -0
- swordfish/asset/tzdb/Europe/Gibraltar +0 -0
- swordfish/asset/tzdb/Europe/Guernsey +0 -0
- swordfish/asset/tzdb/Europe/Helsinki +0 -0
- swordfish/asset/tzdb/Europe/Isle_of_Man +0 -0
- swordfish/asset/tzdb/Europe/Istanbul +0 -0
- swordfish/asset/tzdb/Europe/Jersey +0 -0
- swordfish/asset/tzdb/Europe/Kaliningrad +0 -0
- swordfish/asset/tzdb/Europe/Kiev +0 -0
- swordfish/asset/tzdb/Europe/Kirov +0 -0
- swordfish/asset/tzdb/Europe/Lisbon +0 -0
- swordfish/asset/tzdb/Europe/Ljubljana +0 -0
- swordfish/asset/tzdb/Europe/London +0 -0
- swordfish/asset/tzdb/Europe/Luxembourg +0 -0
- swordfish/asset/tzdb/Europe/Madrid +0 -0
- swordfish/asset/tzdb/Europe/Malta +0 -0
- swordfish/asset/tzdb/Europe/Mariehamn +0 -0
- swordfish/asset/tzdb/Europe/Minsk +0 -0
- swordfish/asset/tzdb/Europe/Monaco +0 -0
- swordfish/asset/tzdb/Europe/Moscow +0 -0
- swordfish/asset/tzdb/Europe/Nicosia +0 -0
- swordfish/asset/tzdb/Europe/Oslo +0 -0
- swordfish/asset/tzdb/Europe/Paris +0 -0
- swordfish/asset/tzdb/Europe/Podgorica +0 -0
- swordfish/asset/tzdb/Europe/Prague +0 -0
- swordfish/asset/tzdb/Europe/Riga +0 -0
- swordfish/asset/tzdb/Europe/Rome +0 -0
- swordfish/asset/tzdb/Europe/Samara +0 -0
- swordfish/asset/tzdb/Europe/San_Marino +0 -0
- swordfish/asset/tzdb/Europe/Sarajevo +0 -0
- swordfish/asset/tzdb/Europe/Saratov +0 -0
- swordfish/asset/tzdb/Europe/Simferopol +0 -0
- swordfish/asset/tzdb/Europe/Skopje +0 -0
- swordfish/asset/tzdb/Europe/Sofia +0 -0
- swordfish/asset/tzdb/Europe/Stockholm +0 -0
- swordfish/asset/tzdb/Europe/Tallinn +0 -0
- swordfish/asset/tzdb/Europe/Tirane +0 -0
- swordfish/asset/tzdb/Europe/Tiraspol +0 -0
- swordfish/asset/tzdb/Europe/Ulyanovsk +0 -0
- swordfish/asset/tzdb/Europe/Uzhgorod +0 -0
- swordfish/asset/tzdb/Europe/Vaduz +0 -0
- swordfish/asset/tzdb/Europe/Vatican +0 -0
- swordfish/asset/tzdb/Europe/Vienna +0 -0
- swordfish/asset/tzdb/Europe/Vilnius +0 -0
- swordfish/asset/tzdb/Europe/Volgograd +0 -0
- swordfish/asset/tzdb/Europe/Warsaw +0 -0
- swordfish/asset/tzdb/Europe/Zagreb +0 -0
- swordfish/asset/tzdb/Europe/Zaporozhye +0 -0
- swordfish/asset/tzdb/Europe/Zurich +0 -0
- swordfish/asset/tzdb/Factory +0 -0
- swordfish/asset/tzdb/GB +0 -0
- swordfish/asset/tzdb/GB-Eire +0 -0
- swordfish/asset/tzdb/GMT +0 -0
- swordfish/asset/tzdb/GMT+0 +0 -0
- swordfish/asset/tzdb/GMT-0 +0 -0
- swordfish/asset/tzdb/GMT0 +0 -0
- swordfish/asset/tzdb/Greenwich +0 -0
- swordfish/asset/tzdb/HST +0 -0
- swordfish/asset/tzdb/Hongkong +0 -0
- swordfish/asset/tzdb/Iceland +0 -0
- swordfish/asset/tzdb/Indian/Antananarivo +0 -0
- swordfish/asset/tzdb/Indian/Chagos +0 -0
- swordfish/asset/tzdb/Indian/Christmas +0 -0
- swordfish/asset/tzdb/Indian/Cocos +0 -0
- swordfish/asset/tzdb/Indian/Comoro +0 -0
- swordfish/asset/tzdb/Indian/Kerguelen +0 -0
- swordfish/asset/tzdb/Indian/Mahe +0 -0
- swordfish/asset/tzdb/Indian/Maldives +0 -0
- swordfish/asset/tzdb/Indian/Mauritius +0 -0
- swordfish/asset/tzdb/Indian/Mayotte +0 -0
- swordfish/asset/tzdb/Indian/Reunion +0 -0
- swordfish/asset/tzdb/Iran +0 -0
- swordfish/asset/tzdb/Israel +0 -0
- swordfish/asset/tzdb/Jamaica +0 -0
- swordfish/asset/tzdb/Japan +0 -0
- swordfish/asset/tzdb/Kwajalein +0 -0
- swordfish/asset/tzdb/Libya +0 -0
- swordfish/asset/tzdb/MET +0 -0
- swordfish/asset/tzdb/MST +0 -0
- swordfish/asset/tzdb/MST7MDT +0 -0
- swordfish/asset/tzdb/Mexico/BajaNorte +0 -0
- swordfish/asset/tzdb/Mexico/BajaSur +0 -0
- swordfish/asset/tzdb/Mexico/General +0 -0
- swordfish/asset/tzdb/NZ +0 -0
- swordfish/asset/tzdb/NZ-CHAT +0 -0
- swordfish/asset/tzdb/Navajo +0 -0
- swordfish/asset/tzdb/PRC +0 -0
- swordfish/asset/tzdb/PST8PDT +0 -0
- swordfish/asset/tzdb/Pacific/Apia +0 -0
- swordfish/asset/tzdb/Pacific/Auckland +0 -0
- swordfish/asset/tzdb/Pacific/Bougainville +0 -0
- swordfish/asset/tzdb/Pacific/Chatham +0 -0
- swordfish/asset/tzdb/Pacific/Chuuk +0 -0
- swordfish/asset/tzdb/Pacific/Easter +0 -0
- swordfish/asset/tzdb/Pacific/Efate +0 -0
- swordfish/asset/tzdb/Pacific/Enderbury +0 -0
- swordfish/asset/tzdb/Pacific/Fakaofo +0 -0
- swordfish/asset/tzdb/Pacific/Fiji +0 -0
- swordfish/asset/tzdb/Pacific/Funafuti +0 -0
- swordfish/asset/tzdb/Pacific/Galapagos +0 -0
- swordfish/asset/tzdb/Pacific/Gambier +0 -0
- swordfish/asset/tzdb/Pacific/Guadalcanal +0 -0
- swordfish/asset/tzdb/Pacific/Guam +0 -0
- swordfish/asset/tzdb/Pacific/Honolulu +0 -0
- swordfish/asset/tzdb/Pacific/Johnston +0 -0
- swordfish/asset/tzdb/Pacific/Kiritimati +0 -0
- swordfish/asset/tzdb/Pacific/Kosrae +0 -0
- swordfish/asset/tzdb/Pacific/Kwajalein +0 -0
- swordfish/asset/tzdb/Pacific/Majuro +0 -0
- swordfish/asset/tzdb/Pacific/Marquesas +0 -0
- swordfish/asset/tzdb/Pacific/Midway +0 -0
- swordfish/asset/tzdb/Pacific/Nauru +0 -0
- swordfish/asset/tzdb/Pacific/Niue +0 -0
- swordfish/asset/tzdb/Pacific/Norfolk +0 -0
- swordfish/asset/tzdb/Pacific/Noumea +0 -0
- swordfish/asset/tzdb/Pacific/Pago_Pago +0 -0
- swordfish/asset/tzdb/Pacific/Palau +0 -0
- swordfish/asset/tzdb/Pacific/Pitcairn +0 -0
- swordfish/asset/tzdb/Pacific/Pohnpei +0 -0
- swordfish/asset/tzdb/Pacific/Ponape +0 -0
- swordfish/asset/tzdb/Pacific/Port_Moresby +0 -0
- swordfish/asset/tzdb/Pacific/Rarotonga +0 -0
- swordfish/asset/tzdb/Pacific/Saipan +0 -0
- swordfish/asset/tzdb/Pacific/Samoa +0 -0
- swordfish/asset/tzdb/Pacific/Tahiti +0 -0
- swordfish/asset/tzdb/Pacific/Tarawa +0 -0
- swordfish/asset/tzdb/Pacific/Tongatapu +0 -0
- swordfish/asset/tzdb/Pacific/Truk +0 -0
- swordfish/asset/tzdb/Pacific/Wake +0 -0
- swordfish/asset/tzdb/Pacific/Wallis +0 -0
- swordfish/asset/tzdb/Pacific/Yap +0 -0
- swordfish/asset/tzdb/Poland +0 -0
- swordfish/asset/tzdb/Portugal +0 -0
- swordfish/asset/tzdb/ROC +0 -0
- swordfish/asset/tzdb/ROK +0 -0
- swordfish/asset/tzdb/Singapore +0 -0
- swordfish/asset/tzdb/Turkey +0 -0
- swordfish/asset/tzdb/UCT +0 -0
- swordfish/asset/tzdb/US/Alaska +0 -0
- swordfish/asset/tzdb/US/Aleutian +0 -0
- swordfish/asset/tzdb/US/Arizona +0 -0
- swordfish/asset/tzdb/US/Central +0 -0
- swordfish/asset/tzdb/US/East-Indiana +0 -0
- swordfish/asset/tzdb/US/Eastern +0 -0
- swordfish/asset/tzdb/US/Hawaii +0 -0
- swordfish/asset/tzdb/US/Indiana-Starke +0 -0
- swordfish/asset/tzdb/US/Michigan +0 -0
- swordfish/asset/tzdb/US/Mountain +0 -0
- swordfish/asset/tzdb/US/Pacific +0 -0
- swordfish/asset/tzdb/US/Samoa +0 -0
- swordfish/asset/tzdb/UTC +0 -0
- swordfish/asset/tzdb/Universal +0 -0
- swordfish/asset/tzdb/W-SU +0 -0
- swordfish/asset/tzdb/WET +0 -0
- swordfish/asset/tzdb/Zulu +0 -0
- swordfish/asset/tzdb/iso3166.tab +274 -0
- swordfish/asset/tzdb/leapseconds +61 -0
- swordfish/asset/tzdb/posixrules +0 -0
- swordfish/asset/tzdb/tzdata.zi +4150 -0
- swordfish/asset/tzdb/tzmap_gen.py +27 -0
- swordfish/asset/tzdb/tzmapping +501 -0
- swordfish/asset/tzdb/windowsZones.xml +781 -0
- swordfish/asset/tzdb/zone.tab +448 -0
- swordfish/asset/tzdb/zone1970.tab +382 -0
- swordfish/connection.py +33 -0
- swordfish/data.py +806 -0
- swordfish/engine.py +28 -0
- swordfish/enums.py +32 -0
- swordfish/function.py +3 -0
- swordfish/infos.py +53 -0
- swordfish/io.py +11 -0
- swordfish/libSwordfish.dll +0 -0
- swordfish/libclucene-contribs-lib.dll +0 -0
- swordfish/libclucene-core.dll +0 -0
- swordfish/libclucene-shared.dll +0 -0
- swordfish/libgcc_s_seh-1.dll +0 -0
- swordfish/libstdc++-6.dll +0 -0
- swordfish/libwinpthread-1.dll +0 -0
- swordfish/module.py +57 -0
- swordfish/plugins/__init__.py +17 -0
- swordfish/plugins/backtest/__init__.py +38 -0
- swordfish/plugins/backtest/backtest.py +4228 -0
- swordfish/plugins/backtest/translator.py +820 -0
- swordfish/plugins/matching_engine_simulator.py +247 -0
- swordfish/streaming.py +19 -0
- swordfish/tools.py +71 -0
- swordfish/types.py +30 -0
|
@@ -0,0 +1,4228 @@
|
|
|
1
|
+
from ..._swordfishcpp import ( # type: ignore
|
|
2
|
+
plugin_backtest_setUniverse,
|
|
3
|
+
plugin_backtest_createBacktestEngine,
|
|
4
|
+
plugin_backtest_dropBacktestEngine,
|
|
5
|
+
plugin_backtest_submitOrder,
|
|
6
|
+
plugin_backtest_cancelOrder,
|
|
7
|
+
plugin_backtest_getPosition,
|
|
8
|
+
plugin_backtest_getDailyPosition,
|
|
9
|
+
plugin_backtest_appendQuotationMsg,
|
|
10
|
+
plugin_backtest_getAvailableCash,
|
|
11
|
+
plugin_backtest_getTodayPnl,
|
|
12
|
+
plugin_backtest_getTradeDetails,
|
|
13
|
+
plugin_backtest_getContextDict,
|
|
14
|
+
plugin_backtest_getDailyTotalPortfolios,
|
|
15
|
+
plugin_backtest_getReturnSummary,
|
|
16
|
+
plugin_backtest_backtestGetOpenOrders,
|
|
17
|
+
plugin_backtest_getBacktestEngineList,
|
|
18
|
+
plugin_backtest_subscribeIndicator,
|
|
19
|
+
plugin_backtest_getLastestPrices,
|
|
20
|
+
plugin_backtest_getTotalPortfolios,
|
|
21
|
+
plugin_backtest_getMarginSecuPosition,
|
|
22
|
+
plugin_backtest_getMarginTradingPosition,
|
|
23
|
+
plugin_backtest_getSecuLendingPosition,
|
|
24
|
+
plugin_backtest_setSecurityReferenceData,
|
|
25
|
+
plugin_backtest_endWaitAPI,
|
|
26
|
+
plugin_backtest_initialize,
|
|
27
|
+
plugin_backtest_setRealTimeOutputTable,
|
|
28
|
+
plugin_backtest_genIndicatorColumns,
|
|
29
|
+
plugin_backtest_createBacktester,
|
|
30
|
+
plugin_backtest_triggerDailySettlement,
|
|
31
|
+
plugin_backtest_getStockTotalPortfolios,
|
|
32
|
+
plugin_backtest_getFuturesTotalPortfolios,
|
|
33
|
+
plugin_backtest_getOptionTotalPortfolios,
|
|
34
|
+
plugin_backtest_setSimulatorTradingMode,
|
|
35
|
+
plugin_backtest_updatePosition,
|
|
36
|
+
plugin_backtest_getCryptocurrencyPosition,
|
|
37
|
+
plugin_backtest_getCryptocurrencyTotalPortfolios,
|
|
38
|
+
plugin_backtest_setPosition,
|
|
39
|
+
plugin_backtest_getConfig,
|
|
40
|
+
plugin_backtest_appendEndMarker,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
from ..._swordfishcpp import ( # type: ignore
|
|
44
|
+
Resource,
|
|
45
|
+
Table,
|
|
46
|
+
Timestamp,
|
|
47
|
+
ProgrammingError,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
from ..._helper import Config
|
|
51
|
+
from ..._runtime import Runtime
|
|
52
|
+
|
|
53
|
+
from ...data import (
|
|
54
|
+
scalar as sf_scalar,
|
|
55
|
+
vector as sf_vector,
|
|
56
|
+
dictionary as sf_dictionary,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
from ...function import (
|
|
60
|
+
swordfish_udf as F_swordfish_udf,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
from ... import (
|
|
64
|
+
data as sf_data,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
import datetime
|
|
68
|
+
import uuid
|
|
69
|
+
from abc import ABC, abstractmethod
|
|
70
|
+
from enum import Enum
|
|
71
|
+
from typing import (
|
|
72
|
+
TypeVar, Type,
|
|
73
|
+
Union, List, Dict,
|
|
74
|
+
final,
|
|
75
|
+
)
|
|
76
|
+
from collections.abc import Iterable
|
|
77
|
+
from collections import defaultdict
|
|
78
|
+
import weakref
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _generate_name():
|
|
82
|
+
return "BACKTESTER_PSF_" + uuid.uuid4().hex[:8]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class AssetType(Enum):
|
|
86
|
+
"""The asset type.
|
|
87
|
+
"""
|
|
88
|
+
STOCK = "stock"
|
|
89
|
+
FUTURES = "futures"
|
|
90
|
+
OPTION = "option"
|
|
91
|
+
CRYPTO = "cryptocurrency"
|
|
92
|
+
MARGIN = "securityCreditAccount"
|
|
93
|
+
CFETS_BOND = "CFETSBond"
|
|
94
|
+
XSHG_BOND = "XSHGBond"
|
|
95
|
+
UNIVERSAL = "universal" # in futures
|
|
96
|
+
MULTIASSET = "multiAsset"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class MarketType(Enum):
|
|
100
|
+
SNAPSHOT = 1
|
|
101
|
+
SNAPSHOT_TICK = 2
|
|
102
|
+
MINUTE = 3
|
|
103
|
+
DAILY = 4
|
|
104
|
+
STOCK_TICK_WIDE = 5
|
|
105
|
+
SNAPSHOT_TICK_WIDE = 6
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class MarketDataType(Enum):
|
|
109
|
+
SNAPSHOT = "snapshot"
|
|
110
|
+
TICK = "tick"
|
|
111
|
+
KLINE = "kline"
|
|
112
|
+
OHLC = "ohlc"
|
|
113
|
+
TRADE = "trade"
|
|
114
|
+
SNAPSHOT_KLINE = "snapshot_kline"
|
|
115
|
+
SNAPSHOT_OHLC = "snapshot_ohlc"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class MatchingMode(Enum):
|
|
119
|
+
CLOSE_MATCH = 1
|
|
120
|
+
OPEN_MATCH = 2
|
|
121
|
+
ORDER_MATCH = 3
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class AccountType(Enum):
|
|
125
|
+
SPOT = "spot"
|
|
126
|
+
STOCK = "stock"
|
|
127
|
+
FUTURES = "futures"
|
|
128
|
+
OPTION = "option"
|
|
129
|
+
DEFAULT = "default"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _convert_Nothing(v):
|
|
133
|
+
return v if v is not None else sf_data.Nothing
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _convert_account(v: AccountType):
|
|
137
|
+
return v.value if v is not AccountType.DEFAULT else sf_data.String()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class Account:
|
|
141
|
+
account_type: AccountType
|
|
142
|
+
engine: "BacktesterBase"
|
|
143
|
+
|
|
144
|
+
def __init__(self, account_type: AccountType, engine: "BacktesterBase"):
|
|
145
|
+
self.account_type = account_type
|
|
146
|
+
self.engine = engine
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def cash(self):
|
|
150
|
+
"""Query available cash in the account.
|
|
151
|
+
"""
|
|
152
|
+
if self.account_type == AccountType.DEFAULT:
|
|
153
|
+
return plugin_backtest_getAvailableCash(self.engine.engine_handle)
|
|
154
|
+
return plugin_backtest_getAvailableCash(self.engine.engine_handle, self.account_type.value)
|
|
155
|
+
|
|
156
|
+
@property
|
|
157
|
+
def trade_details(self):
|
|
158
|
+
"""Retrieve order trade details.
|
|
159
|
+
|
|
160
|
+
Returns
|
|
161
|
+
-------
|
|
162
|
+
Table
|
|
163
|
+
Return a table with the following structure:
|
|
164
|
+
|
|
165
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
166
|
+
| Field | Description |
|
|
167
|
+
+================+=================================================================================================+
|
|
168
|
+
| orderId | Order ID |
|
|
169
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
170
|
+
| symbol | Security code |
|
|
171
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
172
|
+
| direction | Order direction: |
|
|
173
|
+
| | 1: Buy open |
|
|
174
|
+
| | 2: Sell open |
|
|
175
|
+
| | 3: Sell close |
|
|
176
|
+
| | 4: Buy close |
|
|
177
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
178
|
+
| sendTime | Order submission time |
|
|
179
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
180
|
+
| orderPrice | Order submission price |
|
|
181
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
182
|
+
| orderQty | Order quantity |
|
|
183
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
184
|
+
| tradeTime | Trade time |
|
|
185
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
186
|
+
| tradePrice | Trade price |
|
|
187
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
188
|
+
| tradeQty | Trade quantity |
|
|
189
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
190
|
+
| orderStatus | Order status: |
|
|
191
|
+
| | 4: Submitted |
|
|
192
|
+
| | 2: Cancel successful |
|
|
193
|
+
| | 1: Filled |
|
|
194
|
+
| | 0: Partially filled |
|
|
195
|
+
| | -1: Approval rejected |
|
|
196
|
+
| | -2: Cancel rejected |
|
|
197
|
+
| | -3: Unfilled order |
|
|
198
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
199
|
+
| label | Label |
|
|
200
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
201
|
+
| outputOrderInfo| Risk control log, included only if engine parameter outputOrderInfo=true |
|
|
202
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
203
|
+
| seqNum | Sequence number column, included only if engine parameter outputSeqNum=true |
|
|
204
|
+
+----------------+-------------------------------------------------------------------------------------------------+
|
|
205
|
+
|
|
206
|
+
"""
|
|
207
|
+
if self.account_type == AccountType.DEFAULT:
|
|
208
|
+
return plugin_backtest_getTradeDetails(self.engine.engine_handle)
|
|
209
|
+
return plugin_backtest_getTradeDetails(self.engine.engine_handle, self.account_type.value)
|
|
210
|
+
|
|
211
|
+
def get_position(self, symbol: str = None):
|
|
212
|
+
"""Retrieve position information.
|
|
213
|
+
|
|
214
|
+
- If symbol is not specified, a table is returned.
|
|
215
|
+
|
|
216
|
+
- If symbol is specified, a dictionary is returned.
|
|
217
|
+
|
|
218
|
+
- When JIT optimization is enabled, symbol must be specified.
|
|
219
|
+
|
|
220
|
+
Parameters
|
|
221
|
+
----------
|
|
222
|
+
symbol : str, optional
|
|
223
|
+
A STRING scalar indicating the symbol code.
|
|
224
|
+
|
|
225
|
+
Returns
|
|
226
|
+
-------
|
|
227
|
+
Table
|
|
228
|
+
For assets other than SSE bonds, the returned structure is as follows:
|
|
229
|
+
|
|
230
|
+
+-----------------------+----------------------------------+
|
|
231
|
+
| Field | Description |
|
|
232
|
+
+=======================+==================================+
|
|
233
|
+
| symbol | Stock symbol |
|
|
234
|
+
+-----------------------+----------------------------------+
|
|
235
|
+
| lastDayLongPosition | Long position at previous close |
|
|
236
|
+
+-----------------------+----------------------------------+
|
|
237
|
+
| lastDayShortPosition | Short position at previous close |
|
|
238
|
+
+-----------------------+----------------------------------+
|
|
239
|
+
| longPosition | Current long position |
|
|
240
|
+
+-----------------------+----------------------------------+
|
|
241
|
+
| longPositionAvgPrice | Average price of long position |
|
|
242
|
+
+-----------------------+----------------------------------+
|
|
243
|
+
| shortPosition | Current short position |
|
|
244
|
+
+-----------------------+----------------------------------+
|
|
245
|
+
| shortPositionAvgPrice | Average price of short position |
|
|
246
|
+
+-----------------------+----------------------------------+
|
|
247
|
+
| todayBuyVolume | Buy volume today |
|
|
248
|
+
+-----------------------+----------------------------------+
|
|
249
|
+
| todayBuyValue | Buy value today |
|
|
250
|
+
+-----------------------+----------------------------------+
|
|
251
|
+
| todaySellVolume | Sell volume today |
|
|
252
|
+
+-----------------------+----------------------------------+
|
|
253
|
+
| todaySellValue | Sell value today |
|
|
254
|
+
+-----------------------+----------------------------------+
|
|
255
|
+
|
|
256
|
+
For SSE bonds, the returned structure is as follows:
|
|
257
|
+
|
|
258
|
+
+--------------------+----------------------------------+
|
|
259
|
+
| Field | Description |
|
|
260
|
+
+====================+==================================+
|
|
261
|
+
| symbol | Bond symbol |
|
|
262
|
+
+--------------------+----------------------------------+
|
|
263
|
+
| lastDayLongPosition | Long position at previous close |
|
|
264
|
+
+--------------------+----------------------------------+
|
|
265
|
+
| longPosition | Current long position |
|
|
266
|
+
+--------------------+----------------------------------+
|
|
267
|
+
| longPositionAvgPrice | Average price of long position |
|
|
268
|
+
+--------------------+----------------------------------+
|
|
269
|
+
| todayBuyVolume | Buy volume today |
|
|
270
|
+
+--------------------+----------------------------------+
|
|
271
|
+
| todayBuyValue | Buy value today |
|
|
272
|
+
+--------------------+----------------------------------+
|
|
273
|
+
| totalValue | Total position value |
|
|
274
|
+
+--------------------+----------------------------------+
|
|
275
|
+
| accruedInterest | Accrued interest |
|
|
276
|
+
+--------------------+----------------------------------+
|
|
277
|
+
| fullBondPrice | Full bond price |
|
|
278
|
+
+--------------------+----------------------------------+
|
|
279
|
+
| lastPrice | Bond clean price |
|
|
280
|
+
+--------------------+----------------------------------+
|
|
281
|
+
| yield | Yield |
|
|
282
|
+
+--------------------+----------------------------------+
|
|
283
|
+
| interestIncome | Interest income |
|
|
284
|
+
+--------------------+----------------------------------+
|
|
285
|
+
| floatingProfit | Floating profit/loss |
|
|
286
|
+
+--------------------+----------------------------------+
|
|
287
|
+
| realizedProfit | Realized profit/loss |
|
|
288
|
+
+--------------------+----------------------------------+
|
|
289
|
+
| totalProfit | Total profit/loss |
|
|
290
|
+
+--------------------+----------------------------------+
|
|
291
|
+
| duration | Duration |
|
|
292
|
+
+--------------------+----------------------------------+
|
|
293
|
+
| convexity | Convexity |
|
|
294
|
+
+--------------------+----------------------------------+
|
|
295
|
+
| DV01 | DV01 |
|
|
296
|
+
+--------------------+----------------------------------+
|
|
297
|
+
|
|
298
|
+
"""
|
|
299
|
+
if self.account_type == AccountType.DEFAULT:
|
|
300
|
+
return plugin_backtest_getPosition(self.engine.engine_handle, _convert_Nothing(symbol))
|
|
301
|
+
return plugin_backtest_getPosition(self.engine.engine_handle, _convert_Nothing(symbol), self.account_type.value)
|
|
302
|
+
|
|
303
|
+
def set_position(self, symbol: str, qty: int, order_price: float, last_price: float = None):
|
|
304
|
+
"""Set the initial position. This function must be called before "append_data" is used to insert market data,
|
|
305
|
+
and it is typically invoked within the ``initialize`` callback function.
|
|
306
|
+
|
|
307
|
+
Unlike *set_last_day_position* config, which defines base positions that do not occupy initial capital,
|
|
308
|
+
``set_position`` defines initial positions whose cost does occupy initial capital.
|
|
309
|
+
|
|
310
|
+
Parameters
|
|
311
|
+
----------
|
|
312
|
+
symbol : str
|
|
313
|
+
A STRING scalar indicating the security code.
|
|
314
|
+
qty : int
|
|
315
|
+
An INT scalar indicating the position quantity:
|
|
316
|
+
|
|
317
|
+
- qty > 0: indicates a long (buy) position.
|
|
318
|
+
|
|
319
|
+
- qty < 0: indicates a short (sell) position. Short selling is not supported for stocks and bonds.
|
|
320
|
+
order_price : float
|
|
321
|
+
A DOUBLE scalar indicating the cost price of the position.
|
|
322
|
+
last_price : float, optional
|
|
323
|
+
A DOUBLE scalar indicating the latest price of the security (i.e., the previous closing price).
|
|
324
|
+
If omitted or set to 0, the default value is orderPrice.
|
|
325
|
+
|
|
326
|
+
"""
|
|
327
|
+
if self.account_type == AccountType.DEFAULT:
|
|
328
|
+
return plugin_backtest_setPosition(self.engine.engine_handle, symbol, qty, order_price, _convert_Nothing(last_price))
|
|
329
|
+
return plugin_backtest_setPosition(self.engine.engine_handle, symbol, qty, order_price, _convert_Nothing(last_price), self.account_type.value)
|
|
330
|
+
|
|
331
|
+
def get_daily_position(self, symbol: str = None):
|
|
332
|
+
"""This function is typically called after the backtest completes to retrieve detailed end-of-day position data.
|
|
333
|
+
|
|
334
|
+
If it is called during the trading session, the current day's data will be unavailable,
|
|
335
|
+
and only the previous day's position data will be returned.
|
|
336
|
+
|
|
337
|
+
When the asset type is stocks, futures, or options, the structure of the
|
|
338
|
+
returned position detail table is as follows:
|
|
339
|
+
|
|
340
|
+
+-----------------------+--------------------------------------+
|
|
341
|
+
| Field | Description |
|
|
342
|
+
+=======================+======================================+
|
|
343
|
+
| symbol | Symbol code |
|
|
344
|
+
+-----------------------+--------------------------------------+
|
|
345
|
+
| tradeDate | Trading date |
|
|
346
|
+
+-----------------------+--------------------------------------+
|
|
347
|
+
| lastDayLongPosition | Long position from the previous day |
|
|
348
|
+
+-----------------------+--------------------------------------+
|
|
349
|
+
| lastDayShortPosition | Short position from the previous day |
|
|
350
|
+
+-----------------------+--------------------------------------+
|
|
351
|
+
| longPosition | Current long position |
|
|
352
|
+
+-----------------------+--------------------------------------+
|
|
353
|
+
| longPositionAvgPrice | Average long trade price |
|
|
354
|
+
+-----------------------+--------------------------------------+
|
|
355
|
+
| shortPosition | Current short position |
|
|
356
|
+
+-----------------------+--------------------------------------+
|
|
357
|
+
| shortPositionAvgPrice | Average short trade price |
|
|
358
|
+
+-----------------------+--------------------------------------+
|
|
359
|
+
| todayBuyVolume | Today's total buy volume |
|
|
360
|
+
+-----------------------+--------------------------------------+
|
|
361
|
+
| todayBuyValue | Today's total buy amount |
|
|
362
|
+
+-----------------------+--------------------------------------+
|
|
363
|
+
| todaySellVolume | Today's total sell volume |
|
|
364
|
+
+-----------------------+--------------------------------------+
|
|
365
|
+
| todaySellValue | Today's total sell amount |
|
|
366
|
+
+-----------------------+--------------------------------------+
|
|
367
|
+
|
|
368
|
+
When in margin trading mode, the detailed position table has the following structure:
|
|
369
|
+
|
|
370
|
+
+------------------------------------+-----------------------------------------------+
|
|
371
|
+
| Field | Description |
|
|
372
|
+
+====================================+===============================================+
|
|
373
|
+
| symbol | Symbol code |
|
|
374
|
+
+------------------------------------+-----------------------------------------------+
|
|
375
|
+
| tradeDate | Trading date |
|
|
376
|
+
+------------------------------------+-----------------------------------------------+
|
|
377
|
+
| lastDayMarginSecuPosition | Collateral buy position at previous close |
|
|
378
|
+
+------------------------------------+-----------------------------------------------+
|
|
379
|
+
| lastDayMarginDebt | Margin loan debt at previous close |
|
|
380
|
+
+------------------------------------+-----------------------------------------------+
|
|
381
|
+
| lastDaySecuLendingDebt | Securities lending debt at previous close |
|
|
382
|
+
+------------------------------------+-----------------------------------------------+
|
|
383
|
+
| marginSecuPosition | Collateral buy position |
|
|
384
|
+
+------------------------------------+-----------------------------------------------+
|
|
385
|
+
| marginSecuAvgPrice | Average price of collateral buy position |
|
|
386
|
+
+------------------------------------+-----------------------------------------------+
|
|
387
|
+
| marginBuyPosition | Margin buy position |
|
|
388
|
+
+------------------------------------+-----------------------------------------------+
|
|
389
|
+
| marginBuyValue | Margin buy value |
|
|
390
|
+
+------------------------------------+-----------------------------------------------+
|
|
391
|
+
| secuLendingPosition | Securities lending (short sell) position |
|
|
392
|
+
+------------------------------------+-----------------------------------------------+
|
|
393
|
+
| secuLendingSellValue | Securities lending (short sell) value |
|
|
394
|
+
+------------------------------------+-----------------------------------------------+
|
|
395
|
+
| closePrice | Closing price |
|
|
396
|
+
+------------------------------------+-----------------------------------------------+
|
|
397
|
+
| longPositionConcentration | Long position concentration ratio |
|
|
398
|
+
+------------------------------------+-----------------------------------------------+
|
|
399
|
+
| shortPositionConcentration | Net short position concentration ratio |
|
|
400
|
+
+------------------------------------+-----------------------------------------------+
|
|
401
|
+
| marginBuyProfit | Profit/loss from margin buying |
|
|
402
|
+
+------------------------------------+-----------------------------------------------+
|
|
403
|
+
| financialFee | Margin interest fee |
|
|
404
|
+
+------------------------------------+-----------------------------------------------+
|
|
405
|
+
| secuLendingProfit | Profit/loss from securities lending |
|
|
406
|
+
+------------------------------------+-----------------------------------------------+
|
|
407
|
+
| secuLendingFee | Securities lending fee |
|
|
408
|
+
+------------------------------------+-----------------------------------------------+
|
|
409
|
+
|
|
410
|
+
When the asset type is a bond, the detailed position table has the following structure:
|
|
411
|
+
|
|
412
|
+
+------------------------+--------------------------------------------+
|
|
413
|
+
| Field | Description |
|
|
414
|
+
+========================+============================================+
|
|
415
|
+
| symbol | Symbol code |
|
|
416
|
+
+------------------------+--------------------------------------------+
|
|
417
|
+
| tradeDate | Trading date |
|
|
418
|
+
+------------------------+--------------------------------------------+
|
|
419
|
+
| lastDayLongPosition | Long position quantity at previous close |
|
|
420
|
+
+------------------------+--------------------------------------------+
|
|
421
|
+
| longPosition | Long position quantity |
|
|
422
|
+
+------------------------+--------------------------------------------+
|
|
423
|
+
| longPositionAvgPrice | Average buy price |
|
|
424
|
+
+------------------------+--------------------------------------------+
|
|
425
|
+
| todayBuyVolume | Buy volume of the day |
|
|
426
|
+
+------------------------+--------------------------------------------+
|
|
427
|
+
| todayBuyValue | Buy value of the day |
|
|
428
|
+
+------------------------+--------------------------------------------+
|
|
429
|
+
| totalValue | Total position value |
|
|
430
|
+
+------------------------+--------------------------------------------+
|
|
431
|
+
| accruedInterest | Accrued interest |
|
|
432
|
+
+------------------------+--------------------------------------------+
|
|
433
|
+
| fullBondPrice | Full bond price |
|
|
434
|
+
+------------------------+--------------------------------------------+
|
|
435
|
+
| lastPrice | Net bond price |
|
|
436
|
+
+------------------------+--------------------------------------------+
|
|
437
|
+
| yield | Yield |
|
|
438
|
+
+------------------------+--------------------------------------------+
|
|
439
|
+
| interestIncome | Interest income |
|
|
440
|
+
+------------------------+--------------------------------------------+
|
|
441
|
+
| floatingProfit | Unrealized profit/loss |
|
|
442
|
+
+------------------------+--------------------------------------------+
|
|
443
|
+
| realizedProfit | Realized profit/loss |
|
|
444
|
+
+------------------------+--------------------------------------------+
|
|
445
|
+
| totalProfit | Total profit/loss |
|
|
446
|
+
+------------------------+--------------------------------------------+
|
|
447
|
+
| duration | Duration |
|
|
448
|
+
+------------------------+--------------------------------------------+
|
|
449
|
+
| convexity | Convexity |
|
|
450
|
+
+------------------------+--------------------------------------------+
|
|
451
|
+
| DV01 | DV01 (Dollar Value of 1 Basis Point) |
|
|
452
|
+
+------------------------+--------------------------------------------+
|
|
453
|
+
|
|
454
|
+
Parameters
|
|
455
|
+
----------
|
|
456
|
+
symbol : str, optional
|
|
457
|
+
A STRING scalar indicating the target instrument to query. The default is empty,
|
|
458
|
+
in which case the function returns position data for all instruments.
|
|
459
|
+
|
|
460
|
+
"""
|
|
461
|
+
if self.account_type == AccountType.DEFAULT:
|
|
462
|
+
return plugin_backtest_getDailyPosition(self.engine.engine_handle, _convert_Nothing(symbol))
|
|
463
|
+
return plugin_backtest_getDailyPosition(self.engine.engine_handle, _convert_Nothing(symbol), self.account_type.value)
|
|
464
|
+
|
|
465
|
+
@property
|
|
466
|
+
def total_portfolios(self):
|
|
467
|
+
"""Retrieve the current strategy equity metrics.
|
|
468
|
+
|
|
469
|
+
Returns
|
|
470
|
+
-------
|
|
471
|
+
Dictionary
|
|
472
|
+
A dictionary is returned with the following structure:
|
|
473
|
+
|
|
474
|
+
**For stocks:**
|
|
475
|
+
|
|
476
|
+
+------------------+--------------------------------------+
|
|
477
|
+
| Field Name | Description |
|
|
478
|
+
+==================+======================================+
|
|
479
|
+
| tradeDate | Trading date |
|
|
480
|
+
+------------------+--------------------------------------+
|
|
481
|
+
| cash | Available cash |
|
|
482
|
+
+------------------+--------------------------------------+
|
|
483
|
+
| totalMarketValue | Total market value of the account |
|
|
484
|
+
+------------------+--------------------------------------+
|
|
485
|
+
| totalEquity | Total equity of the account |
|
|
486
|
+
+------------------+--------------------------------------+
|
|
487
|
+
| netValue | Unit net value of the account |
|
|
488
|
+
+------------------+--------------------------------------+
|
|
489
|
+
| totalReturn | Cumulative return up to the day |
|
|
490
|
+
+------------------+--------------------------------------+
|
|
491
|
+
| ratio | Daily return of the account |
|
|
492
|
+
+------------------+--------------------------------------+
|
|
493
|
+
| pnl | Daily profit and loss of the account |
|
|
494
|
+
+------------------+--------------------------------------+
|
|
495
|
+
| frozenFunds | Frozen funds |
|
|
496
|
+
+------------------+--------------------------------------+
|
|
497
|
+
| totalFee | Total fees |
|
|
498
|
+
+------------------+--------------------------------------+
|
|
499
|
+
| floatingPnl | Floating profit and loss |
|
|
500
|
+
+------------------+--------------------------------------+
|
|
501
|
+
| realizedPnl | Realized profit and loss |
|
|
502
|
+
+------------------+--------------------------------------+
|
|
503
|
+
| totalPnl | Total profit and loss |
|
|
504
|
+
+------------------+--------------------------------------+
|
|
505
|
+
|
|
506
|
+
**For Margin Trading and Securities Lending:**
|
|
507
|
+
|
|
508
|
+
+---------------------------+-------------------------------------------+
|
|
509
|
+
| Field Name | Description |
|
|
510
|
+
+===========================+===========================================+
|
|
511
|
+
| tradeDate | Trading date |
|
|
512
|
+
+---------------------------+-------------------------------------------+
|
|
513
|
+
| lineOfCredit | Credit line |
|
|
514
|
+
+---------------------------+-------------------------------------------+
|
|
515
|
+
| availableCash | Available cash |
|
|
516
|
+
+---------------------------+-------------------------------------------+
|
|
517
|
+
| lastDayMarginDebt | Margin debt at previous close |
|
|
518
|
+
+---------------------------+-------------------------------------------+
|
|
519
|
+
| lastDaySecuLendingDebt | Securities lending debt at previous close |
|
|
520
|
+
+---------------------------+-------------------------------------------+
|
|
521
|
+
| marginSecuMarketValue | Market value of collateral purchased |
|
|
522
|
+
+---------------------------+-------------------------------------------+
|
|
523
|
+
| marginDebt | Margin debt |
|
|
524
|
+
+---------------------------+-------------------------------------------+
|
|
525
|
+
| secuLendingSellValue | Securities lending sell value |
|
|
526
|
+
+---------------------------+-------------------------------------------+
|
|
527
|
+
| marginBalance | Margin trading balance |
|
|
528
|
+
+---------------------------+-------------------------------------------+
|
|
529
|
+
| secuLendingDebt | Securities lending debt |
|
|
530
|
+
+---------------------------+-------------------------------------------+
|
|
531
|
+
| financialFee | Financing interest |
|
|
532
|
+
+---------------------------+-------------------------------------------+
|
|
533
|
+
| secuLendingFee | Securities lending fee |
|
|
534
|
+
+---------------------------+-------------------------------------------+
|
|
535
|
+
| maintenanceMargin | Maintenance margin ratio |
|
|
536
|
+
+---------------------------+-------------------------------------------+
|
|
537
|
+
| availableMarginBalance | Available margin balance |
|
|
538
|
+
+---------------------------+-------------------------------------------+
|
|
539
|
+
| totalMarketValue | Total market value of the account |
|
|
540
|
+
+---------------------------+-------------------------------------------+
|
|
541
|
+
| totalEquity | Total equity of the account |
|
|
542
|
+
+---------------------------+-------------------------------------------+
|
|
543
|
+
| netValue | Unit net value of the account |
|
|
544
|
+
+---------------------------+-------------------------------------------+
|
|
545
|
+
| totalReturn | Cumulative return up to the day |
|
|
546
|
+
+---------------------------+-------------------------------------------+
|
|
547
|
+
| yield | Daily return of the account |
|
|
548
|
+
+---------------------------+-------------------------------------------+
|
|
549
|
+
| pnl | Daily profit and loss of the account |
|
|
550
|
+
+---------------------------+-------------------------------------------+
|
|
551
|
+
| frozenFunds | Frozen funds |
|
|
552
|
+
+---------------------------+-------------------------------------------+
|
|
553
|
+
|
|
554
|
+
**For Futures/Options:**
|
|
555
|
+
|
|
556
|
+
+-------------------+---------------------------------------+
|
|
557
|
+
| Field Name | Description |
|
|
558
|
+
+===================+=======================================+
|
|
559
|
+
| tradeDate | Trading date |
|
|
560
|
+
+-------------------+---------------------------------------+
|
|
561
|
+
| margin | Margin used |
|
|
562
|
+
+-------------------+---------------------------------------+
|
|
563
|
+
| floatingPnl | Floating PnL |
|
|
564
|
+
+-------------------+---------------------------------------+
|
|
565
|
+
| realizedPnl | Realized cumulative PnL |
|
|
566
|
+
+-------------------+---------------------------------------+
|
|
567
|
+
| totalPnl | Total PnL |
|
|
568
|
+
+-------------------+---------------------------------------+
|
|
569
|
+
| cash | Available cash |
|
|
570
|
+
+-------------------+---------------------------------------+
|
|
571
|
+
| totalEquity | Total equity of the account |
|
|
572
|
+
+-------------------+---------------------------------------+
|
|
573
|
+
| marginRatio | Margin usage ratio |
|
|
574
|
+
+-------------------+---------------------------------------+
|
|
575
|
+
| pnl | Daily profit and loss |
|
|
576
|
+
+-------------------+---------------------------------------+
|
|
577
|
+
| netValue | Account unit net value |
|
|
578
|
+
+-------------------+---------------------------------------+
|
|
579
|
+
| totalReturn | Cumulative return up to the day |
|
|
580
|
+
+-------------------+---------------------------------------+
|
|
581
|
+
| ratio | Daily return of the account |
|
|
582
|
+
+-------------------+---------------------------------------+
|
|
583
|
+
| totalFee | Total fees |
|
|
584
|
+
+-------------------+---------------------------------------+
|
|
585
|
+
|
|
586
|
+
**For Bonds:**
|
|
587
|
+
|
|
588
|
+
+-------------------+-------------------------------+
|
|
589
|
+
| Field Name | Description |
|
|
590
|
+
+===================+===============================+
|
|
591
|
+
| tradeDate | Trading date |
|
|
592
|
+
+-------------------+-------------------------------+
|
|
593
|
+
| cash | Available cash |
|
|
594
|
+
+-------------------+-------------------------------+
|
|
595
|
+
| totalMarketValue | Total market value of account |
|
|
596
|
+
+-------------------+-------------------------------+
|
|
597
|
+
| totalEquity | Total equity of account |
|
|
598
|
+
+-------------------+-------------------------------+
|
|
599
|
+
| netValue | Account unit net value |
|
|
600
|
+
+-------------------+-------------------------------+
|
|
601
|
+
| totalReturn | Cumulative return up to date |
|
|
602
|
+
+-------------------+-------------------------------+
|
|
603
|
+
| ratio | Daily return |
|
|
604
|
+
+-------------------+-------------------------------+
|
|
605
|
+
| pnl | Daily profit and loss |
|
|
606
|
+
+-------------------+-------------------------------+
|
|
607
|
+
| totalProfit | Total profit and loss |
|
|
608
|
+
+-------------------+-------------------------------+
|
|
609
|
+
|
|
610
|
+
"""
|
|
611
|
+
if self.account_type == AccountType.DEFAULT:
|
|
612
|
+
return plugin_backtest_getTotalPortfolios(self.engine.engine_handle)
|
|
613
|
+
return plugin_backtest_getTotalPortfolios(self.engine.engine_handle, self.account_type.value)
|
|
614
|
+
|
|
615
|
+
@property
|
|
616
|
+
def daily_total_portfolios(self):
|
|
617
|
+
"""Called at the end of a backtest to obtain the strategy's daily equity metrics table.
|
|
618
|
+
|
|
619
|
+
Returns
|
|
620
|
+
-------
|
|
621
|
+
Table
|
|
622
|
+
Return a table. The structure of the returned table varies depending on the asset type of the backtest.
|
|
623
|
+
|
|
624
|
+
**For Stocks:**
|
|
625
|
+
|
|
626
|
+
+---------------------+------------------------------------------------------------------+
|
|
627
|
+
| Field Name | Description |
|
|
628
|
+
+=====================+==================================================================+
|
|
629
|
+
| tradeDate | Date |
|
|
630
|
+
+---------------------+------------------------------------------------------------------+
|
|
631
|
+
| cash | Available cash |
|
|
632
|
+
+---------------------+------------------------------------------------------------------+
|
|
633
|
+
| totalMarketValue | Total market value of the account |
|
|
634
|
+
+---------------------+------------------------------------------------------------------+
|
|
635
|
+
| totalEquity | Total equity of the account |
|
|
636
|
+
+---------------------+------------------------------------------------------------------+
|
|
637
|
+
| netValue | Unit net value of the account |
|
|
638
|
+
+---------------------+------------------------------------------------------------------+
|
|
639
|
+
| totalReturn | Cumulative return up to the current day |
|
|
640
|
+
+---------------------+------------------------------------------------------------------+
|
|
641
|
+
| ratio | Daily return of the account |
|
|
642
|
+
+---------------------+------------------------------------------------------------------+
|
|
643
|
+
| pnl | Daily P&L of the account |
|
|
644
|
+
+---------------------+------------------------------------------------------------------+
|
|
645
|
+
| frozenFunds | Frozen funds |
|
|
646
|
+
+---------------------+------------------------------------------------------------------+
|
|
647
|
+
| totalFee | Total fees |
|
|
648
|
+
+---------------------+------------------------------------------------------------------+
|
|
649
|
+
| floatingPnl | Floating P&L |
|
|
650
|
+
+---------------------+------------------------------------------------------------------+
|
|
651
|
+
| realizedPnl | Realized P&L |
|
|
652
|
+
+---------------------+------------------------------------------------------------------+
|
|
653
|
+
| totalPnl | Total P&L |
|
|
654
|
+
+---------------------+------------------------------------------------------------------+
|
|
655
|
+
| benchmarkClosePrice | Benchmark daily closing price; returned only if benchmark is set |
|
|
656
|
+
+---------------------+------------------------------------------------------------------+
|
|
657
|
+
| benchmarkNetValue | Benchmark daily net value; returned only if benchmark is set |
|
|
658
|
+
+---------------------+------------------------------------------------------------------+
|
|
659
|
+
|
|
660
|
+
**For Margin Trading and Securities Lending:**
|
|
661
|
+
|
|
662
|
+
+------------------------+------------------------------------------------------------------+
|
|
663
|
+
| Field Name | Description |
|
|
664
|
+
+========================+==================================================================+
|
|
665
|
+
| tradeDate | Date |
|
|
666
|
+
+------------------------+------------------------------------------------------------------+
|
|
667
|
+
| lineOfCredit | Line of credit |
|
|
668
|
+
+------------------------+------------------------------------------------------------------+
|
|
669
|
+
| availableCash | Available cash |
|
|
670
|
+
+------------------------+------------------------------------------------------------------+
|
|
671
|
+
| lastDayMarginDebt | Previous day's closing margin debt |
|
|
672
|
+
+------------------------+------------------------------------------------------------------+
|
|
673
|
+
| lastDaySecuLendingDebt | Previous day's closing securities lending debt |
|
|
674
|
+
+------------------------+------------------------------------------------------------------+
|
|
675
|
+
| marginSecuMarketValue | Market value of collateral purchased |
|
|
676
|
+
+------------------------+------------------------------------------------------------------+
|
|
677
|
+
| marginDebt | Margin debt |
|
|
678
|
+
+------------------------+------------------------------------------------------------------+
|
|
679
|
+
| secuLendingSellValue | Amount from securities lending sell (lending liability) |
|
|
680
|
+
+------------------------+------------------------------------------------------------------+
|
|
681
|
+
| marginBalance | Margin trading and securities lending balance |
|
|
682
|
+
+------------------------+------------------------------------------------------------------+
|
|
683
|
+
| secuLendingDebt | Securities lending debt |
|
|
684
|
+
+------------------------+------------------------------------------------------------------+
|
|
685
|
+
| financialFee | Financing interest |
|
|
686
|
+
+------------------------+------------------------------------------------------------------+
|
|
687
|
+
| secuLendingFee | Securities lending fee |
|
|
688
|
+
+------------------------+------------------------------------------------------------------+
|
|
689
|
+
| maintenanceMargin | Maintenance margin ratio |
|
|
690
|
+
+------------------------+------------------------------------------------------------------+
|
|
691
|
+
| availableMarginBalance | Available margin balance |
|
|
692
|
+
+------------------------+------------------------------------------------------------------+
|
|
693
|
+
| totalMarketValue | Total market value of the account |
|
|
694
|
+
+------------------------+------------------------------------------------------------------+
|
|
695
|
+
| totalEquity | Total equity of the account |
|
|
696
|
+
+------------------------+------------------------------------------------------------------+
|
|
697
|
+
| netValue | Unit net value of the account |
|
|
698
|
+
+------------------------+------------------------------------------------------------------+
|
|
699
|
+
| totalReturn | Cumulative return up to the current day |
|
|
700
|
+
+------------------------+------------------------------------------------------------------+
|
|
701
|
+
| yield | Daily return of the account |
|
|
702
|
+
+------------------------+------------------------------------------------------------------+
|
|
703
|
+
| pnl | Daily P&L of the account |
|
|
704
|
+
+------------------------+------------------------------------------------------------------+
|
|
705
|
+
| benchmarkClosePrice | Benchmark daily closing price; returned only if benchmark is set |
|
|
706
|
+
+------------------------+------------------------------------------------------------------+
|
|
707
|
+
| benchmarkNetValue | Benchmark daily net value; returned only if benchmark is set |
|
|
708
|
+
+------------------------+------------------------------------------------------------------+
|
|
709
|
+
|
|
710
|
+
**For Futures/Options:**
|
|
711
|
+
|
|
712
|
+
+------------------------+------------------------------------------------------------------+
|
|
713
|
+
| Field Name | Description |
|
|
714
|
+
+========================+==================================================================+
|
|
715
|
+
| tradeDate | Date |
|
|
716
|
+
+------------------------+------------------------------------------------------------------+
|
|
717
|
+
| margin | Margin used |
|
|
718
|
+
+------------------------+------------------------------------------------------------------+
|
|
719
|
+
| floatingPnl | Floating P&L |
|
|
720
|
+
+------------------------+------------------------------------------------------------------+
|
|
721
|
+
| realizedPnl | Realized cumulative P&L |
|
|
722
|
+
+------------------------+------------------------------------------------------------------+
|
|
723
|
+
| totalPnl | Total P&L |
|
|
724
|
+
+------------------------+------------------------------------------------------------------+
|
|
725
|
+
| totalMarketValue | Total market value (options only) |
|
|
726
|
+
+------------------------+------------------------------------------------------------------+
|
|
727
|
+
| cash | Available cash |
|
|
728
|
+
+------------------------+------------------------------------------------------------------+
|
|
729
|
+
| totalEquity | Total equity of the account |
|
|
730
|
+
+------------------------+------------------------------------------------------------------+
|
|
731
|
+
| marginRatio | Margin usage ratio |
|
|
732
|
+
+------------------------+------------------------------------------------------------------+
|
|
733
|
+
| pnl | Daily P&L of the account |
|
|
734
|
+
+------------------------+------------------------------------------------------------------+
|
|
735
|
+
| netValue | Unit net value of the account |
|
|
736
|
+
+------------------------+------------------------------------------------------------------+
|
|
737
|
+
| totalReturn | Cumulative return up to the current day |
|
|
738
|
+
+------------------------+------------------------------------------------------------------+
|
|
739
|
+
| ratio | Daily return of the account |
|
|
740
|
+
+------------------------+------------------------------------------------------------------+
|
|
741
|
+
| benchmarkClosePrice | Benchmark daily closing price; returned only if benchmark is set |
|
|
742
|
+
+------------------------+------------------------------------------------------------------+
|
|
743
|
+
| benchmarkNetValue | Benchmark daily net value; returned only if benchmark is set |
|
|
744
|
+
+------------------------+------------------------------------------------------------------+
|
|
745
|
+
|
|
746
|
+
**For Bonds:**
|
|
747
|
+
|
|
748
|
+
+------------------------+------------------------------------------------------------------+
|
|
749
|
+
| Field Name | Description |
|
|
750
|
+
+========================+==================================================================+
|
|
751
|
+
| tradeDate | Date |
|
|
752
|
+
+------------------------+------------------------------------------------------------------+
|
|
753
|
+
| cash | Available cash |
|
|
754
|
+
+------------------------+------------------------------------------------------------------+
|
|
755
|
+
| totalMarketValue | Total market value of the account |
|
|
756
|
+
+------------------------+------------------------------------------------------------------+
|
|
757
|
+
| totalEquity | Total equity of the account |
|
|
758
|
+
+------------------------+------------------------------------------------------------------+
|
|
759
|
+
| netValue | Unit net value of the account |
|
|
760
|
+
+------------------------+------------------------------------------------------------------+
|
|
761
|
+
| totalReturn | Cumulative return up to the current day |
|
|
762
|
+
+------------------------+------------------------------------------------------------------+
|
|
763
|
+
| ratio | Daily return of the account |
|
|
764
|
+
+------------------------+------------------------------------------------------------------+
|
|
765
|
+
| pnl | Daily P&L of the account |
|
|
766
|
+
+------------------------+------------------------------------------------------------------+
|
|
767
|
+
| totalProfit | Total profit/loss |
|
|
768
|
+
+------------------------+------------------------------------------------------------------+
|
|
769
|
+
| benchmarkClosePrice | Benchmark daily closing price; returned only if benchmark is set |
|
|
770
|
+
+------------------------+------------------------------------------------------------------+
|
|
771
|
+
| benchmarkNetValue | Benchmark daily net value; returned only if benchmark is set |
|
|
772
|
+
+------------------------+------------------------------------------------------------------+
|
|
773
|
+
|
|
774
|
+
"""
|
|
775
|
+
if self.account_type == AccountType.DEFAULT:
|
|
776
|
+
return plugin_backtest_getDailyTotalPortfolios(self.engine.engine_handle)
|
|
777
|
+
return plugin_backtest_getDailyTotalPortfolios(self.engine.engine_handle, self.account_type.value)
|
|
778
|
+
|
|
779
|
+
@property
|
|
780
|
+
def return_summary(self):
|
|
781
|
+
"""Used to calculate the strategy's performance summary at the end of backtesting.
|
|
782
|
+
|
|
783
|
+
Returns
|
|
784
|
+
-------
|
|
785
|
+
Table
|
|
786
|
+
A table is returned with the following structure:
|
|
787
|
+
|
|
788
|
+
**For Stocks/Futures/Options/Bonds:**
|
|
789
|
+
|
|
790
|
+
+--------------------+--------------------------------------------------------+
|
|
791
|
+
| Field Name | Description |
|
|
792
|
+
+====================+========================================================+
|
|
793
|
+
| totalReturn | Total return |
|
|
794
|
+
+--------------------+--------------------------------------------------------+
|
|
795
|
+
| annualReturn | Annualized return |
|
|
796
|
+
+--------------------+--------------------------------------------------------+
|
|
797
|
+
| annualVolatility | Annualized volatility |
|
|
798
|
+
+--------------------+--------------------------------------------------------+
|
|
799
|
+
| annualSkew | Return skewness |
|
|
800
|
+
+--------------------+--------------------------------------------------------+
|
|
801
|
+
| annualKur | Return kurtosis |
|
|
802
|
+
+--------------------+--------------------------------------------------------+
|
|
803
|
+
| sharpeRatio | Sharpe ratio |
|
|
804
|
+
+--------------------+--------------------------------------------------------+
|
|
805
|
+
| maxDrawdown | Maximum drawdown |
|
|
806
|
+
+--------------------+--------------------------------------------------------+
|
|
807
|
+
| drawdownRatio | Drawdown-to-return ratio |
|
|
808
|
+
+--------------------+--------------------------------------------------------+
|
|
809
|
+
| beta | Beta coefficient |
|
|
810
|
+
+--------------------+--------------------------------------------------------+
|
|
811
|
+
| alpha | Alpha coefficient |
|
|
812
|
+
+--------------------+--------------------------------------------------------+
|
|
813
|
+
| benchmarkReturn | Benchmark return |
|
|
814
|
+
+--------------------+--------------------------------------------------------+
|
|
815
|
+
| annualExcessReturn | Annualized excess return |
|
|
816
|
+
+--------------------+--------------------------------------------------------+
|
|
817
|
+
| turnoverRate | Turnover rate |
|
|
818
|
+
+--------------------+--------------------------------------------------------+
|
|
819
|
+
| dailyWinningRate | Daily winning rate |
|
|
820
|
+
+--------------------+--------------------------------------------------------+
|
|
821
|
+
| maxMarginRatio | Maximum margin usage (field unique to futures/options) |
|
|
822
|
+
+--------------------+--------------------------------------------------------+
|
|
823
|
+
|
|
824
|
+
In Margin Trading and Securities Lending mode, the returned table contains the above fields
|
|
825
|
+
as well as the following additional fields:
|
|
826
|
+
|
|
827
|
+
+-------------------+------------------------------------------+
|
|
828
|
+
| Field Name | Description |
|
|
829
|
+
+===================+==========================================+
|
|
830
|
+
| totalFee | Total commission and fees |
|
|
831
|
+
+-------------------+------------------------------------------+
|
|
832
|
+
| financialFee | Financing interest |
|
|
833
|
+
+-------------------+------------------------------------------+
|
|
834
|
+
| secuLendingFee | Securities lending fee |
|
|
835
|
+
+-------------------+------------------------------------------+
|
|
836
|
+
| bottomRet | Base position return |
|
|
837
|
+
+-------------------+------------------------------------------+
|
|
838
|
+
| bottomExcessRet | Base position excess return |
|
|
839
|
+
+-------------------+------------------------------------------+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
"""
|
|
843
|
+
if self.account_type == AccountType.DEFAULT:
|
|
844
|
+
return plugin_backtest_getReturnSummary(self.engine.engine_handle)
|
|
845
|
+
return plugin_backtest_getReturnSummary(self.engine.engine_handle, self.account_type.value)
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
class BacktestBasicConfig(Config):
|
|
849
|
+
start_date: datetime.date
|
|
850
|
+
end_date: datetime.date
|
|
851
|
+
asset_type: AssetType
|
|
852
|
+
data_type: MarketType
|
|
853
|
+
cash: Union[Dict[AccountType, float], float] = None
|
|
854
|
+
matching_mode: MatchingMode = None
|
|
855
|
+
|
|
856
|
+
universe: List[str] = None
|
|
857
|
+
|
|
858
|
+
context: dict = None
|
|
859
|
+
|
|
860
|
+
latency: int = None
|
|
861
|
+
benchmark: str = None
|
|
862
|
+
data_retention_window: Union[str, int] = None
|
|
863
|
+
|
|
864
|
+
is_backtest_mode: bool = True
|
|
865
|
+
msg_as_table: bool = False # always False
|
|
866
|
+
enable_indicator_optimize: bool = False
|
|
867
|
+
add_time_column_in_indicator: bool = False
|
|
868
|
+
|
|
869
|
+
orderbook_matching_ratio: float = 1.0
|
|
870
|
+
output_order_info: bool = False
|
|
871
|
+
matching_ratio: float = None
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
class StockConfig(BacktestBasicConfig):
|
|
875
|
+
set_last_day_position: Table = None
|
|
876
|
+
prev_close_price: Table = None
|
|
877
|
+
enable_subscription_to_tick_quotes: bool = False
|
|
878
|
+
commission: float = None
|
|
879
|
+
tax: float = None
|
|
880
|
+
output_queue_position: int = 0
|
|
881
|
+
stock_dividend: Table = None
|
|
882
|
+
frequency: int = 0
|
|
883
|
+
callback_for_snapshot: int = 0
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
class MarginConfig(BacktestBasicConfig):
|
|
887
|
+
line_of_credit: float
|
|
888
|
+
margin_trading_interest_rate: float
|
|
889
|
+
secu_lending_interest_rate: float
|
|
890
|
+
maintenance_margin: List[float]
|
|
891
|
+
long_concentration: List[float] = None
|
|
892
|
+
short_concentration: List[float] = None
|
|
893
|
+
repay_without_margin_buy: bool = None
|
|
894
|
+
set_last_day_position: Table = None
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
class OptionConfig(BacktestBasicConfig):
|
|
898
|
+
security_reference: Table
|
|
899
|
+
frequency: int = 0
|
|
900
|
+
callback_for_snapshot: int = 0
|
|
901
|
+
maintenance_margin: float = 1.0
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
class FuturesConfig(BacktestBasicConfig):
|
|
905
|
+
security_reference: Table
|
|
906
|
+
frequency: int = 0
|
|
907
|
+
futures_type: str = None
|
|
908
|
+
enable_algo_order: bool
|
|
909
|
+
callback_for_snapshot: int = 0
|
|
910
|
+
maintenance_margin: float = 1.0
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
class BondConfig(BacktestBasicConfig):
|
|
914
|
+
security_reference: Table
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
class CryptoConfig(BacktestBasicConfig):
|
|
918
|
+
security_reference: Table
|
|
919
|
+
funding_rate: Table
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
class BacktesterBase(ABC):
|
|
923
|
+
engine_handle: Resource
|
|
924
|
+
accounts: Dict[AccountType, Account]
|
|
925
|
+
|
|
926
|
+
@abstractmethod
|
|
927
|
+
def append_data(self, data):
|
|
928
|
+
pass
|
|
929
|
+
|
|
930
|
+
@abstractmethod
|
|
931
|
+
def append_end(self):
|
|
932
|
+
pass
|
|
933
|
+
|
|
934
|
+
@property
|
|
935
|
+
@abstractmethod
|
|
936
|
+
def context_dict(self):
|
|
937
|
+
"""Return the logical context.
|
|
938
|
+
"""
|
|
939
|
+
pass
|
|
940
|
+
|
|
941
|
+
@property
|
|
942
|
+
@abstractmethod
|
|
943
|
+
def universe(self):
|
|
944
|
+
"""Set the symbol pool for the engine.
|
|
945
|
+
"""
|
|
946
|
+
pass
|
|
947
|
+
|
|
948
|
+
@universe.setter
|
|
949
|
+
@abstractmethod
|
|
950
|
+
def universe(self, val: List[str]):
|
|
951
|
+
pass
|
|
952
|
+
|
|
953
|
+
@property
|
|
954
|
+
@abstractmethod
|
|
955
|
+
def config(self) -> sf_data.Dictionary:
|
|
956
|
+
"""Return the backtest configuration.
|
|
957
|
+
"""
|
|
958
|
+
pass
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
class StrategyBase:
|
|
962
|
+
engine: BacktesterBase
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
def trigger_time(t: str):
|
|
966
|
+
"""A timer event that triggers at a specified time or frequency. Set the trigger using
|
|
967
|
+
the @trigger_time decorator. Only intervals in seconds (SECOND) are supported.
|
|
968
|
+
|
|
969
|
+
Parameters
|
|
970
|
+
----------
|
|
971
|
+
t : str
|
|
972
|
+
A STRING scalar specifying the trigger time in "HH:MM:SS" format. The callback function will be executed at this time.
|
|
973
|
+
|
|
974
|
+
"""
|
|
975
|
+
if isinstance(t, str):
|
|
976
|
+
t = sf_scalar(t, type="SECOND")
|
|
977
|
+
elif isinstance(t, Iterable):
|
|
978
|
+
t = sf_vector(t, type="SECOND")
|
|
979
|
+
|
|
980
|
+
if not isinstance(t, sf_data.Second) and not isinstance(t, sf_data.Vector):
|
|
981
|
+
raise ProgrammingError("The trigger time must be a Second object or a Second Vector object.")
|
|
982
|
+
|
|
983
|
+
def wrapper(func):
|
|
984
|
+
func._is_timer_func = True
|
|
985
|
+
func._timer_time = t
|
|
986
|
+
return func
|
|
987
|
+
return wrapper
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
class InterfaceMeta(type):
|
|
991
|
+
def __new__(cls, name, bases, attrs):
|
|
992
|
+
new_cls = super().__new__(cls, name, bases, attrs)
|
|
993
|
+
new_cls._timer_funcs = defaultdict(dict)
|
|
994
|
+
|
|
995
|
+
for attr_name, attr_value in attrs.items():
|
|
996
|
+
if callable(attr_value) and getattr(attr_value, '_is_timer_func', False):
|
|
997
|
+
time_key = attr_value._timer_time
|
|
998
|
+
func_name = attr_value.__name__
|
|
999
|
+
new_cls._timer_funcs[func_name] = time_key
|
|
1000
|
+
|
|
1001
|
+
return new_cls
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
class StrategyInterface(metaclass=InterfaceMeta):
|
|
1005
|
+
def initialize(self, context):
|
|
1006
|
+
"""Strategy initialization callback function, triggered when the backtester is created.
|
|
1007
|
+
It is used for preparatory tasks such as loading parameters, initializing states,
|
|
1008
|
+
and registering indicators.
|
|
1009
|
+
|
|
1010
|
+
Parameters
|
|
1011
|
+
----------
|
|
1012
|
+
context : _type_
|
|
1013
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
1014
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
1015
|
+
the backtesting engine maintains four internal variables within the context:
|
|
1016
|
+
|
|
1017
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
1018
|
+
|
|
1019
|
+
- context.tradeDate — Returns the current trading date.
|
|
1020
|
+
|
|
1021
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
1022
|
+
are aggregated into lower-frequency bars.
|
|
1023
|
+
|
|
1024
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
1025
|
+
"""
|
|
1026
|
+
pass
|
|
1027
|
+
|
|
1028
|
+
def before_trading(self, context):
|
|
1029
|
+
"""The daily callback function is triggered before the market opens each trading day.
|
|
1030
|
+
|
|
1031
|
+
Parameters
|
|
1032
|
+
----------
|
|
1033
|
+
context : _type_
|
|
1034
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
1035
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
1036
|
+
the backtesting engine maintains four internal variables within the context:
|
|
1037
|
+
|
|
1038
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
1039
|
+
|
|
1040
|
+
- context.tradeDate — Returns the current trading date.
|
|
1041
|
+
|
|
1042
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
1043
|
+
are aggregated into lower-frequency bars.
|
|
1044
|
+
|
|
1045
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
1046
|
+
"""
|
|
1047
|
+
pass
|
|
1048
|
+
|
|
1049
|
+
def on_tick(self, context, msg, indicator):
|
|
1050
|
+
"""The callback function is triggered whenever the latest tick-by-tick order or trade data is received.
|
|
1051
|
+
|
|
1052
|
+
Parameters
|
|
1053
|
+
----------
|
|
1054
|
+
context : _type_
|
|
1055
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
1056
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
1057
|
+
the backtesting engine maintains four internal variables within the context:
|
|
1058
|
+
|
|
1059
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
1060
|
+
|
|
1061
|
+
- context.tradeDate — Returns the current trading date.
|
|
1062
|
+
|
|
1063
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
1064
|
+
are aggregated into lower-frequency bars.
|
|
1065
|
+
|
|
1066
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
1067
|
+
msg : _type_
|
|
1068
|
+
Represents the tick-level market data. Each record is provided as either a dictionary object
|
|
1069
|
+
or a table (depending on the msg_as_table configuration). The specific fields vary
|
|
1070
|
+
by asset type. For details, refer to the msg data section below.
|
|
1071
|
+
indicator : _type_
|
|
1072
|
+
A dictionary or nested dictionary with a structure consistent with the corresponding msg,
|
|
1073
|
+
containing the data subscribed to by the strategy.
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
Msg data
|
|
1077
|
+
---------------
|
|
1078
|
+
|
|
1079
|
+
- Stock Tick / Margin Trading
|
|
1080
|
+
|
|
1081
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1082
|
+
| Field | Type | Description |
|
|
1083
|
+
+==============+===========+=========================================================================================================+
|
|
1084
|
+
| symbol | SYMBOL | Stock code: |
|
|
1085
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1086
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1087
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1088
|
+
| symbolSource | STRING | ".XSHG" (Shanghai Stock Exchange) or ".XSHE" (Shenzhen Stock Exchange) |
|
|
1089
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1090
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1091
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1092
|
+
| sourceType | INT | 0 represents entrust data; 1 represents trade data |
|
|
1093
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1094
|
+
| orderType | INT | - entrust: |
|
|
1095
|
+
| | | |
|
|
1096
|
+
| | | 1: Market order |
|
|
1097
|
+
| | | |
|
|
1098
|
+
| | | 2: Limit order |
|
|
1099
|
+
| | | |
|
|
1100
|
+
| | | 3: Best own side price |
|
|
1101
|
+
| | | |
|
|
1102
|
+
| | | 10: Cancel order (Shanghai only, cancel records are in entrust) |
|
|
1103
|
+
| | | - trade: |
|
|
1104
|
+
| | | |
|
|
1105
|
+
| | | 0: Trade |
|
|
1106
|
+
| | | |
|
|
1107
|
+
| | | 1: Cancel order (Shenzhen only, cancel records are in trade) |
|
|
1108
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1109
|
+
| price | DOUBLE | Order price |
|
|
1110
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1111
|
+
| qty | LONG | Order quantity |
|
|
1112
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1113
|
+
| buyNo | LONG | Original buy order number in trade; filled for entrust orders |
|
|
1114
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1115
|
+
| sellNo | LONG | Original sell order number in trade; filled for entrust orders |
|
|
1116
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1117
|
+
| direction | INT | 1 (Buy) or 2 (Sell) |
|
|
1118
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1119
|
+
| channelNo | INT | Channel number |
|
|
1120
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1121
|
+
| seqNum | LONG | Tick data sequence number |
|
|
1122
|
+
+--------------+-----------+---------------------------------------------------------------------------------------------------------+
|
|
1123
|
+
|
|
1124
|
+
- Stock Tick or Tick + Snapshot
|
|
1125
|
+
|
|
1126
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1127
|
+
| Field | Type | Description |
|
|
1128
|
+
+==============+===========+==========================================================================================================+
|
|
1129
|
+
| symbol | SYMBOL | Stock code: |
|
|
1130
|
+
| | | |
|
|
1131
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1132
|
+
| | | |
|
|
1133
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1134
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1135
|
+
| symbolSource | STRING | ".XSHG" (Shanghai Stock Exchange) or ".XSHE" (Shenzhen Stock Exchange) |
|
|
1136
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1137
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1138
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1139
|
+
| sourceType | INT | 0 represents entrust data; 1 represents trade data |
|
|
1140
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1141
|
+
| orderType | INT | - entrust: |
|
|
1142
|
+
| | | |
|
|
1143
|
+
| | | 1: Market order |
|
|
1144
|
+
| | | |
|
|
1145
|
+
| | | 2: Limit order |
|
|
1146
|
+
| | | |
|
|
1147
|
+
| | | 3: Best own side price |
|
|
1148
|
+
| | | |
|
|
1149
|
+
| | | 10: Cancel order (Shanghai only; cancel records are in entrust) |
|
|
1150
|
+
| | | |
|
|
1151
|
+
| | | - trade: |
|
|
1152
|
+
| | | |
|
|
1153
|
+
| | | 0: Trade |
|
|
1154
|
+
| | | |
|
|
1155
|
+
| | | 1: Cancel order (Shenzhen only; cancel records are in trade) |
|
|
1156
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1157
|
+
| price | DOUBLE | Order price |
|
|
1158
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1159
|
+
| qty | LONG | Order quantity |
|
|
1160
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1161
|
+
| buyNo | LONG | Original buy order number in trade; filled for entrust orders |
|
|
1162
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1163
|
+
| sellNo | LONG | Original sell order number in trade; filled for entrust orders |
|
|
1164
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1165
|
+
| direction | INT | 1 (Buy) or 2 (Sell) |
|
|
1166
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1167
|
+
| channelNo | INT | Channel number |
|
|
1168
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1169
|
+
| seqNum | LONG | Tick data sequence number |
|
|
1170
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1171
|
+
|
|
1172
|
+
- Stock Tick (Wide Table)
|
|
1173
|
+
|
|
1174
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1175
|
+
| Field | Type | Description |
|
|
1176
|
+
+==============+===========+==========================================================================================================+
|
|
1177
|
+
| symbol | SYMBOL | Stock code: |
|
|
1178
|
+
| | | |
|
|
1179
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1180
|
+
| | | |
|
|
1181
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1182
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1183
|
+
| symbolSource | STRING | ".XSHG" (Shanghai Stock Exchange) or ".XSHE" (Shenzhen Stock Exchange) |
|
|
1184
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1185
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1186
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1187
|
+
| sourceType | INT | 0 represents entrust data; 1 represents trade data |
|
|
1188
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1189
|
+
| orderType | INT | - entrust: |
|
|
1190
|
+
| | | |
|
|
1191
|
+
| | | 1: Market order |
|
|
1192
|
+
| | | |
|
|
1193
|
+
| | | 2: Limit order |
|
|
1194
|
+
| | | |
|
|
1195
|
+
| | | 3: Best own side price |
|
|
1196
|
+
| | | |
|
|
1197
|
+
| | | 10: Cancel order (Shanghai only; cancel records are in entrust) |
|
|
1198
|
+
| | | |
|
|
1199
|
+
| | | - trade: |
|
|
1200
|
+
| | | |
|
|
1201
|
+
| | | 0: Trade |
|
|
1202
|
+
| | | |
|
|
1203
|
+
| | | 1: Cancel order (Shenzhen only; cancel records are in trade) |
|
|
1204
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1205
|
+
| price | DOUBLE | Order price |
|
|
1206
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1207
|
+
| qty | LONG | Order quantity |
|
|
1208
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1209
|
+
| buyNo | LONG | Original buy order number in trade; filled for entrust orders |
|
|
1210
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1211
|
+
| sellNo | LONG | Original sell order number in trade; filled for entrust orders |
|
|
1212
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1213
|
+
| direction | INT | 1 (Buy) or 2 (Sell) |
|
|
1214
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1215
|
+
| channelNo | INT | Channel number |
|
|
1216
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1217
|
+
| seqNum | LONG | Tick data sequence number |
|
|
1218
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1219
|
+
| reserve1 | DOUBLE | Reserved field 1 (for wide table) |
|
|
1220
|
+
+--------------+-----------+----------------------------------------------------------------------------------------------------------+
|
|
1221
|
+
|
|
1222
|
+
- Stock Tick + Snapshot (Wide Table)
|
|
1223
|
+
|
|
1224
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1225
|
+
| Field | Type | Description |
|
|
1226
|
+
+================+================+==============================================================================================================+
|
|
1227
|
+
| symbol | SYMBOL | Stock code: |
|
|
1228
|
+
| | | |
|
|
1229
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1230
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1231
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1232
|
+
| symbolSource | STRING | ".XSHG" (Shanghai Stock Exchange) or ".XSHE" (Shenzhen Stock Exchange) |
|
|
1233
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1234
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1235
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1236
|
+
| sourceType | INT | 0 represents entrust data; 1 represents trade; 2 represents snapshot |
|
|
1237
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1238
|
+
| orderType | INT | - entrust: |
|
|
1239
|
+
| | | |
|
|
1240
|
+
| | | 1: Market order |
|
|
1241
|
+
| | | |
|
|
1242
|
+
| | | 2: Limit order |
|
|
1243
|
+
| | | |
|
|
1244
|
+
| | | 3: Best own side price |
|
|
1245
|
+
| | | |
|
|
1246
|
+
| | | 10: Cancel order (Shanghai only; cancel records are in entrust) |
|
|
1247
|
+
| | | |
|
|
1248
|
+
| | | - trade: |
|
|
1249
|
+
| | | |
|
|
1250
|
+
| | | 0: Trade |
|
|
1251
|
+
| | | |
|
|
1252
|
+
| | | 1: Cancel order (Shenzhen only; cancel records are in trade) |
|
|
1253
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1254
|
+
| price | DOUBLE | Order price |
|
|
1255
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1256
|
+
| qty | LONG | Order quantity |
|
|
1257
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1258
|
+
| buyNo | LONG | Original buy order number in trade; filled for entrust orders |
|
|
1259
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1260
|
+
| sellNo | LONG | Original sell order number in trade; filled for entrust orders |
|
|
1261
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1262
|
+
| direction | INT | 1 (Buy) or 2 (Sell) |
|
|
1263
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1264
|
+
| channelNo | INT | Channel number |
|
|
1265
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1266
|
+
| seqNum | LONG | Tick data sequence number |
|
|
1267
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1268
|
+
| lastPrice | DOUBLE | Latest trade price |
|
|
1269
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1270
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1271
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1272
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1273
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1274
|
+
| totalBidQty | LONG | Interval buy quantity |
|
|
1275
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1276
|
+
| totalOfferQty | LONG | Interval sell quantity |
|
|
1277
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1278
|
+
| bidPrice | DOUBLE[] | List of buy prices |
|
|
1279
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1280
|
+
| bidQty | LONG[] | List of buy quantities |
|
|
1281
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1282
|
+
| offerPrice | DOUBLE[] | List of sell prices |
|
|
1283
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1284
|
+
| offerQty | LONG[] | List of sell quantities |
|
|
1285
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1286
|
+
| prevClosePrice | DOUBLE | Previous close price |
|
|
1287
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1288
|
+
| reserve1 | DOUBLE | Reserved field 1 (for wide table) |
|
|
1289
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1290
|
+
| reserve2 | DOUBLE | Reserved field 2 (for wide table) |
|
|
1291
|
+
+----------------+----------------+--------------------------------------------------------------------------------------------------------------+
|
|
1292
|
+
|
|
1293
|
+
"""
|
|
1294
|
+
pass
|
|
1295
|
+
|
|
1296
|
+
def on_snapshot(self, context, msg, indicator):
|
|
1297
|
+
"""The callback function is triggered upon receiving snapshot market data.
|
|
1298
|
+
|
|
1299
|
+
Parameters
|
|
1300
|
+
----------
|
|
1301
|
+
context : _type_
|
|
1302
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
1303
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
1304
|
+
the backtesting engine maintains four internal variables within the context:
|
|
1305
|
+
|
|
1306
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
1307
|
+
|
|
1308
|
+
- context.tradeDate — Returns the current trading date.
|
|
1309
|
+
|
|
1310
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
1311
|
+
are aggregated into lower-frequency bars.
|
|
1312
|
+
|
|
1313
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
1314
|
+
msg : _type_
|
|
1315
|
+
Represents the tick-level market data. Each record is provided as either a dictionary object
|
|
1316
|
+
or a table (depending on the msg_as_table configuration). The specific fields vary
|
|
1317
|
+
by asset type. For details, refer to the msg data section below.
|
|
1318
|
+
indicator : _type_
|
|
1319
|
+
A dictionary or nested dictionary with a structure consistent with the corresponding msg,
|
|
1320
|
+
containing the data subscribed to by the strategy.
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
Msg data
|
|
1324
|
+
---------------
|
|
1325
|
+
|
|
1326
|
+
- Stock Tick / Margin Trading
|
|
1327
|
+
|
|
1328
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1329
|
+
| Field | Type | Description |
|
|
1330
|
+
+=================+===========+===========================================================+
|
|
1331
|
+
| symbol | SYMBOL | Stock code |
|
|
1332
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1333
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1334
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1335
|
+
| symbolSource | SYMBOL | Stock market |
|
|
1336
|
+
| | | |
|
|
1337
|
+
| | | - ".XSHG": Shanghai Stock Exchange |
|
|
1338
|
+
| | | - ".XSHE": Shenzhen Stock Exchange |
|
|
1339
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1340
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1341
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1342
|
+
| lastPrice | DOUBLE | Latest trade price |
|
|
1343
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1344
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1345
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1346
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1347
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1348
|
+
| totalBidQty | LONG | Total bid quantity |
|
|
1349
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1350
|
+
| totalOfferQty | LONG | Total offer quantity |
|
|
1351
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1352
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
1353
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1354
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
1355
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1356
|
+
| offerPrice | DOUBLE[] | List of offer prices |
|
|
1357
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1358
|
+
| offerQty | LONG[] | List of offer quantities |
|
|
1359
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1360
|
+
| signal | DOUBLE[] | List of indicators |
|
|
1361
|
+
+-----------------+-----------+-----------------------------------------------------------+
|
|
1362
|
+
|
|
1363
|
+
- Stock tick or tick + snapshot
|
|
1364
|
+
|
|
1365
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1366
|
+
| Name | Type | Description |
|
|
1367
|
+
+=================+============+==================================================================================+
|
|
1368
|
+
| symbol | SYMBOL | Stock code |
|
|
1369
|
+
| | | |
|
|
1370
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1371
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1372
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1373
|
+
| symbolSource | STRING | Stock market |
|
|
1374
|
+
| | | |
|
|
1375
|
+
| | | - ".XSHG": Shanghai Stock Exchange |
|
|
1376
|
+
| | | - ".XSHE": Shenzhen Stock Exchange |
|
|
1377
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1378
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1379
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1380
|
+
| lastPrice | DOUBLE | Latest trade price |
|
|
1381
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1382
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1383
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1384
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1385
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1386
|
+
| totalBidQty | LONG | Total bid quantity |
|
|
1387
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1388
|
+
| totalOfferQty | LONG | Total offer quantity |
|
|
1389
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1390
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
1391
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1392
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
1393
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1394
|
+
| offerPrice | DOUBLE[] | List of offer prices |
|
|
1395
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1396
|
+
| offerQty | LONG[] | List of offer quantities |
|
|
1397
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1398
|
+
| signal | DOUBLE[] | Other indicators |
|
|
1399
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1400
|
+
| open | DOUBLE | Open price of the aggregated bar data |
|
|
1401
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1402
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1403
|
+
| close | DOUBLE | Close price of the aggregated bar data |
|
|
1404
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1405
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1406
|
+
| low | DOUBLE | Lowest price of the aggregated bar data |
|
|
1407
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1408
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1409
|
+
| high | DOUBLE | Highest price of the aggregated bar data |
|
|
1410
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1411
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1412
|
+
| volume | LONG | Trading volume of the aggregated bar data |
|
|
1413
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1414
|
+
+-----------------+------------+----------------------------------------------------------------------------------+
|
|
1415
|
+
|
|
1416
|
+
- Stock Snapshot + Tick
|
|
1417
|
+
|
|
1418
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1419
|
+
| Field | Type | Description |
|
|
1420
|
+
+=================+============+=================================================================================+
|
|
1421
|
+
| symbol | SYMBOL | Stock code |
|
|
1422
|
+
| | | |
|
|
1423
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1424
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1425
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1426
|
+
| symbolSource | STRING | Stock market identifier |
|
|
1427
|
+
| | | |
|
|
1428
|
+
| | | - ".XSHG": Shanghai Stock Exchange |
|
|
1429
|
+
| | | - ".XSHE": Shenzhen Stock Exchange |
|
|
1430
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1431
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1432
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1433
|
+
| lastPrice | DOUBLE | Latest traded price |
|
|
1434
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1435
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1436
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1437
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1438
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1439
|
+
| totalBidQty | LONG | Total bid quantity executed |
|
|
1440
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1441
|
+
| totalOfferQty | LONG | Total offer quantity executed |
|
|
1442
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1443
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
1444
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1445
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
1446
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1447
|
+
| offerPrice | DOUBLE[] | List of offer prices |
|
|
1448
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1449
|
+
| offerQty | LONG[] | List of offer quantities |
|
|
1450
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1451
|
+
| signal | DOUBLE[] | Other indicators |
|
|
1452
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1453
|
+
| open | DOUBLE | Open price of the aggregated bar data |
|
|
1454
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1455
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1456
|
+
| close | DOUBLE | Close price of the aggregated bar data |
|
|
1457
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1458
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1459
|
+
| low | DOUBLE | Lowest price of the aggregated bar data |
|
|
1460
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1461
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1462
|
+
| high | DOUBLE | Highest price of the aggregated bar data |
|
|
1463
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1464
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1465
|
+
| volume | LONG | Trading volume of the aggregated bar data |
|
|
1466
|
+
| | | *(Available only when data_type = 1 or 2, and callback_for_snapshot = 1 or 2)* |
|
|
1467
|
+
+-----------------+------------+---------------------------------------------------------------------------------+
|
|
1468
|
+
|
|
1469
|
+
- Stock Tick (Wide Table)
|
|
1470
|
+
|
|
1471
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1472
|
+
| Field | Type | Description |
|
|
1473
|
+
+===============+============+===========================================================================================================================================+
|
|
1474
|
+
| symbol | SYMBOL | Stock code |
|
|
1475
|
+
| | | |
|
|
1476
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1477
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1478
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1479
|
+
| symbolSource | STRING | Stock market identifier |
|
|
1480
|
+
| | | |
|
|
1481
|
+
| | | - ".XSHG": Shanghai Stock Exchange |
|
|
1482
|
+
| | | - ".XSHE": Shenzhen Stock Exchange |
|
|
1483
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1484
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1485
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1486
|
+
| sourceType | INT | Source type indicator |
|
|
1487
|
+
| | | |
|
|
1488
|
+
| | | - 0: Entrust data (`entrust`) |
|
|
1489
|
+
| | | - 1: Trade data (`trade`) |
|
|
1490
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1491
|
+
| orderType | INT | Order type |
|
|
1492
|
+
| | | |
|
|
1493
|
+
| | | - For `entrust`: |
|
|
1494
|
+
| | | |
|
|
1495
|
+
| | | 1 = Market order; 2 = Limit order; 3 = Best bid/ask; 10 = Cancel order *(only for SSE, since cancellations are recorded in `entrust`)* |
|
|
1496
|
+
| | | |
|
|
1497
|
+
| | | - For `trade`: |
|
|
1498
|
+
| | | |
|
|
1499
|
+
| | | 0 = Trade; 1 = Cancel order *(only for SZSE, since cancellations are recorded in `trade`)* |
|
|
1500
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1501
|
+
| price | DOUBLE | Order price |
|
|
1502
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1503
|
+
| qty | LONG | Order quantity |
|
|
1504
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1505
|
+
| buyNo | LONG | For `trade`: Corresponds to the original data. |
|
|
1506
|
+
| | | For `entrust`: Populated with the entrust order number. |
|
|
1507
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1508
|
+
| sellNo | LONG | For `trade`: Corresponds to the original data. |
|
|
1509
|
+
| | | For `entrust`: Populated with the entrust order number. |
|
|
1510
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1511
|
+
| direction | INT | Trade direction: 1 = Buy, 2 = Sell |
|
|
1512
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1513
|
+
| channelNo | INT | Channel number |
|
|
1514
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1515
|
+
| seqNum | LONG | Tick-by-tick data sequence number |
|
|
1516
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1517
|
+
| reserve1 | DOUBLE | Reserved field 1 (for wide table) |
|
|
1518
|
+
+---------------+------------+-------------------------------------------------------------------------------------------------------------------------------------------+
|
|
1519
|
+
|
|
1520
|
+
- Stock Tick + Snapshot (Wide Table)
|
|
1521
|
+
|
|
1522
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1523
|
+
| Field | Type | Description |
|
|
1524
|
+
+==================+============+==================================================================================================================================+
|
|
1525
|
+
| symbol | SYMBOL | Stock code |
|
|
1526
|
+
| | | |
|
|
1527
|
+
| | | - Ends with ".XSHG" for Shanghai Stock Exchange |
|
|
1528
|
+
| | | - Ends with ".XSHE" for Shenzhen Stock Exchange |
|
|
1529
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1530
|
+
| symbolSource | STRING | Stock market identifier |
|
|
1531
|
+
| | | |
|
|
1532
|
+
| | | - ".XSHG": Shanghai Stock Exchange |
|
|
1533
|
+
| | | - ".XSHE": Shenzhen Stock Exchange |
|
|
1534
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1535
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1536
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1537
|
+
| sourceType | INT | Source type indicator |
|
|
1538
|
+
| | | |
|
|
1539
|
+
| | | - 0: Entrust data (`entrust`) |
|
|
1540
|
+
| | | - 1: Trade data (`trade`) |
|
|
1541
|
+
| | | - 2: Snapshot data (`snapshot`) |
|
|
1542
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1543
|
+
| orderType | INT | Order type |
|
|
1544
|
+
| | | |
|
|
1545
|
+
| | | - For `entrust`: |
|
|
1546
|
+
| | | |
|
|
1547
|
+
| | | 1 = Market order; 2 = Limit order; 3 = Best bid/ask; 10 = Cancel order *(only for SSE, cancellations recorded in `entrust`)* |
|
|
1548
|
+
| | | |
|
|
1549
|
+
| | | - For `trade`: |
|
|
1550
|
+
| | | |
|
|
1551
|
+
| | | 0 = Trade; 1 = Cancel order *(only for SZSE, cancellations recorded in `trade`)* |
|
|
1552
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1553
|
+
| price | DOUBLE | Order price |
|
|
1554
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1555
|
+
| qty | LONG | Order quantity |
|
|
1556
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1557
|
+
| buyNo | LONG | For `trade`: corresponds to the original data |
|
|
1558
|
+
| | | For `entrust`: populated with the entrust order number |
|
|
1559
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1560
|
+
| sellNo | LONG | For `trade`: corresponds to the original data |
|
|
1561
|
+
| | | For `entrust`: populated with the entrust order number |
|
|
1562
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1563
|
+
| direction | INT | Trade direction: 1 = Buy, 2 = Sell |
|
|
1564
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1565
|
+
| channelNo | INT | Channel number |
|
|
1566
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1567
|
+
| seqNum | LONG | Tick-by-tick data sequence number |
|
|
1568
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1569
|
+
| lastPrice | DOUBLE | Last traded price |
|
|
1570
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1571
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1572
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1573
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1574
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1575
|
+
| totalBidQty | LONG | Total bid quantity in the interval |
|
|
1576
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1577
|
+
| totalOfferQty | LONG | Total ask quantity in the interval |
|
|
1578
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1579
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
1580
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1581
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
1582
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1583
|
+
| offerPrice | DOUBLE[] | List of ask prices |
|
|
1584
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1585
|
+
| offerQty | LONG[] | List of ask quantities |
|
|
1586
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1587
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
1588
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1589
|
+
| reserve1 | DOUBLE | Reserved field 1 (for wide table) |
|
|
1590
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1591
|
+
| reserve2 | DOUBLE | Reserved field 2 (for wide table) |
|
|
1592
|
+
+------------------+------------+----------------------------------------------------------------------------------------------------------------------------------+
|
|
1593
|
+
|
|
1594
|
+
- Options snapshot
|
|
1595
|
+
|
|
1596
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1597
|
+
| Name | Type | Description |
|
|
1598
|
+
+===================+================+================================================================================+
|
|
1599
|
+
| symbol | SYMBOL | Option code |
|
|
1600
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1601
|
+
| symbolSource | STRING | Exchange |
|
|
1602
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1603
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1604
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1605
|
+
| tradingDay | DATE | Trading day / Settlement date |
|
|
1606
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1607
|
+
| lastPrice | DOUBLE | Latest trade price |
|
|
1608
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1609
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1610
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1611
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1612
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1613
|
+
| totalBidQty | LONG | Interval total buy quantity |
|
|
1614
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1615
|
+
| totalOfferQty | LONG | Interval total sell quantity |
|
|
1616
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1617
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
1618
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1619
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
1620
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1621
|
+
| offerPrice | DOUBLE[] | List of ask prices |
|
|
1622
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1623
|
+
| offerQty | LONG[] | List of ask quantities |
|
|
1624
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1625
|
+
| highPrice | DOUBLE | Highest price |
|
|
1626
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1627
|
+
| lowPrice | DOUBLE | Lowest price |
|
|
1628
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1629
|
+
| signal | DOUBLE[] | List of other fields |
|
|
1630
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1631
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
1632
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1633
|
+
| settlementPrice | DOUBLE | Settlement price |
|
|
1634
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1635
|
+
| prevSettlementPrice | DOUBLE | Previous settlement price |
|
|
1636
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1637
|
+
| underlyingPrice | DOUBLE | Underlying asset price |
|
|
1638
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1639
|
+
| Theta | DOUBLE | / |
|
|
1640
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1641
|
+
| Vega | DOUBLE | / |
|
|
1642
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1643
|
+
| Gamma | DOUBLE | / |
|
|
1644
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1645
|
+
| Delta | DOUBLE | / |
|
|
1646
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1647
|
+
| IV | DOUBLE | / |
|
|
1648
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1649
|
+
| open | DOUBLE | Open price of synthetic bar (available only if data_type = 1 or 2, and |
|
|
1650
|
+
| | | callback_for_snapshot = 1 or 2) |
|
|
1651
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1652
|
+
| close | DOUBLE | Close price of synthetic bar (available only if data_type = 1 or 2, and |
|
|
1653
|
+
| | | callback_for_snapshot = 1 or 2) |
|
|
1654
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1655
|
+
| low | DOUBLE | Lowest price of synthetic bar (available only if data_type = 1 or 2, and |
|
|
1656
|
+
| | | callback_for_snapshot = 1 or 2) |
|
|
1657
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1658
|
+
| high | DOUBLE | Highest price of synthetic bar (available only if data_type = 1 or 2, and |
|
|
1659
|
+
| | | callback_for_snapshot = 1 or 2) |
|
|
1660
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1661
|
+
| volume | LONG | Volume of synthetic bar (available only if data_type = 1 or 2, and |
|
|
1662
|
+
| | | callback_for_snapshot = 1 or 2) |
|
|
1663
|
+
+-------------------+----------------+--------------------------------------------------------------------------------+
|
|
1664
|
+
|
|
1665
|
+
.. note::
|
|
1666
|
+
When generating bar data from snapshots (frequency > 0 and callback_for_snapshot = 1 or 2),
|
|
1667
|
+
the input parameter msg should additionally include five fields: "open", "close", "low", "high", "volume".
|
|
1668
|
+
|
|
1669
|
+
- Futures Snapshot
|
|
1670
|
+
|
|
1671
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1672
|
+
| Field | Type | Description |
|
|
1673
|
+
+=====================+===========+==============================================================================================+
|
|
1674
|
+
| symbol | SYMBOL | Futures contract code |
|
|
1675
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1676
|
+
| symbolSource | STRING | Exchange |
|
|
1677
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1678
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1679
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1680
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
1681
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1682
|
+
| lastPrice | DOUBLE | Last traded price |
|
|
1683
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1684
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1685
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1686
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1687
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1688
|
+
| totalBidQty | LONG | Total buy quantity in the interval |
|
|
1689
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1690
|
+
| totalOfferQty | LONG | Total sell quantity in the interval |
|
|
1691
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1692
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
1693
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1694
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
1695
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1696
|
+
| offerPrice | DOUBLE[] | List of ask prices |
|
|
1697
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1698
|
+
| offerQty | LONG[] | List of ask quantities |
|
|
1699
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1700
|
+
| highPrice | DOUBLE | Highest price |
|
|
1701
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1702
|
+
| lowPrice | DOUBLE | Lowest price |
|
|
1703
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1704
|
+
| signal | DOUBLE[] | List of other fields |
|
|
1705
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1706
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
1707
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1708
|
+
| settlementPrice | DOUBLE | Settlement price |
|
|
1709
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1710
|
+
| prevSettlementPrice | DOUBLE | Previous settlement price |
|
|
1711
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1712
|
+
| open | DOUBLE | Open price of the synthesized bar (available only when dataType = 1 or 2 and |
|
|
1713
|
+
| | | callbackForSnapshot = 1 or 2) |
|
|
1714
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1715
|
+
| close | DOUBLE | Close price of the synthesized bar (available only when dataType = 1 or 2 and |
|
|
1716
|
+
| | | callbackForSnapshot = 1 or 2) |
|
|
1717
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1718
|
+
| low | DOUBLE | Lowest price of the synthesized bar (available only when dataType = 1 or 2 and |
|
|
1719
|
+
| | | callbackForSnapshot = 1 or 2) |
|
|
1720
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1721
|
+
| high | DOUBLE | Highest price of the synthesized bar (available only when dataType = 1 or 2 and |
|
|
1722
|
+
| | | callbackForSnapshot = 1 or 2) |
|
|
1723
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1724
|
+
| volume | LONG | Volume of the synthesized bar (available only when dataType = 1 or 2 and |
|
|
1725
|
+
| | | callbackForSnapshot = 1 or 2) |
|
|
1726
|
+
+---------------------+-----------+----------------------------------------------------------------------------------------------+
|
|
1727
|
+
|
|
1728
|
+
.. note::
|
|
1729
|
+
When generating bar data from snapshots (frequency > 0 and callbackForSnapshot = 1 or 2),
|
|
1730
|
+
the input parameter msg should additionally include five fields: "open", "close", "low", "high", "volume".
|
|
1731
|
+
|
|
1732
|
+
- Interbank Bond Snapshot
|
|
1733
|
+
|
|
1734
|
+
+----------------+-----------+-----------------------------------+
|
|
1735
|
+
| Field | Type | Description |
|
|
1736
|
+
+================+===========+===================================+
|
|
1737
|
+
| symbol | SYMBOL | Instrument code |
|
|
1738
|
+
+----------------+-----------+-----------------------------------+
|
|
1739
|
+
| messageSource | SYMBOL | Market: Interbank "X_BOND" |
|
|
1740
|
+
+----------------+-----------+-----------------------------------+
|
|
1741
|
+
| byield | TIMESTAMP | Timestamp |
|
|
1742
|
+
+----------------+-----------+-----------------------------------+
|
|
1743
|
+
| ayield | DOUBLE[] | Bid yields |
|
|
1744
|
+
+----------------+-----------+-----------------------------------+
|
|
1745
|
+
| bmdEntryPrice | DOUBLE[] | Bid net price (CNY) |
|
|
1746
|
+
+----------------+-----------+-----------------------------------+
|
|
1747
|
+
| amdEntryPrice | DOUBLE[] | Ask net price (CNY) |
|
|
1748
|
+
+----------------+-----------+-----------------------------------+
|
|
1749
|
+
| bmdEntrySize | LONG[] | Bid quantity (CNY) |
|
|
1750
|
+
+----------------+-----------+-----------------------------------+
|
|
1751
|
+
| amdEntrySize | LONG[] | Ask quantity (CNY) |
|
|
1752
|
+
+----------------+-----------+-----------------------------------+
|
|
1753
|
+
| bsettlType | LONG[] | Buy settlement speed |
|
|
1754
|
+
+----------------+-----------+-----------------------------------+
|
|
1755
|
+
| asettlType | LONG[] | Sell settlement speed |
|
|
1756
|
+
+----------------+-----------+-----------------------------------+
|
|
1757
|
+
| settlType | LONG[] | Interval settlement speed |
|
|
1758
|
+
+----------------+-----------+-----------------------------------+
|
|
1759
|
+
| tradePrice | DOUBLE[] | List of interval trade prices |
|
|
1760
|
+
+----------------+-----------+-----------------------------------+
|
|
1761
|
+
| tradeYield | DOUBLE[] | Yield to maturity |
|
|
1762
|
+
+----------------+-----------+-----------------------------------+
|
|
1763
|
+
| tradeQty | LONG[] | List of interval trade quantities |
|
|
1764
|
+
+----------------+-----------+-----------------------------------+
|
|
1765
|
+
|
|
1766
|
+
- Cryptocurrency Snapshot
|
|
1767
|
+
|
|
1768
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1769
|
+
| Field | Type | Description |
|
|
1770
|
+
+=====================+================+================================================================================+
|
|
1771
|
+
| symbol | STRING | Instrument code |
|
|
1772
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1773
|
+
| symbolSource | STRING | Exchange |
|
|
1774
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1775
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
1776
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1777
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
1778
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1779
|
+
| lastPrice | DECIMAL128(8) | Last traded price |
|
|
1780
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1781
|
+
| upLimitPrice | DECIMAL128(8) | Upper limit price |
|
|
1782
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1783
|
+
| downLimitPrice | DECIMAL128(8) | Lower limit price |
|
|
1784
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1785
|
+
| totalBidQty | DECIMAL128(8) | Interval bid quantity |
|
|
1786
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1787
|
+
| totalOfferQty | DECIMAL128(8) | Interval ask quantity |
|
|
1788
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1789
|
+
| bidPrice | DECIMAL128(8)[]| List of bid prices |
|
|
1790
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1791
|
+
| bidQty | DECIMAL128(8)[]| List of bid quantities |
|
|
1792
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1793
|
+
| offerPrice | DECIMAL128(8)[]| List of ask prices |
|
|
1794
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1795
|
+
| offerQty | DECIMAL128(8)[]| List of ask quantities |
|
|
1796
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1797
|
+
| highPrice | DECIMAL128(8) | Highest price |
|
|
1798
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1799
|
+
| lowPrice | DECIMAL128(8) | Lowest price |
|
|
1800
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1801
|
+
| signal | DOUBLE[] | Other indicator fields |
|
|
1802
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1803
|
+
| prevClosePrice | DECIMAL128(8) | Previous closing price |
|
|
1804
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1805
|
+
| settlementPrice | DECIMAL128(8) | Settlement price |
|
|
1806
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1807
|
+
| prevSettlementPrice | DECIMAL128(8) | Previous settlement price |
|
|
1808
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1809
|
+
| contractType | INT | Instrument type: |
|
|
1810
|
+
| | | |
|
|
1811
|
+
| | | 0: Spot |
|
|
1812
|
+
| | | |
|
|
1813
|
+
| | | 1: Delivery contract |
|
|
1814
|
+
| | | |
|
|
1815
|
+
| | | 2: Perpetual contract |
|
|
1816
|
+
| | | |
|
|
1817
|
+
| | | 3: Option |
|
|
1818
|
+
+---------------------+----------------+--------------------------------------------------------------------------------+
|
|
1819
|
+
"""
|
|
1820
|
+
pass
|
|
1821
|
+
|
|
1822
|
+
def on_bar(self, context, msg, indicator):
|
|
1823
|
+
"""The callback function (minute or daily frequency) is triggered when subscribed
|
|
1824
|
+
snapshot data is aggregated into bar data.
|
|
1825
|
+
|
|
1826
|
+
Parameters
|
|
1827
|
+
----------
|
|
1828
|
+
context : _type_
|
|
1829
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
1830
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
1831
|
+
the backtesting engine maintains four internal variables within the context:
|
|
1832
|
+
|
|
1833
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
1834
|
+
|
|
1835
|
+
- context.tradeDate — Returns the current trading date.
|
|
1836
|
+
|
|
1837
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
1838
|
+
are aggregated into lower-frequency bars.
|
|
1839
|
+
|
|
1840
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
1841
|
+
msg : _type_
|
|
1842
|
+
Represents the tick-level market data. Each record is provided as either a dictionary object
|
|
1843
|
+
or a table (depending on the msg_as_table configuration). The specific fields vary
|
|
1844
|
+
by asset type. For details, refer to the msg data section below.
|
|
1845
|
+
indicator : _type_
|
|
1846
|
+
A dictionary or nested dictionary with a structure consistent with the corresponding msg,
|
|
1847
|
+
containing the data subscribed to by the strategy.
|
|
1848
|
+
|
|
1849
|
+
|
|
1850
|
+
Msg data
|
|
1851
|
+
----------------
|
|
1852
|
+
|
|
1853
|
+
- Stock snapshot / Snapshot + tick trade details / Minute or daily frequency
|
|
1854
|
+
|
|
1855
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1856
|
+
| Field | Type | Description |
|
|
1857
|
+
+================+===============+========================================================+
|
|
1858
|
+
| symbol | SYMBOL | Stock code |
|
|
1859
|
+
| | | Ends with ".XSHG" for SSE |
|
|
1860
|
+
| | | Ends with ".XSHE" for SZSE |
|
|
1861
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1862
|
+
| tradeTime | TIMESTAMP | Trading day |
|
|
1863
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1864
|
+
| open | DOUBLE | Opening price |
|
|
1865
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1866
|
+
| low | DOUBLE | Lowest price |
|
|
1867
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1868
|
+
| high | DOUBLE | Highest price |
|
|
1869
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1870
|
+
| close | DOUBLE | Closing price |
|
|
1871
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1872
|
+
| volume | LONG | Trading volume |
|
|
1873
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1874
|
+
| amount | DOUBLE | Trading amount |
|
|
1875
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1876
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1877
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1878
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1879
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1880
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
1881
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1882
|
+
| signal | DOUBLE[] | Other indicators / metrics |
|
|
1883
|
+
+----------------+---------------+--------------------------------------------------------+
|
|
1884
|
+
|
|
1885
|
+
- Option snapshot
|
|
1886
|
+
|
|
1887
|
+
When callback_for_snapshot = 1 or 2 is configured in the backtesting engine,
|
|
1888
|
+
the callback function on_bar will be triggered. The input parameter msg represents
|
|
1889
|
+
a K-line bar, with each K-line containing the following fields:
|
|
1890
|
+
|
|
1891
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1892
|
+
| Field | Type | Description |
|
|
1893
|
+
+=====================+===============+==========================================================+
|
|
1894
|
+
| symbol | SYMBOL | Option code |
|
|
1895
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1896
|
+
| symbolSource | STRING | Exchange |
|
|
1897
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1898
|
+
| tradeTime | TIMESTAMP | Timestamp |
|
|
1899
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1900
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
1901
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1902
|
+
| open | DOUBLE | Opening price |
|
|
1903
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1904
|
+
| low | DOUBLE | Lowest price |
|
|
1905
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1906
|
+
| high | DOUBLE | Highest price |
|
|
1907
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1908
|
+
| close | DOUBLE | Closing price |
|
|
1909
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1910
|
+
| volume | LONG | Trading volume |
|
|
1911
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1912
|
+
| amount | DOUBLE | Trading amount |
|
|
1913
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1914
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1915
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1916
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1917
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1918
|
+
| signal | DOUBLE[] | Other indicators / metrics |
|
|
1919
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1920
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
1921
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1922
|
+
| settlementPrice | DOUBLE | Settlement price |
|
|
1923
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1924
|
+
| prevSettlementPrice | DOUBLE | Previous settlement price |
|
|
1925
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1926
|
+
| underlyingPrice | DOUBLE | Underlying asset price |
|
|
1927
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1928
|
+
| Theta | DOUBLE | / |
|
|
1929
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1930
|
+
| Vega | DOUBLE | / |
|
|
1931
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1932
|
+
| Gamma | DOUBLE | / |
|
|
1933
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1934
|
+
| Delta | DOUBLE | / |
|
|
1935
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1936
|
+
| IV | DOUBLE | / |
|
|
1937
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1938
|
+
|
|
1939
|
+
- Option minute or daily frequency
|
|
1940
|
+
|
|
1941
|
+
The input parameter msg represents a K-line bar, with each K-line containing the following fields:
|
|
1942
|
+
|
|
1943
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1944
|
+
| Field | Type | Description |
|
|
1945
|
+
+=====================+===============+==========================================================+
|
|
1946
|
+
| symbol | SYMBOL | Option code |
|
|
1947
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1948
|
+
| symbolSource | STRING | Exchange |
|
|
1949
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1950
|
+
| tradeTime | TIMESTAMP | Timestamp |
|
|
1951
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1952
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
1953
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1954
|
+
| open | DOUBLE | Opening price |
|
|
1955
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1956
|
+
| low | DOUBLE | Lowest price |
|
|
1957
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1958
|
+
| high | DOUBLE | Highest price |
|
|
1959
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1960
|
+
| close | DOUBLE | Closing price |
|
|
1961
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1962
|
+
| volume | LONG | Trading volume |
|
|
1963
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1964
|
+
| amount | DOUBLE | Trading amount |
|
|
1965
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1966
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
1967
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1968
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
1969
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1970
|
+
| signal | DOUBLE[] | Other fields list |
|
|
1971
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1972
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
1973
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1974
|
+
| settlementPrice | DOUBLE | Settlement price |
|
|
1975
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1976
|
+
| prevSettlementPrice | DOUBLE | Previous settlement price |
|
|
1977
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1978
|
+
| underlyingPrice | DOUBLE | Underlying asset price |
|
|
1979
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1980
|
+
| Theta | DOUBLE | / |
|
|
1981
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1982
|
+
| Vega | DOUBLE | / |
|
|
1983
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1984
|
+
| Gamma | DOUBLE | / |
|
|
1985
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1986
|
+
| Delta | DOUBLE | / |
|
|
1987
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1988
|
+
| IV | DOUBLE | / |
|
|
1989
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1990
|
+
|
|
1991
|
+
- Futures snapshot
|
|
1992
|
+
|
|
1993
|
+
When callbackForSnapshot = 1, the callback function on_bar will be triggered.
|
|
1994
|
+
The input parameter msg represents a K-line bar, with each K-line containing the following fields:
|
|
1995
|
+
|
|
1996
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
1997
|
+
| Field | Type | Description |
|
|
1998
|
+
+=====================+===============+==========================================================+
|
|
1999
|
+
| symbol | SYMBOL | Futures code |
|
|
2000
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2001
|
+
| symbolSource | STRING | Exchange |
|
|
2002
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2003
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
2004
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2005
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
2006
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2007
|
+
| lastPrice | DOUBLE | Latest traded price |
|
|
2008
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2009
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
2010
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2011
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
2012
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2013
|
+
| totalBidQty | LONG | Interval buy quantity |
|
|
2014
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2015
|
+
| totalOfferQty | LONG | Interval sell quantity |
|
|
2016
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2017
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
2018
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2019
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
2020
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2021
|
+
| offerPrice | DOUBLE[] | List of ask prices |
|
|
2022
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2023
|
+
| offerQty | LONG[] | List of ask quantities |
|
|
2024
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2025
|
+
| highPrice | DOUBLE | Highest price |
|
|
2026
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2027
|
+
| lowPrice | DOUBLE | Lowest price |
|
|
2028
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2029
|
+
| signal | DOUBLE[] | Other fields list |
|
|
2030
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2031
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
2032
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2033
|
+
| settlementPrice | DOUBLE | Settlement price |
|
|
2034
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2035
|
+
| prevSettlementPrice | DOUBLE | Previous settlement price |
|
|
2036
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2037
|
+
|
|
2038
|
+
- Futures minute or daily frequency
|
|
2039
|
+
|
|
2040
|
+
The input parameter msg represents a K-line bar, with each K-line containing the following fields:
|
|
2041
|
+
|
|
2042
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2043
|
+
| Field | Type | Description |
|
|
2044
|
+
+=====================+===============+==========================================================+
|
|
2045
|
+
| symbol | SYMBOL | Futures code |
|
|
2046
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2047
|
+
| symbolSource | STRING | Exchange |
|
|
2048
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2049
|
+
| timestamp | TIMESTAMP | Timestamp |
|
|
2050
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2051
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
2052
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2053
|
+
| lastPrice | DOUBLE | Latest traded price |
|
|
2054
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2055
|
+
| upLimitPrice | DOUBLE | Upper limit price |
|
|
2056
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2057
|
+
| downLimitPrice | DOUBLE | Lower limit price |
|
|
2058
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2059
|
+
| totalBidQty | LONG | Interval buy quantity |
|
|
2060
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2061
|
+
| totalOfferQty | LONG | Interval sell quantity |
|
|
2062
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2063
|
+
| bidPrice | DOUBLE[] | List of bid prices |
|
|
2064
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2065
|
+
| bidQty | LONG[] | List of bid quantities |
|
|
2066
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2067
|
+
| offerPrice | DOUBLE[] | List of ask prices |
|
|
2068
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2069
|
+
| offerQty | LONG[] | List of ask quantities |
|
|
2070
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2071
|
+
| highPrice | DOUBLE | Highest price |
|
|
2072
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2073
|
+
| lowPrice | DOUBLE | Lowest price |
|
|
2074
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2075
|
+
| signal | DOUBLE[] | Other fields list |
|
|
2076
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2077
|
+
| prevClosePrice | DOUBLE | Previous closing price |
|
|
2078
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2079
|
+
| settlementPrice | DOUBLE | Settlement price |
|
|
2080
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2081
|
+
| prevSettlementPrice | DOUBLE | Previous settlement price |
|
|
2082
|
+
+---------------------+---------------+----------------------------------------------------------+
|
|
2083
|
+
|
|
2084
|
+
- Digital currency minute or daily frequency
|
|
2085
|
+
|
|
2086
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2087
|
+
| Field | Type | Description |
|
|
2088
|
+
+=====================+================+==================================================+
|
|
2089
|
+
| symbol | SYMBOL | Instrument code |
|
|
2090
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2091
|
+
| symbolSource | SYMBOL | Exchange |
|
|
2092
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2093
|
+
| tradeTime | TIMESTAMP | Timestamp |
|
|
2094
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2095
|
+
| tradingDay | DATE | Trading day / settlement date |
|
|
2096
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2097
|
+
| open | DECIMAL128(8) | Opening price |
|
|
2098
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2099
|
+
| low | DECIMAL128(8) | Lowest price |
|
|
2100
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2101
|
+
| high | DECIMAL128(8) | Highest price |
|
|
2102
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2103
|
+
| close | DECIMAL128(8) | Closing price |
|
|
2104
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2105
|
+
| volume | DECIMAL128(8) | Trading volume |
|
|
2106
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2107
|
+
| amount | DECIMAL128(8) | Trading amount |
|
|
2108
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2109
|
+
| upLimitPrice | DECIMAL128(8) | Upper limit price |
|
|
2110
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2111
|
+
| downLimitPrice | DECIMAL128(8) | Lower limit price |
|
|
2112
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2113
|
+
| signal | DOUBLE[] | Other fields list |
|
|
2114
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2115
|
+
| prevClosePrice | DECIMAL128(8) | Previous closing price |
|
|
2116
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2117
|
+
| settlementPrice | DECIMAL128(8) | Settlement price |
|
|
2118
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2119
|
+
| prevSettlementPrice | DECIMAL128(8) | Previous settlement price |
|
|
2120
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2121
|
+
| contractType | INT | Instrument type |
|
|
2122
|
+
| | | |
|
|
2123
|
+
| | | 0: Spot |
|
|
2124
|
+
| | | |
|
|
2125
|
+
| | | 1: Delivery contract |
|
|
2126
|
+
| | | |
|
|
2127
|
+
| | | 2: Perpetual contract |
|
|
2128
|
+
| | | |
|
|
2129
|
+
| | | 3: Option |
|
|
2130
|
+
+---------------------+----------------+--------------------------------------------------+
|
|
2131
|
+
|
|
2132
|
+
"""
|
|
2133
|
+
pass
|
|
2134
|
+
|
|
2135
|
+
def on_transaction(self, context, msg, indicator):
|
|
2136
|
+
"""The callback function is triggered upon receiving tick-level trade details.
|
|
2137
|
+
It is supported only for bonds traded on the Shanghai Stock Exchange.
|
|
2138
|
+
|
|
2139
|
+
Parameters
|
|
2140
|
+
----------
|
|
2141
|
+
context : _type_
|
|
2142
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
2143
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
2144
|
+
the backtesting engine maintains four internal variables within the context:
|
|
2145
|
+
|
|
2146
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
2147
|
+
|
|
2148
|
+
- context.tradeDate — Returns the current trading date.
|
|
2149
|
+
|
|
2150
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
2151
|
+
are aggregated into lower-frequency bars.
|
|
2152
|
+
|
|
2153
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
2154
|
+
msg : _type_
|
|
2155
|
+
Represents the market data. Each record is provided as either a dictionary object
|
|
2156
|
+
or a table (depending on the msg_as_table configuration).
|
|
2157
|
+
indicator : _type_
|
|
2158
|
+
A dictionary or nested dictionary with a structure consistent with the corresponding msg,
|
|
2159
|
+
containing the data subscribed to by the strategy.
|
|
2160
|
+
"""
|
|
2161
|
+
pass
|
|
2162
|
+
|
|
2163
|
+
def on_order(self, context, orders):
|
|
2164
|
+
"""The order update notification function is triggered when an order status changes.
|
|
2165
|
+
|
|
2166
|
+
Parameters
|
|
2167
|
+
----------
|
|
2168
|
+
context : _type_
|
|
2169
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
2170
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
2171
|
+
the backtesting engine maintains four internal variables within the context:
|
|
2172
|
+
|
|
2173
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
2174
|
+
|
|
2175
|
+
- context.tradeDate — Returns the current trading date.
|
|
2176
|
+
|
|
2177
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
2178
|
+
are aggregated into lower-frequency bars.
|
|
2179
|
+
|
|
2180
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
2181
|
+
orders : _type_
|
|
2182
|
+
A dictionary containing order information. The field structure varies
|
|
2183
|
+
depending on the asset type (for example, Shanghai Stock Exchange bonds
|
|
2184
|
+
have a different order field structure than other asset classes).
|
|
2185
|
+
|
|
2186
|
+
- Shanghai Stock Exchange Bonds
|
|
2187
|
+
|
|
2188
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2189
|
+
| Field | Type | Description |
|
|
2190
|
+
+==================+============+============================================================+
|
|
2191
|
+
| orderId | LONG | Order ID |
|
|
2192
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2193
|
+
| symbol | STRING | Security code |
|
|
2194
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2195
|
+
| timestamp | TIMESTAMP | Order timestamp |
|
|
2196
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2197
|
+
| bidQty | LONG | Bid quantity |
|
|
2198
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2199
|
+
| bidPrice | DOUBLE | Bid price |
|
|
2200
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2201
|
+
| bidTotalVolume | LONG | Executed bid quantity |
|
|
2202
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2203
|
+
| askQty | LONG | Ask quantity |
|
|
2204
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2205
|
+
| askPrice | DOUBLE | Ask price |
|
|
2206
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2207
|
+
| askTotalVolume | LONG | Executed ask quantity |
|
|
2208
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2209
|
+
| status | INT | Order status. Possible values: |
|
|
2210
|
+
| | | |
|
|
2211
|
+
| | | 4: Reported |
|
|
2212
|
+
| | | |
|
|
2213
|
+
| | | 0: Partially filled |
|
|
2214
|
+
| | | |
|
|
2215
|
+
| | | 1: Fully filled |
|
|
2216
|
+
| | | |
|
|
2217
|
+
| | | 2: Cancelled |
|
|
2218
|
+
| | | |
|
|
2219
|
+
| | | -1: Rejected |
|
|
2220
|
+
| | | |
|
|
2221
|
+
| | | -2: Cancel reject |
|
|
2222
|
+
| | | |
|
|
2223
|
+
| | | -3: Unfilled |
|
|
2224
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2225
|
+
| direction | INT | Order direction. Possible values: |
|
|
2226
|
+
| | | |
|
|
2227
|
+
| | | 1: Buy |
|
|
2228
|
+
| | | |
|
|
2229
|
+
| | | 2: Sell |
|
|
2230
|
+
| | | |
|
|
2231
|
+
| | | 3: Two-sided |
|
|
2232
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2233
|
+
| bidTradeValue | DOUBLE | Executed buy value |
|
|
2234
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2235
|
+
| askTradeValue | DOUBLE | Executed sell value |
|
|
2236
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2237
|
+
| label | STRING | Label, used to add remarks to the order |
|
|
2238
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2239
|
+
| updateTime | TIMESTAMP | Update time |
|
|
2240
|
+
+------------------+------------+------------------------------------------------------------+
|
|
2241
|
+
|
|
2242
|
+
- Other Assets (excluding SSE Bonds)
|
|
2243
|
+
|
|
2244
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2245
|
+
| Field | Type | Description |
|
|
2246
|
+
+================+============+============================================================+
|
|
2247
|
+
| orderId | LONG | Order ID |
|
|
2248
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2249
|
+
| symbol | STRING | Security code |
|
|
2250
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2251
|
+
| symbolSource | STRING | Exchange (futures only) |
|
|
2252
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2253
|
+
| timestamp | TIMESTAMP | Order timestamp |
|
|
2254
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2255
|
+
| qty | LONG | Order quantity |
|
|
2256
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2257
|
+
| price | DOUBLE | Order price |
|
|
2258
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2259
|
+
| status | INT | Order status. Possible values: |
|
|
2260
|
+
| | | |
|
|
2261
|
+
| | | 4: Reported |
|
|
2262
|
+
| | | |
|
|
2263
|
+
| | | 0: Partially filled |
|
|
2264
|
+
| | | |
|
|
2265
|
+
| | | 1: Fully filled |
|
|
2266
|
+
| | | |
|
|
2267
|
+
| | | 2: Cancelled |
|
|
2268
|
+
| | | |
|
|
2269
|
+
| | | -1: Rejected |
|
|
2270
|
+
| | | |
|
|
2271
|
+
| | | -2: Cancel reject |
|
|
2272
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2273
|
+
| direction | INT | Order direction. Possible values: |
|
|
2274
|
+
| | | |
|
|
2275
|
+
| | | 1: Open long |
|
|
2276
|
+
| | | |
|
|
2277
|
+
| | | 2: Open short |
|
|
2278
|
+
| | | |
|
|
2279
|
+
| | | 3: Close short |
|
|
2280
|
+
| | | |
|
|
2281
|
+
| | | 4: Close long |
|
|
2282
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2283
|
+
| tradeQty | LONG | Total executed quantity |
|
|
2284
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2285
|
+
| tradeValue | DOUBLE | Total executed value |
|
|
2286
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2287
|
+
| label | STRING | Label, used to add remarks to the order |
|
|
2288
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2289
|
+
| updateTime | TIMESTAMP | Update time |
|
|
2290
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2291
|
+
"""
|
|
2292
|
+
pass
|
|
2293
|
+
|
|
2294
|
+
def on_trade(self, context, trades):
|
|
2295
|
+
"""The trade update notification function is triggered when an order is executed.
|
|
2296
|
+
|
|
2297
|
+
Parameters
|
|
2298
|
+
----------
|
|
2299
|
+
context : _type_
|
|
2300
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
2301
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
2302
|
+
the backtesting engine maintains four internal variables within the context:
|
|
2303
|
+
|
|
2304
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
2305
|
+
|
|
2306
|
+
- context.tradeDate — Returns the current trading date.
|
|
2307
|
+
|
|
2308
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
2309
|
+
are aggregated into lower-frequency bars.
|
|
2310
|
+
|
|
2311
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
2312
|
+
trades : _type_
|
|
2313
|
+
A dictionary containing trade information. The field definitions are as follows:
|
|
2314
|
+
|
|
2315
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2316
|
+
| Field | Type | Description |
|
|
2317
|
+
+================+============+============================================================+
|
|
2318
|
+
| orderId | LONG | Order ID |
|
|
2319
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2320
|
+
| symbol | STRING | Security code |
|
|
2321
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2322
|
+
| tradePrice | DOUBLE | Trade price of the current execution |
|
|
2323
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2324
|
+
| tradeQty | LONG | Trade quantity of the current execution |
|
|
2325
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2326
|
+
| tradeValue | DOUBLE | Trade value of the current execution |
|
|
2327
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2328
|
+
| totalFee | DOUBLE | Total transaction fee |
|
|
2329
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2330
|
+
| bidTotalQty | LONG | Cumulative buy quantity |
|
|
2331
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2332
|
+
| bidTotalValue | DOUBLE | Cumulative buy value |
|
|
2333
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2334
|
+
| askTotalQty | LONG | Cumulative sell quantity |
|
|
2335
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2336
|
+
| askTotalValue | DOUBLE | Cumulative sell value |
|
|
2337
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2338
|
+
| direction | INT | Order direction. Possible values: |
|
|
2339
|
+
| | | |
|
|
2340
|
+
| | | 1: Open long |
|
|
2341
|
+
| | | |
|
|
2342
|
+
| | | 2: Open short |
|
|
2343
|
+
| | | |
|
|
2344
|
+
| | | 3: Close short |
|
|
2345
|
+
| | | |
|
|
2346
|
+
| | | 4: Close long |
|
|
2347
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2348
|
+
| tradeTime | TIMESTAMP | Trade time |
|
|
2349
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2350
|
+
| orderPrice | DOUBLE | Order price |
|
|
2351
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2352
|
+
| label | STRING | Label, used to add remarks to the order |
|
|
2353
|
+
+----------------+------------+------------------------------------------------------------+
|
|
2354
|
+
"""
|
|
2355
|
+
pass
|
|
2356
|
+
|
|
2357
|
+
def after_trading(self, context):
|
|
2358
|
+
"""The callback function is triggered at the end of each trading day.
|
|
2359
|
+
It can be used to summarize the day's trades, positions, and other statistics.
|
|
2360
|
+
|
|
2361
|
+
Note: This function is not required for cryptocurrency strategies.
|
|
2362
|
+
|
|
2363
|
+
Parameters
|
|
2364
|
+
----------
|
|
2365
|
+
context : _type_
|
|
2366
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
2367
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
2368
|
+
the backtesting engine maintains four internal variables within the context:
|
|
2369
|
+
|
|
2370
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
2371
|
+
|
|
2372
|
+
- context.tradeDate — Returns the current trading date.
|
|
2373
|
+
|
|
2374
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
2375
|
+
are aggregated into lower-frequency bars.
|
|
2376
|
+
|
|
2377
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
2378
|
+
"""
|
|
2379
|
+
pass
|
|
2380
|
+
|
|
2381
|
+
def finalize(self, context):
|
|
2382
|
+
"""The strategy termination callback function is triggered when the backtest completes.
|
|
2383
|
+
|
|
2384
|
+
Parameters
|
|
2385
|
+
----------
|
|
2386
|
+
context : _type_
|
|
2387
|
+
A dictionary representing the global context of the strategy. It is used to store
|
|
2388
|
+
and manage all user-defined variables within the strategy. In addition,
|
|
2389
|
+
the backtesting engine maintains four internal variables within the context:
|
|
2390
|
+
|
|
2391
|
+
- context.tradeTime — Returns the latest market timestamp.
|
|
2392
|
+
|
|
2393
|
+
- context.tradeDate — Returns the current trading date.
|
|
2394
|
+
|
|
2395
|
+
- context.BarTime — Returns the current bar timestamp when market snapshots
|
|
2396
|
+
are aggregated into lower-frequency bars.
|
|
2397
|
+
|
|
2398
|
+
- context.engine — Returns the internal instance handle of the backtesting engine.
|
|
2399
|
+
"""
|
|
2400
|
+
pass
|
|
2401
|
+
|
|
2402
|
+
|
|
2403
|
+
class StrategyTemplate(StrategyBase, StrategyInterface):
|
|
2404
|
+
def __init__(self, engine: BacktesterBase):
|
|
2405
|
+
self.engine = weakref.ref(engine)
|
|
2406
|
+
|
|
2407
|
+
@final
|
|
2408
|
+
@property
|
|
2409
|
+
def accounts(self):
|
|
2410
|
+
"""Get the corresponding account based on the account type, and perform operations
|
|
2411
|
+
or retrieve information using this account.
|
|
2412
|
+
|
|
2413
|
+
"""
|
|
2414
|
+
return self.engine().accounts
|
|
2415
|
+
|
|
2416
|
+
@final
|
|
2417
|
+
def submit_order(self, msg, label: str = "", order_type: int = 0, account_type: AccountType = AccountType.DEFAULT):
|
|
2418
|
+
"""This function can be called within callback functions to submit an order and returns the order ID.
|
|
2419
|
+
|
|
2420
|
+
Parameters
|
|
2421
|
+
----------
|
|
2422
|
+
msg : _type_
|
|
2423
|
+
A tuple representing the order information. The format varies depending on the asset type.
|
|
2424
|
+
Please see details in "Order Format by Asset Type"
|
|
2425
|
+
label : str, optional
|
|
2426
|
+
A STRING scalar, used to assign a label to the order for categorization.
|
|
2427
|
+
order_type : int, optional
|
|
2428
|
+
An INT scalar. Optional values:
|
|
2429
|
+
|
|
2430
|
+
- 0: Default, general order
|
|
2431
|
+
|
|
2432
|
+
- 5: Limit stop-loss/take-profit order
|
|
2433
|
+
|
|
2434
|
+
- 6: Market stop-loss/take-profit order
|
|
2435
|
+
|
|
2436
|
+
- 8: Two-sided quote order (supported only for futures and options)
|
|
2437
|
+
|
|
2438
|
+
Note: Types 5, 6, and 8 are algorithmic orders. Types 5 and 6 are supported
|
|
2439
|
+
for stocks, futures, and options. Type 8 is supported only for futures and
|
|
2440
|
+
options and can be enabled via the enableAlgoOrder configuration.
|
|
2441
|
+
account_type : AccountType, optional
|
|
2442
|
+
A STRING scalar, indicating the account type. Optional values:
|
|
2443
|
+
"spot", "futures", "option", representing spot, futures/perpetual,
|
|
2444
|
+
and option accounts, respectively. This parameter is applicable only for cryptocurrency strategies.
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
Order Format by Asset Type
|
|
2448
|
+
---------------------------
|
|
2449
|
+
- For orderType = 0, the format is:
|
|
2450
|
+
|
|
2451
|
+
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2452
|
+
| Asset Type | Format | Description |
|
|
2453
|
+
+=====================================+===========================================================================================================================+==================================================================================================================================================+
|
|
2454
|
+
| Stock (including | (Stock Code, Order Time, Order Type, Order Price, Order Quantity, Buy/Sell Direction) | **Buy/Sell Direction:** |
|
|
2455
|
+
| | | |
|
|
2456
|
+
| convertible bonds, funds) | | 1: Buy Open; 2: Sell Open; 3: Sell Close; 4: Buy Close |
|
|
2457
|
+
| | | |
|
|
2458
|
+
| | | **Order Type:** |
|
|
2459
|
+
| | | |
|
|
2460
|
+
| | | *Shanghai Stock Exchange:* |
|
|
2461
|
+
| | | |
|
|
2462
|
+
| | | - 0: Market order - best 5 levels immediate or cancel |
|
|
2463
|
+
| | | - 1: Market order - best 5 levels immediate and remaining as limit order |
|
|
2464
|
+
| | | - 2: Market order - best price on own side |
|
|
2465
|
+
| | | - 3: Market order - best price on opposite side |
|
|
2466
|
+
| | | - 5: Limit order |
|
|
2467
|
+
| | | - 6: Cancel order |
|
|
2468
|
+
| | | |
|
|
2469
|
+
| | | *Shenzhen Stock Exchange:* |
|
|
2470
|
+
| | | |
|
|
2471
|
+
| | | - 0: Market order - best 5 levels immediate or cancel |
|
|
2472
|
+
| | | - 1: Market order - immediate or cancel remaining order |
|
|
2473
|
+
| | | - 2: Market order - best price on own side |
|
|
2474
|
+
| | | - 3: Market order - best price on opposite side |
|
|
2475
|
+
| | | - 4: Market order - fill or kill order |
|
|
2476
|
+
| | | - 5: Limit order |
|
|
2477
|
+
| | | - 6: Cancel order |
|
|
2478
|
+
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2479
|
+
| Futures / Options | (Symbol, Exchange Code, Time, Order Type, Order Price, Stop Loss/Take Profit Price, Order Quantity, Buy/Sell Direction, | **Buy/Sell Direction:** |
|
|
2480
|
+
| | | |
|
|
2481
|
+
| | Order Validity) | 1: Buy Open; 2: Sell Open; 3: Sell Close; 4: Buy Close; 5: Option Exercise (only supported in multi-asset backtesting mode; |
|
|
2482
|
+
| | | underlyingCode must be configured in the instrument info table) |
|
|
2483
|
+
| | | |
|
|
2484
|
+
| | | **Order Type:** |
|
|
2485
|
+
| | | |
|
|
2486
|
+
| | | - 0: Market order - submitted at limit up/down price, time priority |
|
|
2487
|
+
| | | - 1: Market stop-loss order |
|
|
2488
|
+
| | | - 2: Market take-profit order |
|
|
2489
|
+
| | | - 3: Limit stop-loss order |
|
|
2490
|
+
| | | - 4: Limit take-profit order |
|
|
2491
|
+
| | | - 5: Limit order (default) |
|
|
2492
|
+
| | | - 6: Cancel order |
|
|
2493
|
+
| | | - 9: Auto-trading mode (only Buy Open=1 or Sell Open=2 supported): |
|
|
2494
|
+
| | | |
|
|
2495
|
+
| | | * If Buy Open (1): |
|
|
2496
|
+
| | | |
|
|
2497
|
+
| | | - If order quantity > longPosition, submit a Buy Open order. |
|
|
2498
|
+
| | | - If order quantity ≤ longPosition, submit a Sell Close (3) order. |
|
|
2499
|
+
| | | |
|
|
2500
|
+
| | | * If Sell Open (2): |
|
|
2501
|
+
| | | |
|
|
2502
|
+
| | | - If order quantity > shortPosition, submit a Sell Open order. |
|
|
2503
|
+
| | | - If order quantity ≤ shortPosition, submit a Buy Close (4) order. |
|
|
2504
|
+
| | | |
|
|
2505
|
+
| | | **Order Validity:** |
|
|
2506
|
+
| | | |
|
|
2507
|
+
| | | - 0: Good for the day (default) |
|
|
2508
|
+
| | | - 1: Fill or Kill (FOK) |
|
|
2509
|
+
| | | - 2: Fill and Kill (FAK) |
|
|
2510
|
+
| | | |
|
|
2511
|
+
| | | Stop loss/take profit prices are currently not supported and default to 0. |
|
|
2512
|
+
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2513
|
+
| Margin Trading / Securities Lending | (Stock Code, Order Time, Order Type, Order Price, Order Quantity, Buy/Sell Flag) | **Order Type:** |
|
|
2514
|
+
| | | |
|
|
2515
|
+
| | | - 0: Market order |
|
|
2516
|
+
| | | - 5: Limit order |
|
|
2517
|
+
| | | |
|
|
2518
|
+
| | | **Buy/Sell Flag:** |
|
|
2519
|
+
| | | |
|
|
2520
|
+
| | | - 1: Collateral purchase |
|
|
2521
|
+
| | | - 2: Collateral sale |
|
|
2522
|
+
| | | - 3: Margin purchase |
|
|
2523
|
+
| | | - 4: Short selling |
|
|
2524
|
+
| | | - 5: Direct repayment |
|
|
2525
|
+
| | | - 6: Sell-to-repay |
|
|
2526
|
+
| | | - 7: Direct stock return |
|
|
2527
|
+
| | | - 8: Buy-to-return |
|
|
2528
|
+
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2529
|
+
| Bond | (Symbol, Order Time, Order Type, Clearing Speed, Bid Price, Bid Volume, Ask Price, Ask Volume, Buy/Sell Direction, | **Buy/Sell Flag:** |
|
|
2530
|
+
| | | |
|
|
2531
|
+
| | User Order ID, Channel) | - 1: Buy Open |
|
|
2532
|
+
| | | - 2: Sell Close |
|
|
2533
|
+
| | | - 3: Two-way quote |
|
|
2534
|
+
| | | |
|
|
2535
|
+
| | | **Order Type:** |
|
|
2536
|
+
| | | |
|
|
2537
|
+
| | | - 1: Limit order |
|
|
2538
|
+
| | | - 3: Market-to-cancel order |
|
|
2539
|
+
| | | - 4: Market-to-limit order |
|
|
2540
|
+
| | | - 5: Elastic order |
|
|
2541
|
+
| | | - 7: FAK - execute immediately, cancel unfilled portion |
|
|
2542
|
+
| | | - 8: FOK - execute immediately in full, cancel otherwise |
|
|
2543
|
+
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2544
|
+
| Cryptocurrency / Multi-Asset | (Symbol, Exchange Code, Time, Order Type, Order Price, Stop Loss Price, Take Profit Price, Order Quantity, Buy/Sell | **Buy/Sell Direction:** |
|
|
2545
|
+
| | | |
|
|
2546
|
+
| | Direction, Slippage, Order Validity, Expiration Time) | 1: Buy Open; 2: Sell Open; 3: Sell Close; 4: Buy Close |
|
|
2547
|
+
| | | |
|
|
2548
|
+
| | | **Order Type:** |
|
|
2549
|
+
| | | |
|
|
2550
|
+
| | | - 5: Limit order (default) |
|
|
2551
|
+
| | | - 0: Market order - submitted at limit up/down price, time priority |
|
|
2552
|
+
| | | |
|
|
2553
|
+
| | | **Order Validity:** |
|
|
2554
|
+
| | | |
|
|
2555
|
+
| | | - 0: Good for the day (default) |
|
|
2556
|
+
| | | - 1: Fill or Kill (FOK) |
|
|
2557
|
+
| | | - 2: Fill and Kill (FAK) |
|
|
2558
|
+
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2559
|
+
|
|
2560
|
+
- For orderType = 5 or 6, the tuple format is: (Symbol, Exchange Code, Time, Order Type, Order Price, Stop Loss Price,
|
|
2561
|
+
Take Profit Price, Order Quantity, Buy/Sell Direction, Slippage, Order Validity, Expiration Time).
|
|
2562
|
+
|
|
2563
|
+
- For orderType = 8, the format is:
|
|
2564
|
+
|
|
2565
|
+
+------------------+---------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2566
|
+
| Asset Type | Format | Description |
|
|
2567
|
+
+==================+=========================================================+======================================================================================================================================================+
|
|
2568
|
+
| Futures / Options| (Contract Code, Exchange Code, Time, Order Type, | **Notes:** |
|
|
2569
|
+
| | Buy Open/Close Flag, Buy Price, Buy Quantity, | The fields *bidDifftolerance*, *askDifftolerance*, and *quantityAllowed* are reserved. |
|
|
2570
|
+
| | Sell Open/Close Flag, Sell Price, Sell Quantity, | |
|
|
2571
|
+
| | bidDifftolerance, askDifftolerance, quantityAllowed) | **Buy/Sell Direction:** |
|
|
2572
|
+
| | | |
|
|
2573
|
+
| | | 1: Buy open |
|
|
2574
|
+
| | | |
|
|
2575
|
+
| | | 2: Sell open |
|
|
2576
|
+
| | | |
|
|
2577
|
+
| | | 3: Sell close |
|
|
2578
|
+
| | | |
|
|
2579
|
+
| | | 4: Buy close |
|
|
2580
|
+
| | | |
|
|
2581
|
+
| | | 5: Option exercise (only supported in multi-asset backtesting mode; the *underlyingCode* field in the basic information table must be configured) |
|
|
2582
|
+
| | | |
|
|
2583
|
+
| | | **Order Type:** |
|
|
2584
|
+
| | | |
|
|
2585
|
+
| | | 0: Market order — submitted at limit up or limit down price, following time priority |
|
|
2586
|
+
| | | |
|
|
2587
|
+
| | | 1: Market stop-loss order |
|
|
2588
|
+
| | | |
|
|
2589
|
+
| | | 2: Market take-profit order |
|
|
2590
|
+
| | | |
|
|
2591
|
+
| | | 3: Limit stop-loss order |
|
|
2592
|
+
| | | |
|
|
2593
|
+
| | | 4: Limit take-profit order |
|
|
2594
|
+
| | | |
|
|
2595
|
+
| | | 5: Limit order (default) |
|
|
2596
|
+
| | | |
|
|
2597
|
+
| | | 6: Cancel order |
|
|
2598
|
+
| | | |
|
|
2599
|
+
| | | **Order Validity:** |
|
|
2600
|
+
| | | |
|
|
2601
|
+
| | | 0: Good for the day (default) |
|
|
2602
|
+
| | | |
|
|
2603
|
+
| | | 1: Fill or kill (FOK) — execute fully immediately or cancel |
|
|
2604
|
+
| | | |
|
|
2605
|
+
| | | 2: Fill-and-kill (FAK) — execute immediately, cancel any unfilled portion |
|
|
2606
|
+
| | | |
|
|
2607
|
+
| | | Stop-loss and take-profit prices are currently not supported (default value: 0). |
|
|
2608
|
+
+------------------+---------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2609
|
+
|
|
2610
|
+
"""
|
|
2611
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, msg, label, sf_scalar(order_type, type="INT"), _convert_account(account_type))
|
|
2612
|
+
|
|
2613
|
+
@final
|
|
2614
|
+
def cancel_order(self, *, symbol=None, orders=None, label=None):
|
|
2615
|
+
"""Cancel orders.
|
|
2616
|
+
|
|
2617
|
+
Parameters
|
|
2618
|
+
----------
|
|
2619
|
+
symbol : _type_, optional
|
|
2620
|
+
A STRING scalar, indicating the security code of the orders to be canceled.
|
|
2621
|
+
The default value is None.
|
|
2622
|
+
orders : _type_, optional
|
|
2623
|
+
An INTEGRAL vector, indicating the list of order IDs to be canceled. The default value is None.
|
|
2624
|
+
label : _type_, optional
|
|
2625
|
+
A STRING scalar, indicating the label or remark associated with the
|
|
2626
|
+
orders to be canceled. The default value is None.
|
|
2627
|
+
"""
|
|
2628
|
+
return plugin_backtest_cancelOrder(self.engine().engine_handle, symbol, orders, label)
|
|
2629
|
+
|
|
2630
|
+
@final
|
|
2631
|
+
def get_open_orders(self, symbol=None, orders=None, label=None, output_queue_position: bool = False):
|
|
2632
|
+
"""Query the information of unfilled orders.
|
|
2633
|
+
|
|
2634
|
+
- If symbol is specified, queries unfilled orders for that security.
|
|
2635
|
+
|
|
2636
|
+
- If symbol is empty but orders is specified, queries unfilled orders in the provided orders list.
|
|
2637
|
+
|
|
2638
|
+
- If both symbol and orders are empty, queries unfilled orders specified by label.
|
|
2639
|
+
|
|
2640
|
+
Parameters
|
|
2641
|
+
----------
|
|
2642
|
+
symbol : _type_, optional
|
|
2643
|
+
A STRING scalar, indicating the security code. The default value is None.
|
|
2644
|
+
orders : _type_, optional
|
|
2645
|
+
An INTEGRAL vector, indicating a list of order IDs. The default value is None.
|
|
2646
|
+
label : _type_, optional
|
|
2647
|
+
A STRING scalar, indicating a remark or label. The default value is None.
|
|
2648
|
+
output_queue_position : bool, optional
|
|
2649
|
+
A BOOL scalar, indicating whether to output detailed information,
|
|
2650
|
+
including openVolumeWithBetterPrice, openVolumeWithWorsePrice,
|
|
2651
|
+
openVolumeAtOrderPrice, priorOpenVolumeAtOrderPrice, and depthWithBetterPrice.
|
|
2652
|
+
Default is false, indicating no detailed output. This parameter is supported only for stocks and futures.
|
|
2653
|
+
|
|
2654
|
+
Returns
|
|
2655
|
+
-------
|
|
2656
|
+
Dictionary
|
|
2657
|
+
Returns a dictionary or table.
|
|
2658
|
+
|
|
2659
|
+
For all asset types except Shanghai Stock Exchange bonds, the table structure is as follows:
|
|
2660
|
+
|
|
2661
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2662
|
+
| Key | Value Type | Description |
|
|
2663
|
+
+=============================+=============+==========================================================================================================================================+
|
|
2664
|
+
| orderId | LONG | Order ID |
|
|
2665
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2666
|
+
| timestamp | TIMESTAMP | Time |
|
|
2667
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2668
|
+
| symbol | STRING | Symbol code |
|
|
2669
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2670
|
+
| price | DOUBLE | Order price |
|
|
2671
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2672
|
+
| totalQty | LONG | Total quantity of the order |
|
|
2673
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2674
|
+
| openQty | LONG | Remaining quantity of the order |
|
|
2675
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2676
|
+
| direction | INT | 1: Buy open; 2: Sell open; 3: Sell close; 4: Buy close |
|
|
2677
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2678
|
+
| isMacthing | INT | Indicates whether the order has reached the matching time |
|
|
2679
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2680
|
+
| openVolumeWithBetterPrice | LONG | Total quantity of unfilled orders with better prices (returned only when ``outputQueuePosition = true``) |
|
|
2681
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2682
|
+
| openVolumeWithWorsePrice | LONG | Total quantity of unfilled orders with worse prices (returned only when ``outputQueuePosition = true``) |
|
|
2683
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2684
|
+
| openVolumeAtOrderPrice | LONG | Total quantity of unfilled orders at the same price (returned only when ``outputQueuePosition = true``) |
|
|
2685
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2686
|
+
| priorOpenVolumeAtOrderPrice | LONG | Total quantity of unfilled orders at the same price placed earlier (returned only when ``outputQueuePosition = true``) |
|
|
2687
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2688
|
+
| depthVolumeWithBetterPrice | INT | Depth level of unfilled quotes with better prices (returned only when ``outputQueuePosition = true``) |
|
|
2689
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2690
|
+
| updateTime | TIMESTAMP | Last update time |
|
|
2691
|
+
+-----------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
|
|
2692
|
+
|
|
2693
|
+
For Shanghai Stock Exchange bonds, the table structure is as follows:
|
|
2694
|
+
|
|
2695
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2696
|
+
| Name | Type | Description |
|
|
2697
|
+
+================+=============+====================================================+
|
|
2698
|
+
| orderId | LONG | Order ID |
|
|
2699
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2700
|
+
| time | TIMESTAMP | Timestamp |
|
|
2701
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2702
|
+
| symbol | STRING | Security code |
|
|
2703
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2704
|
+
| bidPrice | DOUBLE | Bid price |
|
|
2705
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2706
|
+
| bidTotalQty | LONG | Total bid quantity placed by the user |
|
|
2707
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2708
|
+
| bidRemainQty | LONG | Remaining bid quantity of the user order |
|
|
2709
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2710
|
+
| askPrice | DOUBLE | Ask price |
|
|
2711
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2712
|
+
| askTotalQty | LONG | Total ask quantity placed by the user |
|
|
2713
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2714
|
+
| askRemainQty | LONG | Remaining ask quantity of the user order |
|
|
2715
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2716
|
+
| direction | INT | Trade direction: |
|
|
2717
|
+
| | | |
|
|
2718
|
+
| | | 1: Buy |
|
|
2719
|
+
| | | |
|
|
2720
|
+
| | | 2: Sell |
|
|
2721
|
+
| | | |
|
|
2722
|
+
| | | 3: Two-way |
|
|
2723
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2724
|
+
| label | STRING | Remarks |
|
|
2725
|
+
+----------------+-------------+----------------------------------------------------+
|
|
2726
|
+
|
|
2727
|
+
"""
|
|
2728
|
+
return plugin_backtest_backtestGetOpenOrders(self.engine().engine_handle, symbol, orders, label, output_queue_position)
|
|
2729
|
+
|
|
2730
|
+
@final
|
|
2731
|
+
@property
|
|
2732
|
+
def universe(self):
|
|
2733
|
+
"""Set the symbol pool for the engine.
|
|
2734
|
+
|
|
2735
|
+
"""
|
|
2736
|
+
return self.engine().universe
|
|
2737
|
+
|
|
2738
|
+
@final
|
|
2739
|
+
@universe.setter
|
|
2740
|
+
def universe(self, val: List[str]):
|
|
2741
|
+
self.engine().universe = val
|
|
2742
|
+
|
|
2743
|
+
@final
|
|
2744
|
+
def subscribe_indicator(self, market_data_type: MarketDataType, metrics, account_type: AccountType = AccountType.DEFAULT):
|
|
2745
|
+
"""Set the market data indicators to subscribe.
|
|
2746
|
+
|
|
2747
|
+
Parameters
|
|
2748
|
+
----------
|
|
2749
|
+
market_data_type : MarketDataType
|
|
2750
|
+
The type of market data to subscribe to. Optional values:
|
|
2751
|
+
|
|
2752
|
+
- SNAPSHOT: Snapshot
|
|
2753
|
+
|
|
2754
|
+
- TICK: Tick-by-tick order data
|
|
2755
|
+
|
|
2756
|
+
- KLINE / OHLC: K-line data
|
|
2757
|
+
|
|
2758
|
+
- TRADE: Tick-by-tick trade details
|
|
2759
|
+
|
|
2760
|
+
- SNAPSHOT_KLINE / SNAPSHOT_OHLC: Snapshot-based K-line data
|
|
2761
|
+
metrics : _type_
|
|
2762
|
+
Metaprogramming object, can be generated using ``sf.meta_code``.
|
|
2763
|
+
account_type : AccountType, optional
|
|
2764
|
+
A STRING scalar, indicating the account type.
|
|
2765
|
+
|
|
2766
|
+
"""
|
|
2767
|
+
return plugin_backtest_subscribeIndicator(self.engine().engine_handle, market_data_type.value, metrics, _convert_account(account_type))
|
|
2768
|
+
|
|
2769
|
+
|
|
2770
|
+
T = TypeVar("T", bound=StrategyInterface)
|
|
2771
|
+
|
|
2772
|
+
|
|
2773
|
+
class Backtester(BacktesterBase):
|
|
2774
|
+
def __init__(self, strategy_cls: Type[T], config: BacktestBasicConfig):
|
|
2775
|
+
self.strategy_cls = strategy_cls
|
|
2776
|
+
self._config = config
|
|
2777
|
+
self._universe = None
|
|
2778
|
+
self.engine_handle = None
|
|
2779
|
+
|
|
2780
|
+
self.strategy = self.strategy_cls(self)
|
|
2781
|
+
|
|
2782
|
+
if hasattr(strategy_cls, "context"):
|
|
2783
|
+
config_d = self._extract_config(config, strategy_cls.context)
|
|
2784
|
+
else:
|
|
2785
|
+
config_d = self._extract_config(config)
|
|
2786
|
+
|
|
2787
|
+
callback_d = sf_dictionary(key_type="STRING", val_type="ANY")
|
|
2788
|
+
|
|
2789
|
+
wkptr = weakref.ref(self)
|
|
2790
|
+
|
|
2791
|
+
def real_initialize(context):
|
|
2792
|
+
wkptr().engine_handle = context["engine"]
|
|
2793
|
+
F_swordfish_udf(getattr(wkptr().strategy, "initialize"))(context)
|
|
2794
|
+
|
|
2795
|
+
callback_d["initialize"] = F_swordfish_udf(real_initialize)
|
|
2796
|
+
self._check_valid_callback(callback_d, "beforeTrading", "before_trading")
|
|
2797
|
+
self._check_valid_callback(callback_d, "onTick", "on_tick")
|
|
2798
|
+
self._check_valid_callback(callback_d, "onSnapshot", "on_snapshot")
|
|
2799
|
+
self._check_valid_callback(callback_d, "onBar", "on_bar")
|
|
2800
|
+
self._check_valid_callback(callback_d, "onTransaction", "on_transaction")
|
|
2801
|
+
self._check_valid_callback(callback_d, "onOrder", "on_order")
|
|
2802
|
+
self._check_valid_callback(callback_d, "onTrade", "on_trade")
|
|
2803
|
+
self._check_valid_callback(callback_d, "afterTrading", "after_trading")
|
|
2804
|
+
self._check_valid_callback(callback_d, "finalize", "finalize")
|
|
2805
|
+
on_timer_d = sf_dictionary(key_type="SECOND", val_type="ANY")
|
|
2806
|
+
for k in self.strategy_cls._timer_funcs:
|
|
2807
|
+
child_method = getattr(self.strategy, k)
|
|
2808
|
+
t = self.strategy_cls._timer_funcs[k]
|
|
2809
|
+
if isinstance(t, sf_data.Vector):
|
|
2810
|
+
for tt in t:
|
|
2811
|
+
on_timer_d[tt] = F_swordfish_udf(child_method)
|
|
2812
|
+
else:
|
|
2813
|
+
on_timer_d[self.strategy_cls._timer_funcs[k]] = F_swordfish_udf(child_method)
|
|
2814
|
+
callback_d["onTimer"] = on_timer_d
|
|
2815
|
+
|
|
2816
|
+
if hasattr(strategy_cls, "callback_d"):
|
|
2817
|
+
for k, v in strategy_cls.callback_d.items():
|
|
2818
|
+
callback_d[k] = v
|
|
2819
|
+
|
|
2820
|
+
security_reference = self._config.get("security_reference", None)
|
|
2821
|
+
engine_name = _generate_name()
|
|
2822
|
+
while engine_name in plugin_backtest_getBacktestEngineList():
|
|
2823
|
+
engine_name = _generate_name()
|
|
2824
|
+
if isinstance(config["cash"], dict):
|
|
2825
|
+
self.accounts = {}
|
|
2826
|
+
for k in config.cash.keys():
|
|
2827
|
+
if isinstance(k, list) or isinstance(k, tuple):
|
|
2828
|
+
for x in k:
|
|
2829
|
+
self.accounts[x] = Account(x, self)
|
|
2830
|
+
else:
|
|
2831
|
+
self.accounts[k] = Account(k, self)
|
|
2832
|
+
else:
|
|
2833
|
+
self.accounts = {
|
|
2834
|
+
AccountType.DEFAULT: Account(AccountType.DEFAULT, self)
|
|
2835
|
+
}
|
|
2836
|
+
self.engine_handle = plugin_backtest_createBacktester(engine_name, config_d, callback_d, False, security_reference)
|
|
2837
|
+
self.engine_name = engine_name
|
|
2838
|
+
|
|
2839
|
+
def __del__(self):
|
|
2840
|
+
if self.engine_handle is None:
|
|
2841
|
+
return
|
|
2842
|
+
if not Runtime().check():
|
|
2843
|
+
return
|
|
2844
|
+
if self.engine_name in plugin_backtest_getBacktestEngineList().keys():
|
|
2845
|
+
plugin_backtest_dropBacktestEngine(self.engine_name)
|
|
2846
|
+
self.engine_handle = None
|
|
2847
|
+
|
|
2848
|
+
@final
|
|
2849
|
+
def _extract_config(self, config: BacktestBasicConfig, context=None):
|
|
2850
|
+
config_d = sf_dictionary(key_type="STRING", val_type="ANY")
|
|
2851
|
+
config_d["startDate"] = sf_scalar(config["start_date"], type="DATE")
|
|
2852
|
+
config_d["endDate"] = sf_scalar(config["end_date"], type="DATE")
|
|
2853
|
+
config_d["strategyGroup"] = config["asset_type"].value
|
|
2854
|
+
config_d["dataType"] = sf_scalar(config["data_type"].value, type="INT")
|
|
2855
|
+
if isinstance(config["cash"], dict):
|
|
2856
|
+
cash_d = sf_dictionary(key_type="STRING", val_type="ANY")
|
|
2857
|
+
for k, v in config.cash.items():
|
|
2858
|
+
if isinstance(k, list) or isinstance(k, tuple):
|
|
2859
|
+
cash_d[", ".join([_.value for _ in k])] = sf_scalar(v, type="DOUBLE")
|
|
2860
|
+
else:
|
|
2861
|
+
cash_d[k.value] = sf_scalar(v, type="DOUBLE")
|
|
2862
|
+
config_d["cash"] = cash_d
|
|
2863
|
+
else:
|
|
2864
|
+
config_d["cash"] = sf_scalar(config["cash"], type="DOUBLE")
|
|
2865
|
+
if hasattr(config, "matching_mode") and config["matching_mode"] is not None:
|
|
2866
|
+
config_d["matchingMode"] = sf_scalar(config["matching_mode"].value, type="INT")
|
|
2867
|
+
if hasattr(config, "universe"):
|
|
2868
|
+
config_d["universe"] = config["universe"]
|
|
2869
|
+
if hasattr(config, "context"):
|
|
2870
|
+
if context is not None:
|
|
2871
|
+
new_context = sf_dictionary(config["context"], key_type="STRING", val_type="ANY")
|
|
2872
|
+
for k, v in new_context.items():
|
|
2873
|
+
context[k] = v
|
|
2874
|
+
config_d["context"] = context
|
|
2875
|
+
else:
|
|
2876
|
+
config_d["context"] = sf_dictionary(config["context"], key_type="STRING", val_type="ANY")
|
|
2877
|
+
elif context is not None:
|
|
2878
|
+
config_d["context"] = context
|
|
2879
|
+
if hasattr(config, "latency"):
|
|
2880
|
+
config_d["latency"] = sf_scalar(config["latency"], type="INT")
|
|
2881
|
+
if hasattr(config, "benchmark"):
|
|
2882
|
+
config_d["benchmark"] = sf_scalar(config["benchmark"], type="STRING")
|
|
2883
|
+
if hasattr(config, "data_retention_window"):
|
|
2884
|
+
if isinstance(config["data_retention_window"], int):
|
|
2885
|
+
config_d["dataRetentionWindow"] = sf_scalar(config["data_retention_window"], type="INT")
|
|
2886
|
+
elif isinstance(config["data_retention_window"], str):
|
|
2887
|
+
config_d["dataRetentionWindow"] = sf_scalar(config["data_retention_window"], type="STRING")
|
|
2888
|
+
else:
|
|
2889
|
+
config_d["dataRetentionWindow"] = config["data_retention_window"]
|
|
2890
|
+
if hasattr(config, "is_backtest_mode"):
|
|
2891
|
+
config_d["isBacktestMode"] = sf_scalar(config["is_backtest_mode"], type="BOOL")
|
|
2892
|
+
if hasattr(config, "enable_indicator_optimize"):
|
|
2893
|
+
config_d["enableIndicatorOptimize"] = sf_scalar(config["enable_indicator_optimize"], type="BOOL")
|
|
2894
|
+
if hasattr(config, "add_time_column_in_indicator"):
|
|
2895
|
+
config_d["addTimeColumnInIndicator"] = sf_scalar(config["add_time_column_in_indicator"], type="BOOL")
|
|
2896
|
+
if hasattr(config, "orderbook_matching_ratio"):
|
|
2897
|
+
config_d["orderBookMatchingRatio"] = sf_scalar(config["orderbook_matching_ratio"], type="DOUBLE")
|
|
2898
|
+
if hasattr(config, "matching_ratio"):
|
|
2899
|
+
config_d["matchingRatio"] = sf_scalar(config["matching_ratio"], type="DOUBLE")
|
|
2900
|
+
if hasattr(config, "output_order_info"):
|
|
2901
|
+
config_d["outputOrderInfo"] = sf_scalar(config["output_order_info"], type="BOOL")
|
|
2902
|
+
|
|
2903
|
+
# STOCK config
|
|
2904
|
+
if hasattr(config, "set_last_day_position"):
|
|
2905
|
+
config_d["setLastDayPosition"] = config["set_last_day_position"] # Table
|
|
2906
|
+
if hasattr(config, "prev_close_price"):
|
|
2907
|
+
config_d["prevClosePrice"] = config["prev_close_price"] # Table
|
|
2908
|
+
if hasattr(config, "enable_subscription_to_tick_quotes"):
|
|
2909
|
+
config_d["enableSubscriptionToTickQuotes"] = sf_scalar(config["enable_subscription_to_tick_quotes"], type="BOOL")
|
|
2910
|
+
if hasattr(config, "commission"):
|
|
2911
|
+
config_d["commission"] = sf_scalar(config["commission"], type="DOUBLE")
|
|
2912
|
+
if hasattr(config, "tax"):
|
|
2913
|
+
config_d["tax"] = sf_scalar(config["tax"], type="DOUBLE")
|
|
2914
|
+
if hasattr(config, "output_queue_position"):
|
|
2915
|
+
config_d["outputQueuePosition"] = sf_scalar(config["output_queue_position"], type="INT")
|
|
2916
|
+
if hasattr(config, "stock_dividend"):
|
|
2917
|
+
config_d["stockDividend"] = config["stock_dividend"] # Table
|
|
2918
|
+
if hasattr(config, "frequency"):
|
|
2919
|
+
config_d["frequency"] = sf_scalar(config["frequency"], type="INT")
|
|
2920
|
+
if hasattr(config, "callback_for_snapshot"):
|
|
2921
|
+
config_d["callbackForSnapshot"] = sf_scalar(config["callback_for_snapshot"], type="INT")
|
|
2922
|
+
|
|
2923
|
+
# MARGIN config
|
|
2924
|
+
if hasattr(config, "line_of_credit"):
|
|
2925
|
+
config_d["lineOfCredit"] = sf_scalar(config["line_of_credit"], type="DOUBLE")
|
|
2926
|
+
if hasattr(config, "margin_trading_interest_rate"):
|
|
2927
|
+
config_d["marginTradingInterestRate"] = sf_scalar(config["margin_trading_interest_rate"], type="DOUBLE")
|
|
2928
|
+
if hasattr(config, "secu_lending_interest_rate"):
|
|
2929
|
+
config_d["secuLendingInterestRate"] = sf_scalar(config["secu_lending_interest_rate"], type="DOUBLE")
|
|
2930
|
+
if hasattr(config, "maintenance_margin"):
|
|
2931
|
+
if isinstance(config, MarginConfig):
|
|
2932
|
+
config_d["maintenanceMargin"] = sf_vector(config["maintenance_margin"], type="DOUBLE")
|
|
2933
|
+
else:
|
|
2934
|
+
config_d["maintenanceMargin"] = sf_scalar(config["maintenance_margin"], type="DOUBLE")
|
|
2935
|
+
if hasattr(config, "long_concentration"):
|
|
2936
|
+
if config["long_concentration"] is None:
|
|
2937
|
+
config_d["longConcentration"] = sf_data.Nothing
|
|
2938
|
+
else:
|
|
2939
|
+
config_d["longConcentration"] = sf_vector(config["long_concentration"], type="DOUBLE")
|
|
2940
|
+
if hasattr(config, "short_concentration"):
|
|
2941
|
+
if config["short_concentration"] is None:
|
|
2942
|
+
config_d["shortConcentration"] = sf_data.Nothing
|
|
2943
|
+
else:
|
|
2944
|
+
config_d["shortConcentration"] = sf_vector(config["short_concentration"], type="DOUBLE")
|
|
2945
|
+
if hasattr(config, "repay_without_margin_buy"):
|
|
2946
|
+
config_d["repayWithoutMarginBuy"] = sf_scalar(config["repay_without_margin_buy"], type="BOOL")
|
|
2947
|
+
# set_last_day_position (stock)
|
|
2948
|
+
|
|
2949
|
+
# BOND config
|
|
2950
|
+
|
|
2951
|
+
# OPTION config
|
|
2952
|
+
# frequency (stock)
|
|
2953
|
+
# callback_for_snapshot (stock)
|
|
2954
|
+
# maintenance_margin (margin)
|
|
2955
|
+
|
|
2956
|
+
# FUTURES config
|
|
2957
|
+
# frequency (stock)
|
|
2958
|
+
if hasattr(config, "futures_type") and config["futures_type"] is not None:
|
|
2959
|
+
config_d["futuresType"] = sf_scalar(config["futures_type"], type="STRING")
|
|
2960
|
+
if hasattr(config, "enable_algo_order"):
|
|
2961
|
+
config_d["enableAlgoOrder"] = sf_scalar(config["enable_algo_order"], type="BOOL")
|
|
2962
|
+
# callback_for_snapshot (stock)
|
|
2963
|
+
# maintenance_margin (margin)
|
|
2964
|
+
|
|
2965
|
+
# CRYPTO config
|
|
2966
|
+
if hasattr(config, "funding_rate"):
|
|
2967
|
+
config_d["fundingRate"] = config["funding_rate"] # Table
|
|
2968
|
+
return config_d
|
|
2969
|
+
|
|
2970
|
+
@final
|
|
2971
|
+
def append_data(self, data):
|
|
2972
|
+
"""Insert market data to execute the backtesting strategy.
|
|
2973
|
+
|
|
2974
|
+
Parameters
|
|
2975
|
+
----------
|
|
2976
|
+
data : _type_
|
|
2977
|
+
A table containing market data.
|
|
2978
|
+
"""
|
|
2979
|
+
plugin_backtest_appendQuotationMsg(self.engine_handle, data)
|
|
2980
|
+
|
|
2981
|
+
@final
|
|
2982
|
+
def append_end(self):
|
|
2983
|
+
"""Insert end marker to indicate the end of market data.
|
|
2984
|
+
"""
|
|
2985
|
+
plugin_backtest_appendEndMarker(self.engine_handle)
|
|
2986
|
+
|
|
2987
|
+
@final
|
|
2988
|
+
def _check_valid_callback(self, callback_d, callback_name: str, method_name: str):
|
|
2989
|
+
parent_method = getattr(StrategyInterface, method_name, None)
|
|
2990
|
+
child_method = getattr(self.strategy_cls, method_name, None)
|
|
2991
|
+
if child_method is not parent_method:
|
|
2992
|
+
callback_d[callback_name] = F_swordfish_udf(getattr(self.strategy, method_name))
|
|
2993
|
+
|
|
2994
|
+
@final
|
|
2995
|
+
@property
|
|
2996
|
+
def context_dict(self):
|
|
2997
|
+
"""Return the logical context.
|
|
2998
|
+
|
|
2999
|
+
"""
|
|
3000
|
+
return plugin_backtest_getContextDict(self.engine_handle)
|
|
3001
|
+
|
|
3002
|
+
@final
|
|
3003
|
+
@property
|
|
3004
|
+
def universe(self):
|
|
3005
|
+
"""Set the symbol pool for the engine.
|
|
3006
|
+
|
|
3007
|
+
"""
|
|
3008
|
+
return self._universe
|
|
3009
|
+
|
|
3010
|
+
@final
|
|
3011
|
+
@universe.setter
|
|
3012
|
+
def universe(self, val: List[str]):
|
|
3013
|
+
plugin_backtest_setUniverse(self.engine_handle, val)
|
|
3014
|
+
self._universe = val
|
|
3015
|
+
|
|
3016
|
+
@final
|
|
3017
|
+
@property
|
|
3018
|
+
def config(self) -> sf_data.Dictionary:
|
|
3019
|
+
return plugin_backtest_getConfig(self.engine_handle)
|
|
3020
|
+
|
|
3021
|
+
|
|
3022
|
+
class AlgoOrderMixin(StrategyBase):
|
|
3023
|
+
|
|
3024
|
+
@final
|
|
3025
|
+
def submit_limit_tp_sl_order(
|
|
3026
|
+
self,
|
|
3027
|
+
code: str,
|
|
3028
|
+
exchange: str,
|
|
3029
|
+
time: Timestamp,
|
|
3030
|
+
order_type: int,
|
|
3031
|
+
order_price: float,
|
|
3032
|
+
stop_loss_price: float,
|
|
3033
|
+
take_profit_price: float,
|
|
3034
|
+
quantity: int,
|
|
3035
|
+
direct: int,
|
|
3036
|
+
slippage: float,
|
|
3037
|
+
order_validity: int,
|
|
3038
|
+
expiration_time: Timestamp,
|
|
3039
|
+
/,
|
|
3040
|
+
label: str = "",
|
|
3041
|
+
account_type: AccountType = AccountType.DEFAULT,
|
|
3042
|
+
):
|
|
3043
|
+
"""Submit a take-profit/stop-loss limit algorithmic order.
|
|
3044
|
+
|
|
3045
|
+
Parameters
|
|
3046
|
+
----------
|
|
3047
|
+
code : str
|
|
3048
|
+
A STRING scalar, indicating the futures code.
|
|
3049
|
+
exchange : str
|
|
3050
|
+
A STRING scalar, indicating the exchange code.
|
|
3051
|
+
time : Timestamp
|
|
3052
|
+
A TIMESTAMP scalar, indicating the order timestamp.
|
|
3053
|
+
order_type : int
|
|
3054
|
+
An INT scalar, indicating the order type:
|
|
3055
|
+
|
|
3056
|
+
- 0: Market order (submitted at limit up/limit-down price, following time-priority rules)
|
|
3057
|
+
|
|
3058
|
+
- 1: Stop-loss market order
|
|
3059
|
+
|
|
3060
|
+
- 2: Take-profit market order
|
|
3061
|
+
|
|
3062
|
+
- 3: Stop-loss limit order
|
|
3063
|
+
|
|
3064
|
+
- 4: Take-profit limit order
|
|
3065
|
+
|
|
3066
|
+
- 5: Limit order (default)
|
|
3067
|
+
|
|
3068
|
+
- 6: Cancel order request
|
|
3069
|
+
order_price : float
|
|
3070
|
+
A FLOAT scalar indicating the order price.
|
|
3071
|
+
stop_loss_price : float
|
|
3072
|
+
A FLOAT scalar indicating the stop-loss price.
|
|
3073
|
+
take_profit_price : float
|
|
3074
|
+
A FLOAT scalar indicating the take-profit price.
|
|
3075
|
+
quantity : int
|
|
3076
|
+
An INT scalar indicating the order quantity.
|
|
3077
|
+
direct : int
|
|
3078
|
+
An INT scalar indicating the buy/sell direction.
|
|
3079
|
+
slippage : float
|
|
3080
|
+
A FLOAT scalar indicating the slippage.
|
|
3081
|
+
order_validity : int
|
|
3082
|
+
An INT scalar indicating the validity of the order.
|
|
3083
|
+
expiration_time : Timestamp
|
|
3084
|
+
A TIMESTAMP scalar indicating the order timestamp.
|
|
3085
|
+
label : str, optional
|
|
3086
|
+
A STRING scalar used to specify the tag for order categorization.
|
|
3087
|
+
account_type : AccountType, optional
|
|
3088
|
+
An Enum value indicating the account type:
|
|
3089
|
+
|
|
3090
|
+
- SPOT: Cash account
|
|
3091
|
+
|
|
3092
|
+
- STOCK: Stock account
|
|
3093
|
+
|
|
3094
|
+
- FUTURES: Futures account
|
|
3095
|
+
|
|
3096
|
+
- OPTION: Options account
|
|
3097
|
+
|
|
3098
|
+
"""
|
|
3099
|
+
return plugin_backtest_submitOrder(
|
|
3100
|
+
self.engine().engine_handle,
|
|
3101
|
+
[
|
|
3102
|
+
code,
|
|
3103
|
+
exchange,
|
|
3104
|
+
time,
|
|
3105
|
+
order_type,
|
|
3106
|
+
order_price,
|
|
3107
|
+
stop_loss_price,
|
|
3108
|
+
take_profit_price,
|
|
3109
|
+
quantity,
|
|
3110
|
+
direct,
|
|
3111
|
+
slippage,
|
|
3112
|
+
order_validity,
|
|
3113
|
+
expiration_time,
|
|
3114
|
+
],
|
|
3115
|
+
label,
|
|
3116
|
+
sf_scalar(5, type="INT"),
|
|
3117
|
+
_convert_account(account_type),
|
|
3118
|
+
)
|
|
3119
|
+
|
|
3120
|
+
@final
|
|
3121
|
+
def submit_market_tp_sl_order(
|
|
3122
|
+
self,
|
|
3123
|
+
code: str,
|
|
3124
|
+
exchange: str,
|
|
3125
|
+
time: Timestamp,
|
|
3126
|
+
order_type: int,
|
|
3127
|
+
order_price: float,
|
|
3128
|
+
stop_loss_price: float,
|
|
3129
|
+
take_profit_price: float,
|
|
3130
|
+
quantity: int,
|
|
3131
|
+
direct: int,
|
|
3132
|
+
slippage: float,
|
|
3133
|
+
order_validity: int,
|
|
3134
|
+
expiration_time: Timestamp,
|
|
3135
|
+
/,
|
|
3136
|
+
label: str = "",
|
|
3137
|
+
account_type: AccountType = AccountType.DEFAULT,
|
|
3138
|
+
):
|
|
3139
|
+
"""Submit a take-profit/stop-loss market algorithmic order.
|
|
3140
|
+
|
|
3141
|
+
Parameters
|
|
3142
|
+
----------
|
|
3143
|
+
code : str
|
|
3144
|
+
A STRING scalar, indicating the futures code.
|
|
3145
|
+
exchange : str
|
|
3146
|
+
A STRING scalar, indicating the exchange code.
|
|
3147
|
+
time : Timestamp
|
|
3148
|
+
A TIMESTAMP scalar, indicating the order timestamp.
|
|
3149
|
+
order_type : int
|
|
3150
|
+
An INT scalar, indicating the order type:
|
|
3151
|
+
|
|
3152
|
+
- 0: Market order (submitted at limit up/limit-down price, following time-priority rules)
|
|
3153
|
+
|
|
3154
|
+
- 1: Stop-loss market order
|
|
3155
|
+
|
|
3156
|
+
- 2: Take-profit market order
|
|
3157
|
+
|
|
3158
|
+
- 3: Stop-loss limit order
|
|
3159
|
+
|
|
3160
|
+
- 4: Take-profit limit order
|
|
3161
|
+
|
|
3162
|
+
- 5: Limit order (default)
|
|
3163
|
+
|
|
3164
|
+
- 6: Cancel order request
|
|
3165
|
+
order_price : float
|
|
3166
|
+
A FLOAT scalar indicating the order price.
|
|
3167
|
+
stop_loss_price : float
|
|
3168
|
+
A FLOAT scalar indicating the stop-loss price.
|
|
3169
|
+
take_profit_price : float
|
|
3170
|
+
A FLOAT scalar indicating the take-profit price.
|
|
3171
|
+
quantity : int
|
|
3172
|
+
An INT scalar indicating the order quantity.
|
|
3173
|
+
direct : int
|
|
3174
|
+
An INT scalar indicating the buy/sell direction.
|
|
3175
|
+
slippage : float
|
|
3176
|
+
A FLOAT scalar indicating the slippage.
|
|
3177
|
+
order_validity : int
|
|
3178
|
+
An INT scalar indicating the validity of the order.
|
|
3179
|
+
expiration_time : Timestamp
|
|
3180
|
+
A TIMESTAMP scalar indicating the order timestamp.
|
|
3181
|
+
label : str, optional
|
|
3182
|
+
A STRING scalar used to specify the tag for order categorization.
|
|
3183
|
+
account_type : AccountType, optional
|
|
3184
|
+
An Enum value indicating the account type:
|
|
3185
|
+
|
|
3186
|
+
- SPOT: Cash account
|
|
3187
|
+
|
|
3188
|
+
- STOCK: Stock account
|
|
3189
|
+
|
|
3190
|
+
- FUTURES: Futures account
|
|
3191
|
+
|
|
3192
|
+
- OPTION: Options account
|
|
3193
|
+
|
|
3194
|
+
"""
|
|
3195
|
+
return plugin_backtest_submitOrder(
|
|
3196
|
+
self.engine().engine_handle,
|
|
3197
|
+
[
|
|
3198
|
+
code,
|
|
3199
|
+
exchange,
|
|
3200
|
+
time,
|
|
3201
|
+
order_type,
|
|
3202
|
+
order_price,
|
|
3203
|
+
stop_loss_price,
|
|
3204
|
+
take_profit_price,
|
|
3205
|
+
quantity,
|
|
3206
|
+
direct,
|
|
3207
|
+
slippage,
|
|
3208
|
+
order_validity,
|
|
3209
|
+
expiration_time,
|
|
3210
|
+
],
|
|
3211
|
+
label,
|
|
3212
|
+
sf_scalar(6, type="INT"),
|
|
3213
|
+
_convert_account(account_type),
|
|
3214
|
+
)
|
|
3215
|
+
|
|
3216
|
+
@final
|
|
3217
|
+
def submit_ask_bid_order(
|
|
3218
|
+
self,
|
|
3219
|
+
code: str,
|
|
3220
|
+
exchange: str,
|
|
3221
|
+
time: Timestamp,
|
|
3222
|
+
order_type: int,
|
|
3223
|
+
bid_offset_flag: int,
|
|
3224
|
+
bid_price: float,
|
|
3225
|
+
bid_qty: int,
|
|
3226
|
+
ask_offset_flag: int,
|
|
3227
|
+
ask_price: float,
|
|
3228
|
+
ask_qty: int,
|
|
3229
|
+
bid_difftolerance: float,
|
|
3230
|
+
ask_difftolerance: float,
|
|
3231
|
+
quantity_allowed: bool,
|
|
3232
|
+
/,
|
|
3233
|
+
label: str = "",
|
|
3234
|
+
account_type: AccountType = AccountType.DEFAULT,
|
|
3235
|
+
):
|
|
3236
|
+
"""Submit a two sided quote order.
|
|
3237
|
+
|
|
3238
|
+
Parameters
|
|
3239
|
+
----------
|
|
3240
|
+
code : str
|
|
3241
|
+
A STRING scalar, indicating the futures code.
|
|
3242
|
+
exchange : str
|
|
3243
|
+
A STRING scalar, indicating the exchange code.
|
|
3244
|
+
time : Timestamp
|
|
3245
|
+
A TIMESTAMP scalar, indicating the order timestamp.
|
|
3246
|
+
order_type : int
|
|
3247
|
+
An INT scalar, indicating the order type:
|
|
3248
|
+
|
|
3249
|
+
- 0: Market order (submitted at limit up/limit-down price, following time-priority rules)
|
|
3250
|
+
|
|
3251
|
+
- 1: Stop-loss market order
|
|
3252
|
+
|
|
3253
|
+
- 2: Take-profit market order
|
|
3254
|
+
|
|
3255
|
+
- 3: Stop-loss limit order
|
|
3256
|
+
|
|
3257
|
+
- 4: Take-profit limit order
|
|
3258
|
+
|
|
3259
|
+
- 5: Limit order (default)
|
|
3260
|
+
|
|
3261
|
+
- 6: Cancel order request
|
|
3262
|
+
bid_offset_flag : int
|
|
3263
|
+
An INT scalar indicating the status of buy action, where 1 represents
|
|
3264
|
+
buy to open and 4 represents buy to close.
|
|
3265
|
+
bid_price : float
|
|
3266
|
+
A FLOAT scalar indicating the bid price.
|
|
3267
|
+
bid_qty : int
|
|
3268
|
+
An INT scalar indicating the bid quantity.
|
|
3269
|
+
ask_offset_flag : int
|
|
3270
|
+
An INT scalar indicating the status of sell action, where 2 represents
|
|
3271
|
+
sell to open and 3 represents sell to close.
|
|
3272
|
+
ask_price : float
|
|
3273
|
+
A FLOAT scalar indicating the ask price.
|
|
3274
|
+
ask_qty : int
|
|
3275
|
+
An INT scalar indicating the ask quantity.
|
|
3276
|
+
bid_difftolerance : float
|
|
3277
|
+
A FLOAT scalar indicating the maximum allowable deviation between
|
|
3278
|
+
the bid price and the reference market price.
|
|
3279
|
+
ask_difftolerance : float
|
|
3280
|
+
A FLOAT scalar indicating the maximum allowable deviation between
|
|
3281
|
+
the ask price and the reference market price.
|
|
3282
|
+
quantity_allowed : bool
|
|
3283
|
+
_description_
|
|
3284
|
+
label : str, optional
|
|
3285
|
+
A STRING scalar used to specify the tag for order categorization.
|
|
3286
|
+
account_type : AccountType, optional
|
|
3287
|
+
An Enum value representing the account type:
|
|
3288
|
+
|
|
3289
|
+
- SPOT: Cash account
|
|
3290
|
+
|
|
3291
|
+
- STOCK: Stock account
|
|
3292
|
+
|
|
3293
|
+
- FUTURES: Futures account
|
|
3294
|
+
|
|
3295
|
+
- OPTION: Options account
|
|
3296
|
+
|
|
3297
|
+
"""
|
|
3298
|
+
return plugin_backtest_submitOrder(
|
|
3299
|
+
self.engine().engine_handle,
|
|
3300
|
+
[
|
|
3301
|
+
code,
|
|
3302
|
+
exchange,
|
|
3303
|
+
time,
|
|
3304
|
+
order_type,
|
|
3305
|
+
bid_offset_flag,
|
|
3306
|
+
bid_price,
|
|
3307
|
+
bid_qty,
|
|
3308
|
+
ask_offset_flag,
|
|
3309
|
+
ask_price,
|
|
3310
|
+
ask_qty,
|
|
3311
|
+
bid_difftolerance,
|
|
3312
|
+
ask_difftolerance,
|
|
3313
|
+
quantity_allowed,
|
|
3314
|
+
],
|
|
3315
|
+
label,
|
|
3316
|
+
sf_scalar(8, type="INT"),
|
|
3317
|
+
_convert_account(account_type),
|
|
3318
|
+
)
|
|
3319
|
+
|
|
3320
|
+
@final
|
|
3321
|
+
def submit_auto_order(
|
|
3322
|
+
self,
|
|
3323
|
+
code: str,
|
|
3324
|
+
exchange: str,
|
|
3325
|
+
time: Timestamp,
|
|
3326
|
+
order_type: int,
|
|
3327
|
+
order_price: float,
|
|
3328
|
+
stop_price: float,
|
|
3329
|
+
quantity: int,
|
|
3330
|
+
direct: int,
|
|
3331
|
+
order_validity: int,
|
|
3332
|
+
/,
|
|
3333
|
+
label: str = None,
|
|
3334
|
+
account_type: AccountType = AccountType.DEFAULT,
|
|
3335
|
+
):
|
|
3336
|
+
"""Submit a auto order.
|
|
3337
|
+
|
|
3338
|
+
Parameters
|
|
3339
|
+
----------
|
|
3340
|
+
code : str
|
|
3341
|
+
A STRING scalar, indicating the futures code.
|
|
3342
|
+
exchange : str
|
|
3343
|
+
A STRING scalar, indicating the exchange code.
|
|
3344
|
+
time : Timestamp
|
|
3345
|
+
A TIMESTAMP scalar, indicating the order timestamp.
|
|
3346
|
+
order_type : int
|
|
3347
|
+
An INT scalar, indicating the order type:
|
|
3348
|
+
|
|
3349
|
+
- 0: Market order (submitted at limit up/limit-down price, following time-priority rules)
|
|
3350
|
+
|
|
3351
|
+
- 1: Stop-loss market order
|
|
3352
|
+
|
|
3353
|
+
- 2: Take-profit market order
|
|
3354
|
+
|
|
3355
|
+
- 3: Stop-loss limit order
|
|
3356
|
+
|
|
3357
|
+
- 4: Take-profit limit order
|
|
3358
|
+
|
|
3359
|
+
- 5: Limit order (default)
|
|
3360
|
+
|
|
3361
|
+
- 6: Cancel order request
|
|
3362
|
+
order_price : float
|
|
3363
|
+
A FLOAT scalar indicating the bid or ask price.
|
|
3364
|
+
stop_price : float
|
|
3365
|
+
A FLOAT scalar indicating the take-profit or stop-loss price.
|
|
3366
|
+
quantity : int
|
|
3367
|
+
An INT scalar indicating the order quantity.
|
|
3368
|
+
direct : int
|
|
3369
|
+
An INT scalar indicating the trade side.
|
|
3370
|
+
order_validity : int
|
|
3371
|
+
An INT scalar indicating the validity of the order.
|
|
3372
|
+
label : str, optional
|
|
3373
|
+
A STRING scalar used to specify the tag for order categorization.
|
|
3374
|
+
account_type : AccountType, optional
|
|
3375
|
+
An Enum value representing the account type:
|
|
3376
|
+
|
|
3377
|
+
- SPOT: Cash account
|
|
3378
|
+
|
|
3379
|
+
- STOCK: Stock account
|
|
3380
|
+
|
|
3381
|
+
- FUTURES: Futures account
|
|
3382
|
+
|
|
3383
|
+
- OPTION: Options account
|
|
3384
|
+
"""
|
|
3385
|
+
return plugin_backtest_submitOrder(
|
|
3386
|
+
self.engine().engine_handle,
|
|
3387
|
+
[
|
|
3388
|
+
code,
|
|
3389
|
+
exchange,
|
|
3390
|
+
time,
|
|
3391
|
+
order_type,
|
|
3392
|
+
order_price,
|
|
3393
|
+
stop_price,
|
|
3394
|
+
quantity,
|
|
3395
|
+
direct,
|
|
3396
|
+
order_validity,
|
|
3397
|
+
],
|
|
3398
|
+
_convert_Nothing(label),
|
|
3399
|
+
sf_scalar(9, type="INT"),
|
|
3400
|
+
_convert_account(account_type),
|
|
3401
|
+
)
|
|
3402
|
+
|
|
3403
|
+
|
|
3404
|
+
class StockOrderMixin(StrategyBase):
|
|
3405
|
+
@final
|
|
3406
|
+
def submit_stock_order(self, code: str, time: Timestamp, order_type: int, order_price: float, quantity: int, direct: int, /, label: str = None, account_type: AccountType = AccountType.DEFAULT):
|
|
3407
|
+
"""Submit a stock order.
|
|
3408
|
+
|
|
3409
|
+
Parameters
|
|
3410
|
+
----------
|
|
3411
|
+
code : str
|
|
3412
|
+
A STRING scalar indicating the stock code.
|
|
3413
|
+
time : Timestamp
|
|
3414
|
+
A TIMESTAMP scalar indicating the order timestamp.
|
|
3415
|
+
order_type : int
|
|
3416
|
+
An INT scalar indicating the order type. Available values:
|
|
3417
|
+
|
|
3418
|
+
- Shanghai Stock Exchange:
|
|
3419
|
+
|
|
3420
|
+
- 0: best five levels immediate or cancel (IOC).
|
|
3421
|
+
|
|
3422
|
+
- 1: best five levels immediate-or-convert-to-limit.
|
|
3423
|
+
|
|
3424
|
+
- 2: best price on own side.
|
|
3425
|
+
|
|
3426
|
+
- 3: best price on counterparty side.
|
|
3427
|
+
|
|
3428
|
+
- 5: Limit order.
|
|
3429
|
+
|
|
3430
|
+
- 6: Cancel order.
|
|
3431
|
+
|
|
3432
|
+
- Shenzhen Stock Exchange:
|
|
3433
|
+
|
|
3434
|
+
- 0: best five levels immediate or cancel (IOC).
|
|
3435
|
+
|
|
3436
|
+
- 1: immediate or cancel (IOC).
|
|
3437
|
+
|
|
3438
|
+
- 2: best price on own side.
|
|
3439
|
+
|
|
3440
|
+
- 3: best price on counterparty side.
|
|
3441
|
+
|
|
3442
|
+
- 4: fill or kill (FOK).
|
|
3443
|
+
|
|
3444
|
+
- 5: Limit order.
|
|
3445
|
+
|
|
3446
|
+
- 6: Cancel order.
|
|
3447
|
+
order_price : float
|
|
3448
|
+
A FLOAT scalar indicating the order price.
|
|
3449
|
+
quantity : int
|
|
3450
|
+
An INT scalar indicating the order quantity.
|
|
3451
|
+
direct : int
|
|
3452
|
+
An INT scalar indicating the trade direction. Optional values:
|
|
3453
|
+
|
|
3454
|
+
- 1: Buy open
|
|
3455
|
+
|
|
3456
|
+
- 2: Sell open
|
|
3457
|
+
|
|
3458
|
+
- 3: Sell close
|
|
3459
|
+
|
|
3460
|
+
- 4: Buy close
|
|
3461
|
+
label : str, optional
|
|
3462
|
+
A STRING scalar indicating the tag for categorizing the order.
|
|
3463
|
+
account_type : AccountType, optional
|
|
3464
|
+
Account type. Optional values:
|
|
3465
|
+
|
|
3466
|
+
- SPOT: Cash account
|
|
3467
|
+
|
|
3468
|
+
- STOCK: Stock account
|
|
3469
|
+
|
|
3470
|
+
- FUTURES: Futures account
|
|
3471
|
+
|
|
3472
|
+
- OPTION: Options account
|
|
3473
|
+
"""
|
|
3474
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, [code, time, order_type, order_price, quantity, direct], _convert_Nothing(label), sf_scalar(0, type="INT"), _convert_account(account_type))
|
|
3475
|
+
|
|
3476
|
+
@final
|
|
3477
|
+
def get_today_pnl(self, symbol: str):
|
|
3478
|
+
"""This interface is applicable only to stocks and is used to retrieve account profit and loss (P&L).
|
|
3479
|
+
|
|
3480
|
+
Parameters
|
|
3481
|
+
----------
|
|
3482
|
+
symbol : str
|
|
3483
|
+
A STRING scalar, representing the stock symbol.
|
|
3484
|
+
|
|
3485
|
+
Returns
|
|
3486
|
+
-------
|
|
3487
|
+
Dictionary
|
|
3488
|
+
|
|
3489
|
+
A dictionary with the following structure:
|
|
3490
|
+
|
|
3491
|
+
+-----------+------------------------------------------------------------+
|
|
3492
|
+
| Key | Description |
|
|
3493
|
+
+===========+============================================================+
|
|
3494
|
+
| symbol | Security code |
|
|
3495
|
+
+-----------+------------------------------------------------------------+
|
|
3496
|
+
| pnl | Profit or loss amount of the specified security in the |
|
|
3497
|
+
| | current account |
|
|
3498
|
+
+-----------+------------------------------------------------------------+
|
|
3499
|
+
| todayPnl | Profit or loss amount of the specified security for the |
|
|
3500
|
+
| | current trading day |
|
|
3501
|
+
+-----------+------------------------------------------------------------+
|
|
3502
|
+
"""
|
|
3503
|
+
return plugin_backtest_getTodayPnl(self.engine().engine_handle, symbol)
|
|
3504
|
+
|
|
3505
|
+
@final
|
|
3506
|
+
@property
|
|
3507
|
+
def stock_total_portfolios(self):
|
|
3508
|
+
"""Retrieve the current equity metrics of the stock strategy.
|
|
3509
|
+
|
|
3510
|
+
Returns
|
|
3511
|
+
-------
|
|
3512
|
+
Dictionary
|
|
3513
|
+
A dictionary with the following key-value pairs:
|
|
3514
|
+
|
|
3515
|
+
- tradeDate: The date
|
|
3516
|
+
|
|
3517
|
+
- cash: Available cash
|
|
3518
|
+
|
|
3519
|
+
- totalMarketValue: Total market value of the account
|
|
3520
|
+
|
|
3521
|
+
- totalEquity: Total equity of the account
|
|
3522
|
+
|
|
3523
|
+
- netValue: Net value per unit of the account
|
|
3524
|
+
|
|
3525
|
+
- totalReturn: Cumulative return up to the current day
|
|
3526
|
+
|
|
3527
|
+
- ratio: Daily return of the account
|
|
3528
|
+
|
|
3529
|
+
- pnl: Profit and loss of the account for the current day
|
|
3530
|
+
"""
|
|
3531
|
+
return plugin_backtest_getStockTotalPortfolios(self.engine().engine_handle)
|
|
3532
|
+
|
|
3533
|
+
|
|
3534
|
+
class FuturesOrderMixin(StrategyBase):
|
|
3535
|
+
@final
|
|
3536
|
+
def submit_futures_order(self, code: str, exchange: str, time: Timestamp, order_type: int, order_price: float, stop_price: float, quantity: int, direct: int, order_validity: int, /, label: str = None, account_type: AccountType = AccountType.DEFAULT):
|
|
3537
|
+
"""Submits a futures order.
|
|
3538
|
+
|
|
3539
|
+
Parameters
|
|
3540
|
+
----------
|
|
3541
|
+
code : str
|
|
3542
|
+
A STRING scalar representing the futures symbol.
|
|
3543
|
+
exchange : str
|
|
3544
|
+
A STRING scalar representing the exchange code.
|
|
3545
|
+
time : Timestamp
|
|
3546
|
+
A TIMESTAMP scalar indicating the order timestamp.
|
|
3547
|
+
order_type : int
|
|
3548
|
+
An INT scalar specifying the order type. Possible values are:
|
|
3549
|
+
|
|
3550
|
+
- 0: Market order, submitted at the limit up or limit down price, following the time priority rule.
|
|
3551
|
+
|
|
3552
|
+
- 1: Market stop-loss order.
|
|
3553
|
+
|
|
3554
|
+
- 2: Market take-profit order.
|
|
3555
|
+
|
|
3556
|
+
- 3: Limit stop-loss order.
|
|
3557
|
+
|
|
3558
|
+
- 4: Limit take-profit order.
|
|
3559
|
+
|
|
3560
|
+
- 5: Limit order (default).
|
|
3561
|
+
|
|
3562
|
+
order_price : float
|
|
3563
|
+
A FLOAT scalar representing the order price.
|
|
3564
|
+
stop_price : float
|
|
3565
|
+
A FLOAT scalar representing the stop-loss or take-profit price.
|
|
3566
|
+
quantity : int
|
|
3567
|
+
An INT scalar representing the order quantity.
|
|
3568
|
+
direct : int
|
|
3569
|
+
An INT scalar specifying the trade direction. Possible values are:
|
|
3570
|
+
|
|
3571
|
+
- 1: Buy open
|
|
3572
|
+
|
|
3573
|
+
- 2: Sell open
|
|
3574
|
+
|
|
3575
|
+
- 3: Sell close
|
|
3576
|
+
|
|
3577
|
+
- 4: Buy close
|
|
3578
|
+
|
|
3579
|
+
- 5: Option exercise
|
|
3580
|
+
order_validity : int
|
|
3581
|
+
An INT scalar indicating the order validity type. Possible values are:
|
|
3582
|
+
|
|
3583
|
+
- 0: Good for day (default).
|
|
3584
|
+
|
|
3585
|
+
- 1: Fill or kill (FOK) — execute immediately in full or cancel.
|
|
3586
|
+
|
|
3587
|
+
- 2: Fill and kill (FAK) — execute immediately and cancel any remaining quantity.
|
|
3588
|
+
label : str, optional
|
|
3589
|
+
A STRING scalar used to tag or categorize the order.
|
|
3590
|
+
account_type : AccountType, optional
|
|
3591
|
+
The account type. Possible values are:
|
|
3592
|
+
|
|
3593
|
+
- SPOT: Cash account
|
|
3594
|
+
|
|
3595
|
+
- STOCK: Stock account
|
|
3596
|
+
|
|
3597
|
+
- FUTURES: Futures account
|
|
3598
|
+
|
|
3599
|
+
- OPTION: Options account
|
|
3600
|
+
|
|
3601
|
+
"""
|
|
3602
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, [code, exchange, time, order_type, order_price, stop_price, quantity, direct, order_validity], _convert_Nothing(label), sf_scalar(0, type="INT"), _convert_account(account_type))
|
|
3603
|
+
|
|
3604
|
+
@final
|
|
3605
|
+
@property
|
|
3606
|
+
def futures_total_portfolios(self):
|
|
3607
|
+
"""Query the daily futures profit and loss.
|
|
3608
|
+
|
|
3609
|
+
Returns
|
|
3610
|
+
-------
|
|
3611
|
+
Table
|
|
3612
|
+
Return a table with the following structure:
|
|
3613
|
+
|
|
3614
|
+
+------------+-----------------------------+
|
|
3615
|
+
| Field Name | Description |
|
|
3616
|
+
+============+=============================+
|
|
3617
|
+
| tradeDate | Date |
|
|
3618
|
+
+------------+-----------------------------+
|
|
3619
|
+
| margin | Margin Occupied |
|
|
3620
|
+
+------------+-----------------------------+
|
|
3621
|
+
| floatingPnl| Floating PnL |
|
|
3622
|
+
+------------+-----------------------------+
|
|
3623
|
+
| realizedPnl| Realized Cumulative PnL |
|
|
3624
|
+
+------------+-----------------------------+
|
|
3625
|
+
| totalPnl | Total PnL |
|
|
3626
|
+
+------------+-----------------------------+
|
|
3627
|
+
| cash | Available Cash |
|
|
3628
|
+
+------------+-----------------------------+
|
|
3629
|
+
| totalEquity| Total Equity |
|
|
3630
|
+
+------------+-----------------------------+
|
|
3631
|
+
| marginRatio| Margin Occupancy Ratio |
|
|
3632
|
+
+------------+-----------------------------+
|
|
3633
|
+
| pnl | Daily PnL |
|
|
3634
|
+
+------------+-----------------------------+
|
|
3635
|
+
| netValue | Unit Net Value |
|
|
3636
|
+
+------------+-----------------------------+
|
|
3637
|
+
| totalReturn| Cumulative Return as of Date|
|
|
3638
|
+
+------------+-----------------------------+
|
|
3639
|
+
| ratio | Daily Return |
|
|
3640
|
+
+------------+-----------------------------+
|
|
3641
|
+
|
|
3642
|
+
"""
|
|
3643
|
+
return plugin_backtest_getFuturesTotalPortfolios(self.engine().engine_handle)
|
|
3644
|
+
|
|
3645
|
+
|
|
3646
|
+
class OptionOrderMixin(StrategyBase):
|
|
3647
|
+
@final
|
|
3648
|
+
def submit_option_order(self, code: str, exchange: str, time: Timestamp, order_type: int, order_price: float, stop_price: float, quantity: int, direct: int, order_validity: int, /, label: str = None, account_type: AccountType = AccountType.DEFAULT):
|
|
3649
|
+
"""Submits an options order.
|
|
3650
|
+
|
|
3651
|
+
Parameters
|
|
3652
|
+
----------
|
|
3653
|
+
code : str
|
|
3654
|
+
A STRING scalar representing the option symbol.
|
|
3655
|
+
exchange : str
|
|
3656
|
+
A STRING scalar representing the exchange code.
|
|
3657
|
+
time : Timestamp
|
|
3658
|
+
A TIMESTAMP scalar indicating the order timestamp.
|
|
3659
|
+
order_type : int
|
|
3660
|
+
An INT scalar specifying the order type. Possible values are:
|
|
3661
|
+
|
|
3662
|
+
- 0: Market order, submitted at the limit up or limit down price, following the time priority rule.
|
|
3663
|
+
|
|
3664
|
+
- 1: Market stop-loss order.
|
|
3665
|
+
|
|
3666
|
+
- 2: Market take-profit order.
|
|
3667
|
+
|
|
3668
|
+
- 3: Limit stop-loss order.
|
|
3669
|
+
|
|
3670
|
+
- 4: Limit take-profit order.
|
|
3671
|
+
|
|
3672
|
+
- 5: Limit order (default).
|
|
3673
|
+
|
|
3674
|
+
- 6: Cancel order.
|
|
3675
|
+
|
|
3676
|
+
- 9: Auto order mode. In this mode, only directions 1 and 2 are supported:
|
|
3677
|
+
|
|
3678
|
+
- If direct = 1:
|
|
3679
|
+
|
|
3680
|
+
- When the order quantity is greater than the current long position (longPosition), a buy open order is submitted.
|
|
3681
|
+
|
|
3682
|
+
- When the order quantity is less than or equal to longPosition, a sell close (direction = 3) order is submitted.
|
|
3683
|
+
|
|
3684
|
+
- If direct = 2:
|
|
3685
|
+
|
|
3686
|
+
- When the order quantity is greater than the current short position (shortPosition), a sell open order is submitted.
|
|
3687
|
+
|
|
3688
|
+
- When the order quantity is less than or equal to shortPosition, a buy close (direction = 4) order is submitted.
|
|
3689
|
+
order_price : float
|
|
3690
|
+
A FLOAT scalar representing the order price.
|
|
3691
|
+
stop_price : float
|
|
3692
|
+
A FLOAT scalar representing the stop-loss or take-profit price.
|
|
3693
|
+
quantity : int
|
|
3694
|
+
An INT scalar representing the order quantity.
|
|
3695
|
+
direct : int
|
|
3696
|
+
An INT scalar specifying the trade direction. Possible values are:
|
|
3697
|
+
|
|
3698
|
+
- 1: Buy open
|
|
3699
|
+
|
|
3700
|
+
- 2: Sell open
|
|
3701
|
+
|
|
3702
|
+
- 3: Sell close
|
|
3703
|
+
|
|
3704
|
+
- 4: Buy close
|
|
3705
|
+
|
|
3706
|
+
- 5: Option exercise
|
|
3707
|
+
order_validity : int
|
|
3708
|
+
An INT scalar indicating the order validity type. Possible values are:
|
|
3709
|
+
|
|
3710
|
+
- 0: Good for day (default).
|
|
3711
|
+
|
|
3712
|
+
- 1: Fill or kill (FOK) — execute immediately in full or cancel.
|
|
3713
|
+
|
|
3714
|
+
- 2: Fill and kill (FAK) — execute immediately and cancel any remaining quantity.
|
|
3715
|
+
|
|
3716
|
+
The stop-loss/take-profit price is not currently supported and defaults to 0.
|
|
3717
|
+
label : str, optional
|
|
3718
|
+
A STRING scalar used to tag or categorize the order.
|
|
3719
|
+
account_type : AccountType, optional
|
|
3720
|
+
The account type. Possible values are:
|
|
3721
|
+
|
|
3722
|
+
- SPOT: Cash account
|
|
3723
|
+
|
|
3724
|
+
- STOCK: Stock account
|
|
3725
|
+
|
|
3726
|
+
- FUTURES: Futures account
|
|
3727
|
+
|
|
3728
|
+
- OPTION: Options account
|
|
3729
|
+
|
|
3730
|
+
"""
|
|
3731
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, [code, exchange, time, order_type, order_price, stop_price, quantity, direct, order_validity], _convert_Nothing(label), sf_scalar(0, type="INT"), _convert_account(account_type))
|
|
3732
|
+
|
|
3733
|
+
@final
|
|
3734
|
+
@property
|
|
3735
|
+
def option_total_portfolios(self):
|
|
3736
|
+
"""Query daily option PnL
|
|
3737
|
+
|
|
3738
|
+
Returns
|
|
3739
|
+
-------
|
|
3740
|
+
Table
|
|
3741
|
+
Return a table with the following structure:
|
|
3742
|
+
|
|
3743
|
+
+----------------+--------------------------------------+
|
|
3744
|
+
| Field Name | Description |
|
|
3745
|
+
+================+======================================+
|
|
3746
|
+
| tradeDate | Date |
|
|
3747
|
+
+----------------+--------------------------------------+
|
|
3748
|
+
| margin | Margin occupied |
|
|
3749
|
+
+----------------+--------------------------------------+
|
|
3750
|
+
| floatingPnl | Floating PnL |
|
|
3751
|
+
+----------------+--------------------------------------+
|
|
3752
|
+
| realizedPnl | Realized cumulative PnL |
|
|
3753
|
+
+----------------+--------------------------------------+
|
|
3754
|
+
| totalPnl | Total cumulative PnL |
|
|
3755
|
+
+----------------+--------------------------------------+
|
|
3756
|
+
| cash | Available cash |
|
|
3757
|
+
+----------------+--------------------------------------+
|
|
3758
|
+
| totalEquity | Total account equity |
|
|
3759
|
+
+----------------+--------------------------------------+
|
|
3760
|
+
| marginRatio | Margin ratio |
|
|
3761
|
+
+----------------+--------------------------------------+
|
|
3762
|
+
| pnl | Daily PnL |
|
|
3763
|
+
+----------------+--------------------------------------+
|
|
3764
|
+
| netValue | Account unit net value |
|
|
3765
|
+
+----------------+--------------------------------------+
|
|
3766
|
+
| totalReturn | Cumulative return up to the day |
|
|
3767
|
+
+----------------+--------------------------------------+
|
|
3768
|
+
| ratio | Daily return |
|
|
3769
|
+
+----------------+--------------------------------------+
|
|
3770
|
+
|
|
3771
|
+
"""
|
|
3772
|
+
return plugin_backtest_getOptionTotalPortfolios(self.engine().engine_handle)
|
|
3773
|
+
|
|
3774
|
+
|
|
3775
|
+
class MarginOrderMixin(StrategyBase):
|
|
3776
|
+
@final
|
|
3777
|
+
def submit_margin_order(self, code: str, time: Timestamp, order_type: int, order_price: float, quantity: int, direct: int, /, label: str = None, account_type: AccountType = AccountType.DEFAULT):
|
|
3778
|
+
"""Submit a margin trading order.
|
|
3779
|
+
|
|
3780
|
+
Parameters
|
|
3781
|
+
----------
|
|
3782
|
+
code : str
|
|
3783
|
+
A STRING scalar indicating the stock symbol.
|
|
3784
|
+
time : Timestamp
|
|
3785
|
+
A TIMESTAMP scalar indicating the timestamp of the order.
|
|
3786
|
+
order_type : int
|
|
3787
|
+
An INT scalar indicating the order type. Available values include:
|
|
3788
|
+
|
|
3789
|
+
- 0: Market order
|
|
3790
|
+
|
|
3791
|
+
- 5: Limit order
|
|
3792
|
+
order_price : float
|
|
3793
|
+
A FLOAT scalar indicating the order price.
|
|
3794
|
+
quantity : int
|
|
3795
|
+
An INT scalar indicating the order quantity.
|
|
3796
|
+
direct : int
|
|
3797
|
+
An INT scalar indicating the trade direction. Available values include:
|
|
3798
|
+
|
|
3799
|
+
- 1: Collateral purchase
|
|
3800
|
+
|
|
3801
|
+
- 2: Collateral sale
|
|
3802
|
+
|
|
3803
|
+
- 3: Margin purchase
|
|
3804
|
+
|
|
3805
|
+
- 4: Short sale
|
|
3806
|
+
|
|
3807
|
+
- 5: Direct repayment
|
|
3808
|
+
|
|
3809
|
+
- 6: Sell for repayment
|
|
3810
|
+
|
|
3811
|
+
- 7: Direct return of borrowed shares
|
|
3812
|
+
|
|
3813
|
+
- 8: Buy for return of borrowed shares
|
|
3814
|
+
label : str, optional
|
|
3815
|
+
A STRING scalar indicating the label assigned to the order for classification.
|
|
3816
|
+
account_type : AccountType, optional
|
|
3817
|
+
The account type. Available values include:
|
|
3818
|
+
|
|
3819
|
+
- SPOT: Cash account
|
|
3820
|
+
|
|
3821
|
+
- STOCK: Stock account
|
|
3822
|
+
|
|
3823
|
+
- FUTURES: Futures account
|
|
3824
|
+
|
|
3825
|
+
- OPTION: Options account
|
|
3826
|
+
"""
|
|
3827
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, [code, time, order_type, order_price, quantity, direct], _convert_Nothing(label), sf_scalar(0, type="INT"), _convert_account(account_type))
|
|
3828
|
+
|
|
3829
|
+
@final
|
|
3830
|
+
def get_margin_secu_position(self, symbols: List[str] = None):
|
|
3831
|
+
"""Query the collateral purchase position information.
|
|
3832
|
+
|
|
3833
|
+
|
|
3834
|
+
Parameters
|
|
3835
|
+
----------
|
|
3836
|
+
symbols : List[str], optional
|
|
3837
|
+
A STRING vector indicating the list of stock symbols. If omitted,
|
|
3838
|
+
the function returns all collateral purchase positions.
|
|
3839
|
+
|
|
3840
|
+
Returns
|
|
3841
|
+
-------
|
|
3842
|
+
Dictionary or Table
|
|
3843
|
+
- When the length of symbolList is 1, the function returns a dictionary.
|
|
3844
|
+
|
|
3845
|
+
- When the length of symbolList is not 1, an error is raised.
|
|
3846
|
+
|
|
3847
|
+
- When symbolList is omitted, the function returns a table.
|
|
3848
|
+
|
|
3849
|
+
The returned table's structure is as follows:
|
|
3850
|
+
|
|
3851
|
+
+---------------------+------------------------------------------------------------+
|
|
3852
|
+
| Name | Description |
|
|
3853
|
+
+=====================+============================================================+
|
|
3854
|
+
| symbol | Stock symbol. |
|
|
3855
|
+
+---------------------+------------------------------------------------------------+
|
|
3856
|
+
| lastDayLongPosition | Collateral purchase position quantity at the previous |
|
|
3857
|
+
| | day's close. |
|
|
3858
|
+
+---------------------+------------------------------------------------------------+
|
|
3859
|
+
| lastDayBuyValue | Collateral purchase value at the previous day's close. |
|
|
3860
|
+
+---------------------+------------------------------------------------------------+
|
|
3861
|
+
| longPosition | Current collateral purchase position quantity. |
|
|
3862
|
+
+---------------------+------------------------------------------------------------+
|
|
3863
|
+
| buyValue | Current collateral purchase value. |
|
|
3864
|
+
+---------------------+------------------------------------------------------------+
|
|
3865
|
+
| todayBuyVolume | Collateral purchase traded quantity for the current day. |
|
|
3866
|
+
+---------------------+------------------------------------------------------------+
|
|
3867
|
+
| todayBuyValue | Collateral purchase traded value for the current day. |
|
|
3868
|
+
+---------------------+------------------------------------------------------------+
|
|
3869
|
+
|
|
3870
|
+
"""
|
|
3871
|
+
return plugin_backtest_getMarginSecuPosition(self.engine().engine_handle, _convert_Nothing(symbols))
|
|
3872
|
+
|
|
3873
|
+
@final
|
|
3874
|
+
def get_margin_trading_position(self, symbols: List[str] = None):
|
|
3875
|
+
"""Queries margin purchase position information.
|
|
3876
|
+
|
|
3877
|
+
Parameters
|
|
3878
|
+
----------
|
|
3879
|
+
symbols : List[str], optional
|
|
3880
|
+
A STRING vector indicating the list of stock symbols. If omitted,
|
|
3881
|
+
the function returns margin purchase positions for all stocks.
|
|
3882
|
+
|
|
3883
|
+
Returns
|
|
3884
|
+
-------
|
|
3885
|
+
Dictionary or Table
|
|
3886
|
+
- When symbolList contains only one symbol, a dictionary is returned.
|
|
3887
|
+
|
|
3888
|
+
- When symbolList contains more than one symbol, an error is raised.
|
|
3889
|
+
|
|
3890
|
+
- When symbolList is omitted, a table is returned.
|
|
3891
|
+
|
|
3892
|
+
The returned table's structure is as follows:
|
|
3893
|
+
|
|
3894
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3895
|
+
| Field | Type | Description |
|
|
3896
|
+
+====================+================+===============================================+
|
|
3897
|
+
| symbol | STRING | Stock symbol |
|
|
3898
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3899
|
+
| lastDayLongPosition| DECIMAL128(8) | Margin purchase position at previous day's |
|
|
3900
|
+
| | | close |
|
|
3901
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3902
|
+
| lastDayBuyValue | DECIMAL128(8) | Margin purchase amount at previous day's |
|
|
3903
|
+
| | | close |
|
|
3904
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3905
|
+
| lastDayMarginDebt | DECIMAL128(8) | Margin debt at previous day's close |
|
|
3906
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3907
|
+
| longPosition | DECIMAL128(8) | Current margin purchase position |
|
|
3908
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3909
|
+
| buyValue | DECIMAL128(8) | Current margin purchase amount |
|
|
3910
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3911
|
+
| todayBuyVolume | DECIMAL128(8) | Margin purchase traded volume today |
|
|
3912
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3913
|
+
| todayBuyValue | DECIMAL128(8) | Margin purchase traded amount today |
|
|
3914
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3915
|
+
| marginBuyProfit | DECIMAL128(8) | Profit or loss of margin purchase |
|
|
3916
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3917
|
+
| financialFee | DECIMAL128(8) | Financing interest |
|
|
3918
|
+
+--------------------+----------------+-----------------------------------------------+
|
|
3919
|
+
|
|
3920
|
+
"""
|
|
3921
|
+
return plugin_backtest_getMarginTradingPosition(self.engine().engine_handle, _convert_Nothing(symbols))
|
|
3922
|
+
|
|
3923
|
+
@final
|
|
3924
|
+
def get_secu_lending_position(self, symbols: List[str] = None):
|
|
3925
|
+
"""Query short sale positions.
|
|
3926
|
+
|
|
3927
|
+
Parameters
|
|
3928
|
+
----------
|
|
3929
|
+
symbols : List[str], optional
|
|
3930
|
+
A STRING vector indicating a list of stock symbols. If omitted, all
|
|
3931
|
+
short-sale positions will be returned by default.
|
|
3932
|
+
|
|
3933
|
+
Returns
|
|
3934
|
+
-------
|
|
3935
|
+
Dictionary or Table
|
|
3936
|
+
- When the length of symbolList is 1, a dictionary is returned.
|
|
3937
|
+
|
|
3938
|
+
- When the length of symbolList is not 1, an error is raised.
|
|
3939
|
+
|
|
3940
|
+
- If symbolList is omitted, a table is returned.
|
|
3941
|
+
|
|
3942
|
+
The returned table's structure is as follows:
|
|
3943
|
+
|
|
3944
|
+
+------------------------+----------------------------+
|
|
3945
|
+
| Name | Description |
|
|
3946
|
+
+========================+============================+
|
|
3947
|
+
| symbol | The underlying stock symbol|
|
|
3948
|
+
+------------------------+----------------------------+
|
|
3949
|
+
| lastDayShortPosition | Short-sale position at |
|
|
3950
|
+
| | previous close |
|
|
3951
|
+
+------------------------+----------------------------+
|
|
3952
|
+
| lastDayShortValue | Short-sale value at |
|
|
3953
|
+
| | previous close |
|
|
3954
|
+
+------------------------+----------------------------+
|
|
3955
|
+
| lastDaySecuLendingDebt | Short-sale debt at |
|
|
3956
|
+
| | previous close |
|
|
3957
|
+
+------------------------+----------------------------+
|
|
3958
|
+
| shortPosition | Current short-sale position|
|
|
3959
|
+
+------------------------+----------------------------+
|
|
3960
|
+
| shortValue | Current short-sale value |
|
|
3961
|
+
+------------------------+----------------------------+
|
|
3962
|
+
| todayShortVolume | Executed short-sale volume |
|
|
3963
|
+
| | for the day |
|
|
3964
|
+
+------------------------+----------------------------+
|
|
3965
|
+
| todayShortValue | Executed short-sale value |
|
|
3966
|
+
| | for the day |
|
|
3967
|
+
+------------------------+----------------------------+
|
|
3968
|
+
| secuLendingProfit | Profit or loss from short |
|
|
3969
|
+
| | selling |
|
|
3970
|
+
+------------------------+----------------------------+
|
|
3971
|
+
| secuLendingFee | Fees for short selling |
|
|
3972
|
+
+------------------------+----------------------------+
|
|
3973
|
+
|
|
3974
|
+
"""
|
|
3975
|
+
return plugin_backtest_getSecuLendingPosition(self.engine().engine_handle, _convert_Nothing(symbols))
|
|
3976
|
+
|
|
3977
|
+
|
|
3978
|
+
class BondOrderMixin(StrategyBase):
|
|
3979
|
+
@final
|
|
3980
|
+
def submit_bond_order(self, code: str, time: Timestamp, order_type: int, settlement_speed: int, bid_price: float, bid_quantity: int, ask_price: float, ask_quantity: int, direct: int, order_id: int, channel: str, /, label: str = None, account_type: AccountType = AccountType.DEFAULT):
|
|
3981
|
+
"""Submit a bond order.
|
|
3982
|
+
|
|
3983
|
+
Parameters
|
|
3984
|
+
----------
|
|
3985
|
+
code : str
|
|
3986
|
+
A STRING scalar representing the option symbol.
|
|
3987
|
+
time : Timestamp
|
|
3988
|
+
A TIMESTAMP scalar indicating the order timestamp.
|
|
3989
|
+
order_type : int
|
|
3990
|
+
An INT scalar specifying the order type. Possible values are:
|
|
3991
|
+
|
|
3992
|
+
- 0: A Market order.
|
|
3993
|
+
|
|
3994
|
+
- 9: For automated orders, the buy/sell direction can only be set to 1 or 2.
|
|
3995
|
+
settlement_speed : int
|
|
3996
|
+
An INT scalar indiacting the settlement speed.
|
|
3997
|
+
bid_price : float
|
|
3998
|
+
A FLOAT scalar indicating buy order price.
|
|
3999
|
+
bid_quantity : int
|
|
4000
|
+
An INT scalar indicating the buy order quantity.
|
|
4001
|
+
ask_price : float
|
|
4002
|
+
A FLOAT scalar indicating sell order price.
|
|
4003
|
+
ask_quantity : int
|
|
4004
|
+
An INT scalar indicating the sell order quantity.
|
|
4005
|
+
direct : int
|
|
4006
|
+
An INT scalar indicating the order direction.
|
|
4007
|
+
order_id : int
|
|
4008
|
+
An INT scalar indicating the order id.
|
|
4009
|
+
channel : str
|
|
4010
|
+
A STRING scalar indicating the channel number.
|
|
4011
|
+
label : str, optional
|
|
4012
|
+
A STRING scalar indicating the label assigned to the order for classification.
|
|
4013
|
+
account_type : AccountType, optional
|
|
4014
|
+
The account type. Available values include:
|
|
4015
|
+
|
|
4016
|
+
- SPOT: Cash account
|
|
4017
|
+
|
|
4018
|
+
- STOCK: Stock account
|
|
4019
|
+
|
|
4020
|
+
- FUTURES: Futures account
|
|
4021
|
+
|
|
4022
|
+
- OPTION: Options account
|
|
4023
|
+
|
|
4024
|
+
"""
|
|
4025
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, [code, time, order_type, settlement_speed, bid_price, bid_quantity, ask_price, ask_quantity, direct, order_id, channel], _convert_Nothing(label), sf_scalar(0, type="INT"), _convert_account(account_type))
|
|
4026
|
+
|
|
4027
|
+
@final
|
|
4028
|
+
def update_position(self, symbol: str, quantity: int, price: float = None):
|
|
4029
|
+
"""Update positions and return the order ID. This interface can only be called in simulated trading mode.
|
|
4030
|
+
|
|
4031
|
+
Parameters
|
|
4032
|
+
----------
|
|
4033
|
+
symbol : str
|
|
4034
|
+
A STRING scalar, indicating the instrument.
|
|
4035
|
+
quantity : int
|
|
4036
|
+
An INT scalar, positive to increase position, negative to decrease position.
|
|
4037
|
+
price : float, optional
|
|
4038
|
+
A DOUBLE scalar, indicating the trade price. If set to 0 or left empty, the latest market price is used.
|
|
4039
|
+
|
|
4040
|
+
"""
|
|
4041
|
+
return plugin_backtest_updatePosition(self.engine().engine_handle, symbol, quantity, _convert_Nothing(price))
|
|
4042
|
+
|
|
4043
|
+
|
|
4044
|
+
class CryptoOrderMixin(StrategyBase):
|
|
4045
|
+
@final
|
|
4046
|
+
def submit_crypto_order(self, code: str, exchange: str, time: Timestamp, order_type: int, order_price: float, stop_loss_price: float, take_profit_price: float, quantity: int, direct: int, slippage: float, order_validity: int, expiration_time: Timestamp, /, label: str = None, account_type: AccountType = AccountType.DEFAULT):
|
|
4047
|
+
"""Submit an cryptocurrency order.
|
|
4048
|
+
|
|
4049
|
+
Parameters
|
|
4050
|
+
----------
|
|
4051
|
+
code : str
|
|
4052
|
+
A STRING scalar, representing the crypto code.
|
|
4053
|
+
exchange : str
|
|
4054
|
+
A STRING scalar, representing the exchange code.
|
|
4055
|
+
time : Timestamp
|
|
4056
|
+
A TIMESTAMP scalar, representing the order timestamp.
|
|
4057
|
+
order_type : int
|
|
4058
|
+
An INT scalar, representing the order type:
|
|
4059
|
+
|
|
4060
|
+
- 5: Limit order (default).
|
|
4061
|
+
|
|
4062
|
+
- 0: Market order, submitted at daily price limits, following time-priority rules.
|
|
4063
|
+
order_price : float
|
|
4064
|
+
A FLOAT scalar, representing the order price.
|
|
4065
|
+
stop_loss_price : float
|
|
4066
|
+
A FLOAT scalar, representing the stop-loss price.
|
|
4067
|
+
take_profit_price : float
|
|
4068
|
+
A FLOAT scalar, representing the take-profit price.
|
|
4069
|
+
quantity : int
|
|
4070
|
+
An INT scalar, representing the order quantity.
|
|
4071
|
+
direct : int
|
|
4072
|
+
An INT scalar, representing the buy/sell direction:
|
|
4073
|
+
|
|
4074
|
+
- 1: Buy open
|
|
4075
|
+
|
|
4076
|
+
- 2: Sell open
|
|
4077
|
+
|
|
4078
|
+
- 3: Sell close
|
|
4079
|
+
|
|
4080
|
+
- 4: Buy close
|
|
4081
|
+
slippage : float
|
|
4082
|
+
FLOAT, representing slippage.
|
|
4083
|
+
order_validity : int
|
|
4084
|
+
An INT scalar, representing order validity:
|
|
4085
|
+
|
|
4086
|
+
- 0: Valid for the day (default)
|
|
4087
|
+
|
|
4088
|
+
- 1: Immediate full execution or cancel (FOK)
|
|
4089
|
+
|
|
4090
|
+
- 2: Immediate partial execution, remaining canceled (FAK)
|
|
4091
|
+
expiration_time : Timestamp
|
|
4092
|
+
A TIMESTAMP scalar, representing the order expiration time.
|
|
4093
|
+
label : str, optional
|
|
4094
|
+
A STRING scalar, a tag for categorizing the order.
|
|
4095
|
+
account_type : AccountType, optional
|
|
4096
|
+
An Enum value representing the account type:
|
|
4097
|
+
|
|
4098
|
+
- SPOT: Cash account
|
|
4099
|
+
|
|
4100
|
+
- STOCK: Stock account
|
|
4101
|
+
|
|
4102
|
+
- FUTURES: Futures account
|
|
4103
|
+
|
|
4104
|
+
- OPTION: Options account
|
|
4105
|
+
|
|
4106
|
+
"""
|
|
4107
|
+
return plugin_backtest_submitOrder(self.engine().engine_handle, [code, exchange, time, order_type, order_price, stop_loss_price, take_profit_price, quantity, direct, slippage, order_validity, expiration_time], _convert_Nothing(label), sf_scalar(0, type="INT"), _convert_account(account_type))
|
|
4108
|
+
|
|
4109
|
+
|
|
4110
|
+
class TraditionalBacktester(BacktesterBase):
|
|
4111
|
+
def __init__(self, engine_name: sf_data.String, strategy_cls: Type[T], config: sf_data.Dictionary, security_reference: sf_data.Table = None):
|
|
4112
|
+
self.strategy_cls = strategy_cls
|
|
4113
|
+
self._config = config
|
|
4114
|
+
self._universe = None
|
|
4115
|
+
self.engine_handle = None
|
|
4116
|
+
|
|
4117
|
+
self.strategy = self.strategy_cls(self)
|
|
4118
|
+
|
|
4119
|
+
context = sf_dictionary(key_type="STRING", val_type="ANY")
|
|
4120
|
+
if "context" in self._config:
|
|
4121
|
+
for k, v in self._config["context"].items():
|
|
4122
|
+
context[k] = v
|
|
4123
|
+
if hasattr(strategy_cls, "context"):
|
|
4124
|
+
for k, v in strategy_cls.context.items():
|
|
4125
|
+
context[k] = v
|
|
4126
|
+
config["context"] = context
|
|
4127
|
+
|
|
4128
|
+
callback_d = sf_dictionary(key_type="STRING", val_type="ANY")
|
|
4129
|
+
|
|
4130
|
+
wkptr = weakref.ref(self)
|
|
4131
|
+
|
|
4132
|
+
def real_initialize(context):
|
|
4133
|
+
wkptr().engine_handle = context["engine"]
|
|
4134
|
+
F_swordfish_udf(getattr(wkptr().strategy, "initialize"))(context)
|
|
4135
|
+
|
|
4136
|
+
callback_d["initialize"] = F_swordfish_udf(real_initialize)
|
|
4137
|
+
self._check_valid_callback(callback_d, "beforeTrading", "before_trading")
|
|
4138
|
+
self._check_valid_callback(callback_d, "onTick", "on_tick")
|
|
4139
|
+
self._check_valid_callback(callback_d, "onSnapshot", "on_snapshot")
|
|
4140
|
+
self._check_valid_callback(callback_d, "onBar", "on_bar")
|
|
4141
|
+
self._check_valid_callback(callback_d, "onTransaction", "on_transaction")
|
|
4142
|
+
self._check_valid_callback(callback_d, "onOrder", "on_order")
|
|
4143
|
+
self._check_valid_callback(callback_d, "onTrade", "on_trade")
|
|
4144
|
+
self._check_valid_callback(callback_d, "afterTrading", "after_trading")
|
|
4145
|
+
self._check_valid_callback(callback_d, "finalize", "finalize")
|
|
4146
|
+
on_timer_d = sf_dictionary(key_type="SECOND", val_type="ANY")
|
|
4147
|
+
for k in self.strategy_cls._timer_funcs:
|
|
4148
|
+
child_method = getattr(self.strategy, k)
|
|
4149
|
+
on_timer_d[self.strategy_cls._timer_funcs[k]] = F_swordfish_udf(child_method)
|
|
4150
|
+
callback_d["onTimer"] = on_timer_d
|
|
4151
|
+
|
|
4152
|
+
if hasattr(strategy_cls, "callback_d"):
|
|
4153
|
+
for k, v in strategy_cls.callback_d.items():
|
|
4154
|
+
callback_d[k] = v
|
|
4155
|
+
|
|
4156
|
+
self.engine_handle = plugin_backtest_createBacktester(engine_name, config, callback_d, False, _convert_Nothing(security_reference))
|
|
4157
|
+
self.engine_name = engine_name
|
|
4158
|
+
|
|
4159
|
+
if isinstance(config["cash"], sf_data.Dictionary):
|
|
4160
|
+
self.accounts = {}
|
|
4161
|
+
for k in config["cash"].keys():
|
|
4162
|
+
keys = [_.strip() for _ in str(k).split(",")]
|
|
4163
|
+
for key in keys:
|
|
4164
|
+
self.accounts[AccountType(key)] = Account(AccountType(key), self.engine_handle)
|
|
4165
|
+
else:
|
|
4166
|
+
self.accounts = {
|
|
4167
|
+
AccountType.DEFAULT: Account(AccountType.DEFAULT, self.engine_handle)
|
|
4168
|
+
}
|
|
4169
|
+
|
|
4170
|
+
def __del__(self):
|
|
4171
|
+
if self.engine_handle is None:
|
|
4172
|
+
return
|
|
4173
|
+
if not Runtime().check():
|
|
4174
|
+
return
|
|
4175
|
+
if self.engine_name in plugin_backtest_getBacktestEngineList().keys():
|
|
4176
|
+
plugin_backtest_dropBacktestEngine(self.engine_name)
|
|
4177
|
+
self.engine_handle = None
|
|
4178
|
+
|
|
4179
|
+
@final
|
|
4180
|
+
def append_data(self, data):
|
|
4181
|
+
"""Insert market data to execute a strategy backtest.
|
|
4182
|
+
|
|
4183
|
+
Parameters
|
|
4184
|
+
----------
|
|
4185
|
+
data : _type_
|
|
4186
|
+
A table containing market data as input.
|
|
4187
|
+
"""
|
|
4188
|
+
plugin_backtest_appendQuotationMsg(self.engine_handle, data)
|
|
4189
|
+
|
|
4190
|
+
@final
|
|
4191
|
+
def append_end(self):
|
|
4192
|
+
"""Insert end marker to indicate the end of market data.
|
|
4193
|
+
"""
|
|
4194
|
+
plugin_backtest_appendEndMarker(self.engine_handle)
|
|
4195
|
+
|
|
4196
|
+
@final
|
|
4197
|
+
def _check_valid_callback(self, callback_d, callback_name: str, method_name: str):
|
|
4198
|
+
parent_method = getattr(StrategyInterface, method_name, None)
|
|
4199
|
+
child_method = getattr(self.strategy_cls, method_name, None)
|
|
4200
|
+
if child_method is not parent_method:
|
|
4201
|
+
callback_d[callback_name] = F_swordfish_udf(getattr(self.strategy, method_name))
|
|
4202
|
+
|
|
4203
|
+
@final
|
|
4204
|
+
@property
|
|
4205
|
+
def context_dict(self):
|
|
4206
|
+
"""Return the logical context.
|
|
4207
|
+
|
|
4208
|
+
"""
|
|
4209
|
+
return plugin_backtest_getContextDict(self.engine_handle)
|
|
4210
|
+
|
|
4211
|
+
@final
|
|
4212
|
+
@property
|
|
4213
|
+
def universe(self):
|
|
4214
|
+
"""Set the symbol pool for the engine.
|
|
4215
|
+
|
|
4216
|
+
"""
|
|
4217
|
+
return self._universe
|
|
4218
|
+
|
|
4219
|
+
@final
|
|
4220
|
+
@universe.setter
|
|
4221
|
+
def universe(self, val: List[str]):
|
|
4222
|
+
plugin_backtest_setUniverse(self.engine_handle, val)
|
|
4223
|
+
self._universe = val
|
|
4224
|
+
|
|
4225
|
+
@final
|
|
4226
|
+
@property
|
|
4227
|
+
def config(self) -> sf_data.Dictionary:
|
|
4228
|
+
return plugin_backtest_getConfig(self.engine_handle)
|