Vix Fix Gold Trend Stats

edwinardyanto · strategy · 187 行 · 点赞 29 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// Vix Fix Gold Trend Stats
// Based on the open-source "CM_Williams_Vix_Fix Finds Market Bottoms" by ChrisMoody (Williams Vix Fix idea by Larry Williams):
// https://www.tradingview.com/script/og7JPrRA-CM-Williams-Vix-Fix-Finds-Market-Bottoms/
// The indicator math is ported to Pine v6 unchanged. Added: rule-based entries/exits, BUY / EXIT markers on the price chart,
// stop loss / take profit, a 200 SMA trend filter for the best row, and a statistics panel comparing both rules on the same chart.
// Signals on candle close, fills on the next candle open. Historical statistics only.
strategy("Vix Fix Gold Trend Stats", shorttitle = "VixFix Gold Stats", overlay = false, initial_capital = 10000,
     default_qty_type = strategy.percent_of_equity, default_qty_value = 10, commission_type = strategy.commission.percent,
     commission_value = 0.02, max_labels_count = 500)
 
// ---- original ChrisMoody inputs ----
pd = input.int(22, "LookBack Period Standard Deviation High")
bbl = input.int(20, "Bolinger Band Length")
mult = input.float(2.0, "Bollinger Band Standard Devaition Up", minval = 1, maxval = 5)
lb = input.int(50, "Look Back Period Percentile High")
ph = input.float(0.85, "Highest Percentile - 0.90=90%, 0.95=95%, 0.99=99%")
pl = input.float(1.01, "Lowest Percentile - 1.10=90%, 1.05=95%, 1.01=99%")
hp = input.bool(false, "Show High Range - Based on Percentile and LookBack Period?")
sd = input.bool(false, "Show Standard Deviation Line?")
// ---- added: rules and costs ----
trendLen = input.int(200, "Best row: buy only when close > SMA of this length")
slAtr = input.float(2.0, "Best row: stop loss (x ATR 14)", step = 0.5)
tpR = input.float(3.0, "Best row: take profit (x stop distance = R)", step = 0.5)
spreadPct = input.float(0.02, "Spread per side (%)", step = 0.01)
swapYr = input.float(5.0, "Swap / financing per year (%)", step = 0.5)
tStart = input.time(timestamp("2023-09-08T05:00:00+00:00"), "Start date")
tEnd = input.time(timestamp("2026-10-15T09:00:00+00:00"), "End date (last candle used)")
tSplit = input.time(timestamp("2025-03-27T19:00:00+00:00"), "Sample In / Sample Out split")
spread = spreadPct / 100
swapDay = swapYr / 100 / 365
 
// ---- Williams Vix Fix (ChrisMoody), ported to v6 ----
wvf = ((ta.highest(close, pd) - low) / ta.highest(close, pd)) * 100
sDev = mult * ta.stdev(wvf, bbl)
midLine = ta.sma(wvf, bbl)
lowerBand = midLine - sDev
upperBand = midLine + sDev
rangeHigh = ta.highest(wvf, lb) * ph
rangeLow = ta.lowest(wvf, lb) * pl
green = wvf >= upperBand or wvf >= rangeHigh
plot(hp ? rangeHigh : na, "Range High Percentile", color = color.orange, linewidth = 4)
plot(hp ? rangeLow : na, "Range Low Percentile", color = color.orange, linewidth = 4)
plot(wvf, "Williams Vix Fix", color = green ? color.lime : color.gray, style = plot.style_histogram, linewidth = 4)
plot(sd ? upperBand : na, "Upper Band", color = color.aqua, linewidth = 3)
 
firstGreen = green and not green[1]
trendUp = close > ta.sma(close, trendLen)
// row 0 = basic rule: buy on the first green bar (market bottom signal), exit when the bar turns gray again (buy only, as the indicator)
L0 = firstGreen
XL0 = not green
// row 1 = best: same entry only while close > SMA 200; exit only at stop loss / take profit
L1 = firstGreen
atrNow = ta.atr(14)
atrPrev = atrNow[1]
 
// ---- own trade log: fixed 1x position, % of capital per trade, spread both sides + swap per day held ----
var pos = array.from(0.0, 0.0)
var want = array.from(0.0, 0.0)
var ePx = array.from(0.0, 0.0)
var eT = array.from(0.0, 0.0)
var eSL = array.from(0.0, 0.0)
var eTP = array.from(0.0, 0.0)
var sN = array.from(0.0, 0.0)
var sW = array.from(0.0, 0.0)
var sSum = array.from(0.0, 0.0)
var sGW = array.from(0.0, 0.0)
var sGL = array.from(0.0, 0.0)
var sPk = array.from(0.0, 0.0)
var sDD = array.from(0.0, 0.0)
var sIn = array.from(0.0, 0.0)
var sOut = array.from(0.0, 0.0)
var nIn = array.from(0.0, 0.0)
var nOut = array.from(0.0, 0.0)
 
closeT(int r, float px) =>
    p = array.get(pos, r)
    days = (time - array.get(eT, r)) / 86400000.0
    ret = 100 * (p * (px / array.get(ePx, r) - 1) - 2 * spread - swapDay * days)
    array.set(sN, r, array.get(sN, r) + 1)
    array.set(sSum, r, array.get(sSum, r) + ret)
    if ret > 0
        array.set(sW, r, array.get(sW, r) + 1)
        array.set(sGW, r, array.get(sGW, r) + ret)
    else
        array.set(sGL, r, array.get(sGL, r) - ret)
    cum = array.get(sSum, r)
    array.set(sPk, r, math.max(array.get(sPk, r), cum))
    array.set(sDD, r, math.max(array.get(sDD, r), array.get(sPk, r) - cum))
    if array.get(eT, r) < tSplit
        array.set(sIn, r, array.get(sIn, r) + ret)
        array.set(nIn, r, array.get(nIn, r) + 1)
    else
        array.set(sOut, r, array.get(sOut, r) + ret)
        array.set(nOut, r, array.get(nOut, r) + 1)
    array.set(pos, r, 0.0)
 
bool sigExit = false
if time >= tStart and time <= tEnd
    // 1) execute the order decided on the previous candle, at this candle's open
    for r = 0 to 1
        w = array.get(want, r)
        p = array.get(pos, r)
        if w != p
            if p != 0
                closeT(r, open)
            if w != 0
                array.set(pos, r, w)
                array.set(ePx, r, open)
                array.set(eT, r, time)
                array.set(eSL, r, r == 1 ? open - w * slAtr * atrPrev : na)
                array.set(eTP, r, r == 1 ? open + w * tpR * slAtr * atrPrev : na)
    // 2) stop loss / take profit inside the candle (row 1 only); stop is checked first; a gap fills at the open
    p1 = array.get(pos, 1)
    s1 = array.get(eSL, 1)
    t1 = array.get(eTP, 1)
    if p1 > 0 and not na(s1)
        if open <= s1
            closeT(1, open)
            sigExit := true
        else if low <= s1
            closeT(1, s1)
            sigExit := true
        else if open >= t1
            closeT(1, open)
            sigExit := true
        else if high >= t1
            closeT(1, t1)
            sigExit := true
    array.set(want, 1, array.get(pos, 1))
 
// 3) decide on candle close
decide(int r, bool L, bool S, bool XL, bool XS, bool okL, bool okS) =>
    p = array.get(pos, r)
    w = p
    if p > 0 and XL
        w := 0.0
    if p < 0 and XS
        w := 0.0
    if w == 0 or p == 0
        if L and okL and p <= 0
            w := 1.0
        else if S and okS and p >= 0
            w := -1.0
    w
bool sigBuy = false
if time >= tStart and time < tEnd
    array.set(want, 0, decide(0, L0, false, XL0, false, true, false))
    pB = array.get(pos, 1)
    wB = decide(1, L1, false, false, false, trendUp, false)
    array.set(want, 1, wB)
    if wB != pB
        if wB == 1
            strategy.entry("Buy", strategy.long)
            strategy.exit("SL/TP", "Buy", stop = close - slAtr * atrNow, limit = close + tpR * slAtr * atrNow)
            sigBuy := true
        else
            strategy.close_all()
else if time >= tEnd
    array.set(want, 0, array.get(pos, 0))
    array.set(want, 1, array.get(pos, 1))
 
plotshape(sigBuy, "BUY", shape.labelup, location.belowbar, color.new(#19b39a, 0), text = "BUY", textcolor = color.white, size = size.small, force_overlay = true)
plotshape(sigExit, "EXIT", shape.labeldown, location.abovebar, color.new(#ff6b78, 0), text = "EXIT", textcolor = color.white, size = size.small, force_overlay = true)
 
fPct(float x) => (x >= 0 ? "+" : "") + str.tostring(x, "#0.0") + "%"
fPF(int r) => array.get(sGL, r) > 0 ? str.tostring(array.get(sGW, r) / array.get(sGL, r), "#.00") : "-"
fLine(int r) =>
    n = array.get(sN, r)
    str.tostring(n, "#") + " trades · WR " + (n > 0 ? str.tostring(100 * array.get(sW, r) / n, "#0.0") : "-") + "% · " + fPct(array.get(sSum, r)) + " · PF " + fPF(r)
var tb = table.new(position.top_right, 2, 7, bgcolor = color.new(#0b1730, 5), border_width = 1, border_color = color.new(color.white, 85), force_overlay = true)
if barstate.islast
    table.cell(tb, 0, 0, "Vix Fix Gold Stats", text_color = color.white, text_size = size.normal)
    table.cell(tb, 1, 0, "spread " + str.tostring(spreadPct) + "%/side · swap " + str.tostring(swapYr) + "%/yr", text_color = color.silver, text_size = size.small)
    table.cell(tb, 0, 1, "Default (buy first green bar, exit on gray)", text_color = color.silver, text_size = size.normal)
    table.cell(tb, 1, 1, fLine(0), text_color = #ff6b78, text_size = size.normal)
    table.cell(tb, 0, 2, "Best (first green + close > SMA" + str.tostring(trendLen) + ", SL " + str.tostring(slAtr) + " ATR, TP " + str.tostring(tpR) + "R)", text_color = color.silver, text_size = size.normal)
    table.cell(tb, 1, 2, fLine(1), text_color = #19e3c1, text_size = size.normal)
    table.cell(tb, 0, 3, "Default / Best max drawdown", text_color = color.silver, text_size = size.normal)
    table.cell(tb, 1, 3, str.tostring(array.get(sDD, 0), "#0.0") + "% / " + str.tostring(array.get(sDD, 1), "#0.0") + "%", text_color = #19e3c1, text_size = size.normal)
    table.cell(tb, 0, 4, "Best Sample In / Out", text_color = color.silver, text_size = size.normal)
    table.cell(tb, 1, 4, fPct(array.get(sIn, 1)) + " (" + str.tostring(array.get(nIn, 1), "#") + ") / " + fPct(array.get(sOut, 1)) + " (" + str.tostring(array.get(nOut, 1), "#") + ")", text_color = #19e3c1, text_size = size.normal)
    table.cell(tb, 0, 5, "Default Sample In / Out", text_color = color.silver, text_size = size.normal)
    table.cell(tb, 1, 5, fPct(array.get(sIn, 0)) + " (" + str.tostring(array.get(nIn, 0), "#") + ") / " + fPct(array.get(sOut, 0)) + " (" + str.tostring(array.get(nOut, 0), "#") + ")", text_color = #ff6b78, text_size = size.normal)
    table.cell(tb, 0, 6, "Indicator: CM_Williams_Vix_Fix by ChrisMoody · returns = sum of % per trade, fixed 1x", text_color = color.gray, text_size = size.tiny)
 
plot(bar_index, "Bar index", display = display.data_window)

← 返回列表