// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © WS1612 //@version=5 strategy("Funded Challenge Final", overlay=true, margin_long=100, margin_short=100,initial_capital =25000) //inputs // Get all inputs to calculate indicators zScoreLength = input(20) rsiLength = input(14) // macd macdFastMALengthDAY = input(3) macdSlowMALengthDAY = input(10) macdSignalLengthDAY = input(16) // macd day to day normMacdFastMALength = input(12) normMacdSlowMALength = input(26) normMacdSignalLength = input(9) normMacdSmoothLength = input(5) normMacdNormLength = input(20) cciLength = input(50) tsiShortLength = input(13) tsiLongLength = input(25) rocLength = input(14) cilength = input(28) stochK = input(1) stochD = input(1) stochRsiLength = input(100) stochLength = input(100) demaLengthlow = input(200) demaLengthhigh = input(200) // above = indicator calculations // below = pulling items // zscore basis = ta.sma(close,zScoreLength) zScore= (close-basis)/ta.stdev(close, zScoreLength) // rsi rsi = ta.rsi(close, rsiLength) //macd2 fast_maDAY = ta.ema(close, macdFastMALengthDAY) slow_maDAY = ta.ema(close,macdSlowMALengthDAY) macdDAY = fast_maDAY - slow_maDAY macdSignalDAY = ta.ema(macdDAY,macdSignalLengthDAY) // cci cci = ta.cci(close, cciLength) //tsi tsi = ta.tsi(close, tsiShortLength,tsiLongLength) * 100 //roc roc = ta.roc(close,rocLength) // choppiness Index chop = 100 * math.log10(math.sum(ta.atr(1), cilength) / (ta.highest(cilength) - ta.lowest(cilength))) / math.log10(cilength) // stochastics stochRsi = ta.rsi(close, stochRsiLength) k = ta.sma(ta.stoch(stochRsi, stochRsi, stochRsi, stochRsiLength), stochK) d = ta.sma(k, stochD) // demalow demaEma1low = ta.ema(low, demaLengthlow) demaEma2low = ta.ema(demaEma1low, demaLengthlow) dema = 2 * (demaEma1low - demaEma2low) // demahigh demaEma1high = ta.ema(high, demaLengthhigh) demaEma2high = ta.ema(demaEma1high, demaLengthhigh) demahigh = 2 * (demaEma1high - demaEma2high) // Inputs for Twin Range TRsource = input(defval=close, title='TRSource') per1 = input.int(defval=12, minval=1, title='Fast period') mult1 = input.float(defval=1, minval=0.1, title='Fast range') per2 = input.int(defval=55, minval=1, title='Slow period') mult2 = input.float(defval=2, minval=0.1, title='Slow range') smoothrng(x, t, m) => wper = t * 2 - 1 avrng = ta.ema(math.abs(x - x[1]), t) smoothrng = ta.ema(avrng, wper) * m smoothrng smrng1 = smoothrng(TRsource, per1, mult1) smrng2 = smoothrng(TRsource, per2, mult2) smrng = (smrng1 + smrng2) / 2 // Range Filter rngfilt(x, r) => rngfilt = x rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r rngfilt filt = rngfilt(TRsource, smrng) upward = 0.0 upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1]) downward = 0.0 downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1]) hband = filt + smrng lband = filt - smrng longCond = bool(na) shortCond = bool(na) longCond := TRsource > filt and TRsource > TRsource[1] and upward > 0 or TRsource > filt and TRsource < TRsource[1] and upward > 0 shortCond := TRsource < filt and TRsource < TRsource[1] and downward > 0 or TRsource < filt and TRsource > TRsource[1] and downward > 0 CondIni = 0 CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1] TRFlong = longCond and CondIni[1] == -1 TRFshort = shortCond and CondIni[1] == 1 // Salma // Corrects price points within specific StdDev band before calculting a smoothed WMA salmaprice = input(close, 'Source') salmalength = input.int(3, 'Length', minval=1) salmasmooth = input.int(1, 'Extra Smooth [1 = None]', minval=1) mult = input.float(0.3, minval=0.05, maxval=3, step=0.05, title='Width', inline = 'SD Channel', group='Volatility Filter (SD Channel)') sd_len = input.int(5, minval=1, title='Length', inline = 'SD Channel', group='Volatility Filter (SD Channel)') baseline = ta.wma(salmaprice, sd_len) dev = mult * ta.stdev(salmaprice, sd_len) upper = baseline + dev lower = baseline - dev cprice = salmaprice > upper ? upper : salmaprice < lower ? lower : salmaprice REMA = ta.wma(ta.wma(cprice, salmalength), salmasmooth) c_up = color.new(#33ff00, 0) c_dn = color.new(#ff1111, 0) REMA_up = REMA > REMA[1] REMA_dn = REMA < REMA[1] // ====================================================================================================== // add optional MA's - to enable us to track what many other traders are working with // These MA's will be hidden by default until user exposes them as needed in the Settings // the below code is based on the built-in MA Ribbon in the TV library - with some modifications // ====================================================================== f_ma(source, length, type) => type == 'SMA' ? ta.sma(source, length) : type == 'EMA' ? ta.ema(source, length) : ta.wma(source, length) // ====================================================================== gr_ma = 'Optional MA\'s' t_ma1 = 'MA #1' t_ma2 = 'MA #2' // Plotting TRFs plotshape(TRFlong, title='Long', text='Long', style=shape.labelup, textcolor=color.new(color.black, 0), size=size.tiny, location=location.belowbar, color=color.new(color.lime, 0)) plotshape(TRFshort, title='Short', text='Short', style=shape.labeldown, textcolor=color.new(color.white, 0), size=size.tiny, location=location.abovebar, color=color.new(color.red, 0)) // Video use_trailing_Stop_loss = input.bool(title="Use Trailing Stop Loss?", defval = true) percent_based= input.float(1.5,"Stop Multiplier", minval=0.0, step = 0.5) // stop loss is going to be a percentage of the closing price. r_ratio=input.float(2,"RR Ratio",minval=0.0, step = 0.5) // rr ratio here is 2 stop_loss_amount = close * percent_based/100 bought=strategy.position_size[0] > strategy.position_size[1] // if we have a buy, the last pos is greater than the position before it since_entry_long = ta.barssince(bought) sold=strategy.position_size[0] < strategy.position_size[1] since_entry_short= ta.barssince(sold) price_stop_long = 0.0 price_stop_short = 100000000000000.0 targetvalue_long_1 = 0.0 targetvalue_short_1 = 0.0 stopvalue_long = 0.0 stopvalue_short = 0.0 // stop loss (constant stop loss) and constant if(strategy.position_size>0) stopvalue_long := close - stop_loss_amount if use_trailing_Stop_loss == false price_stop_long := close[since_entry_long] - stop_loss_amount[since_entry_long] else price_stop_long := math.max(stopvalue_long,price_stop_long[1]) targetvalue_long_1 := close[since_entry_long] + close[since_entry_long]*(percent_based/100)*r_ratio else price_stop_long := 0.0 if(strategy.position_size>0) stopvalue_short := close + stop_loss_amount if use_trailing_Stop_loss == false price_stop_short := close[since_entry_short] + stop_loss_amount[since_entry_short] else price_stop_short := math.min(stopvalue_short,price_stop_short) targetvalue_short_1 := close[since_entry_short] - close[since_entry_short]*(percent_based/100)*r_ratio else price_stop_short := 100000000000000.0 longCondition = k[0] > k[1] and macdDAY[0] > macdDAY[1] and macdSignalDAY[0] > macdSignalDAY[1] and REMA_up and cci > -150 and chop < 70 and roc[0] > roc [1] and tsi[0] > tsi[1] shortCondition = TRFshort and REMA_dn and macdDAY[0] < macdDAY [1] // executing trades if (longCondition) == true strategy.entry("Go Long", strategy.long, when = strategy.position_size <=0,alert_message = "Long Entry") if(shortCondition) == true strategy.entry("Short", strategy.short, when = strategy.position_size >=0,alert_message ="Short Entry") // stop loss and target price trading logics. strategy.exit("Exit Long", from_entry = "Long", limit=targetvalue_long_1, stop = price_stop_long, when = strategy.position_size > 0 ) strategy.exit("Exit Short", from_entry = "Short", limit=targetvalue_short_1, stop = price_stop_short, when = strategy.position_size < 0 ) // Plots P1 = plot(price_stop_long > 0 ? price_stop_long : na, "SL Long", color = color.rgb(205, 29, 26), style = plot.style_linebr) P2 = plot(price_stop_long > 0 ? targetvalue_long_1 : na, "Target Long", color = color.rgb(39, 176, 55), style = plot.style_linebr) P3 = plot(price_stop_short < 100000000000000.0 ? price_stop_long : na, "SL Short", color = color.rgb(205, 29, 26), style = plot.style_linebr) P4 = plot(price_stop_short < 100000000000000.0 ? targetvalue_long_1 : na, "Target Short", color = color.rgb(39, 176, 55), style = plot.style_linebr)