Strategy Trade Excursion Report - Demo

Ku_Hero · strategy · 77 行 · 点赞 2 · TradingView 原页

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

Pine Script

// 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
// Strategy Trade Excursion Report - Demo  v1.1
// Shows how to add the ExcursionReport library to a strategy. The trading rule is a plain moving-average
// cross with an ATR stop and a fixed-R target. 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.
strategy("Strategy Trade Excursion Report - Demo", shorttitle = "Excursion 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/ExcursionReport/1 as xr
 
// ===== 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")
atrLen  = input.int(14, "ATR length", minval = 1, group = "Example rule")
atrMult = input.float(1.5, "Stop = ATR x", minval = 0.1, step = 0.1, group = "Example rule")
rrMult  = input.float(2.0, "Target = stop distance x", minval = 0.1, step = 0.1, group = "Example rule")
 
// ===== Inputs: report =====
slLevels = input.string("0.5, 0.75, 1, 1.25", "Stop levels (1-5, comma separated)", group = "Report", tooltip = "For each level X: share of winning trades whose MAE reached X.")
tpLevels = input.string("0.5, 1, 1.5, 2, 3", "Target levels (1-5, comma separated)", group = "Report", tooltip = "For each level Y: share of all trades whose MFE reached Y.")
stopTim  = input.string("Signal bar", "Stop recorded from", options = ["Signal bar", "Fill bar"], group = "Report", tooltip = "Signal bar = the stop set when the order is sent (this demo). Fill bar = the stop computed after the fill, e.g. from strategy.position_avg_price.")
inclCom  = input.bool(false, "Include entry commission", group = "Report", tooltip = "Off = pure price moves. On = the Strategy Tester run-up / drawdown figures, which include the entry commission.")
lvUnit   = input.string("R", "Level unit", options = ["R", "Price"], group = "Report", tooltip = "R = multiples of the initial stop distance. Price = price distance.")
tPosStr  = input.string("Bottom right", "Table position", options = ["Top left", "Top right", "Bottom left", "Bottom right", "Middle right"], group = "Report", tooltip = "Top left can overlap the chart legend.")
tSizeStr = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = "Report")
showTbl  = input.bool(true, "Show report table", group = "Report")
 
tPos  = switch tPosStr
    "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)
atr    = ta.atr(atrLen)
 
// Stop distance (price, rounded to the tick) is fixed on the signal bar. The exits are given in ticks,
// so TradingView measures the stop and the target from the actual fill price.
var float stopDist = na
 
bool longSig  = ta.crossover(fastMA, slowMA) and not na(atr)
bool shortSig = ta.crossunder(fastMA, slowMA) and not na(atr)
if longSig or shortSig
    stopDist := math.max(math.round(atr * atrMult / syminfo.mintick), 1) * syminfo.mintick
if longSig
    strategy.entry("Long", strategy.long)
if shortSig
    strategy.entry("Short", strategy.short)
 
strategy.exit("Long exit", "Long", loss = stopDist / syminfo.mintick, profit = rrMult * stopDist / syminfo.mintick)
strategy.exit("Short exit", "Short", loss = stopDist / syminfo.mintick, profit = rrMult * stopDist / syminfo.mintick)
 
plot(fastMA, "Fast MA", color.new(color.orange, 0))
plot(slowMA, "Slow MA", color.new(color.blue, 0))
 
// ===== Report: one call, after the entries / exits =====
// stopDistance = the stop distance fixed on the signal bar (stopTiming = Signal bar); it becomes 1R.
xr.run(stopDistance = stopDist, stopTiming = stopTim, includeCommission = inclCom, slLevels = slLevels, tpLevels = tpLevels, levelUnit = lvUnit, tablePosition = tPos, textSize = tSize, showTable = showTbl)

← 返回列表