• Get $40 off VIP by signing up for a free account! Sign Up

Reference Previous 2 Days close on 1 min chart

thetradingfrog

New member
Hello,

I have a scan that needs to look at 1 minute data and pull a value from the last bar ( 3:59 ) of the last 3 days. I created a simple test using just the close value ( I will have a custom calculation in the final scan).

Code:
def RTHclose = If(GetTime() crosses above RegularTradingEnd(GetYYYYMMDD()) - 60000, close, RTHclose[1]);
def RTHclose2 = If(RTHclose != RTHclose[1], RTHclose[1], RTHclose[2]);
def RTHclose3 = if(RTHclose2 != RTHclose2[1], RTHclose2[1], RTHclose[3]);

AddLabel(yes,
    "RTHclose: " + RTHclose + ", RTHclose2: " + RTHclose2 + ", RTHclose3: " + RTHclose3,
     Color.YELLOW
);

AddChartBubble(
    GetTime() crosses above (RegularTradingEnd(GetYYYYMMDD()) - 60000),
    high, BarNumber() + " : " + RTHclose2[1],
    Color.BLUE, no);

This results in the first and second values being correct (RTHclose and RTHclose2) but RTHclose3 is the same as RTHclose2. I am not sure the best way to get the last 3.

I created a more complicated version which works correctly when used on a chart but doesn't work in a scan because apparently the HighestAll functions will only look back 1 day worth of 1 minute bars in a scan. Below is the version that will retrieve the correct values on a chart but not a scan.

Code:
# Determine if bar is the last of the day #
def isRollover = currentDate != currentDate[1];
def currentDate = GetYYYYMMDD();

def beforeStart = GetTime() < RegularTradingStart(currentDate);
def afterEnd = GetTime() > RegularTradingEnd(currentDate);
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;

# Determine the highest bar number with a proper close indicating it is finished and not just a buffer bar #
def highestFinishedBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
# Checks that the current bar is not the most recent daily last bar #
def notCurrent = BarNumber() !=  highestFinishedBar and BarNumber() < highestFinishedBar;
# Finds the previous daily last bar #
def last_cond_bn_daily = HighestAll( If(lastBarOfDay and notCurrent, BarNumber(), 0));
# Finds the daily last bar 2 days back #
def last_cond_bn2_daily = HighestAll( If(lastBarOfDay and notCurrent and BarNumber() != last_cond_bn_daily, BarNumber(), 0));

def currentFirstHalf = _customCalculation(condition = conditionFirstHalf, rollOver = break);
# Populate the 1 bar back and 2 bars back values #
def oneBackFirstHalf = GetValue(currentFirstHalf, (BarNumber() - last_cond_bn_daily) );
def twoBackFirstHalf = GetValue(currentFirstHalf, (BarNumber() - last_cond_bn2_daily) );

I would love some help figuring this out as I have been running in circles for several days.

Thanks,
Nathan
 
Hello,

I have a scan that needs to look at 1 minute data and pull a value from the last bar ( 3:59 ) of the last 3 days. I created a simple test using just the close value ( I will have a custom calculation in the final scan).

Code:
def RTHclose = If(GetTime() crosses above RegularTradingEnd(GetYYYYMMDD()) - 60000, close, RTHclose[1]);
def RTHclose2 = If(RTHclose != RTHclose[1], RTHclose[1], RTHclose[2]);
def RTHclose3 = if(RTHclose2 != RTHclose2[1], RTHclose2[1], RTHclose[3]);

AddLabel(yes,
    "RTHclose: " + RTHclose + ", RTHclose2: " + RTHclose2 + ", RTHclose3: " + RTHclose3,
     Color.YELLOW
);

AddChartBubble(
    GetTime() crosses above (RegularTradingEnd(GetYYYYMMDD()) - 60000),
    high, BarNumber() + " : " + RTHclose2[1],
    Color.BLUE, no);

This results in the first and second values being correct (RTHclose and RTHclose2) but RTHclose3 is the same as RTHclose2. I am not sure the best way to get the last 3.

I created a more complicated version which works correctly when used on a chart but doesn't work in a scan because apparently the HighestAll functions will only look back 1 day worth of 1 minute bars in a scan. Below is the version that will retrieve the correct values on a chart but not a scan.

Code:
# Determine if bar is the last of the day #
def isRollover = currentDate != currentDate[1];
def currentDate = GetYYYYMMDD();

def beforeStart = GetTime() < RegularTradingStart(currentDate);
def afterEnd = GetTime() > RegularTradingEnd(currentDate);
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;

# Determine the highest bar number with a proper close indicating it is finished and not just a buffer bar #
def highestFinishedBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
# Checks that the current bar is not the most recent daily last bar #
def notCurrent = BarNumber() !=  highestFinishedBar and BarNumber() < highestFinishedBar;
# Finds the previous daily last bar #
def last_cond_bn_daily = HighestAll( If(lastBarOfDay and notCurrent, BarNumber(), 0));
# Finds the daily last bar 2 days back #
def last_cond_bn2_daily = HighestAll( If(lastBarOfDay and notCurrent and BarNumber() != last_cond_bn_daily, BarNumber(), 0));

def currentFirstHalf = _customCalculation(condition = conditionFirstHalf, rollOver = break);
# Populate the 1 bar back and 2 bars back values #
def oneBackFirstHalf = GetValue(currentFirstHalf, (BarNumber() - last_cond_bn_daily) );
def twoBackFirstHalf = GetValue(currentFirstHalf, (BarNumber() - last_cond_bn2_daily) );

I would love some help figuring this out as I have been running in circles for several days.

Thanks,
Nathan

See if this works in a scan for you without using highestall. You were very close in your original. Nice work!

Capture.jpg
Ruby:
def RTHclose  = If(GetTime() crosses above (RegularTradingEnd(GetYYYYMMDD()) - 60000), close, RTHclose[1]);
def RTHclose2 = If(RTHclose != RTHclose[1], RTHclose[1], RTHclose2[1]);
def RTHclose3 = If(RTHclose2 != RTHclose2[1], RTHclose2[1], RTHclose3[1]);

AddLabel(yes,
    "RTHclose: " + RTHclose[1] + ", RTHclose2: " + RTHclose2[1] + ", RTHclose3: " + RTHclose3[1],
     Color.YELLOW
);

AddChartBubble(
    GetTime() crosses above (RegularTradingEnd(GetYYYYMMDD()) - 60000),
    high, BarNumber() + " : " + RTHclose,
    Color.YELLOW, no);
 

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

See if this works in a scan for you without using highestall. You were very close in your original. Nice work!
I tried to adapt this code and it worked great as an indicator on the chart but sadly is having the same problem as a scan. It seems like anything beyond the first day is NaN. Below is a short example scan which demonstrates the problem. If you run that scan against the S&P 500 should will see that all the results come back indicating the 1 day back value is always NaN. On a chart works great in scan NaN.

Code:
input start = 0930;
input midpoint = 1245;
input end = 1600;


def firstHalf = SecondsFromTime(start) >= 0 and SecondsTillTime(midpoint) >= 0;

def break = GetDay() <> GetDay()[1];

def vp = volume * hlc3;
rec volSum = If(break, 0, If(firstHalf, volSum[1] + volume, volSum[1]));
rec priceWeightedSum = If(break,0,If( firstHalf, priceWeightedSum[1] + vp, priceWeightedSum[1]));
def currentFirstHalfVWAP =  priceWeightedSum / volSum;

def RTHclose  = If(GetTime() crosses above (RegularTradingEnd(GetYYYYMMDD()) - 60000), currentFirstHalfVWAP, RTHclose[1]);
def oneBackFirstHalfVWAP = If(RTHclose != RTHclose[1], RTHclose[1], oneBackFirstHalfVWAP[1]);
def twoBackFirstHalfVWAP = If(oneBackFirstHalfVWAP != oneBackFirstHalfVWAP[1], oneBackFirstHalfVWAP[1], twoBackFirstHalfVWAP[1]);

plot res = isNaN(oneBackFirstHalfVWAP);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
292 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