diff --git a/links/bond_close_days_back.pine.link b/links/bond_close_days_back.pine.link index d22e7ccd8..d8ff4f50a 100644 --- a/links/bond_close_days_back.pine.link +++ b/links/bond_close_days_back.pine.link @@ -5,11 +5,14 @@ yearYield = 365 dayAgoYield = timenow - 1000 * 60 * 60 * 24 * dayYield monthAgoYield = timenow - 1000 * 60 * 60 * 24 * monthYield yearAgoYield = timenow - 1000 * 60 * 60 * 24 * yearYield -countOfBars1DayAgoBond = fastSearchTimeIndex(dayAgoYield, dayYield) -countOfBars1MonthAgoBond = fastSearchTimeIndex(monthAgoYield, monthYield) -countOfBars1YearAgoBond = fastSearchTimeIndex(yearAgoYield, yearYield) +countOfBars1DayAgoBond = fastSearchN(time, dayAgoYield, dayYield) +countOfBars1MonthAgoBond = fastSearchN(time, monthAgoYield, monthYield) +countOfBars1YearAgoBond = fastSearchN(time, yearAgoYield, yearYield) -max_bars_back(close, yearYield) -plot(close[countOfBars1DayAgoBond], title="close_1_days_back") -plot(close[countOfBars1MonthAgoBond], title="close_30_days_back") -plot(close[countOfBars1YearAgoBond], title="close_365_days_back") \ No newline at end of file +series = close +if bar_index == 0 + series += series[yearYield] * 0 // max_bars_back + +plot(series[countOfBars1DayAgoBond], title="close_1_days_back") +plot(series[countOfBars1MonthAgoBond], title="close_30_days_back") +plot(series[countOfBars1YearAgoBond], title="close_365_days_back") \ No newline at end of file diff --git a/links/high_and_low.pine.link b/links/high_and_low.pine.link index 49fe16b7e..33d59d0e3 100644 --- a/links/high_and_low.pine.link +++ b/links/high_and_low.pine.link @@ -70,22 +70,24 @@ custom_highest_and_date(_x, _xt, len, maxbarsback) => res := x[i] date := xt[i] [res, date] -fastSearchTimeIndex(x, maxbarsback) => - // max_bars_back(time, maxbarsback) - left = 0 * time[maxbarsback] +fastSearchN(_xs, x, maxbarsback) => // xs - sorted, ascending + xs = _xs + if bar_index == 0 + xs += xs[maxbarsback] * 0 // max_bars_back + left = 0 right = math.min(bar_index, maxbarsback) mid = 0 - if time < x + if xs < x 0 else for i = 0 to 9 by 1 mid := math.ceil((left + right) / 2) if left == right break - else if time[mid] < x + else if xs[mid] < x right := mid continue - else if time[mid] > x + else if xs[mid] > x left := mid continue else @@ -93,13 +95,13 @@ fastSearchTimeIndex(x, maxbarsback) => mid years5 = 365 * 4 + 366 years5_ago = timenow - 1000 * 60 * 60 * 24 * years5 -countOfBars5YearAgo = fastSearchTimeIndex(years5_ago, years5) +countOfBars5YearAgo = fastSearchN(time, years5_ago, years5) years3 = 365 * 3 years3_ago = timenow - 1000 * 60 * 60 * 24 * years3 -countOfBars3YearAgo = fastSearchTimeIndex(years3_ago, years3) +countOfBars3YearAgo = fastSearchN(time, years3_ago, years3) weeks52 = 7 * 52 weeks52_ago = timenow - 1000 * 60 * 60 * 24 * weeks52 -countOfBars52WeekAgo = fastSearchTimeIndex(weeks52_ago, weeks52) +countOfBars52WeekAgo = fastSearchN(time, weeks52_ago, weeks52) [weeks52_low, weeks52_low_date] = custom_lowest_and_date(low, time, countOfBars52WeekAgo, weeks52) plot(weeks52_low, title='price_52_week_low') plot(weeks52_low_date, title='price_52_week_low_date') @@ -108,7 +110,7 @@ plot(weeks52_high, title='price_52_week_high') plot(weeks52_high_date, title='price_52_week_high_date') month6 = 180 months6_ago = timenow - 1000 * 60 * 60 * 24 * month6 -countOfBars6MonthAgo = fastSearchTimeIndex(months6_ago, month6) +countOfBars6MonthAgo = fastSearchN(time, months6_ago, month6) [months6_low, months6_low_date] = custom_lowest_and_date(low, time, countOfBars6MonthAgo, month6) plot(months6_low, title='Low.6M') plot(months6_low_date, title='Low.6M.Date') @@ -117,7 +119,7 @@ plot(months6_high, title='High.6M') plot(months6_high_date, title='High.6M.Date') month3 = 90 months3_ago = timenow - 1000 * 60 * 60 * 24 * month3 -countOfBars3MonthAgo = fastSearchTimeIndex(months3_ago, month3) +countOfBars3MonthAgo = fastSearchN(time, months3_ago, month3) [months3_low, months3_low_date] = custom_lowest_and_date(low, time, countOfBars3MonthAgo, month3) plot(months3_low, title='Low.3M') plot(months3_low_date, title='Low.3M.Date') @@ -127,8 +129,8 @@ plot(months3_high_date, title='High.3M.Date') month1 = 30 month_ago_this_bar = time - 1000 * 60 * 60 * 24 * month1 month_ago = timenow - 1000 * 60 * 60 * 24 * month1 -countOfBars1MonthAgo = fastSearchTimeIndex(month_ago, month1) -countOfBars1MonthAgoThisBar = fastSearchTimeIndex(month_ago_this_bar, month1) +countOfBars1MonthAgo = fastSearchN(time, month_ago, month1) +countOfBars1MonthAgoThisBar = fastSearchN(time, month_ago_this_bar, month1) [month1_low, month1_low_date] = custom_lowest_and_date(low, time, countOfBars1MonthAgo, month1) plot(month1_low, title='Low.1M') plot(month1_low_date, title='Low.1M.Date') @@ -138,8 +140,8 @@ plot(month1_high_date, title='High.1M.Date') week1 = 7 week_ago = timenow - 1000 * 60 * 60 * 24 * week1 week_ago_this_bar = time - 1000 * 60 * 60 * 24 * week1 -countOfBarsWeekAgo = fastSearchTimeIndex(week_ago, week1) -countOfBarsWeekAgoThisBar = fastSearchTimeIndex(week_ago_this_bar, week1) +countOfBarsWeekAgo = fastSearchN(time, week_ago, week1) +countOfBarsWeekAgoThisBar = fastSearchN(time, week_ago_this_bar, week1) [week1_low] = custom_lowest_and_date(low, time, countOfBarsWeekAgo, week1) plot(week1_low, title='Low.5D') [week1_high] = custom_highest_and_date(high, time, countOfBarsWeekAgo, week1) diff --git a/links/performance_crypto.pine.link b/links/performance_crypto.pine.link index 96f319953..484aac879 100644 --- a/links/performance_crypto.pine.link +++ b/links/performance_crypto.pine.link @@ -22,7 +22,7 @@ plot(perf6M, title='Perf.6M') plot(perf6M_USD, title='Perf.6M.USD') oneYear = 365 oneYearAgo = timenow - 1000 * 60 * 60 * 24 * oneYear -barsCountOneYear = fastSearchTimeIndex(oneYearAgo, oneYear) +barsCountOneYear = fastSearchN(time, oneYearAgo, oneYear) perfY = rr(barsCountOneYear, oneYear) perfY_USD = request.security(syminfo.tickerid, "D", perfY, lookahead = barmerge.lookahead_off, currency = "USD", ignore_invalid_symbol=true) plot(perfY, title='Perf.Y') diff --git a/links/performance_stocks.pine.link b/links/performance_stocks.pine.link index 0909ab69f..1d4df42c6 100644 --- a/links/performance_stocks.pine.link +++ b/links/performance_stocks.pine.link @@ -2,7 +2,7 @@ // Perf. oneYear = 365 oneYearAgo = timenow - 1000 * 60 * 60 * 24 * oneYear -barsCountOneYear = fastSearchTimeIndex(oneYearAgo, oneYear) +barsCountOneYear = fastSearchN(time, oneYearAgo, oneYear) perfYTD = perfYTD() plot((close - open[4]) / open[4] * 100, title='Perf.5D') plot(rr(countOfBarsWeekAgo, week1), title='Perf.W')