{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "std::cout are redirected to python::stdout\n", "std::cerr are redirected to python::stderr\n", "[2019-03-27 21:36:20.830] [info] Loading market information...\n", "[2019-03-27 21:36:20.834] [info] Loading stock type information...\n", "[2019-03-27 21:36:20.836] [info] Loading stock information...\n", "[2019-03-27 21:36:25.799] [info] Loading KData...\n", "[2019-03-27 21:36:25.810] [info] Preloading all day kdata to buffer!\n", "[2019-03-27 21:36:35.496] [info] 9.69635s Loaded Data.\n", "Wall time: 17.6 s\n" ] } ], "source": [ "%matplotlib inline\n", "%time from hikyuu.interactive.interactive import *" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# 1 利用 TM 实现简单的记账本\n", "\n", "TradeManager对象可以理解为一个模拟的交易账户,负责交易的买/卖操作、记录交易记录以及持仓情况,也可以通过修改其买/卖操作的接口实现实盘接入。创建一个模拟交易账户,通常使用快捷创建函数 crtTM。TM对象的基本操作:\n", "\n", "- buy 买入\n", "- sell 卖出\n", "- checkin 存入现金\n", "- checkout 取出现金\n", "\n", "可以利用 TM 实现简单的记账本,手工记录自己的操作情况,例如:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TradeManager {\n", " params: params[precision(int): 2, reinvest(bool): 0, save_action(bool): 1, support_borrow_cash(bool): 0, support_borrow_stock(bool): 0, ],\n", " name: SYS,\n", " init_date: 2017-1-1 0:0:0,\n", " init_cash: 100000.00,\n", " firstDatetime: 2017-1-3 0:0:0,\n", " lastDatetime: 2017-1-3 0:0:0,\n", " TradeCostFunc: TradeCostFunc(TC_Zero, params[]),\n", " current cash: 99089.00,\n", " current market_value: 916.00,\n", " current short_market_value: 0.00,\n", " current base_cash: 100000.00,\n", " current base_asset: 0.00,\n", " current borrow_cash: 0.00,\n", " current borrow_asset: 0.00,\n", " Position: \n", " SZ000001 平安银行 2017-1-3 0:0:0 543 100 911.00 1238.00 327.00 35.89% 0.33%\n", " Short Position: \n", " Borrow Stock: \n", "}\n" ] } ], "source": [ "#创建一个初始资金10万元,起始日期2017年1月1日的模拟账户\n", "my_tm = crtTM(initCash=100000, datetime=Datetime(201701010000))\n", "\n", "#2017年1月3日以9.11的价格买入100股\n", "td = my_tm.buy(Datetime(201701030000), sm['sz000001'], 9.11, 100)\n", "\n", "#查看当前资金及持仓情况\n", "print(my_tm)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | 证券名称 | \n", "买入日期 | \n", "已持仓天数 | \n", "持仓数量 | \n", "投入金额 | \n", "当前市值 | \n", "盈亏金额 | \n", "盈亏比例 | \n", "
---|---|---|---|---|---|---|---|---|
证券代码 | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
SZ000001 | \n", "平安银行 | \n", "2017-01-03 | \n", "543 | \n", "100 | \n", "911.0 | \n", "1238.0 | \n", "327.0 | \n", "35.894621 | \n", "