0
votes

I am trying to convert built-in Momentum strategy to study with alerts. I don't know if I did it right. Chart is compressed to straight line :( Please, help me. Thank you.

This is original built-in Momentum strategy:

//@version=4
strategy("Momentum Strategy", overlay=true)
length = input(12)
price = close

momentum(seria, length) =>
    mom = seria - seria[length]
    mom

mom0 = momentum(price, length)
mom1 = momentum( mom0, 1)

if (mom0 > 0 and mom1 > 0)
    strategy.entry("MomLE", strategy.long, stop=high+syminfo.mintick, comment="MomLE")
else
    strategy.cancel("MomLE")

if (mom0 < 0 and mom1 < 0)
    strategy.entry("MomSE", strategy.short, stop=low-syminfo.mintick, comment="MomSE")
else
    strategy.cancel("MomSE")

//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)

This is my study with alerts:

//@version=4
study("Momentum Alert", overlay=true)
length = input(12)
price = close

momentum(seria, length) =>
    mom = seria - seria[length]
    mom

mom0 = momentum(price, length)
mom1 = momentum(mom0, 1)

alertcondition(condition=mom0 > 0 and mom1 > 0, message="Momentum increased")
alertcondition(condition=mom0 < 0 and mom1 < 0, message="Momentum decreased")

plot(series=mom1)
2

2 Answers

0
votes

I fugered out that this code works. Bud i still dont know if it is same as built-in strategy

//@version=4
study(title="Momentum", shorttitle="Mom")

len = input(40, minval=1, title="Length")
src = input(close, title="Source")

mom = src - src[len]
mom0 = mom(src,len)
mom1 = mom(mom0,1)

alertcondition(condition=mom0 > 0 and mom1 > 0, message="Momentum increased")
alertcondition(condition=mom0 < 0 and mom1 < 0, message="Momentum decreased")

plot(mom0, color=color.olive, title="Mom")
plot(mom1, color=color.red, title="Mom")

I dont understand this: enter image description here

0
votes

Your code will generate the same entry conditions than the strategy, but reproducing all the strategy's behavior will require you to build logic that replicates both the specific `strategy.*() calls it uses and how the TV broker emulator will execute them.

For example, your entries are issuing stop orders which are canceled when the entry condition is no longer true, and this isn't reflected in your study code.

You could improve your code by eliminating this line, as it's poor practice to name a variable with a name used as a built-in, and that variable is not used later in your script. There is a built-in function in Pine called mom() (which the TV strategy should actually use instead of a user-defined function doing the same thing).

mom = src - src[len]

Your following two lines are using that function right now, rather than the mom variable from the previous line:

mom0 = mom(src,len)
mom1 = mom(mom0,1)