Open/Close on specified day of week at close



New member
ThinkScript newb here.

I'm trying to do something that should be simple but getting tripped up. I want to take an input parameter for two days of the week. One to open a position at close, and another to close that position at close. I would expect the bare minimum script to be something like what I have below but when it labels the chart its a day off, opening the trade a day late and closing it a day late. A classic off-by-1 software problem, but when I change the "+1" I use to force a cast of the enum to a 0, the chart doesn't even draw. So can someone explain/fix what I am doing wrong? Also, is there a way to change it to the n-th and n-th + x trading day of the week to account for holidays?

input BuyOnCloseDay  = {default Monday, Tuesday, Wednesday, Thursday, Friday};
input SellOnCloseDay = {Monday, Tuesday, Wednesday, default Thursday, Friday};

# GetDayOfWeek return value ranges from from 1 (Monday) to 7 (Sunday)
#Don't know how to cast directly, but presuming enums start with 0.
def buy  = GetDayOfWeek(GetYYYYMMDD()) == (BuyOnCloseDay  + 1);# && RegularTradingEnd(GetYYYYMMDD());
def sell = GetDayOfWeek(GetYYYYMMDD()) == (SellOnCloseDay + 1);# && RegularTradingEnd(GetYYYYMMDD());

#the labels on the chart buy/sells end up being on different days than expected
addOrder(OrderType.BUY_TO_OPEN,    buy, name="Buy "  + BuyOnCloseDay  + "Close", price = CLOSE);
addOrder(OrderType.Sell_TO_CLOSE, sell, name="Sell " + SellOnCloseDay + "Close", price = CLOSE);

Similar threads