Created
December 6, 2025 01:12
-
-
Save twinsant/9a89a0658eda8f2e1f135b0954818b79 to your computer and use it in GitHub Desktop.
SuperTrend stratagy in Pine language.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ | |
| // © AtmanAn | |
| //@version=5 | |
| //indicator("Super Trend", overlay=true) | |
| strategy("Super Trend", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=500, commission_type=strategy.commission.percent, commission_value=0.0006) | |
| fromYear = input(2022, "Year From") | |
| fromMonth = input(5, "Month From") | |
| fromDay = input(1, "Day From") | |
| toYear = input(2022, "Year To") | |
| toMonth = input(12, "Month To") | |
| m = input(10, "Fast EMA") | |
| n = input(3, "Upper range") | |
| k = input(3, "Lower range") | |
| op = input(true, "Long / Short") | |
| start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start window | |
| finish = timestamp(toYear, toMonth, 31, 23, 59) // backtest finish window | |
| window() => time >= start and time <= finish ? true : false // create function "within window of time" | |
| f_print(_text) => var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_none, color.white, size.large, text.align_left), label.set_xy(_label, bar_index, ta.highest(10)[1]), label.set_text(_label, _text) | |
| //plotchar(default_m, "v", "", location.top, size = size.tiny) | |
| super_trend(source, m, n, k) => | |
| ema7 = ta.ema(source, m) | |
| atr = ta.atr(m) | |
| //[macdLine, signalLine, histLine] = ta.macd(close, 7, 30, 7) | |
| //up = (high+low)/2+n*atr | |
| up = ema7 + n*atr | |
| final_up = up | |
| if na(up[1]) == false | |
| if up < final_up[1] or source[1] > final_up[1] | |
| final_up := up | |
| else | |
| final_up := final_up[1] | |
| //down = (high+low)/2-k*atr | |
| down = ema7 - k*atr | |
| final_down = down | |
| if na(down[1]) == false | |
| if down > final_down[1] or source[1] < final_down[1] | |
| final_down := down | |
| else | |
| final_down := final_down[1] | |
| super = final_up | |
| if na(super[1]) == false | |
| if super[1] == final_up[1] and source <= final_up | |
| super := final_up | |
| else | |
| if super[1] == final_up[1] and source > final_up | |
| super := final_down | |
| else | |
| if super[1] == final_down[1] and source >= final_down | |
| super := final_down | |
| else | |
| if super[1] == final_down[1] and source < final_down | |
| super := final_up | |
| super | |
| super = super_trend(close, m, n, k) | |
| ema180 = ta.ema(close, 180) | |
| color c = na | |
| if op == false | |
| strategy.entry("short", strategy.short, when=window() and ta.crossunder(close, super)) | |
| if window() and close < super | |
| c := color.new(color.red, 90) | |
| strategy.close("short", when=window() and ta.crossover(close, super)) | |
| else | |
| strategy.entry("long", strategy.long, when=window() and ta.crossover(close, super)) | |
| if window() and close > super | |
| c := color.new(color.green, 90) | |
| strategy.close("long", when=window() and ta.crossunder(close, super)) | |
| bgcolor(color=c) | |
| plot(close, title="close", color=color.new(color.blue, 30)) | |
| //plot(close[1], color=color.new(color.blue, 70)) | |
| // plot(ema7, title="ema", color=color.yellow) | |
| // // plot(atr, title="atr") | |
| // plot(up, title="upper", color=color.new(color.green, 70)) | |
| // plot(final_up, title="fupper", color=color.new(color.green, 30)) | |
| // plot(down, title="lower", color=color.new(color.red, 70)) | |
| // plot(final_down, title="flower", color=color.new(color.red, 30)) | |
| plot(super, title="st", color=close < super ? color.red : color.green, linewidth=1) | |
| // [u, d] = ta.supertrend(3, 10) | |
| // plot(u, color=d < 0 ? color.green : color.red) | |
| // plot(macdLine, color=color.blue) | |
| // plot(signalLine, color=color.orange) | |
| // plot(histLine, color=color.red, style=plot.style_histogram) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment