EMA Crossover with Historical Price Projection

garvitrakhecha · study · 132 行 · 点赞 265 · TradingView 原页

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

Pine Script

//@version=5
indicator('EMA Crossover with Historical Price Projection', 'EMA Crossover with Historical Price Projection', overlay = true, max_bars_back = 5000)
 
// Constants and inputs for historical projection
GREEN                   = #089981
RED                     = #F23645
GRAY                    = color.new(color.gray, 80)
 
DATA_GROUP              = 'Data Gathering'
TRANSFORMATION_GROUP    = 'Data transformation'
STYLE_GROUP             = 'Style'
 
anchoringPointTooltip   = 'Starting point to gather data from, up to 500 bars will be used.'
growthFactorTooltip     = 'Values from 0 to 100, represents how much drift will be used to calculate next price in the serie.'
areaGrowthFactorTooltip = 'Values from 0 to 100, controls the width of the area around the forecast as an increment/decrement of the growth factor.'
lineWidthTooltip        = 'Size and color of the projection line'
lineConnectorTooltip    = 'Display the line connecting the price located at the anchor point and the price of the most recent confirmed bar'
areaColorTooltip        = 'Display forecasting area'
 
anchoringPointInput     = input.time(timestamp('1 Jan 2099 00:00 +0100'), 'Anchor point', group = DATA_GROUP, tooltip = anchoringPointTooltip, confirm = true)
growthFactorInput       = input.int(0, 'Growth Factor', group = TRANSFORMATION_GROUP, tooltip = growthFactorTooltip, maxval = 100) / 100
areaGrowthFactorInput   = input.int(5, 'Area Width', group = TRANSFORMATION_GROUP, tooltip = areaGrowthFactorTooltip, minval = 0, maxval = 100) / 100
 
lineWidthInput          = input.int(1, 'Projection Line Width', group = STYLE_GROUP, tooltip = lineWidthTooltip, inline = '1', minval = 1)
bullishColorInput       = input.color(GREEN, '', group = STYLE_GROUP, tooltip = '', inline = '1')
bearishColorInput       = input.color(RED, '', group = STYLE_GROUP, tooltip = '', inline = '1')
 
lineConnectorInput      = input.bool(true, 'Execution Window Line', group = STYLE_GROUP, tooltip = lineConnectorTooltip)
 
showAreaInput           = input.bool(true, 'Show Area', group = STYLE_GROUP, tooltip = areaColorTooltip, inline = '2')
areaColorInput          = input.color(GRAY, '', group = STYLE_GROUP, tooltip = areaColorTooltip, inline = '2')
 
// Data structures and variables for projection
var negativeDrift       = array.new<float>()
var positiveDrift       = array.new<float>()
var points              = array.new<chart.point>(2)
var barDeltaPercents    = array.new<float>()
var float windowOpen    = 0
var float windowClose   = 0
var int firstBarIndex   = 0
 
// EMA calculations
ema9 = ta.ema(close, 9)
ema21 = ta.ema(close, 21)
 
// Determine crossover conditions based on EMAs
bullishCross = ta.crossover(ema9, ema21)
bearishCross = ta.crossunder(ema9, ema21)
 
// Plot EMA lines
plot(ema9, title="EMA 9", color=color.blue)
plot(ema21, title="EMA 21", color=color.red)
 
// Plot Buy/Sell signals with symbols
plotshape(series=bullishCross, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=bearishCross, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
 
// User-defined functions for projection
showInfoPanel(string message) =>
    var table t_able = table.new(position.top_right, 1, 1)
    table.cell(t_able, 0, 0, message, text_color = chart.fg_color, text_halign = text.align_right)
 
getBarDeltaPercent() =>
    array.push(barDeltaPercents, close / close[1])
 
flushPolylines() =>
    for eachPolyline in polyline.all
        polyline.delete(eachPolyline)
 
drawProjectedPrice(float drift, color polylineColor) =>
    anchorPrice     = close
    upperPrice      = close
    lowerPrice      = close
 
    chartPoints = array.new<chart.point>()
    upperPoints = array.new<chart.point>()
    lowerPoints = array.new<chart.point>()
 
    barIndex    = 0
 
    for eachDeltaPercent in barDeltaPercents
        barIndex    += 1
 
        anchorPrice *= (eachDeltaPercent + drift * growthFactorInput)
        array.push(chartPoints, chart.point.new(na, last_bar_index + barIndex, anchorPrice))
 
        if showAreaInput
            upperPrice *= (eachDeltaPercent + drift * (growthFactorInput + areaGrowthFactorInput))
            lowerPrice *= (eachDeltaPercent + drift * (growthFactorInput - areaGrowthFactorInput))
 
            array.push(upperPoints, chart.point.new(na, last_bar_index + barIndex, upperPrice))
            array.unshift(lowerPoints, chart.point.new(na, last_bar_index + barIndex, lowerPrice))
 
    polyline.new(chartPoints, line_color = polylineColor, line_width = lineWidthInput)
    
    if showAreaInput
        areaPoints = upperPoints.concat(lowerPoints)
        polyline.new(areaPoints, line_color = color(na), fill_color = areaColorInput, line_width = lineWidthInput)
 
// Mutable variables and execution for projection
totalProcessedBars  = array.size(barDeltaPercents)
timeWindow          = time >= anchoringPointInput
 
if timeWindow
    if not timeWindow[1]
        windowOpen := open
        firstBarIndex := bar_index
 
        if lineConnectorInput
            array.set(points, 0, chart.point.new(na, bar_index, windowOpen))
   
    if barstate.isconfirmed
        if firstBarIndex < last_bar_index - 5000
            showInfoPanel('Oops... Starting point is too far away from current bar. Select a closer starting point.')
        else
            if totalProcessedBars < 500
                windowClose := close
                getBarDeltaPercent()
                array.push(close < close[1] ? negativeDrift : positiveDrift, close / close[1] - 1)
 
                if lineConnectorInput
                    array.set(points, 1, chart.point.new(na, bar_index, windowClose))
 
            if barstate.islastconfirmedhistory or barstate.islast
                flushPolylines()
                polylineColor = windowClose >= windowOpen ? bullishColorInput : bearishColorInput
 
                if lineConnectorInput
                    polyline.new(points, line_color = polylineColor, line_width = 1)
 
                drift = windowClose >= windowOpen ? -1 * array.avg(negativeDrift) : -1 * array.avg(positiveDrift)
                drawProjectedPrice(drift, polylineColor)

← 返回列表