【问题标题】:How to set condition rules如何设置条件规则
【发布时间】:2021-04-18 17:55:38
【问题描述】:

我在 Pine Editor/TradingView 中编写了这个买入/卖出策略:

buy_condition= a<b
sell_condition= a>b

strategy.entry("Long", strategy.long, when=buy_condition)
strategy.exit("L_Out", "Long", profit=1000, loss=500)
strategy.entry("Short", strategy.short, when=sell_condition)
strategy.exit("S_Out", "Short", profit=1000, loss=500)

在多头或空头头寸中,当相反的条件成立时,它会改变头寸,但我希望它保持在头寸,直到它达到盈利或止损。然后它可以再次进入任何适合的位置。我该如何编写这条规则?

【问题讨论】:

标签: python algorithmic-trading


【解决方案1】:

也许你想替换你的:

buy_condition= a<b
sell_condition= a>b

作者:

def buy_condition(a, b):
    return a < b


def sell_condition(a, b):
    return a > b

当您创建buy_condition 时,它会在此时计算结果,而不是稍后。 所以如果a&lt;b在这一行,后面总是True

如果您将这些函数传递给您的 strategy.exit 方法,您必须使用类似 by_condition(a, b) 的方式调用该函数。

【讨论】:

  • 当我在 Pine Editor 中输入“def”时,它无法识别。以下链接是关于“Pine Script 语言参考手册”的。您可以检查一下以获取指导吗? tradingview.com/pine-script-reference
  • 我刚刚在条件中添加了 'strategy.position_size==0' 并且它起作用了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多