Elliott Wave Pro Engine by (JPT)

Jos-ProTrader · study · 328 行 · 点赞 374 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jos-ProTrader
 
//@version=6
indicator("Elliott Wave Pro Engine [JPT]", shorttitle="EW Pro [JPT]", overlay=true, max_lines_count=300, max_labels_count=300)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 1. INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
string G1 = "Wave Detection"
int pivotLen = input.int(11, "Pivot Length", minval=2, maxval=30, group=G1)
int lookback = input.int(600, "Analysis Lookback", minval=100, maxval=600, group=G1)
bool invert = input.bool(false, "Invert Pattern", group=G1)
bool inProgress = input.bool(true, "In Progress", group=G1)
string lengthType = input.string("Absolute", "Length Type", options=["Absolute", "Percent"], group=G1)
 
string G2 = "Pattern Display"
bool showImpulse = input.bool(true, "Show Impulse Waves", group=G2)
bool showZigzag = input.bool(true, "Show Corrective Waves", group=G2)
bool showSubwaves = input.bool(true, "Show Sub-wave Labels", group=G2)
bool showProjections = input.bool(true, "Show Fibonacci Projections", group=G2)
bool showZigzagLines = input.bool(true, "Show Wave Lines", group=G2)
int lineWidth = input.int(2, "Line Width", minval=1, maxval=4, group=G2)
 
string G3 = "Fibonacci Projections"
float retrace2Min = input.float(0.236, "Wave 2 Minimum Retracement", minval=0.0, maxval=1.0, step=0.001, group=G3)
float retrace2Max = input.float(0.999, "Wave 2 Maximum Retracement", minval=0.1, maxval=1.0, step=0.001, group=G3)
float wave3Extension = input.float(1.618, "Wave 3 Projection", minval=1.0, maxval=3.0, step=0.001, group=G3)
float wave5Extension = input.float(1.000, "Wave 5 Projection", minval=0.5, maxval=2.0, step=0.001, group=G3)
float waveCExtension = input.float(1.000, "Wave C Projection", minval=0.5, maxval=2.0, step=0.001, group=G3)
 
string G4 = "Colors"
color impulseColor = input.color(color.blue, "Impulse Color", group=G4)
color correctionColor = input.color(color.orange, "Correction Color", group=G4)
color projectionColor = input.color(color.purple, "Projection Color", group=G4)
color bullishColor = input.color(color.lime, "Bullish Color", group=G4)
color bearishColor = input.color(color.red, "Bearish Color", group=G4)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 2. SWING POINT STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// Type 1 = high, -1 = low.
 
var float[] swingPrice = array.new_float()
var int[] swingBar = array.new_int()
var int[] swingType = array.new_int()
 
var line[] patternLines = array.new_line()
var label[] patternLabels = array.new_label()
var line[] projectionLines = array.new_line()
var label[] projectionLabels = array.new_label()
 
float ph = ta.pivothigh(high, pivotLen, pivotLen)
float pl = ta.pivotlow(low, pivotLen, pivotLen)
 
f_addPivot(float price, int barIndex, int typ) =>
    bool changed = false
    int n = array.size(swingPrice)
 
    if n == 0
        array.push(swingPrice, price)
        array.push(swingBar, barIndex)
        array.push(swingType, typ)
        changed := true
    else
        int last = n - 1
        int previousType = array.get(swingType, last)
        float previousPrice = array.get(swingPrice, last)
 
        if typ == previousType
            bool moreExtreme = typ == 1 ? price > previousPrice : price < previousPrice
 
            if moreExtreme
                array.set(swingPrice, last, price)
                array.set(swingBar, last, barIndex)
                changed := true
        else
            array.push(swingPrice, price)
            array.push(swingBar, barIndex)
            array.push(swingType, typ)
            changed := true
 
    while array.size(swingPrice) > 150
        array.shift(swingPrice)
        array.shift(swingBar)
        array.shift(swingType)
 
    changed
 
bool pivotUpdated = false
 
if not na(ph)
    pivotUpdated := f_addPivot(ph, bar_index - pivotLen, 1) or pivotUpdated
 
if not na(pl)
    pivotUpdated := f_addPivot(pl, bar_index - pivotLen, -1) or pivotUpdated
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 3. HELPER FUNCTIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
f_waveLength(float startPrice, float endPrice) =>
    lengthType == "Absolute" ? math.abs(endPrice - startPrice) : math.abs(endPrice - startPrice) / math.max(math.abs(startPrice), syminfo.mintick) * 100.0
 
f_clearDrawings() =>
    while array.size(patternLines) > 0
        line.delete(array.pop(patternLines))
    while array.size(patternLabels) > 0
        label.delete(array.pop(patternLabels))
    while array.size(projectionLines) > 0
        line.delete(array.pop(projectionLines))
    while array.size(projectionLabels) > 0
        label.delete(array.pop(projectionLabels))
 
f_drawSegment(int x1, float y1, int x2, float y2, color col, int width, bool dashed) =>
    line ln = line.new(
         x1, y1, x2, y2,
         xloc=xloc.bar_index,
         color=col,
         width=width,
         style=dashed ? line.style_dashed : line.style_solid
     )
    array.push(patternLines, ln)
 
f_drawWaveLabel(int x, float y, string txt, bool isHigh, color col) =>
    label lb = label.new(
         x, y, txt,
         xloc=xloc.bar_index,
         style=isHigh ? label.style_label_down : label.style_label_up,
         color=color.new(color.white, 100),
         textcolor=col,
         size=size.normal
     )
    array.push(patternLabels, lb)
 
f_drawProjection(int x1, float y1, int x2, float y2, string txt, color col) =>
    line ln = line.new(
         x1, y1, x2, y2,
         xloc=xloc.bar_index,
         color=col,
         width=2,
         style=line.style_dashed
     )
    array.push(projectionLines, ln)
 
    label lb = label.new(
         x2, y2, txt,
         xloc=xloc.bar_index,
         style=label.style_label_left,
         color=color.new(col, 75),
         textcolor=col,
         size=size.small
     )
    array.push(projectionLabels, lb)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 4. PATTERN RECOGNITION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
bool impulseFound = false
bool correctionFound = false
 
var string lastPattern = "Waiting for pivots"
var float projectedPrice = na
 
if pivotUpdated and barstate.isconfirmed
    int n = array.size(swingPrice)
 
    if n >= 5
        f_clearDrawings()
 
        int first = math.max(0, n - 30)
 
        // Analyze recent swing sequences, starting with the
        // latest five alternating pivots.
        int s = n - 5
 
        float p0 = array.get(swingPrice, s)
        float p1 = array.get(swingPrice, s + 1)
        float p2 = array.get(swingPrice, s + 2)
        float p3 = array.get(swingPrice, s + 3)
        float p4 = array.get(swingPrice, s + 4)
 
        int b0 = array.get(swingBar, s)
        int b1 = array.get(swingBar, s + 1)
        int b2 = array.get(swingBar, s + 2)
        int b3 = array.get(swingBar, s + 3)
        int b4 = array.get(swingBar, s + 4)
 
        int t0 = array.get(swingType, s)
        int t1 = array.get(swingType, s + 1)
        int t2 = array.get(swingType, s + 2)
        int t3 = array.get(swingType, s + 3)
        int t4 = array.get(swingType, s + 4)
 
        bool alternating = t0 != t1 and t1 != t2 and t2 != t3 and t3 != t4
 
        // A normal bullish impulse starts at a low and ends at a high.
        // A bearish impulse starts at a high and ends at a low.
        bool bullishImpulse = t0 == -1 and t1 == 1 and t2 == -1 and t3 == 1 and t4 == -1
        bool bearishImpulse = t0 == 1 and t1 == -1 and t2 == 1 and t3 == -1 and t4 == 1
 
        // Alternating pivots alone are not sufficient to establish an
        // Elliott impulse. Six points are required for five wave legs.
        // These checks are preliminary and are refined below.
 
        float w1 = f_waveLength(p0, p1)
        float w2 = f_waveLength(p1, p2)
        float w3 = f_waveLength(p2, p3)
        float w4 = f_waveLength(p3, p4)
 
        bool validBullish = false
        bool validBearish = false
 
        if alternating and (bullishImpulse or bearishImpulse)
            // This initial sequence contains only four legs.
            // Wave 5 is not complete yet, so validate the available
            // structure and draw it only in In Progress mode.
            if bullishImpulse
                bool wave2Valid = p2 > p0 and w2 < w1 * retrace2Max and w2 >= w1 * retrace2Min
                bool wave3Valid = p3 > p1
                bool wave4Valid = p4 > p1
                validBullish := wave2Valid and wave3Valid and wave4Valid
 
            if bearishImpulse
                bool wave2Valid = p2 < p0 and w2 < w1 * retrace2Max and w2 >= w1 * retrace2Min
                bool wave3Valid = p3 < p1
                bool wave4Valid = p4 < p1
                validBearish := wave2Valid and wave3Valid and wave4Valid
 
        if showImpulse and (validBullish or validBearish)
            color c = validBullish ? impulseColor : bearishColor
 
            f_drawSegment(b0, p0, b1, p1, c, lineWidth, false)
            f_drawSegment(b1, p1, b2, p2, c, lineWidth, false)
            f_drawSegment(b2, p2, b3, p3, c, lineWidth, false)
            f_drawSegment(b3, p3, b4, p4, c, lineWidth, false)
 
            f_drawWaveLabel(b1, p1, "(1)", t1 == 1, c)
            f_drawWaveLabel(b2, p2, "(2)", t2 == 1, c)
            f_drawWaveLabel(b3, p3, "(3)", t3 == 1, c)
            f_drawWaveLabel(b4, p4, "(4)", t4 == 1, c)
 
            if showProjections and inProgress
                float projected5 = validBullish ? p4 + w1 * wave5Extension : p4 - w1 * wave5Extension
                int projectionBar = b4 + math.max(5, pivotLen * 3)
                f_drawProjection(b4, p4, projectionBar, projected5, "(5) Projection", projectionColor)
                projectedPrice := projected5
 
            impulseFound := true
            lastPattern := validBullish ? "Bullish impulse developing" : "Bearish impulse developing"
 
        // Search for a corrective A-B-C zigzag.
        // Three consecutive legs require four pivots.
        if n >= 4 and showZigzag
            int z = n - 4
 
            float za = array.get(swingPrice, z)
            float zb = array.get(swingPrice, z + 1)
            float zc = array.get(swingPrice, z + 2)
            float zd = array.get(swingPrice, z + 3)
 
            int zba = array.get(swingBar, z)
            int zbb = array.get(swingBar, z + 1)
            int zbc = array.get(swingBar, z + 2)
            int zbd = array.get(swingBar, z + 3)
 
            int zta = array.get(swingType, z)
            int ztb = array.get(swingType, z + 1)
            int ztc = array.get(swingType, z + 2)
            int ztd = array.get(swingType, z + 3)
 
            bool zAlt = zta != ztb and ztb != ztc and ztc != ztd
 
            float lenA = f_waveLength(za, zb)
            float lenB = f_waveLength(zb, zc)
            float lenC = f_waveLength(zc, zd)
 
            bool bullZigzag = zAlt and zta == 1 and ztb == -1 and ztc == 1 and ztd == -1 and lenB < lenA and zd < zb
            bool bearZigzag = zAlt and zta == -1 and ztb == 1 and ztc == -1 and ztd == 1 and lenB < lenA and zd > zb
 
            if bullZigzag or bearZigzag
                color zcol = bullZigzag ? correctionColor : bearishColor
 
                f_drawSegment(zba, za, zbb, zb, zcol, lineWidth, false)
                f_drawSegment(zbb, zb, zbc, zc, zcol, lineWidth, false)
                f_drawSegment(zbc, zc, zbd, zd, zcol, lineWidth, false)
 
                f_drawWaveLabel(zbb, zb, "(a)", ztb == 1, zcol)
                f_drawWaveLabel(zbc, zc, "(b)", ztc == 1, zcol)
                f_drawWaveLabel(zbd, zd, "(c)", ztd == 1, zcol)
 
                if showProjections and inProgress
                    float projectedC = bullZigzag ? zc - lenA * waveCExtension : zc + lenA * waveCExtension
                    int projectionBar = zbd + math.max(5, pivotLen * 3)
                    f_drawProjection(zbd, zd, projectionBar, projectedC, "C Projection", projectionColor)
                    projectedPrice := projectedC
 
                correctionFound := true
                lastPattern := bullZigzag ? "Bullish corrective zigzag" : "Bearish corrective zigzag"
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 5. STATUS TABLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var table status = table.new(position.top_right, 2, 4, border_width=1)
 
if barstate.islast
    table.cell(status, 0, 0, "ELLIOTT WAVE PRO", text_color=color.white, bgcolor=color.rgb(25, 45, 85))
    table.cell(status, 1, 0, "JPT", text_color=color.white, bgcolor=color.rgb(25, 45, 85))
 
    table.cell(status, 0, 1, "Pattern", text_color=color.white, bgcolor=color.new(color.gray, 70))
    table.cell(status, 1, 1, lastPattern, text_color=color.white, bgcolor=color.new(color.gray, 70))
 
    table.cell(status, 0, 2, "Swing Points", text_color=color.white, bgcolor=color.new(color.gray, 70))
    table.cell(status, 1, 2, str.tostring(array.size(swingPrice)), text_color=color.white, bgcolor=color.new(color.gray, 70))
 
    table.cell(status, 0, 3, "Projection", text_color=color.white, bgcolor=color.new(color.gray, 70))
    table.cell(status, 1, 3, na(projectedPrice) ? "N/A" : str.tostring(projectedPrice, format.mintick), text_color=color.white, bgcolor=color.new(color.gray, 70))
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 6. ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(impulseFound, title="New Impulse Pattern", message="Potential Elliott impulse pattern detected.")
alertcondition(correctionFound, title="New Corrective Pattern", message="Potential Elliott corrective zigzag detected.")

← 返回列表