krish srinivasan
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: August 24th, 2023
Contact: TradingView Profile

Need help .. rookie here

I have coded in V5 a strategy for 1 min scalping. What I need is to count a certain number of candles after strategy.exit is executed before entering into the next long or short trade based on rules that I have set.

I could not find strategy.position_exit as a function.

Any help would be greatly appreciated. It is after watching the Art of Trading YouTube I picked up the confidence to try coding, and here i am asking for help

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Need help .. rookie here

Krish, can you post the strategy and I can see if I can help

krish srinivasan
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: August 24th, 2023
Contact: TradingView Profile

Re: Need help .. rookie here

Hi Steve, Thanks for your response. pasted below is my code for a strategy. I'm having an issue with the exit condition. it's very erratic. Not exiting as per condition. Also pasted is the image of what the code should do. Appreciate and thanks in anticipation.


//@version=5
// *********************** INPUT Section **************************************
strategy(title="1min index scalping with ema-stoch-roc", shorttitle="1 min index scalping", overlay = true)

fastema = input.int(title = "fastEMA", defval = 5, minval = 1, maxval = 100, step = 1, group = "EMA settings")
medema = input.int(title = "Medium EMA", defval = 21,minval = 1, maxval = 100, step = 1, group = "EMA settings")
sloema = input.int(title = "SLo EMA", defval = 50, minval = 1, maxval = 100, step = 1, group = "EMA settings")

fastroc = input.int(title = "fast ROC", defval = 8, minval = 1, maxval = 100, step = 1, group = "ROC settings")
medroc = input.int(title = "medium ROC", defval = 21, minval = 1, maxval = 100, step = 1, group = "ROC settings")
sloroc = input.int(title = "slow ROC", defval = 34, minval = 1, maxval = 100, step = 1, group = "ROC settings")

source = input(close)
stochLen = input.int(title = "stoch Len", defval = 14, minval = 1, maxval = 100, step = 1, group = "Stoch settings")
stochKlen = input.int(title = "stoch K smoothing", defval = 6, minval = 1, maxval = 100, step = 1, group = "Stoch settings")
stochDLen = input.int(title = "stoch D smoothing", defval = 3, minval = 1, maxval = 100, step = 1, group = "Stoch settings")

//TpCE = input.int(title = "call optionE take profit", defval = 20, minval = 5, maxval = 80, step = 1, group = "P&L settings")
//TpPE = input.int(title = "put option take profit", defval = 20, minval = 5, maxval = 80, step = 1, group = "P&L settings")
slCE = input.int(title = "call option stop loss", defval = 20, minval = 5, maxval = 80, step = 1, group = "P&L settings")
SlPE = input.int(title = "put option stop loss", defval = 20, minval = 5, maxval = 80, step = 1, group = "P&L settings")

longQty = input.int(title="long quantity", defval=45, minval = 15, maxval = 2700, step = 15, group = "Trade quantity")
shortQty = input.int(title="short quantity", defval=45, minval = 15, maxval = 2700, step = 15, group = "Trade quantity")

// ********************** COMPUTE INDICATORS VALUES ******************************

emafast = ta.ema(source, fastema)
emamed = ta.ema(source, medema)

stoch = ta.stoch(source, high, low, stochLen)
stochk = ta.sma(stoch, stochKlen)
stochd = ta.sma(stochk, stochDLen)

rocfast = ta.roc(emamed, fastroc)


// function in Global scope to check if the current position is flat before a new order can be entered

IsFlat() =>
strategy.position_size == 0

// ********************** Buy - Sell and exit conditions **************************

// Long

stochBuy = stochk > stochd
rocBuy = rocfast > 0 and rocfast >= rocfast[1]
goLong = stochBuy and rocBuy


//square off the long

longsquareoff = rocfast < rocfast[1] and rocfast > 0

// short

stochSell = stochk < stochd
rocSell = rocfast < 0 and rocfast <= rocfast[1]
goShort = stochSell and rocSell

//cover the short
shortcover = rocfast > rocfast[1] and rocfast < 0


// ********************* Order Execution ********************************************

// Enter Call positions

if goLong and IsFlat()
strategy.entry(id = "Buy", direction = strategy.long, qty = longQty, limit = close, comment = "Long")

lastEntryPriceCE = strategy.opentrades.entry_price(strategy.opentrades - 1)
orderpriceCe = math.floor(lastEntryPriceCE)

stoplossCe = orderpriceCe - slCE

if strategy.position_size > 0 and longsquareoff
strategy.exit(id= "Buy", from_entry = "Buy", qty = longQty, stop = stoplossCe, comment = "sqroff")

// Enter Put positions

if goShort and IsFlat()
strategy.entry(id = "short", direction = strategy.short, qty = shortQty, limit = close, comment = "short")

lastEntryPricePE = strategy.opentrades.entry_price(strategy.opentrades - 1)
orderpricePe = math.floor(lastEntryPricePE)

stoplossPe = orderpricePe + SlPE

if strategy.position_size < 0 and shortcover
strategy.exit(id= "short", from_entry = "short", qty = shortQty, stop = stoplossPe, comment = "cover")

// ********************** PLOT OUTPUTS *******************************************

//plot(emafast, title = "fast EMA", color = color.blue, linewidth = 1)

plot(emamed, title = "medium EMA", color = color.black, linewidth = 1)
plot(rocfast, title = "fast roc", color = color.teal, linewidth = 1)
plot(stochk, title = "Stochastic K", color = color.blue, linewidth = 1)
plot(stochd, title = "Stochastic D", color = color.orange, linewidth = 1)
plot(stoplossCe, title = "SLCE", color = color.fuchsia, style = plot.style_circles)
plot(orderpriceCe, title = "ordCE", color = color.orange, style = plot.style_circles)
plot(stoplossPe, title = "SLCE", color = color.red, style = plot.style_circles)
plot(orderpricePe, title = "ordCE", color = color.green, style = plot.style_circles)

Return to “Pine Script Q&A”