{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# Shioaji\n", "![shioaji-logo](https://sinotrade.github.io/images/shioaji-logo-01.png)![sinopac-logo](https://www.sinotrade.com.tw/Images/logo.png)\n", "\n", "\n", "[![PyPI - Status](https://img.shields.io/pypi/v/shioaji.svg?style=for-the-badge)](https://pypi.org/project/shioaji)\n", "[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/shioaji.svg?style=for-the-badge)]()\n", "[![PyPI - Downloads](https://img.shields.io/pypi/dm/shioaji.svg?style=for-the-badge)](https://pypi.org/project/shioaji)\n", "[![Coverage](https://img.shields.io/badge/coverage%20-99%25-yellowgreen.svg?style=for-the-badge)]()\n", "[![Binder](https://img.shields.io/badge/launch-Tutorial-ff69b4.svg?style=for-the-badge)](https://mybinder.org/v2/gh/Sinotrade/Sinotrade.github.io/master?filepath=tutorial%2Fshioaji_tutorial.ipynb)\n", "[![doc](https://img.shields.io/badge/docs%20-passing-orange.svg?style=for-the-badge)](https://sinotrade.github.io/)\n", "[![Telegram](https://img.shields.io/badge/chat-%20on%20telegram-blue.svg?style=for-the-badge)](https://t.me/joinchat/973EyAQlrfthZTk1)\n", "\n", "Shioaji is a trading API provided by Sinopac that offers a comprehensive and user-friendly platform for accessing the Taiwan financial markets. With Shioaji, you can trade a variety of financial instruments including stocks, futures, and options using your favorite Python packages such as numpy, scipy, pandas, pytorch, or tensorflow to build your own custom trading models. The platform is easy to use and intuitive, with advanced charting tools, real-time market data, and a customizable interface that allows you to tailor your trading experience to your specific needs. Shioaji is fast and efficient, with a high-performance core implemented in C++ and using FPGA event broker technology, and it is the first Python trading API in Taiwan that is compatible with Linux and Mac, making it a truly cross-platform solution. Whether you are a beginner looking to get started in the world of trading or an experienced trader looking for a more powerful platform, Shioaji has something to offer. [Sign up for a free account today and start trading with confidence.](https://sinotrade.github.io/tutor/prepare/open_account)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## [Quickstarts](https://sinotrade.github.io/quickstart/)\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Getting Started" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install shioaji" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Just import Our api like other popular python library to get start" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import shioaji as sj\n", "from shioaji import constant" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Use Shioaji object to setup setting and login" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "api = sj.Shioaji(simulation=True)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## ⚠️Apply for an API token on the [Sinopac](https://www.sinotrade.com.tw/newweb/PythonAPIKey/)⚠️" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "api_key = \"your api key\"\n", "secret_key =\"your secret key\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "api.login(api_key=api_key, secret_key=secret_key)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### List all your account" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "api.list_accounts()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Activate your cetifacation to start ordering\n", "❗❗❗ activate_ca is not necessary when using the **simulation** environment." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# api.activate_ca(ca_path='../ca/Sinopac.pfx', ca_passwd='SCCEIEFAJA', person_id=person_id)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Contract object for subscribe quote and place order" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### You can obtain **contracts** in the following ways." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Stock" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# For example, to obtain contract of TSMC (symbol 2330)\n", "api.Contracts.Stocks.TSE.TSE2330\n", "api.Contracts.Stocks['2330']" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Futures" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "# For example, to obtain contract of TXF (symbol TXF)\n", "api.Contracts.Futures.TXF.TXF202301\n", "api.Contracts.Futures['TXFA3']" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Futures - R1、R2 the ticker symbol refers to the near month of expiration\n", "**⚠️Currently only supports historical quotes query ⚠️**\n", "- \"R1\" is referred to as the \"current month\" contract. \n", "- \"R2\" is referred to as the \"next month\" contract." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "api.Contracts.Futures.TXF.TXFR1\n", "api.Contracts.Futures.TXF['TXFR1']" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Quote Api" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Prepare your callback, now just keep it simple\n", "official document\n", "- [Streaming Data for Stock](https://sinotrade.github.io/tutor/market_data/streaming/stocks/)\n", "- [Streaming Data for Futures](https://sinotrade.github.io/tutor/market_data/streaming/futures/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# For example, subscribe to the tick for TSMC (symbol 2330)\n", "from shioaji import Exchange, TickSTKv1, TickFOPv1\n", "from shioaji import constant\n", " \n", "@api.on_tick_stk_v1()\n", "def quote_callback(exchange: Exchange, tick:TickSTKv1):\n", " print(f\"Exchange: {exchange}, Tick: {tick}\")\n", "\n", "api.quote.subscribe(\n", " api.Contracts.Stocks[\"2330\"], \n", " quote_type = constant.QuoteType.Tick,\n", " version = constant.QuoteVersion.v1\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# For example, subscribe to the tick for Taiwan Stock Exchange Capitalization Weighted Stock Index (TAIEX) (symbol TXF)\n", "from shioaji import Exchange, TickSTKv1, TickFOPv1\n", "from shioaji import constant\n", "\n", "from shioaji import TickFOPv1, Exchange\n", "\n", "@api.on_tick_fop_v1()\n", "def quote_callback(exchange:Exchange, tick:TickFOPv1):\n", " print(f\"Exchange: {exchange}, Tick: {tick}\")\n", "\n", "api.quote.subscribe(\n", " api.Contracts.Futures.TXF['TXF202301'],\n", " quote_type = constant.QuoteType.Tick, # or 'tick'\n", " version = constant.QuoteVersion.v1, # or 'v1'\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Making Order object to place order" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shioaji import constant" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Place Order - Stock" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "TSE2890 = api.Contracts.Stocks['2890']\n", "\n", "sample_order = api.Order(price=15.1,\n", " quantity=2,\n", " action=constant.Action.Buy,\n", " price_type=constant.StockPriceType.LMT,\n", " order_type=constant.OrderType.ROD,\n", " daytrade=True,\n", " account=api.futopt_account,\n", " )\n", "\n", "# just pass Order object to place_order fuction to place order then will get the Trade object return \n", "trade = api.place_order(TSE2890, sample_order)\n", "trade" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Place Order - Futures" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "sample_order = api.Order(price=14000,\n", " quantity=5,\n", " action=constant.Action.Buy,\n", " price_type=constant.FuturesPriceType.LMT,\n", " order_type=constant.OrderType.ROD,\n", " octype=constant.FuturesOCType.Auto,\n", " account=api.futopt_account,\n", " )\n", "\n", "# just pass Order object to place_order fuction to place order then will get the Trade object return \n", "trade = api.place_order(TXFA3, sample_order)\n", "trade" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Update the trade object status to get the trade information" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "api.update_status()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Obtain all **trade** objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "api.list_trades()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Cancel order of the trade" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "trade = api.cancel_order(trade)\n", "trade" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Communication\n", "- [Chat](https://t.me/joinchat/YpbYTNlXfoViYzQ1): The most timely and recommended channel for communication\n", "- [Discuss](https://sjapi.tw): general chat, online discussions, collaboration etc.\n", "- [GitHub issues](https://github.com/Sinotrade/Shioaji/issues): bug reports, feature requests, install issues, RFCs, thoughts, etc." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Releases and Contributing\n", "Shioaji current state is Pre-Alpha, we expect no obvious bugs. Please let us know if you encounter a bug by [filing an issue](https://github.com/Sinotrade/Shioaji/issues).\n", "\n", "We appreciate all suggestions. If you have any idea want us to implement, please discuss with us in [telegram](https://t.me/joinchat/YpbYTNlXfoViYzQ1)." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The Team\n", "Shioaji is currently maintained by [ec666 team](https://github.com/orgs/Sinotrade/teams/shioaji_dev/members) with major contributions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.15" }, "vscode": { "interpreter": { "hash": "78d9b63bcf394f52dc771fd92ca63dfd965393d0fa098fc40dce082b8313661e" } } }, "nbformat": 4, "nbformat_minor": 2 }