Strategy · Pine Script v4 · On the chart

Buy and Hold entry finder Strategy

by hasnocool· code credited to runescapeyttanic

This strategy searches for long buy-and-hold entries on the price chart.

View on GitHub

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code62
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

62 lines of Pine Script v4 from Buy and Hold entry finder Strategy.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Buy and Hold entry finder Strategy.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © runescapeyttanic
//@version=4
strategy("Buy and Hold entry finder Strategy",pyramiding=10000, overlay=true,initial_capital=0,default_qty_type=strategy.cash,default_qty_value=1000,currency = currency.EUR,commission_type=strategy.commission.cash_per_order,commission_value=0)
//INPUTS##################################################################################################################
maxEmaDistance = input(title="Maximum EMA Distance", type=input.float, step=0.01, defval=50000)
emalength = input(title="EMA Length", type=input.integer,defval=200)
// Make input options that configure backtest date range
startDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
     defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
     defval=2020, minval=1800, maxval=2100)
endDate = input(title="End Date", type=input.integer,
     defval=12, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer,
     defval=02, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer,
     defval=2021, minval=1800, maxval=2100)
endDate1=endDate-1
//starttag
//startmonat
//MACD########################################################################################################################
fast_length=12
slow_length=26
src=close
col_macd=#0094ff
fast_ma = ema(src, fast_length)
slow_ma = ema(src, slow_length)
macd = fast_ma - slow_ma
//EMA Distance CALC########################################################################################################
ma1 =ema(close,emalength)
distFromMean = close - ma1
inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate1, 0, 0))
longCondition = (distFromMean<=maxEmaDistance and distFromMean>=distFromMean[1] and macd<=0 and inDateRange)
longnow=false
if(longCondition and strategy.position_size == 0)
    strategy.entry("My Long Entry Id", strategy.long)
    longnow:=true
if(longCondition and strategy.position_size > 0)
    longnow:=true
    
if(longCondition and strategy.position_size > 0 and month>valuewhen(longnow, month ,1) or longCondition and strategy.position_size > 0 and year>valuewhen(longnow, year ,1) and inDateRange)
    strategy.entry("My Long Entry Id", strategy.long)
plotchar(minute, "Minuten", "", location = location.top)
plotchar(hour, "Stunden", "", location = location.top)    
plotchar(dayofmonth, "Tage", "", location = location.top)
plotchar(month, "Monat", "", location = location.top)
plotchar(year, "Jahr", "", location = location.top)
plotchar(strategy.position_size, "Positionen", "", location = location.top)
plotchar(longCondition, "Long Condition", "", location = location.top)
if(time > timestamp(syminfo.timezone, endYear, endMonth, endDate1, 0, 0))
    strategy.close_all()
//#########################################################################################################################
plotArrow = if (distFromMean<=maxEmaDistance and distFromMean>=distFromMean[1] and macd<=0)
    1
else
    0
    
plotarrow(series=plotArrow)
Expand (87 lines)

Overview

Buy and Hold entry finder Strategy calculates a user-length EMA and a fixed 12/26 MACD, then opens a long position when price is within the maximum EMA distance, that distance is not shrinking, MACD is at or below zero, and the bar falls inside the chosen date range. It can add another long entry when a new month or year begins while the condition remains true. There is no stop loss or take profit; all positions close after the end date. Main settings change EMA length, the distance limit, and the backtest window.

Details

Published by

hasnocool on GitHub

Code credit

runescapeyttanic, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Moving averages, Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy can add several entries in the same direction
  • 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
Maximum EMA Distancefloat50000maxEmaDistance
EMA Lengthint200emalength
Start Dateint1startDate
Start Monthint1startMonth
Start Yearint2020startYear
End Dateint12endDate
End Monthint02endMonth
End Yearint2021endYear
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

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

This strategy combines Arnaud Legoux Moving Average percentage price change calculations with an adaptive Gaussian Moving Average to generate trend-following signals. Long and short entries are triggered when the ALMA change calculation crosses above or below

Strategy v5 Moving averagesOscillators & momentum
Updated 6 weeks ago · None stated GitHub repository of ESM2024 - ALMA Smoothed Gaussian Moving Average

MACD Strategy

by TradersPost
218 stars

This overlay strategy is built around MACD inputs for a fast average, a slow average, and a signal line. Comments say the script keeps MACD values available and can draw a histogram and zero line in a separate pane, with an optional overlay on price. Settings

Strategy v6 Oscillators & momentumMoving averages
Updated 3 months ago · None stated GitHub repository of MACD Strategy
104 stars

This overlay strategy combines a machine-learning distance model with a MACD oscillator to open trades on the price chart. It compares recent history using cosine similarity or Euclidean and Lorentzian distance, then filters direction with selectable moving-av

Strategy v5 AI / machine learningOscillators & momentum
Updated 3 years ago · MIT GitHub repository of MLCEL and MACD Strategy

SMA RSI Strategy

by PythonForForex
64 stars

This strategy evaluates simple moving averages and the Relative Strength Index to generate trading signals. It calculates a 10-period SMA, a 30-period SMA, and a 14-period RSI. Long positions are entered when the fast SMA crosses above the slow SMA while RSI i

Strategy v5 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of SMA RSI Strategy

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