I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.
def num_trading_days = absValue(CountTradingDays(X_Date, Y_Date));
def num_calendar_days = absvalue(DaysFromDate(X_Date) - DaysFromDate(Y_Date) + 1);
Ruby:input anchor_date = 20210801; input to_date = 20210831; input...
Thank you my friend it is great help almost close what I am looking for the following :See if this helps
Thank you my friend it is great help almost close what I am looking for the following :
- Start_month=20210801
- End_moth=20210831
- X= high value between start_month to end month
- Y= low value between start_month to end month
- X_Date=date at which high occurred between start_month to end month
- Y_Date=date at which low occurred between start_month to end month
Ruby:input anchor_date = 20210801; input to_date = 20210831; input show_label = yes; def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date then high else if Between(GetYYYYMMDD(), anchor_date, to_date) then Max(high, Xamt[1]) else double.nan; def X = highestall(Xamt); def X_Date = if high == X then GetYYYYMMDD() else X_Date[1]; AddLabel(show_label, "X: " + astext(X) + " X_Date: " + AsPrice(X_Date), Color.WHITE); def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date then low else if Between(GetYYYYMMDD(), anchor_date, to_date) then Min(low, Yamt[1]) else double.nan; def Y = lowestall(Yamt); def Y_Date = if low == Y then GetYYYYMMDD() else Y_Date[1]; AddLabel(show_label, "Y: " + astext(Y) + " Y_Date: " + AsPrice(Y_Date), Color.WHITE);
Mate found that your not in discord ....But the question is why am not able to calucalte the difference between X-date and Y-date using the following code. And when I calculate the difference can keep the absolute difference...I mean low - high sometimes will be negative I want to keep it positive.Try this
The following always has to be countradingdays(earliest date, latest date); formatMate found that your not in discord ....But the question is why am not able to calucalte the difference between X-date and Y-date using the following code. And when I calculate the difference can keep the absolute difference...I mean low - high sometimes will be negative I want to keep it positive.
input anchor_date = 20210801;
input to_date = 20210813;
def num_trading_days = CountTradingDays(anchor_date, to_date);
def num_calendar_days = DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1;
AddLabel(yes, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days +
" - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
Thank you Sleepz...Absvalue is helpful....The error I am getting is from the previous code where we calculated the X_date and Y_date. When I apply the same step from above it is showing an error;The following always has to be countradingdays(earliest date, latest date); format
def num_trading_days = CountTradingDays(anchor_date, to_date);
The following can be forced to be positive by use of absvalue as shown no matter which is put first
def num_calendar_days = absvalue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);
You will need to give my more information. I need something that shows the error on a chart with the code you used and the data you expected to help you.Thank you Sleepz...Absvalue is helpful....The error I am getting is from the previous code where we calculated the X_date and Y_date. When I apply the same step from above it is showing an error;
You will need to give my more information. I need something that shows the error on a chart with the code you used and the data you expected to help you.
I do not know what symbol or timeframe you are having problems with the above code. I combined the code I provided you and it seems to be working.input anchor_date = 20210801;
input to_date = 20210831;
input show_label = yes;
def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then high
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Max(high, Xamt[1])
else double.nan;
def X = highestall(Xamt);
def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then low
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Min(low, Yamt[1])
else double.nan;
def Y = lowestall(Yamt);
def X_Date = if high == X then GetYYYYMMDD() else X_Date[1];
def Y_Date = if low == Y then GetYYYYMMDD() else Y_Date[1];
/THIS DIDNT WORK As it doesn't produce the last label***/
def num_trading_days = absvalue(CountTradingDays(X_date, Y_date));
def num_calendar_days = absvalue(DaysFromDate(X_date) - DaysFromDate(Y_date) + 1);
/***************************************/
AddLabel(show_label, "X: " + astext(X) + " X_Date: " + AsPrice(X_Date), Color.WHITE);
AddLabel(show_label, "Y: " + astext(Y) + " Y_Date: " + AsPrice(Y_Date), Color.WHITE);
/THIS IS NOT popping up*/
AddLabel(yes, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days +
" - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
Ruby:input anchor_date = 20210801; input to_date = 20210831; input show_label = yes; def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date then high else if Between(GetYYYYMMDD(), anchor_date, to_date) then Max(high, Xamt[1]) else double.nan; def X = highestall(Xamt); def X_Date = if high == X then GetYYYYMMDD() else X_Date[1]; AddLabel(show_label, "X: " + astext(X) + " X_Date: " + AsPrice(X_Date), Color.WHITE); def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date then low else if Between(GetYYYYMMDD(), anchor_date, to_date) then Min(low, Yamt[1]) else double.nan; def Y = lowestall(Yamt); def Y_Date = if low == Y then GetYYYYMMDD() else Y_Date[1]; AddLabel(show_label, "Y: " + astext(Y) + " Y_Date: " + AsPrice(Y_Date), Color.WHITE); def num_trading_days = absValue(CountTradingDays(anchor_date, to_date)); def num_calendar_days = absvalue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1); AddLabel(show_label, "TDays: " + num_trading_days + " | CDays: " + num_calendar_days + " - from & including ... " + AsPrice(anchor_date) + " to " + AsPrice(to_date), color.white );
I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.I do not know what symbol or timeframe you are having problems with the above code. I combined the code I provided you and it seems to be working.
If you are still having problems, it could be because you must be using at least a chart of the number of trading days to get the "X/Y" code to work. TOS, other than with most built-in functions (eg: counttradingdays, daysfromdate) seems to only work on custom codes (eg: "X, X_Date, Y, Y_Date) if the data required for these is present in chart being displayed (eg: 22 trading days will require a chart likely set at least 30 days of data)..
Here is a chart of JBLU set at 15min, 30 days, and everything appears ok
Here is the same chart of JBLU set at 15min, 10 days, and there are problems with the custom code (Y and Y-Date), but not the built-in functions
Here is the combined code used in both of the above.
I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.
def num_trading_days = absValue(CountTradingDays(X_Date, Y_Date));
def num_calendar_days = absvalue(DaysFromDate(X_Date) - DaysFromDate(Y_Date) + 1);
Ruby:input anchor_date = 20210801; input to_date = 20210831; input show_label = yes; AddLabel(show_label, "Anchor", Color.YELLOW); AddLabel(show_label, AsPrice(anchor_date) + " to " + AsPrice(to_date), Color.YELLOW); def num_trading_days = AbsValue(CountTradingDays(anchor_date, to_date)); def num_calendar_days = AbsValue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1); AddLabel(show_label, "TDays: " + num_trading_days + " | CDays: " + num_calendar_days , Color.YELLOW ); #X/Y (High/Low)----------------------------------------- AddLabel(show_label, "H/L", Color.WHITE); def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date then high(period=aggregationPeriod.DAY) else if Between(GetYYYYMMDD(), anchor_date, to_date) then Max(high(period=aggregationPeriod.DAY), Xamt[1]) else Double.NaN; def X = HighestAll(Xamt); def X_Date = HighestAll(if high(period=aggregationPeriod.DAY) == X then GetYYYYMMDD() else 0); def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date then low(period=aggregationPeriod.DAY) else if Between(GetYYYYMMDD(), anchor_date, to_date) then Min(low(period=aggregationPeriod.DAY), Yamt[1]) else Double.NaN; def Y = LowestAll(Yamt); def Y_Date = highestAll(if low(period=aggregationPeriod.DAY) == Y then GetYYYYMMDD() else double.nan); def min_date = X_date==Min(Y_Date, X_Date); AddLabel(show_label, if min_date==0 then "L: " + AsText(Y) + " Y_Date: " + AsPrice(Y_Date) else "H: " + AsText(X) + " H_Date: " + AsPrice(X_Date), Color.WHITE); AddLabel(show_label, if min_date==0 then "H: " + AsText(X) + " X_Date: " + AsPrice(X_Date) else "L: " + AsText(Y) + " L_Date: " + AsPrice(Y_Date), Color.WHITE); def num_trading_days1 = AbsValue(CountTradingDays(Min(Y_Date, X_Date), Max(Y_Date, X_Date))); def num_calendar_days1 = AbsValue(DaysFromDate(Min(Y_Date, X_Date)) - DaysFromDate(Max(Y_Date, X_Date)) + 1); AddLabel(show_label, "TDays: " + num_trading_days1 + " | CDays: " + num_calendar_days1 , Color.WHITE );
I am not finding exactly what I was looking for here. I am a complete moron when it comes to scripting and have been trying to learn. What I am trying to do is get an exact count of how many bars (candles) there are from the Previous Day's open to just before the Current Day's open and include all pre and post market candles as well. I need this number for a calculation, but since the number of bars in the after hours trading session is changing I need to get a counter that would work on any chart so that it can be referenced for the calculations.
Ruby:def rthstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD()); def prevopen = if GetDay() == GetLastDay() - 1 and rthstart then open else prevopen[1]; def xcount = if GetDay() == GetLastDay() - 1 and rthstart then 1 else if GetDay() == GetLastDay() and GetTime() >= RegularTradingStart(GetYYYYMMDD()) then 0 else xcount[1] + 1; plot count = xcount; count.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW); AddLabel(1, "Prev Open: " + prevopen + " | Count to Today's Open: " + highestall(count) + " bars", color.yellow);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
K | Candlestick trading daily count | Questions | 4 | |
Z | Sum function to count throughout the trading session | Questions | 2 | |
B | Count green candles | Questions | 1 | |
S | RoundDown(HL2,0) as Loop Count Limit in Fold Not Working | Questions | 4 | |
M | Count number of active RSI chart bubbles and create alert | Questions | 6 |
Start a new thread and receive assistance from our community.
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.
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.