我正在使用 ta.lowest 函数来计算止损和止盈。虽然我希望在执行交易时这些成为静态固定数字
我使用 ta.lowest 和 ta.highest 函数来计算止损和获利。尽管我希望这些在执行交易时成为静态固定数字。因此,在多头交易中,我尝试将止损设在过去 10 天的低点,并将止盈设为该低点的数倍。不过,当交易执行时,这些数字会继续根据过去的柱进行计算并随价格变化(正如它们应该的那样)。有没有办法可以在交易执行后将它们设为静态数字?
high10 = ta.highestbars(10)
low10 = ta.lowest(low, 10)
takeProfit = ((close - low10) * 2) + close
longCondition = ta.crossover(EMA_fast, EMA_slow)
longExitCondition = ta.crossunder(EMA_fast, EMA_slow)
longExitCondition1 = takeProfit
if longCondition
strategy.entry("long", strategy.long)
stopLoss = low10
strategy.exit("exit", "long", loss=stopLoss)
if high >= longExitCondition1
strategy.close(id="long")
I'm using the ta.lowest and ta.highest function to calculate a stop loss and take profit. Though I want these to become a static fixed number when trade is executed. So on a long trade, im trying to put my stop at the low of the past 10 days, and the take profit a multiple of that. Though when the trade executes, these numbers continue to calculate off the past bars and move with price (as they should). Is there a way I can make these a static number when the trade has been executed?
high10 = ta.highestbars(10)
low10 = ta.lowest(low, 10)
takeProfit = ((close - low10) * 2) + close
longCondition = ta.crossover(EMA_fast, EMA_slow)
longExitCondition = ta.crossunder(EMA_fast, EMA_slow)
longExitCondition1 = takeProfit
if longCondition
strategy.entry("long", strategy.long)
stopLoss = low10
strategy.exit("exit", "long", loss=stopLoss)
if high >= longExitCondition1
strategy.close(id="long")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
var
来防止重新计算柱之间的值。使用var
声明的变量在运行时在第一个柱上初始化,并保留它们的值,除非我们指定另一个值。稍后可以使用:=
运算符将新值分配给脚本中的变量。在这里,我采用了您的脚本并添加了停止和限制,并用var
声明它们。它们是根据进入条件分配的。我还绘制了它们,以便您可以看到它们,并包含用于描述更改的注释。干杯并祝您编码和交易好运
we can use
var
to prevent recalculation of values from bar to bar. Variables declared withvar
initialize on the first bar at runtime, and hold their values, unless we dictate another value. New values can be assigned to a variable later in the script using the:=
operator. Here I have taken your script and added a stop and a limit declaring them withvar
. They are assigned under the entry condition. I also plotted them so you can see them and included notes for descriptions of the changes.Cheers and best of luck with your coding and trading