288 lines
8.3 KiB
Plaintext
288 lines
8.3 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center XTrade RF15M Strategy
|
|
// ---------------------------------------------
|
|
// in this strategy i must use:
|
|
// - Price Action Candle Recognition;
|
|
// -
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
//@version=5
|
|
indicator(title="SaherElm XTrade RF15M Strategy", shorttitle = "XST_RF15M", overlay = true)
|
|
|
|
//
|
|
// START Inputs ....
|
|
//
|
|
//
|
|
// Determines Show Signals or not ...
|
|
showSignals = input.bool(defval = true, title = "Show Signals", group = "Strategy")
|
|
showStopLosses = input.bool(defval = true, title = "Show Stop Losses", group = "Strategy")
|
|
showRangeFilter = input.bool(defval = false, title = "Show Range Filter", group = "Strategy")
|
|
|
|
//
|
|
// Show Signals based on their types ...
|
|
showLongSignals = input.bool(defval = true, title = "Show Long Signals", group = "Signals")
|
|
showShortSignals = input.bool(defval = true, title = "Show Short Signals", group = "Signals")
|
|
|
|
//
|
|
// Show Stop Losses based on their types ...
|
|
showLongStopLosses = input.bool(defval = true, title = "Show Long Stop Losses", group = "Stop Loss")
|
|
showShortStopLosses = input.bool(defval = true, title = "Show Short Stop Losses", group = "Stop Loss")
|
|
//
|
|
// END Inputs ....
|
|
//
|
|
|
|
//
|
|
// START Functions ....
|
|
//
|
|
//////////////////////////////
|
|
// START Price Action Candles
|
|
//////////////////////////////
|
|
//
|
|
// Retrieve Specific Candle Height (based on low and high) ...
|
|
getCandleRange() =>
|
|
high - low
|
|
|
|
//
|
|
// Retrieve Candle Ranges Thirs Section ...
|
|
getCandleThird() =>
|
|
getCandleRange() / 3
|
|
|
|
//
|
|
// Determines a Candle is Bullish or not ...
|
|
isBullishCandle() =>
|
|
close > low + (getCandleThird() * 2)
|
|
|
|
//
|
|
// Determines a Candle is Bearish or not ...
|
|
isNeutralCandle() =>
|
|
close < low + (getCandleThird() * 2) and close > low + getCandleThird()
|
|
|
|
//
|
|
// Determines a Candle is Neutral or not ...
|
|
isBearishCandle() =>
|
|
close < low + getCandleThird()
|
|
/////////////////////////////
|
|
// END Price Action Candles
|
|
/////////////////////////////
|
|
|
|
///////////////////////////////
|
|
// START Range Filter Buy Sell
|
|
///////////////////////////////
|
|
//
|
|
// Smooth Average Range
|
|
smoothAverageRange(x, t, m) =>
|
|
wper = t * 2 - 1
|
|
avrng = ta.ema(math.abs(x - x[1]), t)
|
|
result = ta.ema(avrng, wper) * m
|
|
result
|
|
|
|
//
|
|
// Range Filter ...
|
|
rangeFilter(x, r) =>
|
|
result = x
|
|
result := x > nz(result[1]) ? x - r < nz(result[1]) ? nz(result[1]) : x - r : x + r > nz(result[1]) ? nz(result[1]) : x + r
|
|
result
|
|
///////////////////////////////
|
|
// END Range Filter Buy Sell
|
|
///////////////////////////////
|
|
//
|
|
// END Functions ....
|
|
//
|
|
|
|
//
|
|
// START Calculations ....
|
|
//
|
|
//
|
|
var shortColor = color.red
|
|
var longColor = color.lime
|
|
var longStopLossColor = color.fuchsia
|
|
var shortStopLossColor = color.aqua
|
|
|
|
//////////////////////////////
|
|
// START Price Action Candles
|
|
//////////////////////////////
|
|
//
|
|
var bullishColor = color.lime
|
|
var bullishShape = shape.arrowup
|
|
var bullishTitle = "Bullish"
|
|
var bullishType = 1
|
|
|
|
//
|
|
var bearishColor = color.red
|
|
var bearishShape = shape.arrowdown
|
|
var bearishTitle = "Bearish"
|
|
var bearishType = -1
|
|
|
|
//
|
|
var neutralColor = color.gray
|
|
var neutralShape = shape.circle
|
|
var neutralTitle = "Neutral"
|
|
var neutralType = 0
|
|
|
|
//
|
|
candleType = isBullishCandle() ? bullishType : isNeutralCandle() ? neutralType : isBearishCandle() ? bearishType : neutralType
|
|
|
|
//
|
|
prevCandleType = not na(candleType[1]) ? candleType[1] : neutralType
|
|
|
|
//
|
|
candleNeighborhoodBaseType = prevCandleType + candleType
|
|
/////////////////////////////
|
|
// END Price Action Candles
|
|
/////////////////////////////
|
|
|
|
///////////////////////////////
|
|
// START Range Filter Buy Sell
|
|
///////////////////////////////
|
|
//
|
|
// Source
|
|
RFBSSrc = close
|
|
|
|
//
|
|
// Sampling Period
|
|
RFBSPer = 41
|
|
|
|
//
|
|
// Range Multiplier ...
|
|
RFBSMult = 3.0
|
|
|
|
//
|
|
// Color variables ...
|
|
RFBSUpColor = color.green
|
|
RFBSMidColor = color.blue
|
|
RFBSDownColor = color.red
|
|
|
|
//
|
|
RFBSSmrng = smoothAverageRange(RFBSSrc, RFBSPer, RFBSMult)
|
|
RFBSFilt = rangeFilter(RFBSSrc, RFBSSmrng)
|
|
|
|
//
|
|
// Filter UP Direction ...
|
|
RFBSUpward = 0.0
|
|
RFBSUpward := RFBSFilt > RFBSFilt[1] ? nz(RFBSUpward[1]) + 1 : RFBSFilt < RFBSFilt[1] ? 0 : nz(RFBSUpward[1])
|
|
|
|
//
|
|
// Filter DOWN Direction ...
|
|
RFBSDownward = 0.0
|
|
RFBSDownward := RFBSFilt < RFBSFilt[1] ? nz(RFBSDownward[1]) + 1 : RFBSFilt > RFBSFilt[1] ? 0 : nz(RFBSDownward[1])
|
|
|
|
//
|
|
// Target Bands
|
|
RFBSHband = RFBSFilt + RFBSSmrng
|
|
RFBSLband = RFBSFilt - RFBSSmrng
|
|
|
|
//
|
|
// Colors
|
|
RFBSFiltcolor = RFBSUpward > 0 ? RFBSUpColor : RFBSDownward > 0 ? RFBSDownColor : RFBSMidColor
|
|
RFBSBarcolor = RFBSSrc > RFBSFilt and RFBSSrc > RFBSSrc[1] and RFBSUpward > 0 ? RFBSUpColor : RFBSSrc > RFBSFilt and RFBSSrc < RFBSSrc[1] and RFBSUpward > 0 ? RFBSUpColor : RFBSSrc < RFBSFilt and RFBSSrc < RFBSSrc[1] and RFBSDownward > 0 ? RFBSDownColor : RFBSSrc < RFBSFilt and RFBSSrc > RFBSSrc[1] and RFBSDownward > 0 ? RFBSDownColor : RFBSMidColor
|
|
|
|
//
|
|
// Break Outs
|
|
RFBSLongCond = bool(na)
|
|
RFBSShortCond = bool(na)
|
|
RFBSLongCond := RFBSSrc > RFBSFilt and RFBSSrc > RFBSSrc[1] and RFBSUpward > 0 or RFBSSrc > RFBSFilt and RFBSSrc < RFBSSrc[1] and RFBSUpward > 0
|
|
RFBSShortCond := RFBSSrc < RFBSFilt and RFBSSrc < RFBSSrc[1] and RFBSDownward > 0 or RFBSSrc < RFBSFilt and RFBSSrc > RFBSSrc[1] and RFBSDownward > 0
|
|
|
|
//
|
|
RFBSCondIni = 0
|
|
RFBSCondIni := RFBSLongCond ? 1 : RFBSShortCond ? -1 : RFBSCondIni[1]
|
|
RFBSLongCondition = RFBSLongCond and RFBSCondIni[1] == -1
|
|
RFBSShortCondition = RFBSShortCond and RFBSCondIni[1] == 1
|
|
///////////////////////////////
|
|
// END Range Filter Buy Sell
|
|
///////////////////////////////
|
|
|
|
///////////////////////////////
|
|
// START Stochastic Histogram
|
|
///////////////////////////////
|
|
//
|
|
STHSLength = 14
|
|
STHSSmoothK = 3
|
|
|
|
//
|
|
STHSK = (ta.sma(ta.stoch(close, high, low, STHSLength), STHSSmoothK) - 50)
|
|
///////////////////////////////
|
|
// END Stochastic Histogram
|
|
///////////////////////////////
|
|
|
|
///////////////////////
|
|
// START Strategy ...
|
|
///////////////////////
|
|
//
|
|
canShowStopLoss = showSignals and showStopLosses and (showLongSignals or showShortSignals) and (showLongStopLosses or showShortStopLosses)
|
|
canShowLongStopLoss = canShowStopLoss and showLongStopLosses and not na(RFBSLongCondition)
|
|
canShowShortStopLoss = canShowStopLoss and showShortStopLosses and not na(RFBSShortCondition)
|
|
|
|
//
|
|
hasSignal = RFBSLongCondition or RFBSShortCondition
|
|
|
|
//
|
|
// Stochastic Verification ...
|
|
isStochasticLongVerified = STHSK > 0
|
|
isStochasticShortVerified = STHSK < 0
|
|
|
|
//
|
|
// and open > RFBSFilt ...
|
|
// and close > RFBSFilt ...
|
|
hasLongSignal = hasSignal and not na(RFBSLongCondition) and open > RFBSFilt and isBullishCandle() and isStochasticLongVerified
|
|
longSignal = hasLongSignal ? RFBSLongCondition : na
|
|
longStopLoss = RFBSLband
|
|
|
|
//
|
|
hasShortSignal = hasSignal and not na(RFBSShortCondition) and isBearishCandle() and isStochasticShortVerified
|
|
shortSignal = hasShortSignal ? RFBSShortCondition : na
|
|
shortStopLoss = RFBSHband
|
|
|
|
//
|
|
canShowSignal = showSignals and (showLongSignals or showShortSignals)
|
|
canShowLongSignal = canShowSignal and showLongSignals and hasLongSignal
|
|
canShowShortSignal = canShowSignal and showShortSignals and hasShortSignal
|
|
///////////////////////
|
|
// END Strategy ...
|
|
///////////////////////
|
|
//
|
|
// END Calculations ....
|
|
//
|
|
|
|
//
|
|
// START Plots ....
|
|
//
|
|
//
|
|
// Signals ...
|
|
plotshape(canShowLongSignal ? longSignal : na, title = "Long Signal", color = longColor, location = location.belowbar, style = shape.triangleup, size = size.normal)
|
|
plotshape(canShowShortSignal ? shortSignal : na, title = "Short Signal", color = shortColor, location = location.abovebar, style = shape.triangledown, size = size.normal)
|
|
|
|
//
|
|
plot(series = showRangeFilter ? RFBSFilt : na, color = RFBSFiltcolor, linewidth = 2, title = "RFBS > Range Filter", style = plot.style_line)
|
|
|
|
//
|
|
// Stop Losses ...
|
|
plot(series = canShowLongStopLoss ? longStopLoss : na, color = longStopLossColor, linewidth = 1, style = plot.style_line, title = "Long Stop Loss")
|
|
plot(series = canShowShortStopLoss ? shortStopLoss : na, color = shortStopLossColor, linewidth = 1, style = plot.style_line, title = "Short Stop Loss")
|
|
//
|
|
// END Plots ....
|
|
//
|
|
|
|
//
|
|
// START Alerts ...
|
|
//
|
|
alertcondition(hasSignal, "XST_RF15M > Signal")
|
|
alertcondition(hasLongSignal, "XST_RF15M > Long Signal")
|
|
alertcondition(hasShortSignal, "XST_RF15M > Short Signal")
|
|
//
|
|
// END Alerts ...
|
|
//
|
|
|
|
//////////////////////////////////////////////////////
|
|
// TODOS:
|
|
// -------
|
|
// - []
|
|
//////////////////////////////////////////////////////
|