{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wall time: 11.7 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": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TradeManager {\n", " params: params[precision(i): 2, reinvest(b): 0, save_action(b): 1, support_borrow_cash(b): 0, support_borrow_stock(b): 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 184 100 911.00 1111.00 200.00 21.95% 0.20%\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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
证券名称买入日期已持仓天数持仓数量投入金额当前市值盈亏金额盈亏比例
证券代码
SZ000001平安银行2017-01-03184100911.01111.0200.021.953897
\n", "
" ], "text/plain": [ " 证券名称 买入日期 已持仓天数 持仓数量 投入金额 当前市值 盈亏金额 盈亏比例\n", "证券代码 \n", "SZ000001 平安银行 2017-01-03 184 100 911.0 1111.0 200.0 21.953897" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#转化为pandas的DataFrame显示当前持仓情况 \n", "position = my_tm.getPositionList()\n", "position.to_df()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#2017年2月21日以9.60的价格卖出100股\n", "td = my_tm.sell(Datetime(201702210000), sm['sz000001'], 9.60)\n", "\n", "my_tm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2 利用 Excel 查看交易详情\n", "\n", "使用 tocsv 方法将 TM 的交易记录、当前持仓及已平仓详细情况分别保存为 csv 文件,以便用 Excel 查看详情。\n", "\n", "tocsv方法参数为一个指定的目录,目录必须以存在。其输出会在指定目录中,生成三个文件,“TM名称_交易记录.csv”、“TM名称_未平仓记录.csv”、“TM名称_已平仓记录.csv”。TM名称可在crtTM创建TM对象时指定,默认为“SYS”,如下图所示。\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#在 hikyuu_XXX.ini 文件中配置的临时路径中输出\n", "my_tm.tocsv(sm.tmpdir())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 Excel 查看 csv,如:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3 使用序列化保存或重新载入已有TM对象" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#保存至指定文件\n", "from datetime import date\n", "filename = \"{}/my_trade/my_trade_record_{}.xml\".format(sm.tmpdir(), date.today());\n", "hku_save(my_tm, filename)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#载入已保存的TM对象\n", "#filename = \"{}/my_trade/my_trade_record_{}.xml\".format(sm.tmpdir(), date.today())\n", "new_my_tm = crtTM()\n", "hku_load(new_my_tm, filename)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 hku_save 保存的对象,其格式为XML文件,可直接使用 XML 工具或浏览器查看:\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.5.3" } }, "nbformat": 4, "nbformat_minor": 0 }