Saurabh Bulandani
Saurabh Bulandani

Reputation: 7

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

I have been struggling with my code in pine script. When buy order is applied, i do not want another order to be applied when one order is in place even if all conditions are being fulfilled. My code works on ADX strategy, if the ADX value is crossing over a entry threshold and close value is greater than EMA value a BUy trade signal is generated, also If the ADX value is crossing under a exit threshold and close value is less than EMA value a sell trade signal is generated. Trade will be exited if Stop loss of 1% is executed.

Sell order is executed once buy order is only in place

enter image description here

// 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)

How to formulate this strategy?

Upvotes: 0

Views: 2914

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

This should restrict your entry conditions to times when there is no trade active yet:

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

Upvotes: 0

Related Questions