금융공학/BackTrader 공부

BackTrader 6 : Orders

냥냥펀치데스 2021. 12. 30. 20:33
728x90

Orders

더보기
  • Orders translate the decisions made by the logic in a Strategy into a message suitable for the Broker to execute an action - strategy <-> broker 에서 order 중요
  •  This is done with:
      • Creation
        • buy,sell
        • close(Strategy) ->  return order instance
      • Cancellation
        • cancel(Strategy) -> operate order instance
      • Notification
        • notify_order (Strategy) -> report order instance

Order creation

Strategy param 과 유사

더보기
  • data (default: None
  • size (default: None)
  • price (default: None)
  • plimit (default: None)
  • exectype (default: None)
  • valid (default: None)
  • tradeid (default: 0)
  • **kwargs

Order notification

더보기

receive notifications the notify_order, 3 notifications will happen with the following status values

  • 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)
  • Order.Partial 
    • Notifications may happen even several times for the same status
    • Real brokers may issue one or more executions before updating a position

Order Status values

더보기
  • Order.Created : 
    • set when the Order instance is created.
  • Order.Submitted :
    • set when the order instance has been transmitted to the broker
    • bactesting 모드에서는 한번에, live 모드에서는 exchange 까지 시간걸림
  • Order.Accepted :
    • the broker has taken the order and it is in the system (or already in a exchange) awaiting execution
  • Order.Partial :
    • the order has been partially executed
    • partial 별 정도 -> order.executed contains the current filled size and average price.
    • detail -> 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.
  • Order.Margin :
    • the order execution would imply a margin call 
    • the previously accepted order has been taken off the system
  • Order.Cancelled (or Order.Canceled) :
    • confirmation of the user requested cancellation
    • 하지만 주문이 이미 실행되어있을수도 있음으로 장담 못함
  • Order.Expired : 
    •  previously accepted order which had a time validity has expired
    • been taken off the system

Reference: Order and associated classes

class backtrader.order.Order()

더보기

The order may have the following status:

  • Submitted: sent to the broker and awaiting confirmation
  • Accepted: accepted by the broker
  • Partial: partially executed
  • Completed: fully exexcuted
  • Canceled/Cancelled: canceled by the user
  • Expired: expired
  • Margin: not enough cash to execute the order.
  • Rejected: Rejected by the broker

Member Attributes:

  • ref: unique order identifier
  • created: OrderData holding creation data
  • executed: OrderData holding execution data
  • info: custom information passed over method addinfo().

User Methods:

  • isbuy(): returns bool indicating if the order buys
  • issell(): returns bool indicating if the order sells
  • alive(): returns bool if order is in status Partial or Accepted

class backtrader.order.OrderData(dt=None, size=0, price=0.0, pricelimit=0.0, remsize=0, pclose=0.0, trailamount=0.0, trailpercent=0.0)

더보기

Holds actual order data for Creation and Executio

 

Member Attributes:

  • exbits : iterable of OrderExecutionBits for this OrderData
  • dt: datetime (float) creation/execution time
  • size: requested/executed size
  • price: execution price Note: if no price is given and no pricelimite is given, the closing price at the time or order creation will be used as reference
  • pricelimit: holds pricelimit for StopLimit (which has trigger first)
  • trailamount: absolute price distance in trailing stops
  • trailpercent: percentage price distance in trailing stops
  • value: market value for the entire bit size
  • comm: commission for the entire bit execution
  • pnl: pnl generated by this bit (if something was closed)
  • margin: margin incurred by the Order (if any)
  • psize: current open position size
  • pprice: current open position price

class backtrader.order.OrderExecutionBit(dt=None, size=0, price=0.0, closed=0, closedvalue=0.0, closedcomm=0.0, opened=0, openedvalue=0.0, openedcomm=0.0, pnl=0.0, psize=0, pprice=0.0)

더보기

Intended to hold information about order execution. A “bit” does not determine if the order has been fully/partially executed, it just holds information.

 

Member Attributes:

  • dt: datetime (float) execution time
  • size: how much was executed
  • price: execution price
  • closed: how much of the execution closed an existing postion
  • opened: how much of the execution opened a new position
  • openedvalue: market value of the “opened” part
  • closedvalue: market value of the “closed” part
  • closedcomm: commission for the “closed” part
  • openedcomm: commission for the “opened” part
  • value: market value for the entire bit size
  • comm: commission for the entire bit execution
  • pnl: pnl generated by this bit (if something was closed)
  • psize: current open position size
  • pprice: current open position price