How do you script a...
 
Notifications
Clear all

How do you script a MACD crossover strategy?

5 Posts
2 Users
1 Likes
1,066 Views
Mike
 Mike
(@vanguardai)
Posts: 70
Co-Founder Admin
Topic starter
 

*This is a sample question. You have the option to put more information here.

 
Posted : 28/12/2020 10:42 am
Topic Tags
Mike
 Mike
(@vanguardai)
Posts: 70
Co-Founder Admin
Topic starter
 

You can copy-paste the code below into your VAI Script editor. The code below can also be found here.

This formula will also plot the trading signals into the VAI Chart

//Buy when MACD Line crossed up, otherwise sell

//set variables
set macd_line = MACD(13, 26, 9, EXPONENTIAL)
set macd_signal =  MACDSIGNAL(13, 26, 9, EXPONENTIAL)
set hist = macd_line – macd_signal

//Buy condition
Set BuySignal =  CROSSOVER(macd_line , macd_signal)

//Sell Condition
Set SellSignal = CROSSOVER(macd_signal, macd_line)

//Plot MACD
plot(macd_line, line, any, macd)
plot(macd_signal, line, any, macd)
plot(hist, bar, any, macd)

//Plot Buy and sell signals
Plot(BuySignal, Buy, Green, main)
Plot(SellSignal, Sell, Red, main)

 
Posted : 28/12/2020 10:44 am
jabberwock
(@jabberwock)
Posts: 2
New Member User
 

@vanguardai hello, would it be possible for a command/script with conditions regarding MACD Histogram?
e.g.:
BUY if MACD Histogram is ascending (current bar is greater than previous bar) and vice versa for SELL

 
Posted : 24/03/2021 2:44 pm
Mike
 Mike
(@vanguardai)
Posts: 70
Co-Founder Admin
Topic starter
 

Yes, but it may show a buy signal everyday if the histrogram is increasing everyday but it is possible.

 

Set BuySignal = hist > ref(hist,1)
Set SellSignal = hist < ref(hist,1)

The ref function means reference to a past period, in this case 1 period in the past. so the system will check everyday if the histogram is higher or lower than the previous period and provide the appropriate signal.

 
Posted : 24/03/2021 3:02 pm
jabberwock
(@jabberwock)
Posts: 2
New Member User
 

@vanguardai thank you so much, its ok if its giving me signals everyday because its just a part of my system, confirming momentum movement.

 
Posted : 29/03/2021 5:36 pm
Mike reacted
Share: