High/Low based on number of candles

Bill Belt

New member
Is there any Thinkscript code that will draw a line based on user define number of candles? I can find based on a time period but I'm looking for high and low based on candles.
 

SleepyZ

Well-known member
Is there any Thinkscript code that will draw a line based on user define number of candles? I can find based on a time period but I'm looking for high and low based on candles.

I used the volumeprofile study, set to bar, to have an input bars and to plot the profilehigh and profilelow. No need to reinvent the wheel if it works for your request. The chart below is set to 39 bars with the extended hours turned off. Also, added are value bubbles and vertical lines.

Screenshot-2021-08-20-192725.jpg
Ruby:
input bars = 5;

input pricePerRowHeightMode = { AUTOMATIC, default TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default BAR};
input onExpansion = no;
input profiles = 1000;

def period;

switch (timePerProfile) {
case BAR:
    period = BarNumber() - 1;
}

def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % bars else count[1], 0);
def cond = count < count[1] + period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = 1000, "pricePerRow" = height, "value area percent" = 0);
def con = CompoundValue(1, onExpansion, no);

def hProfile = if IsNaN(vol.GetHighest()) and con then hProfile[1] else vol.GetHighest();
def lProfile = if IsNaN(vol.GetLowest()) and con then lProfile[1] else vol.GetLowest();
def plotsDomain = IsNaN(close) == onExpansion;
def ProfileHigh = if plotsDomain then hProfile else Double.NaN;
def ProfileLow = if plotsDomain then lProfile else Double.NaN;

plot hrange = ProfileHigh;
plot lrange = ProfileLow;
hrange.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lrange.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

input showbubbles = yes;
AddChartBubble(showbubbles and hrange != hrange[-1], hrange, AsText(hrange), Color.LIGHT_GREEN);
AddChartBubble(showbubbles and lrange != lrange[-1], lrange, AsText(lrange), Color.LIGHT_RED, up = no);

input showverticalline = yes;
AddVerticalLine(showverticalline and hrange != hrange[1], "", Color.BLUE, stroke = Curve.FIRM);
 

Bill Belt

New member
I used the volumeprofile study, set to bar, to have an input bars and to plot the profilehigh and profilelow. No need to reinvent the wheel if it works for your request. The chart below is set to 39 bars with the extended hours turned off. Also, added are value bubbles and vertical lines.
Thank you. I'll give it a try
 

Bill Belt

New member
I'm getting the bubbles but not the horizontal lines. Any idea what I don't have set right? The Style is dimmed out so I can't choose the line if that means anything. I tried to post a screen shot but it would only accept a URL so I can't show you my settings. Appreciate the help.
 

SleepyZ

Well-known member
I'm getting the bubbles but not the horizontal lines. Any idea what I don't have set right? The Style is dimmed out so I can't choose the line if that means anything. I tried to post a screen shot but it would only accept a URL so I can't show you my settings. Appreciate the help.
The plots of the lines are hrange and lrange. I have added some color and lineweight code in case you are having issues. Also, I centered the bubbles on the middle of the lines and added a bubblemover which you can use to move the bubbles left/right;

Here is a share link for the chart. https://tos.mx/EUPNQRY

Screenshot-2021-08-15-064611.jpg
Ruby:
input bars = 5;

input pricePerRowHeightMode = { AUTOMATIC, default TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default BAR};
input onExpansion = no;
input profiles = 1000;

def period;

switch (timePerProfile) {
case BAR:
    period = BarNumber() - 1;
}

def count = CompoundValue(1, if period != period[1] then (count[1] + period - period[1]) % bars else count[1], 0);
def cond = count < count[1] + period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = 1000, "pricePerRow" = height, "value area percent" = 0);
def con = CompoundValue(1, onExpansion, no);

def hProfile = if IsNaN(vol.GetHighest()) and con then hProfile[1] else vol.GetHighest();
def lProfile = if IsNaN(vol.GetLowest()) and con then lProfile[1] else vol.GetLowest();
def plotsDomain = IsNaN(close) == onExpansion;
def ProfileHigh = if plotsDomain then hProfile else Double.NaN;
def ProfileLow = if plotsDomain then lProfile else Double.NaN;

plot hrange = ProfileHigh;
plot lrange = ProfileLow;
hrange.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lrange.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hrange.setdefaultColor(color.green);
lrange.setdefaultColor(color.red);
hrange.setlineWeight(2);
lrange.setlineweight(2);

input bubblemover = 0;
def bct = if hrange != hrange[1] then 1 else if bct[1] <= bars then bct[1] + 1 else bct[1];
input showbubbles = yes;
AddChartBubble(showbubbles and bct == Floor(bars / 2) + bubblemover, hrange, AsText(hrange), Color.LIGHT_GREEN);
AddChartBubble(showbubbles and bct == Floor(bars / 2) + bubblemover, lrange, AsText(lrange), Color.LIGHT_RED, up = no);

input showverticalline = yes;
AddVerticalLine(showverticalline and hrange != hrange[1], "", Color.BLUE, stroke = Curve.FIRM);
 

Bill Belt

New member
The plots of the lines are hrange and lrange. I have added some color and lineweight code in case you are having issues. Also, I centered the bubbles on the middle of the lines and added a bubblemover which you can use to move the bubbles left/right;

Here is a share link for the chart. https://tos.mx/EUPNQRY
Thanks. Looks like I'll need to call support on Monday. Bubble show up but no horizontal lines even with the new code. I updated my Java as well as downloading a new TOS but the same results. Not sure why mine is different and it won't put the horizontal lines on the chart.
 

SleepyZ

Well-known member
Thanks. Looks like I'll need to call support on Monday. Bubble show up but no horizontal lines even with the new code. I updated my Java as well as downloading a new TOS but the same results. Not sure why mine is different and it won't put the horizontal lines on the chart.
Did you try loading the share link that I included. That should give you another attempt with the settings from my grid, in case it is something with the settings. Also, open the script settings and make sure that hrange and lrange show plots are checked.
 

Similar threads

Top