Join useThinkScript to post your question to a community of 21,000+ developers and traders.
# buy startOffset bars into then open
# close endOffset bars before the close
input startOffset = 5;
input endOffset = 2;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if
(beforeStart[1+startOffset] == 1 and beforeStart[startOffset] == 0) or
(isRollover[startOffset] and beforeStart[startOffset] == 0)
then 1
else 0;
def lastBarOfDay = if
(afterEnd[-1-endOffset] == 1 and afterEnd[endOffset] == 0) or
(isRollover[-1-endOffset] and firstBarOfDay[-1-endOffset])
then 1
else 0;
AddOrder(OrderType.BUY_TO_OPEN, firstBarOfDay[-1], Open[-1], 1);
AddOrder(OrderType.SELL_TO_CLOSE, lastBarOfDay[-1], Open[-1], 1);
declare lower;
input startOffset = 5;
input endOffset = 2;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if
(beforeStart[1+startOffset] == 1 and beforeStart[startOffset] == 0) or
(isRollover[startOffset] and beforeStart[startOffset] == 0)
then 1
else 0;
def lastBarOfDay = if
(afterEnd[-1-endOffset] == 1 and afterEnd[endOffset] == 0) or
(isRollover[-1-endOffset] and firstBarOfDay[-1-endOffset])
then 1
else 0;
plot x = lastBarOfDay;
input startOffset = 5;
input endOffset = 2;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if
(beforeStart[1 + startOffset] == 1 and beforeStart[startOffset] == 0) or
(isRollover[startOffset] and beforeStart[startOffset] == 0)
then 1
else 0;
def lastBarOfDay = if
(afterEnd[-1 - endOffset] == 1 and afterEnd[endOffset] == 0) or
(isRollover[-1 - endOffset] and firstBarOfDay[-1 - endOffset])
then 1
else 0;
AddOrder(OrderType.SELL_TO_CLOSE, if lastBarOfDay == 1 then lastBarOfDay else Double.NaN, open, name = "SELL.EndOfDay");
##ADD AT TOP OF CODE##
def CloseAllCondition = SecondstillTime(1558) == 0;
def EOD = if SecondsTillTime(1558) == 0 and
SecondsFromTime(1558) == 0
then 1
else 0;
## ADD TOWARDS BOTTOM WITH OTHER AddOrder calls##
AddOrder(OrderType.Sell_TO_CLOSE, EOD or closeAllCondition, price = close, tickcolor = Color.ORANGE, arrowcolor = Color.ORANGE, name = "SELL EOD");
AddOrder(OrderType.BUY_TO_CLOSE, EOD or closeAllCondition, price = close, tickcolor = Color.ORANGE, arrowcolor = Color.ORANGE, name = "BUY EOD");
Start a new thread and receive assistance from our community.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.