Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

exit 和 filter 没有起到作用

  1. User

    exit 和 filter 没有起到作用

  2. Assistant

    很抱歉这两个函数没有起作用。请提供更多具体信息,以便我能够给出更准确的解答。

  3. User

    //@version=5 strategy("Supertrend RTX001", overlay=true)

    // Input variables atrPeriod = input(10, "ATR Length") factor = input.float(3.0, "Factor", step=0.01) trailingStopPercentage = input.float(1.0, "Trailing Stop Percentage", step=0.1) hullPeriod = input(14, "Hull Moving Average Period")

    // Calculate Supertrend [supertrend, direction] = ta.supertrend(factor, atrPeriod)

    // Calculate Hull Moving Average hull = ta.hma(close, hullPeriod)

    // Plot Supertrend and Hull Moving Average bodyMiddle = plot((open + close) / 2, display=display.none) upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color=color.green, style=plot.style_linebr) downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color=color.red, style=plot.style_linebr) hullPlot = plot(hull, "Hull Moving Average", color=color.blue)

    // Fill body middle fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false) fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)

    // Entry Rules longCondition = ta.crossover(close, supertrend) and close > hull shortCondition = ta.crossunder(close, supertrend) and close < hull

    // Exit Rules longExitCondition = ta.crossunder(close, hull) shortExitCondition = ta.crossover(close, hull)

    // Trailing Stop var float longTrailingStopPrice = na var float shortTrailingStopPrice = na

    if longCondition strategy.entry("Long", strategy.long) longTrailingStopPrice := close * (1 - trailingStopPercentage / 100)

    if shortCondition strategy.entry("Short", strategy.short) shortTrailingStopPrice := close * (1 + trailingStopPercentage / 100)

    if longExitCondition strategy.exit("Long Exit", "Long", stop=longTrailingStopPrice)

    if shortExitCondition strategy.exit("Short Exit", "Short", stop=shortTrailingStopPrice)

  4. Assistant

    根据您提供的代码,其中的 exit 和 filter 函数没有被用到,因此可能无法达到您期望的目标。如果您想要添加出场条件或筛选条件,请使用 strategy.exit 和 strategy.filter 函数来设置。

    在您的代码中,已经定义了出场条件 longExitCondition 和 shortExitCondition,您可以使用 strategy.exit 函数来设置出场操作。例如,您可以添加以下代码来设置固定止损出场:

    if longExitCondition
        strategy.exit("Long Exit", "Long")
    
    if shortExitCondition
        strategy.exit("Short Exit", "Short")
    

    如果您想要使用移动止损,可以使用 stop 参数来设置动态止损价格。例如,您可以将以下代码添加到您的 if longExitCondition 和 if shortExitCondition 的条件块中来设置移动止损:

    longTrailingStopPrice := close * (1 - trailingStopPercentage / 100)
    strategy.exit("Long Exit", "Long", stop=longTrailingStopPrice)
    
    shortTrailingStopPrice := close * (1 + trailingStopPercentage / 100)
    strategy.exit("Short Exit", "Short", stop=shortTrailingStopPrice)
    

    请确保在 if longCondition 和 if shortCondition 条件块中设置入场操作之后设置出场操作。

    另外,如果您想要添加筛选条件,可以使用 strategy.filter 函数。例如,您可以添加以下代码来设置筛选条件:

    filterCondition = ta.some_other_condition()
    strategy.filter(filterCondition)
    

    请确保在定义 filterCondition 之后,并在设置入场操作之前设置筛选条件。

    希望能对您有所帮助!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.