341 lines
10 KiB
Plaintext
341 lines
10 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center XTrade TM15M Strategy
|
|
// ---------------------------------------------
|
|
// in this strategy i must use:
|
|
// - ;
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
//@version=5
|
|
indicator(title="SaherElm XTrade TM15M Strategy", shorttitle = "XST_TM15M", overlay = true)
|
|
|
|
//
|
|
// START Inputs ....
|
|
//
|
|
//
|
|
showTrendMagic = input.bool(defval = false, title = "Show Trend Magic")
|
|
|
|
//
|
|
// 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")
|
|
|
|
//
|
|
// 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 ...
|
|
swingLoopback = input.int(defval = 7, title = "Swing Loopback Period", group = "Stop Loss")
|
|
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
|
|
/////////////////////////////
|
|
//
|
|
// END Functions ....
|
|
//
|
|
|
|
//
|
|
// START Calculations ....
|
|
//
|
|
//
|
|
var shortColor = color.red
|
|
var longColor = color.lime
|
|
|
|
//////////////////////////////
|
|
// 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 Last Market Price
|
|
////////////////////////////
|
|
//
|
|
lastMarketLowest = ta.lowest(low, swingLoopback)
|
|
lastMarketLowest2 = ta.lowest(low, swingLoopback * 2)
|
|
lastMarketLowest3 = ta.lowest(low, swingLoopback * 3)
|
|
|
|
//
|
|
lastMarketHighest = ta.highest(high, swingLoopback)
|
|
lastMarketHighest2 = ta.highest(high, swingLoopback * 2)
|
|
lastMarketHighest3 = ta.highest(high, swingLoopback * 3)
|
|
////////////////////////////
|
|
// END Last Market Price
|
|
////////////////////////////
|
|
|
|
////////////////////////////
|
|
// START Trend Magic ...
|
|
////////////////////////////
|
|
TMAP = 5
|
|
TMCoeff = 1
|
|
TMPeriod = 20
|
|
TMSrc = close
|
|
|
|
//
|
|
TMATR = ta.sma(ta.tr,TMAP)
|
|
|
|
//
|
|
TMUpT = low - TMATR * TMCoeff
|
|
TMDownT = high + TMATR * TMCoeff
|
|
|
|
//
|
|
TMMagicTrend = 0.0
|
|
TMMagicTrend := ta.cci(TMSrc, TMPeriod) >= 0 ? (TMUpT < nz(TMMagicTrend[1]) ? nz(TMMagicTrend[1]) : TMUpT) : (TMDownT > nz(TMMagicTrend[1]) ? nz(TMMagicTrend[1]) : TMDownT)
|
|
|
|
//
|
|
// Check Candle State based on TMMagic Trend ...
|
|
TMCandleState = ta.cci(TMSrc, TMPeriod)
|
|
|
|
//
|
|
TMTrendUp = TMCandleState > 0
|
|
TMTrendDown = TMCandleState < 0
|
|
TMTrendNeutral = TMCandleState == 0
|
|
|
|
//
|
|
TMTrendDiff = TMMagicTrend - TMMagicTrend[1]
|
|
// plot(series = TMTrendDiff, color = color.yellow)
|
|
|
|
// //
|
|
// TMTrendNeutral = TMTrendDiff == 0
|
|
// TMTrendIncreased = TMTrendDiff > 0
|
|
// TMTrendDecreased = TMTrendDiff < 0
|
|
|
|
//
|
|
TMTrendChanged = TMTrendUp and not TMTrendUp[1] or not TMTrendUp and TMTrendUp[1]
|
|
|
|
//
|
|
TMColor = TMTrendUp ? color.blue : TMTrendDown ? color.red : TMTrendNeutral ? color.gray : na
|
|
////////////////////////////
|
|
// END Trend Magic ...
|
|
////////////////////////////
|
|
|
|
///////////////////////
|
|
// START Cycle Channel
|
|
///////////////////////
|
|
//
|
|
CCHSclT = 10
|
|
CCHMclT = 30
|
|
CCHScm = 1.0
|
|
CCHMcm = 3.0
|
|
CCHSrc = close
|
|
CCHEbc = false
|
|
|
|
//
|
|
CCHScl = CCHSclT / 2
|
|
CCHMcl = CCHMclT / 2
|
|
|
|
//
|
|
CCHMaScl = ta.rma(CCHSrc, CCHScl)
|
|
CCHMaMcl = ta.rma(CCHSrc, CCHMcl)
|
|
|
|
//
|
|
CCHScmOff = CCHScm * ta.atr(CCHScl)
|
|
CCHMcmOff = CCHMcm * ta.atr(CCHMcl)
|
|
|
|
//
|
|
CCHScl2 = CCHScl / 2
|
|
CCHMcl2 = CCHMcl / 2
|
|
|
|
//
|
|
CCHSct = nz(CCHMaScl[CCHScl2], CCHSrc) + CCHScmOff
|
|
CCHScb = nz(CCHMaScl[CCHScl2], CCHSrc) - CCHScmOff
|
|
|
|
//
|
|
CCHMct = nz(CCHMaMcl[CCHMcl2], CCHSrc) + CCHMcmOff
|
|
CCHMcb = nz(CCHMaMcl[CCHMcl2], CCHSrc) - CCHMcmOff
|
|
|
|
//
|
|
CCHScmm = math.avg(CCHSct, CCHScb)
|
|
|
|
//
|
|
CCHOmed = (CCHScmm - CCHMcb) / (CCHMct - CCHMcb)
|
|
CCHOshort = (CCHSrc - CCHMcb) / (CCHMct - CCHMcb)
|
|
|
|
//
|
|
CCHSlowOSC = CCHOmed
|
|
CCHFastOSC = CCHOshort
|
|
|
|
//
|
|
CCHMidPoint = 0.5
|
|
isCCHUpTrend = CCHFastOSC > CCHMidPoint
|
|
isCCHDownTrend = CCHFastOSC < CCHMidPoint
|
|
CCHTrendUp = ta.crossover(CCHFastOSC, CCHMidPoint)
|
|
CCHTrendDown = ta.crossunder(CCHFastOSC, CCHMidPoint)
|
|
CCHTrendBullish = ta.crossover(CCHFastOSC, CCHSlowOSC) and isBullishCandle()
|
|
|
|
//
|
|
CCHFastOSCDiff = CCHFastOSC - CCHFastOSC[1]
|
|
CCHSlowOSCDiff = CCHSlowOSC - CCHSlowOSC[1]
|
|
// plot(CCHFastOSCDiff, color = color.fuchsia)
|
|
// plot(CCHSlowOSCDiff, color =color.yellow)
|
|
// isFastOSCDiffPassed = true // CCHFastOSCDiff < 0 and CCHFastOSCDiff < -0.07
|
|
// isSlowOSCDiffPassed = true // CCHSlowOSCDiff > 0 and CCHSlowOSCDiff <= 0.03
|
|
|
|
//
|
|
// lastMarketRange = lastMarketHighest - TMMagicTrend
|
|
// candleRangeInLastMarket = math.abs(lastMarketRange - getCandleRange())
|
|
// isCandleVerifiedInTrendClopud = lastMarketRange > getCandleRange() and candleRangeInLastMarket >= lastMarketRange / 2
|
|
|
|
// isCandleRageVerified =
|
|
// plot(lastMarketRange, color = color.maroon)
|
|
// plot(candleRangeInLastMarket, color = color.rgb(11, 108, 199))
|
|
|
|
CCHTrendBearish = ta.crossunder(CCHFastOSC, CCHSlowOSC) and isBearishCandle() and low < TMMagicTrend and high < TMMagicTrend // and isCandleVerifiedInTrendClopud // and isFastOSCDiffPassed and isSlowOSCDiffPassed
|
|
|
|
//
|
|
CCHBc = (CCHOshort > 0.5) ? (CCHOshort > 1.0 ? color.purple : (CCHOshort > CCHOmed ? color.lime : color.green)) : (CCHOshort < 0 ? color.purple : ( CCHOshort < CCHOmed ? color.red : color.orange))
|
|
///////////////////////
|
|
// END Cycle Channel
|
|
///////////////////////
|
|
|
|
///////////////////////
|
|
// START Strategy ...
|
|
///////////////////////
|
|
//
|
|
canShowStopLoss = showSignals and showStopLosses and (showLongSignals or showShortSignals) and (showLongStopLosses or showShortStopLosses)
|
|
canShowLongStopLoss = canShowStopLoss and showLongStopLosses // and not na(ATRSLLongStopLoss)
|
|
canShowShortStopLoss = canShowStopLoss and showShortStopLosses // and not na(ATRSLShortStopLoss)
|
|
|
|
//
|
|
hasSignal = TMTrendUp and CCHTrendUp or CCHTrendBearish // (TMTrendDown and CCHTrendDown)
|
|
|
|
//
|
|
hasLongSignal = hasSignal and TMTrendUp and CCHTrendUp and TMTrendDiff[1] >= 0// and open < TMMagicTrend //and (close < TMMagicTrend) and isBullishCandle()
|
|
longSignal = hasLongSignal ? bar_index : na
|
|
float longStopLoss = lastMarketLowest
|
|
|
|
//
|
|
hasShortSignal = hasSignal and CCHTrendBearish // and TMTrendDown and open > TMMagicTrend // and close > TMMagicTrend) and isBearishCandle()
|
|
shortSignal = hasShortSignal ? bar_index : na
|
|
float shortStopLoss = lastMarketHighest
|
|
|
|
//
|
|
canShowSignal = showSignals and (showLongSignals or showShortSignals)
|
|
canShowLongSignal = canShowSignal and showLongSignals and hasLongSignal
|
|
canShowShortSignal = canShowSignal and showShortSignals and hasShortSignal
|
|
///////////////////////
|
|
// END Strategy ...
|
|
///////////////////////
|
|
//
|
|
// END Calculations ....
|
|
//
|
|
|
|
//
|
|
// START Plots ....
|
|
//
|
|
//
|
|
// Trend Magic ...
|
|
plot(series = showTrendMagic ? TMMagicTrend : na, color = TMColor, linewidth = 3, title = "TM > Trend Magic")
|
|
|
|
//
|
|
// CCH Trend Signals ...
|
|
// plotshape(CCHTrendUp ? bar_index : na, title = "CCH > Trend Up", location = location.belowbar ,style = shape.arrowup, size = size.normal, color = color.green)
|
|
// plotshape(CCHTrendBullish ? bar_index : na, title = "CCH > Trend Bullish", location = location.belowbar ,style = shape.arrowup, size = size.normal, color = color.aqua)
|
|
// plotshape(CCHTrendDown ? bar_index : na, title = "CCH > Trend Down", location = location.abovebar, style = shape.arrowdown, size = size.normal, color = color.red)
|
|
// plotshape(CCHTrendBearish ? bar_index : na, title = "CCH > Trend Bearish", location = location.abovebar, style = shape.arrowdown, size = size.normal, color = color.purple)
|
|
|
|
//
|
|
// TM Trend Signals ...
|
|
// plotshape(TMTrendUp ? bar_index : na, title = "TM > Trend Up", location = location.belowbar ,style = shape.labelup, size = size.normal, color = color.green)
|
|
// plotshape(TMTrendDown ? bar_index : na, title = "TM > Trend Down", location = location.abovebar, style = shape.labeldown, size = size.normal, color = color.red)
|
|
|
|
|
|
//
|
|
// 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)
|
|
|
|
//
|
|
// Stop Losses ...
|
|
plot(series = canShowLongStopLoss ? longStopLoss : na, color = color.fuchsia, linewidth = 1, style = plot.style_stepline, title = "Long Stop Loss")
|
|
plot(series = canShowShortStopLoss ? shortStopLoss : na, color = color.aqua, linewidth = 1, style = plot.style_stepline, title = "Short Stop Loss")
|
|
//
|
|
// END Plots ....
|
|
//
|
|
|
|
//
|
|
// START Alerts ...
|
|
//
|
|
alertcondition(hasSignal, "XST_SHULL5M > Signal")
|
|
alertcondition(hasLongSignal, "XST_SHULL5M > Long Signal")
|
|
alertcondition(hasShortSignal, "XST_SHULL5M > Short Signal")
|
|
//
|
|
// END Alerts ...
|
|
//
|
|
|
|
//////////////////////////////////////////////////////
|
|
// TODOS:
|
|
// -------
|
|
// - []
|
|
//////////////////////////////////////////////////////
|