1
votes

The code looks like this:

study(title = "Hull MA", shorttitle="HMA", overlay = true, resolution="60")
length = input(48, minval=1)
price = input(close, title="Source")
hull = wma(2*wma(price, length/2)-wma(price, length), round(sqrt(length)))
plot(hull, title='Hull', color=change(hull) < 0 ? color.orange : color.purple, linewidth=2, transp=0)

So, what I would like to be able to do is send a BUY-signal when Hull turns purple and a SELL-signal when Hull turns orange. I thought I could do it by adding this:

plotshape(change(hull) < 0, color=color.orange, style=shape.circle, location=location.belowbar, text="sell", title='sell')
plotshape(change(hull) > 0, color=color.purple, style=shape.circle, location=location.abovebar, text="buy", title='buy')

But if I do it like that, I get a BUY-signal on every candle where hull > 0 and a SELL-signal on every candle where hull < 0 (I'm running this on an 8h-candle with a resolution of 1h for the HullMA). I just want 1 BUY-signal when the plot turns from Orange to Purple and 1 SELL-signal when the plot turns from Purple to Orange.

Anyone have any ideas? I'd be much obliged! :)

1

1 Answers

0
votes

I figured it out! The answer lies in the history referencing operator "[]". When using [], you can tell the script to look for the specified value from n candles ago, in this case "(hull[1])". I simply told the script to only plot a buy-signal when the HullMA had a value bigger than 0 and if the HullMA-value from the previous candle had a value less than 0. The code now looks like this:

study(title = "Hull MA_TEST_v1.0", shorttitle="HMA_TESTv1.0", overlay = true, resolution="")
length = input(10, minval=1)
price = input(close, title="Source")
hull = wma(2*wma(price, length/2)-wma(price, length), round(sqrt(length)))
plot(hull, title='Hull', color=change(hull) < 0 ? color.blue : color.yellow, linewidth=2)

makeShape1 = if change(hull) > 0 and change(hull[1]) < 0
    true
else
    false

plotshape(series=makeShape1, style=shape.cross, color=color.yellow, transp=10, text="buy", title='buy')

makeShape2 = if change(hull) < 0 and change(hull[1]) > 0
    true
else
    false

plotshape(series=makeShape2, style=shape.cross, color=color.blue, transp=10, text="sell", title='sell')

This may not be the most efficient way of coding it, but it works and I'm happy!