Plotting a horizontal ray at the at the crossing of 2 moving averages on tradingview

136 Views Asked by At

I am new in pine script. What I am trying to do is plot a horizontal ray at the crossing point of ma50 and ma100 at 4 hour resolution. Should looks like something like this:

ma100

Just need a basic script to begin with

Thank you

Here is something I came up but doesn't work at all

//@version=5
indicator("CU",shorttitle = "CU", overlay=true)

ma50 = ta.sma(close, 50)
ma100 = ta.sma(close, 100)
bcu = ma50 == ma100


bcu4h = input.timeframe("4H", "BCU4H")


bcu_sec = request.security(syminfo.tickerid, bcu4h, close)

line_bcu1d = line.new(na, na, na, na, style=line.style_solid,extend=extend.right, color=color.green)

//PLOT
plot(ma50, "MA50", color.rgb(255, 255, 255, 0), 2)
plot(ma100, "MA100", color.rgb(255, 235, 59, 0), 2)

EDIT: OK i got it found some parts and put them back together, but now

I need to delete new lines once the price crosses it after the line was created

//@version=5
indicator("CU2",shorttitle = "CU2", overlay=true)

var float   last_intersect_x    = na
var float   last_intersect_y    = na

// Gets intersection point of two line segments [(x1, y1), (x2, y2)] and [(u1, v1), (u2, v2)].
f_get_intersect_coordinates(x1, y1, x2, y2, u1, v1, u2, v2) =>
    x = -1 * ((x1 - x2) * (u1 * v2 - u2 * v1) - (u2 - u1) * (x2 * y1 - x1 * y2)) / ((v1 - v2) * (x1 - x2) - (u2 - u1) * (y2 - y1))
    y = -1 * (u1 * v2 * y1 - u1 * v2 * y2 - u2 * v1 * y1 + u2 * v1 * y2 - v1 * x1 * y2 + v1 * x2 * y1 + v2 * x1 * y2 - v2 * x2 * y1) / (-1 * u1 * y1 + u1 * y2 + u2 * y1 - u2 * y2 + v1 * x1 - v1 * x2 - v2 * x1 + v2 * x2)
    [x,y]

_ma50 = ta.sma(close, 50)
_ma100 = ta.sma(close, 100)
line line_bcu = na


if ta.crossover(_ma50, _ma100)
    x1 = bar_index[1]
    y1 = _ma50[1]
    x2 = bar_index
    y2 = _ma50
    u1 = bar_index[1]
    v1 = _ma100[1]
    u2 = bar_index
    v2 = _ma100
    
    [x,y] = f_get_intersect_coordinates(x1, y1, x2, y2, u1, v1, u2, v2)
    
    last_intersect_x := x
    last_intersect_y := y

    line_bcu := line.new(bar_index-1, last_intersect_y, bar_index, last_intersect_y, extend=extend.right, color=color.rgb(255, 255, 255), style=line.style_solid, width = 2)
//=====================================================
//I tried with this loop, but it is not working, only some of the lines are deleted

for i = 0 to 100

        if line.get_y1(line_bcu[i]) > low
            line.delete(line_bcu[i])
//=====================================================
plot(_ma50, "MA50", color.rgb(255, 255, 255, 0), 2)
plot(_ma100, "MA100", color.rgb(255, 235, 59, 0), 2)

2

There are 2 best solutions below

1
AmphibianTrading On

This is a basic code that plots a ray at the crossover/crossunders

//@version=5
indicator("My script", overlay = true)
ma1 = request.security(syminfo.ticker, '240', ta.sma(close,50))
ma2 = request.security(syminfo.ticker, '240', ta.sma(close,100))
plot(ma1, color=color.green)
plot(ma2, color=color.red)
crossover = ta.crossover(ma1, ma2)
crossunder = ta.crossunder(ma1, ma2)
crossOPrice = ta.valuewhen(crossover, ma2, 0)
crossUPrice = ta.valuewhen(crossunder, ma2, 0)

var line crossLine = na
if crossover
    (crossLine[1]).delete()
    crossLine := line.new(bar_index, crossOPrice, bar_index+1, crossOPrice, extend = extend.right)
if crossunder
    (crossLine[1]).delete()
    crossLine := line.new(bar_index, crossUPrice, bar_index+1, crossUPrice, extend = extend.right)

if (close < crossLine.get_price(bar_index) and close[1] > crossLine.get_price(bar_index)) or (close > crossLine.get_price(bar_index) and close[1] < crossLine.get_price(bar_index))
    crossLine.delete()
0
xlameee On

Ok I have started with CROSSOVER Only and I will add CROSSUNDER later so I won't mess up

The problem here I believe is pine script limitation "max_lines_count" but should be way around for the deletion of the lines. All lines are created, but some are not deleted because the "LOW" price occur after the limit of 500 in the given time frame. For higher time frame have no problems, but with lower time frame becomes a mess.

The way around should be, if possible to have a "main time frame" like 1 week or even 1 year doesn't matter in order to delete the line, if price is < of the price of that line

//@version=5
indicator("script",shorttitle = "script", overlay = true, max_lines_count = 500, max_labels_count = 500)

//INPUTS
_timeframe = input.timeframe("10", "timeframe")
_ema_input = input.int(50, "EMA")
_ma_input = input.int(100, "MA")
_ema = ta.ema(close, _ema_input)
_ma = ta.sma(close, _ma_input)



_bcu1d_ema = request.security(syminfo.ticker, _timeframe, _ema)
_bcu1d_ma = request.security(syminfo.ticker, _timeframe, _ma)


_co1d = ta.crossover(_bcu1d_ema, _bcu1d_ma)
_cop1d = ta.valuewhen(_co1d, _bcu1d_ema, 0)




var line line_bcu1d = na
var label label_bcu1d = na





if _co1d
    line_bcu1d := line.new(bar_index, _cop1d, bar_index+1, _cop1d, extend = extend.right, color=color.rgb(255, 255, 255, 0), width = 2) 
    label_bcu1d := label.new(chart.right_visible_bar_time, _cop1d, "BCU1D", xloc=xloc.bar_time, color=#00000000, textcolor=#fcfdfc, style = label.style_none, size = size.large)
if (low < line_bcu1d.get_price(bar_index))
    line_bcu1d.delete()
    label_bcu1d.delete()



//PLOT
plot(_ema, color=color.rgb(255, 255, 255, 0), linewidth = 2)
plot(_ma, color=color.rgb(255, 255, 0, 0), linewidth = 2)

EDIT: But in this example the line is not deleted also even if there are only 195 line ahead !!!! Don't understand what am I doing wrong

line-delete-image