OPEN-SOURCE SCRIPT

CHoCH with alerts

//This source code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © CBriggsnz

// This a modification to add alert functions. All credits belong to the original author CBriggsnz
// The original indicator can be found here:
CHoCH Finder



//version=5
indicator("CHoCH with alerts", overlay = true, max_lines_count=500)

// ============================================================
// Imports
// ============================================================{
// Debugging
import ed56/debugger/2 as db
[c, f] = db.debugger()

// ============================================================}


// ============================================================
// Inputs
// ============================================================{
var CHoCH_Group = "Visual Settings"
var CHoCH_Style_in = input.string("┈┈┈┈", "CHoCH Style", options=["──────", "┈┈┈┈", "╌╌╌╌"], inline = "CH1_line", group = CHoCH_Group)
var CHoCH_Style = (CHoCH_Style_in == "┈┈┈┈") ? line.style_dotted : (CHoCH_Style_in == "╌╌╌╌") ? line.style_dashed : line.style_solid
var CHoCH_Color = input.color(color.new(color.blue,20),"Color", inline = "CH1_line", group = CHoCH_Group)
var CHoCH_Thickness = input.int(1, "Thickness", minval = 1, step = 1, inline = "CH1_line", group = CHoCH_Group)

var CHoCH_Change = input(false,"Show CHoCH continuations", group = CHoCH_Group, tooltip = "If unset then only show CHoCHs on Change of Trend")
var CHoCH_cont_Style_in = input.string("┈┈┈┈", "CHoCH Cont Style", options=["──────", "┈┈┈┈", "╌╌╌╌"], inline = "CH2_line", group = CHoCH_Group)
var CHoCH_cont_Style = (CHoCH_cont_Style_in == "┈┈┈┈") ? line.style_dotted : (CHoCH_cont_Style_in == "╌╌╌╌") ? line.style_dashed : line.style_solid
var CHoCH_cont_Color = input.color(color.new(color.aqua,45),"Color", inline = "CH2_line", group = CHoCH_Group)
var CHoCH_cont_Thickness = input.int(1, "Thickness", minval = 1, step = 1, inline = "CH2_line", group = CHoCH_Group)
var CHoCH_number = input.int(6, "Number of CHoCHs to display", minval = 0, group = CHoCH_Group)

var CHoCH_extras = "CHoCH Prediction"
var CHoCH_helper_Style_in= input.string("┈┈┈┈", "Prediction Style", options=["──────", "┈┈┈┈", "╌╌╌╌"], inline = "CH1_line", group = CHoCH_extras)
var CHoCH_helper_Style = (CHoCH_helper_Style_in == "┈┈┈┈") ? line.style_dotted : (CHoCH_helper_Style_in == "╌╌╌╌") ? line.style_dashed : line.style_solid
var CHoCH_helper_Color = input.color(color.new(color.blue,20),"Color", inline = "CH1_line", group = CHoCH_extras)
var CHoCH_helper_Thickness= input.int(2, "Thickness", minval = 1, step = 1, inline = "CH1_line", group = CHoCH_extras)
var show_CHoCH_fractal_HL= input(false,"Show Fractal High and Low Line", group = CHoCH_extras, tooltip = "Shows the current fractal range, good when learning how to draw CHoCHs.")
var show_next_CHoCH = input(true,"Show Next CHoCH line", group = CHoCH_extras, tooltip = "Shows the the line where the next CHoCH will happen.")

// ============================================================
// Modification: Add Alert Functions
// ============================================================

grp = "Alert Functions"


alert_bull_choch = input.bool(true,"Activate alerts for bullish CHoCHs", group = grp)
alert_bear_choch = input.bool(true,"Activate alerts for bearish CHoCHs", group = grp)
alert_cont_choch = input.bool(false,"Activate alerts for continuation CHoCHs", group = grp)

alert_freq = input.string(alert.freq_once_per_bar,"Alert Frequency", options = [ alert.freq_once_per_bar , alert.freq_once_per_bar_close], group = grp)

// Note: the added alert conditions are part of the main code

// =======================End of Input=========================}


// ============================================================
// Constants - Initial Variable Setup
// ============================================================ {

var float CH_high_price = na
var int CH_high_index = na
var float CH_low_price = na
var int CH_low_index = na

var CH_line_array = array.new_line(0)
var CH_cont_line_array = array.new_line(0)

var CHoCH_trend = ""

if barstate.isfirst
CH_low_price := low
CH_low_index := bar_index
CH_high_price := high
CH_high_index := bar_index

var label CH_high = label.new(na,na,"Fractal High", style = label.style_label_left, color = color.new(color.white, 100), textcolor = CHoCH_Color)
var label CH_low = label.new(na,na, "Fractal Low", style = label.style_label_left, color = color.new(color.white, 100), textcolor = CHoCH_Color)

var line CH_high_line = line.new(na,na,na,na, width = 2, color = CHoCH_helper_Color, style =CHoCH_helper_Style, width = CHoCH_helper_Thickness )
var line CH_low_line = line.new(na,na,na,na, width = 2, color = CHoCH_helper_Color, style =CHoCH_helper_Style, width = CHoCH_helper_Thickness)

// ====================End of CONSTANTS========================}

// ============================================================
// Functions
// ============================================================{

// ==============================
// Utility Functions
// ============================== {

ts(value) =>
// converts int, float, bool into strings
// [any] value -> variable to be converted
// returns ->
// string value -> a string
str.tostring(value)

print(value) =>
// Print log to screen
// any value -> variable to be printed
db.print(c, f, ts(value))

// ============================== }

// ==============================
// Drawing Functions
// ============================== {

drawLine(lin_array, x1, y1, x2, y2, style = line.style_solid, width = 1, linecolor = color.white) =>
// Draw a line
// [line] lin_array -> array to add the line to
// int x1 -> index to draw the start of the line at
// float y1 -> price to draw the start of the line at
// int x2 -> index to draw the end of the line at
// float y2 -> price to draw the end of the line at
// print("New CHoCH at " + ts(x1) + " " + ts(y1))

l = line.new( x1=x1, y1=y1, x2=x2, y2=y2, style = style,color = linecolor, width = width)
array.unshift(lin_array, l)

clearDrawing(line_array) =>
// erase CHoCH drawings that are beyond the requested number of CHoCH's
// [line] line_array -> an array of lines to be deleted
if array.size(line_array) > CHoCH_number
for i = CHoCH_number to array.size(line_array) - 1
line.delete(array.get(line_array,i)[0])
for i = array.size(line_array) - 1 to CHoCH_number
array.remove(line_array,i)

changeHighLowLines(hi, hp, li, lp) =>
// Change the fractal high and low lines
// int hi -> index of high
// float hp -> price of high
// int li -> index of low
// float lp -> price of low

line.set_xy1(CH_high_line, hi, hp)
line.set_xy2(CH_high_line, bar_index + 10, hp)
line.set_xy1(CH_low_line, li, lp)
line.set_xy2(CH_low_line, bar_index + 10, lp)
label.set_xy(CH_high, bar_index + 10, CH_high_price)
label.set_xy(CH_low, bar_index + 10, CH_low_price)

changeNextCHoCH(hi, hp, li, lp) =>
// CHoCH_trend
if CHoCH_trend == "Bullish"
line.set_color(CH_low_line, CHoCH_helper_Color)
line.set_color(CH_high_line, color.new(color.white,100))
line.set_xy1(CH_low_line, li, lp)
line.set_xy2(CH_low_line, bar_index + 10, lp)
if CHoCH_trend == "Bearish"
line.set_color(CH_low_line, color.new(color.white,100))
line.set_color(CH_high_line, CHoCH_helper_Color)
line.set_xy1(CH_high_line, hi, hp)
line.set_xy2(CH_high_line, bar_index + 10, hp)
// ============================== }

// ==============================
// Calculation Functions
// ============================== {

highSince(int end, start = 0) =>
// Find the highest value since start bars back
// int start -> bars back
// returns ->
//int -> bars back to highest high
int hIndex = 0
float hPrice = 0
for i = (end) to (start)
if high > hPrice
hIndex := i
hPrice := high
hIndex


lowSince(int end, start = 0) =>
// Find the lowest value since start bars back
// int start -> bars back
// returns ->
// int -> bars back to lowest low
int hIndex = 0
float hPrice = 99999999
for i = (end) to (start)
if low < hPrice
hIndex := i
hPrice := low
hIndex

findBreakInLow() =>
// finds where the low run stops
// returns ->
// int highSince(_i) -> index of highest candle since low
int _i = 0
cond = low[_i + 1] > low[_i]
while cond
_i := _i + 1
cond := low[_i + 1] > low[_i]
highSince(_i)

findBreakInHigh() =>
// finds where the high run stops
// returns ->
// int lowSince(_i) -> index of lowest candle since low
int _i = 0
cond = high[_i + 1] < high[_i]
while cond
_i := _i + 1
cond := high[_i + 1] < high[_i]
lowSince(_i)

// ============================== }
// ====================== End of Function =====================}

// ============================================================
// Main Code
// ============================================================{
engulfed = false

// Candle is engulfing candle
if high > CH_high_price and low < CH_low_price and low < CH_low_price and low[1] > CH_low_price[1]
// candle engulfs previous candle and takes out both low and high in one move
engulfed := true
if CHoCH_trend == "Bearish"
drawLine(CH_line_array, CH_high_index, CH_high_price, bar_index, CH_high_price, style = CHoCH_Style, width = CHoCH_Thickness, linecolor = CHoCH_Color)
clearDrawing(CH_line_array)
CHoCH_trend := "Bullish"
if CHoCH_trend == "Bullish"
drawLine(CH_line_array, CH_low_index, CH_low_price, bar_index, CH_low_price, style = CHoCH_Style, width = CHoCH_Thickness, linecolor = CHoCH_Color)
clearDrawing(CH_line_array)
CHoCH_trend := "Bearish"

// breaks fractal high
if high > CH_high_price
if high[1] <= CH_high_price[1] and not engulfed
// candle caused CHoCH or continuation CHoCH (name?)
if CHoCH_trend == "Bearish"
drawLine(CH_line_array, CH_high_index, CH_high_price, bar_index, CH_high_price, style = CHoCH_Style, width = CHoCH_Thickness, linecolor = CHoCH_Color)
clearDrawing(CH_line_array)
if alert_bull_choch // modification to add alerts
alert("Bullish CHoCH",alert_freq)
else if alert_cont_choch // modification to add alerts
alert("Bullish Cont CHoCH",alert_freq)
else if CHoCH_Change
drawLine(CH_cont_line_array, CH_high_index, CH_high_price, bar_index, CH_high_price, style = CHoCH_cont_Style, width = CHoCH_cont_Thickness, linecolor = CHoCH_cont_Color)
clearDrawing(CH_cont_line_array)
CHoCH_trend := "Bullish"
// calculate new fractal high and low
CH_high_price := high
CH_high_index := bar_index
lowOffset = findBreakInHigh()
CH_low_index := bar_index - lowOffset
CH_low_price := low[lowOffset]

// breaks fractal low
if low < CH_low_price
if low[1] >= CH_low_price[1] and not engulfed
// candle caused CHoCH or continuation CHoCH (name?)
if CHoCH_trend == "Bullish"
drawLine(CH_line_array, CH_low_index, CH_low_price, bar_index, CH_low_price, style = CHoCH_Style, width = CHoCH_Thickness, linecolor = CHoCH_Color)
clearDrawing(CH_line_array)
if alert_bear_choch // modification to add alerts
alert("Bearish CHoCH",alert_freq)
else if alert_cont_choch // modification to add alerts
alert("Bearish Cont CHoCH",alert_freq)

else if CHoCH_Change
drawLine(CH_cont_line_array, CH_low_index, CH_low_price, bar_index, CH_low_price, style = CHoCH_cont_Style, width = CHoCH_cont_Thickness, linecolor = CHoCH_cont_Color)
clearDrawing(CH_cont_line_array)
CHoCH_trend := "Bearish"
// calculate new fractal high and low
CH_low_price := low
CH_low_index := bar_index
highOffset = findBreakInLow()
CH_high_index := bar_index - highOffset
CH_high_price := high[highOffset]

// Draw in fractal high and low if selected
if show_CHoCH_fractal_HL
changeHighLowLines(CH_high_index, CH_high_price, CH_low_index, CH_low_price)
else if show_next_CHoCH
changeNextCHoCH(CH_high_index, CH_high_price, CH_low_index, CH_low_price)
// ==================== End of Main Code ======================}
// plot(bar_index)

// print("CHoCH_trend: " + CHoCH_trend)
Candlestick analysis

Скрипт с открытым кодом

В истинном духе TradingView автор этого скрипта опубликовал его с открытым исходным кодом, чтобы трейдеры могли понять, как он работает, и проверить на практике. Вы можете воспользоваться им бесплатно, но повторное использование этого кода в публикации регулируется Правилами поведения. Вы можете добавить этот скрипт в избранное и использовать его на графике.

Хотите использовать этот скрипт на графике?

Отказ от ответственности