Strategies KB.ZH
Strategies

趋势波段交易EMA-MACD复合策略-EMA-MACD-Composite-Strategy-for-Trend-Scalping

ChaoZhang · PineScript · Trend Following · 2024-12-12 15:05:37

All strategies →

No English write-up in the source (no author description).

Read the Chinese version →

Source code (56 lines)
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-10 08: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/
// © YDMykael

//@version=6
//@version=5
strategy("TrendScalp Bot", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// Inputs for indicators
fastEMA = input.int(50, title="Fast EMA")
slowEMA = input.int(200, title="Slow EMA")
rsiPeriod = input.int(14, title="RSI Period")
macdFast = input.int(12, title="MACD Fast Length")
macdSlow = input.int(26, title="MACD Slow Length")
macdSignal = input.int(9, title="MACD Signal Length")

// Indicators
fastEMAValue = ta.ema(close, fastEMA)
slowEMAValue = ta.ema(close, slowEMA)
rsiValue = ta.rsi(close, rsiPeriod)
[macdLine, signalLine, _] = ta.macd(close, macdFast, macdSlow, macdSignal)

// Trend detection
isUptrend = fastEMAValue > slowEMAValue
isDowntrend = fastEMAValue < slowEMAValue

// Entry conditions
longCondition = isUptrend and rsiValue > 55 and macdLine > signalLine
shortCondition = isDowntrend and rsiValue < 45 and macdLine < signalLine

// Plot EMA
plot(fastEMAValue, color=color.blue, title="Fast EMA")
plot(slowEMAValue, color=color.red, title="Slow EMA")

// Buy/Sell signals
if (longCondition)
    strategy.entry("Buy", strategy.long)
if (shortCondition)
    strategy.entry("Sell", strategy.short)

// Exit on opposite signal
if (not isUptrend or not (macdLine > signalLine))
    strategy.close("Buy")
if (not isDowntrend or not (macdLine < signalLine))
    strategy.close("Sell")

// Alerts
alertcondition(longCondition, title="Buy Alert", message="TrendScalp Bot: Buy Signal")
alertcondition(shortCondition, title="Sell Alert", message="TrendScalp Bot: Sell Signal")