Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Alex
- //@version=5
- indicator("My script",overlay=true, max_lines_count = 50, max_bars_back = 200)
- line_limit = input(50)
- any_initial_signal = input.bool(true)
- ignore_motherbar_signals = input.bool(true)
- ignore_gaps = input.bool(false)
- show_entry_stop = input(true)
- unready_after_x_bars = input.int(5)
- doji_range_input = input(.2)
- dots = input.bool(false)
- paint_old_liquidity = input.bool(false)
- var high_liq_array = array.new_line()
- var low_liq_array = array.new_line()
- var line readied_level = na
- var bool readied_bull = false
- var bool setup_bull = false
- var bool readied_bear = false
- var bool setup_bear = false
- var line bull_entry_line = na
- var line bull_stop_line = na
- var line bear_entry_line = na
- var line bear_stop_line = na
- var line mother_bar_high_line = na
- var line mother_bar_low_line = na
- var label mother_bar_high_label = na
- var label mother_bar_low_label = na
- initial_signal = readied_bull[1] ? false : true
- ATRPeriod = input.int(12, "ATR Period", tooltip = "This is the number of bars back that the script uses to calculate the Average True Range.")
- ATRMultiplier = input.float(3, "ATR Multiplier", step=.1, tooltip = "This is the multiple of the ATR average that will function as the trail.")
- ATR = ta.atr(ATRPeriod)
- Stop = ATRMultiplier*ATR
- var ATRTrailingStop = 0.0
- ATRTrailingStop := if close>ATRTrailingStop[1] and close[1]>ATRTrailingStop[1]
- math.max(ATRTrailingStop[1], close-Stop)
- else if close<ATRTrailingStop[1] and close[1]<ATRTrailingStop[1]
- math.min(ATRTrailingStop[1], close+Stop)
- else if close>ATRTrailingStop[1]
- close-Stop
- else
- close+Stop
- var Position = 0.0
- Position := if close[1]<ATRTrailingStop[1] and close>ATRTrailingStop[1]
- 1
- else if close[1]>ATRTrailingStop[1] and close<ATRTrailingStop[1]
- -1
- else
- Position[1]
- current_candle_range = high-low
- doji_range = current_candle_range*doji_range_input
- low_formed = low[2] > low[1] and low[1] < low
- high_formed = high[2] < high[1] and high[1] > high
- mother_bar_formed = high[2] > high[1] and high[2] > high and low[2] < low[1] and low[2] < low
- if high_formed
- bull_line = line.new(bar_index[1],high[1],bar_index[0],high[1],extend=extend.right, color= color.red, style = line.style_solid)
- high_liq_array.push(bull_line)
- if low_formed
- bear_line = line.new(bar_index[1],low[1],bar_index[0],low[1],extend=extend.right,color = color.green, style = line.style_solid)
- low_liq_array.push(bear_line)
- if mother_bar_formed and na(mother_bar_high_line) and na(mother_bar_low_line)
- mother_bar_high_line:= line.new(bar_index[1],high[1],bar_index[0],high[1],extend=extend.right,color=color.black,style=line.style_dotted)
- mother_bar_low_line:= line.new(bar_index[1],low[1],bar_index[0],low[1],extend=extend.right,color=color.black,style=line.style_dotted)
- mother_bar_high_label:= label.new(bar_index+5,high[1],str.tostring(mother_bar_high_line.get_y1()), style = label.style_none)
- mother_bar_low_label:= label.new(bar_index+5,low[1],str.tostring(mother_bar_high_line.get_y1()), style= label.style_none)
- for [index,value] in high_liq_array
- linex = high_liq_array.get(index)
- if high >= linex.get_y1() and low < linex.get_y1()
- if paint_old_liquidity
- line.new(linex.get_x1(),linex.get_y1(),bar_index[0],linex.get_y1(),color=color.black)
- array.remove(high_liq_array,index)
- readied_level:=linex
- readied_bear:= true
- line.delete(linex)
- if na(mother_bar_high_line) == false and mother_bar_high_line.get_y1() < high
- readied_level:= mother_bar_low_line
- readied_bear:= true
- mother_bar_low_label.delete()
- mother_bar_high_label.delete()
- mother_bar_high_line.delete()
- mother_bar_low_line.delete()
- mother_bar_low_label:=na
- mother_bar_high_label:=na
- mother_bar_low_line:=na
- mother_bar_high_line:=na
- for [index,value] in low_liq_array
- linex = low_liq_array.get(index)
- if low < linex.get_y1() and high > linex.get_y1()
- if paint_old_liquidity
- line.new(linex.get_x1(),linex.get_y1(),bar_index[0],linex.get_y1(),color=color.black)
- array.remove(low_liq_array,index)
- readied_level:=linex
- readied_bull:= true
- line.delete(linex)
- if na(mother_bar_low_line) == false and mother_bar_low_line.get_y1() > low
- readied_level:= mother_bar_low_line
- readied_bull:= true
- mother_bar_low_label.delete()
- mother_bar_high_label.delete()
- mother_bar_high_line.delete()
- mother_bar_low_line.delete()
- mother_bar_low_label:= na
- mother_bar_high_label:=na
- mother_bar_low_line:=na
- mother_bar_high_line:=na
- inside_motherbar = na(mother_bar_high_line.get_y1()) == false and na(mother_bar_low_line.get_y1()) == false //True if both are not NA
- bear_stop = high
- bull_stop = low
- if readied_bull
- if open > bull_entry_line.get_y1() and ignore_gaps==true
- setup_bull:=false
- readied_bull:=false
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- if low < low[1] and close > open and inside_motherbar == false
- setup_bull:= true
- if show_entry_stop
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:= line.new(bar_index[0],high,bar_index[0]+3,high, color=color.orange)
- bull_stop_line:= line.new(bar_index[0],bull_stop,bar_index[0]+3,bull_stop,color =color.red)
- if high < high[1] and low > low[1] and inside_motherbar == false
- setup_bull:= true
- if show_entry_stop
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:= line.new(bar_index[0],high,bar_index[0]+3,high, color=color.orange)
- bull_stop_line:= line.new(bar_index[0],bull_stop,bar_index[0]+3,bull_stop,color =color.red)
- if math.abs(close-open) <= doji_range and low<low[1]
- setup_bull:= true
- if show_entry_stop
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:= line.new(bar_index[0],high,bar_index[0]+3,high, color=color.orange)
- bull_stop_line:= line.new(bar_index[0],bull_stop,bar_index[0]+3,bull_stop,color =color.red)
- if ignore_motherbar_signals==false and inside_motherbar == true
- setup_bull:= true
- if show_entry_stop
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:= line.new(bar_index[0],high,bar_index[0]+3,high, color=color.orange, style = line.style_dashed)
- bull_stop_line:= line.new(bar_index[0],bull_stop,bar_index[0]+3,bull_stop,color =color.red)
- if any_initial_signal==true and initial_signal and low < low[1]
- setup_bull:=true
- if show_entry_stop
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:= line.new(bar_index[0],high,bar_index[0]+3,high, color=color.orange, style = line.style_dashed)
- bull_stop_line:= line.new(bar_index[0],bull_stop,bar_index[0]+3,bull_stop,color =color.red)
- //Large Lower Wick
- if close>open and (math.abs(low-close) > math.abs(close-open)) and low < low[1]
- setup_bull:= true
- if show_entry_stop
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:= line.new(bar_index[0],high,bar_index[0]+3,high, color=color.orange)
- bull_stop_line:= line.new(bar_index[0],bull_stop,bar_index[0]+3,bull_stop,color =color.red)
- //If it goes outside after the ready just negate it
- if high > high[1] and low < low[1]
- setup_bull:= false
- readied_bull:=false
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:=na
- bull_stop_line:=na
- if readied_bear
- //Negate if gaps
- if open < bear_entry_line.get_y1() and ignore_gaps == true
- setup_bear:=false
- readied_bear:=false
- line.delete(bear_entry_line)
- line.delete(bear_stop_line)
- bear_entry_line:=na
- bear_stop_line:=na
- if high > high[1] and close < open and low > low[1] and inside_motherbar==false
- setup_bear:= true
- readied_bear:=false
- if show_entry_stop
- line.delete(bear_entry_line)
- line.delete(bear_stop_line)
- bear_entry_line:= line.new(bar_index[0],low,bar_index[0]+3,low, color=color.orange)
- bear_stop_line:= line.new(bar_index[0],bear_stop,bar_index[0]+3,bear_stop,color = color.red)
- if (math.abs(close-open) <= doji_range) and high > high[1]
- setup_bear:= true
- if show_entry_stop
- line.delete(bear_entry_line)
- line.delete(bear_stop_line)
- bear_entry_line:= line.new(bar_index[0],low,bar_index[0]+3,low, color=color.orange)
- bear_stop_line:= line.new(bar_index[0],bear_stop,bar_index[0]+3,bear_stop,color =color.red)
- //Large Upper Wick
- if close>open and (math.abs(high-close) > math.abs(close-open))
- setup_bear:= true
- if show_entry_stop
- line.delete(bear_entry_line)
- line.delete(bear_stop_line)
- bear_entry_line:= line.new(bar_index[0],low,bar_index[0]+3,low, color=color.orange)
- bear_stop_line:= line.new(bar_index[0],bear_stop,bar_index[0]+3,bear_stop,color =color.red)
- //If it goes outside after the ready just negate it
- if high > high[1] and low < low[1]
- setup_bear:= false
- readied_bear:=false
- line.delete(bear_entry_line)
- line.delete(bear_stop_line)
- bear_entry_line:=na
- bear_stop_line:=na
- bull_trigger = setup_bull==true and high > bull_entry_line.get_y1()
- bear_trigger = setup_bear==true and low < bear_entry_line.get_y1()
- var table values_display = table.new(position.top_right,2,5,bgcolor = color.rgb(77, 109, 96),frame_color=color.white,frame_width=1,border_color=color.black,border_width=1)
- // We call `atr()` outside the `if` block so it executes on each bar.
- if barstate.islast
- // We only populate the table on the last bar.
- table.cell(values_display, 1,0, str.upper(str.tostring(readied_bull)),text_color = color.green)
- table.cell(values_display, 0,0,'Readied Bull')
- table.cell(values_display, 1,1, str.upper(str.tostring(readied_bear)),text_color = color.red)
- table.cell(values_display, 0,1, 'Readied Bear')
- table.cell(values_display, 0,2, 'MotherBar')
- table.cell(values_display, 1,2, str.upper(str.tostring(inside_motherbar)))
- table.cell(values_display, 0,3, 'Runners Direction')
- table.cell(values_display, 1,3, 'NaN')
- table.cell(values_display, 0,4, 'Setup Direction')
- table.cell(values_display, 1,4, (setup_bull ? 'Bull' : setup_bear ? 'Bear' : 'NaN'))
- plotshape(bull_trigger, "none",shape.arrowup,location.belowbar,color.green,offset=0)
- plotshape(bear_trigger, "none",shape.arrowdown,location.abovebar,color.red,offset=0)
- if bull_trigger
- readied_bull:= false
- setup_bull:=false
- bull_entry_line:=na
- bull_stop_line:=na
- //Initial setup negates
- if initial_signal
- line.delete(bull_entry_line)
- line.delete(bull_stop_line)
- bull_entry_line:=na
- bull_stop_line:=na
- if bear_trigger
- readied_bear:= false
- setup_bear:= false
- bear_entry_line:=na
- bear_stop_line:=na
- plotshape(low_formed and dots,"none",shape.circle,location.belowbar,color.green,offset=-1)
- plotshape(high_formed and dots, "none", shape.circle, location.abovebar,color.red,offset=-1)
- PlotColor = Position == -1 ? color.red: Position == 1 ? color.green : color.navy
- plot(ATRTrailingStop, color=PlotColor, linewidth=input(1, "Line Width"), title="ATR Trailing Stop")
Add Comment
Please, Sign In to add comment