Strategy · Pine Script v5 · On the chart

XO_macd

by wynnforthework· code credited to btc_charlie / @TheParagonGrp

XO_macd is an overlay strategy combining exponential moving average crossovers with MACD calculations.

View on GitHub

Repaints

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code152
Settings18inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Oct 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

152 lines of Pine Script v5 from 策略/开源分享/xo_macd.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

xo_macd.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © btc_charlie / @TheParagonGrp

//@version=5
strategy('XO_macd', shorttitle="XO_macd", overlay=true, precision=2, pyramiding=1, 
 calc_on_every_tick=true, backtest_fill_limits_assumption=1, default_qty_type=strategy.percent_of_equity, default_qty_value = 50, currency=currency.USD, slippage=1, commission_type=strategy.commission.percent,
 commission_value=.03, process_orders_on_close=true, initial_capital=10000, margin_long=100, margin_short=100)

lowest = input(5, title='多少根K线参与计算')
RRR = input(3, title='盈亏比')

longOn = input.bool(true , '是否开启多单')
shortOn = input.bool(true , '是否开启空单')

emaNo = input(200, title='ema参数')
long_Ts2          = input.float(0.004,          title='long移动止盈参数',     minval=0,       step=0.001)
short_Ts2          = input.float(0.002,          title='short移动止盈参数',     minval=0,       step=0.001)
// Variables
var ok = 0
var countBuy = 0
var countSell = 0
src = input(close, title='OHLC Type')
i_fastEMA = input(12, title='Fast EMA')
i_slowEMA = input(25, title='Slow EMA')
i_defEMA = input(25, title='Consolidated EMA')

// Getting inputs
fast_length = input(title="Fast Length", defval=26)
slow_length = input(title="Slow Length", defval=100)
src_macd = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

// Allow the option to show single or double EMA
i_bothEMAs = input(title='Show Both EMAs', defval=true)

// Define EMAs
v_fastEMA = ta.ema(src, i_fastEMA)
v_slowEMA = ta.ema(src, i_slowEMA)
v_biasEMA = ta.ema(src, i_defEMA)

// Color the EMAs
emaColor = v_fastEMA > v_slowEMA ? color.green : v_fastEMA < v_slowEMA ? color.red : #FF530D

// Plot EMAs
plot(i_bothEMAs ? na : v_biasEMA, color=emaColor, linewidth=3, title='Consolidated EMA')
plot(i_bothEMAs ? v_fastEMA : na, title='Fast EMA', color=emaColor)
plot(i_bothEMAs ? v_slowEMA : na, title='Slow EMA', color=emaColor)

// Colour the bars
buy = v_fastEMA > v_slowEMA
sell = v_fastEMA < v_slowEMA

if buy
    countBuy += 1
    countBuy

if buy
    countSell := 0
    countSell

if sell
    countSell += 1
    countSell

if sell
    countBuy := 0
    countBuy

buysignal = countBuy < 2 and countBuy > 0 and countSell < 1 and buy and not buy[1]
sellsignal = countSell > 0 and countSell < 2 and countBuy < 1 and sell and not sell[1]

barcolor(buysignal ? color.green : na)
barcolor(sellsignal ? color.red : na)

// Plot Bull/Bear

plotshape(buysignal, title='Bull', text='Bull', style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.black, 0), size=size.tiny)
plotshape(sellsignal, title='Bear', text='Bear', style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.black, 0), size=size.tiny)

// macd 计算
fast_ma = sma_source == "SMA" ? ta.sma(src_macd, fast_length) : ta.ema(src_macd, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src_macd, slow_length) : ta.ema(src_macd, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
ema200 = ta.ema(close, emaNo)


// 开仓时的止损和止盈价位,并存储在变量中
long_stop_loss = ta.lowest(lowest) //5根K线内最低价
long_take_profit = ((close - long_stop_loss) * RRR) + close 
var long_stop_loss_initial = 0.0
var long_take_profit_initial = 0.0
// 多头开单
if buysignal and hist >= 0 and ema200 < close and longOn
    // 在开仓时存储止损和止盈价位 bar_index
    long_stop_loss_initial := long_stop_loss
    long_take_profit_initial := long_take_profit
    strategy.entry('long',strategy.long)
    strategy.exit("平仓", "long",stop=long_stop_loss_initial, comment_loss="long 止损")
if close < ema200
    strategy.close("long", comment="多头趋势结束 止损")
pivotsizing = 20 //length
pivotLookup = 1
L_TP = long_take_profit_initial
hih     = ta.pivothigh(high, pivotLookup, pivotLookup)
top     = ta.valuewhen(hih, high[pivotLookup], 0)
float highest = ta.highest(top,pivotsizing)
moveHigh = ta.crossunder(close,highest*(1 - long_Ts2))
L_reversal = ta.crossunder(close,L_TP)

if close > L_TP and moveHigh and strategy.position_size > 0
    strategy.close_all(comment = "多头趋势止盈")
else if close > L_TP and L_reversal and strategy.position_size > 0
    strategy.close_all(comment = "多头趋势不明显保护性止盈")

// 开仓时的止损和止盈价位,并存储在变量中
short_stop_loss = ta.highest(lowest) //5根K线内最高价
short_take_profit = close - ((short_stop_loss - close) * RRR)  //计算止盈位
var short_stop_loss_initial = 0.0
var short_take_profit_initial = 0.0
// 空头开单
if sellsignal and hist <= 0 and ema200 > close and shortOn
    // 在开仓时存储止损和止盈价位 bar_index
    short_stop_loss_initial := short_stop_loss
    short_take_profit_initial := short_take_profit
    strategy.entry('short',strategy.short)
    strategy.exit("平仓", "short",stop=short_stop_loss_initial, comment_loss="short 止损")

S_TP = short_take_profit_initial
bot     = ta.pivotlow(low, pivotLookup, pivotLookup)
bottom     = ta.valuewhen(bot, low[pivotLookup], 0)
float bottomst = ta.lowest(bottom,pivotsizing)
moveBottom = ta.crossunder(close,bottomst*(1 - short_Ts2))
S_reversal = ta.crossunder(close,S_TP)

if close < L_TP and moveBottom and strategy.position_size < 0
    strategy.close_all(comment = "空头趋势止盈")
else if close < L_TP and S_reversal and strategy.position_size < 0
    strategy.close_all(comment = "空头趋势不明显保护性止盈")




// 在每个周期结束时,检查订单状态并更新止损和止盈价位
if strategy.position_size == 0
    long_stop_loss_initial := long_stop_loss
    long_take_profit_initial := long_take_profit
    short_stop_loss_initial := short_stop_loss
    short_take_profit_initial := short_take_profit

Overview

XO_macd tracks fast, slow, and consolidated bias EMAs to identify trend changes, coloring price bars and plotting Bull or Bear triangle shapes when conditions trigger. The strategy executes long and short trades based on EMA alignment and MACD filters. Inputs allow traders to adjust EMA lengths, MACD smoothing types, trade direction toggles, risk-reward ratios, and trailing profit parameters. All visual elements render directly on the main price chart. Live bar signals repaint before bar close.

Details

Published by

wynnforthework on GitHub

Code credit

btc_charlie / @TheParagonGrp, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy can add several entries in the same direction

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
多少根K线参与计算int5lowest
盈亏比int3RRR
是否开启多单booltruelongOn
是否开启空单booltrueshortOn
ema参数int200emaNo
long移动止盈参数float0.004long_Ts2
short移动止盈参数float0.002short_Ts2
OHLC Typesourceclosesrc
Fast EMAint12i_fastEMA
Slow EMAint25i_slowEMA
Consolidated EMAint25i_defEMA
Fast Lengthint26fast_length
Show all 18 settings
SettingTypeDefaultVariable
Slow Lengthint100slow_length
Sourcesourceclosesrc_macd
Signal Smoothingint9signal_length
Oscillator MA TypestringEMAsma_source
Signal Line MA TypestringEMAsma_signal
Show Both EMAsbooltruei_bothEMAs
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy

This strategy combines Arnaud Legoux Moving Average percentage price change calculations with an adaptive Gaussian Moving Average to generate trend-following signals. Long and short entries are triggered when the ALMA change calculation crosses above or below

Strategy v5 Moving averagesOscillators & momentum
Updated 7 weeks ago · None stated GitHub repository of ESM2024 - ALMA Smoothed Gaussian Moving Average
228 stars

This strategy generates trading signals based on a selectable momentum engine, supporting MACD, RSI, Stochastic, or Rate of Change. Entries trigger on signal crosses, zero-line crosses, or threshold crosses, with an optional moving average trend filter. Risk m

Strategy v6 Oscillators & momentumTrend
Updated 3 months ago · None stated GitHub repository of Momentum Strategy Template

255 EMA Strategy

by hasnocool
106 stars

255 EMA Strategy combines an exponential moving average with Average True Range bands and standard MACD crossovers to generate entry signals. Drawn directly on the price chart, it plots the central 255 EMA along with upper and lower volatility envelopes. Trade

Strategy v4 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 255 EMA Strategy

SMA RSI Strategy

by PythonForForex
64 stars

This strategy evaluates simple moving averages and the Relative Strength Index to generate trading signals. It calculates a 10-period SMA, a 30-period SMA, and a 14-period RSI. Long positions are entered when the fast SMA crosses above the slow SMA while RSI i

Strategy v5 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of SMA RSI Strategy

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers