study("Candlestick Pattern", overlay=true) // Установим таймфрейм на 1 минуту…
- User
study("Candlestick Pattern", overlay=true)
// Установим таймфрейм на 1 минуту //@timeframe=1m
// Зеленая свеча isBullish = close > open
// Красная свеча isBearish = open > close
// Функция для определения последней свечи (текущей) на графике last_candle() => c0 = isBullish ? color.green : isBearish ? color.red : color.gray barcolor(c0)
// Функция для проверки комбинации свечей check_pattern(pattern) => result = true for i = 0 to (len(pattern) - 2) if (pattern[i] == 1 and not isBullish) or (pattern[i] == -1 and not isBearish) result := false break result and pattern[len(pattern) - 1] == 1
// Функция для сокращения текущей последовательности свечей до необходимой длины reduce_pattern(arr, len) => while len(arr) > len array.shift(arr) arr
// Определяем текущую последовательность из 10 свечей current_pattern = array.new_int(0) for i = 0 to 9 if i == 0 array.push(current_pattern, 0) else array.push(current_pattern, isBullish ? 1 : isBearish ? -1 : 0) current_pattern = reduce_pattern(current_pattern, 10)
// Проверяем комбинации свечей patterns = array.new_int(0) array.push(patterns, array.copy(current_pattern)) for i = 0 to (len(current_pattern) - 1) tmp = array.copy(current_pattern) tmp[i] := -tmp[i] array.push(patterns, tmp)
// Вычисляем количество раз, когда каждая комбинация была обнаружена counts = array.new_int(len(patterns)) for i = 9 to (bar_index - 1) pattern_detected = false for j = 0 to (len(patterns) - 1) if (not pattern_detected) and check_pattern(patterns[j]) array.set(counts, j, array.get(counts, j) + 1) current_pattern_shift = array.copy(current_pattern) array.shift(current_pattern_shift) array.set(current_pattern_shift, len(current_pattern_shift) - 1, isBullish ? 1 : isBearish ? -1 : 0) current_pattern = reduce_pattern(current_pattern_shift, 10) pattern_detected := true if not pattern_detected current_pattern_shift = array.copy(current_pattern) array.shift(current_pattern_shift) array.set(current_pattern_shift, len(current_pattern_shift) - 1, isBullish ? 1 : isBearish ? -1 : 0) current_pattern = reduce_pattern(current_pattern_shift, 10)
// Определяем наиболее частые комбинации max_pattern = array.new_int(0) max_count = 0 for i = 0 to (len(patterns) - 1) if array.get(counts, i) > max_count max_count := array.get(counts, i) max_pattern := array.copy
- Assistant
(patterns[i])
// Выводим все найденные комбинации и количество раз, когда они были обнаружены for i = 0 to (len(patterns) - 1) label.new(bar_index - i * 10, high, tostring(array.get(patterns, i)), color=color.white, textcolor=color.black)
// Выводим наиболее частую комбинацию и количество раз, когда она была обнаружена label.new(bar_index, high + 50, "Наиболее частая комбинация: " + tostring(max_pattern) + "\nКоличество обнаружений: " + tostring(max_count), color=color.red)
// Применяем функцию последней свечи (текущей) на графике last_candle()