提交 cd45f7d2 编写于 作者: 绝不原创的飞龙's avatar 绝不原创的飞龙

2024-04-12 18:55:12

上级 49dabdad
......@@ -3,26 +3,31 @@
prefs:
- PREF_H1
type: TYPE_NORMAL
zh: 期货和现货补偿
- en: 原文:[https://www.backtrader.com/docu/order-creation-execution/futurespot/future-vs-spot/](https://www.backtrader.com/docu/order-creation-execution/futurespot/future-vs-spot/)
id: totrans-1
prefs:
- PREF_BQ
type: TYPE_NORMAL
zh: 原文:[https://www.backtrader.com/docu/order-creation-execution/futurespot/future-vs-spot/](https://www.backtrader.com/docu/order-creation-execution/futurespot/future-vs-spot/)
- en: Release `1.9.32.116` adds support for an interesting use case presented in the
[Community](https://community.backtrader.com/)
id: totrans-2
prefs: []
type: TYPE_NORMAL
zh: 发布`1.9.32.116`添加了对在[社区](https://community.backtrader.com/)中提出的一个有趣用例的支持。
- en: Start a trade with a future, which includes **physical delivery**
id: totrans-3
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 通过未来开始交易,其中包括**实物交割**
- en: Have an indicator tell you something
id: totrans-4
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 让指标告诉你些什么。
- en: If needed be, close the position by operating on the spot price, effectively
canceling the physical delivery, be it for receiving the goods or for having to
deliver them (and hopefully making a profit)
......@@ -30,37 +35,44 @@
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 如果需要,通过对现货价格进行操作来关闭头寸,有效地取消物理交割,无论是为了收到货物还是为了交付货物(并有望获利)。
- en: The future expires on the same day the operation on the spot price takes place
id: totrans-6
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 未来到期日与现货价格操作在同一天发生。
- en: 'That means:'
id: totrans-7
prefs: []
type: TYPE_NORMAL
zh: 这意味着:
- en: The platform is fed with data points from two different assets
id: totrans-8
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 平台从两种不同的资产中获取数据点。
- en: The platform has to somehow understand the assets are related and that operations
on the *spot* price will close positions open on the *future*
id: totrans-9
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 平台必须以某种方式理解资产之间的关系,并且操作*现货*价格将关闭在*期货*上开放的头寸。
- en: In reality, the future is not closed, only the physical delivery is *compensated*
id: totrans-10
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 实际上,未来并没有关闭,只有物理交割是*补偿*的。
- en: Using that *compensation* concept, `backtrader` adds a way to let the user communicate
to the platform that things on one data feed will have compensating effects on
another. The usage pattern
id: totrans-11
prefs: []
type: TYPE_NORMAL
zh: 使用*补偿*概念,`backtrader`添加了一种让用户向平台传达在一个数据源上的事情将对另一个数据源产生补偿效果的方法。使用模式
- en: '[PRE0]'
id: totrans-12
prefs: []
......@@ -71,17 +83,20 @@
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 将所有内容放在一起
- en: An example is always worth a thousand posts, so let’s put all the pieces together
for it.
id: totrans-14
prefs: []
type: TYPE_NORMAL
zh: 例子总是值得一千篇文章,所以让我们把所有的东西放在一起。
- en: Use one of the standard sample feeds from the `backtrader` sources. This will
be the future
id: totrans-15
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 使用`backtrader`源中的标准示例数据之一。这将是未来的。
- en: 'Simulate a similar but distinct price, by reusing the same feed and adding
a filter which will randomly move the price some points above/below, to create
a spread. As simple as:'
......@@ -89,6 +104,7 @@
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 通过重复使用相同的数据源并添加一个过滤器,该过滤器将随机将价格移动到上/下几个点,以创建一个价差,来模拟类似但不同的价格。如下所示:
- en: '[PRE1]'
id: totrans-17
prefs:
......@@ -102,21 +118,25 @@
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 将绘图到相同的轴上会混合默认包含的`BuyObserver`标记,因此标准观察器将被禁用,并手动重新添加以使用不同的每个数据标记进行绘图。
- en: Positions will be entered randomly and exited 10 days later
id: totrans-19
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 位置将随机输入,并在10天后退出。
- en: This doesn’t match future expiration periods, but this is just putting the functionality
in place and not checking a trading calendar
id: totrans-20
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 这与期货到期期限不符,但这只是为了实现功能,而不是检查交易日历。
- en: '!!! note'
id: totrans-21
prefs: []
type: TYPE_NORMAL
zh: '!!! 注意'
- en: '[PRE2]'
id: totrans-22
prefs: []
......@@ -127,18 +147,21 @@
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 注意策略
- en: '`buy` operations are executed on `data0`'
id: totrans-24
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: '`buy`操作在`data0`上执行。'
- en: '`sell` operations are executed on `data1`'
id: totrans-25
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: '`sell`操作在`data1`上执行。'
- en: '[PRE3]'
id: totrans-26
prefs:
......@@ -149,6 +172,7 @@
id: totrans-27
prefs: []
type: TYPE_NORMAL
zh: 执行:
- en: '[PRE4]'
id: totrans-28
prefs: []
......@@ -158,26 +182,31 @@
id: totrans-29
prefs: []
type: TYPE_NORMAL
zh: 使用此图形输出。
- en: '![image](../Images/a6204d4e6c557f947ffc6c19bf64c75b.png)'
id: totrans-30
prefs: []
type: TYPE_IMG
zh: '![image](../Images/a6204d4e6c557f947ffc6c19bf64c75b.png)'
- en: 'And it works:'
id: totrans-31
prefs: []
type: TYPE_NORMAL
zh: 它是有效的:
- en: '`buy` operations are signaled with a green triangle pointing upwards and the
legend tells us they belong to `data0` as expected'
id: totrans-32
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`buy`操作以指向上的绿色三角形的形式发出信号,图例告诉我们它们属于`data0`,如预期的那样。'
- en: '`sell` operations are signaled with an arrow pointing downwards and the legend
tells us they belong to `data1` as expected'
id: totrans-33
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`sell`操作以向下箭头的形式发出信号,图例告诉我们它们属于`data1`,如预期的那样。'
- en: Trades are being closed, even if they are being open with `data0` and being
closed with `data1`, achieving the desired effect (which in real life is avoiding
the physical delivery of the goods acquired by means of the *future*)
......@@ -185,11 +214,13 @@
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 交易正在关闭,即使它们是以`data0`开仓并以`data1`平仓,也能实现期望的效果(在现实生活中,这意味着避免通过*期货*获得的商品的实物交割)。
- en: 'One could only imagine what would happen if the same logic is applied without
the *compensation* taking place. Let’s do it:'
id: totrans-35
prefs: []
type: TYPE_NORMAL
zh: 如果没有*补偿*,人们可能会想象会发生什么。让我们来做一下:
- en: '[PRE5]'
id: totrans-36
prefs: []
......@@ -199,14 +230,17 @@
id: totrans-37
prefs: []
type: TYPE_NORMAL
zh: 输出结果
- en: '![image](../Images/7c47768a70258cb8ad6b6954d6621046.png)'
id: totrans-38
prefs: []
type: TYPE_IMG
zh: '![image](../Images/7c47768a70258cb8ad6b6954d6621046.png)'
- en: 'It should be quite obvious that this fails miserably:'
id: totrans-39
prefs: []
type: TYPE_NORMAL
zh: 很明显,这是失败的:
- en: The logic expects positions on `data0` to be closed by the operations on `data1`
and to only open positions on `data0` when not in the market
id: totrans-40
......
- en: StopTrail(Limit)
id: totrans-0
prefs:
- PREF_H1
type: TYPE_NORMAL
zh: StopTrail(Limit)
- en: 原文:[https://www.backtrader.com/docu/order-creation-execution/trail/stoptrail/](https://www.backtrader.com/docu/order-creation-execution/trail/stoptrail/)
id: totrans-1
prefs:
- PREF_BQ
type: TYPE_NORMAL
zh: 原文:[https://www.backtrader.com/docu/order-creation-execution/trail/stoptrail/](https://www.backtrader.com/docu/order-creation-execution/trail/stoptrail/)
- en: Release `1.9.35.116` adds the `StopTrail` and `StopTrailLimit` order execution
types to the backtesting arsenal.
id: totrans-2
prefs: []
type: TYPE_NORMAL
zh: 版本`1.9.35.116`将`StopTrail`和`StopTrailLimit`订单执行类型添加到回测武器库。
- en: Note
id: totrans-3
prefs: []
type: TYPE_NORMAL
zh: 注意
- en: This is only implemented in backtesting and there isn’t yet an implementation
for live brokers
id: totrans-4
prefs: []
type: TYPE_NORMAL
zh: 这仅在回测中实现,尚未针对实时经纪人实现
- en: Note
id: totrans-5
prefs: []
type: TYPE_NORMAL
zh: 注意
- en: Updated with release `1.9.36.116`. Interactive Brokers support for `StopTrail`,
`StopTrailLimit` and `OCO`.
id: totrans-6
prefs: []
type: TYPE_NORMAL
zh: 用版本`1.9.36.116`更新。交互式经纪人支持`StopTrail`、`StopTrailLimit`和`OCO`。
- en: '`OCO` Specify always the 1^(st) order in a group as parameter `oco`'
id: totrans-7
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`OCO`总是将第1个订单作为参数`oco`指定为一组'
- en: '`StopTrailLimit`: the broker simulation and the `IB` broker have the same behavior.
Specify: `price` as the initial stop trigger price (specify also `trailamount`)
and then `plimi` as the initial limit price. The difference between the two will
determine the `limitoffset` (the distance at which the limit price remains from
the stop trigger price)'
id: totrans-8
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`StopTrailLimit`:经纪人模拟和`IB`经纪人具有相同的行为。指定:`price`作为初始触发价格(也指定`trailamount`),然后`plimi`作为初始限价。两者之间的差异将确定`limitoffset`(限价与停止触发价格之间的距离)'
- en: 'The usage pattern is fully integrated into the standard `buy`, `sell` and `close`
market operation methods of the strategy instances. To notice:'
id: totrans-9
prefs: []
type: TYPE_NORMAL
zh: 用法模式完全集成到策略实例的标准`buy`、`sell`和`close`市场操作方法中。注意:
- en: Indicate which execution type is wished as in `exectype=bt.Order.StopTrail`
id: totrans-10
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 指示希望的执行类型,如`exectype=bt.Order.StopTrail`
- en: And whether the trailing price must be calculated with a fixed distance or with
a percentage based distance
id: totrans-11
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 以及是否必须使用固定距离或百分比距离计算跟踪价格
- en: 'Fixed distance: `trailamount=10`'
id: totrans-12
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 固定距离:`trailamount=10`
- en: 'Percentage based distance: `trailpercent=0.02` (i.e.: `2%`)'
id: totrans-13
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 百分比距离:`trailpercent=0.02`(即:`2%`)
- en: 'If one has entered the market long by issuing a `buy`, this is what a `sell`
with `StopTrail` and `trailamount` does:'
id: totrans-14
prefs: []
type: TYPE_NORMAL
zh: 如果通过发出`buy`进入市场,这是`sell`与`StopTrail`和`trailamount`一起的操作:
- en: If no `price` is specified, the latest `close` price is used
id: totrans-15
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 如果未指定`price`,则使用最新的`close`价格
- en: '`trailamount` is substracted from the price to find the `stop` (or trigger)
price'
id: totrans-16
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`trailamount`从价格中减去以找到`stop`(或触发)价格'
- en: The next iteration of the *broker* checks if the trigger price has been reached
id: totrans-17
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 下一个迭代的*经纪人*检查触发价格是否已达到
- en: 'If **Yes**: the order is executed with a `Market` execution type approach'
id: totrans-18
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果**是**:订单以`Market`执行类型的方法执行
- en: If **No**, the `stop` price is recalculated by using the latest `close` price
and substracting the `trailamount` distance
id: totrans-19
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果**否**,则使用最新的`close`价格减去`trailamount`距离重新计算`stop`价格
- en: If the new price goes up, it is updated
id: totrans-20
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果新价格上涨,则更新
- en: If the new price would go down (or not change at all), it is discarded
id: totrans-21
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果新价格将下跌(或根本不变),则将其丢弃
- en: 'That is: the trailing stop price follows the price upwards, but remains fixed
if the prices start falling, to potentially secure a profit.'
id: totrans-22
prefs: []
type: TYPE_NORMAL
zh: 也就是说:跟踪止损价格随价格上涨而上涨,但如果价格开始下跌,则保持不变,以潜在地获利。
- en: 'If one had entered the market with a `sell`, then issuing a `buy` order with
`StopTrail` simply does the opposite, i.e.: prices are followed downwards.'
id: totrans-23
prefs: []
type: TYPE_NORMAL
zh: 如果通过`sell`进入市场,然后通过`StopTrail`发出`buy`订单只是做相反的操作,即:价格向下跟随。
- en: Some usage patterns
id: totrans-24
prefs: []
type: TYPE_NORMAL
zh: 一些用法模式
- en: '[PRE0]'
id: totrans-25
prefs: []
type: TYPE_PRE
zh: '[PRE0]'
- en: One can also specify `trailpercent` instead of `trailamount` and the distance
to the price will be calculated as a percentage of the price
id: totrans-26
prefs: []
type: TYPE_NORMAL
zh: 也可以指定`trailpercent`而不是`trailamount`,价格与价格的距离将被计算为价格的百分比
- en: '[PRE1]'
id: totrans-27
prefs: []
type: TYPE_PRE
zh: '[PRE1]'
- en: For a `StopTrailLimit`
id: totrans-28
prefs: []
type: TYPE_NORMAL
zh: 对于`StopTrailLimit`
- en: The only difference is what happens when the trailing stop price is triggered.
id: totrans-29
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 唯一的区别在于当触发跟踪止损价格时会发生什么。
- en: In this case the order is executed as a `Limit` order (the same behavior a `StopLimit`
order has, but in this case with a dynamic triggering price)
id: totrans-30
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 在这种情况下,订单以`Limit`订单的形式执行(与`StopLimit`订单的行为相同,但在这种情况下,触发价格是动态的)
- en: '**NOTE**: one has to specify `plimit=x.x` to `buy` or `sell`, which will be
the *limit* price'
id: totrans-31
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: '**注意**:必须指定`plimit=x.x`给`buy`或`sell`,这将是*限价*'
- en: '**NOTE**: the *limit* price is not changed dynamically like the stop/trigger
price'
id: totrans-32
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: '**注意**:*限价*不像停止/触发价格那样动态更改'
- en: An example is always worth a thousand words and hence the usual *backtrader*
sample, which
id: totrans-33
prefs: []
type: TYPE_NORMAL
zh: 例如总是值得一看,因此通常的*backtrader*示例,其中
- en: Uses a moving average crossing up to enter the market long
id: totrans-34
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 使用移动平均线上穿进入市场多头
- en: Uses a trailing stop to exit the market
id: totrans-35
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 使用跟踪止损退出市场
- en: The execution with `50` points of fixed price distance
id: totrans-36
prefs: []
type: TYPE_NORMAL
zh: 使用`50`点固定价格距离的执行
- en: '[PRE2]'
id: totrans-37
prefs: []
type: TYPE_PRE
zh: '[PRE2]'
- en: Which produces the following chart
id: totrans-38
prefs: []
type: TYPE_NORMAL
zh: 这产生了以下图表
- en: '![image](../Images/551fb950e5b5964370cf4a2c884e6a9f.png)'
id: totrans-39
prefs: []
type: TYPE_IMG
zh: '![image](../Images/551fb950e5b5964370cf4a2c884e6a9f.png)'
- en: 'And the following output:'
id: totrans-40
prefs: []
type: TYPE_NORMAL
zh: 和以下输出:
- en: '[PRE3]'
id: totrans-41
prefs: []
type: TYPE_PRE
zh: '[PRE3]'
- en: Rather than waiting for the usual cross down pattern the system uses the trailing
stop to exit the market. Let’s see the 1^(st) operation for example
id: totrans-42
prefs: []
type: TYPE_NORMAL
zh: 而不是等待通常的下穿模式,系统使用跟踪止损退出市场。例如,让我们看看第一次操作。
- en: 'Closing price when entering long: `3075.76`'
id: totrans-43
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 进入多头时的收盘价:`3075.76`
- en: 'System calculated trail stop price: `3025.76` (which is `50` units away)'
id: totrans-44
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 系统计算的跟踪止损价:`3025.76`(相距`50`个单位)
- en: 'Sample calculated trail stop price: `3025.76` (last price shown in each line)'
id: totrans-45
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 样本计算的跟踪止损价:`3025.76`(每行显示的最后价格)
- en: 'After this first calculation:'
id: totrans-46
prefs: []
type: TYPE_NORMAL
zh: 在第一次计算之后:
- en: The closing price goes up to `3086.95` and the stop price is adjusted to `3036.95`
id: totrans-47
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 收盘价上涨至`3086.95`,止损价调整为`3036.95`
- en: The following closing prices don’t exceed `3086.95` and the trigger price doesn’t
change
id: totrans-48
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 以下收盘价不超过`3086.95`,触发价格不变
- en: The same pattern can be seen in the other 2 operations.
id: totrans-49
prefs: []
type: TYPE_NORMAL
zh: 在其他两次操作中也可以看到相同的模式。
- en: For the sake of comparison, an execution with just `30` points of fixed distance
(just the chart)
id: totrans-50
prefs: []
type: TYPE_NORMAL
zh: 为了比较,仅使用`30`点固定距离的执行(仅图表)
- en: '[PRE4]'
id: totrans-51
prefs: []
type: TYPE_PRE
zh: '[PRE4]'
- en: And the chart
id: totrans-52
prefs: []
type: TYPE_NORMAL
zh: 和图表
- en: '![image](../Images/700eceb9bc1fb34867831b2eee88bc64.png)'
id: totrans-53
prefs: []
type: TYPE_IMG
zh: '![image](../Images/700eceb9bc1fb34867831b2eee88bc64.png)'
- en: Followed by one last execution with `trailpercent=0.02`
id: totrans-54
prefs: []
type: TYPE_NORMAL
zh: 最后一次执行后跟随`trailpercent=0.02`
- en: '[PRE5]'
id: totrans-55
prefs: []
type: TYPE_PRE
zh: '[PRE5]'
- en: The corresponding chart.
id: totrans-56
prefs: []
type: TYPE_NORMAL
zh: 相应的图表。
- en: '![image](../Images/1d0cd296d8c7430931eacd19a3b2e23e.png)'
id: totrans-57
prefs: []
type: TYPE_IMG
zh: '![image](../Images/1d0cd296d8c7430931eacd19a3b2e23e.png)'
- en: The sample usage
id: totrans-58
prefs: []
type: TYPE_NORMAL
zh: 示例用法
- en: '[PRE6]'
id: totrans-59
prefs: []
type: TYPE_PRE
zh: '[PRE6]'
- en: The sample code
id: totrans-60
prefs: []
type: TYPE_NORMAL
zh: 示例代码
- en: '[PRE7]'
id: totrans-61
prefs: []
type: TYPE_PRE
zh: '[PRE7]'
- en: Broker
id: totrans-0
prefs:
- PREF_H1
type: TYPE_NORMAL
zh: 经纪人
此差异已折叠。
- en: Slippage
id: totrans-0
prefs:
- PREF_H1
type: TYPE_NORMAL
zh: 滑点
- en: 原文:[https://www.backtrader.com/docu/slippage/slippage/](https://www.backtrader.com/docu/slippage/slippage/)
id: totrans-1
prefs:
- PREF_BQ
type: TYPE_NORMAL
zh: 原文:[https://www.backtrader.com/docu/slippage/slippage/](https://www.backtrader.com/docu/slippage/slippage/)
- en: 'backtesting cannot guarantee real market conditions. No matter how good the
market simulation is, under real market conditions slippage can happen. That means:'
id: totrans-2
prefs: []
type: TYPE_NORMAL
zh: 回测无法保证真实市场条件。无论市场模拟有多好,在真实市场条件下都可能发生滑点。这意味着:
- en: The requested price may not be matched.
id: totrans-3
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 请求的价格可能无法匹配。
- en: The integrated backtesting broker supports slippage. The following parameters
can be passed to the broker
id: totrans-4
prefs: []
type: TYPE_NORMAL
zh: 集成的回测经纪人支持滑点。以下参数可以传递给经纪人
- en: '`slip_perc` (default: `0.0`) Percentage in absolute termns (and positive) that
should be used to slip prices up/down for buy/sell orders'
id: totrans-5
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`slip_perc` (默认值: `0.0`) 绝对百分比(正数),用于上下滑动买入/卖出订单的价格'
- en: 'Note:'
id: totrans-6
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: '注意:'
- en: '`0.01` is `1%`'
id: totrans-7
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: '`0.01` `1%`'
- en: '`0.001` is `0.1%`'
id: totrans-8
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: '`0.001` `0.1%`'
- en: '`slip_fixed` (default: `0.0`) Percentage in units (and positive) that should
be used to slip prices up/down for buy/sell orders'
id: totrans-9
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`slip_fixed` (默认值: `0.0`) 单位百分比(正数),用于上下滑动买入/卖出订单的价格'
- en: 'Note: if `slip_perc` is non zero, it takes precendence over this.'
id: totrans-10
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 注意:如果`slip_perc`不为零,则它优先于此。
- en: '`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.'
id: totrans-11
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`slip_open` (默认值: `False`) 是否为订单执行滑动价格,该价格特别使用下一个柱的*开盘*价格。一个示例是使用下一个可用刻度执行的`Market`订单,即柱的开盘价格。'
- en: 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.
id: totrans-12
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 这也适用于其他一些执行,因为逻辑试图检测*开盘*价格是否会在移动到新柱时匹配请求的价格/执行类型。
- en: '`slip_match` (default: `True`)'
id: totrans-13
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`slip_match` (默认值: `True`)'
- en: If `True` the broker will offer a match by capping slippage at `high/low` prices
in case they would be exceeded.
id: totrans-14
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 如果为`True`,经纪人将通过在超出时将滑点限制在`high/low`价格上来提供匹配。
- en: If `False` the broker will not match the order with the current prices and will
try execution during the next iteration
id: totrans-15
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 如果 `False`,经纪人将不会根据当前价格匹配订单,并将在下一次迭代中尝试执行
- en: '`slip_limit` (default: `True`)'
id: totrans-16
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`slip_limit` (默认值: `True`)'
- en: '`Limit` orders, given the exact match price requested, will be matched even
if `slip_match` is `False`.'
id: totrans-17
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 即使 `slip_match` 为 `False`,给定请求的精确匹配价格的`Limit`订单也会被匹配。
- en: This option controls that behavior.
id: totrans-18
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 此选项控制该行为。
- en: If `True`, then `Limit` orders will be matched by capping prices to the `limit`
/ `high/low` prices
id: totrans-19
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 如果为`True`,则`Limit`订单将通过将价格限制在`limit` / `high/low`价格上来匹配
- en: If `False` and slippage exceeds the cap, then there will be no match
id: totrans-20
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 如果 `False` 并且滑点超过上限,则不会匹配
- en: '`slip_out` (default: `False`)'
id: totrans-21
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`slip_out` (默认值: `False`)'
- en: Provide *slippage* even if the price falls outside the `high` - `low` range.
id: totrans-22
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 即使价格超出`high` - `low`范围,也提供*滑点*。
- en: How it works
id: totrans-23
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 工作原理
- en: 'In order to decide when to apply *slippage* the order execution type is taken
into account:'
id: totrans-24
prefs: []
type: TYPE_NORMAL
zh: 为了决定何时应用*滑点*,订单执行类型被考虑在内:
- en: '`Close` - **No slippage** is applied'
id: totrans-25
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`Close` - **不应用滑点**'
- en: 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.
id: totrans-26
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 此订单与`close`价格匹配,此价格是当天的最后一个价格。滑点不会发生,因为订单只能在会话的最后一个刻度发生,这是一个唯一的价格,没有容忍度。
- en: '`Market` - *Slippage* is applied'
id: totrans-27
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`Market` - *滑点*被应用'
- en: Please check the `slip_open` exception. Because `Market` orders will be matched
against the *opening* price of the next bar.
id: totrans-28
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 请检查`slip_open`异常。因为`Market`订单将与下一个柱的*开盘*价格匹配。
- en: '`Limit` - *Slippage* is applied following this logic'
id: totrans-29
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`Limit` - *滑点*按照这个逻辑应用'
- en: 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
id: totrans-30
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果匹配价格将是*开盘*价格,则根据参数 `slip_open` 应用*滑点*。如果应用,则价格永远不会比请求的`limit`价格更差
- en: 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
id: totrans-31
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果匹配价格不是`limit`价格,则应用*滑点*,并在`high/low`上限制。在这种情况下,`slip_mlimit`适用于决定是否在超出上限时会发生匹配
- en: If the matching price is the `limit` price, then no slippage is applied
id: totrans-32
prefs:
- PREF_IND
- PREF_UL
type: TYPE_NORMAL
zh: 如果匹配价格是`limit`价格,则不会应用滑点
- en: '`Stop` - once the order is *triggered* the same logic as for `Market` orders
apply'
id: totrans-33
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`Stop` - 一旦订单被*触发*,与`市价`订单相同的逻辑将适用'
- en: '`StopLimit` - once the order is *triggered* the same logic as for `Limit` orders
apply'
id: totrans-34
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`StopLimit` - 一旦订单被*触发*,与`限价`订单相同的逻辑将适用'
- en: This approach tries to offer the most realistic possible approach within the
limits of the simulation and available data
id: totrans-35
prefs: []
type: TYPE_NORMAL
zh: 该方法试图在模拟和可用数据的限制内提供尽可能逼真的方法
- en: Configuring slippage
id: totrans-36
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 配置滑点
- en: 'A *broker* is already instantiated by a *cerebro* engine for each run with
the default parameters. There are two ways to alter the behavior:'
id: totrans-37
prefs: []
type: TYPE_NORMAL
zh: '*经纪人*已经由每个运行的*智能交易引擎*实例化,具有默认参数。有两种方法可以更改其行为:'
- en: Use methods to configure *slippage*
id: totrans-38
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 使用方法来配置*滑点*
- en: BackBroker.set_slippage_perc(perc, slip_open=True, slip_limit=True, slip_match=True,
slip_out=False)
id: totrans-39
prefs:
- PREF_IND
- PREF_H4
type: TYPE_NORMAL
zh: BackBroker.set_slippage_perc(perc, slip_open=True, slip_limit=True, slip_match=True,
slip_out=False)
- en: Configure slippage to be percentage based
id: totrans-40
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 配置滑点为基于百分比的
- en: BackBroker.set_slippage_fixed(fixed, slip_open=True, slip_limit=True, slip_match=True,
slip_out=False)
id: totrans-41
prefs:
- PREF_IND
- PREF_H4
type: TYPE_NORMAL
zh: BackBroker.set_slippage_fixed(fixed, slip_open=True, slip_limit=True, slip_match=True,
slip_out=False)
- en: Configure slippage to be fixed points based
id: totrans-42
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 配置滑点为固定点位基础
- en: 'Replace the broker as in:'
id: totrans-43
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 将经纪人替换为:
- en: '[PRE0]'
id: totrans-44
prefs:
- PREF_IND
type: TYPE_PRE
zh: '[PRE0]'
- en: Practical examples
id: totrans-45
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 实际示例
- en: The sources contain a sample which uses the order execution type `Market` and
a *long/short* approach using *signals*. This should allow to understand the logic.
id: totrans-46
prefs: []
type: TYPE_NORMAL
zh: 源代码包含一个示例,该示例使用了执行类型为`市价`的订单以及使用*信号*的*多头/空头*方法。这应该能帮助理解其逻辑。
- en: 'A run with no slippage and an initial plot for reference later:'
id: totrans-47
prefs: []
type: TYPE_NORMAL
zh: 一个没有滑点并且具有初始图表供以后参考的运行:
- en: '[PRE1]'
id: totrans-48
prefs: []
type: TYPE_PRE
zh: '[PRE1]'
- en: '![image](../Images/308d5e797cb3b9b08716db17ff65f3ab.png)'
id: totrans-49
prefs: []
type: TYPE_IMG
zh: '![图片](../Images/308d5e797cb3b9b08716db17ff65f3ab.png)'
- en: 'And the same run using *slippage* with a `1.5%` configured:'
id: totrans-50
prefs: []
type: TYPE_NORMAL
zh: 同样的运行使用了配置为`1.5%`的*滑点*:
- en: '[PRE2]'
id: totrans-51
prefs: []
type: TYPE_PRE
zh: '[PRE2]'
- en: There is **NO CHANGE**. This is the expected behavior for the scenario.
id: totrans-52
prefs: []
type: TYPE_NORMAL
zh: 没有**变动**。这是该场景的预期行为。
- en: 'Execution Type: `Market`'
id: totrans-53
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 执行类型:`市价`
- en: And `slip_open` has not been set to `True`
id: totrans-54
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 而`slip_open`没有被设置为`True`
- en: The `Market` orders are matched against the *opening* price of the next bar
and we are not allowing the `open` price to be moved.
id: totrans-55
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: '`市价`订单与下一根柱的*开盘*价格匹配,我们不允许`open`价格被移动。'
- en: 'A run setting `slip_open` to `True`:'
id: totrans-56
prefs: []
type: TYPE_NORMAL
zh: 将`slip_open`设置为`True`的一个运行设置:
- en: '[PRE3]'
id: totrans-57
prefs: []
type: TYPE_PRE
zh: '[PRE3]'
- en: And one can immediately see tht the prices **HAVE MOVED**. And the allocated
prices are worst or equal like for operation 35\. *This is not a copy and paste
error*
id: totrans-58
prefs: []
type: TYPE_NORMAL
zh: 人们可以立即看到价格**已经变动**。并且分配的价格比操作35的最坏或相等。*这不是复制粘贴错误*
- en: The `open` and the `high` on 20016-12-19 were the same.
id: totrans-59
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 2016-12-19的`open`和`high`是相同的。
- en: The price cannot be pushed above the `high` because that would mean returning
a non-existent price.
id: totrans-60
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 价格不能被推高到`high`之上,因为那将意味着返回一个不存在的价格。
- en: 'Of course, *backtrader* allows to match outide the `high` - `low` range if
wished with `slip_out`. A run with it activated:'
id: totrans-61
prefs: []
type: TYPE_NORMAL
zh: 当然,*backtrader*允许匹配超出`high`-`low`范围的价格,如果愿意的话,可以激活`slip_out`进行运行:
- en: '[PRE4]'
id: totrans-62
prefs: []
type: TYPE_PRE
zh: '[PRE4]'
- en: 'A matching expression for the matched prices would be: OMG! (Oh My God!). The
prices are clearly outside of the range. Suffice to look at operation 35, which
has been matched at `4182.83`. A quick inspection of the chart in this document
shows that the asset never came close to that price.'
id: totrans-63
prefs: []
type: TYPE_NORMAL
zh: 用于匹配的匹配表达式将是:哦天呐!(我的天啊!)。价格明显超出了范围。只需看看操作35,该操作在`4182.83`处匹配。快速检查本文档中的图表可以看到该资产从未接近过该价格。
- en: '`slip_match` has a default of `True` and that means that *backtrader* offers
a match, be it with capped or uncapped prices as seen above. Let’s disable it:'
id: totrans-64
prefs: []
type: TYPE_NORMAL
zh: '`slip_match`的默认值为`True`,这意味着*backtrader*提供了一个匹配,无论是带上限价还是不带上限价,如上所示。让我们将其禁用:'
- en: '[PRE5]'
id: totrans-65
prefs: []
type: TYPE_PRE
zh: '[PRE5]'
- en: 'Blistering barnacles! Down to 13 from 35\. The rationale:'
id: totrans-66
prefs: []
type: TYPE_NORMAL
zh: 天啊!从35降到了13。原因是:
- en: Deactivating `slip_match` disallows matching operations if *slippage* would
push the matching price above the `high` or below the `low` of the bar. It seems
that with the `1.5%` of requested *slippage*, around 22 of the operations fail
to be executed.
id: totrans-67
prefs: []
type: TYPE_NORMAL
zh: 停用`slip_match`将阻止匹配操作,如果*滑点*将匹配价格推高到`high`之上或推低到柱的`low`之下。看起来,随着请求的*滑点*为`1.5%`,大约有22个操作无法执行。
- en: The examples should have shown how the different *slippage* options work together.
id: totrans-68
prefs: []
type: TYPE_NORMAL
zh: 这些示例应该已经展示了不同的*滑点*选项是如何共同工作的。
- en: Cheat On Open
id: totrans-0
prefs:
- PREF_H1
type: TYPE_NORMAL
zh: 以开盘作弊
- en: 原文:[https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open/](https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open/)
id: totrans-1
prefs:
- PREF_BQ
type: TYPE_NORMAL
zh: 原文:[https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open/](https://www.backtrader.com/docu/cerebro/cheat-on-open/cheat-on-open/)
- en: Release `1.9.44.116` adds support for `Cheat-On-Open`. This seems to be a demanded
feature for people who go *all-in*, having made a calculation after the close
of a bar, but expecting to be matched against the `open` price.
id: totrans-2
prefs: []
type: TYPE_NORMAL
zh: 版本 `1.9.44.116` 增加了对 `Cheat-On-Open` 的支持。这似乎是对那些*全情投入*的人的一项需求功能,他们在柱结束后做了计算,但期望与`open`价格匹配。
- en: Such a use case fails when the *opening* price gaps (up or down, depending on
whether `buy` or `sell` is in effect) and the cash is not enough for an *all-in*
operation. This forces the broker to reject the operation.
id: totrans-3
prefs: []
type: TYPE_NORMAL
zh: 当*开盘*价格出现差距(向上或向下,取决于`buy`或`sell`是否生效)并且现金不足以进行*全情投入*操作时,此用例会失败。这迫使经纪人拒绝操作。
- en: And although people can try to look into the future with a positive `[1]` index
approach, this requires preloading data which is not always available.
id: totrans-4
prefs: []
type: TYPE_NORMAL
zh: 虽然人们可以尝试用积极的`[1]`索引方法来展望未来,但这需要预加载数据,而这并不总是可用的。
- en: 'The pattern:'
id: totrans-5
prefs: []
type: TYPE_NORMAL
zh: 模式:
- en: '[PRE0]'
id: totrans-6
prefs: []
type: TYPE_PRE
zh: '[PRE0]'
- en: 'This:'
id: totrans-7
prefs: []
type: TYPE_NORMAL
zh: 这个:
- en: Activates an extra cycle in the system which calls the methods in the strategy
`next_open`, `nextstart_open` and `prenext_open`
id: totrans-8
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 在系统中激活了额外的周期,该周期调用策略中的`next_open`、`nextstart_open`和`prenext_open`方法。
- en: The decision to have an additional family of methods has been made to make a
clear separation between the regular methods which operate on the basis that the
prices being examined are no longer available and the future is unknown and the
operation in cheating mode.
id: totrans-9
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 决定增加一个额外的方法系列是为了清晰地区分常规方法和作弊模式之间的区别。常规方法是基于所检查的价格不再可用且未来未知的基础上运行的,而作弊模式则是另一种操作。
- en: This also avoids having 2 calls to the regular `next` method.
id: totrans-10
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 这也避免了对常规`next`方法进行两次调用。
- en: 'The following holds true when inside a `xxx_open` method:'
id: totrans-11
prefs: []
type: TYPE_NORMAL
zh: 当处于`xxx_open`方法内部时,以下情况成立:
- en: The indicators have not been recalculated and hold the values that were last
seen during the previous cycle in the equivalent `xxx` regular methods
id: totrans-12
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 指标尚未重新计算,并保留了上一个周期中在等效的`xxx`常规方法中最后看到的值。
- en: The broker has not yet evaluated the pending orders for the new cycle and new
orders can be introduced which will be evaluated if possible.
id: totrans-13
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 经纪人尚未评估新周期的待处理订单,可以引入新订单,如果可能的话将进行评估。
- en: 'Notice that:'
id: totrans-14
prefs: []
type: TYPE_NORMAL
zh: 请注意:
- en: '`Cerebro` also has a `broker_coo` (default: `True`) parameter which tells cerebro
that if `cheat-on-open` has been activated, it shall try to activate it also in
the broker if possible.'
id: totrans-15
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`Cerebro`还有一个名为`broker_coo`(默认为`True`)的参数,告诉`cerebro`如果已经激活了`cheat-on-open`,它应该尽可能地在经纪人中也激活它。'
- en: 'The simulation broker has a parameter named: `coo` and a method to set it named
`set_coo`'
id: totrans-16
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 仿真经纪人有一个名为:`coo`的参数和一个设置它的方法名为`set_coo`
- en: Trying cheat-on-open
id: totrans-17
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 尝试作弊开盘
- en: 'The sample below has a strategy with 2 different behaviors:'
id: totrans-18
prefs: []
type: TYPE_NORMAL
zh: 下面的示例具有具有2种不同行为的策略:
- en: If *cheat-on-open* is *True*, it will only operate from `next_open`
id: totrans-19
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 如果*cheat-on-open*是*True*,它将只从`next_open`操作
- en: If *cheat-on-open* is *False*, it will only operate from `next`
id: totrans-20
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 如果*cheat-on-open*是*False*,它将只从`next`操作
- en: In both cases the matching price must be the **same**
id: totrans-21
prefs: []
type: TYPE_NORMAL
zh: 在这两种情况下,匹配价格必须是**相同的**
- en: If not cheating, the order is issued at the end of the previous day and will
be matched with the next incoming price which is the `open` price
id: totrans-22
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 如果不作弊,订单将在前一天结束时发布,并将与下一个输入价格(即`open`价格)匹配
- en: If cheating, the order is issued on the same day it is executed. Because the
order is issued before the broker has evaluated orders, it will also be matched
with the next incoming price, the `open` price.
id: totrans-23
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 如果作弊,订单将在执行当天发布。因为订单是在经纪人评估订单之前发布的,所以它也将与下一个输入价格(即`open`价格)匹配。
- en: This second scenario, allows calculation of exact stakes for *all-in* strategies,
because one can directly access the current `open` price.
id: totrans-24
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 第二种情况允许计算*全情投入*策略的确切赌注,因为可以直接访问当前的`open`价格。
- en: In both cases
id: totrans-25
prefs: []
type: TYPE_NORMAL
zh: 在这两种情况下
- en: The current `open` and `close` prices will be printed from `next`.
id: totrans-26
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 当前的`open`和`close`价格将从`next`打印出来。
- en: 'Regular execution:'
id: totrans-27
prefs: []
type: TYPE_NORMAL
zh: 常规执行:
- en: '[PRE1]'
id: totrans-28
prefs: []
type: TYPE_PRE
zh: '[PRE1]'
- en: '![image](../Images/0fdbcee754e7caf6d5abcfa1d35f4dbc.png)'
id: totrans-29
prefs: []
type: TYPE_IMG
zh: '![图片](../Images/0fdbcee754e7caf6d5abcfa1d35f4dbc.png)'
- en: 'The order:'
id: totrans-30
prefs: []
type: TYPE_NORMAL
zh: 顺序:
- en: Is issued on 2005-04-08 after the *close*
id: totrans-31
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 在2005-04-08*close*之后发布
- en: It is executed on 2005-04-11 with the `open` price of `3088.47`
id: totrans-32
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 它是在2005-04-11执行的,以`3088.47`的`open`价格执行。
- en: 'Cheating execution:'
id: totrans-33
prefs: []
type: TYPE_NORMAL
zh: 作弊执行:
- en: '[PRE2]'
id: totrans-34
prefs: []
type: TYPE_PRE
zh: '[PRE2]'
- en: '![image](../Images/563421cf98c94af36cb0ecaff39f95ad.png)'
id: totrans-35
prefs: []
type: TYPE_IMG
zh: '![图片](../Images/563421cf98c94af36cb0ecaff39f95ad.png)'
- en: 'The order:'
id: totrans-36
prefs: []
type: TYPE_NORMAL
zh: 顺序:
- en: Is issued on 2005-04-11 before the *open*
id: totrans-37
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 在2005-04-11 *开盘* 之前发布
- en: It is executed on 2005-04-11 with the `open` price of `3088.47`
id: totrans-38
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: 它在2005-04-11执行,开盘价为`3088.47`
- en: And the overall result as seen on the chart is also the same.
id: totrans-39
prefs: []
type: TYPE_NORMAL
zh: 而图表上整体的结果也是相同的。
- en: Conclusion
id: totrans-40
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 结论
- en: Cheating on the open allows issuing orders before the open which can for example
allow the exact calculation of stakes for *all-in* scenarios.
id: totrans-41
prefs: []
type: TYPE_NORMAL
zh: 在开盘前作弊允许在开盘前发布订单,例如允许精确计算*全仓*情况下的股份。
- en: Sample usage
id: totrans-42
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 样本用法
- en: '[PRE3]'
id: totrans-43
prefs: []
type: TYPE_PRE
zh: '[PRE3]'
- en: Sample source
id: totrans-44
prefs:
- PREF_H2
type: TYPE_NORMAL
zh: 样本来源
- en: '[PRE4]'
id: totrans-45
prefs: []
type: TYPE_PRE
zh: '[PRE4]'
- en: Fillers
id: totrans-0
prefs:
- PREF_H1
type: TYPE_NORMAL
- en: 原文:[https://www.backtrader.com/docu/filler/](https://www.backtrader.com/docu/filler/)
id: totrans-1
prefs:
- PREF_BQ
type: TYPE_NORMAL
- en: 'The *backtrader* broker simulation has a default strategy when it comes to
using volume for order execution:'
id: totrans-2
prefs: []
type: TYPE_NORMAL
- en: Ignore volume
id: totrans-3
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: 'This is based on 2 premises:'
id: totrans-4
prefs: []
type: TYPE_NORMAL
- en: Trade in markets liquid enough to fully absorb *buy/sell* orders in one go
id: totrans-5
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: Real volume matching requires a real wolrd
id: totrans-6
prefs:
- PREF_UL
type: TYPE_NORMAL
......@@ -30,170 +37,224 @@
extra actors happen to be in the market to discriminate if such an order would
be or would not be matched to stick to the `Fill` part or if the order should
be `Kill`
id: totrans-7
prefs:
- PREF_IND
type: TYPE_NORMAL
- en: But the *broker* can accept *Volume Fillers* which determine how much of the
volume at a given point in time has to be used for *order matching*.
id: totrans-8
prefs: []
type: TYPE_NORMAL
- en: The fillers signature
id: totrans-9
prefs:
- PREF_H2
type: TYPE_NORMAL
- en: 'A *filler* in the *backtrader* ecosystem can be any *callable* which matches
the following signature:'
id: totrans-10
prefs: []
type: TYPE_NORMAL
- en: '[PRE0]'
id: totrans-11
prefs: []
type: TYPE_PRE
zh: '[PRE0]'
- en: 'Where:'
id: totrans-12
prefs: []
type: TYPE_NORMAL
- en: '`order` is the order which is going to be executed'
id: totrans-13
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: This object gives access to the `data` object which is the target of the operation,
creation sizes/prices, execution prices/sizes/remaining sizes and other details
id: totrans-14
prefs:
- PREF_IND
type: TYPE_NORMAL
- en: '`price` at which the order is going to be executed'
id: totrans-15
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: '`ago` is the index to the `data` in the *order* in which to look for the volume
and price elements'
id: totrans-16
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: In almost all cases this will be `0` (current point in time) but in a corner
case to cover `Close` orders this may be `-1`
id: totrans-17
prefs:
- PREF_IND
type: TYPE_NORMAL
- en: 'To for example access the bar volume do:'
id: totrans-18
prefs:
- PREF_IND
type: TYPE_NORMAL
- en: '[PRE1]'
id: totrans-19
prefs:
- PREF_IND
type: TYPE_PRE
zh: '[PRE1]'
- en: 'The callable can be a function or for example an instance of a class supporting
the `__call__` method, like in:'
id: totrans-20
prefs: []
type: TYPE_NORMAL
- en: '[PRE2]'
id: totrans-21
prefs: []
type: TYPE_PRE
zh: '[PRE2]'
- en: Adding a Filler to the broker
id: totrans-22
prefs:
- PREF_H2
type: TYPE_NORMAL
- en: 'The most straightforward method is to use the `set_filler`:'
id: totrans-23
prefs: []
type: TYPE_NORMAL
- en: '[PRE3]'
id: totrans-24
prefs: []
type: TYPE_PRE
zh: '[PRE3]'
- en: 'The second choice is to completely replace the `broker`, although this is probably
only meant for subclasses of `BrokerBack` which have rewritten portions of the
functionality:'
id: totrans-25
prefs: []
type: TYPE_NORMAL
- en: '[PRE4]'
id: totrans-26
prefs: []
type: TYPE_PRE
zh: '[PRE4]'
- en: The sample
id: totrans-27
prefs:
- PREF_H2
type: TYPE_NORMAL
- en: The *backtrader* sources contain a sample named `volumefilling` which allows
to test some of the integrated `fillers` (initially all)
id: totrans-28
prefs: []
type: TYPE_NORMAL
- en: Reference
id: totrans-29
prefs:
- PREF_H2
type: TYPE_NORMAL
- en: class backtrader.fillers.FixedSize()
id: totrans-30
prefs:
- PREF_H4
type: TYPE_NORMAL
- en: Returns the execution size for a given order using a *percentage* of the volume
in a bar.
id: totrans-31
prefs: []
type: TYPE_NORMAL
- en: This percentage is set with the parameter `perc`
id: totrans-32
prefs: []
type: TYPE_NORMAL
- en: 'Params:'
id: totrans-33
prefs: []
type: TYPE_NORMAL
- en: '`size` (default: `None`) maximum size to be executed. The actual volume of
the bar at execution time is also a limit if smaller than the size'
id: totrans-34
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: If the value of this parameter evaluates to False, the entire volume of the
bar will be used to match the order
id: totrans-35
prefs:
- PREF_IND
type: TYPE_NORMAL
- en: class backtrader.fillers.FixedBarPerc()
id: totrans-36
prefs:
- PREF_H4
type: TYPE_NORMAL
- en: Returns the execution size for a given order using a *percentage* of the volume
in a bar.
id: totrans-37
prefs: []
type: TYPE_NORMAL
- en: This percentage is set with the parameter `perc`
id: totrans-38
prefs: []
type: TYPE_NORMAL
- en: 'Params:'
id: totrans-39
prefs: []
type: TYPE_NORMAL
- en: '`perc` (default: `100.0`) (valied values: `0.0 - 100.0`)'
id: totrans-40
prefs:
- PREF_UL
type: TYPE_NORMAL
- en: Percentage of the volume bar to use to execute an order
id: totrans-41
prefs:
- PREF_IND
type: TYPE_NORMAL
- en: class backtrader.fillers.BarPointPerc()
id: totrans-42
prefs:
- PREF_H4
type: TYPE_NORMAL
- en: Returns the execution size for a given order. The volume will be distributed
uniformly in the range *high*-*low* using `minmov` to partition.
id: totrans-43
prefs: []
type: TYPE_NORMAL
- en: From the allocated volume for the given price, the `perc` percentage will be
used
id: totrans-44
prefs: []
type: TYPE_NORMAL
zh: 从给定价格的分配交易量中,将使用 `perc` 百分比
- en: 'Params:'
id: totrans-45
prefs: []
type: TYPE_NORMAL
zh: 参数:
- en: '`minmov` (default: `0.01`)'
id: totrans-46
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`minmov`(默认值:`0.01`)'
- en: Minimum price movement. Used to partition the range *high*-*low* to proportionally
distribute the volume amongst possible prices
id: totrans-47
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 最小价格变动。用于将范围 *高*-*低* 分割,以在可能的价格之间按比例分配交易量
- en: '`perc` (default: `100.0`) (valied values: `0.0 - 100.0`)'
id: totrans-48
prefs:
- PREF_UL
type: TYPE_NORMAL
zh: '`perc`(默认值:`100.0`)(有效值:`0.0 - 100.0`)'
- en: Percentage of the volume allocated to the order execution price to use for matching
id: totrans-49
prefs:
- PREF_IND
type: TYPE_NORMAL
zh: 分配给订单执行价格的交易量百分比,用于匹配
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册