1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.
Source Code
17 lines of Pine Script v4 from BarUpDn Strategy.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.
BarUpDn Strategy.pine
//@version=4
strategy("BarUpDn Strategy", overlay=true, initial_capital = 10000, default_qty_value = 10000, default_qty_type = strategy.cash)
BarsUp = input(1)
BarsDown = input(1)
// Strategy Backesting
startDate = input(timestamp("2021-01-01T00:00:00"), type = input.time)
finishDate = input(timestamp("2021-12-31T00:00:00"), type = input.time)
time_cond = time >= startDate and time <= finishDate
// Messages for buy and sell
message_buy = input("{{strategy.order.alert_message}}", title="Buy message")
message_sell = input("{{strategy.order.alert_message}}", title="Sell message")
if (close > open and open > close[BarsUp]) and time_cond
strategy.entry("BarUp", strategy.long, stop = high + syminfo.mintick, alert_message = message_buy)
if (close < open and open < close[BarsDown]) and time_cond
strategy.entry("BarDn", strategy.short, stop = low + syminfo.mintick, alert_message = message_sell)
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
Expand (21 lines)
Overview
BarUpDn Strategy enters a long position with a stop order above the high when the current candle closes above its open and its open is higher than the close of a specified lookback bar. Conversely, it enters short when the candle closes below its open and its open is below the lookback close. Drawn directly on the main chart, key settings include the lookback periods and backtesting date range filters. The strategy lacks built-in stop loss or take profit orders.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
BarsUp
int
1
BarsUp
BarsDown
int
1
BarsDown
startDate
time
timestamp("2021-01-01T00:00:00")
startDate
finishDate
time
timestamp("2021-12-31T00:00:00")
finishDate
Buy message
string
{{strategy.order.alert_message}}
message_buy
Sell message
string
{{strategy.order.alert_message}}
message_sell
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This trend-following strategy executes a classic Donchian channel breakout system designed to cross-validate backtest results against external framework benchmarks. It triggers a long entry when the closing price breaks above the highest high of the prior entr
This strategy trades price action pin bar patterns aligned with moving average trend alignment on the main price chart. It defines uptrends or downtrends using a fan arrangement of three moving averages (6 EMA, 18 EMA, and 50 SMA). Entry conditions require a b
Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish
This strategy plots running pivot highs as red R circles and pivot lows as blue S circles directly on the price chart. It computes an EMA 3 and a Cto trend line from close prices, then generates long signals on upward EMA-Cto crossovers combined with price pos
The Chained Linear Regression Reversal Strategy plots linear regression channels with upper and lower standard deviation bands directly on the price chart across a user-defined time window. When price breaks out of the static channel for a confirmed number of
This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the