这是一个基于MACD指标的量化交易策略,通过设定特定的时间范围来执行交易。策略核心是利用快速和慢速移动平均线计算MACD值,并与信号线的交叉来确定买卖时机。策略还包含了止损和止盈机制,以控制风险和锁定利润。
策略使用8周期和16周期的指数移动平均线(EMA)计算MACD值,并使用11周期的简单移动平均线(SMA)作为信号线。当MACD线上穿信号线时产生买入信号,下穿时产生卖出信号。同时,策略引入了1%的止损和2%的止盈设置,并且只在用户指定的时间范围内(默认2023全年)执行交易。
这是一个结构完整、逻辑清晰的量化交易策略。通过MACD交叉产生交易信号,配合时间筛选和风险管理,形成了一个实用的交易系统。策略的可调节性强,适合进一步优化和个性化调整。建议交易者在实盘使用前进行充分的回测,并根据具体交易品种和市场环境调整参数。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sergengurgen83
//@version=5
strategy(title="MACD Crossover Strategy with Date Range", shorttitle="MACD Crossover strategys.g", overlay=true)
// Kullanıcı girişleri
fastLength = input.int(8, minval=1, title="Hızlı MA Süresi")
slowLength = input.int(16, minval=1, title="Yavaş MA Süresi")
signalLength = input.int(11, minval=1, title="Sinyal MA Süresi")
stopLossPercent = input.float(1.0, title="Stop-Loss Yüzdesi") / 100
takeProfitPercent = input.float(2.0, title="Kar Al Yüzdesi") / 100
// Tarih aralığı girişleri
startDate = input(timestamp("2023-01-01 00:00"), title="Başlangıç Tarihi")
endDate = input(timestamp("2023-12-31 23:59"), title="Bitiş Tarihi")
// Tarih aralığı kontrolü
inDateRange = true
// Hareketli Ortalamalar ve MACD Hesaplamaları
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macd = fastMA - slowMA
signal = ta.sma(macd, signalLength)
// Alım ve Satım sinyalleri
buySignal = ta.crossover(macd, signal) and inDateRange
sellSignal = ta.crossunder(macd, signal) and inDateRange
// Strateji kuralları
if (buySignal)
strategy.entry("Buy", strategy.long)
if (sellSignal)
strategy.close("Buy")
// Stop-Loss ve Kar Al seviyeleri
strategy.exit("Sell", from_entry="Buy", loss=stopLossPercent * close, profit=takeProfitPercent * close)
// Sinyallerin grafikte gösterilmesi
plot(macd, color=color.blue, title="MACD")
plot(signal, color=color.red, title="Sinyal")
hline(0, color=color.purple, linestyle=hline.style_dashed)
plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="Al", text="AL")
plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sat", text="SAT")