Trades that follow a specific day-of-the-week strategy can be beneficial for many traders. By entering trades on the same day each week, traders can build consistent patterns to help them make informed decisions. For example, a trader may enter a buy trade every Monday and then close the trade on Friday or add this as an additional filter to his signals.
In this article, I’m showing how to work with days of the week in Pine Script and an example of a simple strategy built with only days of the week signals. To know the current day of the week, I’m using “dayofweek()” function. It outputs numbers from 1 to 7 depending on the day of the week. Be careful with the values, 1 is Sunday, and 7 is Saturday here.
So to create a strategy that enters positions at Monday close and exits on Friday close, you have to create only two simple conditions:
long_entry = dayofweek(time) == 2 // Monday
long_exit = dayofweek(time) == 6 // Friday
to force strategy to execute orders at the close, add “process_orders_on_close = true” parameter to your strategy function call. As you can see, it’s quite easy to work with time in Pine Script, and this strategy requires only a few lines of code. Of course, you can combine these conditions with additional filters from other indicators and make your strategy much more advanced.
Here is the strategy’s complete code:
//@version=5
strategy("My strategy", overlay=true, process_orders_on_close = true)
long_entry = dayofweek(time) == 2 // Monday
long_exit = dayofweek(time) == 6 // Friday
if (long_entry)
strategy.entry("Long", strategy.long)
if (long_exit)
strategy.close("Long")
Follow me on TradingView and YouTube.
Hi,
If i wanted to include Mon-Thurs to filter alerts.
would
long_entry = dayofweek(time) >=1 and <=4
work?
yep, it should work. But I think that Mon is 2 in Pine Script.