本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) K'u Hero
//@version=6
// Yearly Performance Table - Demo v1.01
// Shows how to add the YearlyPerformanceTable library to a strategy. The trading rule is a plain moving-average
// cross (always in the market, long or short). It is only an example to produce trades; it is not presented as
// a profitable system.
//
// Default properties (why):
// - initial_capital 10,000 USD (currency = USD): a typical small retail account, in USD on every symbol.
// - 10% of equity per trade, margin 100% (no leverage): position size stays small relative to the account.
// - commission 0.02% per order and slippage 2 ticks: rough costs so the results are not cost-free.
// - pyramiding 0: one position at a time.
// No time-of-day or session conditions are used. "Start date" / "End date" only limit the backtest period.
strategy("Yearly Performance Table - Demo", shorttitle = "Yearly Table Demo", overlay = true,
initial_capital = 10000, currency = currency.USD, default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
commission_type = strategy.commission.percent, commission_value = 0.02, slippage = 2,
margin_long = 100, margin_short = 100, pyramiding = 0)
import Ku_Hero/YearlyPerformanceTable/1 as ypt
// ===== Inputs: example trading rule =====
fastLen = input.int(20, "Fast MA length", minval = 1, group = "Example rule")
slowLen = input.int(50, "Slow MA length", minval = 2, group = "Example rule")
startDate = input.time(timestamp("1970-01-01T00:00:00"), "Start date", group = "Example rule", tooltip = "No new entries before this date. Years before it are listed with 0 trades.")
endDate = input.time(timestamp("2099-12-31T00:00:00"), "End date", group = "Example rule", tooltip = "From this date on: no new entries and an open position is closed.")
// ===== Inputs: table =====
showY = input.bool(true, "Show yearly table", group = "Table")
showM = input.bool(false, "Show monthly table", group = "Table")
modeStr = input.string("Currency", "Values", options = ["Currency", "Percent"], group = "Table", tooltip = "Currency = account currency. Percent = of the balance at the start of the year / month.")
yPosStr = input.string("Top right", "Yearly table position", options = ["Top left", "Top right", "Bottom left", "Bottom right", "Middle right"], group = "Table", tooltip = "Top left can overlap the chart legend.")
mPosStr = input.string("Bottom right", "Monthly table position", options = ["Top left", "Top right", "Bottom left", "Bottom right", "Middle right"], group = "Table")
tSizeStr = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = "Table")
nYears = input.int(20, "Years shown", minval = 1, maxval = 60, group = "Table", tooltip = "Most recent years. The All row always covers the whole history.")
tzStr = input.string("", "Time zone (blank = exchange)", group = "Table", tooltip = "Used to assign trades to years / months by exit time. IANA name (America/New_York, Asia/Tokyo) or UTC+9 / UTC-05:00. Other formats fall back to the exchange time zone.")
_pos(string s) =>
switch s
"Top left" => position.top_left
"Top right" => position.top_right
"Bottom left" => position.bottom_left
"Middle right" => position.middle_right
=> position.bottom_right
tSize = switch tSizeStr
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
=> size.small
// ===== Example rule =====
fastMA = ta.sma(close, fastLen)
slowMA = ta.sma(close, slowLen)
bool inRange = time >= startDate and time < endDate
bool crossUp = ta.crossover(fastMA, slowMA)
bool crossDn = ta.crossunder(fastMA, slowMA)
if inRange and crossUp
strategy.entry("Long", strategy.long)
if inRange and crossDn
strategy.entry("Short", strategy.short)
if time >= endDate and strategy.position_size != 0
strategy.close_all(comment = "End date")
plot(fastMA, "Fast MA", color.new(color.orange, 0))
plot(slowMA, "Slow MA", color.new(color.blue, 0))
// ===== Table: one call per bar, from the global scope, after the order calls =====
ypt.run(showYearly = showY, showMonthly = showM, valueMode = modeStr, yearlyPosition = _pos(yPosStr), monthlyPosition = _pos(mPosStr), textSize = tSize, maxYears = nYears, timezoneName = tzStr)