Strategy · Pine Script v5 · On the chart

KC15

by benso87

KC15 is a price-chart strategy that configures two moving averages, a Bollinger Band Width Percentile filter, and PMARP entry and exit thresholds for long and short trades.

View on GitHub

GitHub stars24on the repository
Pine versionv5runs as is
Lines of code226
Settings26inputs
LicenceMPL-2.0
Last update4 years ago5 Oct 2022

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

226 lines of Pine Script v5 from KC15.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

KC15.pine
//@version=5
//
// Author: benso87
// benso87.com
//
//
strategy("KC15", overlay=true, initial_capital=100, default_qty_value=100, default_qty_type=strategy.percent_of_equity, process_orders_on_close=true, backtest_fill_limits_assumption=1, pyramiding=0, max_bars_back=500)

noColor = color.new(color.white, transp=100)
////////////////////////////////////////////////////////////
// Inputs

// Dates
startDate    = input.time    (timestamp('1 Jan 2022 00:00'), title='Start', group='Dates')
endDate        = input.time    (timestamp('1 Jan 2100 00:00'), title='End', group='Dates')

// Moving Average Inputs
ma1Color     = input.color    (#0bff00, title="MA 1", inline="MA 1", group="Moving Averages")
ma1Length     = input.int        (21, title="", minval=1, inline="MA 1", group="Moving Averages")
ma1Type     = input.string    ("EMA", title="", options=["SMA","EMA"], inline="MA 1", group="Moving Averages")
ma2Color     = input.color    (#ff0014, title="MA 2", inline="MA 2", group="Moving Averages")
ma2Length     = input.int        (55, title="", minval=1, inline="MA 2", group="Moving Averages")
ma2Type     = input.string    ("EMA", title="", options=["SMA","EMA"], inline="MA 2", group="Moving Averages")

//Signal inputs
bbwp_threshold         = input.float    (35, 'BBWP threshold', minval=0.1, maxval=100, step=0.01, inline='1', group='Signal Settings')
includeLongs        = input.bool    (true, 'Longs', inline='2', group='Signal Settings')
pmarp_long_signal     = input.float    (15, 'PMARP long signal', minval=0.1, maxval=100, step=0.01, inline='3', group='Signal Settings')
pmarp_long_exit        = input.float    (75, 'PMARP long exit', minval=0.1, maxval=100, step=0.01, inline='3', group='Signal Settings')
includeShorts        = input.bool    (true, 'Shorts', inline='4', group='Signal Settings')
pmarp_short_signal    = input.float    (85, 'PMARP short signal', minval=0.1, maxval=100, step=0.01, inline='5', group='Signal Settings')
pmarp_short_exit    = input.float    (25, 'PMARP short exit', minval=0.1, maxval=100, step=0.01, inline='5', group='Signal Settings')
errorMarginInput    = input.float   (100, 'Margin for Error (%)', minval=0, inline='6', group='Signal Settings')

// Pivots
pvtLenL = input.int(20, minval=1, title='Pivot Length Left', group='Pivots')
pvtLenR = input.int(20, minval=1, title='Pivot Length Right', group='Pivots')

//BBWP input variable declarations
var string STM = 'Spectrum'
var string SLD = 'Solid'
var string BGR = 'Blue Green Red'
var string BR  = 'Blue Red'

// BBWP Inputs
i_priceSrc      = input.source  (close, 'Price Source', group='BBWP Properties')
i_basisType     = input.string  ('SMA', 'Basis Type', options=['SMA', 'EMA', 'VWMA'], group='BBWP Properties')
i_bbwpLen       = input.int     (13, 'BBWP Length', minval=1, group='BBWP Properties')
i_bbwpLkbk      = input.int     (252, 'BBWP Lookback', minval=1, group='BBWP Properties')

// PMARP Inputs
i_src_price         = input.source  (close, 'Price source', inline='1', group='PMARP Properties')
i_pmarp_lookback    = input.int        (350, 'PMARP Lookback', minval=1, maxval=1900, inline='2', group='PMARP Properties')
i_ma_len            = input.int     (20, 'PMAR length', minval=1, inline='3', group='PMARP Properties')
i_ma_typ            = input.string  ('VWMA', 'MA type...', options=[ 'SMA', 'EMA', 'HMA', 'RMA', 'VWMA' ], inline='3', group='PMARP Properties')

////////////////////////////////////////////////////////////
// Calculations

//////////////////////////////////////////////////////////////
// Credit to The_Caretaker
// BBWP

// BBWP function declarations
f_maType ( _price, _len, _type ) =>
    _type == 'SMA' ? ta.sma ( _price, _len ) : _type == 'EMA' ? ta.ema ( _price, _len ) : ta.vwma ( _price, _len )

f_bbwp ( _price, _bbwLen, _bbwpLen, _type ) =>
    float _basis = f_maType ( _price, _bbwLen, _type )
    float _dev = ta.stdev ( _price, _bbwLen )
    _bbw = ( _basis + _dev - ( _basis - _dev )) / _basis
    _bbwSum = 0.0
    _len = bar_index < _bbwpLen ? bar_index : _bbwpLen
    for _i = 1 to _len by 1
        _bbwSum += ( _bbw[_i] > _bbw ? 0 : 1 )
        _bbwSum
    _return = bar_index >= _bbwLen ? ( _bbwSum / _len) * 100 : na
    _return

// BBWP calculations
bbwp    = f_bbwp ( i_priceSrc, i_bbwpLen, i_bbwpLkbk, i_basisType )


//////////////////////////////////////////////////////////////
// Credit to The_Caretaker
// PMARP

// PMARP function declarations
f_prior_sum ( _P, _X ) =>
    math.sum ( _P[1], _X - 1 )

f_ma_val ( _P, _typ, _len ) =>
    _typ == 'SMA' ? ta.sma ( _P, _len ) : _typ == 'EMA' ? ta.ema ( _P, _len ) : _typ == 'RMA' ? ta.rma ( _P, _len ) : _typ == 'HMA' ? ta.hma ( _P, _len ) : ta.vwma ( _P, _len )

f_pmarp ( _price, _pmarLen, _pmarpLen, _type_p ) =>
    _pmar = math.abs ( _price / f_ma_val ( _price, _type_p, _pmarLen ))
    _pmarpSum = 0
    _len = bar_index < _pmarpLen ? bar_index : _pmarpLen
    for i = 1 to _len by 1
        _pmarpSum += ( _pmar[i] > _pmar ? 0 : 1 )
        _pmarpSum
    _return = bar_index >= _pmarLen ? _pmarpSum / _len * 100 : na

// PMARP calculations
ma          = f_ma_val ( i_src_price, i_ma_typ, i_ma_len )
pmar        = i_src_price / ma
pmarp       = f_pmarp ( i_src_price, i_ma_len, i_pmarp_lookback, i_ma_typ )


//////////////////////////////////////////////////////////////
// Calculations

// Error margin
errorMargin = errorMarginInput / 100

f_near(ma, margin) =>
    x = close > ma - (ma * margin) and close < ma + (ma * margin)

// Swings
pvthi_ = ta.pivothigh(high, pvtLenL, pvtLenR)
pvtlo_ = ta.pivotlow(low, pvtLenL, pvtLenR)

// Force Pivot completion before plotting.
Shunt = 0  //Wait for close before printing pivot? 1 for true 0 for false
maxLvlLen = 0  //Maximum Extension Length
pvthi = pvthi_[Shunt]
pvtlo = pvtlo_[Shunt]

// Count How many candles for current Pivot Level, If new reset.
counthi = ta.barssince(not na(pvthi))
countlo = ta.barssince(not na(pvtlo))

pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = ta.change(pvthis) != 0 ? na : #ff0014
lopc = ta.change(pvtlos) != 0 ? na : #0bff00

// Moving averages
ma1 = ta.ema(close, ma1Length)
ma2 = ta.ema(close, ma2Length)

ma1Above2 = ma1 > ma2
ma1Below2 = ma1 < ma2
ma12mid = math.avg(ma1, ma2)

silverCross = ta.crossover(ma1, ma2)
darkCross = ta.crossover(ma2, ma1)

ma1ishHigh = ma1 + ((ma1 - ma2) * errorMargin)
ma1ishLow = ma1 - ((ma2 - ma1) * errorMargin)
ma2ishHigh = ma2 + ((ma1 - ma2) * errorMargin)
ma2ishLow = ma2 - ((ma2 - ma1) * errorMargin)

ma2TestLong = close <= open and low <= ma2ishHigh or close <= ma2ishHigh
ma2TestShort = close >= open and high >= ma2ishLow or close >= ma2ishLow

maLongSignal     = ma1Above2 and ma2TestLong
maShortSignal     = ma1Below2 and ma2TestShort

// BBWP
bbwpLow    = bbwp < bbwp_threshold

// PMARP
pmarpLongSignal        = pmarp <= pmarp_long_signal
pmarpShortSignal    = pmarp >= pmarp_short_signal


// Entries and exits
longEntry   = includeLongs and maLongSignal and bbwpLow and time >= startDate and time <= endDate
longExit    = pmarp >= pmarp_long_exit
shortEntry  = includeShorts and maShortSignal and bbwpLow and time >= startDate and time <= endDate
shortExit   = pmarp <= pmarp_short_exit

// Trade variables
var float entryPrice = na
var float slLong = na
var float slShort = na
var float tpLong = na
var float tpShort = na
var string tradeType = na

if longEntry and strategy.position_size == 0
    entryPrice := close
    slLong := pvtlos
    tpLong := pvthis
    tradeType := 'long'

if shortEntry and strategy.position_size == 0
    entryPrice := close
    slShort := pvthis
    tpShort := pvtlos
    tradeType := 'short'

stopLoss = tradeType == 'long' ? slLong : tradeType == 'short' ? slShort : na
takeProfit = tradeType == 'long' ? tpLong : tradeType == 'short' ? tpShort : na


//////////////////////////////////////////////////////////////
// Strategy

// Entries
strategy.entry('Long', strategy.long, when=longEntry)
strategy.entry('Short', strategy.short, when=shortEntry)

// Exits
strategy.exit('Long Exit', from_entry='Long', stop=slLong)
strategy.exit('Short Exit', from_entry='Short', stop=slShort)

strategy.close('Long', when=longExit)
strategy.close('Short', when=shortExit)


////////////////////////////////////////////////////////////
// Plots

// Trades
sl = plot(strategy.position_size != 0 or strategy.position_size[1] != 0 ? stopLoss : na, title='Stop Loss', color=color.red, style=plot.style_linebr)
e = plot(strategy.position_size != 0 or strategy.position_size[1] != 0 ? entryPrice : na, title='Entry', color=color.yellow, style=plot.style_linebr)
//tp = plot(strategy.position_size != 0 or strategy.position_size[1] != 0 ? takeProfit : na, title='Take Profit', color=color.green, style=plot.style_linebr)

// fill(sl, e, color=color.new(color.red, transp=80))
// fill(tp, e, color=color.new(color.green, transp=80))


//////////////////////////////////////////////////////////////
// Testing

Overview

Users can switch each average between SMA and EMA, set lengths and colors, and toggle longs or shorts. A start and end date limits when orders may run, and pivot lengths plus a percentage error margin adjust related checks. Position size defaults to full equity with no pyramiding. The supplied excerpt ends before order and stop logic, so those rules are not shown.

Details

Published by

benso87 on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Moving averages, Volatility & bands, Oscillators & momentum, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
Starttimetimestamp('1 Jan 2022 00:00')startDate
Endtimetimestamp('1 Jan 2100 00:00')endDate
MA 1color#0bff00ma1Color
ma1Lengthint21ma1Length
ma1TypestringEMAma1Type
MA 2color#ff0014ma2Color
ma2Lengthint55ma2Length
ma2TypestringEMAma2Type
BBWP thresholdfloat35bbwp_threshold
LongsbooltrueincludeLongs
PMARP long signalfloat15pmarp_long_signal
PMARP long exitfloat75pmarp_long_exit
Show all 26 settings
SettingTypeDefaultVariable
ShortsbooltrueincludeShorts
PMARP short signalfloat85pmarp_short_signal
PMARP short exitfloat25pmarp_short_exit
Margin for Error (%)float100errorMarginInput
Pivot Length Leftint20pvtLenL
Pivot Length Rightint20pvtLenR
Price Sourcesourceclosei_priceSrc
Basis TypestringSMAi_basisType
BBWP Lengthint13i_bbwpLen
BBWP Lookbackint252i_bbwpLkbk
Price sourcesourceclosei_src_price
PMARP Lookbackint350i_pmarp_lookback
PMAR lengthint20i_ma_len
MA type...stringVWMAi_ma_typ
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

This strategy plots MACD line, signal line, Bollinger Bands, and RSI on the price chart. It signals long entries on MACD crossover above signal with RSI below 50 and price below BB basis, plus optional DMI filter. Short entries are available but disabled by de

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of MACD + BB + RSI [Alorse]
257 stars

This overlay strategy is built for ES futures on the one-minute chart. It plots a volume-weighted moving average of price opens and standard-deviation bands at Fibonacci ratios, plus a second longer volume-weighted average from a higher-timeframe open. Call an

Strategy v5 Volatility & bandsMoving averages
Updated 6 weeks ago · None stated GitHub repository of ES Futures 1M No Repaint
106 stars

This strategy identifies trend-following entries using a triple exponential moving average alignment and ATR-based exits on the main price chart. Long signals trigger when the fast EMA is above the medium EMA and medium is above slow, accompanied by a close ab

Strategy v4 Moving averagesVolatility & bands Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3 EMA + Stochastic RSI + ATR

Take Profits & Stop Losses

by PythonForForex
64 stars

This educational strategy demonstrates moving average crossovers filtered by RSI momentum, incorporating ATR-based risk management. It calculates a 10-period SMA, a 30-period SMA, a 14-period RSI, and a 14-period ATR. Long entries trigger when the fast SMA cro

Strategy v5 Moving averagesOscillators & momentum
Updated 4 years ago · None stated GitHub repository of Take Profits & Stop Losses

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL

Bollinger bands examples

by tradingstrategy-ai
53 stars

This strategy plots exponential-moving-average Bollinger bands on the price chart and calculates RSI. It opens a long position when close crosses above the upper band while RSI is at or above a threshold, the date filter allows trading, and no position is open

Strategy v5 Volatility & bandsOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Bollinger bands examples

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