Buy if 1st candle of day closes above yesterdays daily close

bmn

Member
How can I fix this code to buy if first 5 minute or x minute candle of the day closes higher than yesterdays daily close, and short if the opposite?

Here is what i have. It kinda works, but something is too processor intensive and the floating profit and loss in Ondemand fails to calculate anything about 50% of the time. I am using a 5 minute chart.

Rich (BB code):
#simply buys if first candle is higher than last day close, or shorts if lower than last close, close all eod

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBar = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def yesterdayclose =  close(period = AggregationPeriod.DAY)[1];
def golong = close >= yesterdayclose;
def isopen=if secondsFromTime(0930)>=0 and secondstillTime(1600)>=0 then 1 else 0;

plot closedaily = close(period = AggregationPeriod.DAY)[1];

AddOrder(OrderType.BUY_to_OPEN, isopen and golong and firstbar, name = "long gap up", tickcolor = GetColor(0), arrowcolor = GetColor(0));
AddOrder(OrderType.sell_to_open, isopen and !golong and firstbar, name = "short gap down", tickcolor = GetColor(1), arrowcolor = GetColor(1));

input barsFromEnd = 1;
def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;

#mod note:  changed to: open[-1] previous code was set up to repaint & give false results.
AddOrder(OrderType.SELL_TO_CLOSE, EOD[-1], open[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX");
AddOrder(OrderType.BUY_TO_CLOSE, EOD[-1], open[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");
 
Last edited by a moderator:
Solution
How can I fix this code to buy if first 5 minute or x minute candle of the day closes higher than yesterdays daily close, and short if the opposite?

Here is what i have. It kinda works, but something is too processor intensive and the floating profit and loss in Ondemand fails to calculate anything about 50% of the time. I am using a 5 minute chart.

Rich (BB code):
#simply buys if first candle is higher than last day close, or shorts if lower than last close, close all eod

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBar = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart ==...
How can I fix this code to buy if first 5 minute or x minute candle of the day closes higher than yesterdays daily close, and short if the opposite?

Here is what i have. It kinda works, but something is too processor intensive and the floating profit and loss in Ondemand fails to calculate anything about 50% of the time. I am using a 5 minute chart.

Rich (BB code):
#simply buys if first candle is higher than last day close, or shorts if lower than last close, close all eod

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBar = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def yesterdayclose =  close(period = AggregationPeriod.DAY)[1];
def golong = close >= yesterdayclose;
def isopen=if secondsFromTime(0930)>=0 and secondstillTime(1600)>=0 then 1 else 0;

plot closedaily = close(period = AggregationPeriod.DAY)[1];

AddOrder(OrderType.BUY_to_OPEN, isopen and golong and firstbar, name = "long gap up", tickcolor = GetColor(0), arrowcolor = GetColor(0));
AddOrder(OrderType.sell_to_open, isopen and !golong and firstbar, name = "short gap down", tickcolor = GetColor(1), arrowcolor = GetColor(1));

input barsFromEnd = 1;
def EOD = RegularTradingEnd(GetYYYYMMDD()) - GetTime() <= GetAggregationPeriod() * barsFromEnd - 1;

#mod note:  changed to: open[-1] previous code was set up to repaint & give false results.
AddOrder(OrderType.SELL_TO_CLOSE, EOD[-1], open[-1], 0, Color.ORANGE, Color.ORANGE, "EOD LX");
AddOrder(OrderType.BUY_TO_CLOSE, EOD[-1], open[-1], 0, Color.ORANGE, Color.ORANGE, "EOD SX");


draw arrows depending on the first bar of day close, compared to previous day close.

yesterdays daily close is the close of the last bar, during normal trading hours.
if data is collected from 2 different time periods, that don't overlap, 2nd aggregation isn't needed.

if using several lines of code from someones code, make a note where you got them.
(korygill code)


Code:
# firstbarofdayaboveprevdayclose_00

# https://usethinkscript.com/threads/buy-if-1st-candle-of-day-closes-above-yesterdays-daily-close.14654/

def na = double.nan;
def bn = barnumber();

# firstlast_barofday_00
# https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
# Author: Kory Gill, @korygill

def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollover[-1] and firstBarOfDay[-1])
    then 1
    else 0;


def rth = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and GetTime() < RegularTradingEnd(GetYYYYMMDD());

#def dayhigh = if bn == 1 then 0
# else if firstBarOfDay then high
# else if rth and high > dayhigh[1] then high
# else dayhigh[1];
 
def daylastclose = if lastBarOfDay then close else daylastclose[1];
def dayfirstclose = if firstBarOfDay then close else dayfirstclose[1];


plot firstabove = if firstBarOfDay and close > daylastclose then low else na;
firstabove.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
firstabove.SetDefaultColor(Color.cyan);
firstabove.setlineweight(4);
firstabove.hidebubble();

plot firstbelow = if firstBarOfDay and close < daylastclose then high else na;
firstbelow.SetPaintingStrategy(PaintingStrategy.ARROW_down);
firstbelow.SetDefaultColor(Color.white);
firstbelow.setlineweight(4);
firstbelow.hidebubble();
#
 
  • Like
Reactions: bmn
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
356 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

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.

How do I get started?

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.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top