FOLD Question

VIP_TOS

Member
Can someone please assist me in what I have wrong here. I have FOLD used for 2 different formulas.
The FIRST one works halfway and the SECOND one works perfect.

FIRST FOLD Formula - Captures 1 Hour TF (High - Low) for specific period in time defined with SecondsTillTime / it Works on 1-hour Chart BUT Not on a Lower Time Frame chart.

SECOND FOLD Formula - Captures the SUM of ALL (High - Low) for the "Lookback" length. This works perfect on 1 Hour time frame and also works perfect on the lower time frame based off the 1-hour data.


What i am trying to accomplish is the FIRST FOLD where it captures 1-hour time frame (High - Low) for a specific period in time defined by SecondsTillTime on a lower time frame. The FIRST FOLD Formula captures it on the higher TF but not lower. I am looking to capture it on a lower TF as well.

*The SECOND FOLD Formula was to just confirm that it is possible to capture FOLD data from higher TF on a lower TF.

FOLD is a difficult one to master, but I am learning through trial and error along with the much-appreciated help on this forum!

See code along with picture reference below


Ruby:
#aggregation set to HOUR for FOLD High - Low
def H     = High(period=aggregationPeriod.HOUR);
def L     = Low (period=aggregationPeriod.HOUR);
def Close = Close(period=aggregationPeriod.HOUR);

# High - Low
def HL = H-L;

# Candle Lookback period
def LookBack = 30 ;

# Used For First FOLD
def Time_Frame_9AM_HL  =  if SecondsTillTime(900)  <= 0 and SecondsTillTime(1000) > 0  then HL else 0;
def Time_Frame_10AM_HL =  if SecondsTillTime(1000) <= 0 and SecondsTillTime(1100) > 0  then HL else 0;


# FIRST FOLD Captures 1 Hour TF High - Low / it Works on 1 Hour Chart but Not on Lower Time Frames
def SUM_Time_Frame_9AM   = fold a = 0 to LookBack with  b do b + GetValue(Time_Frame_9AM_HL,  a );
def SUM_Time_Frame_10AM  = fold c = 0 to LookBack with  d do d + GetValue(Time_Frame_10AM_HL, c );

addLabel(1, "9AM  High - Low SUM   = "  + round(SUM_Time_Frame_9AM  ,0), color.CYAN);
addLabel(1, "10AM High - Low SUM   = "  + round(SUM_Time_Frame_10AM ,0), color.CYAN);


# SECOND Fold Captures the SUM of ALL High Low for the "Lookback" length
def High_Low_Sum_All = fold g = 0 to LookBack with j do j + If(GetValue(Close <> Close[1], g), GetValue(HL, g), 0);
addLabel(1, "ALL High - Low SUM = "  + round(High_Low_Sum_All,0), color.green);
 

Attachments

  • tos.png
    tos.png
    283.3 KB · Views: 130
Solution
Can someone please assist me in what I have wrong here. I have FOLD used for 2 different formulas.
The FIRST one works halfway and the SECOND one works perfect.

FIRST FOLD Formula - Captures 1 Hour TF (High - Low) for specific period in time defined with SecondsTillTime / it Works on 1-hour Chart BUT Not on a Lower Time Frame chart.

SECOND FOLD Formula - Captures the SUM of ALL (High - Low) for the "Lookback" length. This works perfect on 1 Hour time frame and also works perfect on the lower time frame based off the 1-hour data.


What i am trying to accomplish is the FIRST FOLD where it captures 1-hour time frame (High - Low) for a specific period in time defined by SecondsTillTime on a lower time frame. The FIRST FOLD...
Can someone please assist me in what I have wrong here. I have FOLD used for 2 different formulas.
The FIRST one works halfway and the SECOND one works perfect.

FIRST FOLD Formula - Captures 1 Hour TF (High - Low) for specific period in time defined with SecondsTillTime / it Works on 1-hour Chart BUT Not on a Lower Time Frame chart.

SECOND FOLD Formula - Captures the SUM of ALL (High - Low) for the "Lookback" length. This works perfect on 1 Hour time frame and also works perfect on the lower time frame based off the 1-hour data.


What i am trying to accomplish is the FIRST FOLD where it captures 1-hour time frame (High - Low) for a specific period in time defined by SecondsTillTime on a lower time frame. The FIRST FOLD Formula captures it on the higher TF but not lower. I am looking to capture it on a lower TF as well.

*The SECOND FOLD Formula was to just confirm that it is possible to capture FOLD data from higher TF on a lower TF.

FOLD is a difficult one to master, but I am learning through trial and error along with the much-appreciated help on this forum!

See code along with picture reference below


Ruby:
#aggregation set to HOUR for FOLD High - Low
def H     = High(period=aggregationPeriod.HOUR);
def L     = Low (period=aggregationPeriod.HOUR);
def Close = Close(period=aggregationPeriod.HOUR);

# High - Low
def HL = H-L;

# Candle Lookback period
def LookBack = 30 ;

# Used For First FOLD
def Time_Frame_9AM_HL  =  if SecondsTillTime(900)  <= 0 and SecondsTillTime(1000) > 0  then HL else 0;
def Time_Frame_10AM_HL =  if SecondsTillTime(1000) <= 0 and SecondsTillTime(1100) > 0  then HL else 0;


# FIRST FOLD Captures 1 Hour TF High - Low / it Works on 1 Hour Chart but Not on Lower Time Frames
def SUM_Time_Frame_9AM   = fold a = 0 to LookBack with  b do b + GetValue(Time_Frame_9AM_HL,  a );
def SUM_Time_Frame_10AM  = fold c = 0 to LookBack with  d do d + GetValue(Time_Frame_10AM_HL, c );

addLabel(1, "9AM  High - Low SUM   = "  + round(SUM_Time_Frame_9AM  ,0), color.CYAN);
addLabel(1, "10AM High - Low SUM   = "  + round(SUM_Time_Frame_10AM ,0), color.CYAN);


# SECOND Fold Captures the SUM of ALL High Low for the "Lookback" length
def High_Low_Sum_All = fold g = 0 to LookBack with j do j + If(GetValue(Close <> Close[1], g), GetValue(HL, g), 0);
addLabel(1, "ALL High - Low SUM = "  + round(High_Low_Sum_All,0), color.green);

i have no idea what you are trying to do.
are you trying to do one of these?
1. when the current candle is within a desired time period, look back 30 bars ( using 2nd aggregation at 1 hour) , and add up some data from 30 previous candles.
2. add up data only from 30 previous candles within a desired time period.


if data doesn't look right, add a bubble to display some values.
add this to end of your study

Code:
addchartbubble(1, low,
SUM_Time_Frame_9AM + "\n" +
SUM_Time_Frame_10AM + "\n" +
High_Low_Sum_All
, color.cyan, no);

def t1 = if SecondsTillTime(900) == 0 then 1 else 0;
def t2 = if SecondsTillTime(1000) == 0 then 1 else 0;

addchartbubble(t1 or t2, high,
( if t1 then "9am" else if t2 then "10am" else "")
, color.yellow, yes);


observations,
maybe it has something to do with 9am EST is before normal trading hours? do you have extended hours on?
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/SecondsTillTime

if a chart is set to 1 hour , and ext hours are off, the candle times will be at the bottom of the hour, 9:30, 10:30, 11:30,...
 
Solution

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

i have no idea what you are trying to do.
are you trying to do one of these?
1. when the current candle is within a desired time period, look back 30 bars ( using 2nd aggregation at 1 hour) , and add up some data from 30 previous candles.
2. add up data only from 30 previous candles within a desired time period.


if data doesn't look right, add a bubble to display some values.
add this to end of your study

Code:
addchartbubble(1, low,
SUM_Time_Frame_9AM + "\n" +
SUM_Time_Frame_10AM + "\n" +
High_Low_Sum_All
, color.cyan, no);

def t1 = if SecondsTillTime(900) == 0 then 1 else 0;
def t2 = if SecondsTillTime(1000) == 0 then 1 else 0;

addchartbubble(t1 or t2, high,
( if t1 then "9am" else if t2 then "10am" else "")
, color.yellow, yes);


observations,
maybe it has something to do with 9am EST is before normal trading hours? do you have extended hours on?
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/SecondsTillTime

if a chart is set to 1 hour , and ext hours are off, the candle times will be at the bottom of the hour, 9:30, 10:30, 11:30,...

Thank you for the reply halcyonguy,

My desired is more towered your first response "1. when the current candle is within a desired time period, look back 30 bars ( using 2nd aggregation at 1 hour) , and add up some data from 30 previous candles."

I tried your other solutions with no luck.

The problem and solution I need is i believe is my def LookBack 30 is counting 30 candles on a 1 min chart when I need to it count 30 candles on the 1 hr 2nd aggregation chart.
 
Thank you for the reply halcyonguy,

My desired is more towered your first response "1. when the current candle is within a desired time period, look back 30 bars ( using 2nd aggregation at 1 hour) , and add up some data from 30 previous candles."

I tried your other solutions with no luck.

The problem and solution I need is i believe is my def LookBack 30 is counting 30 candles on a 1 min chart when I need to it count 30 candles on the 1 hr 2nd aggregation chart.

I am not sure if you were just practicing with using Fold, or was looking to use the results in your labels for analysis. If the latter, the following code, which does not use Fold, may help.

It uses a snippet to identify each day's candles on your chart.
Please use a 90 day chart for your 1hour and also the lower timeframes to get a better match between both.
It uses a definition of Hour_ending bars that are equal to 0 to be able to use your HL hourly aggregationperiod. It is not exact as there are some bars on the lower timeframe that are not active on the Hour. I did not take the time to try to capture those at this time.

Capture.jpg
Ruby:
#aggregation set to HOUR for FOLD High - Low
def H     = high(period = AggregationPeriod.HOUR);
def L     = low (period = AggregationPeriod.HOUR);
def Close = close(period = AggregationPeriod.HOUR);

# High - Low
def HL = H - L;

# Candle Lookback period
def LookBack = 30;
def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(Close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

# First Captures Select Hourly Period HL Sums

def Sum_Time_Frame_9AM   = if thisDay <= LookBack and  SecondsTillTime(1000) == 0
                           then Sum_Time_Frame_9AM[1] + HL
                           else Sum_Time_Frame_9AM[1];
def Sum_Time_Frame_10AM  = if thisDay <= LookBack and  SecondsTillTime(1100) == 0
                           then Sum_Time_Frame_10AM[1] + HL
                            else Sum_Time_Frame_10AM[1];

AddLabel(1, "9AM  High - Low SUM   = "  + Round(Sum_Time_Frame_9AM, 0), Color.CYAN);
AddLabel(1, "10AM High - Low SUM   = "  + Round(Sum_Time_Frame_10AM , 0), Color.CYAN);

# SECOND Captures the SUM of ALL High Low for the "Lookback" length

# Define Hour_ending on Lower Timeframes as Hour_ending == 0
def Hour_ending = (Round(SecondsFromTime(0) / 3600, 0) - (Round(SecondsFromTime(0) / 3600, 2)));

def High_Low_Sum_All = if thisDay <= 30 and Hour_ending == 0 or
                       (Hour_ending[1] > 0 and Hour_ending < 0)
                       then High_Low_Sum_All[1] + HL
                       else High_Low_Sum_All[1];
AddLabel(1, "ALL High - Low SUM = "  + Round(High_Low_Sum_All, 0), Color.GREEN);

input debug = no;
AddChartBubble(debug, low - .05, Hour_ending + "\n" + HL + "\n" + Round(High_Low_Sum_All, 3), Color.WHITE, no);
 
SleepyZ, Thank you for the help & reply! A little of both I am new to this & learning. I tired your code & it almost is correct. it seems as if one bar is off though? Your 9am seems to be 10am and 10am seems to be 11am.
I have been trying to get a solution that Mobius suggested to me. That was to use a bar counter and AggregationPeriod.HOUR / GetAggregationPeriod(); to get periods. I got it to work on plots as it will plot high & low for the given time frame. the problem is my FOLD sums the first period times the number of lookback periods given. it is not getting the sum of all set periods for some reason. Maybe you can assist on what I have wrong, or how would I add compound value to make this work? as you can see from the pics below the 9am High - low candle is 132.25 I set my fold to lookback 2 and it added 132.25 + 132.25 for a total of 264.5 instead of adding the next 9am period that is 159.
TOD1.png
tod3.png

Ruby:
#BAR COUNTER
input Time_Start = 900;
def Bar_Count = AggregationPeriod.HOUR / GetAggregationPeriod();

def Bar_Number = BarNumber();

def Start_Bar = if SecondsFromTime(Time_Start) == 0 then Bar_Number else Start_Bar[1];
def Start_Bar_Counter = if Between(Bar_Number, Start_Bar , Start_Bar + Bar_Count) then 1 else 0;

def High_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then high else if Start_Bar_Counter == 1 then Max(high, High_A [1]) else 0;
def High_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then High_A [1] else High_B[1];

def Low_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then low else if Start_Bar_Counter == 1 then Min(low, Low_A[1]) else 0 ;
def Low_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then Low_A[1] else Low_B[1];

#FOLD
def LookBack = 2;

def Sum_High_Period = fold A = 0 to LookBack with B do B + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B, A), GetValue(High_B, A),0);
def Sum_Low_Period = fold C = 0 to LookBack with m do m + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B,C), GetValue(Low_B, C),0);

def High_Low = Sum_High_Period -Sum_Low_Period;

#PLOTS
plot p_High_B = if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B;
p_High_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_High_B.SetDefaultColor(Color.GREEN);

plot p_Low_B = if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B ;
p_Low_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_Low_B.SetDefaultColor(Color.RED);


#LABELS
def H_L = High_B - Low_B;

AddLabel(1, High_B , Color.green);
AddLabel(1, Low_B, Color.red);
AddLabel(1, H_L, Color.CYAN);

AddLabel(1,"FOLD High-LOW = " + High_Low, Color.yellow);
 

Attachments

  • TOD1.png
    TOD1.png
    10.4 KB · Views: 138
Last edited by a moderator:
I am not sure if you were just practicing with using Fold, or was looking to use the results in your labels for analysis. If the latter, the following code, which does not use Fold, may help.

It uses a snippet to identify each day's candles on your chart.
Please use a 90 day chart for your 1hour and also the lower timeframes to get a better match between both.
It uses a definition of Hour_ending bars that are equal to 0 to be able to use your HL hourly aggregationperiod. It is not exact as there are some bars on the lower timeframe that are not active on the Hour. I did not take the time to try to capture those at this time.

This does gives me what I am trying to accomplish. The only downfall is as you mentioned 90 day / longer time is needed to reference data. When using a 1min chart the longer time frame slows up the chart load time. I believe if fold can be accomplished somehow a longer time frame wouldn't need to be populated? If so can your solution incorporate fold or could my last post be fixed to run fold properly to fix this ?
 
This does gives me what I am trying to accomplish. The only downfall is as you mentioned 90 day / longer time is needed to reference data. When using a 1min chart the longer time frame slows up the chart load time. I believe if fold can be accomplished somehow a longer time frame wouldn't need to be populated? If so can your solution incorporate fold or could my last post be fixed to run fold properly to fix this ?
I am not an expert on Fold, so I would have to leave that to others. I would suggest that a Fold is going to be dependent as did the method I used on the data that is shown on the chart.

You may get by with 30 days for both 1hour and lower timeframes, but you would have to test the results for usefulness.

The main problem in my opinion was your wanting to use an hourly aggregation period and then have the lower timeframe (5min chart) match the results that you get on the same code on a 1hour chart. There is often periods when data is not available on the lower timeframe at the hourly times, especially outside regular trading hours, causing the need to approximate those hourly bars, if possible.
 
Thank you SleepyZ & halcyonguy for the help! Hopefully a FOLD expert can
jump in and assist on what I am doing wrong. As it stands I got FOLD to do a calculation, but what it is doing is input value = "lookback" * first period (High-Low). It is not doing what I want it to do and this is value = "lookback" + all lookback periods.

*On a 1 min chart.
*Looking for 1hr Specific Time Frame High-Low SUM using FOLD


Correct Scenario what I am looking for

input value = "lookback" = 3


Day 1 - 9am - 10am High - Low = 132
Day 2 - 9am - 10am High - Low = 159
Day 3 - 9am - 10am High - Low = 140
Total Sum = 431 <<< Looking For This Number

Below is Wrong Scenario and what is happening

input value = "lookback" = 3

Day 1 - 9am - 10am High - Low = 132
Should Sum Day 2 - Day 1 - 9am - 10am High - Low = 132
Should Sum Day 3 - Day 1 - 9am - 10am High - Low = 132
Total Sum = 396 <<<< Not The Correct Sum


*If I set input lookback to say 30-40 or 50 is should SUM all the defined periods together without have to load I longer timeframe on
the 1 min chart.



Ruby:
#BAR COUNTER
input Time_Start = 900;
def Bar_Count = AggregationPeriod.HOUR / GetAggregationPeriod();

def Bar_Number = BarNumber();

def Start_Bar = if SecondsFromTime(Time_Start) == 0 then Bar_Number else Start_Bar[1];
def Start_Bar_Counter = if Between(Bar_Number, Start_Bar , Start_Bar + Bar_Count) then 1 else 0;

def High_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then high else if Start_Bar_Counter == 1 then Max(high, High_A [1]) else 0;
def High_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then High_A [1] else High_B[1];

def Low_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then low else if Start_Bar_Counter == 1 then Min(low, Low_A[1]) else 0 ;
def Low_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then Low_A[1] else Low_B[1];

#FOLD
input LookBack = 2;

def Sum_High_Period = fold A = 0 to LookBack with B do B + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B, A), GetValue(High_B, A),0);
def Sum_Low_Period = fold C = 0 to LookBack with m do m + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B,C), GetValue(Low_B, C),0);

def High_Low = Sum_High_Period -Sum_Low_Period;

#PLOTS
plot p_High_B = if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B;
p_High_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_High_B.SetDefaultColor(Color.GREEN);

plot p_Low_B = if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B ;
p_Low_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_Low_B.SetDefaultColor(Color.RED);


#LABELS
def H_L = High_B - Low_B;

#AddLabel(1, High_B , Color.green);
#AddLabel(1, Low_B, Color.red);
#AddLabel(1, H_L, Color.CYAN);

AddLabel(1,"FOLD High-LOW = " + High_Low, Color.yellow);
 
Thank you SleepyZ & halcyonguy for the help! Hopefully a FOLD expert can
jump in and assist on what I am doing wrong. As it stands I got FOLD to do a calculation, but what it is doing is input value = "lookback" * first period (High-Low). It is not doing what I want it to do and this is value = "lookback" + all lookback periods.

*On a 1 min chart.
*Looking for 1hr Specific Time Frame High-Low SUM using FOLD


Correct Scenario what I am looking for

input value = "lookback" = 3


Day 1 - 9am - 10am High - Low = 132
Day 2 - 9am - 10am High - Low = 159
Day 3 - 9am - 10am High - Low = 140
Total Sum = 431 <<< Looking For This Number

Below is Wrong Scenario and what is happening

input value = "lookback" = 3

Day 1 - 9am - 10am High - Low = 132
Should Sum Day 2 - Day 1 - 9am - 10am High - Low = 132
Should Sum Day 3 - Day 1 - 9am - 10am High - Low = 132
Total Sum = 396 <<<< Not The Correct Sum


*If I set input lookback to say 30-40 or 50 is should SUM all the defined periods together without have to load I longer timeframe on
the 1 min chart.



Ruby:
#BAR COUNTER
input Time_Start = 900;
def Bar_Count = AggregationPeriod.HOUR / GetAggregationPeriod();

def Bar_Number = BarNumber();

def Start_Bar = if SecondsFromTime(Time_Start) == 0 then Bar_Number else Start_Bar[1];
def Start_Bar_Counter = if Between(Bar_Number, Start_Bar , Start_Bar + Bar_Count) then 1 else 0;

def High_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then high else if Start_Bar_Counter == 1 then Max(high, High_A [1]) else 0;
def High_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then High_A [1] else High_B[1];

def Low_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then low else if Start_Bar_Counter == 1 then Min(low, Low_A[1]) else 0 ;
def Low_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then Low_A[1] else Low_B[1];

#FOLD
input LookBack = 2;

def Sum_High_Period = fold A = 0 to LookBack with B do B + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B, A), GetValue(High_B, A),0);
def Sum_Low_Period = fold C = 0 to LookBack with m do m + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B,C), GetValue(Low_B, C),0);

def High_Low = Sum_High_Period -Sum_Low_Period;

#PLOTS
plot p_High_B = if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B;
p_High_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_High_B.SetDefaultColor(Color.GREEN);

plot p_Low_B = if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B ;
p_Low_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_Low_B.SetDefaultColor(Color.RED);


#LABELS
def H_L = High_B - Low_B;

#AddLabel(1, High_B , Color.green);
#AddLabel(1, Low_B, Color.red);
#AddLabel(1, H_L, Color.CYAN);

AddLabel(1,"FOLD High-LOW = " + High_Low, Color.yellow);
Well I am for sure no expert but I was playing with this out of curiosity and think I have some insight for you. Somebody please correct me if anything I say is incorrect. I believe the way the scripts work is they run for every bar, and a FOLD is basically a "for" loop that will run however many times specified. So in the above issue the "incorrect sum" was because the numbers it was calculating were true for the previous amount of "lookback" bars.

This code:
input LookBack = 2;

def Sum_High_Period = fold A = 0 to LookBack with B do B + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B, A), GetValue(High_B, A),0);
def Sum_Low_Period = fold C = 0 to LookBack with m do m + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else Low_B,C), GetValue(Low_B, C),0);
is telling the fold to run "lookback" amount of times and pulling the value of High_B/Low_B for those bars, which has been the same value through the day.

So I started thinking in terms of pulling that information from as many bars ago to get the data you were looking for with some success but ran into a few KEY issues in being able to keep it accurate. I added code to break things down to the 1min and count how many bars there should be based on whatever time frame you are on, HOWEVER my intraday charts with extended hours have different amounts of bars for different 24hr periods. I assume this is due to lack of price movement, but obviously poses a challenge if counting x bars back. Below is a code that will do what you are looking for but only during regular trading hours (no extended) because that's the only time the number of bars by time frame can stay the same. I tried to maintain most of your original code. I couldn't come up with a solution to the varying number of bars or a simpler way to involve "day checking" so I kept it simple and functional, maybe that code from @SleepyZ above could hold the key but I haven't had much time to look into it, hope this helps!

Side note: I think I remember some people asking about sectioning off parts of the day for different scripts(like volume at a certain time?) maybe this can help them as well.


Code:
##TTSMikeC 6/8/2022 using code by VIP_TOS of usethinkscript.com



#BAR COUNTER
input Time_Start = 930;
input Opening_Range_TF = aggregationPeriod.THIRTY_MIN;


def Bar_Count = Opening_Range_TF / GetAggregationPeriod();
def Bar_Number = BarNumber();
def Start_Bar = if SecondsFromTime(Time_Start) == 0 then Bar_Number else Start_Bar[1];
def Start_Bar_Counter = if Between(Bar_Number, Start_Bar , Start_Bar + Bar_Count) then 1 else 0;
def mins =  getAggregationPeriod()/ (1000*60); #TF to minutes
def BarsPerDay = 390/mins; #Trading minutes/day


def High_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then high else if Start_Bar_Counter == 1 then Max(high, High_A[1]) else 0;
def High_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then High_A[1] else High_B[1];

def Low_A = if Start_Bar_Counter[1] == 0 and Start_Bar_Counter == 1 then low else if Start_Bar_Counter == 1 then Min(low, Low_A[1]) else 0 ;
def Low_B = if Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0 then Low_A[1] else Low_B[1];

#FOLD
input LookBack = 3;

def Sum_High_Period = fold A = 0 to Lookback with B do B + GetValue(High_B, BarsPerDay * A ) ;

def Sum_Low_Period = fold C = 0 to Lookback with m do m + GetValue(Low_B, BarsPerDay * C ) ;

def High_Low = Sum_High_Period -Sum_Low_Period;

#PLOTS
plot p_High_B = High_B;
p_High_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_High_B.SetDefaultColor(Color.GREEN);

plot p_Low_B = Low_B ;
p_Low_B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
p_Low_B.SetDefaultColor(Color.RED);


#LABELS
def H_L = High_B - Low_B;
def HLL = Sum(H_L, lookback);
#AddLabel(1, High_B , Color.green);
#AddLabel(1, Low_B, Color.red);
#AddLabel(1, H_L, Color.CYAN);
AddLabel(1,"FOLD High-LOW = " + Round(High_Low,2), Color.yellow);

## For testing, can be removed
addchartBubble( Start_Bar_Counter[1] == 1 and Start_Bar_Counter == 0, (low_B + ((High_B - Low_B) / 2)), (High_B - Low_B));



#END
@VIP_TOS
 
TTs_MikeC,
Thank you for taking the time help! The attached picture hold truth to what you’re saying I believe. I changed the script around (see below) a bit to pull the High - Low directly from the aggregationPeriod.Hour chart on a 1 min chart. As you see it populated the Hourly High-Low correct during the defined time period on the 1 min chart but the value prints each time per minute bar it looks. That must be why it is calculating the same High-Low sum over and over on the fold loop length. There must be a correct FOLD solution out there somewhere :(

I must say i by no means am close to being an expert on thinkscript, but I have learned so much over the past couple months of starting this from the great knowledgeable people on this forum. Thank you all for the help you offer & valuable information you post!

Ruby:
def Hourly_Time_Frame = if SecondsFromTime(900) >= 0 and SecondsFromTime(1000) < 0 then 1 else double.NaN;

def Hour_High = high(period = AggregationPeriod.HOUR);
def Hour_Low  = low(period = AggregationPeriod.HOUR);

plot Time_Frame_Sum = fold i = 0 to 10 with n do if GetValue(((Hourly_Time_Frame==1)), i) then GetValue((Hour_High-Hour_Low), i)  else double.NaN ;
Time_Frame_Sum.SetPaintingStrategy(PaintingStrategy.valuES_ABOVE);

AddLabel(1, round(Time_Frame_Sum,0) , Color.yellow);


FOLD.png
 
SleepyZ, Thank you for the help & reply! A little of both I am new to this & learning. I tired your code & it almost is correct. it seems as if one bar is off though? Your 9am seems to be 10am and 10am seems to be 11am.
I have been trying to get a solution that Mobius suggested to me. That was to use a bar counter and AggregationPeriod.HOUR / GetAggregationPeriod(); to get periods. I got it to work on plots as it will plot high & low for the given time frame. the problem is my FOLD sums the first period times the number of lookback periods given. it is not getting the sum of all set periods for some reason. Maybe you can assist on what I have wrong, or how would I add compound value to make this work? as you can see from the pics below the 9am High - low candle is 132.25 I set my fold to lookback 2 and it added 132.25 + 132.25 for a total of 264.5 instead of adding the next 9am period that is 159.

some advice, stop trying to force a specific function to become the answer. (fold)

list out what needs to be done, then expand on the task list, then some more. until you have a specific list from which you can create some code for each sentence.
collect the desired data , ( high , low)
from the desired time period, ( 9 to 10)
on the desired days, ( past LookBack days)
and subtract one from the other,
and add them up for the past lookback days.


please go back to post#4 and reread what sleepyz wrote. it should work. just need to change the start and stop times, to be converted to the desired EST times.


i think you want to collect data from a specific 1 hour period, on multiple days.
your fold isn't working, because it is looking back 'lookback' hours not 'lookback' days.

from post5
def Sum_High_Period = fold A = 0 to LookBack with B do B + If(GetValue(if Start_Bar_Counter or secondsFromTime(Time_Start) == 0 then Double.NaN else High_B, A), GetValue(High_B, A),0);

if you want to loop through several days and just pull the desired data, you will need enough bars in the loop to span the days.
with 1 hour periods, there are 6.5 hours in a trading day. you can't have a half bar, so there will be 7 bars per day.
if you change the loop 'to' number, it might work,

def Sum_High_Period = fold A = 0 to ( LookBack * 7 )
 
@ TTs_MikeC it is solely for FUTURES trading where they trade for 23 hr a day. So I am looking for 1hr defined periods like 9-10, 10-11 and so on



@ halcyonguy Thank you for the advice! post4 does work but it loads data from the current chart so longer periods slow load time on my chart. From my limited experience with FOLD is that it doesn’t need to pull data from current chart so I can have my 1 min chart open with 1-2 day & pull data further aways with FOLD where as in post4 solution I cant as its using the current bars on the loaded chart.

I now have the basics down in thinkscript so I moved on to the more complex stuff & have been experimenting with FOLD. Again, nowhere near an expert but I do now have a much better understanding of how FOLD works from this. your comment on “looking back 'lookback' hours not 'lookback' days" makes since. Thank you again!
 
@ TTs_MikeC it is solely for FUTURES trading where they trade for 23 hr a day. So I am looking for 1hr defined periods like 9-10, 10-11 and so on



@ halcyonguy Thank you for the advice! post4 does work but it loads data from the current chart so longer periods slow load time on my chart. From my limited experience with FOLD is that it doesn’t need to pull data from current chart so I can have my 1 min chart open with 1-2 day & pull data further aways with FOLD where as in post4 solution I cant as its using the current bars on the loaded chart.

I now have the basics down in thinkscript so I moved on to the more complex stuff & have been experimenting with FOLD. Again, nowhere near an expert but I do now have a much better understanding of how FOLD works from this. your comment on “looking back 'lookback' hours not 'lookback' days" makes since. Thank you again!

go here and follow the link to jshingler...
https://usethinkscript.com/threads/loops-while.9558/#post-86805
many good examples , including using fold.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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