如何在趋势的一个方向上进行一笔交易?

How to make one trade in one direction of the trend?

我正在尝试解决问题,以使其在长期内变得完美,而不是在达到目标或停止时重蹈覆辙。例如,我打开一个空头交易,我不希望在达到止盈时再次打开交易(因为交易的所有条件都满足),相反,我希望脚本等待下一个信号相反的方向。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DavydovEV
//@version=5
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)
// Input settings
SLfixed = input.float(0.5, "Stop loss", minval=0.1, maxval=100, step=0.1)
TPfixed = input.float(1, "Take profit", minval=0.1, maxval=100,step=0.1)
// Long/short condition
longCondition = ta.sma(close, 14) > ta.sma(close, 28)
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = ta.sma(close, 14) < ta.sma(close, 28)
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

longStop = strategy.position_avg_price*(100-SLfixed)/100
longProfit = strategy.position_avg_price*(100+TPfixed)/100
shortStop = strategy.position_avg_price*(100+SLfixed)/100
shortProfit = strategy.position_avg_price*(100-TPfixed)/100

strategy.exit("Exit","My Long Entry Id", stop=longStop, limit=longProfit, when=strategy.position_size>0)
strategy.exit("Exit","My Short Entry Id", stop=shortStop, limit=shortProfit, when=strategy.position_size>0)

// @AndreyD solution
getLastPosSign() =>
    strategy.closedtrades > 0 ? math.sign(strategy.closedtrades.size(strategy.closedtrades-1)) : na

    
plot(ta.sma(close, 14), color=color.green)
plot(ta.sma(close, 28), color=color.red)
plot( getLastPosSign(), style = plot.style_linebr )

我已经问过这个问题,@AndreyD 建议了以下解决方案

getLastPosSign() =>
    strategy.closedtrades > 0 ? math.sign(strategy.closedtrades.size(strategy.closedtrades-1)) : na

不幸的是,我没有设法将这个函数附加到我的脚本中。在此解决方案中,当交易方向改变时,该值从 -1 变为 1。在这种情况下,我必须写条件,如果过去的价值与当前的价值相同,那么就不需要开仓。

我不太明白这个解决方案是否适合我,所以我无法在我的脚本中实现它。

您需要将 getLastPosSign() 结果与 longConditionshortCondition 一起使用:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DavydovEV
//@version=5
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)
// Input settings
SLfixed = input.float(0.5, "Stop loss", minval=0.1, maxval=100, step=0.1)
TPfixed = input.float(1, "Take profit", minval=0.1, maxval=100,step=0.1)

// @AndreyD solution
getLastPosSign() =>
    strategy.closedtrades > 0 ? math.sign(strategy.closedtrades.size(strategy.closedtrades-1)) : na

lastPos = getLastPosSign()
canEnterLong = nz(lastPos) <= 0
canEnterShort = nz(lastPos) >= 0

// Long/short conditio
longCondition = ta.sma(close, 14) > ta.sma(close, 28)
if (longCondition and canEnterLong)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = ta.sma(close, 14) < ta.sma(close, 28)
if (shortCondition and canEnterShort)
    strategy.entry("My Short Entry Id", strategy.short)

longStop = strategy.position_avg_price*(100-SLfixed)/100
longProfit = strategy.position_avg_price*(100+TPfixed)/100
shortStop = strategy.position_avg_price*(100+SLfixed)/100
shortProfit = strategy.position_avg_price*(100-TPfixed)/100

strategy.exit("Exit","My Long Entry Id", stop=longStop, limit=longProfit)
strategy.exit("Exit","My Short Entry Id", stop=shortStop, limit=shortProfit)

plot(ta.sma(close, 14), color=color.green)
plot(ta.sma(close, 28), color=color.red)
plot( lastPos, style = plot.style_linebr )