Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=6
- indicator("Ocs Ai Trader", shorttitle = "Ocs Ai Trader", overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
- const string GRP_GEN = "General Settings"
- const string GRP_TSP = "Time Series Processing Layer"
- const string GRP_SPL = "Signal Processing Layer"
- const string GRP_FLT = "Filters"
- const string GRP_ML = "Machine Learning (KNN)"
- const string GRP_GAU = "Gaussian Structure"
- const string GRP_VTE = "Virtual Trade Emulator"
- const string GRP_SR = "Support & Resistance"
- const string GRP_DSP = "Display"
- const string GRP_TBL = "Backtesting Compiler (Stats Table)"
- const string GRP_COL = "Colors"
- const string TXT_TINY = "Tiny"
- const string TXT_SMALL = "Small"
- const string TXT_NORMAL = "Normal"
- const string TXT_LARGE = "Large"
- const string HIT_BOOKED = "✓Booked"
- const string HIT_PLAIN = "Hit"
- i_confirmBar = input.bool(true, "Show Trade Signal only when bar is confirmed", group = GRP_GEN, tooltip = "When enabled, signals are produced on bar close only (no repaint).")
- i_isPremium = input.bool(true, "Is your TradingView Subscription Premium and above ?", group = GRP_GEN, tooltip = "Premium plans can read second based intrabar data for a precise Volume Delta. If your plan is not Premium, UNCHECK this option.")
- i_ltf = input.timeframe("1", "Lower Timeframe to use if TradingView Subscription is not premium !", group = GRP_GEN, tooltip = "Lower timeframe used for the Volume Delta when the Premium option is unchecked. Ignored if it is not lower than the chart timeframe.")
- i_textSize = input.string(TXT_SMALL, "Text Size :", options = [TXT_TINY, TXT_SMALL, TXT_NORMAL, TXT_LARGE], group = GRP_GEN)
- i_sensitivity = input.float(1.0, "Signal Sensitivity", minval = 0.1, maxval = 5.0, step = 0.1, group = GRP_GEN, tooltip = "Exponential Probability Squeeze factor. Higher values produce more decisive probabilities and more signals.")
- i_alertPass = input.string("", "Alert Passcode", group = GRP_GEN, tooltip = "Optional passcode appended to every alert() message (useful for webhook authentication).")
- i_dcMin = input.int(8, "Dominant Cycle Period Min", minval = 4, maxval = 50, group = GRP_TSP, inline = "dc")
- i_dcMax = input.int(48, "Max", minval = 10, maxval = 100, group = GRP_TSP, inline = "dc", tooltip = "Bounds of the Ehlers Homodyne Dominant Cycle. The detected cycle is used as the dynamic length of the adaptive calculations.")
- i_amaFast = input.int(2, "Adaptive Moving Average Fast", minval = 1, group = GRP_TSP, inline = "ama")
- i_amaSlow = input.int(30, "Slow", minval = 2, group = GRP_TSP, inline = "ama")
- i_stFactor = input.float(3.0, "Supertrend Factor", minval = 0.5, step = 0.1, group = GRP_TSP, inline = "st")
- i_stAtrLen = input.int(10, "ATR", minval = 1, group = GRP_TSP, inline = "st")
- i_stochLen = input.int(14, "Relative Strength Stochastics Length", minval = 2, group = GRP_TSP, inline = "sto")
- i_stochSmooth = input.int(3, "Smooth", minval = 1, group = GRP_TSP, inline = "sto")
- i_cvdLen = input.int(21, "Cumulative Volume Delta Length", minval = 2, group = GRP_TSP)
- i_trixLen = input.int(14, "TRIX Length", minval = 2, group = GRP_TSP, inline = "trix")
- i_trixSig = input.int(9, "Signal", minval = 1, group = GRP_TSP, inline = "trix")
- i_conf = input.float(0.60, "Probability Distribution Confidence", minval = 0.50, maxval = 0.95, step = 0.01, group = GRP_SPL, tooltip = "Minimum bullish probability for long intents (and maximum for short intents = 1 - value).")
- i_probAvg = input.int(3, "Probability Average Length", minval = 1, maxval = 20, group = GRP_SPL)
- i_probVarMax = input.float(0.30, "Probability Variable Filter (Max StDev)", minval = 0.01, maxval = 1.0, step = 0.01, group = GRP_SPL)
- i_lmsLen = input.int(16, "Adaptive LMS Filter Order", minval = 2, maxval = 64, group = GRP_SPL, inline = "lms")
- i_lmsMu = input.float(0.05, "Step (mu)", minval = 0.001, maxval = 1.0, step = 0.001, group = GRP_SPL, inline = "lms")
- i_fisherTol = input.float(0.25, "Fisher Transform Tolerance", minval = 0.0, maxval = 3.0, step = 0.05, group = GRP_SPL)
- i_qrLen = input.int(20, "Quadratic Regression / Trimean Length", minval = 5, maxval = 200, group = GRP_SPL)
- i_useCvdDiv = input.bool(true, "CVD Divergence Signal Structure", group = GRP_SPL, inline = "div")
- i_divLen = input.int(14, "Lookback", minval = 3, group = GRP_SPL, inline = "div")
- i_useRegime = input.bool(true, "Regime Filter", group = GRP_FLT, inline = "reg")
- i_regimeThr = input.float(-0.1, "Threshold", minval = -10.0, maxval = 10.0, step = 0.1, group = GRP_FLT, inline = "reg")
- i_useVolFilter = input.bool(true, "Volatility Probability Filter", group = GRP_FLT)
- i_useAtrImpulse = input.bool(true, "Impulsive ATR Filter", group = GRP_FLT, inline = "imp")
- i_atrImpMult = input.float(0.75, "Multiplier", minval = 0.1, step = 0.05, group = GRP_FLT, inline = "imp")
- i_useVolDeriv = input.bool(true, "Volatility Derivative Filter (Elder)", group = GRP_FLT, inline = "vd")
- i_volDerivTol = input.float(0.15, "Tolerance", minval = 0.0, step = 0.01, group = GRP_FLT, inline = "vd")
- i_useTrix = input.bool(true, "TRIX Strength / Weakness Analyser", group = GRP_FLT)
- i_useCvd = input.bool(true, "Cumulative Volume Delta Regime Filter", group = GRP_FLT)
- i_useMarubozu = input.bool(true, "Marubozu Abnormality Detection", group = GRP_FLT, inline = "maru")
- i_maruBody = input.float(0.85, "Body Ratio", minval = 0.5, maxval = 1.0, step = 0.01, group = GRP_FLT, inline = "maru")
- i_useKnn = input.bool(true, "K-Nearest Neighbour Classification", group = GRP_ML)
- i_knnK = input.int(8, "Neighbours (K)", minval = 1, maxval = 50, group = GRP_ML)
- i_knnWindow = input.int(1000, "Training Window (Samples)", minval = 50, maxval = 5000, group = GRP_ML)
- i_knnStride = input.int(4, "Sampling Stride", minval = 1, maxval = 20, group = GRP_ML)
- i_knnHorizon = input.int(4, "Prediction Horizon (Bars)", minval = 1, maxval = 50, group = GRP_ML)
- i_knnWeight = input.float(0.50, "KNN Weight in Final Probability", minval = 0.0, maxval = 1.0, step = 0.05, group = GRP_ML)
- i_gaussAdaptive = input.bool(true, "Adaptive Length (Dominant Cycle)", group = GRP_GAU)
- i_gaussLen = input.int(21, "Gaussian Length", minval = 2, group = GRP_GAU, inline = "gau")
- i_gaussPoles = input.int(4, "Poles", minval = 1, maxval = 4, group = GRP_GAU, inline = "gau")
- i_gaussMult = input.float(1.5, "Band ATR Multiplier", minval = 0.1, step = 0.1, group = GRP_GAU)
- i_baseLen = input.int(144, "Gaussian Baseline Length", minval = 10, group = GRP_GAU)
- i_atrLenTrade = input.int(14, "ATR Length", minval = 1, group = GRP_VTE)
- i_tMult = input.float(1.0, "Target Step (ATR)", minval = 0.1, step = 0.05, group = GRP_VTE)
- i_stopMult = input.float(2.25, "Stop Distance (ATR)", minval = 0.1, step = 0.05, group = GRP_VTE)
- i_showT45 = input.bool(false, "Dynamic Target 4 & Target 5", group = GRP_VTE)
- i_recoveryBars = input.int(10, "Recovery Window (Bars)", minval = 0, maxval = 100, group = GRP_VTE, tooltip = "After the stop is touched, a trade that still reaches T1 within this window is counted as a T1 Win (With Recoveries).")
- i_labelOffset = input.int(4, "Levels Label Offset (Bars)", minval = 1, maxval = 100, group = GRP_VTE, tooltip = "Distance, in bars, between the last bar and the level labels.")
- i_hitStyle = input.string(HIT_BOOKED, "Target Hit Text", options = [HIT_BOOKED, HIT_PLAIN], group = GRP_VTE)
- i_showVolSR = input.bool(true, "Volume Float Support & Resistance", group = GRP_SR)
- i_srPivotLen = input.int(10, "Pivot Length", minval = 2, group = GRP_SR, inline = "vsr")
- i_srVolMult = input.float(1.2, "Volume Multiplier", minval = 0.1, step = 0.1, group = GRP_SR, inline = "vsr")
- i_srMaxBars = input.int(60, "Max Extension (Bars)", minval = 5, maxval = 500, group = GRP_SR)
- i_showPivots = input.bool(true, "Pivotal Points & Lines", group = GRP_SR)
- i_pivLen = input.int(20, "Pivot Length", minval = 2, group = GRP_SR, inline = "piv")
- i_pivMaxBars = input.int(50, "Max Extension (Bars)", minval = 5, maxval = 500, group = GRP_SR, inline = "piv")
- i_maxZones = input.int(20, "Max Zones Kept On Chart", minval = 2, maxval = 100, group = GRP_SR)
- i_showBand = input.bool(true, "Gaussian Trend Band", group = GRP_DSP, inline = "band")
- i_bandWidth = input.int(10, "Thickness", minval = 1, maxval = 30, group = GRP_DSP, inline = "band")
- i_showBaseline = input.bool(true, "Gaussian Baseline", group = GRP_DSP)
- i_showDots = input.bool(true, "Probability Triangles", group = GRP_DSP)
- i_showBarColor = input.bool(true, "Colour Signal Candles", group = GRP_DSP)
- i_showHistory = input.bool(false, "Historical Buy Sell Positions by Algo", group = GRP_DSP)
- i_showLevels = input.bool(true, "Targets, Strength & Stop Levels", group = GRP_DSP)
- i_showTable = input.bool(true, "Show Stats Table", group = GRP_TBL)
- i_tablePos = input.string("Bottom Left", "Position", options = ["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group = GRP_TBL, inline = "tbl")
- i_tableSize = input.string(TXT_TINY, "Size", options = [TXT_TINY, TXT_SMALL, TXT_NORMAL, TXT_LARGE], group = GRP_TBL, inline = "tbl")
- i_colBandBull = input.color(color.new(#4caf50, 82), "Trend Band Bull", group = GRP_COL, inline = "c1")
- i_colBandBear = input.color(color.new(#f23645, 82), "Bear", group = GRP_COL, inline = "c1")
- i_colDotBull = input.color(color.new(#81c784, 30), "Triangles Bull", group = GRP_COL, inline = "c2")
- i_colDotBear = input.color(color.new(#ef9a9a, 20), "Bear", group = GRP_COL, inline = "c2")
- i_colDotNeut = input.color(color.new(#9598a1, 25), "Triangles Neutral", group = GRP_COL, inline = "c2b")
- i_colDotWarn = input.color(color.new(#ffb74d, 10), "Caution", group = GRP_COL, inline = "c2b")
- i_colBaseline = input.color(color.new(#a5d6a7, 40), "Gaussian Baseline", group = GRP_COL)
- i_colBuyBar = input.color(#7ee8c4, "Signal Candle Buy", group = GRP_COL, inline = "c3")
- i_colSellBar = input.color(#f4a6d7, "Sell", group = GRP_COL, inline = "c3")
- i_colBuyTgt = input.color(#2962ff, "Buy Targets Text", group = GRP_COL, inline = "b1")
- i_colBuyTgtBg = input.color(color.new(#2962ff, 90), "Bg", group = GRP_COL, inline = "b1")
- i_colBuyTgtLn = input.color(color.new(#4caf50, 30), "Line", group = GRP_COL, inline = "b1")
- i_colBuyEnt = input.color(#089981, "Buy Strength Text", group = GRP_COL, inline = "b2")
- i_colBuyEntBg = input.color(color.new(#4caf50, 88), "Bg", group = GRP_COL, inline = "b2")
- i_colBuyEntLn = input.color(#2e9d4a, "Line", group = GRP_COL, inline = "b2")
- i_colBuyStp = input.color(#f23645, "Buy Stop Text", group = GRP_COL, inline = "b3")
- i_colBuyStpBg = input.color(color.new(#f7525f, 88), "Bg", group = GRP_COL, inline = "b3")
- i_colBuyStpLn = input.color(color.new(#f23645, 30), "Line", group = GRP_COL, inline = "b3")
- i_colSellTgt = input.color(#f23645, "Sell Targets Text", group = GRP_COL, inline = "s1")
- i_colSellTgtBg = input.color(color.new(#f23645, 90), "Bg", group = GRP_COL, inline = "s1")
- i_colSellTgtLn = input.color(color.new(#f23645, 30), "Line", group = GRP_COL, inline = "s1")
- i_colSellEnt = input.color(#f23645, "Sell Weakness Text", group = GRP_COL, inline = "s2")
- i_colSellEntBg = input.color(color.new(#ff9800, 85), "Bg", group = GRP_COL, inline = "s2")
- i_colSellEntLn = input.color(#f23645, "Line", group = GRP_COL, inline = "s2")
- i_colSellStp = input.color(#2962ff, "Sell Stop Text", group = GRP_COL, inline = "s3")
- i_colSellStpBg = input.color(color.new(#4caf50, 88), "Bg", group = GRP_COL, inline = "s3")
- i_colSellStpLn = input.color(color.new(#2962ff, 30), "Line", group = GRP_COL, inline = "s3")
- i_colBrand = input.color(#f7525f, "Brand Text", group = GRP_COL, inline = "c7")
- i_colBrandBg = input.color(color.new(#ff9800, 88), "Background", group = GRP_COL, inline = "c7")
- i_colVolZone = input.color(color.new(#787b86, 85), "Volume Float Zones", group = GRP_COL)
- i_colPivZone = input.color(color.new(#2196f3, 85), "Pivotal Zones", group = GRP_COL)
- i_colLong = input.color(#089981, "Stats Long", group = GRP_COL, inline = "c8")
- i_colShort = input.color(#f23645, "Short", group = GRP_COL, inline = "c8")
- i_colTblBg = input.color(#ffffff, "Table Background", group = GRP_COL, inline = "c9")
- i_colTblText = input.color(#1e3a8a, "Text", group = GRP_COL, inline = "c9")
- i_colTblBorder = input.color(#b2b5be, "Border", group = GRP_COL, inline = "c9")
- const string VERSION_TXT = "Ocs AI ML V.312"
- const string BRAND_TXT = "Ocs Ai Trader"
- const string RECOVERY_TXT = "(With Recoveries)"
- const float EPS = 1e-10
- const int WARMUP_MULT = 2
- const float ZONE_MIN_ATR = 0.25
- const float PIV_ZONE_ATR = 0.50
- const float BRAND_GAP = 0.60
- const string ARROW_UP = "▲ "
- const string ARROW_DN = "▼ "
- const string WARN_ICON = "⚠ "
- const color TXT_DARK = #131722
- type TradeState
- int dir = 0
- int sigBar = 0
- float trigger = na
- float stop = na
- float t1 = na
- float t2 = na
- float t3 = na
- float t4 = na
- float t5 = na
- bool triggered = false
- bool t1Hit = false
- bool t2Hit = false
- bool t3Hit = false
- bool t4Hit = false
- bool t5Hit = false
- bool stopHit = false
- int stopBar = na
- bool closed = false
- type Stats
- int longTotal = 0
- int shortTotal = 0
- int longWins = 0
- int shortWins = 0
- type Zone
- box bx
- float top
- float bottom
- int dir
- int startBar
- bool active = true
- f_tanh(float x) =>
- float xc = math.max(math.min(nz(x), 20.0), -20.0)
- float e2 = math.exp(2.0 * xc)
- (e2 - 1.0) / (e2 + 1.0)
- f_smooth(float src, float alpha) =>
- float s = na
- s := na(s[1]) ? src : alpha * src + (1.0 - alpha) * s[1]
- s
- f_rma(float src, float len) =>
- f_smooth(src, 1.0 / math.max(len, 1.0))
- f_rsi(float src, int len) =>
- float chg = src - nz(src[1], src)
- float up = f_rma(math.max(chg, 0.0), len)
- float dn = f_rma(math.max(-chg, 0.0), len)
- dn <= EPS ? 100.0 : up <= EPS ? 0.0 : 100.0 - 100.0 / (1.0 + up / dn)
- f_normalize(float src, float lo, float hi) =>
- var float hMin = 1e10
- var float hMax = -1e10
- hMin := math.min(nz(src, hMin), hMin)
- hMax := math.max(nz(src, hMax), hMax)
- lo + (hi - lo) * (nz(src) - hMin) / math.max(hMax - hMin, EPS)
- f_vwma(float src, float vol, int len) =>
- float den = math.sum(vol, len)
- den > EPS ? math.sum(src * vol, len) / den : src
- f_kama(float src, int len, int fastLen, int slowLen) =>
- float change = math.abs(src - nz(src[len], src))
- float noise = math.sum(math.abs(src - nz(src[1], src)), len)
- float er = noise > EPS ? change / noise : 0.0
- float fastSc = 2.0 / (fastLen + 1.0)
- float slowSc = 2.0 / (slowLen + 1.0)
- float sc = math.pow(er * (fastSc - slowSc) + slowSc, 2)
- float k = na
- k := na(k[1]) ? src : k[1] + sc * (src - k[1])
- k
- f_dominantCycle(float src, int minP, int maxP) =>
- float period = 0.0
- float smoothP = 0.0
- float i2 = 0.0
- float q2 = 0.0
- float re = 0.0
- float im = 0.0
- float prevPeriod = nz(period[1], 15.0)
- float smooth = (4.0 * src + 3.0 * nz(src[1], src) + 2.0 * nz(src[2], src) + nz(src[3], src)) / 10.0
- float adj = 0.075 * prevPeriod + 0.54
- float detrender = (0.0962 * smooth + 0.5769 * nz(smooth[2]) - 0.5769 * nz(smooth[4]) - 0.0962 * nz(smooth[6])) * adj
- float q1 = (0.0962 * detrender + 0.5769 * nz(detrender[2]) - 0.5769 * nz(detrender[4]) - 0.0962 * nz(detrender[6])) * adj
- float i1 = nz(detrender[3])
- float jI = (0.0962 * i1 + 0.5769 * nz(i1[2]) - 0.5769 * nz(i1[4]) - 0.0962 * nz(i1[6])) * adj
- float jQ = (0.0962 * q1 + 0.5769 * nz(q1[2]) - 0.5769 * nz(q1[4]) - 0.0962 * nz(q1[6])) * adj
- i2 := 0.2 * (i1 - jQ) + 0.8 * nz(i2[1])
- q2 := 0.2 * (q1 + jI) + 0.8 * nz(q2[1])
- re := 0.2 * (i2 * nz(i2[1]) + q2 * nz(q2[1])) + 0.8 * nz(re[1])
- im := 0.2 * (i2 * nz(q2[1]) - q2 * nz(i2[1])) + 0.8 * nz(im[1])
- float p = im != 0.0 and re != 0.0 ? 2.0 * math.pi / math.atan(im / re) : prevPeriod
- p := math.min(p, 1.5 * prevPeriod)
- p := math.max(p, 0.67 * prevPeriod)
- p := math.max(math.min(p, maxP), minP)
- period := 0.2 * p + 0.8 * prevPeriod
- smoothP := 0.33 * period + 0.67 * nz(smoothP[1], period)
- smoothP
- f_gaussian(float src, float len, int poles) =>
- float beta = (1.0 - math.cos(2.0 * math.pi / math.max(len, 2.0))) / (math.pow(2.0, 1.0 / poles) - 1.0)
- float alpha = -beta + math.sqrt(beta * beta + 2.0 * beta)
- float x = 1.0 - alpha
- float f = na
- float f1 = nz(f[1], src)
- float f2 = nz(f[2], f1)
- float f3 = nz(f[3], f2)
- float f4 = nz(f[4], f3)
- f := switch poles
- 1 => alpha * src + x * f1
- 2 => math.pow(alpha, 2) * src + 2.0 * x * f1 - math.pow(x, 2) * f2
- 3 => math.pow(alpha, 3) * src + 3.0 * x * f1 - 3.0 * math.pow(x, 2) * f2 + math.pow(x, 3) * f3
- => math.pow(alpha, 4) * src + 4.0 * x * f1 - 6.0 * math.pow(x, 2) * f2 + 4.0 * math.pow(x, 3) * f3 - math.pow(x, 4) * f4
- f
- f_trailBand(float base, float atrv, float mult, float src) =>
- float a = nz(atrv, high - low)
- float upRaw = base - mult * a
- float dnRaw = base + mult * a
- float upT = na
- float dnT = na
- int dir = 1
- float upPrev = nz(upT[1], upRaw)
- float dnPrev = nz(dnT[1], dnRaw)
- upT := nz(src[1], src) > upPrev ? math.max(upRaw, upPrev) : upRaw
- dnT := nz(src[1], src) < dnPrev ? math.min(dnRaw, dnPrev) : dnRaw
- int prevDir = nz(dir[1], 1)
- dir := prevDir == -1 and src > dnPrev ? 1 : prevDir == 1 and src < upPrev ? -1 : prevDir
- float trail = dir == 1 ? upT : dnT
- [trail, dir]
- f_fisher(float src, int len) =>
- float hh = ta.highest(src, len)
- float ll = ta.lowest(src, len)
- float rng = hh - ll
- float v = 0.0
- v := 0.66 * (rng > EPS ? (src - ll) / rng - 0.5 : 0.0) + 0.67 * nz(v[1])
- v := math.max(math.min(v, 0.999), -0.999)
- float fish = 0.0
- fish := 0.5 * math.log((1.0 + v) / (1.0 - v)) + 0.5 * nz(fish[1])
- fish
- f_lmsPredict(float src, simple int len, float mu) =>
- var array<float> w = array.new_float(len, 0.0)
- float pred = 0.0
- float energy = EPS
- for i = 0 to len - 1
- float xi = nz(src[i + 1])
- pred += w.get(i) * xi
- energy += xi * xi
- float err = nz(src) - pred
- for i = 0 to len - 1
- w.set(i, w.get(i) + mu * err * nz(src[i + 1]) / energy)
- float nxt = 0.0
- for i = 0 to len - 1
- nxt += w.get(i) * nz(src[i])
- nxt
- f_quadReg(float src, simple int n) =>
- float sx = 0.0
- float sx2 = 0.0
- float sx3 = 0.0
- float sx4 = 0.0
- float sy = 0.0
- float sxy = 0.0
- float sx2y = 0.0
- for i = 0 to n - 1
- float x = n - 1.0 - i
- float y = nz(src[i], src)
- sx += x
- sx2 += x * x
- sx3 += x * x * x
- sx4 += x * x * x * x
- sy += y
- sxy += x * y
- sx2y += x * x * y
- float det = n * (sx2 * sx4 - sx3 * sx3) - sx * (sx * sx4 - sx3 * sx2) + sx2 * (sx * sx3 - sx2 * sx2)
- float a = src
- float b = 0.0
- float c = 0.0
- if math.abs(det) > EPS
- a := (sy * (sx2 * sx4 - sx3 * sx3) - sx * (sxy * sx4 - sx3 * sx2y) + sx2 * (sxy * sx3 - sx2 * sx2y)) / det
- b := (n * (sxy * sx4 - sx3 * sx2y) - sy * (sx * sx4 - sx3 * sx2) + sx2 * (sx * sx2y - sxy * sx2)) / det
- c := (n * (sx2 * sx2y - sxy * sx3) - sx * (sx * sx2y - sxy * sx2) + sy * (sx * sx3 - sx2 * sx2)) / det
- float xn = n - 1.0
- [a + b * xn + c * xn * xn, b + 2.0 * c * xn]
- f_trimean(float src, int len) =>
- float q1 = ta.percentile_linear_interpolation(src, len, 25)
- float q2 = ta.median(src, len)
- float q3 = ta.percentile_linear_interpolation(src, len, 75)
- (q1 + 2.0 * q2 + q3) / 4.0
- f_regimeFilter(float src, float threshold) =>
- float value1 = 0.0
- float value2 = 0.0
- float klmf = 0.0
- value1 := 0.2 * (src - nz(src[1], src)) + 0.8 * nz(value1[1])
- value2 := 0.1 * (high - low) + 0.8 * nz(value2[1])
- float omega = value2 != 0.0 ? math.abs(value1 / value2) : 0.0
- float alpha = (-omega * omega + math.sqrt(math.pow(omega, 4) + 16.0 * math.pow(omega, 2))) / 8.0
- klmf := alpha * src + (1.0 - alpha) * nz(klmf[1], src)
- float absSlope = math.abs(klmf - nz(klmf[1], klmf))
- float avgSlope = nz(ta.ema(absSlope, 200))
- float normDecline = avgSlope != 0.0 ? (absSlope - avgSlope) / avgSlope : 0.0
- normDecline >= threshold
- f_knnPredict(array<float> a1, array<float> a2, array<float> a3, array<float> a4, array<float> a5, array<int> lbl, float q1, float q2, float q3, float q4, float q5, int k, int stride) =>
- array<float> dists = array.new_float()
- array<int> votes = array.new_int()
- int n = lbl.size()
- if n > 0
- for i = 0 to n - 1 by stride
- float d = math.log(1.0 + math.abs(a1.get(i) - q1)) + math.log(1.0 + math.abs(a2.get(i) - q2)) + math.log(1.0 + math.abs(a3.get(i) - q3)) + math.log(1.0 + math.abs(a4.get(i) - q4)) + math.log(1.0 + math.abs(a5.get(i) - q5))
- int sz = dists.size()
- bool accept = sz < k
- if not accept
- accept := d < dists.get(sz - 1)
- if accept
- int pos = 0
- while pos < sz
- if dists.get(pos) > d
- break
- pos += 1
- dists.insert(pos, d)
- votes.insert(pos, lbl.get(i))
- if dists.size() > k
- dists.pop()
- votes.pop()
- float s = 0.0
- for v in votes
- s += v
- votes.size() > 0 ? s / votes.size() : 0.0
- f_reached(int dir, float lvl) =>
- dir == 1 ? high >= lvl : low <= lvl
- f_stopped(int dir, float lvl) =>
- dir == 1 ? low <= lvl : high >= lvl
- f_updateZones(array<Zone> zones, int maxBars) =>
- for z in zones
- if z.active
- z.bx.set_right(bar_index)
- bool broken = z.dir == 1 ? close < z.bottom : close > z.top
- if broken or bar_index - z.startBar >= maxBars
- z.active := false
- zones.size()
- f_addZone(array<Zone> zones, Zone z, int maxKeep) =>
- zones.push(z)
- if zones.size() > maxKeep
- Zone old = zones.shift()
- old.bx.delete()
- zones.size()
- f_drawLevel(array<line> lns, array<label> lbs, int x1, int x2, float y, color lineCol, string lineStyle, int width, string txt, color txtCol, color bgCol, string txtSize, bool bold) =>
- lns.push(line.new(x1, y, x2, y, xloc = xloc.bar_index, color = lineCol, style = lineStyle, width = width))
- label lb = label.new(x2, y, txt, xloc = xloc.bar_index, color = bgCol, style = label.style_label_left, textcolor = txtCol, size = txtSize, textalign = text.align_left)
- if bold
- lb.set_text_formatting(text.format_bold)
- lbs.push(lb)
- lns.size()
- f_fmt(float v) =>
- str.tostring(v, format.mintick)
- f_targetText(int dir, string name, float lvl, bool hit) =>
- string arrow = hit or dir == 1 ? ARROW_UP : ARROW_DN
- string suffix = hit ? (i_hitStyle == HIT_BOOKED ? " " + HIT_BOOKED : ", " + HIT_PLAIN) : ""
- arrow + name + " at " + f_fmt(lvl) + suffix
- f_pct(int wins, int total) =>
- total > 0 ? str.tostring(wins * 100.0 / total, "#.##") + " %" : "0 %"
- f_tablePosition(string s) =>
- switch s
- "Top Left" => position.top_left
- "Top Center" => position.top_center
- "Top Right" => position.top_right
- "Middle Left" => position.middle_left
- "Middle Right" => position.middle_right
- "Bottom Center" => position.bottom_center
- "Bottom Right" => position.bottom_right
- => position.bottom_left
- f_textSize(string s) =>
- switch s
- TXT_SMALL => size.small
- TXT_NORMAL => size.normal
- TXT_LARGE => size.large
- => size.tiny
- float volSafe = na(volume) or volume <= 0 ? 1.0 : volume
- float barRange = high - low
- float atr14 = ta.atr(14)
- float atrTrade = ta.atr(i_atrLenTrade)
- float atrSafe = math.max(nz(atr14, barRange), EPS)
- float dcPeriod = f_dominantCycle(hl2, i_dcMin, i_dcMax)
- int dynLen = math.max(i_dcMin, math.min(i_dcMax, math.round(nz(dcPeriod, i_gaussLen))))
- int chartSec = timeframe.in_seconds(timeframe.period)
- string premiumLtf = chartSec <= 3600 ? "1" : chartSec <= 14400 ? "5" : chartSec <= 86400 ? "15" : "60"
- string wantedLtf = i_isPremium ? premiumLtf : i_ltf
- string ltfTf = timeframe.in_seconds(wantedLtf) < chartSec ? wantedLtf : timeframe.period
- [ltfUpArr, ltfDnArr] = request.security_lower_tf(syminfo.tickerid, ltfTf, [close > open ? nz(volume) : 0.0, close < open ? nz(volume) : 0.0], ignore_invalid_symbol = true)
- bool useLtf = ltfTf != timeframe.period and ltfUpArr.size() > 0
- float ltfUp = useLtf ? nz(ltfUpArr.sum()) : 0.0
- float ltfDn = useLtf ? nz(ltfDnArr.sum()) : 0.0
- float estBuy = barRange > 0 ? volSafe * (close - low) / barRange : volSafe * 0.5
- float estSell = volSafe - estBuy
- float barDelta = useLtf ? ltfUp - ltfDn : estBuy - estSell
- float cvd = ta.cum(barDelta)
- float cvdEma = ta.ema(cvd, i_cvdLen)
- float cvdOsc = (cvd - nz(cvdEma, cvd)) / math.max(nz(ta.stdev(barDelta, i_cvdLen)), EPS)
- float cvdSlope = nz(ta.change(cvdEma))
- float obvV = ta.cum(math.sign(nz(ta.change(close))) * volSafe)
- float obvFast = ta.ema(obvV, 5)
- float obvMid = ta.ema(obvV, 13)
- float obvSlow = ta.ema(obvV, 34)
- float obvStd = math.max(nz(ta.stdev(obvV, 34)), EPS)
- float obvImpulse = (0.2 * (obvV - nz(obvFast, obvV)) + 0.5 * (nz(obvFast, obvV) - nz(obvMid, obvV)) + 0.3 * (nz(obvMid, obvV) - nz(obvSlow, obvV))) / obvStd
- float trixE = ta.ema(ta.ema(ta.ema(math.log(close), i_trixLen), i_trixLen), i_trixLen)
- float trix = 10000.0 * nz(ta.change(trixE))
- float trixSignal = ta.ema(trix, i_trixSig)
- float trixNorm = trix / math.max(nz(ta.stdev(trix, 50)), EPS)
- float vwmaV = f_vwma(close, volSafe, dynLen)
- float kamaV = f_kama(close, dynLen, i_amaFast, i_amaSlow)
- float rsiV = f_rsi(close, dynLen)
- float stochRsiK = ta.sma(ta.stoch(rsiV, rsiV, rsiV, i_stochLen), i_stochSmooth)
- [stLine, stDir] = ta.supertrend(i_stFactor, i_stAtrLen)
- float fisherV = f_fisher(hl2, dynLen)
- float gaussLenF = i_gaussAdaptive ? float(dynLen) : float(i_gaussLen)
- float gaussBase = f_gaussian(hlc3, gaussLenF, i_gaussPoles)
- [bandTrail, bandDir] = f_trailBand(gaussBase, atr14, i_gaussMult, close)
- float baseline = f_gaussian(close, i_baseLen, 4)
- float lmsSrc = close - gaussBase
- float lmsNext = f_lmsPredict(lmsSrc, i_lmsLen, i_lmsMu)
- float lmsStd = math.max(nz(ta.stdev(lmsSrc, 50)), EPS)
- [qrFit, qrSlope] = f_quadReg(close, i_qrLen)
- float trimeanV = f_trimean(close, i_qrLen)
- float hyperV = f_tanh((close - nz(close[dynLen], close)) / math.max(atrSafe * math.sqrt(dynLen), EPS))
- float c1 = f_tanh((close - vwmaV) / atrSafe)
- float c2 = f_tanh((close - kamaV) / atrSafe)
- float c3 = (rsiV - 50.0) / 50.0
- float c4 = (nz(stochRsiK, 50.0) - 50.0) / 50.0
- float c5 = stDir < 0 ? 1.0 : -1.0
- float c6 = math.abs(fisherV) > i_fisherTol ? f_tanh(fisherV) : 0.0
- float c7 = f_tanh(trixNorm)
- float c8 = f_tanh(cvdOsc)
- float c9 = f_tanh(obvImpulse)
- float c10 = f_tanh(nz(lmsNext) / lmsStd)
- float c11 = f_tanh(nz(qrSlope) * 5.0 / atrSafe)
- float c12 = hyperV
- float c13 = f_tanh((close - nz(trimeanV, close)) / atrSafe)
- float tsScore = (c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10 + c11 + c12 + c13) / 13.0
- float probRaw = 1.0 / (1.0 + math.exp(-4.0 * i_sensitivity * tsScore))
- float probAvg = nz(ta.sma(probRaw, i_probAvg), probRaw)
- float probStd = nz(ta.stdev(probRaw, 5))
- float feat1 = c3
- float feat2 = f_tanh(fisherV / 2.0)
- float feat3 = f_normalize(trixNorm, -1.0, 1.0)
- float feat4 = f_normalize(cvdOsc, -1.0, 1.0)
- float feat5 = c4
- var array<float> knnA1 = array.new_float()
- var array<float> knnA2 = array.new_float()
- var array<float> knnA3 = array.new_float()
- var array<float> knnA4 = array.new_float()
- var array<float> knnA5 = array.new_float()
- var array<int> knnLbl = array.new_int()
- if i_useKnn and bar_index > i_knnHorizon
- int lbl = close > close[i_knnHorizon] ? 1 : close < close[i_knnHorizon] ? -1 : 0
- knnA1.push(feat1[i_knnHorizon])
- knnA2.push(feat2[i_knnHorizon])
- knnA3.push(feat3[i_knnHorizon])
- knnA4.push(feat4[i_knnHorizon])
- knnA5.push(feat5[i_knnHorizon])
- knnLbl.push(lbl)
- if knnLbl.size() > i_knnWindow
- knnA1.shift()
- knnA2.shift()
- knnA3.shift()
- knnA4.shift()
- knnA5.shift()
- knnLbl.shift()
- float knnScore = i_useKnn ? f_knnPredict(knnA1, knnA2, knnA3, knnA4, knnA5, knnLbl, feat1, feat2, feat3, feat4, feat5, i_knnK, i_knnStride) : 0.0
- float knnProb = (knnScore + 1.0) / 2.0
- float knnW = i_useKnn ? i_knnWeight : 0.0
- float finalProb = probAvg * (1.0 - knnW) + knnProb * knnW
- bool regimeOk = f_regimeFilter(ohlc4, i_regimeThr)
- bool volOk = nz(ta.atr(1)) > nz(ta.atr(10))
- bool atrImpulseOk = nz(ta.atr(5)) >= nz(ta.sma(ta.tr(true), 30)) * i_atrImpMult
- float volDeriv = (atrSafe - nz(atr14[3], atrSafe)) / atrSafe
- bool volDerivOk = nz(ta.ema(volDeriv, 3)) >= -i_volDerivTol
- bool trixLongOk = trix >= nz(trixSignal, trix) or trix > 0
- bool trixShortOk = trix <= nz(trixSignal, trix) or trix < 0
- bool cvdLongOk = cvdSlope >= 0
- bool cvdShortOk = cvdSlope <= 0
- float bodySize = math.abs(close - open)
- bool isMarubozu = barRange > 0 and bodySize / barRange >= i_maruBody and barRange >= atrSafe * 1.2
- bool bullMarubozu = isMarubozu and close > open
- bool bearMarubozu = isMarubozu and close < open
- bool probVarOk = probStd <= i_probVarMax
- float pLow = ta.lowest(low, i_divLen)
- float pHigh = ta.highest(high, i_divLen)
- float cLow = ta.lowest(cvd, i_divLen)
- float cHigh = ta.highest(cvd, i_divLen)
- bool bullDiv = pLow < nz(pLow[i_divLen], pLow) and cLow > nz(cLow[i_divLen], cLow)
- bool bearDiv = pHigh > nz(pHigh[i_divLen], pHigh) and cHigh < nz(cHigh[i_divLen], cHigh)
- bool barOk = not i_confirmBar or barstate.isconfirmed
- bool filtersLong = (not i_useRegime or regimeOk) and (not i_useVolFilter or volOk) and (not i_useAtrImpulse or atrImpulseOk) and (not i_useVolDeriv or volDerivOk) and (not i_useTrix or trixLongOk) and (not i_useCvd or cvdLongOk or bullDiv) and (not i_useMarubozu or not bearMarubozu) and probVarOk
- bool filtersShort = (not i_useRegime or regimeOk) and (not i_useVolFilter or volOk) and (not i_useAtrImpulse or atrImpulseOk) and (not i_useVolDeriv or volDerivOk) and (not i_useTrix or trixShortOk) and (not i_useCvd or cvdShortOk or bearDiv) and (not i_useMarubozu or not bullMarubozu) and probVarOk
- bool rawLong = bandDir == 1 and (finalProb >= i_conf or (i_useCvdDiv and bullDiv and finalProb > 0.5))
- bool rawShort = bandDir == -1 and (finalProb <= 1.0 - i_conf or (i_useCvdDiv and bearDiv and finalProb < 0.5))
- var int lastDir = 0
- bool buySignal = false
- bool sellSignal = false
- if barOk and bar_index > i_dcMax * WARMUP_MULT
- if rawLong and filtersLong and lastDir != 1
- buySignal := true
- lastDir := 1
- else if rawShort and filtersShort and lastDir != -1
- sellSignal := true
- lastDir := -1
- var TradeState cur = na
- var Stats stats = Stats.new()
- bool evTriggered = false
- bool evT1 = false
- bool evT2 = false
- bool evT3 = false
- bool evT4 = false
- bool evT5 = false
- bool evStop = false
- int evDir = 0
- if not na(cur)
- if not cur.closed and not cur.triggered and bar_index > cur.sigBar
- if f_reached(cur.dir, cur.trigger)
- cur.triggered := true
- evTriggered := true
- evDir := cur.dir
- if cur.dir == 1
- stats.longTotal := stats.longTotal + 1
- else
- stats.shortTotal := stats.shortTotal + 1
- if not cur.closed and cur.triggered
- evDir := cur.dir
- if not cur.stopHit and f_stopped(cur.dir, cur.stop)
- cur.stopHit := true
- cur.stopBar := bar_index
- evStop := true
- if not cur.t1Hit and f_reached(cur.dir, cur.t1)
- cur.t1Hit := true
- evT1 := true
- if cur.dir == 1
- stats.longWins := stats.longWins + 1
- else
- stats.shortWins := stats.shortWins + 1
- if not cur.stopHit
- if cur.t1Hit and not cur.t2Hit and f_reached(cur.dir, cur.t2)
- cur.t2Hit := true
- evT2 := true
- if cur.t2Hit and not cur.t3Hit and f_reached(cur.dir, cur.t3)
- cur.t3Hit := true
- evT3 := true
- if i_showT45 and cur.t3Hit and not cur.t4Hit and f_reached(cur.dir, cur.t4)
- cur.t4Hit := true
- evT4 := true
- if i_showT45 and cur.t4Hit and not cur.t5Hit and f_reached(cur.dir, cur.t5)
- cur.t5Hit := true
- evT5 := true
- bool finalTargetHit = i_showT45 ? cur.t5Hit : cur.t3Hit
- if finalTargetHit
- cur.closed := true
- else if cur.stopHit
- if cur.t1Hit or bar_index - cur.stopBar >= i_recoveryBars
- cur.closed := true
- if buySignal or sellSignal
- int d = buySignal ? 1 : -1
- float a = nz(atrTrade, barRange)
- float trg = d == 1 ? high : low
- if not na(cur) and not cur.closed
- cur.closed := true
- cur := TradeState.new(dir = d, sigBar = bar_index, trigger = trg, stop = trg - d * i_stopMult * a, t1 = trg + d * 1.0 * i_tMult * a, t2 = trg + d * 2.0 * i_tMult * a, t3 = trg + d * 3.0 * i_tMult * a, t4 = trg + d * 4.0 * i_tMult * a, t5 = trg + d * 5.0 * i_tMult * a)
- plot(i_showBand and bandDir == 1 ? bandTrail : na, "Gaussian Band Bull", i_colBandBull, i_bandWidth, plot.style_linebr)
- plot(i_showBand and bandDir == -1 ? bandTrail : na, "Gaussian Band Bear", i_colBandBear, i_bandWidth, plot.style_linebr)
- plot(i_showBaseline ? baseline : na, "Gaussian Baseline", i_colBaseline, 1)
- bool dotCaution = bullMarubozu or bearMarubozu or not probVarOk
- bool dotCautionDn = dotCaution and (bullMarubozu or (not bearMarubozu and finalProb >= 0.5))
- bool dotCautionUp = dotCaution and not dotCautionDn
- bool dotBull = not dotCaution and finalProb >= i_conf
- bool dotBear = not dotCaution and finalProb <= 1.0 - i_conf
- bool dotNeutUp = not dotCaution and not dotBull and not dotBear and finalProb >= 0.5
- bool dotNeutDn = not dotCaution and not dotBull and not dotBear and finalProb < 0.5
- plotshape(i_showDots and dotBull and not buySignal, "Bull Probability", shape.triangleup, location.belowbar, i_colDotBull, size = size.tiny)
- plotshape(i_showDots and dotBear and not sellSignal, "Bear Probability", shape.triangledown, location.abovebar, i_colDotBear, size = size.tiny)
- plotshape(i_showDots and dotNeutUp, "Neutral Up Probability", shape.triangleup, location.belowbar, i_colDotNeut, size = size.tiny)
- plotshape(i_showDots and dotNeutDn, "Neutral Down Probability", shape.triangledown, location.abovebar, i_colDotNeut, size = size.tiny)
- plotshape(i_showDots and dotCautionDn, "Caution Down", shape.triangledown, location.abovebar, i_colDotWarn, size = size.tiny)
- plotshape(i_showDots and dotCautionUp, "Caution Up", shape.triangleup, location.belowbar, i_colDotWarn, size = size.tiny)
- plotshape(i_showHistory and buySignal, "Buy Position", shape.labelup, location.belowbar, i_colLong, text = "Buy", textcolor = color.white, size = size.tiny)
- plotshape(i_showHistory and sellSignal, "Sell Position", shape.labeldown, location.abovebar, i_colShort, text = "Sell", textcolor = color.white, size = size.tiny)
- barcolor(i_showBarColor and buySignal ? i_colBuyBar : i_showBarColor and sellSignal ? i_colSellBar : na, title = "Signal Candles")
- var array<Zone> volZones = array.new<Zone>()
- var array<Zone> pivZones = array.new<Zone>()
- float volAvg = ta.sma(volSafe, 20)
- float srPH = ta.pivothigh(high, i_srPivotLen, i_srPivotLen)
- float srPL = ta.pivotlow(low, i_srPivotLen, i_srPivotLen)
- float pvPH = ta.pivothigh(high, i_pivLen, i_pivLen)
- float pvPL = ta.pivotlow(low, i_pivLen, i_pivLen)
- if i_showVolSR
- f_updateZones(volZones, i_srMaxBars)
- bool volFloat = volSafe[i_srPivotLen] >= nz(volAvg[i_srPivotLen]) * i_srVolMult
- float minH = nz(atr14[i_srPivotLen], atrSafe) * ZONE_MIN_ATR
- int leftX = bar_index - i_srPivotLen
- if not na(srPH) and volFloat
- float zt = srPH
- float zb = math.max(open[i_srPivotLen], close[i_srPivotLen])
- zb := zt - zb < minH ? zt - minH : zb
- f_addZone(volZones, Zone.new(box.new(leftX, zt, bar_index, zb, border_color = color(na), bgcolor = i_colVolZone), zt, zb, -1, leftX), i_maxZones)
- if not na(srPL) and volFloat
- float zb = srPL
- float zt = math.min(open[i_srPivotLen], close[i_srPivotLen])
- zt := zt - zb < minH ? zb + minH : zt
- f_addZone(volZones, Zone.new(box.new(leftX, zt, bar_index, zb, border_color = color(na), bgcolor = i_colVolZone), zt, zb, 1, leftX), i_maxZones)
- if i_showPivots
- f_updateZones(pivZones, i_pivMaxBars)
- float pivH = nz(atr14[i_pivLen], atrSafe) * PIV_ZONE_ATR
- int pivX = bar_index - i_pivLen
- if not na(pvPH)
- f_addZone(pivZones, Zone.new(box.new(pivX, pvPH, bar_index, pvPH - pivH, border_color = color(na), bgcolor = i_colPivZone), pvPH, pvPH - pivH, -1, pivX), i_maxZones)
- if not na(pvPL)
- f_addZone(pivZones, Zone.new(box.new(pivX, pvPL + pivH, bar_index, pvPL, border_color = color(na), bgcolor = i_colPivZone), pvPL + pivH, pvPL, 1, pivX), i_maxZones)
- var array<line> lvlLines = array.new<line>()
- var array<label> lvlLabels = array.new<label>()
- if barstate.islast
- for ln in lvlLines
- ln.delete()
- for lb in lvlLabels
- lb.delete()
- lvlLines.clear()
- lvlLabels.clear()
- if i_showLevels and not na(cur)
- string lblSize = f_textSize(i_textSize)
- bool isLong = cur.dir == 1
- int xr = bar_index + i_labelOffset
- int xl = math.max(cur.sigBar - 1, 0)
- float aGap = nz(atrTrade, atrSafe)
- color tgtTxt = isLong ? i_colBuyTgt : i_colSellTgt
- color tgtBg = isLong ? i_colBuyTgtBg : i_colSellTgtBg
- color tgtLn = isLong ? i_colBuyTgtLn : i_colSellTgtLn
- color entTxt = isLong ? i_colBuyEnt : i_colSellEnt
- color entBg = isLong ? i_colBuyEntBg : i_colSellEntBg
- color entLn = isLong ? i_colBuyEntLn : i_colSellEntLn
- color stpTxt = isLong ? i_colBuyStp : i_colSellStp
- color stpBg = isLong ? i_colBuyStpBg : i_colSellStpBg
- color stpLn = isLong ? i_colBuyStpLn : i_colSellStpLn
- if i_showT45
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.t5, tgtLn, line.style_dotted, 1, f_targetText(cur.dir, "T5", cur.t5, cur.t5Hit), tgtTxt, tgtBg, lblSize, false)
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.t4, tgtLn, line.style_dotted, 1, f_targetText(cur.dir, "T4", cur.t4, cur.t4Hit), tgtTxt, tgtBg, lblSize, false)
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.t3, tgtLn, line.style_dotted, 1, f_targetText(cur.dir, "T3", cur.t3, cur.t3Hit), tgtTxt, tgtBg, lblSize, false)
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.t2, tgtLn, line.style_dotted, 1, f_targetText(cur.dir, "T2", cur.t2, cur.t2Hit), tgtTxt, tgtBg, lblSize, false)
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.t1, tgtLn, line.style_dotted, 1, f_targetText(cur.dir, "T1", cur.t1, cur.t1Hit), tgtTxt, tgtBg, lblSize, false)
- string entryTxt = (isLong ? ARROW_UP + "Strength Above " : ARROW_DN + "Weakness Below ") + f_fmt(cur.trigger) + (cur.triggered ? ",\nTriggered" : "")
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.trigger, entLn, line.style_solid, 2, entryTxt, entTxt, entBg, lblSize, true)
- string stopTxt = WARN_ICON + "Stop at " + f_fmt(cur.stop) + (cur.stopHit ? ", " + HIT_PLAIN : "")
- f_drawLevel(lvlLines, lvlLabels, xl, xr, cur.stop, stpLn, line.style_dotted, 1, stopTxt, stpTxt, stpBg, lblSize, false)
- lvlLabels.push(label.new(xr, cur.stop - cur.dir * aGap * BRAND_GAP, BRAND_TXT, xloc = xloc.bar_index, color = i_colBrandBg, style = label.style_label_left, textcolor = i_colBrand, size = lblSize, textalign = text.align_center))
- string statLongRate = f_pct(stats.longWins, stats.longTotal)
- string statShortRate = f_pct(stats.shortWins, stats.shortTotal)
- var table statsTbl = table.new(f_tablePosition(i_tablePos), 4, 4, bgcolor = i_colTblBg, frame_color = i_colTblBorder, frame_width = 1, border_color = i_colTblBorder, border_width = 1)
- if barstate.islast
- table.clear(statsTbl, 0, 0, 3, 3)
- if i_showTable
- string ts = f_textSize(i_tableSize)
- color longBg = color.new(i_colLong, 88)
- color shortBg = color.new(i_colShort, 88)
- table.cell(statsTbl, 0, 0, VERSION_TXT, text_color = i_colTblText, text_size = ts, text_halign = text.align_left)
- table.cell(statsTbl, 1, 0, "Stats", text_color = i_colTblText, text_size = ts, bgcolor = color.new(i_colTblText, 92))
- table.cell(statsTbl, 2, 0, "Long", text_color = i_colLong, text_size = ts, bgcolor = longBg)
- table.cell(statsTbl, 3, 0, "Short", text_color = i_colShort, text_size = ts, bgcolor = shortBg)
- table.cell(statsTbl, 0, 1, "", text_size = ts)
- table.cell(statsTbl, 1, 1, "Total Trades", text_color = i_colTblText, text_size = ts, text_halign = text.align_right)
- table.cell(statsTbl, 2, 1, str.tostring(stats.longTotal), text_color = TXT_DARK, text_size = ts)
- table.cell(statsTbl, 3, 1, str.tostring(stats.shortTotal), text_color = TXT_DARK, text_size = ts)
- table.cell(statsTbl, 0, 2, RECOVERY_TXT, text_color = i_colTblText, text_size = ts)
- table.cell(statsTbl, 1, 2, "T1 Wins", text_color = i_colTblText, text_size = ts, text_halign = text.align_right)
- table.cell(statsTbl, 2, 2, str.tostring(stats.longWins), text_color = TXT_DARK, text_size = ts)
- table.cell(statsTbl, 3, 2, str.tostring(stats.shortWins), text_color = TXT_DARK, text_size = ts)
- table.cell(statsTbl, 0, 3, "", text_color = i_colTblText, text_size = ts)
- table.cell(statsTbl, 1, 3, "T1 Win %", text_color = i_colTblText, text_size = ts, text_halign = text.align_right)
- table.cell(statsTbl, 2, 3, statLongRate, text_color = TXT_DARK, text_size = ts)
- table.cell(statsTbl, 3, 3, statShortRate, text_color = TXT_DARK, text_size = ts)
- alertcondition(buySignal, "Ocs Ai Trader : Buy Signal", "Ocs Ai Trader : Buy Signal on {{ticker}} ({{interval}}) at {{close}}")
- alertcondition(sellSignal, "Ocs Ai Trader : Sell Signal", "Ocs Ai Trader : Sell Signal on {{ticker}} ({{interval}}) at {{close}}")
- alertcondition(buySignal or sellSignal, "Ocs Ai Trader : Any Signal", "Ocs Ai Trader : New Signal on {{ticker}} ({{interval}}) at {{close}}")
- alertcondition(evTriggered, "Ocs Ai Trader : Strength / Weakness Triggered", "Ocs Ai Trader : Entry Triggered on {{ticker}} ({{interval}}) at {{close}}")
- alertcondition(evT1, "Ocs Ai Trader : T1 Hit", "Ocs Ai Trader : T1 Hit on {{ticker}} ({{interval}})")
- alertcondition(evT2, "Ocs Ai Trader : T2 Hit", "Ocs Ai Trader : T2 Hit on {{ticker}} ({{interval}})")
- alertcondition(evT3, "Ocs Ai Trader : T3 Hit", "Ocs Ai Trader : T3 Hit on {{ticker}} ({{interval}})")
- alertcondition(evT4, "Ocs Ai Trader : T4 Hit", "Ocs Ai Trader : T4 Hit on {{ticker}} ({{interval}})")
- alertcondition(evT5, "Ocs Ai Trader : T5 Hit", "Ocs Ai Trader : T5 Hit on {{ticker}} ({{interval}})")
- alertcondition(evStop, "Ocs Ai Trader : Stop Hit", "Ocs Ai Trader : Stop Hit on {{ticker}} ({{interval}})")
- string passTxt = str.length(i_alertPass) > 0 ? " | Passcode: " + i_alertPass : ""
- string side = evDir == 1 ? "Long" : "Short"
- if buySignal or sellSignal
- string sigTxt = BRAND_TXT + " : " + (buySignal ? "BUY " : "SELL ") + syminfo.ticker + " (" + timeframe.period + ")" + (buySignal ? " | Strength Above " : " | Weakness Below ") + f_fmt(cur.trigger) + " | T1 " + f_fmt(cur.t1) + " | T2 " + f_fmt(cur.t2) + " | T3 " + f_fmt(cur.t3) + (i_showT45 ? " | T4 " + f_fmt(cur.t4) + " | T5 " + f_fmt(cur.t5) : "") + " | Stop " + f_fmt(cur.stop) + passTxt
- alert(sigTxt, alert.freq_once_per_bar_close)
- if evTriggered
- alert(BRAND_TXT + " : " + side + " Triggered on " + syminfo.ticker + " (" + timeframe.period + ") at " + f_fmt(close) + passTxt, alert.freq_once_per_bar)
- if evT1 or evT2 or evT3 or evT4 or evT5
- string tgt = evT5 ? "T5" : evT4 ? "T4" : evT3 ? "T3" : evT2 ? "T2" : "T1"
- alert(BRAND_TXT + " : " + side + " " + tgt + " Hit on " + syminfo.ticker + " (" + timeframe.period + ") at " + f_fmt(close) + passTxt, alert.freq_once_per_bar)
- if evStop
- alert(BRAND_TXT + " : " + side + " Stop Hit on " + syminfo.ticker + " (" + timeframe.period + ") at " + f_fmt(close) + passTxt, alert.freq_once_per_bar)
Add Comment
Please, Sign In to add comment