如何在vectorbt中根据ticks数量或每份合同价格设置止损。

huangapple go评论50阅读模式
英文:

How to set a stoploss in vectorbt based on the number of ticks or price per contract

问题

以下是翻译好的部分:

有在 vbt.Portfolio.from_signals 中添加 tp_stopsl_stop 的选项,这是基于百分比的。

sl_stop:浮点数数组 止损。将进行广播。

针对多头/空头头寸,相对于收购价格的百分比。请注意,0.01 = 1%。

我想根据价格移动了 n 个价格刻度或每个合同的 n 美元来退出。例如,如果价格朝我不利移动了50个刻度,那么就退出。

英文:

There's an options to add tp_stop or sl_stop on vbt.Portfolio.from_signals which is percent based.

> sl_stop : array_like of float Stop loss. Will broadcast.
>
> A percentage below/above the acquisition price for long/short
> position. Note that 0.01 = 1%.

I would like to exit based on a move of n price ticks or n dollars per contract. For example if price moves 50 ticks in against me then exit.

答案1

得分: 1

I can provide the translation of the code-related content:

我可以至少指导你正确的方向尽管我不能完全回答

你需要查看 vectorbt 的 "adjust_tp_func_nb""adjust_sl_func_nb"

当被问及如何创建比静态百分比更复杂的止盈规则时vectorbt 的作者曾说过 "要添加更多条件 ... 你需要使用 adjust_tp_func_nb"

类似地你可以使用 "adjust_sl_func_nb" 创建自定义的止损逻辑

查看 [vectorbt 的文档][1]

以及我提到的 [作者的回答][2]

编辑我在第一个链接中找到了一个与你的情况接近的示例[在这里][1]寻找 "我们可以在每个时间步实施自己的止损或止盈,或者调整现有的止损或止盈:"

你会看到一个类似这样的示例

    >>> @njit
    ... def adjust_sl_func_nb(c):
    ...     current_profit = (c.val_price_now - c.init_price) / c.init_price
    ...     if current_profit >= 0.40:
    ...         return 0.25, True
    ...     elif current_profit >= 0.25:
    ...         return 0.15, True
    ...     elif current_profit >= 0.20:
    ...         return 0.07, True
    ...     return c.curr_stop, c.curr_trail

    >>> close = pd.Series([10, 11, 12, 11, 10])
    >>> pf = vbt.Portfolio.from_signals(close, adjust_sl_func_nb=adjust_sl_func_nb)
    >>> pf.asset_flow()
    0    10.0
    1     0.0
    2     0.0
    3   -10.0  # 从 12 到 7% 触发
    4    11.0
    dtype: float64

Please note that the code translation is provided as requested, and it may not include all the explanations and context found in the original text.

英文:

I can at least point you in the right direction, though I can't answer fully.

You need to check out vectorbt's "adjust_tp_func_nb" and "adjust_sl_func_nb".

When asked about how to create a take profit rule that is more elaborate than just a static percentage, vectorbt's author has said "To add a further condition ... you need to use adjust_tp_func_nb."

Similarly, you can create custom stop loss logic with "adjust_sl_func_nb".

See vectorbt's docs

And the author's answer I mentioned

Edit: I found an example that is close to your scenario, at the first link, here. Look for "We can implement our own stop loss or take profit, or adjust the existing one at each time step. Let's implement stepped stop-loss:"

You will see an example like:

>>> @njit
... def adjust_sl_func_nb(c):
...     current_profit = (c.val_price_now - c.init_price) / c.init_price
...     if current_profit >= 0.40:
...         return 0.25, True
...     elif current_profit >= 0.25:
...         return 0.15, True
...     elif current_profit >= 0.20:
...         return 0.07, True
...     return c.curr_stop, c.curr_trail

>>> close = pd.Series([10, 11, 12, 11, 10])
>>> pf = vbt.Portfolio.from_signals(close, adjust_sl_func_nb=adjust_sl_func_nb)
>>> pf.asset_flow()
0    10.0
1     0.0
2     0.0
3   -10.0  # 7% from 12 hit
4    11.0
dtype: float64

huangapple
  • 本文由 发表于 2023年5月25日 10:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定