OPEN-SOURCE SCRIPT
MA均线全仓回测

//version=5
strategy("39日MA均线全仓交易策略 (2017-2025)",
overlay=true,
initial_capital=1000,
commission_type=strategy.commission.percent,
commission_value=0.1,
pyramiding=0)
// 策略参数
startDate = input.time(timestamp("2017-08-01T00:00:00"), title="回测开始日期")
endDate = input.time(timestamp("2025-11-11T23:59:59"), title="回测结束日期")
maLength = input.int(39, title="MA周期", minval=1)
useSMA = input.bool(true, title="使用SMA(简单移动平均)")
// 计算移动平均线
maValue = useSMA ? ta.sma(close, maLength) : ta.ema(close, maLength)
// 绘制MA线
plot(maValue, color=color.new(color.blue, 0), linewidth=2, title="39日MA")
// 确定是否在回测时间范围内
inDateRange = time >= startDate and time <= endDate
// 交易逻辑 - 全仓交易
if inDateRange
// 生成交易信号:价格上穿MA时全仓买入,下穿MA时全仓卖出
longCondition = ta.crossover(close, maValue)
shortCondition = ta.crossunder(close, maValue)
// 执行交易 - 全仓操作
if longCondition
strategy.entry("全仓买入", strategy.long, qty=strategy.equity/close)
if shortCondition
strategy.close("全仓买入", comment="全仓卖出")
// 在回测时间范围外显示背景色
bgcolor(inDateRange ? color.new(color.green, 95) : na, title="回测期间背景")
// 添加性能统计和资金曲线
var float totalReturn = 0.0
if barstate.islast and inDateRange
totalReturn := ((strategy.netprofit + 1000) / 1000 - 1) * 100
label.new(bar_index, high,
text="回测结果:\n初始资金: 1000 USDT\n最终权益: " + str.tostring(strategy.netprofit + 1000, "#.##") + " USDT\n总收益率: " + str.tostring(totalReturn, "#.##") + "%\n总交易次数: " + str.tostring(strategy.closedtrades) + "次",
style=label.style_label_down, color=color.new(color.blue, 80), textcolor=color.white, size=size.normal)
// 绘制权益曲线
plot(strategy.equity, title="资金曲线", color=color.new(color.purple, 0), linewidth=1, trackprice=true)
strategy("39日MA均线全仓交易策略 (2017-2025)",
overlay=true,
initial_capital=1000,
commission_type=strategy.commission.percent,
commission_value=0.1,
pyramiding=0)
// 策略参数
startDate = input.time(timestamp("2017-08-01T00:00:00"), title="回测开始日期")
endDate = input.time(timestamp("2025-11-11T23:59:59"), title="回测结束日期")
maLength = input.int(39, title="MA周期", minval=1)
useSMA = input.bool(true, title="使用SMA(简单移动平均)")
// 计算移动平均线
maValue = useSMA ? ta.sma(close, maLength) : ta.ema(close, maLength)
// 绘制MA线
plot(maValue, color=color.new(color.blue, 0), linewidth=2, title="39日MA")
// 确定是否在回测时间范围内
inDateRange = time >= startDate and time <= endDate
// 交易逻辑 - 全仓交易
if inDateRange
// 生成交易信号:价格上穿MA时全仓买入,下穿MA时全仓卖出
longCondition = ta.crossover(close, maValue)
shortCondition = ta.crossunder(close, maValue)
// 执行交易 - 全仓操作
if longCondition
strategy.entry("全仓买入", strategy.long, qty=strategy.equity/close)
if shortCondition
strategy.close("全仓买入", comment="全仓卖出")
// 在回测时间范围外显示背景色
bgcolor(inDateRange ? color.new(color.green, 95) : na, title="回测期间背景")
// 添加性能统计和资金曲线
var float totalReturn = 0.0
if barstate.islast and inDateRange
totalReturn := ((strategy.netprofit + 1000) / 1000 - 1) * 100
label.new(bar_index, high,
text="回测结果:\n初始资金: 1000 USDT\n最终权益: " + str.tostring(strategy.netprofit + 1000, "#.##") + " USDT\n总收益率: " + str.tostring(totalReturn, "#.##") + "%\n总交易次数: " + str.tostring(strategy.closedtrades) + "次",
style=label.style_label_down, color=color.new(color.blue, 80), textcolor=color.white, size=size.normal)
// 绘制权益曲线
plot(strategy.equity, title="资金曲线", color=color.new(color.purple, 0), linewidth=1, trackprice=true)
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội Quy.
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.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội Quy.
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.