首页 > 解决方案 > 如果最后一个订单相反,我可以将交易视图策略限制为仅做空/做多吗

问题描述

我一直在用 pinescript 编写我的策略,除了一件我无法解决的事情之外,一切都很好。

有没有办法告诉策略只有在最后一个条目是空头时才做多,反之亦然?

编辑:很抱歉之前没有在下面添加正确的代码,但这是因为我从来没有看到它不允许我保存它,因为我在帖子中没有足够的信息。

我目前的代码是:

//@version=4
strategy("My Script", overlay = true)

ma = ema(close, 200)

// Base condition
aboveMa = close > ma
belowMa = close < ma

//long conditions
threeAboveMa = aboveMa[1] and aboveMa[2] and aboveMa[3]
first3AboveMa = not aboveMa[4] and threeAboveMa

//short conditions
threeBelowMa = belowMa[1] and belowMa[2] and belowMa[3]
first3BelowMa = not belowMa[4] and threeBelowMa

// Debugging
plot(ma)
plotchar(aboveMa, "aboveMa", ".", location.top)
plotchar(threeAboveMa, "threeAboveMa", "•&quot;, location.top)
plotchar(first3AboveMa, "first3AboveMa", "▲&quot;, location.top)

// Debugging
plot(ma)
plotchar(belowMa, "aboveMa", ".", location.bottom)
plotchar(threeBelowMa, "threeAboveMa", "•&quot;, location.bottom)
plotchar(first3BelowMa, "first3AboveMa", "▲&quot;, location.bottom)

// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(8.0, title='Stop Loss %', type=input.float) / 100
takePer = input(8.0, title='Take Profit %', type=input.float) / 100

// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)

//TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01

longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - trailStop)
    max(stopValue, longStopPrice[1])
else
    0
shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + trailStop)
    min(stopValue, shortStopPrice[1])
else
    999999

//Long Entry
entrylong() => threeAboveMa
exitlong() => first3AboveMa

//Short Entry
entryshort() => threeBelowMa
exitshort() => first3AboveMa


if (threeAboveMa)
    strategy.entry(id = "long_ma", long = true, when = entrylong())
if (threeBelowMa)
    strategy.entry(id = "short_ma", long = false, when = entryshort())

if (threeAboveMa)
    strategy.exit(id = "exit_long", stop=longStop, limit=longTake)
if (threeBelowMa)
    strategy.exit(id = "exit_short", stop=shortStop, limit=shortTake)


//strategy.entry(id = "long_ma", long = true, when = entry())
//strategy.close(id = "long_ma", when = exit())

标签: pine-script

解决方案


推荐阅读