Dates not working

british43

Member
VIP
Can someone please tell me why my date range not working?

Ruby:
input LookBack = 63;#hint LookBack: Number of bars to look back

def start = !IsNan(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = IsNaN(close[-1]);

def Startdate = if Start then getYyyyMmDd() else Startdate[1];
def enddate = if end then getYyyyMmDd() else enddate[1];


####################################################################

def V = volume;

def VRange = CompoundValue(1,
                   if DaysFromDate(startDate) >= 0
                   and DaysTillDate(endDate) >= 0
                   then VRange[1] + V
                   else VRange[1], 0);

plot vol = vrange;
 
Can someone please tell me why my date range not working?

Ruby:
input LookBack = 63;#hint LookBack: Number of bars to look back

def start = !IsNan(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = IsNaN(close[-1]);

def Startdate = if Start then getYyyyMmDd() else Startdate[1];
def enddate = if end then getYyyyMmDd() else enddate[1];


####################################################################

def V = volume;

def VRange = CompoundValue(1,
                   if DaysFromDate(startDate) >= 0
                   and DaysTillDate(endDate) >= 0
                   then VRange[1] + V
                   else VRange[1], 0);

plot vol = vrange;

maybe this will get you closer to what you want to see

i see this error.
date value can't be less than 19020101
look in the upper left corner of the chart. is there a circle with a !.
click it and read the error.
i tried adding some conditions in an if then, to fix it, but it didn't work.

you have a variable called lookback, but you are using negative offsets.
negative offsets look into the future.

i added vertical lines to verify the start and end values.
the end formula is true on every bar after the last bar.
added a 2nd condition to fix it.

if you were trying to save a date, i assumed you are looking at a day chart. so each bar is 1 day.
i added some code lines to find the barnumber of the start and end.
then checked if the current bar is between those barnumbers, add up the volume.

i added highestall() so startbn and endbn will have the correct values on every bar, and not cause false triggers.


Ruby:
# vol_for_xdays_0

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

input LookBack = 63;#hint LookBack: Number of bars to look back

def start = !IsNan(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = !IsNaN(close[0]) and IsNaN(close[-1]);

def startbn2 = if start then bn else 0;
def startbn = highestall(startbn2);
def endbn = startbn + LookBack;

#def Startdate = if bn == 1 then na else if Start then getYyyyMmDd() else Startdate[1];
#def enddate = if bn == 1 then na else if end then getYyyyMmDd() else enddate[1];

def V = volume;
#def VRange = CompoundValue(1,
#                   if DaysFromDate(startDate) >= 0
#                   and DaysTillDate(endDate) >= 0
#                   then VRange[1] + V
#                   else VRange[1], 0);

def VRange = if bn == 1 then 0 else if bn >= startbn and bn <= endbn then VRange[1] + V  else VRange[1];
plot vol = vrange;

#-----------------------------------------------

addverticalline(start, "  START", color.cyan);
addverticalline(end, "  END", color.cyan);

addchartbubble(0, low,
start + "\n" +
end + "\n" 
#vol
, color.cyan, no);
#

addchartbubble(0, low, 
bn + "\n" +
startbn + "\n" +
endbn
, color.cyan, no);
#
 
@halcyonguy This works. I'm trying us the code above to identify 10 bars within a 45° line across the chart and plot the high and low of the range with extensions. I got the angle piece down but trying to get the past bars but failed. Similar to congestion zone indicator.

Ruby:
def na = Double.NaN;
def bn = BarNumber();

input LookBack = 10;#hint LookBack: Number of bars to look back

def start = !IsNaN(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = !IsNaN(close[0]) and IsNaN(close[-1]);

def startbn2 = if start then bn else 0;
def startbn = HighestAll(startbn2);
def endbn = lowestall(startbn + LookBack);

def endbn2 = if end then bn else 0;

addverticalline(start, "  START", color.cyan);
AddVerticalLine(end, "  END", Color.CYAN);

# Without subscript
def bar = BarNumber();
def h = high; # y axis variable to project forward
def l = low;
def y = close;
def x1 =  startbn2; # starting bar of slope calculation
def x2 =  endbn2; # ending bar of slop calculation
def y1_r = if bar == x1 then y else y1_r[1];
def y2_r = if bar == x2 then l else y2_r[1];

plot "y-axis" = y1_r - y2_r;
plot "x-axis" = (startbn - (endbn));
def mx = "y-axis" / "x-axis";
plot angle = round(atan(mx) * 180 / double.Pi, 0);
 
@halcyonguy This works. I'm trying us the code above to identify 10 bars within a 45° line across the chart and plot the high and low of the range with extensions. I got the angle piece down but trying to get the past bars but failed. Similar to congestion zone indicator.

Ruby:
def na = Double.NaN;
def bn = BarNumber();

input LookBack = 10;#hint LookBack: Number of bars to look back

def start = !IsNaN(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = !IsNaN(close[0]) and IsNaN(close[-1]);

def startbn2 = if start then bn else 0;
def startbn = HighestAll(startbn2);
def endbn = lowestall(startbn + LookBack);

def endbn2 = if end then bn else 0;

addverticalline(start, "  START", color.cyan);
AddVerticalLine(end, "  END", Color.CYAN);

# Without subscript
def bar = BarNumber();
def h = high; # y axis variable to project forward
def l = low;
def y = close;
def x1 =  startbn2; # starting bar of slope calculation
def x2 =  endbn2; # ending bar of slop calculation
def y1_r = if bar == x1 then y else y1_r[1];
def y2_r = if bar == x2 then l else y2_r[1];

plot "y-axis" = y1_r - y2_r;
plot "x-axis" = (startbn - (endbn));
def mx = "y-axis" / "x-axis";
plot angle = round(atan(mx) * 180 / double.Pi, 0);


sorry, i don't know what you are trying to do.
there is no 45 degree line. there are diagonal lines, to the start bar and end bar, but horizontal lines between them


i copied the code from post 3.
i didn't see anything, so i added a bubble to the end of it and made it a lower study.
it plots 2 horizontal lines between start and end.

Code:
declare lower;

addchartbubble(1,5,
"y-axis" + " y\n" +
"x-axis" + " x\n" +
mx + " mx\n" +
angle + " a"
, color.cyan, yes);

drag the vertical axis down to see the bubbles
s7pnL1h.jpg
 
sorry, i don't know what you are trying to do.
there is no 45 degree line. there are diagonal lines, to the start bar and end bar, but horizontal lines between them


i copied the code from post 3.
i didn't see anything, so i added a bubble to the end of it and made it a lower study.
it plots 2 horizontal lines between start and end.

Code:
declare lower;

addchartbubble(1,5,
"y-axis" + " y\n" +
"x-axis" + " x\n" +
mx + " mx\n" +
angle + " a"
, color.cyan, yes);

drag the vertical axis down to see the bubbles
s7pnL1h.jpg
Sorry if I didn't explain it thoroughly. I should have hidden those line. I used zigzag for the line between start and end points. Code posted below with zigzag and labels. The idea is look across every 10 bars and see if there is an angle of -75 and plot R/S lines for the range. I used FB as an example with date range 2/24/21 - 2/24/22

L21gIDV.png


Ruby:
def priceH = high;
def priceL = low;
def priceC = close;

def o = open;
def h = high;
def l = low;
def c = close;

input percentageReversal = 2;
input absoluteReversal = 0.0;
input atrLength = 2;
input atrReversal = 1;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}


def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}



def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}


def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}


plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();
#####################################################################
##                         Change Pct                              ##
#####################################################################

def chg = round((close / close[1] -1),2);

#####################################################################
##                         Bar lookBack                            ##
#####################################################################
def na = Double.NaN;
def bn = BarNumber();

input LookBack = 10;#hint LookBack: Number of bars to look back

def start = !IsNaN(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = !IsNaN(close[0]) and IsNaN(close[-1]);

def startbn2 = if start then bn else 0;
def startbn = HighestAll(startbn2);
def endbn = lowestall(startbn + LookBack);

def endbn2 = if end then bn else 0;

def chg_pct = if bn == 1 then 0 else if bn >= startbn and bn <= endbn then chg_pct[1] + chg  else chg_pct[1];
def change = (chg_pct * 100);

addverticalline(start, "  START", color.cyan);
AddVerticalLine(end, "  END", Color.CYAN);

# Without subscript
def bar = BarNumber();
def y = close;
def x1 =  startbn2; # starting bar of slope calculation
def x2 =  endbn2; # ending bar of slop calculation
def y1_r = if bar == x1 then y else y1_r[1];
def y2_r = if bar == x2 then l else y2_r[1];

def "y-axis" = y1_r - y2_r;
def "x-axis" = (startbn - (endbn));
def mx = "y-axis" / "x-axis";
def angle = round(atan(mx) * 180 / double.Pi, 0);


AddLabel(1, "Chg %:" + " " + change, Color.CYAN);
addlabel(1, "y1: "+y1_r);
addlabel(1, "y2: "+y2_r);
AddLabel(1, "x1:" + " " + startbn, color.yellow);
AddLabel(1, "x2:" + " " + endbn, color.yellow);
AddLabel(1, "y-axis:" + " " + "y-axis", Color.orange);
AddLabel(1, "x-axis:" + " " + "x-axis", Color.orange);
AddLabel(1, "m:" + " " + mx, Color.orange);
AddLabel(1, "Angle:" + " " + angle+"°", Color.orange);
 
Last edited:

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
332 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