1. 程式人生 > 實用技巧 >Backtrader中文筆記之Slippage(滑點)

Backtrader中文筆記之Slippage(滑點)

backtesting cannot guarantee real market conditions. No matter how good the market simulation is, under real market conditions slippage can happen. That means:

回測系統不能保證真實的市場狀況。無論市場模擬有多好,在真實的市場條件下,都可能出現下滑。這意味著:

  • The requested price may not be matched.
  • 請求的價格可能不匹配

The integrated backtesting broker supports slippage. The following parameters can be passed to the broker

完整的回測券商【經紀人】支援滑點。可以將以下引數傳遞給券商【經紀人】

  • slip_perc (default: 0.0) Percentage in absolute termns (and positive) that should be used to slip prices up/down for buy/sell orders

  • slip_perc(預設值:0.0)絕對條件下的百分比(和正值,用於向上/向下滑動買入/賣出訂單的價格
  • Note:

    • 0.01 is 1%

    • 0.001 is 0.1%

  • slip_fixed (default: 0.0) Percentage in units (and positive) that should be used to slip prices up/down for buy/sell orders

    Note: if slip_perc is non zero, it takes precendence over this.

  • slip_open (default: False) whether to slip prices for order execution which would specifically used the opening price of the next bar. An example would be Market order which is executed with the next available tick, i.e: the opening price of the bar.

    This also applies to some of the other executions, because the logic tries to detect if the opening price would match the requested price/execution type when moving to a new bar.

  • slip_match (default: True)

    If True the broker will offer a match by capping slippage at high/low prices in case they would be exceeded.

    If False the broker will not match the order with the current prices and will try execution during the next iteration

  • slip_limit (default: True)

    Limit orders, given the exact match price requested, will be matched even if slip_match is False.

    This option controls that behavior.

    If True, then Limit orders will be matched by capping prices to the limit / high/low prices

    If False and slippage exceeds the cap, then there will be no match

  • slip_out (default: False)

    Provide slippage even if the price falls outside the high - low range.

How it works

如何工作

In order to decide when to apply slippage the order execution type is taken into account:

為了決定何時應用滑移,需要考慮訂單執行型別:
  • Close - No slippage is applied

  • 收盤-不支援引用滑點
  • This order is matched against the close price and this price is the last one of the day. Slippage cannot happen because the order can only happen with the last tick of the session and this is a unique price with no tolerance.

  • 此訂單與收盤價相匹配,此價格為當天最後一個價格。滑點是不可能發生的,因為訂單隻能在交易的最後一刻發生,而且這是一個沒有任何容差的獨特價格。
  • Market - Slippage is applied

    Please check the slip_open exception. Because Market orders will be matched against the opening price of the next bar.

  • Limit - Slippage is applied following this logic

    • If the matching price would be the opening price, then slippage is applied according to the parameter slip_open. If applied, the price will never be worse than the requested limit price

    • If the matching price is not the limit price, slippage is applied capping at high/low. In this case slip_mlimit applies to decide if a match will be happening in case the caps are exceeded

    • If the matching price is the limit price, then no slippage is applied

  • Stop - once the order is triggered the same logic as for Market orders apply

  • StopLimit - once the order is triggered the same logic as for Limit orders apply

This approach tries to offer the most realistic possible approach within the limits of the simulation and available data