pine-scriptpine-script-v5

Pinescript enter on specific days open


I am having trouble getting pine script to enter on the open of a specific day and close at the end of that day or at the next days open.

I have an example entry here. All this strategy should do is buy at Mondays open and sell at Tuesdays open/Mondays close, whatever is easier.

if dayofweek.monday == dayofweek(time)
    strategy.entry("Monday Buy", strategy.long)
    if close <= (open - mondayStop) or dayofweek(time) != dayofweek.monday
        strategy.close("Monday Buy")

This strategy will have longs opening on Tuesdays and some weeks no days at all and selling on Tuesdays the following week. How would I go about getting this to buy at Mondays open and sell at the close/Tuesdays open?


Solution

  • You are using the dayofweek wrong.

    Below will do.

    //@version=5
    strategy("My script", overlay=true, process_orders_on_close = true)
    
    is_monday = dayofweek == dayofweek.monday
    is_tuesday = dayofweek == dayofweek.tuesday
    
    if (is_monday)
        strategy.entry("Long", strategy.long)
    
    if (is_tuesday)
        strategy.close("Long")
    

    enter image description here

    Please note that it will use exchange's timezone.