// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © CryptoSeaTV //@version=5 //Last Modified: 11/10/2024 10:38:54 PM strategy("MTF Supertrend [CryptoSea]", overlay=true,default_qty_value = 100, default_qty_type = strategy.percent_of_equity) supertrendGroup = "Supertrend Settings" factor = input.float(3.0, title="Factor", group=supertrendGroup, tooltip="The multiplier by which the ATR will get multiplied.") atrPeriod = input.int(10, title="ATR Period", group=supertrendGroup, tooltip="Length of ATR.") supertrendThreshold = input.float(10, title="% Threshold for Display", group=supertrendGroup, tooltip="Supertrend values within this % of the close price will be displayed.") lookaheadOption = input.string("On", title="Lookahead Option", options=["On", "Off"], group=supertrendGroup, tooltip="Choose 'On' to see trends based on real-time data, or 'Off' to only show closed candle data.") displayGroup = "Display Options" plotOptions = input.string("Both", title="Plot Options", options=["Both", "Supertrends", "Table"], group=displayGroup) theme = input.string("Dark", title="Theme", options=["Dark", "Light"], group=displayGroup, tooltip="Choose which theme you are using and the table will fit it.") timeframeVisibilityGroup = "Timeframe Visibility" show1M = input.bool(true, title="1m", group=timeframeVisibilityGroup, inline="1-5M") show5M = input.bool(true, title="5m", group=timeframeVisibilityGroup, inline="1-5M") show15M = input.bool(true, title="15m", group=timeframeVisibilityGroup, inline="15-30M") show30M = input.bool(true, title="30m", group=timeframeVisibilityGroup, inline="15-30M") show45M = input.bool(true, title="45m", group=timeframeVisibilityGroup, inline="45-1H") show1H = input.bool(true, title="1h", group=timeframeVisibilityGroup, inline="45-1H") show2H = input.bool(true, title="2h", group=timeframeVisibilityGroup, inline="2-3H") show3H = input.bool(true, title="3h", group=timeframeVisibilityGroup, inline="2-3H") show4H = input.bool(true, title="4h", group=timeframeVisibilityGroup, inline="4H-D") showD = input.bool(true, title="D", group=timeframeVisibilityGroup, inline="4H-D") showW = input.bool(true, title="W", group=timeframeVisibilityGroup, inline="W-M") showM = input.bool(true, title="M", group=timeframeVisibilityGroup, inline="W-M") candleGroup = "Candle Colours" tradColours = input.bool(false, "Neutral", group=candleGroup) gray = #989898 white = theme == "Light" ? color.black : color.white background = theme == "Light" ? color.new(color.white, 95) : color.new(color.black, 95) textColor = theme == "Light" ? color.new(color.black, 0) : color.new(color.white, 0) bearColour = #f23645 bullColour = #089981 calcSupertrend(tf) => lookaheadSetting = lookaheadOption == "On" ? barmerge.lookahead_on : barmerge.lookahead_off [supertrend, direction] = request.security(syminfo.tickerid, tf, ta.supertrend(factor, atrPeriod), lookahead=lookaheadSetting) [supertrend, direction] // Function to determine if one timeframe is hig her than the current isHigherTF(currentTF, checkTF) => s = array.new_string() array.push(s, "1") array.push(s, "5") array.push(s, "15") array.push(s, "30") array.push(s, "45") array.push(s, "60") array.push(s, "120") array.push(s, "180") array.push(s, "240") array.push(s, "D") array.push(s, "W") array.push(s, "M") idxCurrent = array.indexof(s, currentTF) idxCheck = array.indexof(s, checkTF) idxCheck >= idxCurrent calculateColour(direction) => direction == -1 ? bullColour : direction == 1 ? bearColour : color.new(white,100) extPlotLength = 50 shouldPlot(supertrend, direction, closePrice, threshold, isEnabled) => isEnabled and (plotOptions == "Both" or plotOptions == "Supertrends") and ta.barssince(math.abs(supertrend - closePrice) / closePrice <= threshold / 100) < extPlotLength tableText(direction) => direction == -1 ? "Bullish" : direction == 1 ? "Bearish" : "NA" type trend_info string tf float st float dir float dir1 method changed(trend_info this) => this.dir != this.dir1 trendTable = array.new() UpdateTrend(tf) => [supertrend, direction] = calcSupertrend(tf) if isHigherTF(timeframe.period, tf) trendTable.push(trend_info.new(tf,supertrend[1], direction,direction[1])) [supertrend, direction] [supertrend1, direction1] = UpdateTrend("1") [supertrend5, direction5] = UpdateTrend("5") [supertrend15, direction15] = UpdateTrend("15") [supertrend30, direction30] = UpdateTrend("30") [supertrend45, direction45] = UpdateTrend("45") [supertrend60, direction60] = UpdateTrend("60") [supertrend120, direction120] = UpdateTrend("120") [supertrend180, direction180] = UpdateTrend("180") [supertrend240, direction240] = UpdateTrend("240") [supertrendD, directionD] = UpdateTrend("D") [supertrendW, directionW] = UpdateTrend("W") [supertrendM, directionM] = UpdateTrend("M") TradeRR = input.float(3) var int trend_counter = 0 var float trend_SL = na var float SL = na var float TP = na bool changed = false for trend in trendTable if trend.changed() changed := true if trend_counter > 0 if trend.dir == 1 trend_SL := trend.st trend_counter := -1 else if trend_SL > trend.st trend_SL := trend.st trend_counter += 1 else if trend_counter < 0 if trend.dir == -1 trend_SL := trend.st trend_counter := 1 else if trend_SL < trend.st trend_SL := trend.st trend_counter -= 1 else if trend.dir == 1 trend_SL := trend.st trend_counter := -1 if trend.dir == -1 trend_SL := trend.st trend_counter := 1 if strategy.position_size == 0 if trend_counter >= 4 and changed strategy.entry("Long",strategy.long) SL := trend_SL risk = close - trend_SL TP := close+risk * TradeRR strategy.exit("LongExit","Long",stop = SL,limit = TP) if trend_counter <= -4 and changed strategy.entry("Short",strategy.short) SL := trend_SL risk = trend_SL - close TP := close-risk * TradeRR strategy.exit("ShortExit","Short",stop = SL,limit = TP) p1=plot(strategy.position_avg_price,"Entry",color=color.gray,style = plot.style_linebr) p2=plot(strategy.position_size !=0 ? SL: na,"SL",color=color.red,style = plot.style_linebr) p3=plot(strategy.position_size !=0 ? TP: na,"TP",color=color.green,style = plot.style_linebr) fill(p1,p2,color.rgb(255, 82, 82, 86)) fill(p1,p3,color.rgb(76, 175, 79, 86)) plot(show1M and isHigherTF(timeframe.period, "1") and shouldPlot(supertrend1, direction1, close, supertrendThreshold, show1M) ? supertrend1 : na, "Supertrend 1 Minute", color=calculateColour(direction1), style=plot.style_linebr) plot(show5M and isHigherTF(timeframe.period, "5") and shouldPlot(supertrend5, direction5, close, supertrendThreshold, show5M) ? supertrend5 : na, "Supertrend 5 Minutes", color=calculateColour(direction5), style=plot.style_linebr) plot(show15M and isHigherTF(timeframe.period, "15") and shouldPlot(supertrend15, direction15, close, supertrendThreshold, show15M) ? supertrend15 : na, "Supertrend 15 Minutes", color=calculateColour(direction15), style=plot.style_linebr) plot(show30M and isHigherTF(timeframe.period, "30") and shouldPlot(supertrend30, direction30, close, supertrendThreshold, show30M) ? supertrend30 : na, "Supertrend 30 Minutes", color=calculateColour(direction30), style=plot.style_linebr) plot(show45M and isHigherTF(timeframe.period, "45") and shouldPlot(supertrend45, direction45, close, supertrendThreshold, show45M) ? supertrend45 : na, "Supertrend 45 Minutes", color=calculateColour(direction45), style=plot.style_linebr) plot(show1H and isHigherTF(timeframe.period, "60") and shouldPlot(supertrend60, direction60, close, supertrendThreshold, show1H) ? supertrend60 : na, "Supertrend 1 Hour", color=calculateColour(direction60), style=plot.style_linebr) plot(show2H and isHigherTF(timeframe.period, "120") and shouldPlot(supertrend120, direction120, close, supertrendThreshold, show2H) ? supertrend120 : na, "Supertrend 2 Hours", color=calculateColour(direction120), style=plot.style_linebr) plot(show3H and isHigherTF(timeframe.period, "180") and shouldPlot(supertrend180, direction180, close, supertrendThreshold, show3H) ? supertrend180 : na, "Supertrend 3 Hours", color=calculateColour(direction180), style=plot.style_linebr) plot(show4H and isHigherTF(timeframe.period, "240") and shouldPlot(supertrend240, direction240, close, supertrendThreshold, show4H) ? supertrend240 : na, "Supertrend 4 Hours", color=calculateColour(direction240), style=plot.style_linebr) plot(showD and isHigherTF(timeframe.period, "D") and shouldPlot(supertrendD, directionD, close, supertrendThreshold, showD) ? supertrendD : na, "Supertrend 1 Day", color=calculateColour(directionD), style=plot.style_linebr) plot(showW and isHigherTF(timeframe.period, "W") and shouldPlot(supertrendW, directionW, close, supertrendThreshold, showW) ? supertrendW : na, "Supertrend 1 Week", color=calculateColour(directionW), style=plot.style_linebr) plot(showM and isHigherTF(timeframe.period, "M") and shouldPlot(supertrendM, directionM, close, supertrendThreshold, showM) ? supertrendM : na, "Supertrend 1 Month", color=calculateColour(directionM), style=plot.style_linebr) // Display table var table t = table.new(position.top_right, 50, 50, color.new(color.black,100), color.new(color.black,100), 1, color.new(color.black,1),1) if (barstate.isconfirmed and (plotOptions == "Both" or plotOptions == "Table")) if isHigherTF(timeframe.period, "1") and show1M table.cell(t, 0, 1, "1m", bgcolor = background, text_color = textColor) table.cell(t, 1, 1, tableText(direction1), bgcolor = calculateColour(direction1), text_color = textColor) if isHigherTF(timeframe.period, "5") and show5M table.cell(t, 0, 2, "5m", bgcolor = background, text_color = textColor) table.cell(t, 1, 2, tableText(direction5), bgcolor = calculateColour(direction5), text_color = textColor) if isHigherTF(timeframe.period, "15") and show15M table.cell(t, 0, 3, "15m", bgcolor = background, text_color = textColor) table.cell(t, 1, 3, tableText(direction15), bgcolor = calculateColour(direction15), text_color = textColor) if isHigherTF(timeframe.period, "30") and show30M table.cell(t, 0, 4, "30m", bgcolor = background, text_color = textColor) table.cell(t, 1, 4, tableText(direction30), bgcolor = calculateColour(direction30), text_color = textColor) if isHigherTF(timeframe.period, "45") and show45M table.cell(t, 0, 5, "45m", bgcolor = background, text_color = textColor) table.cell(t, 1, 5, tableText(direction45), bgcolor = calculateColour(direction45), text_color = textColor) if isHigherTF(timeframe.period, "60") and show1H table.cell(t, 0, 6, "1h", bgcolor = background, text_color = textColor) table.cell(t, 1, 6, tableText(direction60), bgcolor = calculateColour(direction60), text_color = textColor) if isHigherTF(timeframe.period, "120") and show2H table.cell(t, 0, 7, "2h", bgcolor = background, text_color = textColor) table.cell(t, 1, 7, tableText(direction120), bgcolor = calculateColour(direction120), text_color = textColor) if isHigherTF(timeframe.period, "240") and show4H table.cell(t, 0, 8, "4h", bgcolor = background, text_color = textColor) table.cell(t, 1, 8, tableText(direction240), bgcolor = calculateColour(direction240), text_color = textColor) if isHigherTF(timeframe.period, "D") and showD table.cell(t, 0, 9, "D", bgcolor = background, text_color = textColor) table.cell(t, 1, 9, tableText(directionD), bgcolor = calculateColour(directionD), text_color = textColor) if isHigherTF(timeframe.period, "W") and showW table.cell(t, 0, 10, "W", bgcolor = background, text_color = textColor) table.cell(t, 1, 10, tableText(directionW), bgcolor = calculateColour(directionW), text_color = textColor) if isHigherTF(timeframe.period, "M") and showM table.cell(t, 0, 11, "M", bgcolor = background, text_color = textColor) table.cell(t, 1, 11, tableText(directionM), bgcolor = calculateColour(directionM), text_color = textColor) //Candle Colouring barcolor(tradColours ? close > open ? white : gray : na)