{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json\n", "import shioaji as sj" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Response Code: 0 | Event Code: 0 | Info: host '203.66.91.161:80', IP 203.66.91.161:80 (host 1 of 1) (host connection attempt 1 of 1) (total connection attempt 1 of 1) | Event: Session up\n" ] } ], "source": [ "api = sj.Shioaji()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "api.tft = True" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "with open('login.json', 'rb') as f:\n", " login_kwargs = json.loads(f.read())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "login_kwargs = dict(person_id=\"YOUR_PERSONID\", passwd=\"YOUR_PASSWORD\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " fetch complete.\n", " fetch complete.\n", " fetch complete.\n", " fetch complete.\n" ] } ], "source": [ "accounts = api.login(**login_kwargs, contracts_cb=lambda security_type: print(f\"{repr(security_type)} fetch complete.\"))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "with open('ca.json', 'rb') as f:\n", " ca_kwargs = json.loads(f.read())" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "ca_kwargs = dict(ca_path=\"YOUR_CA_PATH\",\n", " ca_passwd=\"CA_PASSWORD\",\n", " person_id=\"CA_BELONGS_TO_PERSON_ID\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ca Initial Done.\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api.activate_ca(**ca_kwargs)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Stock(exchange=, code='2890', symbol='TSE2890', name='永豐金', category='17')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c2330 = api.Contracts.Stocks[\"2890\"]\n", "c2330" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Order(action=, price=12.5, quantity=2, price_type=, order_type=)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "order = api.Order(price=12.5, \n", " quantity=2, \n", " action=sj.constant.Action.Buy, \n", " price_type=sj.constant.TFTStockPriceType.LMT, \n", " order_type=sj.constant.OrderType.ROD)\n", "order" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Trade(contract=Stock(exchange=, code='2890', symbol='TSE2890', name='永豐金', category='17'), order=Order(action=, price=12.5, quantity=2, account=StockAccount(person_id='P124081046', broker_id='9A95', account_id='9816502', signed=True, username='楊之帆'), price_type=, order_type=), status=OrderStatus(status=))" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "contract=Stock(exchange=, code='2890', symbol='TSE2890', name='永豐金', category='17') order=Order(action=, price=12.5, quantity=2, id='64bb488a', seqno='602143', ordno='00000', account=Account(account_type=, person_id='P124081046', broker_id='9A95', account_id='9816502', signed=True), price_type=, order_type=) status=OrderStatus(id='64bb488a', status=, status_code='0', order_datetime=datetime.datetime(2020, 2, 20, 19, 7, 55), deals=[])\n" ] } ], "source": [ "trade = api.place_order(c2330, order, timeout=0, cb=lambda trade: print(trade))\n", "trade" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "api.update_status()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Trade(contract=Stock(exchange=, code='2890', symbol='TSE2890', name='永豐金', category='17'), order=Order(action=, price=12.5, quantity=2, id='64bb488a', seqno='602143', ordno=' ', account=Account(account_type=, person_id='P124081046', broker_id='9A95', account_id='9816502', signed=True), price_type=, order_type=), status=OrderStatus(id='64bb488a', status=, status_code='R', order_datetime=datetime.datetime(2020, 2, 20, 19, 7, 55), deals=[]))" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trade" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }