예스스탁
예스스탁 답변
2025-06-30 12:43:07
안녕하세요
예스스탁입니다.
올리신 내용은 변환하는데 시간이 많이 소모됩니다.
업무상 일정시간 이상 요구되는 내용인 작성해 드리기 어렵습니다.
도움을 드리지 못해 죄송합니다.
즐거운 하루되세요
> 파생돌이 님이 쓴 글입니다.
> 제목 : 부틱드립니다
> 수고하십니다
예스로 부탁드립니다
// This Pine S cript™ code is subject to the terms of the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © UAlgo
//@version=5
indicator('Log Regression Channel [UAlgo]',shorttitle = 'Log Regression Channel [UAlgo]', overlay = true, max_lines_count = 500)
source_input = input.source(close, title = "Source",group = "Log Regression Channel [UAlgo]")
length_input = input.int(100, title='Length',group = "Log Regression Channel [UAlgo]")
multiplier_input = input.int(2, title='Deviation Multiplier',group = "Log Regression Channel [UAlgo]")
label_color = input.color(color.red, title='Label Color',group = "Log Regression Channel [UAlgo]")
label_size_option = input.string(title='Label Size', defval='small', options=['small', 'normal', 'large', 'huge'],group = "Log Regression Channel [UAlgo]")
var float[] series_price = array.new_float(length_input)
var int[] series_time = array.new_int(length_input)
array.unshift(series_price, source_input)
array.pop(series_price)
array.unshift(series_time, bar_index)
array.pop(series_time)
var line[] reg_lines = array.new_line()
var line[] dev_up_lines = array.new_line()
var line[] dev_down_lines = array.new_line()
var label r_squared_label = label.new(x = na, y = na, style = label.style_label_upper_right, color = #00000000, textcolor = color.new(#FF1493, 0), size=size.large, textalign = text.align_right)
float[] dev_up_series = array.new_float()
float[] dev_down_series = array.new_float()
var int line_step = na
var int line_count = na
if barstate.isfirst
line_count := math.min(length_input, 250)
for i = 0 to line_count - 1
array.unshift(reg_lines, line.new(x1=na, y1=na, x2=na, y2=na, color=color.red, style=line.style_solid))
if length_input > 250
line_step := math.ceil(length_input / 250)
else
line_step := 1
for i = 0 to math.floor(line_count / 2) - 1
array.unshift(dev_up_lines, line.new(x1=na, y1=na, x2=na, y2=na, color=color.new(#00FFFF, 0), style=line.style_solid))
array.unshift(dev_down_lines, line.new(x1=na, y1=na, x2=na, y2=na, color=color.new(#00FF00, 0), style=line.style_solid))
if barstate.islast
int _length_price = array.size(series_price)
int _length_time = array.size(series_time)
float[] _log_price_array = array.new_float(_length_price)
float[] _log_time_array = array.new_float(_length_time)
for _index = 0 to _length_price - 1
array.set(_log_price_array, _index, math.log(array.get(series_price, _index)))
array.set(_log_time_array, _index, math.log(array.get(series_time, _index)))
float _total_log_time = array.sum(_log_time_array)
float _total_log_price = array.sum(_log_price_array)
float _total_log_time_price = 0.0
float _total_log_time_sq = 0.0
float _total_log_price_sq = 0.0
if _length_price == _length_time
for _index = 0 to _length_price - 1
float _log_time_val = nz(array.get(_log_time_array, _index))
float _log_price_val = nz(array.get(_log_price_array, _index))
_total_log_time_price := _total_log_time_price + _log_time_val * _log_price_val
_total_log_time_sq := _total_log_time_sq + math.pow(_log_time_val, 2)
_total_log_price_sq := _total_log_price_sq + math.pow(_log_price_val, 2)
_total_log_price_sq
float _intercept = (_total_log_price * _total_log_time_sq - _total_log_time * _total_log_time_price) / (_length_time * _total_log_time_sq - math.pow(_total_log_time, 2))
float _slope_coef = (_length_time * _total_log_time_price - _total_log_time * _total_log_price) / (_length_time * _total_log_time_sq - math.pow(_total_log_time, 2))
float[] _predictions_array = array.new_float()
for _index = 0 to _length_price - 1
float _predicted_val = _intercept + _slope_coef * array.get(_log_time_array, _index)
array.push(_predictions_array, _predicted_val)
_slope = (array.get(_predictions_array, 0) - array.get(_predictions_array, _length_price - 1)) / (array.get(_log_time_array, 0) - array.get(_log_time_array, _length_time - 1))
_mean_log_price = array.avg(_log_price_array)
float _sum_squared_residuals = 0.0
float _sum_squared_totals = 0.0
for _index = 0 to _length_price - 1
float _predicted = array.get(_predictions_array, _index)
float _log_price_val = array.get(_log_price_array, _index)
_sum_squared_residuals := _sum_squared_residuals + math.pow(_predicted - _log_price_val, 2)
_sum_squared_totals := _sum_squared_totals + math.pow(_mean_log_price - _log_price_val, 2)
_sum_squared_totals
_r_squared = 1 - _sum_squared_residuals / _sum_squared_totals
float _sum_squared_errors = 0
for _index = 0 to _length_price - 1
_sum_squared_errors += math.pow(array.get(_predictions_array, _index) - array.get(_log_price_array, _index), 2)
_std_dev = math.sqrt(_sum_squared_errors / _length_price)
for i = 0 to array.size(_predictions_array) - 1
array.push(dev_up_series, math.exp(array.get(_predictions_array, i) + multiplier_input * _std_dev))
array.push(dev_down_series, math.exp(array.get(_predictions_array, i) - multiplier_input * _std_dev))
for i = 0 to array.size(_predictions_array) - 2 - line_step by line_step
line.set_xy1(array.get(reg_lines, i / line_step), x=bar_index - i, y=math.exp(array.get(_predictions_array, i)))
line.set_xy2(array.get(reg_lines, i / line_step), x=bar_index - i - line_step, y=math.exp(array.get(_predictions_array, i + line_step)))
for i = 0 to array.size(dev_up_series) - 2 - line_step by line_step * 2
line.set_xy1(array.get(dev_up_lines, i / (line_step * 2)), x=bar_index - i, y=array.get(dev_up_series, i))
line.set_xy2(array.get(dev_up_lines, i / (line_step * 2)), x=bar_index - i - line_step, y=array.get(dev_up_series, i + line_step))
line.set_xy1(array.get(dev_down_lines, i / (line_step * 2)), x=bar_index - i, y=array.get(dev_down_series, i))
line.set_xy2(array.get(dev_down_lines, i / (line_step * 2)), x=bar_index - i - line_step, y=array.get(dev_down_series, i + line_step))
label.set_xy(r_squared_label, x = bar_index + 5, y = array.get(dev_down_series, 0))
label.set_text(r_squared_label, text = "R " + str.tostring(math.sqrt(_r_squared)))
label.set_textcolor(r_squared_label, label_color)
label.set_size(r_squared_label, label_size_option)