Strategy · Pine Script v6 · On the chart

Simple Strategy

by TradersPost

Simple Strategy is an educational strategy script designed to demonstrate automated order messaging and webhook alerts.

View on GitHub

GitHub stars6on the repository
Pine versionv6current
Lines of code54
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago20 Dec 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 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

54 lines of Pine Script v6 from strategies/Simple Strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Simple Strategy.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradersPost, Inc (traderspost.io)

//@strategy_alert_message {{strategy.order.alert_message}}

//@version=6
strategy("Simple Strategy", overlay=true, fill_orders_on_standard_ohlc = true, process_orders_on_close = true)

method tags(string str) =>
    msg = str

    msg := str.replace_all(msg, '{{exchange}}', syminfo.prefix)
    msg := str.replace_all(msg, '{{ticker}}', syminfo.ticker)
    msg := str.replace_all(msg, '{{close}}', str.tostring(close))
    msg := str.replace_all(msg, '{{open}}', str.tostring(open))
    msg := str.replace_all(msg, '{{high}}', str.tostring(high))
    msg := str.replace_all(msg, '{{low}}', str.tostring(low))
    msg := str.replace_all(msg, '{{time}}', str.tostring(time))
    msg := str.replace_all(msg, '{{volume}}', str.tostring(volume))
    msg := str.replace_all(msg, '{{timenow}}', str.tostring(timenow))
    msg := str.replace_all(msg, '{{interval}}', timeframe.period)
    msg := str.replace_all(msg, '{{syminfo.currency}}', syminfo.currency)
    msg := str.replace_all(msg, '{{syminfo.basecurrency}}', syminfo.basecurrency)

    log.info(msg)

    msg

tradeDirection = input.string("Both", title = "Trade Direction", options = ['Both', 'Long Only', 'Short Only'])
group1 = "Alert Messages"
longEntryMessage = input.text_area('{\n"ticker": "{{ticker}}",\n"action": "buy",\n"price": "{{close}}",\n"time": "{{timenow}}",\n"my message": "this came from simple strategy"\n}', title = "Long Entry", group = group1)
longExitMessage = input.text_area('{\n"ticker": "{{ticker}}",\n"action": "exit",\n"price": "{{close}}",\n"time": "{{timenow}}",\n"my message": "this came from simple strategy"\n}', title = "Long Exit", group = group1)
shortEntryMessage = input.text_area('{\n"ticker": "{{ticker}}",\n"action": "sell",\n"price": "{{close}}",\n"time": "{{timenow}}",\n"my message": "this came from simple strategy"\n}', title = "Short Entry", group = group1)
shortExitMessage = input.text_area('{\n"ticker": "{{ticker}}",\n"action": "exit",\n"price": "{{close}}",\n"time": "{{timenow}}",\n"my message": "this came from simple strategy"\n}', title = "Short Exit", group = group1)

var longAllowed = tradeDirection == 'Both' or tradeDirection == 'Long Only'
var shortAllowed = tradeDirection == 'Both' or tradeDirection == 'Short Only'

longCondition = longAllowed and close > close[1]
shortCondition = shortAllowed and close < close[1]

if (longCondition)
    strategy.entry("Entry Long", strategy.long, alert_message = longEntryMessage.tags())
else if (strategy.position_size > 0)
    strategy.close("Entry Long", comment = "Close", alert_message = longExitMessage.tags())
else if (shortCondition)
    strategy.entry("Entry Short", strategy.short, alert_message = shortEntryMessage.tags())
else if (strategy.position_size < 0)
    strategy.close("Entry Short", comment = "Close", alert_message = shortExitMessage.tags())

if (barstate.islastconfirmedhistory)
    var tbl = table.new(position.bottom_right, 1, 2)
    tbl.cell(0, 0, "This is an example strategy designed to illustrate how\nto send trade messages to TradersPost for the automation\nof strategies and indicators. It should not be traded on live capital.\n\nPlease see:\nhttps://docs.traderspost.io/docs/core-concepts/webhooks\nfor complete details on how these messages work.", text_color = chart.fg_color, bgcolor = chart.bg_color, text_size = 12, text_halign = text.align_right)
    tbl.cell(0, 1, "\nTradersPost, Inc", text_formatting = text.format_bold, text_color = chart.fg_color, bgcolor = chart.bg_color, text_size = 14, text_halign = text.align_right)

Overview

Simple Strategy executes on the main price chart, entering long when the current bar closes above the previous close and entering short when it closes below the previous close. Open positions are closed on subsequent bars when directional conditions fail. Settings permit filtering trade direction to both, long only, or short only, alongside customizable JSON alert payloads with dynamic tag replacement. The strategy contains no stop loss or profit targets.

Details

Published by

TradersPost on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Trade DirectionstringBothtradeDirection
Long Entrytext{ "ticker": "{{ticker}}", "action": "buy", "price": "{{closelongEntryMessage
Long Exittext{ "ticker": "{{ticker}}", "action": "exit", "price": "{{closlongExitMessage
Short Entrytext{ "ticker": "{{ticker}}", "action": "sell", "price": "{{closshortEntryMessage
Short Exittext{ "ticker": "{{ticker}}", "action": "exit", "price": "{{closshortExitMessage
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.

Similar Pine Scripts

Doge SMA

by hackingthemarkets
474 stars

Doge SMA is a long-only moving-average crossover strategy drawn on the price chart. It calculates a fast and a slow simple moving average of close and enters a long position when the fast average crosses above the slow average, but only inside a user-defined s

Strategy v4 TrendMoving averages
Updated 4 years ago · None stated GitHub repository of Doge SMA

This strategy calculates and plots a fast Exponential Moving Average (EMA) and a slow EMA directly on the main price chart. It enters long trades when the fast EMA crosses above the slow EMA and exits when the fast EMA crosses below the slow EMA. Inputs allow

Strategy v6 TrendMoving averages
Updated today · AGPL-3.0 GitHub repository of Forven S01508 — Simple EMA20/50 Cross
330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
285 stars

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

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 6 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers