mudn8rRuler

mudn8r · study · 77 行 · 点赞 15 · TradingView 原页

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

Pine Script

//@version=6
indicator("mudn8rRuler", shorttitle = "mudn8rRuler", overlay = true, max_lines_count = 2, max_labels_count = 1)
// TradingView adaptation of mudn8rRuler. One ruler per indicator instance.
// Pine has no access to the Trading Panel/broker Order Qty field. Contracts
// is an explicit manual input, not a live broker/account/position quantity.
// Interactive input points can be moved; Pine line/label objects cannot be
// dragged like native drawing tools. Select the indicator to see its points.
// Intended for linear futures. No fees, slippage, or currency conversion.
float startPrice = input.price(0, "Start price", inline = "start", group = "Ruler anchors", confirm = true)
int startTime = input.time(0, "Time", inline = "start", group = "Ruler anchors", confirm = true)
float endPrice = input.price(0, "End price", inline = "end", group = "Ruler anchors", confirm = true)
int endTime = input.time(0, "Time", inline = "end", group = "Ruler anchors", confirm = true)
float textPrice = input.price(0, "Text box price", inline = "text", group = "Ruler anchors", confirm = true)
int textTime = input.time(0, "Time", inline = "text", group = "Ruler anchors", confirm = true)
int contracts = input.int(1, "Contracts", minval = 1, group = "Contract P/L", tooltip = "Set this manually to match your intended order quantity. Pine cannot read the trading panel's Order Qty.")
bool customPointValue = input.bool(false, "Override symbol point value", group = "Contract P/L")
float manualPointValue = input.float(50.0, "Value per 1.0 price point per contract", minval = 0.000001, group = "Contract P/L", tooltip = "Used only when override is enabled. Denominated in the chart symbol's currency.")
string yUnit = input.string("Points", "Y-value display", options = ["Points", "Ticks", "Percent", "Currency"], group = "Appearance")
color lineColor = input.color(color.gray, "Line color", group = "Appearance")
int lineWidth = input.int(1, "Line width", minval = 1, maxval = 4, group = "Appearance")
color textColor = input.color(color.white, "Text color", group = "Appearance")
color boxColor = input.color(color.new(color.black, 10), "Text box color", group = "Appearance")
string textSize = input.string("Normal", "Text size", options = ["Small", "Normal", "Large"], group = "Appearance")
signedNumber(float value) =>
    (value > 0 ? "+" : "") + str.tostring(value, "#,##0.00")
elapsedText(int milliseconds) =>
    int secondsTotal = int(math.abs(milliseconds) / 1000)
    int days = int(secondsTotal / 86400)
    int hours = int(secondsTotal % 86400 / 3600)
    int minutes = int(secondsTotal % 3600 / 60)
    int seconds = secondsTotal % 60
    (days > 0 ? str.tostring(days) + "d " : "") + str.tostring(hours, "00") + ":" + str.tostring(minutes, "00") + ":" + str.tostring(seconds, "00")
// Count actual chart bars, not elapsed time divided by timeframe (which would
// count weekends/session gaps). Only exact bar-opening timestamps are mapped.
// Missing/future/off-bar anchors show N/A rather than an estimated bar count.
var int startIndex = na
var int endIndex = na
if time == startTime and na(startIndex)
    startIndex := bar_index
if time == endTime and na(endIndex)
    endIndex := bar_index
float pointValue = customPointValue ? manualPointValue : syminfo.pointvalue
float priceDelta = math.round_to_mintick(endPrice - startPrice)
float tickDelta = priceDelta / syminfo.mintick
float total = priceDelta * pointValue * contracts
string currency = syminfo.currency
string longMoney = signedNumber(total) + " " + currency
string shortMoney = signedNumber(-total) + " " + currency
string barText = not na(startIndex) and not na(endIndex) ? str.tostring(endIndex - startIndex) : "N/A (anchors outside loaded bars/off-bar)"
string yText = switch yUnit
    "Ticks" => str.tostring(tickDelta, "#") + " ticks"
    "Percent" => startPrice != 0 ? signedNumber(priceDelta / startPrice * 100) + "%" : "N/A (start price is zero)"
    "Currency" => longMoney + " total"
    => str.tostring(priceDelta, format.mintick) + " points"
string labelSize = textSize == "Small" ? size.small : textSize == "Large" ? size.large : size.normal
var line measurement = na
var line connector = na
var label readout = na
if barstate.islast
    string info = "Bars: " + barText + "\nTime: " + elapsedText(endTime - startTime) + "\nY-value: " + yText
    info += "\nContracts: " + str.tostring(contracts) + " (manual)"
    info += "\nLong P/L: " + longMoney + "\nShort P/L: " + shortMoney
    info += "\nGross P/L: start -> end"
    if na(pointValue) or pointValue <= 0
        info += "\nSet a valid point-value override."
    // A constant number of objects, including during realtime updates.
    if na(measurement)
        measurement := line.new(startTime, startPrice, endTime, endPrice, xloc = xloc.bar_time, color = lineColor, width = lineWidth)
        connector := line.new(endTime, endPrice, textTime, textPrice, xloc = xloc.bar_time, color = lineColor, width = lineWidth, style = line.style_dotted)
        readout := label.new(textTime, textPrice, info, xloc = xloc.bar_time, yloc = yloc.price, style = label.style_label_lower_right, color = boxColor, textcolor = textColor, size = labelSize, textalign = text.align_left)
    else
        line.set_xy1(measurement, startTime, startPrice)
        line.set_xy2(measurement, endTime, endPrice)
        line.set_xy1(connector, endTime, endPrice)
        line.set_xy2(connector, textTime, textPrice)
        label.set_xy(readout, textTime, textPrice)
        label.set_text(readout, info)

← 返回列表