Piyush Katariya
Piyush Katariya

Reputation: 35

trigger strategy at specified time in pinescript

i am planning to test one very simple strategy in tradingview which needs to be executed based on time and color of previous candle. Below is my code. I am trying to run it on 15 Minute candle, if the 9:15 to 9:30 candle is green then buy else sell.

strategy("Time_Strategy", overlay=true, margin_long=100, margin_short=100)
if hour(time) == 09 and minute(time) == 31

longCondition = open[1] < close[1]
strategy.entry("Buy", strategy.long,1, when = longCondition)

shortCondition = open[1] > close[1]
strategy.entry("Buy", strategy.long,1, when = shortCondition)

When i run this i get "No Data" error in tradingview strategy tester.

Can someone please help if i am missing something.

Upvotes: 1

Views: 6888

Answers (1)

vitruvius
vitruvius

Reputation: 21121

You have two issues:

  • Your second strategy.entry() call should be of short type.
  • If you want to use this on a 15 min chart, then your time condition must be representable with 15 min bars. There won't be any bars at 09:31 on a 15 min chart. So, change it to 09:30.

This should work:

//@version=5
strategy("Time_Strategy", overlay=true, margin_long=100, margin_short=100)

if hour(time) == 09 and minute(time) == 30

    longCondition = open[1] < close[1]
    strategy.entry("Buy", strategy.long,1, when = longCondition)

    shortCondition = open[1] > close[1]
    strategy.entry("Sell", strategy.short,1, when = shortCondition)

enter image description here

Upvotes: 2

Related Questions