使用我的 pine 脚本代码一次只需要执行一个命令

Need to execute only one order at a time with my pine script code

我一直在努力使用 pine 脚本编写代码。应用购买订单时,即使满足所有条件,我也不希望在一个订单到位时应用另一个订单。 我的代码适用于 ADX 策略,如果 ADX 值越过进入阈值且收盘值大于 EMA 值,则会生成买入交易信号,如果 ADX 值越过退出阈值且收盘值小于 EMA value 生成卖出交易信号。如果执行止损1%,交易将退出。

一旦买单到位即执行卖单

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Saurabhbul1985

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

//Defination of ADX

adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]
adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)

// Input Parameters

entrythreshold=input(title="Entry threshold",defval=16)

exitthreshold=input(title="Exit threshold",defval=46)

slowemalen=input(title="Slow EMA length", defval=150)

longLossPerc = input(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=1) * 0.01

shortLossPerc = input(title="Short Stop Loss (%)", minval=0.0, step=0.1, defval=1) * 0.01

// Calculate moving averages

slowEMA = ema(close, slowemalen)


// Calculate trading conditions

enterLong = crossover(sig, entrythreshold) and (close > slowEMA)
enterShort = crossover(sig, entrythreshold) and (close < slowEMA)


// Calculate Stop loss 

longStopPrice  = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)

//calculate Exit conditions

exitLong  = crossunder(sig, exitthreshold)
exitShort = crossunder(sig,exitthreshold)

// Plot stop loss values for confirmation

plot(series=strategy.position_size > 0 ? longStopPrice : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Long Trail Stop")
plot(series=strategy.position_size < 0 ? shortStopPrice : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Short Trail Stop")
plot(slowEMA)

// Submit entry orders
if enterLong
    strategy.entry(id="EL", long=true)
    

if (enterShort)
    strategy.entry(id="ES", long=false)
    
// Exit trades
if (exitLong)
    strategy.close(id="EL")

if (exitShort)
    strategy.close(id="ES")
   
if (strategy.position_size > 0)
    strategy.exit(id="XL STP", stop=longStopPrice)

if (strategy.position_size < 0)
    strategy.exit(id="XS STP", stop=shortStopPrice)

如何制定这个策略?

这应该将您的入场条件限制在还没有交易活跃的时候:

enterLong = crossover(sig, entrythreshold) and (close > slowEMA) and strategy.opentrades == 0
enterShort = crossover(sig, entrythreshold) and (close < slowEMA) and strategy.opentrades == 0