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

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM