本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("See Saw Trend Reversal",
overlay=true,
pyramiding=0,
process_orders_on_close=true,
initial_capital=2500,
currency=currency.USD,
default_qty_type=strategy.percent_of_equity,
default_qty_value=50,
commission_type=strategy.commission.percent,
commission_value=0.08,
slippage=2)
//==========================================
// INPUTS
// ==========================================
//len = input.int(17, "Volume SMA Length", minval=1)
mult = input.float(4.0, "High-Volume Multiplier", step=0.1)
//delay = input.int(17, "Entry Delay (Bars)", minval=1)
holdMins = input.int(240, "Trade Duration (Minutes)", minval=1)
// Dollar Stop Loss / Take Profit Settings
useStopLoss = input.bool(false, "Enable Stop Loss")
stopLossUSD = input.float(15.0, "Stop Loss ($)",
minval=0.01, step=10.0)
useTakeProfit = input.bool(false, "Enable Take Profit")
takeProfitUSD = input.float(25.0, "Take Profit ($)",
minval=0.01, step=10.0)
periodInterval = str.tonumber(timeframe.period)
n = 60.0 / periodInterval
bars_step_1 = int(math.round(n * math.sqrt(2)))
bars_step_2 = int(math.round(bars_step_1 * math.sqrt(2)))
// ==========================================
// DIRECTIONAL HIGH-VOLUME CONDITIONS
// ==========================================
volSma = ta.sma(volume, bars_step_2)
isHVBar = not na(volSma) and volume > volSma * mult
// Positive up-volume: high-volume bullish candle
positiveUpVolume = isHVBar and close > open
// Negative down-volume: high-volume bearish candle
negativeDownVolume = isHVBar and close < open
// ==========================================
// MOVING AVERAGES
// ==========================================
//fastMALen = input.int(17, "Fast MA Length", minval=1)
//slowMA1Len = input.int(24, "Slow MA 1 Length", minval=1)
slowMA2Len = input.int(200, "Slow MA 2 Length", minval=1)
fastMA = ta.sma(close, bars_step_1)
slowMA1 = ta.sma(close, bars_step_2)
slowMA2 = ta.sma(close, slowMA2Len)
// Plot Moving Averages
plot(fastMA, title="Fast MA",
color=color.yellow, linewidth=2)
plot(slowMA1, title="Slow MA 1",
color=color.orange, linewidth=2)
plot(slowMA2, title="Slow MA 2",
color=color.blue, linewidth=2)
// ==========================================
// PREVAILING TREND
// ==========================================
// Bullish trend: all three MAs are ordered upward
upTrend = fastMA > slowMA1 and slowMA1 > slowMA2
// Bearish trend: all three MAs are ordered downward
downTrend = fastMA < slowMA1 and slowMA1 < slowMA2
// Counter-trend reversal signals
longSignal = downTrend
shortSignal = upTrend
// ==========================================
// RECORD INITIAL HIGH-VOLUME TRIGGER
// ==========================================
var int triggerBar = na
if (positiveUpVolume or negativeDownVolume) and
strategy.position_size == 0 and
na(triggerBar)
triggerBar := bar_index
// ==========================================
// EXACT ENTRY BAR
// ==========================================
// Calculate the exact entry bar
targetEntryBar = not na(triggerBar) ?
triggerBar + bars_step_2 : na
// Entry is permitted ONLY on the target bar
entryReady = not na(targetEntryBar) and
bar_index == targetEntryBar and
strategy.position_size == 0
// ==========================================
// DIRECTIONALLY CONFIRMED REVERSAL ENTRIES
// ==========================================
buyConfirmed = longSignal and positiveUpVolume
sellConfirmed = shortSignal and negativeDownVolume
if entryReady
if longSignal
strategy.entry("Long", strategy.long)
else if shortSignal
strategy.entry("Short", strategy.short)
// Clear trigger whether a trade was entered or not
triggerBar := na
// ==========================================
// RECORD ACTUAL ENTRY TIME
// ==========================================
var int entryTime = na
newPosition = strategy.position_size != 0 and
strategy.position_size[1] == 0
if newPosition
entryTime := time
// ==========================================
// DOLLAR-BASED STOP LOSS / TAKE PROFIT
// ==========================================
if strategy.position_size > 0
longStop = strategy.position_avg_price -
stopLossUSD /
(syminfo.pointvalue *
math.abs(strategy.position_size))
longTarget = strategy.position_avg_price +
takeProfitUSD /
(syminfo.pointvalue *
math.abs(strategy.position_size))
strategy.exit("Long SL/TP",
from_entry="Long",
stop=useStopLoss ? longStop : na,
limit=useTakeProfit ? longTarget : na)
if strategy.position_size < 0
shortStop = strategy.position_avg_price +
stopLossUSD /
(syminfo.pointvalue *
math.abs(strategy.position_size))
shortTarget = strategy.position_avg_price -
takeProfitUSD /
(syminfo.pointvalue *
math.abs(strategy.position_size))
strategy.exit("Short SL/TP",
from_entry="Short",
stop=useStopLoss ? shortStop : na,
limit=useTakeProfit ? shortTarget : na)
// ==========================================
// EXIT AFTER SPECIFIED HOLDING DURATION
timeExpired = strategy.position_size != 0 and
not na(entryTime) and
time >= entryTime + holdMins * 60 * 1000
if timeExpired
strategy.close_all(comment="Time Exit")
entryTime := na
// Reset entry time after position closes
if strategy.position_size == 0 and
strategy.position_size[1] != 0
entryTime := na
// Clear pending triggers while a position is open
if strategy.position_size != 0
triggerBar := na
// ==========================================
// CONTINUOUS HIGH-VOLUME SAW DRAWINGS
// ==========================================
sawAnchor = (high + low) / 2
if isHVBar and barstate.isconfirmed
// Gray line: bars_step_1 forward
line.new(
x1=bar_index,
y1=sawAnchor,
x2=bar_index + bars_step_1,
y2=sawAnchor,
xloc=xloc.bar_index,
extend=extend.none,
color=color.rgb(170,169,173),
width=4)
// Purple line: bars_step_2 forward
line.new(
x1=bar_index,
y1=sawAnchor,
x2=bar_index + bars_step_2,
y2=sawAnchor,
xloc=xloc.bar_index,
extend=extend.none,
color=color.new(color.rgb(57,255,20), 50),
width=4)
// ==========================================
// STRATEGY PERFORMANCE METRICS TABLE
// ==========================================
showMetrics = input.bool(true, "Show Metrics Table")
var table metricsTable = table.new(
position.top_right,
2,
8,
border_width=1)
// Strategy metrics
netProfit = strategy.netprofit
winTrades = strategy.wintrades
lossTrades = strategy.losstrades
totalTrades = strategy.closedtrades
winRate = totalTrades > 0 ?
winTrades * 100.0 / totalTrades : 0.0
profitFactor = strategy.grossloss != 0 ?
strategy.grossprofit / math.abs(strategy.grossloss) : na
openProfit = strategy.openprofit
if barstate.islast and showMetrics
// Header
table.cell(metricsTable, 0, 0, "SEE-SAW METRICS",
bgcolor=color.blue, text_color=color.white)
table.cell(metricsTable, 1, 0, "VALUE",
bgcolor=color.blue, text_color=color.white)
// Net Profit
table.cell(metricsTable, 0, 1, "Net Profit")
table.cell(metricsTable, 1, 1,
"$" + str.tostring(netProfit, "#.##"),
text_color=netProfit >= 0 ? color.lime : color.red)
// Win Rate
table.cell(metricsTable, 0, 2, "Win Rate")
table.cell(metricsTable, 1, 2,
str.tostring(winRate, "#.##") + "%")
// Profit Factor
table.cell(metricsTable, 0, 3, "Profit Factor")
table.cell(metricsTable, 1, 3,
na(profitFactor) ? "N/A" :
str.tostring(profitFactor, "#.##"))
// Total Closed Trades
table.cell(metricsTable, 0, 4, "Total Trades")
table.cell(metricsTable, 1, 4,
str.tostring(totalTrades))
// Winning Trades
table.cell(metricsTable, 0, 5, "Winning Trades")
table.cell(metricsTable, 1, 5,
str.tostring(winTrades),
text_color=color.lime)
// Losing Trades
table.cell(metricsTable, 0, 6, "Losing Trades")
table.cell(metricsTable, 1, 6,
str.tostring(lossTrades),
text_color=color.red)
// Open Position P&L
table.cell(metricsTable, 0, 7, "Open P&L")
table.cell(metricsTable, 1, 7,
"$" + str.tostring(openProfit, "#.##"),
text_color=openProfit >= 0 ? color.lime : color.red)
if barstate.islast and not showMetrics
table.clear(metricsTable, 0, 0, 1, 7)
// ==========================================
// TRADE ENTRY LABELS
// ==========================================
if newPosition
label.new(
bar_index,
strategy.position_size > 0 ? low : high,
(strategy.position_size > 0 ? "LONG ENTRY" : "SHORT ENTRY") +
"\nPrice: " + str.tostring(strategy.position_avg_price, format.mintick) +
"\nBar: " + str.tostring(bar_index),
style=strategy.position_size > 0 ?
label.style_label_up : label.style_label_down,
color=strategy.position_size > 0 ? color.purple : color.black,
textcolor=color.white,
size=size.small)
// ==========================================
// TRADE EXIT LABELS
// ==========================================
tradeClosed = strategy.position_size == 0 and
strategy.position_size[1] != 0
if tradeClosed
label.new(
bar_index,
close,
"EXIT" +
"\nPrice: " + str.tostring(close, format.mintick) +
"\nP&L: $" + str.tostring(strategy.netprofit - strategy.netprofit[1], "#.##"),
style=label.style_label_down,
color=color.silver,
textcolor=color.white,
size=size.small)