MACD Strategy
With PineConnector, you can fully automate the MACD strategy, making it a suitable solution for both day and swing traders. This allows you to streamline your trading process, freeing up time, minimizing the risk of human error and eliminating emotion in your trading.
Implementation of the Strategy
- PineConnector EA running on your MetaTrader terminal — you should see your license details printed on your chart.
- Close on Reverse EA setting set as “On — Hedging”.
1. Create a new script
At the bottom of your TradingView tab, click "Pine Editor" → “Open” → “Strategy”.
2. Attach the code
Remove the default code generated, and attach the following code:
//@version=5
strategy("MACD Strategy - PineConnector", overlay=true)
LicenseID = 6914107867349 // 1. change to your PineConnector License ID (required)
riskvalue = input.int(1, 'Risk Value') // 2. Change the risk value (optional)
slvalue = input.int(500, 'Sl Value') // 3. Change the sl value (optional)
tpvalue = input.int(1000, 'TP Value') // 4. Change the tp value (optional)
fastLength = input(34) // 5. Change the fastlength (optional)
slowlength = input(90) // 6. Change the slowlength (optional)
MACDLength = input(144) // 7. Change the MACDlength (optional)
MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD
MacdBuy = ta.crossover(delta, 0)
MacdSell = ta.crossunder(delta, 0)
plotshape(MacdBuy, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(MacdSell, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
if (ta.crossover(delta, 0))
strategy.entry("MacdBuy", strategy.long, comment="MacdBuy")
alert(str.tostring(LicenseID)+',buy,' + syminfo.ticker + ',risk=' + str.tostring(riskvalue)+ ',sl=' + str.tostring(slvalue)+ ',tp=' + str.tostring(tpvalue), alert.freq_once_per_bar_close)
if (ta.crossunder(delta, 0))
strategy.entry("MacdSell", strategy.short, comment="MacdSell")
alert(str.tostring(LicenseID)+',sell,' + syminfo.ticker + ',risk=' + str.tostring(riskvalue)+ ',sl=' + str.tostring(slvalue)+ ',tp=' + str.tostring(tpvalue), alert.freq_once_per_bar_close)
6914107867349
, input your PineConnector License ID you’re entered in the PineConnector EA.
Basic Optional Change:
- Instead of
riskvalue
being 1, you may increase or decrease the lot size per trade. - How the EA processes this
riskvalue
depends on your Volume Type in your EA. - the default setting of “Lots” will open 1 volume/lot each trade with
riskvalue
of 1 — use this option if you are a beginner - How the EA processes this
tp/slvalue
depends on your Target Type in your EA.
Advanced Optional Changes:
- Instead of
fastlength
of 34, you may increase or shorten the period fast length. - Instead of
lowlength
of 90, you may increase or shorten the period slow length. - Instead of
MACDlength
of 144, you may increase or shorten MACD length.
3. Save the code and Add Strategy to the Chart
After attaching and amending the above code, click “Save” then “Add to chart” to apply the strategy to your current chart. You should be able to see the MACD entries indicated on the chart:
4. Create alerts
On your desired instrument and timeframe create the alerts.
To create an alert, press Alt+A (Windows) or Option+A (Mac), and configure your alert as follows:
Component | Configuration |
Condition | MACD Strategy
alert() function calls only |
Webhook URL | For PineConnector v2:
https://pineconnector.net/webhook/
For PineConnector v3:
https://webhook.pineconnector.com |
Alert name,
Notify on app | “Alert name” does not matter
”Notify on app” selection does not matter as well |
After proper alert configuration, click “Create”. An active alert will appear under your “Alerts” tab.
5. Confirm triggered alerts
When entry conditions are met, an alert will fire. You will see an entry in your TradingView’s Alerts Log indicating the time, alert message, instrument and timeframe it fired based on.
After a alert fires, you should see a corresponding trade entry as per the alert message.
6. Tailor your alerts (optional)
Consider adding a stop-loss, a comment with the position, or even a spread filter.
Component | Relevant Guide |
- Discover more about the proper way to write the alert messages
- Add more components such as a spread filter | |
- Utilise a different Volume Type
- Implement a Daily Profit or Loss | |
- Include additional parameters such as stop-loss
- Implement dynamic symbols and values |