//@版本=5
策略("SPY 200SMA +4% 進入-3% 退出策略",
覆蓋=真,
default_qty_type=strategy.percent_of_equity,
預設數量值=100)
// === 輸入 ===
smaLength = input.int(200, title="SMA 週期", minval=1)
EntryThreshold = input.float(0.04, title="進入閾值 (%)", step=0.01)
exitThreshold = input.float(0.03, title="退出閾值 (%)", step=0.01)
startYear = input.int(1995, "開始年份")
startMonth = input.int(1, "開始月份")
startDay = input.int(1, "開始日")
// === 時間過濾器 ===
開始時間 = 時間戳(開始年份, 開始月份, 開始日期, 0, 0)
isAfterStart = 時間 >= 開始時間
// === 計算 ===
sma200 = ta.sma(關閉, smaLength)
上限閾值 = sma200 * (1 + 條目閾值)
下閾值 = sma200 * (1 - 退出閾值)
// === 策略邏輯 ===
EnterLong = 關閉 > upperThreshold
exitLong = 關閉 < lowerThreshold
如果是在開始之後
如果 EnterLong 且strategy.position_size == 0
strategy.entry("買入",strategy.long)
如果 exitLong 且 Strategy.position_size > 0
策略.close("買入")
// === 繪圖 ===
p_sma = 圖(sma200, title="200 SMA", color=color.rgb(255, 0, 242))
p_upper = 圖(upperThreshold, title="進入閾值 (+4%)", color=color.rgb(0, 200, 0))
p_lower = 圖(lowerThreshold, title="退出閾值 (-3%)", color=color.rgb(255, 0, 0))
fill(p_sma, p_upper, color=color.new(color.green, 80), title="入口區")
// === 進入/退出標籤 ===
prevOpentrades = nz(strategy.opentrades[1], 0)
newOpen = Strategy.opentrades > prevOpentrades
newClose = Strategy.opentrades < prevOpentrades
// 標籤的偏移量
買Y = 最低價 * 0.97
賣出Y = 最高價 * 1.03
如果新開
label.new(x=bar_index, y=buyY, text="BUY", xloc=xloc.bar_index, yloc=yloc.price, color=color.lime, style=label.style_label_up, textcolor=color.black, size=size.large)
如果是新的關閉
label.new(x=bar_index, y=sellY, text="SELL", xloc=xloc.bar_index, yloc=yloc.price, color=color.red, style=label.style_label_down, textcolor=color.white, size=size.large)
策略("SPY 200SMA +4% 進入-3% 退出策略",
覆蓋=真,
default_qty_type=strategy.percent_of_equity,
預設數量值=100)
// === 輸入 ===
smaLength = input.int(200, title="SMA 週期", minval=1)
EntryThreshold = input.float(0.04, title="進入閾值 (%)", step=0.01)
exitThreshold = input.float(0.03, title="退出閾值 (%)", step=0.01)
startYear = input.int(1995, "開始年份")
startMonth = input.int(1, "開始月份")
startDay = input.int(1, "開始日")
// === 時間過濾器 ===
開始時間 = 時間戳(開始年份, 開始月份, 開始日期, 0, 0)
isAfterStart = 時間 >= 開始時間
// === 計算 ===
sma200 = ta.sma(關閉, smaLength)
上限閾值 = sma200 * (1 + 條目閾值)
下閾值 = sma200 * (1 - 退出閾值)
// === 策略邏輯 ===
EnterLong = 關閉 > upperThreshold
exitLong = 關閉 < lowerThreshold
如果是在開始之後
如果 EnterLong 且strategy.position_size == 0
strategy.entry("買入",strategy.long)
如果 exitLong 且 Strategy.position_size > 0
策略.close("買入")
// === 繪圖 ===
p_sma = 圖(sma200, title="200 SMA", color=color.rgb(255, 0, 242))
p_upper = 圖(upperThreshold, title="進入閾值 (+4%)", color=color.rgb(0, 200, 0))
p_lower = 圖(lowerThreshold, title="退出閾值 (-3%)", color=color.rgb(255, 0, 0))
fill(p_sma, p_upper, color=color.new(color.green, 80), title="入口區")
// === 進入/退出標籤 ===
prevOpentrades = nz(strategy.opentrades[1], 0)
newOpen = Strategy.opentrades > prevOpentrades
newClose = Strategy.opentrades < prevOpentrades
// 標籤的偏移量
買Y = 最低價 * 0.97
賣出Y = 最高價 * 1.03
如果新開
label.new(x=bar_index, y=buyY, text="BUY", xloc=xloc.bar_index, yloc=yloc.price, color=color.lime, style=label.style_label_up, textcolor=color.black, size=size.large)
如果是新的關閉
label.new(x=bar_index, y=sellY, text="SELL", xloc=xloc.bar_index, yloc=yloc.price, color=color.red, style=label.style_label_down, textcolor=color.white, size=size.large)
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
