Strategy · Pine Script v5 · On the chart

[CBB] 2023 Strategy Default

by codybbrown· code credited to bright_infinite

This strategy starter boilerplate serves as a backtesting template that overlays directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code111
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago11 Apr 2023

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

111 lines of Pine Script v5 from cbb-boilerplate-utils-backtest-StrategyStarterBasic-2023.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

cbb-boilerplate-utils-backtest-StrategyStarterBasic-2023.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bright_infinite
//@version=5
// ============================================================================= 
strategy('[CBB] 2023 Strategy Default  ', shorttitle='2023 strat default', pyramiding=0, calc_on_every_tick=false, initial_capital=1000000, default_qty_type=strategy.percent_of_equity, default_qty_value=20, max_bars_back=5000, overlay=true)

// ============================================================================= 
//              USER INPUT
// ============================================================================= 
in_lookback = input(defval=5, title='Lookback')
in_stratLong = input.int(defval=0, title='Long Strategy Selector', options=[0, 1, 2, 3, 4, 5])
in_stratShort = input.int(defval=0, title='Short Strategy Selector', options=[0, 1, 2, 3, 4, 5])

// ============================================================================= 
//              CONSTANTS
// =============================================================================   
FLAT = strategy.position_size == 0
LONG = strategy.position_size > 0
SHORT = strategy.position_size < 0
confirmed = barstate.isconfirmed
entry = strategy.position_avg_price

var lookForExit = false
var tradeStopPrice = 0.0
var tradeTargetPrice = 0.0
var trailingStop = 0.0
var tradeExitTriggerPrice = 0.0
var tradeStopDistance = 0.0
var tradeTriggerPrice = 0.0


// math.sum(source, length) → series float
h0 = (high + high[1] + high[2] + high[3] + high) / 2
l0 = (low + low[1]) / 2


// ============================================================================= 
//              FUNCTIONS
// =============================================================================   
isInSession(sess) =>
    na(time(timeframe.period, sess + ':1234567', 'GMT-6')) == false

oc(thresh) =>
    result = open > thresh and close > thresh ? 1 : open < thresh and close < thresh ? -1 : na
    result

newLine(x1, y1, x2, y2, col, style) =>
    _style = style == 'dotted' ? line.style_dotted : style == 'solid' ? line.style_solid : na
    line1 = line.new(x1, y1, x2, y2, extend=extend.none, color=col, width=1, style=_style)
    line.delete(line1[1])

// ============================================================================= 
//              VARIABLES
// =============================================================================   
ema_hl = ta.ema((high[in_lookback] - low[in_lookback] + high - low) / 2 + hl2, 20)

// ============================================================================= 
//              CONDITIONS
// ============================================================================= 

// ------------ TREND & BIAS FILTERS  
eqLo = close == close[1] or low == low[1] ? 1 : 0
eqHi = close == close[1] or high == high[1] ? 1 : 0

// ------------ DIVERGENCE


// ============================================================================= 
//              RISK MANAGEMENT
// ============================================================================= 

// ============================================================================= 
//              ORDER MANAGEMENT
// ============================================================================= 
// L0                          = up1
// S0                          = dn1
// L1                          = bull
// S1                          = bear
// L2                          = eqLo
// S2                          = eqHi
// L3                          = hiDiv
// S3                          = regDiv
// L4                          = bullDiv
// S4                          = bearDiv
// L5                          = dirBullDiv
// S5                          = dirBearDiv


// selStratL                   = in_stratLong  == 0 ? L0 :  in_stratLong  == 1 ? L1 : in_stratLong  == 2 ? L2 : in_stratLong  == 3 ? L3 : in_stratLong  == 4 ? L4 : in_stratLong  == 5 ? L5 : na
// selStratS                   = in_stratShort == 0 ? S0 :  in_stratShort == 1 ? S1 : in_stratShort == 2 ? S2 : in_stratShort == 3 ? S3 : in_stratShort == 4 ? S4 : in_stratShort == 5 ? S5 : na

// ============================================================================= 
//          STRATEGY EXECUTION 
// ============================================================================= 
// if(selStratL)
//     strategy.entry("Buy", strategy.long) // enter long by market if current open great then previous high
// strategy.close("Buy", selStratL) 

// if(selStratS)
//     strategy.entry("Sell", strategy.short) // enter long by market if current open great then previous high
// strategy.close("Sell", selStratS) //


// ============================================================================= 
//              PLOTS & COLORING
// ============================================================================= 
plot(ema_hl, 'EMA')

// ============================================================================= 
//          RESOURCES 
// =============================================================================

Overview

[CBB] 2023 Strategy Default sets default position sizing to twenty percent of equity and calculates an exponential moving average alongside high and low price thresholds. Inputs allow configuring a lookback period and selecting numbered long or short strategy modes. It includes helper functions for session detection and dynamic line drawing. Notably, the script is an unfinished boilerplate containing no active order entries, stop loss rules, or trade exit mechanisms.

Details

Published by

codybbrown on GitHub

Code credit

bright_infinite, 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

Topics

momentum-trading-strategypinescripttrading-strategiestradingviewvolatility-tradingtrading-indcators

Settings

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

SettingTypeDefaultVariable
Lookbackint5in_lookback
Long Strategy Selectorint0in_stratLong
Short Strategy Selectorint0in_stratShort
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

Donald - Test Logic Futures is an overlay strategy designed for intraday futures trading. It combines kernel regression functions with exponential moving averages to evaluate price retracements and entry timing. Trades are filtered by session execution windows

Strategy v5 TrendMoving averages Repaints
Updated 7 weeks ago · None stated GitHub repository of Donald - Test Logic Futures

Andean Scalping

by hasnocool
106 stars

Andean Scalping is a Pine Script v5 strategy template designed for backtesting trading signals. It features session and date-range filtering, directional trade toggles for long and short entries, and optional trend filtering tools including higher-timeframe EM

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of Andean Scalping

This strategy plots a 90-period simple moving average from the daily and four-hour charts plus a daily session VWAP on the price chart. It enters long when VWAP is above the daily average and the four-hour VWAP crosses above its average, and short in the oppos

Strategy v5 TrendMoving averages Repaints
Updated 3 years ago · MIT GitHub repository of 1D Bullish Buy 4H Bearish Sell Session
4 stars

This strategy trades directional momentum breakouts for crude oil on the price chart. It enters long or short positions when a fast exponential moving average crosses a slow exponential moving average, provided that current Average True Range exceeds its movin

Strategy v6 TrendMoving averages
Updated 2 months ago · MIT GitHub repository of CL Crude Oil Momentum [trugurpala]

More Trend 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