
This is a quantitative trading strategy based on the MACD indicator that executes trades within a specified time range. The core strategy utilizes fast and slow moving averages to calculate MACD values and generates signals based on crossovers with the signal line. The strategy also incorporates stop-loss and take-profit mechanisms to control risk and lock in profits.
The strategy employs 8-period and 16-period exponential moving averages (EMA) to calculate MACD values, and uses an 11-period simple moving average (SMA) as the signal line. Buy signals are generated when the MACD line crosses above the signal line, while sell signals occur on downward crosses. The strategy includes a 1% stop-loss and 2% take-profit setting, and only executes trades within a user-specified time range (default is full year 2023).
This is a well-structured quantitative trading strategy with clear logic. It generates trading signals through MACD crossovers, combined with time filtering and risk management to form a practical trading system. The strategy’s high adjustability makes it suitable for further optimization and customization. Traders are advised to conduct thorough backtesting before live implementation and adjust parameters according to specific trading instruments and market conditions.
/*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")