Kaspar Hidayat
Kaspar Hidayat

Reputation: 11

Script compiles and strategy.entry condition is met but no trades are initiated in backtest

Not sure why Tradingview isn't showing this strategy entering any trades - I use the same entry condition for an indicator that shows points at which the condition is met. Been trying to debug this for hours. Would appreciate any help here. Thanks!

strategy("Good news", overlay=true, margin_long=500, margin_short=100, initial_capital=100, calc_on_order_fills=true, calc_on_every_tick=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

periods = 144
multip = 5
volthreshMA = multip*ta.sma(volume[1], periods)

longCondition = (volume > volthreshMA and close > open)
if (longCondition)
    strategy.entry("long", strategy.long)
    

//2% stop loss
stopCondition = strategy.opentrades.profit(0) < (-0.02*strategy.opentrades.size(0))
if (stopCondition)
    strategy.close("stop loss")

//take profit when profit is 80% of max profit during trade    
takeprofitCondition = ((strategy.opentrades.max_runup(0) - strategy.opentrades.profit (0))/strategy.opentrades.max_runup(0)) > 0.2
if (takeprofitCondition)
    strategy.close("take profit")```

Upvotes: 0

Views: 320

Answers (2)

Kaspar Hidayat
Kaspar Hidayat

Reputation: 11

Got it working!

//@version=5
strategy("Good news", overlay=true, margin_long=20, calc_on_order_fills=true, calc_on_every_tick=true, commission_type = strategy.commission.percent, commission_value = 0.07)

//recommended settings
//for 15min: 
//for 5min
//for 3min
//for 1min
periods = input(144,"Number of periods for MA")
multip = input(5, "Volume MA multiplier")


volthreshMA = multip*ta.sma(volume[1], periods)

longCondition = volume > volthreshMA and close > open
orderSize = strategy.equity/close
if (longCondition)
    strategy.entry("long", strategy.long, qty=orderSize)

//2% stop loss    
strategy.exit("stop loss", "long", loss = 200)

//take profit when price crosses under moving average
strategy.close("long", when = ta.crossunder(close,ta.sma(close,6)))

Upvotes: 1

MVP_Script
MVP_Script

Reputation: 131

You are trying to close an order "stop loss", which does not exist. I guess you are trying to exit the order "long". So that should be the 1st parameter in Strategy.close().

strategy("Good news", overlay=true, margin_long=500, margin_short=100, initial_capital=100, calc_on_order_fills=true, calc_on_every_tick=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

periods = 144
multip = 5
volthreshMA = multip*ta.sma(volume[1], periods)

longCondition = (volume > volthreshMA and close > open)
if (longCondition)
    strategy.entry("long", strategy.long)
    

//2% stop loss
stopCondition = strategy.opentrades.profit(0) < (-0.02*strategy.opentrades.size(0))
if (stopCondition)
    strategy.close("long", comment="stop loss")

//take profit when profit is 80% of max profit during trade    
takeprofitCondition = ((strategy.opentrades.max_runup(0) - strategy.opentrades.profit (0))/strategy.opentrades.max_runup(0)) > 0.2
if (takeprofitCondition)
    strategy.close("long", comment = "take profit")```

Upvotes: 0

Related Questions