Strategy · Pine Script v5 · On the chart

200DMA last DOM - ajh

by hasnocool· code credited to muscleriot

This strategy executes a long-only trend-following system based on a simple moving average, designed primarily for daily charts.

View on GitHub

GitHub stars106on the repository
Pine versionv5runs as is
Lines of code50
Settings8inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 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

50 lines of Pine Script v5 from 200DMA last DOM - ajh.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

200DMA last DOM - ajh.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © muscleriot
//200 dma
//1 signal per month - trade only at 'end of month' to cut whipsawing in and out. 'End of Month' is whatever trading day is 
// nearest to the default day 28 of month in pinescript - there is no easy method to obtain trading end of month date.
// Day 28 is default so not to miss Feburary. 
//If > 200DMA enter long
//If < 200DMA goto cash
// Important Steve Burns used SPY TOTAL RETURN INDEX used not just SPY Price. Please use the ADJ at bottom of chart in Trading View. 
// 3 Jan 2000 to 28 Jun 2021 - https://www.newtraderu.com/2021/06/30/200-day-moving-average-vs-buy-and-hold/
//Steve Burns Results : 585.8% return on SPY vs 365% buy and hold with 12 trades
// My Results :         415% return (on SPY with ADJ) vs BUY and HOLD of 318.9% in 12 trades so not perfect
//@version=5
strategy("200DMA last DOM - ajh", overlay =true,default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital = 10000)
// Use 100% of  equity always
ma_days = input.int(defval=200, title="SMA (Chart Period)", minval=1, maxval=600)
trade_dom = input.int(defval=28, title="trade day of month or 0 for none", minval=0, maxval=31)
//e =dayofmonth(time)
// backtesting date range
from_day = input.int(defval=3, title="From Day", minval=1, maxval=31)
from_month = input.int(defval=1, title="From Month", minval=1, maxval=12)
from_year = input.int(defval=2000, title="From Year", minval=1900)
to_day = input.int(defval=28, title="To Day", minval=1, maxval=31)
to_month = input.int(defval=6, title="To Month", minval=1, maxval=12)
to_year = input.int(defval=2021, title="To Year", minval=1900)
dateStart = timestamp(syminfo.timezone, from_year, from_month, from_day, 0, 0) // 2000-01-03
dateEnd = timestamp(syminfo.timezone, to_year, to_month, to_day, 0, 0) //2021-06-28
inDateRange = time >= dateStart and time <= dateEnd
// 200 day ma 
dma200 = ta.sma(close, ma_days)
plot(dma200, color=color.red, linewidth = 2)
//Day of month
dom = dayofmonth(time_tradingday, syminfo.timezone) 
// Set trade entry conditions -
enterLong  = close > dma200
enterSale = close < dma200
TradeTime = (trade_dom == 0) ? true: (dom == trade_dom)? true: false //Is it the time (eom) to decide to go long or short? 
//bgcolor((TradeTime) ?  color.gray:na)
xLong = TradeTime and enterLong
xSell = TradeTime and enterSale
plotchar(xLong, "long","L", color=color.green, location=location.abovebar, size=size.tiny)   
plotchar(xSell, "Sell","S", color=color.red, location = location.abovebar, size = size.tiny)   
if TradeTime and inDateRange and enterLong 
    strategy.entry("long", strategy.long)
if TradeTime and inDateRange and enterSale
    strategy.close("long")
// Exit open market position when date range ends
if (not inDateRange)
    strategy.close_all()    
Expand (58 lines)

Overview

200DMA last DOM - ajh plots the moving average directly on the price chart and evaluates trading signals strictly on a user-defined day of the month, defaulting to the 28th to reduce whipsaws. If the price closes above the moving average on that day, the strategy enters long; if below, it exits to cash. It includes a backtest date filter, but operates without a protective stop loss.

Details

Published by

hasnocool on GitHub

Code credit

muscleriot, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Moving averages, Trend

Markets

Indices, Stocks

Timeframes

H4 and above

Risk Notes

  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
SMA (Chart Period)int200ma_days
trade day of month or 0 for noneint28trade_dom
From Dayint3from_day
From Monthint1from_month
From Yearint2000from_year
To Dayint28to_day
To Monthint6to_month
To Yearint2021to_year
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
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