我正在使用 ta.lowest 函数来计算止损和止盈。虽然我希望在执行交易时这些成为静态固定数字

发布于 2025-01-13 06:51:31 字数 645 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2025-01-20 06:51:31

我们可以使用 var 来防止重新计算柱之间的值。使用 var 声明的变量在运行时在第一个柱上初始化,并保留它们的值,除非我们指定另一个值。稍后可以使用 := 运算符将新值分配给脚本中的变量。在这里,我采用了您的脚本并添加了停止和限制,并用 var 声明它们。它们是根据进入条件分配的。我还绘制了它们,以便您可以看到它们,并包含用于描述更改的注释。

干杯并祝您编码和交易好运

EMA_fast = ta.ema(close, 9)
EMA_slow = ta.ema(close, 21)

// declare 2 variables with `var` which sets them to 0 on first bar and will not recalculate until we "set" them
var stopLoss   = 0.0
var takeProfit = 0.0

long = strategy.position_size > 0 // a variable to check if we are in a long position 

high10 = ta.highestbars(10)

low10 = ta.lowest(low, 10)

takeProfitCalc = ((close - low10) * 2) + close

longCondition = ta.crossover(EMA_fast, EMA_slow)

longExitCondition = ta.crossunder(EMA_fast, EMA_slow)

// we need to check that we are not already long otherwise we may "set" our limits again mid-trade if the condition were to occur before the trade is through
if longCondition and not long
    strategy.entry("long", strategy.long)
    stopLoss   := low10
    takeProfit := takeProfitCalc  // Here we use `:=` to "set" the variables under the entry condition

strategy.exit("exit", "long", stop = stopLoss, limit = takeProfit) // we use the `stop` and `limit` args to enter our levels 

drawOrders = long or longCondition // condition to check when to draw lines. we only want them when in, or entering a position

plot(drawOrders or drawOrders[1] ? stopLoss   : na, "Stop Loss",   color.red   ,style = plot.style_linebr) // we use the draworders cond to draw lines on the exit bar too 
plot(drawOrders or drawOrders[1] ? takeProfit : na, "Take Profit", color.green ,style = plot.style_linebr) // note the plot style. this allows breaks in the lines when not in pos.

// optional close if cross back down
// if longExitCondition
//     strategy.close(id="long")

we can use var to prevent recalculation of values from bar to bar. Variables declared with var 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 with var. 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

EMA_fast = ta.ema(close, 9)
EMA_slow = ta.ema(close, 21)

// declare 2 variables with `var` which sets them to 0 on first bar and will not recalculate until we "set" them
var stopLoss   = 0.0
var takeProfit = 0.0

long = strategy.position_size > 0 // a variable to check if we are in a long position 

high10 = ta.highestbars(10)

low10 = ta.lowest(low, 10)

takeProfitCalc = ((close - low10) * 2) + close

longCondition = ta.crossover(EMA_fast, EMA_slow)

longExitCondition = ta.crossunder(EMA_fast, EMA_slow)

// we need to check that we are not already long otherwise we may "set" our limits again mid-trade if the condition were to occur before the trade is through
if longCondition and not long
    strategy.entry("long", strategy.long)
    stopLoss   := low10
    takeProfit := takeProfitCalc  // Here we use `:=` to "set" the variables under the entry condition

strategy.exit("exit", "long", stop = stopLoss, limit = takeProfit) // we use the `stop` and `limit` args to enter our levels 

drawOrders = long or longCondition // condition to check when to draw lines. we only want them when in, or entering a position

plot(drawOrders or drawOrders[1] ? stopLoss   : na, "Stop Loss",   color.red   ,style = plot.style_linebr) // we use the draworders cond to draw lines on the exit bar too 
plot(drawOrders or drawOrders[1] ? takeProfit : na, "Take Profit", color.green ,style = plot.style_linebr) // note the plot style. this allows breaks in the lines when not in pos.

// optional close if cross back down
// if longExitCondition
//     strategy.close(id="long")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文