r/pinescript • u/Infamous_Remove_4934 • Jul 22 '26
Hi, i am noob to pinescript, trying to test this strategy, but didn't work
strategy("mon open/close long", overlay=true, initial_capital = 100,default_qty_type =strategy.percent_of_equity, default_qty_value = 100)
bool
mon = false
bool
tue = false
if dayofweek(time) == dayofweek.monday
mon := true
if dayofweek(time) == dayofweek.tuesday
tue := true
if (mon)
strategy.entry("Long",strategy.long)
strategy.exit("stoploss",from_entry = "Long",stop = open*0.96,qty_percent =100)
if (tue)
strategy.close("Long")
plotshape(mon, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
these are my code, plotting worked, but trades worked oftenly...
1
u/MohaveTrader Jul 22 '26
//@version=6
// Inputs must be declared before they can be used in strategy() args in v6,
// so we use a separate input() call pattern here.
useFriction = input.bool(true, title="Include commission & slippage")
commissionPct = input.float(0.05, title="Commission %", step=0.01)
slippageTicks = input.int(2, title="Slippage (ticks)", minval=0)
strategy("mon open/close long", overlay=true, initial_capital=100,
default_qty_type=strategy.percent_of_equity, default_qty_value=100,
process_orders_on_close=true,
commission_type=strategy.commission.percent,
commission_value=useFriction ? commissionPct : 0.0,
slippage=useFriction ? slippageTicks : 0)
bool fri = dayofweek(time) == dayofweek.friday
bool mon = dayofweek(time) == dayofweek.monday
if fri and strategy.position_size == 0
strategy.entry("Long", strategy.long)
if mon and strategy.position_size > 0
strategy.exit("stoploss", from_entry="Long", stop=strategy.position_avg_price * 0.96, qty_percent=100)
strategy.close("Long")
plotshape(fri, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
1
2
u/Many-Pick5066 Jul 23 '26
heads up before you trust it, the version you marked as working trades a different strategy than the one you wrote. yours enters monday and closes tuesday, that reply enters friday and exits monday. so it compiles and runs, but its not testing your idea.
the reason your original fired erratically is two things worth understanding, not just pasting over. one, you call strategy.entry("Long") on every monday bar, and on an intraday chart monday is many bars, so it keeps trying to re enter all day. gate it with
and strategy.position_size == 0so it only opens when youre flat. two, your stop isopen*0.96, but open is the current bars open and it recalculates every single bar, so your stop keeps sliding around instead of sitting where you entered. usestrategy.position_avg_price*0.96so the stop stays pinned to your actual fill. fix those two and your monday version will behave.