// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=6 indicator('BFM Magic Trendlines @bitcoinfundmgr', shorttitle='BFM Magic Trendlines', overlay=true) // Set 1 (largest) leftbars1 = input.int(300, minval=1, title='Set 1: Pivot Detection: Left Bars') rightbars1 = input.int(200, minval=1, title='Set 1: Pivot Detection: Right Bars') // Set 2 (~20% less) leftbars2 = input.int(240, minval=1, title='Set 2: Pivot Detection: Left Bars') rightbars2 = input.int(160, minval=1, title='Set 2: Pivot Detection: Right Bars') // Set 3 (~20% less than Set 2) leftbars3 = input.int(192, minval=1, title='Set 3: Pivot Detection: Left Bars') rightbars3 = input.int(128, minval=1, title='Set 3: Pivot Detection: Right Bars') // Set 4 (~20% less than Set 3) leftbars4 = input.int(154, minval=1, title='Set 4: Pivot Detection: Left Bars') rightbars4 = input.int(102, minval=1, title='Set 4: Pivot Detection: Right Bars') plotpivots = input(true, title='Plot Pivots') // TRENDLINE CODE // -------------- get_slope(x1, x2, y1, y2) => m = (y2 - y1) / (x2 - x1) m get_y_intercept(m, x1, y1) => b = y1 - m * x1 b get_y(m, b, ts) => Y = m * ts + b Y // Set 1 ph1 = ta.pivothigh(high, leftbars1, rightbars1) pl1 = ta.pivotlow(low, leftbars1, rightbars1) ph_cond1 = not na(ph1) pl_cond1 = not na(pl1) phv11 = ta.valuewhen(ph_cond1, high[rightbars1], 0) phb11 = ta.valuewhen(ph_cond1, bar_index[rightbars1], 0) phv21 = ta.valuewhen(ph_cond1, high[rightbars1], 1) phb21 = ta.valuewhen(ph_cond1, bar_index[rightbars1], 1) plv11 = ta.valuewhen(pl_cond1, low[rightbars1], 0) plb11 = ta.valuewhen(pl_cond1, bar_index[rightbars1], 0) plv21 = ta.valuewhen(pl_cond1, low[rightbars1], 1) plb21 = ta.valuewhen(pl_cond1, bar_index[rightbars1], 1) // Set 2 ph2 = ta.pivothigh(high, leftbars2, rightbars2) pl2 = ta.pivotlow(low, leftbars2, rightbars2) ph_cond2 = not na(ph2) pl_cond2 = not na(pl2) phv12 = ta.valuewhen(ph_cond2, high[rightbars2], 0) phb12 = ta.valuewhen(ph_cond2, bar_index[rightbars2], 0) phv22 = ta.valuewhen(ph_cond2, high[rightbars2], 1) phb22 = ta.valuewhen(ph_cond2, bar_index[rightbars2], 1) plv12 = ta.valuewhen(pl_cond2, low[rightbars2], 0) plb12 = ta.valuewhen(pl_cond2, bar_index[rightbars2], 0) plv22 = ta.valuewhen(pl_cond2, low[rightbars2], 1) plb22 = ta.valuewhen(pl_cond2, bar_index[rightbars2], 1) // Set 3 ph3 = ta.pivothigh(high, leftbars3, rightbars3) pl3 = ta.pivotlow(low, leftbars3, rightbars3) ph_cond3 = not na(ph3) pl_cond3 = not na(pl3) phv13 = ta.valuewhen(ph_cond3, high[rightbars3], 0) phb13 = ta.valuewhen(ph_cond3, bar_index[rightbars3], 0) phv23 = ta.valuewhen(ph_cond3, high[rightbars3], 1) phb23 = ta.valuewhen(ph_cond3, bar_index[rightbars3], 1) plv13 = ta.valuewhen(pl_cond3, low[rightbars3], 0) plb13 = ta.valuewhen(pl_cond3, bar_index[rightbars3], 0) plv23 = ta.valuewhen(pl_cond3, low[rightbars3], 1) plb23 = ta.valuewhen(pl_cond3, bar_index[rightbars3], 1) // Set 4 ph4 = ta.pivothigh(high, leftbars4, rightbars4) pl4 = ta.pivotlow(low, leftbars4, rightbars4) ph_cond4 = not na(ph4) pl_cond4 = not na(pl4) phv14 = ta.valuewhen(ph_cond4, high[rightbars4], 0) phb14 = ta.valuewhen(ph_cond4, bar_index[rightbars4], 0) phv24 = ta.valuewhen(ph_cond4, high[rightbars4], 1) phb24 = ta.valuewhen(ph_cond4, bar_index[rightbars4], 1) plv14 = ta.valuewhen(pl_cond4, low[rightbars4], 0) plb14 = ta.valuewhen(pl_cond4, bar_index[rightbars4], 0) plv24 = ta.valuewhen(pl_cond4, low[rightbars4], 1) plb24 = ta.valuewhen(pl_cond4, bar_index[rightbars4], 1) // Plot Pivots (all sets combined if plotpivots is true) plotshape(plotpivots and ph_cond1 ? ph1 : na, style=shape.circle, location=location.abovebar, color=color.new(color.black, 70), title='Pivot High Set1', offset=-rightbars1) plotshape(plotpivots and ph_cond2 ? ph2 : na, style=shape.circle, location=location.abovebar, color=color.new(color.black, 70), title='Pivot High Set2', offset=-rightbars2) plotshape(plotpivots and ph_cond3 ? ph3 : na, style=shape.circle, location=location.abovebar, color=color.new(color.black, 70), title='Pivot High Set3', offset=-rightbars3) plotshape(plotpivots and ph_cond4 ? ph4 : na, style=shape.circle, location=location.abovebar, color=color.new(color.black, 70), title='Pivot High Set4', offset=-rightbars4) plotshape(plotpivots and pl_cond1 ? pl1 : na, style=shape.circle, location=location.belowbar, color=color.new(color.black, 70), title='Pivot Low Set1', offset=-rightbars1) plotshape(plotpivots and pl_cond2 ? pl2 : na, style=shape.circle, location=location.belowbar, color=color.new(color.black, 70), title='Pivot Low Set2', offset=-rightbars2) plotshape(plotpivots and pl_cond3 ? pl3 : na, style=shape.circle, location=location.belowbar, color=color.new(color.black, 70), title='Pivot Low Set3', offset=-rightbars3) plotshape(plotpivots and pl_cond4 ? pl4 : na, style=shape.circle, location=location.belowbar, color=color.new(color.black, 70), title='Pivot Low Set4', offset=-rightbars4) // Plot pivot values (optional, all sets) plot(plotpivots and ph_cond1 ? high[rightbars1] : na, color=color.new(color.blue, 80), offset=-rightbars1, title='PH Set1') plot(plotpivots and pl_cond1 ? low[rightbars1] : na, color=color.new(color.blue, 80), offset=-rightbars1, title='PL Set1') plot(plotpivots and ph_cond2 ? high[rightbars2] : na, color=color.new(color.blue, 80), offset=-rightbars2, title='PH Set2') plot(plotpivots and pl_cond2 ? low[rightbars2] : na, color=color.new(color.blue, 80), offset=-rightbars2, title='PL Set2') plot(plotpivots and ph_cond3 ? high[rightbars3] : na, color=color.new(color.blue, 80), offset=-rightbars3, title='PH Set3') plot(plotpivots and pl_cond3 ? low[rightbars3] : na, color=color.new(color.blue, 80), offset=-rightbars3, title='PL Set3') plot(plotpivots and ph_cond4 ? high[rightbars4] : na, color=color.new(color.blue, 80), offset=-rightbars4, title='PH Set4') plot(plotpivots and pl_cond4 ? low[rightbars4] : na, color=color.new(color.blue, 80), offset=-rightbars4, title='PL Set4') // Set 1 Vars var int res_x1_1 = na var float res_y1_1 = na var int res_x2_1 = na var float res_y2_1 = na var int sup_x1_1 = na var float sup_y1_1 = na var int sup_x2_1 = na var float sup_y2_1 = na // Set 1 Resistance if ph_cond1 res_x1_1 := phb11 res_y1_1 := phv11 res_x2_1 := phb21 res_y2_1 := phv21 res_m1 = get_slope(res_x1_1, res_x2_1, res_y1_1, res_y2_1) res_b1 = get_y_intercept(res_m1, res_x1_1, res_y1_1) res_y1 = get_y(res_m1, res_b1, bar_index) // Set 1 Support if pl_cond1 sup_x1_1 := plb11 sup_y1_1 := plv11 sup_x2_1 := plb21 sup_y2_1 := plv21 sup_m1 = get_slope(sup_x1_1, sup_x2_1, sup_y1_1, sup_y2_1) sup_b1 = get_y_intercept(sup_m1, sup_x1_1, sup_y1_1) sup_y1 = get_y(sup_m1, sup_b1, bar_index) // Set 1 Lines if ph_cond1 line.new(phb11, phv11, bar_index, res_y1, style=line.style_dotted, color=color.new(color.gray, 0)) if pl_cond1 line.new(plb11, plv11, bar_index, sup_y1, style=line.style_dotted, color=color.new(color.gray, 0)) // Set 2 Vars var int res_x1_2 = na var float res_y1_2 = na var int res_x2_2 = na var float res_y2_2 = na var int sup_x1_2 = na var float sup_y1_2 = na var int sup_x2_2 = na var float sup_y2_2 = na // Set 2 Resistance if ph_cond2 res_x1_2 := phb12 res_y1_2 := phv12 res_x2_2 := phb22 res_y2_2 := phv22 res_m2 = get_slope(res_x1_2, res_x2_2, res_y1_2, res_y2_2) res_b2 = get_y_intercept(res_m2, res_x1_2, res_y1_2) res_y2 = get_y(res_m2, res_b2, bar_index) // Set 2 Support if pl_cond2 sup_x1_2 := plb12 sup_y1_2 := plv12 sup_x2_2 := plb22 sup_y2_2 := plv22 sup_m2 = get_slope(sup_x1_2, sup_x2_2, sup_y1_2, sup_y2_2) sup_b2 = get_y_intercept(sup_m2, sup_x1_2, sup_y1_2) sup_y2 = get_y(sup_m2, sup_b2, bar_index) // Set 2 Lines if ph_cond2 line.new(phb12, phv12, bar_index, res_y2, style=line.style_dotted, color=color.new(color.red, 0)) if pl_cond2 line.new(plb12, plv12, bar_index, sup_y2, style=line.style_dotted, color=color.new(color.red, 0)) // Set 3 Vars var int res_x1_3 = na var float res_y1_3 = na var int res_x2_3 = na var float res_y2_3 = na var int sup_x1_3 = na var float sup_y1_3 = na var int sup_x2_3 = na var float sup_y2_3 = na // Set 3 Resistance if ph_cond3 res_x1_3 := phb13 res_y1_3 := phv13 res_x2_3 := phb23 res_y2_3 := phv23 res_m3 = get_slope(res_x1_3, res_x2_3, res_y1_3, res_y2_3) res_b3 = get_y_intercept(res_m3, res_x1_3, res_y1_3) res_y3 = get_y(res_m3, res_b3, bar_index) // Set 3 Support if pl_cond3 sup_x1_3 := plb13 sup_y1_3 := plv13 sup_x2_3 := plb23 sup_y2_3 := plv23 sup_m3 = get_slope(sup_x1_3, sup_x2_3, sup_y1_3, sup_y2_3) sup_b3 = get_y_intercept(sup_m3, sup_x1_3, sup_y1_3) sup_y3 = get_y(sup_m3, sup_b3, bar_index) // Set 3 Lines if ph_cond3 line.new(phb13, phv13, bar_index, res_y3, style=line.style_dotted, color=color.new(color.green, 0)) if pl_cond3 line.new(plb13, plv13, bar_index, sup_y3, style=line.style_dotted, color=color.new(color.green, 0)) // Set 4 Vars var int res_x1_4 = na var float res_y1_4 = na var int res_x2_4 = na var float res_y2_4 = na var int sup_x1_4 = na var float sup_y1_4 = na var int sup_x2_4 = na var float sup_y2_4 = na // Set 4 Resistance if ph_cond4 res_x1_4 := phb14 res_y1_4 := phv14 res_x2_4 := phb24 res_y2_4 := phv24 res_m4 = get_slope(res_x1_4, res_x2_4, res_y1_4, res_y2_4) res_b4 = get_y_intercept(res_m4, res_x1_4, res_y1_4) res_y4 = get_y(res_m4, res_b4, bar_index) // Set 4 Support if pl_cond4 sup_x1_4 := plb14 sup_y1_4 := plv14 sup_x2_4 := plb24 sup_y2_4 := plv24 sup_m4 = get_slope(sup_x1_4, sup_x2_4, sup_y1_4, sup_y2_4) sup_b4 = get_y_intercept(sup_m4, sup_x1_4, sup_y1_4) sup_y4 = get_y(sup_m4, sup_b4, bar_index) // Set 4 Lines if ph_cond4 line.new(phb14, phv14, bar_index, res_y4, style=line.style_dotted, color=color.new(color.blue, 0)) if pl_cond4 line.new(plb14, plv14, bar_index, sup_y4, style=line.style_dotted, color=color.new(color.blue, 0))