Here is my Pinescript code for a strategy. Problem is that it is taking many un-wanted trades. Surprisingly those trades are opening and closing at the same candle. It does take correct trades also but not sure why it is picking these un-wanted trades and opening and closing of those trades are in same candle.
//@version=6
strategy("Multi-Indicator Trading Strategy", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Input parameters
psarStep = input.float(0.02, "PSAR Step")
psarMax = input.float(0.2, "PSAR Max")
vwmaLength = input(20, "VWMA Length")
supertrendPeriod = input(10, "Supertrend Period")
supertrendMultiplier = input.float(2.0, "Supertrend Multiplier")
profitTargetINR = input.float(1000, "Fixed Profit Target (INR)", step=1)
trailingStopPercentage = input.float(5, "Trailing Stop %", step=0.1)
trailingTakePercentage = input.float(5, "Trailing Take Profit %", step=0.1)
// Indicator Calculations
vwap = ta.vwap(close)
psar = ta.sar(psarStep, psarStep, psarMax)
vwma = ta.vwma(close, vwmaLength)
atr = ta.atr(supertrendPeriod)
[supertrend, supertrendDir] = ta.supertrend(supertrendMultiplier, supertrendPeriod)
rsi = ta.rsi(close, 14)
// Relaxed Volume condition
volumeCondition = volume + volume[1] >= 250
// Long Entry Conditions (relaxed)
longCondition = (close > vwap and close[1] > vwap) and (close > psar and close[1] > psar) and (close > vwma and close[1] > vwma) and (close > supertrend and close[1] > supertrend) and (rsi >= 50 and rsi <= 75) and (volumeCondition)
// Short Entry Conditions (relaxed)
shortCondition = (close < vwap and close[1] < vwap) and (close < psar and close[1] < psar) and (close < vwma and close[1] < vwma) and (close < supertrend and close[1] < supertrend) and (rsi >= 25 and rsi <= 40) and (volumeCondition)
// Initialize trailing stop and take profit variables
var float trailingStopLong = na
var float trailingTakeLong = na
var float trailingStopShort = na
var float trailingTakeShort = na
// Trade Entry Logic
if (longCondition and strategy.position_size == 0)
strategy.entry("Long", strategy.long)
trailingStopLong := close * (1 - trailingStopPercentage / 100)
trailingTakeLong := close * (1 + trailingTakePercentage / 100)
if (shortCondition and strategy.position_size == 0)
strategy.entry("Short", strategy.short)
trailingStopShort := close * (1 + trailingStopPercentage / 100)
trailingTakeShort := close * (1 - trailingTakePercentage / 100)
// Update trailing stop and take profit levels for long positions
if (strategy.position_size > 0)
trailingStopLong := math.max(trailingStopLong, close * (1 - trailingStopPercentage / 100))
trailingTakeLong := math.max(trailingTakeLong, close * (1 + trailingTakePercentage / 100))
// Exit if price hits trailing stop or take profit
if (close <= trailingStopLong)
strategy.close("Long", comment="Trailing Stop Hit")
if (close >= trailingTakeLong)
strategy.close("Long", comment="Trailing Take Profit Hit")
// Exit if profit reaches ₹100
//if (strategy.netprofit >= profitTargetINR)
// strategy.close("Long", comment="Fixed Profit Target Hit")
// Update trailing stop and take profit levels for short positions
if (strategy.position_size < 0)
trailingStopShort := math.min(trailingStopShort, close * (1 + trailingStopPercentage / 100))
trailingTakeShort := math.min(trailingTakeShort, close * (1 - trailingTakePercentage / 100))
// Exit if price hits trailing stop or take profit
if (close >= trailingStopShort)
strategy.close("Short", comment="Trailing Stop Hit")
if (close <= trailingTakeShort)
strategy.close("Short", comment="Trailing Take Profit Hit")
// Exit if profit reaches ₹100
//if (strategy.netprofit >= profitTargetINR)
// strategy.close("Short", comment="Fixed Profit Target Hit")
// Logic to close all trades at 15:20
closeTimeHour = 15
closeTimeMinute = 20
//if (hour == closeTimeHour and minute >= closeTimeMinute and strategy.position_size != 0)
// strategy.close_all(comment="Closed at 15:20")
// Plot indicators for visual reference
plot(vwap, color=color.black, linewidth=2, title="VWAP")
plot(psar, color=color.red, title="PSAR")
plot(vwma, color=color.yellow, title="VWMA")
plot(supertrend, color=color.purple, title="Supertrend")
Please advise. Thanks
for entry_orders, try this:
if (longCondition and strategy.position_size <= 0)
for SL & TP use strategy.exit() whit stop & limit
strategy.exit(
id = 'Long Exit',
from_entry = 'Long',
stop = trailingStopLong,
limit = trailingTakeLong)
for debug, plot trailingTake & trailingStop
//@version=6
strategy("Multi-Indicator Trading Strategy", overlay=true)
// Input parameters
psarStep = input.float(0.02, "PSAR Step")
psarMax = input.float(0.2, "PSAR Max")
vwmaLength = input(20, "VWMA Length")
supertrendPeriod = input(10, "Supertrend Period")
supertrendMultiplier = input.float(2.0, "Supertrend Multiplier")
profitTargetINR = input.float(1000, "Fixed Profit Target (INR)", step=1)
trailingStopPercentage = input.float(5, "Trailing Stop %", step=0.1)
trailingTakePercentage = input.float(5, "Trailing Take Profit %", step=0.1)
// Indicator Calculations
vwap = ta.vwap(close)
psar = ta.sar(psarStep, psarStep, psarMax)
vwma = ta.vwma(close, vwmaLength)
atr = ta.atr(supertrendPeriod)
[supertrend, supertrendDir] = ta.supertrend(supertrendMultiplier, supertrendPeriod)
rsi = ta.rsi(close, 14)
// Relaxed Volume condition
volumeCondition = volume + volume[1] >= 250
// Long Entry Conditions (relaxed)
longCondition = (close > vwap and close[1] > vwap) and (close > psar and close[1] > psar) and (close > vwma and close[1] > vwma) and (close > supertrend and close[1] > supertrend) and (rsi >= 50 and rsi <= 75) and (volumeCondition)
// Short Entry Conditions (relaxed)
shortCondition = (close < vwap and close[1] < vwap) and (close < psar and close[1] < psar) and (close < vwma and close[1] < vwma) and (close < supertrend and close[1] < supertrend) and (rsi >= 25 and rsi <= 40) and (volumeCondition)
// Initialize trailing stop and take profit variables
var float trailingStopLong = na
var float trailingTakeLong = na
var float trailingStopShort = na
var float trailingTakeShort = na
// Trade Entry Logic
if (longCondition and strategy.position_size <= 0)
strategy.entry("Long", strategy.long)
trailingStopLong := close * (1 - trailingStopPercentage / 100)
trailingTakeLong := close * (1 + trailingTakePercentage / 100)
trailingStopShort := na
trailingTakeShort := na
if (shortCondition and strategy.position_size >= 0)
strategy.entry("Short", strategy.short)
trailingStopShort := close * (1 + trailingStopPercentage / 100)
trailingTakeShort := close * (1 - trailingTakePercentage / 100)
trailingStopLong := na
trailingTakeLong := na
// Update trailing stop and take profit levels for long positions
if (strategy.position_size > 0)
trailingStopLong := math.max(trailingStopLong, close * (1 - trailingStopPercentage / 100))
trailingTakeLong := math.max(trailingTakeLong, close * (1 + trailingTakePercentage / 100))
strategy.exit(
id = 'Long Exit',
from_entry = 'Long',
stop = trailingStopLong,
limit = trailingTakeLong,
comment_loss = 'trailingStopLong',
comment_profit = 'trailingTakeLong')
// Update trailing stop and take profit levels for short positions
if (strategy.position_size < 0)
trailingStopShort := math.min(trailingStopShort, close * (1 + trailingStopPercentage / 100))
trailingTakeShort := math.min(trailingTakeShort, close * (1 - trailingTakePercentage / 100))
strategy.exit(
id = 'Short Exit',
from_entry = 'Short',
stop = trailingStopShort,
limit = trailingTakeShort,
comment_loss = 'trailingStopShort',
comment_profit = 'trailingTakeShort')
// Logic to close all trades at 15:20
closeTimeHour = 15
closeTimeMinute = 20
//if (hour == closeTimeHour and minute >= closeTimeMinute and strategy.position_size != 0)
// strategy.close_all(comment="Closed at 15:20")
// Plot indicators for visual reference
plot(vwap, color=color.black, linewidth=2, title="VWAP")
plot(psar, color=color.red, title="PSAR")
plot(vwma, color=color.yellow, title="VWMA")
plot(supertrend, color=color.purple, title="Supertrend")
plot (
trailingTakeLong,
'trailingTakeLong',
linewidth = 2,
color = color.new(color.olive, 0),
style = plot.style_steplinebr)
plot (
trailingStopLong,
'trailingStopLong',
linewidth = 2,
color = color.new(color.maroon, 0),
style = plot.style_steplinebr)
plot (
trailingTakeShort,
'trailingTakeShort',
linewidth = 2,
color = color.new(color.olive, 0),
style = plot.style_steplinebr)
plot (
trailingStopShort,
'trailingStopShort',
linewidth = 2,
color = color.new(color.maroon, 0),
style = plot.style_steplinebr)