Strategy · Pine Script v5 · On the chart

200DMA Entry

by FinDeckDao· code credited to jfgrissom

The 200DMA Entry strategy plots the 200-day simple moving average on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code55
Settings0inputs
LicenceMIT
Last update1 year ago15 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

55 lines of Pine Script v5 from backtesting/tradingview/200DMAEntry.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

200DMAEntry.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jfgrissom

//@version=5
strategy("200DMA Entry", overlay=true, initial_capital=10000)

// Define the 200-day moving average
ma200 = ta.sma(close, 200)

// Enter a trade when the previous bar closes above the 200 MA
longCondition = ta.crossover(close[1], ma200)

// Set the take profit level at 150% ROI
takeProfitLevel = strategy.position_avg_price * 2.5

// Variables to store the bar index and price of entry and exit
var float entryPrice = na
var int entryBarIndex = na
var float orderSize = na

// Enter long position using 1% of the total account equity
if (longCondition)
    orderSize := 0.01 * strategy.equity / close
    strategy.entry("Long", strategy.long, qty=orderSize)
    entryPrice := close
    entryBarIndex := bar_index

// Exit position when price reaches the take profit level
if (strategy.position_size > 0 and close >= takeProfitLevel)
    strategy.close("Long")
    line.new(x1=entryBarIndex, y1=entryPrice, x2=bar_index, y2=close, color=color.red)

// Plot the 200 MA on the chart
plot(ma200, title="200-day MA", color=color.blue)

// Function to format equity to two decimal places
formatEquity(equity) =>
    str.tostring(equity, "#.00")

// Create a label to display equity in the top right corner in bright green
var label equityLabelTopRight = na
if (na(equityLabelTopRight))
    equityLabelTopRight := label.new(x=bar_index, y=na, text="", xloc=xloc.bar_index, yloc=yloc.abovebar, color=color.new(color.black, 0), textcolor=color.green, style=label.style_label_down)

label.set_xy(equityLabelTopRight, bar_index + 1, high * 1.05)
label.set_text(equityLabelTopRight, "Equity: $" + formatEquity(strategy.equity))

// Display order size on the chart
var label orderSizeLabel = na
if (na(orderSizeLabel))
    orderSizeLabel := label.new(x=bar_index, y=na, text="", xloc=xloc.bar_index, yloc=yloc.abovebar, color=color.new(color.black, 0), textcolor=color.white, style=label.style_label_down)

if (not na(orderSize))
    label.set_xy(orderSizeLabel, bar_index + 1, high * 0.95)
    label.set_text(orderSizeLabel, "Order Size: " + str.tostring(orderSize, "#.000"))

Overview

200DMA Entry generates long signals when the close crosses above the 200 MA on the prior bar. Positions are sized at 1 percent of account equity. The strategy exits at a 2.5 times take profit level. It displays labels for current equity and order size. There is no stop loss.

Details

Published by

FinDeckDao on GitHub

Code credit

jfgrissom, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss
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

257 stars

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

Strategy v6 TrendMoving averages Repaints
Updated 7 weeks ago · None stated GitHub repository of Best Chart 1m Strategy Candle Bar

12/26-IT Strategy

by hasnocool
106 stars

This strategy executes trades based on crossovers between a 12-period and a 26-period exponential moving average (EMA). Long positions can be filtered by pivot resistance breakouts and RSI boundary ranges. For trade management, it provides two configurable par

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 12/26-IT Strategy

均线突破交易系统

by tangly1024
76 stars

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

Strategy v5 TrendMoving averages
Updated 5 months ago · None stated GitHub repository of 均线突破交易系统
43 stars

This strategy executes trades on short-term cryptocurrency charts using a combination of pivot support and resistance levels and a multi-pole smoothed trend filter. Entries trigger when price crosses the smoothed trend line and confirms above or below a short-

Strategy v5 TrendBreakout Repaints
Updated 2 years ago · None stated GitHub repository of SOL 1m - Strategy Candle Bar

ADA4H Optuna Strategy

by skywalker0803r
42 stars

This strategy trades longs and shorts on the price chart. Long entries require a close above the prior Donchian high, a close above a long-term SMA, and RSI above a threshold, with size set as a percentage of equity. Long exits use a stop at the prior Donchian

Strategy v5 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of ADA4H Optuna Strategy
16 stars

This strategy plots Donchian channels, trailing stops, and profit targets directly on the price chart. It enters breakout trades using a 55-bar Donchian channel and sizes positions according to an ATR-based risk percentage per unit. The system adds units as pr

Strategy v6 TrendBreakout
Updated 12 months ago · None stated GitHub repository of Seykota Alt 10: Profit Targets

More Moving averages scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers