Pine Script Strategy trading at specific days of the week.

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.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.