Ganapati Ns
Ganapati Ns

Reputation: 5

Place an order before candle close in tradingview pinescript

# I am creating a 5EMA indicator where i will be place a sell order when previouscandle is above 5ema and current candle price is less than previous low. As soon as this condition is true i need to place a sell order. However the code is placing sell order only after current candle close. How do i edit to place order as soon as the sell condition is met? I tried using nz(request.security(syminfo.tickerid, "1", close), but that didnt help. is there a way we can place an order before the candle close?


//@version=5
    strategy("New Gan Sell Signal 5EMA", overlay=true)

// Calculate 5 EMA
    emaLength = 5
    emaValue = ta.ema(close, emaLength)

    previousCandleClose = close[1]
    previousCandleOpen = open[1]
    previousCandleLow = low[1]
    previousCandleEMA = ta.ema(close, emaLength)[1]

// Check if previous candle's close and open were above the 5 EMA
    previousCandleAboveEMA =previousCandleClose>previousCandleEMA and previousCandleOpen> previousCandleEMA

// Check if previous candle's low is below the previous candle's EMA value
    previousCandleLowBelowEMA = previousCandleLow > previousCandleEMA


// Check if current price is below previous low
   currentPriceBelowPreviousLow = close < low[1] 

// Generate sell signal
    sellSignal = previousCandleAboveEMA and previousCandleLowBelowEMA and currentPriceBelowPreviousLow

// Plotting the 5 EMA and sell signal
    plot(emaValue, color=color.blue, title="5 EMA")
    plotshape(sellSignal, style=shape.triangledown, color=color.red, size=size.small, title="Sell Signal")

// Declare stop loss price variable
    var float stopLossPrice = na

// Open sell position and plot stop loss when sell signal is generated
    if sellSignal // and not strategy.position_size > 0
    strategy.entry("Sell", strategy.short)
    stopLossPrice := high[1]  // Set stop loss at the highest high of the last 5 candles
    strategy.exit("StopLoss", "Sell", loss=stopLossPrice)

// Declare take profit price variable
    var float takeProfitPrice = na

// Calculate take profit price based on stop loss value
    if sellSignal
    float stopLossValue = stopLossPrice - close  // Calculate stop loss value
    takeProfitPrice := close -(stopLossValue * 2)  //Calculate take profit price with 1:3 risk-reward ratio

// Plot take profit line
    line.new(x1 = bar_index[5], y1 = takeProfitPrice, x2 = bar_index, y2 = takeProfitPrice, color =     color.green, width = 1, style = line.style_solid)

// Check if take profit is hit
    if strategy.position_size > 0 and close <= takeProfitPrice
    strategy.close("Sell")  // Close the sell position when take profit is hit

// Plot stop loss line
    var float stopLossHigh = na
    if sellSignal
    stopLossHigh := high[1]  // Set stop loss at the high of the previous candle where the sell signal occurred
    line.new(x1 = bar_index[5], y1 = stopLossHigh, x2 = bar_index, y2 = stopLossHigh, color = color.red, width = 1, style = line.style_solid)

// Calculate take profit price based on stop loss value
    if sellSignal
    float stopLossValue = stopLossPrice - close  // Calculate stop loss value
    takeProfitPrice := close - (stopLossValue * 2)  // Calculate take profit price with 1:3 risk-reward ratio

// Plot take profit line
    line.new(x1 = bar_index[5], y1 = takeProfitPrice, x2 = bar_index, y2 = takeProfitPrice, color =   color.green, width = 1, style = line.style_solid)

// Plot sell signal price
    var float sellSignalPrice = na
    if sellSignal
    sellSignalPrice := close
    plot(sellSignalPrice, color=color.orange, linewidth=1, title="Sell Signal Price")

Upvotes: 0

Views: 476

Answers (1)

vitruvius
vitruvius

Reputation: 21121

By default, it will place a market order when the bar is closed.

If you want to place limit orders, based on a price, you should use the limit or stop arguments of the strategy.entry().

limit (series int/float): An optional parameter. Limit price of the order. If it is specified, the order type is either 'limit', or 'stop-limit'. 'NaN' should be specified for any other order type.

stop (series int/float): An optional parameter. Stop price of the order. If it is specified, the order type is either 'stop', or 'stop-limit'. 'NaN' should be specified for any other order type.

Upvotes: 0

Related Questions