English 中文(简体)
处理“停止损失”和“利润”问题
原标题:Issue with Stop Losses and Take Profits

我的ESMA基于动力战略的文字如下:除利润和停止损失出境令外,还算好。 我有洞察力,以使其能够/处置,如果能够停止损失,就不收取利润。 我确信,这与评估离职条件的顺序有关,但我根本无法正确执行。 如果有人能够看一看,我会非常赞赏。

请参看以下文字:

//@version=5
strategy("Momentum Wolf v3", overlay=true)

// Input parameters
length = input.int(14, title="Length")
adxSmoothing = input.int(14, title="ADX Smoothing")
threshold = input.float(25, title="Threshold")
stopLossEnabled = input.bool(true, title="Enable Stop Loss")
takeProfitEnabled = input.bool(true, title="Enable Take Profit")
stopLossValue = input.float(50, title="Stop Loss Value", minval=0, step=0.1)
takeProfitValue = input.float(100, title="Take Profit Value", minval=0, step=0.1)
allowedSessionStartHour = input.int(9, title="Allowed Trading Session Start Hour", minval=0, maxval=23)
allowedSessionStartMinute = input.int(30, title="Allowed Trading Session Start Minute", minval=0, maxval=59)
allowedSessionEndHour = input.int(16, title="Allowed Trading Session End Hour", minval=0, maxval=23)
allowedSessionEndMinute = input.int(0, title="Allowed Trading Session End Minute", minval=0, maxval=59)
onlyPositiveDMI = input.bool(true, title="Take Longs only when DMI is Positive")
onlyNegativeDMI = input.bool(true, title="Take Shorts only when DMI is Negative")

// Custom function to check if the current time is within the allowed trading session
isAllowedSession() =>
    currentHour = hour(time)
    currentMinute = minute(time)
    sessionStartHour = allowedSessionStartHour
    sessionStartMinute = allowedSessionStartMinute
    sessionEndHour = allowedSessionEndHour
    sessionEndMinute = allowedSessionEndMinute
    
    // Check if the current time is within the allowed session
    (currentHour > sessionStartHour or (currentHour == sessionStartHour and currentMinute >= sessionStartMinute)) and (currentHour < sessionEndHour or (currentHour == sessionEndHour and currentMinute <= sessionEndMinute))

// Custom function to check if the current time is beyond the allowed session end time
isSessionEnded() =>
    currentHour = hour(time)
    currentMinute = minute(time)
    sessionEndHour = allowedSessionEndHour
    sessionEndMinute = allowedSessionEndMinute

    // Check if the current time is beyond the allowed session end time
    currentHour > sessionEndHour or (currentHour == sessionEndHour and currentMinute > sessionEndMinute)

// Calculate moving averages
baseMA = ta.ema(close, 3)
fastMA = ta.ema(close, 20)
mediumMA = ta.ema(close, 100)
slowMA = ta.ema(close, 200)

// Calculate DMI components
[di_plus, di_minus, adx] = ta.dmi(length, adxSmoothing)

// Detect crossover and crossunder
crossover = ta.crossover(di_plus, di_minus)
crossunder = ta.crossunder(di_plus, di_minus)

// Enter long positions
if ta.crossover(baseMA, fastMA) and fastMA > mediumMA and fastMA > slowMA and (onlyPositiveDMI ? di_plus > di_minus : true) and isAllowedSession()
    strategy.entry("buy 1", strategy.long)
    strategy.close("sell 1")
    strategy.close("sell 2")
    strategy.close("sell 3")

if ta.crossover(baseMA, mediumMA) and mediumMA > fastMA and mediumMA > slowMA and (onlyPositiveDMI ? di_plus > di_minus : true) and isAllowedSession()
    strategy.entry("buy 2", strategy.long)
    strategy.close("sell 1")
    strategy.close("sell 2")
    strategy.close("sell 3")

if ta.crossover(baseMA, slowMA) and slowMA > fastMA and slowMA > mediumMA and (onlyPositiveDMI ? di_plus > di_minus : true) and isAllowedSession()
    strategy.entry("buy 3", strategy.long)
    strategy.close("sell 1")
    strategy.close("sell 2")
    strategy.close("sell 3")

// Enter short positions
if ta.crossunder(baseMA, fastMA) and fastMA < mediumMA and fastMA < slowMA and (onlyNegativeDMI ? di_minus > di_plus : true) and isAllowedSession()
    strategy.entry("sell 1", strategy.short)
    strategy.close("buy 1")
    strategy.close("buy 2")
    strategy.close("buy 3")

if ta.crossunder(baseMA, mediumMA) and mediumMA < fastMA and mediumMA < slowMA and (onlyNegativeDMI ? di_minus > di_plus : true) and isAllowedSession()
    strategy.entry("sell 2", strategy.short)
    strategy.close("buy 1")
    strategy.close("buy 2")
    strategy.close("buy 3")

if ta.crossunder(baseMA, slowMA) and slowMA < fastMA and slowMA < mediumMA and (onlyNegativeDMI ? di_minus > di_plus : true) and isAllowedSession()
    strategy.entry("sell 3", strategy.short)
    strategy.close("buy 1")
    strategy.close("buy 2")
    strategy.close("buy 3")

// Exit orders
if ta.crossunder(di_plus, di_minus) or isSessionEnded()
    strategy.close("buy 1")
    strategy.close("buy 2")
    strategy.close("buy 3")

if stopLossEnabled
    strategy.exit("sell SL", "sell 1", loss=stopLossValue)
    strategy.exit("sell SL", "sell 2", loss=stopLossValue)
    strategy.exit("sell SL", "sell 3", loss=stopLossValue)

if takeProfitEnabled
    strategy.exit("sell TP", "sell 1", profit=takeProfitValue)
    strategy.exit("sell TP", "sell 2", profit=takeProfitValue)
    strategy.exit("sell TP", "sell 3", profit=takeProfitValue)

if ta.crossover(di_plus, di_minus) or isSessionEnded()
    strategy.close("sell 1")
    strategy.close("sell 2")
    strategy.close("sell 3")

if stopLossEnabled
    strategy.exit("buy SL", "buy 1", loss=stopLossValue)
    strategy.exit("buy SL", "buy 2", loss=stopLossValue)
    strategy.exit("buy SL", "buy 3", loss=stopLossValue)

if takeProfitEnabled
    strategy.exit("buy TP", "buy 1", profit=takeProfitValue)
    strategy.exit("buy TP", "buy 2", profit=takeProfitValue)
    strategy.exit("buy TP", "buy 3", profit=takeProfitValue)

// Plot moving averages
plot(baseMA, "Base MA", color.white)
plot(fastMA, "Fast MA", color.yellow)
plot(mediumMA, "Medium MA", color.orange)
plot(slowMA, "Slow MA", color.red)

// Plot signals
plotshape(strategy.opentrades != 0 ? crossover : na, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="DMI Crossover")
plotshape(strategy.opentrades != 0 ? crossunder : na, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="DMI Crossunder")

我尝试了各种秩序结构,以确保停机损失和获利后订单都得到评估。 不工作......

问题回答

You are not ensuring that both can fire at the same time, that is why the issue. The proper template is given below:

if stopLossEnabled and not takeProfitEnabled
    strategy.exit("sell SL", "sell 1", loss=stopLossValue)
    strategy.exit("sell SL", "sell 2", loss=stopLossValue)
    strategy.exit("sell SL", "sell 3", loss=stopLossValue)

if takeProfitEnabled and stopLossEnabled
    strategy.exit("sell TP", "sell 1", profit=takeProfitValue)
    strategy.exit("sell TP", "sell 2", profit=takeProfitValue)
    strategy.exit("sell TP", "sell 3", profit=takeProfitValue)

if stopLossEnabled and takeProfitEnabled
    strategy.exit("sell SL", "sell 1", loss=stopLossValue, profit=takeProfitValue)
    strategy.exit("sell SL", "sell 2", loss=stopLossValue, profit=takeProfitValue)
    strategy.exit("sell SL", "sell 3", loss=stopLossValue, profit=takeProfitValue)

适用于其他一套<编码>战略.exit()功能,并重新确定。





相关问题
Pinescript to avoid immediate long/short entry

I am quite new to pine script coding and seeking some advice on the buy/sell alerts. When I tried to run the RSI strategy and back test it, I see that the long is immediately followed by a short entry ...

How to make my script to draw beyond 25 candles?

My script calculates the price distance between OHLC. I found this script online and modified it to suit my needs. The issue is that it only labels up to 25 candles. I believe there s something wrong ...

热门标签