CEO

dineshu07 · study · 240 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("Camarilla Pivots, EMAs and Earnings", shorttitle="Camarilla", overlay=true, max_lines_count=500, dynamic_requests=false)
// Camarilla settings
showMonthly = input.bool(true, "Show Monthly Camarilla")
showYearly = input.bool(true, "Show Yearly Camarilla")
 
// EMA settings
shortEMA = input.int(10, "Short EMA", minval=1)
mediumEMA = input.int(20, "Medium EMA", minval=1)
longEMA = input.int(50, "Long EMA", minval=1)
 
// Earnings table settings
showEarningsTable = input.bool(true, "Show Quarterly Earnings Table")
earningsRows = input.int(5, "Quarterly Earnings Rows", minval=1, maxval=12)
earningsTablePosition = input.string("Top Right", "Earnings Table Position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"])
 
tablePosition = switch earningsTablePosition
    "Top Left"      => position.top_left
    "Top Center"    => position.top_center
    "Top Right"     => position.top_right
    "Middle Left"   => position.middle_left
    "Middle Center" => position.middle_center
    "Middle Right"  => position.middle_right
    "Bottom Left"   => position.bottom_left
    "Bottom Center" => position.bottom_center
    "Bottom Right"  => position.bottom_right
 
// Camarilla H3 and H4 from the previous completed period
camarilla(float prevHigh, float prevLow, float prevClose) =>
    priceRange = prevHigh - prevLow
    h3 = prevClose + priceRange * (1.1 / 4)
    h4 = prevClose + priceRange * (1.1 / 2)
    [h3, h4]
 
[mHigh, mLow, mClose] = request.security(syminfo.tickerid, "M", [high[1], low[1], close[1]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
[yHigh, yLow, yClose] = request.security(syminfo.tickerid, "12M", [high[1], low[1], close[1]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
 
[mH3, mH4] = camarilla(mHigh, mLow, mClose)
[yH3, yH4] = camarilla(yHigh, yLow, yClose)
 
// Alternate plot series to break lines at month and year boundaries.
monthlyEven = ta.cum(nz(ta.change(time("M"))) != 0 ? 1 : 0) % 2 == 0
yearlyEven = ta.cum(nz(ta.change(time("12M"))) != 0 ? 1 : 0) % 2 == 0
 
plot(showMonthly and monthlyEven ? mH3 : na, "Monthly H3", color=color.red, linewidth=1, style=plot.style_linebr)
plot(showMonthly and not monthlyEven ? mH3 : na, "Monthly H3", color=color.red, linewidth=1, style=plot.style_linebr)
plot(showMonthly and monthlyEven ? mH4 : na, "Monthly H4", color=color.green, linewidth=1, style=plot.style_linebr)
plot(showMonthly and not monthlyEven ? mH4 : na, "Monthly H4", color=color.green, linewidth=1, style=plot.style_linebr)
 
plot(showYearly and yearlyEven ? yH3 : na, "Yearly H3", color=color.red, linewidth=2, style=plot.style_linebr)
plot(showYearly and not yearlyEven ? yH3 : na, "Yearly H3", color=color.red, linewidth=2, style=plot.style_linebr)
plot(showYearly and yearlyEven ? yH4 : na, "Yearly H4", color=color.green, linewidth=2, style=plot.style_linebr)
plot(showYearly and not yearlyEven ? yH4 : na, "Yearly H4", color=color.green, linewidth=2, style=plot.style_linebr)
 
// Moving averages
plot(ta.ema(close, shortEMA), "Short EMA", color=color.lime, linewidth=2)
plot(ta.ema(close, mediumEMA), "Medium EMA", color=color.orange, linewidth=2)
plot(ta.ema(close, longEMA), "Long EMA", color=color.red, linewidth=2)
 
// Company and quarterly financial data
quarterFinancial(simple string metric) =>
    request.financial(syminfo.tickerid, metric, "FQ", gaps=barmerge.gaps_on, ignore_invalid_symbol=true)
 
quarterEps = quarterFinancial("EARNINGS_PER_SHARE_BASIC")
quarterSales = quarterFinancial("TOTAL_REVENUE")
quarterEpsGrowth = quarterFinancial("EARNINGS_PER_SHARE_BASIC_ONE_YEAR_GROWTH")
quarterSalesGrowth = quarterFinancial("REVENUE_ONE_YEAR_GROWTH")
quarterRoe = quarterFinancial("RETURN_ON_EQUITY")
quarterOpm = quarterFinancial("OPERATING_MARGIN")
epsTtm = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE_BASIC", "TTM", ignore_invalid_symbol=true)
 
marketCap = close * syminfo.shares_outstanding_total
priceEarnings = epsTtm > 0 ? close / epsTtm : na
floatPercent = syminfo.shares_outstanding_total > 0 ? 100 * syminfo.shares_outstanding_float / syminfo.shares_outstanding_total : na
 
type QuarterRow
    string period
    float eps
    float epsGrowth
    float sales
    float salesGrowth
    float roe
    float opm
 
var quarters = array.new<QuarterRow>()
 
if showEarningsTable and (not na(quarterEps) or not na(quarterSales))
    // Infer the fiscal quarter-end month from the financial update bar.
    priorMonthEnd = timestamp(year, month, 1, 0, 0) - 1
    period = str.format_time(priorMonthEnd, "MMM-yy", syminfo.timezone)
    array.unshift(quarters, QuarterRow.new(period, quarterEps, quarterEpsGrowth, quarterSales, quarterSalesGrowth, quarterRoe, quarterOpm))
    if array.size(quarters) > earningsRows
        array.pop(quarters)
 
// Daily trading statistics
dailyStats() =>
    turnover = close * volume
    adr = ta.sma(low > 0 ? 100 * (high / low - 1) : na, 20)
    averageTurnover = ta.sma(turnover, 50)
    averageVolume = ta.sma(volume[1], 50)
    relativeVolume = averageVolume > 0 ? volume / averageVolume : na
    [ta.highest(high, 252), ta.lowest(low, 252), adr, turnover, averageTurnover, relativeVolume]
 
[high52, low52, adr20, turnover1d, turnover50, rvol50] = request.security(syminfo.tickerid, "D", dailyStats())
 
belowHigh = high52 > 0 ? 100 * (high52 - close) / high52 : na
aboveLow = low52 > 0 ? 100 * (close - low52) / low52 : na
 
// Table formatting
formatNumber(float value, string pattern) =>
    na(value) ? "—" : str.tostring(value, pattern)
 
formatPercent(float value, string pattern) =>
    na(value) ? "—" : str.tostring(value, pattern) + "%"
 
isInr = syminfo.currency == "INR"
moneyDivisor = isInr ? 10000000.0 : 1000000.0
moneyUnit = isInr ? "₹ Cr" : syminfo.currency + " M"
 
formatMarketCap(float value) =>
    scaled = value / moneyDivisor
    na(scaled) ? "—" : scaled >= 1000 ? str.tostring(scaled / 1000, "0.0") + "K " + moneyUnit : str.tostring(scaled, "0.0") + " " + moneyUnit
 
textColor = color.rgb(52, 59, 72)
positiveColor = color.rgb(42, 152, 68)
negativeColor = color.rgb(218, 49, 63)
gridColor = color.rgb(226, 224, 224)
headerBg = color.rgb(249, 249, 249)
bodyBg = color.white
 
metricColor(float value) =>
    na(value) or value == 0 ? textColor : value > 0 ? positiveColor : negativeColor
 
var earningsTable = table.new(tablePosition, 7, earningsRows + 6, border_color=gridColor, border_width=1, frame_color=gridColor, frame_width=1)
footer = earningsRows + 3
 
if showEarningsTable and barstate.isfirst
    table.merge_cells(earningsTable, 0, 0, 6, 0)
    table.merge_cells(earningsTable, 4, 1, 5, 1)
    for row = footer to footer + 2
        table.merge_cells(earningsTable, 0, row, 1, row)
        table.merge_cells(earningsTable, 3, row, 4, row)
        table.merge_cells(earningsTable, 5, row, 6, row)
 
putCell(int column, int row, string value, color ink, color background) =>
    table.cell(earningsTable, column, row, value, text_color=ink, bgcolor=background, text_size=size.small)
 
if showEarningsTable and barstate.islast
    sector = na(syminfo.sector) or syminfo.sector == "" ? syminfo.ticker : syminfo.sector
    industry = na(syminfo.industry) or syminfo.industry == "" ? "" : " ↔ " + syminfo.industry
    putCell(0, 0, sector + industry, textColor, headerBg)
 
    putCell(0, 1, "MCap", textColor, headerBg)
    putCell(1, 1, formatMarketCap(marketCap), textColor, bodyBg)
    putCell(2, 1, "P/E", textColor, headerBg)
    putCell(3, 1, formatNumber(priceEarnings, "0.0"), textColor, bodyBg)
    putCell(4, 1, "FFloat%", textColor, headerBg)
    putCell(6, 1, formatPercent(floatPercent, "0.0"), textColor, bodyBg)
 
    putCell(0, 2, "Qtr", textColor, headerBg)
    putCell(1, 2, "EPS", textColor, headerBg)
    putCell(2, 2, "YoY%", textColor, headerBg)
    putCell(3, 2, "Sales", textColor, headerBg)
    putCell(4, 2, "YoY%", textColor, headerBg)
    putCell(5, 2, "ROE", textColor, headerBg)
    putCell(6, 2, "OPM", textColor, headerBg)
 
    for index = 0 to earningsRows - 1
        row = index + 3
        if index < array.size(quarters)
            q = array.get(quarters, index)
            putCell(0, row, q.period, textColor, bodyBg)
            putCell(1, row, formatNumber(q.eps, "0.0#"), metricColor(q.eps), bodyBg)
            putCell(2, row, formatPercent(q.epsGrowth, "0"), metricColor(q.epsGrowth), bodyBg)
            putCell(3, row, formatNumber(q.sales / moneyDivisor, "0.#"), textColor, bodyBg)
            putCell(4, row, formatPercent(q.salesGrowth, "0"), metricColor(q.salesGrowth), bodyBg)
            putCell(5, row, formatPercent(q.roe, "0"), textColor, bodyBg)
            putCell(6, row, formatPercent(q.opm, "0"), textColor, bodyBg)
        else
            for column = 0 to 6
                putCell(column, row, "—", textColor, bodyBg)
 
    putCell(0, footer, "▼ 52W HIGH", textColor, headerBg)
    putCell(2, footer, formatPercent(belowHigh, "0.00"), textColor, bodyBg)
    putCell(3, footer, "TOver " + moneyUnit + " | 1D", textColor, headerBg)
    putCell(5, footer, formatNumber(turnover1d / moneyDivisor, "0.00"), textColor, bodyBg)
 
    putCell(0, footer + 1, "▲ 52W LOW", textColor, headerBg)
    putCell(2, footer + 1, formatPercent(aboveLow, "0.00"), textColor, bodyBg)
    putCell(3, footer + 1, "Avg TOvr | 50", textColor, headerBg)
    putCell(5, footer + 1, formatNumber(turnover50 / moneyDivisor, "0.00"), textColor, bodyBg)
 
    putCell(0, footer + 2, "ADR | 20", textColor, headerBg)
    putCell(2, footer + 2, formatPercent(adr20, "0.00"), textColor, bodyBg)
    putCell(3, footer + 2, "RVOL(x) | 50", textColor, headerBg)
    putCell(5, footer + 2, formatNumber(rvol50, "0.00"), textColor, bodyBg)
    // Opening Range Breakout
// Optional: add max_lines_count=500 to your existing indicator() declaration
// to display more historical ORB sessions.
 
// Opening Range Breakout
orbSelection = input.string("30 minutes", "ORB Period",
     options=["Off", "15 minutes", "30 minutes"],
     group="Opening Range Breakout")
 
orbTimeframe = orbSelection == "15 minutes" ? "15" : "30"
orbVisible = orbSelection != "Off" and timeframe.isintraday and
     timeframe.in_seconds() < timeframe.in_seconds(orbTimeframe)
 
orbTicker = ticker.new(syminfo.prefix, syminfo.ticker, session.regular)
 
orbOpeningRange() =>
    firstBar = session.isfirstbar_regular
    openingHigh = ta.valuewhen(firstBar, high, 0)
    openingLow = ta.valuewhen(firstBar, low, 0)
    openingTime = ta.valuewhen(firstBar, time, 0)
    openingDay = ta.valuewhen(firstBar, time_tradingday, 0)
    [openingHigh[1], openingLow[1], openingTime[1], openingDay[1]]
 
[orbHigh, orbLow, orbStart, orbTradingDay] = request.security(
     orbTicker, orbTimeframe, orbOpeningRange(),
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
 
orbReady = orbVisible and session.ismarket and
     not na(orbStart) and orbTradingDay == time_tradingday
 
var line orbHighLine = na
var line orbLowLine = na
var int orbDrawnDay = na
 
if orbReady
    if na(orbDrawnDay) or orbDrawnDay != orbTradingDay
        orbHighLine := line.new(orbStart, orbHigh, time_close, orbHigh,
             xloc=xloc.bar_time, extend=extend.none, color=color.blue, width=2)
        orbLowLine := line.new(orbStart, orbLow, time_close, orbLow,
             xloc=xloc.bar_time, extend=extend.none, color=color.black, width=2)
        orbDrawnDay := orbTradingDay
    else
        line.set_x2(orbHighLine, time_close)
        line.set_x2(orbLowLine, time_close)

← 返回列表