{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "参数研究*args, **kwargs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://blog.csdn.net/GODSuner/article/details/117961990" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*args 和 ``kwargs` 这两个魔法变量需要花费大量时间来解释。别被这些语句所绊倒。其实这些并不是什么超级特殊的参数,也并不奇特,只是编程人员约定的变量名字,args 是 arguments 的缩写,表示位置参数;kwargs 是 keyword arguments 的缩写,表示关键字参数。 接下来我们就来一起了解他们的用法**,以及在什么情况下使用?·\n", "\n", "首先你要明白的是:\n", "\n", "其实并不是写成 *args 和 **kwargs ,只有前面的 * (星号)才是必须的。\n", "向python传递参数的方式有两种:\n", "位置参数(positional argument)\n", "关键词参数(keyword argument)\n", "现在我们再来看 *args 与 **kwargs 的区别,两者都是 python 中可变的参数\n", "*args 表示任何多个无名参数, 他本质上是一个 tuple\n", "** kwargs 表示关键字参数, 它本质上是一个 dict\n", "\n", "同时使用时必须要求 *args 参数列要在** kwargs 前面 【因为位置参数在关键字参数的前面。】" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*args 的用法\n", "* args 和 ** args 主要用于函数定义,你可以将不定数量的参数传递给一个函数。\n", "\n", "这里不定的意思是: 预先并不知道,函数使用者会传递多少个参数给你,所在在这个场景下使用这两个关键字。 * args 是用来发送一个 非键值 的可变数量的参数列表给一个函数。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first normal arg: yasoob\n", "another arg through *argv: python\n", "another arg through *argv: eggs\n", "another arg through *argv: test\n" ] } ], "source": [ "def test_var_args(f_arg, *argv):\n", " print(\"first normal arg:\",f_arg)\n", " for arg in argv:\n", " print(\"another arg through *argv:\",arg)\n", "test_var_args('yasoob','python','eggs','test')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*args的用法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*args就是就是传递一个可变参数列表给函数实参,这个参数列表的数目未知,甚至长度可以为0。下面这段代码演示了如何使用args" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Required argument: 1\n", "\n", "Optional argument: 2\n", "Optional argument: 3\n", "Optional argument: 4\n" ] } ], "source": [ "def test_args(first, *args):\n", " print('Required argument: ', first)\n", " print(type(args))\n", " for v in args:\n", " print ('Optional argument: ', v)\n", " print(type(v))\n", "\n", "test_args(1, 2, 3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "第一个参数是必须要传入的参数,所以使用了第一个形参,而后面三个参数则作为可变参数列表传入了实参,并且是作为元组tuple来使用的。代码的运行结果如下" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**kwargs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "而**kwargs则是将一个可变的关键字参数的字典传给函数实参,同样参数列表长度可以为0或为其他值。下面这段代码演示了如何使用kwargs" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Required argument: 1\n", "\n", "Optional argument (args): 2\n", "Optional argument (args): 3\n", "Optional argument (args): 4\n", "{'k1': 5, 'k2': 6}\n", "Optional argument k1 (kwargs): 5\n", "Optional argument k2 (kwargs): 6\n" ] } ], "source": [ "def test_kwargs(first, *args, **kwargs):\n", " print('Required argument: ', first)\n", " print(type(kwargs))\n", " for v in args:\n", " print ('Optional argument (args): ', v)\n", " print(kwargs)\n", " for k, v in kwargs.items():\n", " print ('Optional argument %s (kwargs): %s' % (k, v))\n", "\n", "test_kwargs(1, 2, 3, 4, k1=5, k2=6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "正如前面所说的,args类型是一个tuple,而kwargs则是一个字典dict,并且args只能位于kwargs的前面。代码的运行结果如下" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "调用函数\n", "args和kwargs不仅可以在函数定义中使用,还可以在函数调用中使用。在调用时使用就相当于pack(打包)和unpack(解包),类似于元组的打包和解包。\n", "\n", "首先来看一下使用args来解包调用函数的代码," ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1: two\n", "arg2: 3\n", "arg3: 5\n" ] } ], "source": [ "def test_args_kwargs(arg1, arg2, arg3):\n", " print(\"arg1:\", arg1)\n", " print(\"arg2:\", arg2)\n", " print(\"arg3:\", arg3)\n", "\n", "args = (\"two\", 3, 5)\n", "test_args_kwargs(*args)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "将元组解包后传给对应的实参,kwargs的用法与其类似。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1: 5\n", "arg2: two\n", "arg3: 3\n" ] } ], "source": [ "kwargs = {\"arg3\": 3, \"arg2\": \"two\", \"arg1\": 5}\n", "test_args_kwargs(**kwargs)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "聚合宽数据研究" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "交易函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1 order(security, amount, style=None, side='long', pindex=0, close_today=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "交易函数\n", "1 order(security, amount, style=None, side='long', pindex=0, close_today=False)\n", "买卖标的。调用成功后, 您将可以调用[get_open_orders]取得所有未完成的交易, 也可以调用[cancel_order]取消交易\n", "\n", "参数\n", "\n", "security: 标的代码\n", "amount: 交易数量, 正数表示买入, 负数表示卖出\n", "style: 参见OrderStyle, None代表MarketOrder\n", "side: 'long'/'short',操作多单还是空单。默认为多单,股票、基金暂不支持开空单。\n", "pindex: 在使用set_subportfolios创建了多个仓位时,指定subportfolio 的序号, 从 0 开始, 比如 0 指定第一个 subportfolio, 1 指定第二个 subportfolio,默认为0。\n", "close_today: 平今字段,close_today: 平今字段,仅对上海国际能源中心,上海期货交易所,中金所生效,其他交易所将会报错(其他交易所没有区分平今与平昨,均按照先开先平的方法处理)。\n", "对上海国际能源中心,上海期货交易所,中金所的标的:\n", "close_today = True, 只平今仓,今仓不足的时候,订单将会被废单。\n", "close_today = False, 优先平昨仓,昨仓不足部分平今仓\n", "不管close_today是True还是False,此函数只会产生一个订单,区别在于平仓时的手续费计算,平昨仓使用close_commission对应手续费率,平今仓使用close_today_commission手续费率。\n", "返回 Order对象或者None, 如果创建订单成功, 则返回Order对象, 失败则返回None\n", "\n", "示例\n", "\n", "#买入平安银行股票100股\n", "order('000001.XSHE', 100) # 下一个市价单\n", "order('000001.XSHE', 100, MarketOrderStyle()) # 下一个市价单, 功能同上(科创板市价单需要指定保护价)\n", "order('000001.XSHE', 100, LimitOrderStyle(10.0)) # 以10块价格下一个限价单\n", "'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "order_target(security, amount, style=None, side='long', pindex=0, close_today=False)\n", "买卖标的, 使最终标的的数量达到指定的amount,注意使用此接口下单时若指定的标的有未完成的订单,则先前未完成的订单将会被取消\n", "\n", "参数\n", "\n", "security: 标的代码\n", "amount: 期望的最终数量\n", "style: 参见OrderStyle, None代表MarketOrder\n", "side: 'long'/'short',操作多单还是空单。默认为多单。默认为多单,股票、基金暂不支持开空单。\n", "pindex: 在使用set_subportfolios创建了多个仓位时,指定subportfolio 的序号, 从 0 开始, 比如 0为 指定第一个 subportfolio, 1 为指定第二个 subportfolio,默认为0。\n", "close_today: 平今字段,close_today: 平今字段,仅对上海国际能源中心,上海期货交易所,中金所生效,其他交易所将会报错(其他交易所没有区分平今与平昨,均按照先开先平的方法处理)。\n", "对上海国际能源中心,上海期货交易所,中金所的标的:\n", "close_today = True, 只平今仓,今仓不足的时候,订单将会被废单。\n", "close_today = False, 优先平昨仓,昨仓不足部分平今仓\n", "不管close_today是True还是False,此函数只会产生一个订单,区别在于平仓时的手续费计算,平昨仓使用close_commission对应手续费率,平今仓使用close_today_commission手续费率。\n", "返回 Order对象或者None, 如果创建委托成功, 则返回Order对象, 失败则返回None\n", "\n", "示例\n", "\n", "# 卖出平安银行所有股票\n", "order_target('000001.XSHE', 0)\n", "# 买入平安银行所有股票到100股\n", "order_target('000001.XSHE', 100)\n", "'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "order_value(security, value, style=None, side='long', pindex=0, close_today=False)\n", "买卖价值为value的标的。\n", "\n", "参数\n", "\n", "security: 股票名字\n", "\n", "value: 股票价值,value = 最新价 * 手数 * 保证金率(股票为1) * 乘数(股票为100)\n", "\n", "style: 参见OrderStyle, None代表MarketOrder\n", "\n", "side: 'long'/'short',操作多单还是空单。默认为多单。默认为多单,股票、基金暂不支持开空单。\n", "\n", "pindex: 在使用set_subportfolios创建了多个仓位时,指定subportfolio 的序号, 从 0 开始, 比如 0为 指定第一个 subportfolio, 1 为指定第二个 subportfolio,默认为0。\n", "\n", "close_today: 平今字段,close_today: 平今字段,仅对上海国际能源中心,上海期货交易所,中金所生效,其他交易所将会报错(其他交易所没有区分平今与平昨,均按照先开先平的方法处理)。\n", "\n", "对上海国际能源中心,上海期货交易所,中金所的标的:\n", "close_today = True, 只平今仓,今仓不足的时候,订单将会被废单。\n", "close_today = False, 优先平昨仓,昨仓不足部分平今仓\n", "不管close_today是True还是False,此函数只会产生一个订单,区别在于平仓时的手续费计算,平昨仓使用close_commission对应手续费率,平今仓使用close_today_commission手续费率。\n", "返回 Order对象或者None, 如果创建委托成功, 则返回Order对象, 失败则返回None\n", "\n", "示例\n", "\n", "#卖出价值为10000元的平安银行股票\n", "order_value('000001.XSHE', -10000)\n", "#买入价值为10000元的平安银行股票\n", "order_value('000001.XSHE', 10000)\n", "'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "order_target_value(security, value, style=None, side='long', pindex=0, close_today=False)\n", "调整标的仓位到value价值,注意使用此接口下单时若指定的标的有未完成的订单,则先前未完成的订单将会被取消\n", "\n", "参数\n", "\n", "security: 标的名字\n", "value: 期望的标的最终价值,value = 最新价 * 手数 * 保证金率(股票为1) * 乘数(股票为100)\n", "style: 参见OrderStyle, None代表MarketOrder\n", "side: 'long'/'short',操作多单还是空单。默认为多单。\n", "pindex: 在使用set_subportfolios创建了多个仓位时,指定subportfolio 的序号, 从 0 开始, 比如 0为 指定第一个 subportfolio, 1 为指定第二个 subportfolio,默认为0。\n", "close_today: 平今字段,close_today: 平今字段,仅对上海国际能源中心,上海期货交易所,中金所生效,其他交易所将会报错(其他交易所没有区分平今与平昨,均按照先开先平的方法处理)。\n", "对上海国际能源中心,上海期货交易所,中金所的标的:\n", "close_today = True, 只平今仓,今仓不足的时候,订单将会被废单。\n", "close_today = False, 优先平昨仓,昨仓不足部分平今仓\n", "不管close_today是True还是False,此函数只会产生一个订单,区别在于平仓时的手续费计算,平昨仓使用close_commission对应手续费率,平今仓使用close_today_commission手续费率。\n", "返回 Order对象或者None, 如果创建委托成功, 则返回Order对象, 失败则返回None\n", "\n", "示例\n", "\n", "#卖出平安银行所有股票\n", "order_target_value('000001.XSHE', 0)\n", "#调整平安银行股票仓位到10000元价值\n", "order_target_value('000001.XSHE', 10000)\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "聚宽的类研究" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Order对象" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "买卖订单\n", "关于order/trader对象及订单处理\n", "以csv格式保存order、trade、position数据\n", "\n", "status: 状态, 一个OrderStatus值\n", "add_time: 订单添加时间, [datetime.datetime]对象\n", "is_buy: bool值, 买还是卖,对于期货:\n", "开多/平空 -> 买\n", "开空/平多 -> 卖\n", "amount: 下单数量, 不管是买还是卖, 都是正数\n", "filled: 已经成交的股票数量, 正数\n", "security: 股票代码\n", "order_id: 订单ID\n", "price: 平均成交价格, 已经成交的股票的平均成交价格(一个订单可能分多次成交)\n", "avg_cost: 卖出时表示下卖单前的此股票的持仓成本, 用来计算此次卖出的收益. 买入时表示此次买入的均价(等同于price).\n", "side: 多/空,'long'/'short'\n", "action: 开/平, 'open'/'close'\n", "commission:交易费用(佣金、税费等)\n", "orders = order('000001.XSHE', 100)\n", "print(orders)\n", "if orders is None:\n", " print(\"创建订单失败...\")\n", "else:\n", " print(\"交易费用单:{0}\".format(orders.commission))\n", " print(\"是否买单:{0}\".format(orders.is_buy))\n", " print(\"订单状态:{0}\".format(orders.status))\n", " print(\"订单平均成交价格:{0}\".format(orders.price))\n", "'''" ] } ], "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.8.8" } }, "nbformat": 4, "nbformat_minor": 2 }