Backtrader中文笔记之Orders

Orders

订单

Cerebro is the key control system in backtrader and Strategy (a subclass) is the key control point of the end user. The latter needs a chaining method to other parts of the system and that’s where orders play a key role.

Cerebrobacktrader整个系统的关键控制,策略(子类)是用户的关键控制点。后者需要一种链接到系统其他部分的方法,而这正是订单发挥关键作用的地方。

Orders translate the decisions made by the logic in a Strategy into a message suitable for the Broker to execute an action. This is done with:

订单将策略中所做的逻辑决策转换为适合代理执行操作的消息。这是通过:

  • Creation

  • 创建

     

    Through Strategy’s methods: buy\``,sellandclose(Strategy) which return anorder` instance as a reference

  • 通过策略的方法:buy,sell和close(策略)返回一个order实例作为引用
  • Cancellation

  • 消除

     

    Through Strategy’s method: cancel (Strategy) which takes an order instance to operate on

  • 通过策略的方法:cancel(Strategy)取一个订单实例进行操作

And the orders serve also as a communication method back to the user, to notify how things are running in the broker.

同时,这些命令还作为一种通信方法返回给用户,以通知broker程序中的运行情况。

  • Notification

  • 通知

     

    To Strategy method: notify_order (Strategy) which reports an order instance

  策略的方法:notify_order(策略)他报告一个订单实例

 

Order creation

订单创建

When invoking the buy, sell and close the following parameters apply for creation:

调用“买入”、“卖出”和“关闭”时,以下参数适用于创建:

  • data (default: None)

    For which data the order has to be created. If None then the first data in the system, self.datas[0] or self.data0 (aka self.data) will be used

  • 必须为其选择创建订单的数据。如果没有,那么系统中的第一个数据,self.datas[0] or self.data0(又名self.data)将被使用
  • size (default: None)

    Size to use (positive) of units of data to use for the order.

  • 为订单使用的数据单位的大小(正)。
  •  

    If None the sizer instance retrieved via getsizer will be used to determine the size.

  • 如果没有,那么通过getsizer获取的sizer实例将用于确定大小。
  • price (default: None)

    Price to use (live brokers may place restrictions on the actual format if it does not comply to minimum tick size requirements)

  • 使用价格(如果实际格式不符合最小刻度大小要求,实时经纪人可能会对输入格式进行限制)
  •  

    None is valid for Market and Close orders (the market determines the price)

  • 对市场价交易和关闭交易指令无效(市场决定价格)
  •  

    For Limit, Stop and StopLimit orders this value determines the trigger point (in the case of Limit the trigger is obviously at which price the order should be matched)

  • 只有对于Limit, Stop和StopLimit订单,这个值决定了触发点(在Limit的情况下,触发点显然是该指令应该匹配的价格)。
  • plimit (default: None)

    Only applicable to StopLimit orders. This is the price at which to set the implicit Limit order, once the Stop has been triggered (for which price has been used)

  • 仅适用于StopLimit指令。一旦止损被触发,这就是设定隐含的限价指令的价格(已使用价格)
  • exectype (default: None)

  • 执行模式

    Possible values:

  • 可能的值
  •  

    • Order.Market or None. A market order will be executed with the next available price. In backtesting it will be the opening price of the next bar

    • Order.Market or None.。市场订单将以下一个可用价格执行。在回溯测试中,它将是下一个bar的开盘价
    • Order.Limit. An order which can only be executed at the given price or better

      • Order.Limit.只能以给定价格或更好价格执行的订单
    • Order.Stop. An order which is triggered at price and executed like an Order.Market order

    • Order.Stop.当订单触发到这个价格的时候,就像市场价一样成交。
    • Order.StopLimit. An order which is triggered at price and executed as an implicit Limit order with price given by pricelimit

    • Order.StopLimit.一种以价格触发并以隐含的限价指令的形式执行的指令,价格由price elimit给出
  • valid (default: None)

    Possible values:

    • None: this generates an order that will not expire (aka Good till cancel) and remain in the market until matched or canceled. In reality brokers tend to impose a temporal limit, but this is usually so far away in time to consider it as not expiring

    • 这将生成一个不会过期的订单(也就是在取消之前的好订单),并且在匹配或取消之前保持在市场中。在现实中,经纪人往往会设定一个时间限制,但这通常是如此遥远,以至于认为它不会到期
    • datetime.datetime or datetime.date instance: the date will be used to generate an order valid until the given datetime (aka good till date)

    • datetime.datetime或datetime.date实例:该日期将用于生成在给定日期之前有效的订单(也称为良好日期)
    • Order.DAY or 0 or timedelta(): a day valid until the End of the Session (aka day order) will be generated

    • Order.DAY或0或timedelta():生成会话结束前的有效日期(又名日顺序)
    • numeric value: This is assumed to be a value corresponding to a datetime in matplotlib coding (the one used by backtrader) and will used to generate an order valid until that time (good till date)

    • 数值:假设该值对应matplotlib编码中的日期时间(backtrader使用的日期),并将用于生成在该日期之前有效的订单(最佳日期)
  • tradeid (default: 0)

    This is an internal value applied by backtrader to keep track of overlapping trades on the same asset. This tradeid is sent back to the strategy when notifying changes to the status of the orders.

  • 这是backtrader应用的一个内部值,用于跟踪同一资产上的重叠交易。当通知订单状态的更改时,这个tradeid被发送回策略。
  • **kwargs: additional broker implementations may support extra parameters. backtrader will pass the kwargs down to the created order objects

  • **kwargs:其他代理实现可能支持额外的参数。backtrader将把kwarg传递到创建的order对象
  •  

    Example: if the 4 order execution types directly supported by backtrader are not enough, in the case of for example Interactive Brokers the following could be passed as kwargs:

  • 示例:如果backtrader直接支持的4种订单执行类型还不够,在交互式代理的情况下,可以将以下内容作为kwargs传递:
  • orderType='LIT', lmtPrice=10.0, auxPrice=9.8
    

    This would override the settings created by backtrader and generate a LIMIT IF TOUCHED order with a touched price of 9.8 and a limit price of 10.0.

  • 这将覆盖backtrader创建的设置,并生成一个触及价格为9.8、限价为10.0的限价单。

Note

The close method will examine the current position and correspondingly use buy or sell to effectively close the position. size will also be automatically calculated unless the parameter is an input from the user, in which case a partial close or a reversal can be achieved

平仓方法将检查当前头寸,并相应地使用买入或卖出来有效地平仓。尺寸也将自动计算,除非参数是用户的输入,在这种情况下可以实现部分关闭或反转

 

Order notification

订单通知

To receive notifications the notify_order method has to be overriden in the user subclassed Strategy (the default behavior is to do nothing). The following applies to those notifications:

为了接收通知,notify_order方法必须在user子类策略中被重写(默认行为是什么都不做)。以下内容适用于这些通知:

  • Issued before the strategy’s next method is called

  • 在调用策略的下一个方法之前发出
  • May (and will) happen several times for the same order with the same or different status during the same next cycle.

  • 一些相同的订单有着相同或不同的状态,将在下一个周期发生多次
  •  

    An order may be submitted to the broker and be accepted and its execution completed before next will be invoked again.

  • 一个订单可以提交给broker并被接受,并且在下次调用next之前完成它的执行。
  •  

    In this case at least 3 notifications will happen with the following status values:
    在这种情况下,至少会发生3个通知,状态值如下:

    • Order.Submitted because the order was sent to the broker

    • 订单提交 因为订单已经被发送给经济人
    • Order.Accepted because the order was taken by the broker and awaits potential execution

    • 订单接收 因为订单已经由经纪人接手,等待下一步执行
    • Order.Completed because in the example it was quickly matched and completely filled (which may be the case usually for Market orders)

    • 订单完成 因为在这个例子中,它很快就被匹配并完全完成了交易(市场订单通常就是这样)

Notifications may happen even several times for the same status in the case of Order.Partial. This status will not be seen in the backtesting broker (which doesn’t consider volume when matching) but it will for sure be set by real brokers.

对于Order.Partial,相同状态的通知可能会发生几次【可能为部分成交】。这个状态在回测中惊人是看不到的(匹配时不考虑容量),但是它肯定由真实的经纪人设置。

Real brokers may issue one or more executions before updating a position, and this group of executions will make up for an Order.Partial notification.

实际的代理可能会在更新位置之前执行一次或多次,这组执行将弥补Order.Partiald的通知。

Actual execution data is in the attribute: order.executed which is an object of type OrderData (see below for the reference), with usual fields as size and price

实际执行中数据是属性:order.executed他是一个OrderData的对象(参见下面的引用),通常包含大小与价格字段。

The values at the time of creation are stored in order.created which remains unchanged throughout the lifecycle of an order

创建时的值存储在订单。已创建在订单的整个生命周期中保持不变

 

Order Status values

The following are defined:

  • Order.Created: set when the Order instance is created. Never to be seen by end-users unless order instances are manually created rather than through buy, sell and close

  • Order.Submitted: set when the order instance has been transmitted to the broker. This simply means it has been sent. In backtesting mode this will be an immediate action, but it may take actual time with a real broker, which may receive the order and only first notify when it has been forwarded to an exchange

  • Order.Accepted: the broker has taken the order and it is in the system (or already in a exchange) awaiting execution according to the set parameters like execution type, size, price and validity

  • Order.Partial: the order has been partially executed. order.executed contains the current filled size and average price.

    order.executed.exbits contains a complete list of ExecutionBits detailing the partial fillings

  • Order.Complete: the order has been completely filled average price.

  • Order.Rejected: the broker has rejected the order. A parameter (like for example valid to determine its lifetime) may not be accepted by the broker and the order cannot be accepted.

    The reason will be notified via the notify_store method of the strategy. Although this may seem awkward, the reason is that real life brokers will notify this over an event, which may or may not be direclty related to the order. But the notification from the broker can still be seen in notify_store.

    This status will not be seen in the backtesting broker

  • Order.Margin: the order execution would imply a margin call and the previously accepted order has been taken off the system

  • Order.Cancelled (or Order.Canceled): confirmation of the user requested cancellation

    It must be taken into account that a request to cancel an order via the cancel method of the strategy is no guarantee of cancellation. The order may have been already executed but such execution may not have yet notified by the broker and/or the notification may not have yet been delivered to the strategy

  • Order.Expired: a previously accepted order which had a time validity has expired and been taken off the system

 

 

 

 

 

posted @ 2020-09-14 23:33  就是想学习  阅读(1426)  评论(0编辑  收藏  举报