You are on page 1of 3

// This script uses a combination of technical indicators to generate long and

short signals for trading the stock market


// on a 1-minute time frame.
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/

//@version=5
//Author=Cralin
strategy("MacD -Bollinger Bands %B -RSI-Stochastic With Combined volatility and
ticks V1-1", shorttitle="MBRS & VT V1-1", overlay=true )

// ADX settings
adxLength = input.int(title="ADX Length", defval=15, minval=1, maxval=200,group =
"ADX Settings")
adx_threshold = input.int(title="ADX Threshold", defval=20, minval=1,
maxval=50,group = "ADX Settings")

// Calculate ADX
adxUp = ta.change(high)
adxDown = ta.change(low)
adxClose = ta.change(close)
adxPlusDM = na(adxUp) ? na : (adxUp > adxDown and adxUp > 0 ? adxUp : 0)
adxMinusDM = na(adxDown) ? na : (adxDown > adxUp and adxDown > 0 ? adxDown : 0)
tr = math.max(high - low, math.abs(high - close[1]), math.abs(low - close[1]))
plusDI = fixnan(100 * ta.rma(adxPlusDM , adxLength) / ta.rma(tr, adxLength))
minusDI = fixnan(100 * ta.rma(adxMinusDM, adxLength) / ta.rma(tr, adxLength))
adx = 100 * ta.rma(math.abs(plusDI - minusDI) / (plusDI + minusDI), adxLength)

// Bollinger Band %B settings


bbLength = input.int(title="Bolinger Band Length ", defval=47,minval=1,group =
"Bollinger Band %B settings")
bbSrc = input(close, title="Source",group = "Bollinger Band %B settings")
bbMult = input.float(2.0, minval=0.001, maxval=50, title="StdDev",group =
"Bollinger Band %B settings")
bbMaxLevel = input.float(title="Bollinger Band %B Maximum Level", defval=0.5,
step=0.1,group = "Bollinger Band %B settings")
bbMinLevel = input.float(title="Bollinger Band %B Minimum Level", defval=0.5,
step=0.1,group = "Bollinger Band %B settings")

// Calculate Bollinger Bands %B


bbBasis = ta.sma(bbSrc, bbLength)
bbDev = bbMult * ta.stdev(bbSrc, bbLength)
bbUpper = bbBasis + bbDev
bbLower = bbBasis - bbDev
bbr = (bbSrc - bbLower)/(bbUpper - bbLower)

// RSI settings
rsi_length = input.int(title="RSI Length ", defval=33,group = "RSI Settings")
rsi_overbought =input.int(title="RSI Over Bought", defval=33,group = "RSI
Settings")
rsi_oversold = input.int(title="RSI Over Sold", defval=21,group = "RSI Settings")

// Get RSI value


rsi = ta.rsi(close, rsi_length)

// MACD settings
fast_length = input(title="MACD Fast Length", defval=8,group = "MACD Settings")
slow_length = input(title="MACD Slow Length", defval=26,group = "MACD Settings")
src = input(title="Source", defval=ohlc4,group = "MACD Settings")
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50,
defval = 9,group = "MACD Settings")
sma_source = input.string(title="Oscillator MA Type", defval="EMA",
options=["SMA", "EMA"],group = "MACD Settings")
sma_signal = input.string(title="Signal Line MA Type", defval="SMA",
options=["SMA", "EMA"],group = "MACD Settings")

// Calculate MACD
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd_line = fast_ma - slow_ma
signal_ma = sma_signal == "SMA" ? ta.sma(macd_line, signal_length) :
ta.ema(macd_line, signal_length)
macd_histogram = macd_line - signal_ma

// Stochastic settings
k_length = input(title="Stochastic K Length", defval=3,group = "Stochastic
Settings")
d_length = input(title="Stochastic D Length", defval=3,group = "Stochastic
Settings")

// Calculate Stochastic
stochastic_k = ta.stoch(close, high, low, k_length)
stochastic_d = ta.stoch(close, high, low, d_length)

long_stochastic_k_threshold = input.int(title="Long Stochastic K Threshold",


defval=29, minval=1, maxval=100)
short_stochastic_k_threshold = input.int(title="Short Stochastic K Threshold",
defval=10, minval=1, maxval=100)

// Generate long and short signals


long_signal = adx > adx_threshold and bbr < bbMinLevel and rsi < rsi_oversold and
macd_line > signal_ma and stochastic_k < long_stochastic_k_threshold
short_signal = adx > adx_threshold and bbr > bbMaxLevel and rsi > rsi_overbought
and macd_line < signal_ma and stochastic_k > short_stochastic_k_threshold

// Set the strategy's position as long or short


if long_signal
strategy.entry("Long", strategy.long)

if short_signal
strategy.entry("Short", strategy.short )

// Profit and stop loss settings


long_profit_target = input.float(title="Long Profit Target (in ticks)", defval=5,
step=1)
long_stop_loss = input.float(title="Long Stop Loss (in ticks)", defval=5, step=3)
short_profit_target = input.float(title="Short Profit Target (in ticks)", defval=6,
step=1)
short_stop_loss = input.float(title="Short Stop Loss (in ticks)", defval=5, step=1)

// Set profit and stop loss conditions


strategy.exit("Take Profit", "Long", limit=strategy.position_avg_price +
long_profit_target)
strategy.exit("Stop Loss", "Long", stop=strategy.position_avg_price -
long_stop_loss)
strategy.exit("Take Profit", "Short", limit=strategy.position_avg_price -
short_profit_target)
strategy.exit("Stop Loss", "Short", stop=strategy.position_avg_price +
short_stop_loss)

You might also like