Skip to content

Commit

Permalink
HBS-0: optimize high and low func (attempt 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
stimofeev-tv committed Feb 20, 2024
1 parent b06ffea commit bd4132e
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 71 deletions.
6 changes: 3 additions & 3 deletions links/bond_close_days_back.pine.link
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ yearYield = 365
dayAgoYield = timenow - 1000 * 60 * 60 * 24 * dayYield
monthAgoYield = timenow - 1000 * 60 * 60 * 24 * monthYield
yearAgoYield = timenow - 1000 * 60 * 60 * 24 * yearYield
countOfBars1DayAgoBond = fastSearchN(time, dayAgoYield, dayYield)
countOfBars1MonthAgoBond = fastSearchN(time, monthAgoYield, monthYield)
countOfBars1YearAgoBond = fastSearchN(time, yearAgoYield, yearYield)
countOfBars1DayAgoBond = fastSearchTimeIndex(dayAgoYield, dayYield)
countOfBars1MonthAgoBond = fastSearchTimeIndex(monthAgoYield, monthYield)
countOfBars1YearAgoBond = fastSearchTimeIndex(yearAgoYield, yearYield)

max_bars_back(close, yearYield)
plot(close[countOfBars1DayAgoBond], title="close_1_days_back")
Expand Down
109 changes: 49 additions & 60 deletions links/high_and_low.pine.link
Original file line number Diff line number Diff line change
Expand Up @@ -26,128 +26,117 @@ plot(atl, title='Low.All.Calc')
plot(atl_date, title='Low.All.Calc.Date')
var firstOpen = open
plot(firstOpen, title='Open.All.Calc')
custom_lowest_and_date(_x, _xt, len, maxbarsback) =>
x = _x
xt = _xt/1000
if bar_index == 0
x += x[maxbarsback] * 0 // max_bars_back
xt += xt[maxbarsback] * 0 // max_bars_back
custom_lowest_and_date(len, maxbarsback) =>
// max_bars_back(low, maxbarsback)
// max_bars_back(time, maxbarsback)
fake = low[maxbarsback] * time[maxbarsback]
if len == 0
[na, na]
[na + fake, na]
else if len == 1
[x, xt]
[low, time / 1000]
else
_len = len
if na(x[len + 1]) != true
if not na(low[len + 1])
_len -= 1
_len
res = x
res := x
date = xt
index = 0
for i = 0 to _len by 1
if x[i] < res
res := x[i]
date := xt[i]
[res, date]
custom_highest_and_date(_x, _xt, len, maxbarsback) =>
x = _x
xt = _xt/1000
if bar_index == 0
x += x[maxbarsback] * 0 // max_bars_back
xt += xt[maxbarsback] * 0 // max_bars_back
if low[i] < low[index]
index := i
[low[index], time[index] / 1000]
custom_highest_and_date(len, maxbarsback) =>
// max_bars_back(high, maxbarsback)
// max_bars_back(time, maxbarsback)
fake = high[maxbarsback] * time[maxbarsback]
if len == 0
[na, na]
[na + fake, na]
else if len == 1
[x, xt]
[high, time / 1000]
else
_len = len
if na(x[len + 1]) != true
if not na(high[len + 1])
_len -= 1
res = x
date = xt
index = 0
for i = 0 to _len by 1
if x[i] > res
res := x[i]
date := xt[i]
[res, date]
fastSearchN(_xs, x, maxbarsback) => // xs - sorted, ascending
xs = _xs
if bar_index == 0
xs += xs[maxbarsback] * 0 // max_bars_back
left = 0
if high[i] > high[index]
index := i
[high[index], time[index] / 1000]
fastSearchTimeIndex(x, maxbarsback) =>
// max_bars_back(time, maxbarsback)
mid = 0 * time[maxbarsback]
right = math.min(bar_index, maxbarsback)
mid = 0
if xs < x
left = 0
if time < x
0
else
for i = 0 to 9 by 1
mid := math.ceil((left + right) / 2)
if left == right
break
else if xs[mid] < x
else if time[mid] < x
right := mid
continue
else if xs[mid] > x
else if time[mid] > x
left := mid
continue
else
break
mid
years10 = (365 * 4 + 366) * 2
years10_ago = timenow - 1000 * 60 * 60 * 24 * years10
countOfBars10YearAgo = fastSearchN(time, years10_ago, years10)
countOfBars10YearAgo = fastSearchTimeIndex(years10_ago, years10)
years5 = 365 * 4 + 366
years5_ago = timenow - 1000 * 60 * 60 * 24 * years5
countOfBars5YearAgo = fastSearchN(time, years5_ago, years5)
countOfBars5YearAgo = fastSearchTimeIndex(years5_ago, years5)
years3 = 365 * 3
years3_ago = timenow - 1000 * 60 * 60 * 24 * years3
countOfBars3YearAgo = fastSearchN(time, years3_ago, years3)
countOfBars3YearAgo = fastSearchTimeIndex(years3_ago, years3)
weeks52 = 7 * 52
weeks52_ago = timenow - 1000 * 60 * 60 * 24 * weeks52
countOfBars52WeekAgo = fastSearchN(time, weeks52_ago, weeks52)
[weeks52_low, weeks52_low_date] = custom_lowest_and_date(low, time, countOfBars52WeekAgo, weeks52)
countOfBars52WeekAgo = fastSearchTimeIndex(weeks52_ago, weeks52)
[weeks52_low, weeks52_low_date] = custom_lowest_and_date(countOfBars52WeekAgo, weeks52)
plot(weeks52_low, title='price_52_week_low')
plot(weeks52_low_date, title='price_52_week_low_date')
[weeks52_high, weeks52_high_date] = custom_highest_and_date(high, time, countOfBars52WeekAgo, weeks52)
[weeks52_high, weeks52_high_date] = custom_highest_and_date(countOfBars52WeekAgo, weeks52)
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 = fastSearchN(time, months6_ago, month6)
[months6_low, months6_low_date] = custom_lowest_and_date(low, time, countOfBars6MonthAgo, month6)
countOfBars6MonthAgo = fastSearchTimeIndex(months6_ago, month6)
[months6_low, months6_low_date] = custom_lowest_and_date(countOfBars6MonthAgo, month6)
plot(months6_low, title='Low.6M')
plot(months6_low_date, title='Low.6M.Date')
[months6_high, months6_high_date] = custom_highest_and_date(high, time, countOfBars6MonthAgo, month6)
[months6_high, months6_high_date] = custom_highest_and_date(countOfBars6MonthAgo, month6)
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 = fastSearchN(time, months3_ago, month3)
[months3_low, months3_low_date] = custom_lowest_and_date(low, time, countOfBars3MonthAgo, month3)
countOfBars3MonthAgo = fastSearchTimeIndex(months3_ago, month3)
[months3_low, months3_low_date] = custom_lowest_and_date(countOfBars3MonthAgo, month3)
plot(months3_low, title='Low.3M')
plot(months3_low_date, title='Low.3M.Date')
[months3_high, months3_high_date] = custom_highest_and_date(high, time, countOfBars3MonthAgo, month3)
[months3_high, months3_high_date] = custom_highest_and_date(countOfBars3MonthAgo, month3)
plot(months3_high, title='High.3M')
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 = 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)
countOfBars1MonthAgo = fastSearchTimeIndex(month_ago, month1)
countOfBars1MonthAgoThisBar = fastSearchTimeIndex(month_ago_this_bar, month1)
[month1_low, month1_low_date] = custom_lowest_and_date(countOfBars1MonthAgo, month1)
plot(month1_low, title='Low.1M')
plot(month1_low_date, title='Low.1M.Date')
[month1_high, month1_high_date] = custom_highest_and_date(high, time, countOfBars1MonthAgo, month1)
[month1_high, month1_high_date] = custom_highest_and_date(countOfBars1MonthAgo, month1)
plot(month1_high, title='High.1M')
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 = fastSearchN(time, week_ago, week1)
countOfBarsWeekAgoThisBar = fastSearchN(time, week_ago_this_bar, week1)
[week1_low] = custom_lowest_and_date(low, time, countOfBarsWeekAgo, week1)
countOfBarsWeekAgo = fastSearchTimeIndex(week_ago, week1)
countOfBarsWeekAgoThisBar = fastSearchTimeIndex(week_ago_this_bar, week1)
[week1_low] = custom_lowest_and_date(countOfBarsWeekAgo, week1)
plot(week1_low, title='Low.5D')
[week1_high] = custom_highest_and_date(high, time, countOfBarsWeekAgo, week1)
[week1_high] = custom_highest_and_date(countOfBarsWeekAgo, week1)
plot(week1_high, title='High.5D')
// volatility
volatility(bb) =>
Expand Down
5 changes: 3 additions & 2 deletions links/performance_crypto.pine.link
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plot(perf6M, title='Perf.6M')

oneYear = 365
oneYearAgo = timenow - 1000 * 60 * 60 * 24 * oneYear
barsCountOneYear = fastSearchN(time, oneYearAgo, oneYear)
barsCountOneYear = fastSearchTimeIndex(oneYearAgo, oneYear)
perfY = rr(barsCountOneYear, oneYear)
plot(perfY, title='Perf.Y')

Expand All @@ -39,7 +39,8 @@ plot(perf5Y_USD, title='Perf.5Y.USD')
plot(perfYTD_USD, title='Perf.YTD.USD')

// Perf.All
[firstOpenUSD, lastCloseUSD] = request.security(syminfo.tickerid, "D", [open, close], lookahead = barmerge.lookahead_on, currency = "USD", ignore_invalid_symbol=true)
[openUSD, lastCloseUSD] = request.security(syminfo.tickerid, "D", [open, close], lookahead = barmerge.lookahead_on, currency = "USD", ignore_invalid_symbol=true)
var firstOpenUSD = openUSD
float perfAllUSD = na
if barstate.islast and not na(firstOpenUSD) and not na(lastCloseUSD) and firstOpenUSD > 0
perfAllUSD := (lastCloseUSD - firstOpenUSD) * 100 / math.abs(firstOpenUSD)
Expand Down
2 changes: 1 addition & 1 deletion links/performance_stocks.pine.link
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Perf.<W | 1M | 3M | 6M | Y | 5Y | 10Y | YTD>
oneYear = 365
oneYearAgo = timenow - 1000 * 60 * 60 * 24 * oneYear
barsCountOneYear = fastSearchN(time, oneYearAgo, oneYear)
barsCountOneYear = fastSearchTimeIndex(oneYearAgo, oneYear)
perfYTD = perfYTD()
plot((close - open[4]) / open[4] * 100, title='Perf.5D')
plot(rr(countOfBarsWeekAgo, week1), title='Perf.W')
Expand Down
Loading

0 comments on commit bd4132e

Please sign in to comment.