0
votes

I am desperately looking for help with writing a strategy in trading view where I'm trying to create a crossover buy and sell signals on 3 different MA's , specifically a 9 ema , 21 ema, and 33 simple moving average. What I'm trying to do is have a long condition present itself when the 9 ema crosses above the 21 ema and also having the same long condition happen when the 21 ema crosses above the 33 simple moving average. I want this to work for both Long's and shorts - using the 9 & 21 as a "Small Signal" while using the 21 and 33 as a "Large Signal" and I can't figure it out... this is the closest I have below and it wont work correctly :/ Please help , I will venmo / cash app for your next 6 pack on me !!!!

//@version=3
//study(title="MA Crossover Strategy", overlay = true)
strategy("EMA Crossover Strategy", overlay=true)
src = input(close, title="Source")

price = security(tickerid, period, src)
ema1 = input(9, title="1st EMA Length")
type1 = input("EMA", "1st EMA Type", options=["SMA", "EMA"])

ema2 = input(21, title="2nd EMA Length")
type2 = input("EMA", "2nd EMA Type", options=["SMA", "EMA"])

sma3 = input(33, title="1st MA Length")
type3 = input("SMA", "2nd SMA type", options=["SMA", "EMA"])

price1 = if (type1 == "EMA")
ema(price, ema1)
else
sma(price, ema1)

price2 = if (type2 == "EMA")
sma(price, ema2)
else
ema(price, ema2)

price3 = if (type3 == "SMA")
sma(price, sma3)
else
ema(price, sma3)

//plot(series=price, style=line,  title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=line,  title="1st EMA", color=blue, linewidth=2, transp=0)
plot(series=price2, style=line, title="2nd EMA", color=yellow, linewidth=2, transp=0)
plot(series=price3, style=line, title="1st MA", color=orange, linewidth=2, transp=0)

longCondition = crossover(price1, price2) and crossover(price2, price3)
if (longCondition)
strategy.entry("Long", strategy.long)

shortCondition = crossunder(price1, price2) and crossover(price2, price3)
if (shortCondition)
strategy.entry("Short", strategy.short)

The signals wont show up but If you delete the last "and crossover" part of both conditions it will work only for the 9 ema and 21 ema but I want to incorporate the 21 ema and 33 simple cross

2

2 Answers

0
votes

Please copy your code with spaces/tabs next time, so it does not require reformatting. Also, do not use [tradingiew-api] tag for Pine-related questions, as is mentioned in the tag's description.

You were using and in your conditions, which entailed that both crosses needed to occur on the same bar for condition to be true. Also inverted your ema/sma calcs for second MA.

Always best to print a marker on your conditions to be sure they occur when you expect them too.

//@version=3
//study(title="MA Crossover Strategy", overlay = true)
strategy("EMA Crossover Strategy", overlay=true)
src = input(close, title="Source")

price = security(tickerid, period, src)
ema1 = input(9, title="1st EMA Length")
type1 = input("EMA", "1st EMA Type", options=["SMA", "EMA"])

ema2 = input(21, title="2nd EMA Length")
type2 = input("EMA", "2nd EMA Type", options=["SMA", "EMA"])

sma3 = input(33, title="3rd MA Length")
type3 = input("SMA", "3rd SMA type", options=["SMA", "EMA"])

price1 = if (type1 == "EMA")
    ema(price, ema1)
else
    sma(price, ema1)

price2 = if (type2 == "EMA")
    ema(price, ema2)
else
    sma(price, ema2)

price3 = if (type3 == "SMA")
    sma(price, sma3)
else
    ema(price, sma3)

//plot(series=price, style=line,  title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=line,  title="1st EMA", color=blue, linewidth=2, transp=0)
plot(series=price2, style=line, title="2nd EMA", color=yellow, linewidth=2, transp=0)
plot(series=price3, style=line, title="1st MA", color=orange, linewidth=2, transp=0)

longCondition = crossover(price1, price2) or crossover(price2, price3)
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = crossunder(price1, price2) or crossover(price2, price3)
if (shortCondition)
    strategy.entry("Short", strategy.short)

plotchar(shortCondition, "shortCondition", "▼", location.abovebar, maroon, size = size.tiny)
plotchar(longCondition, "longCondition", "▲", location.belowbar, lime, size = size.tiny)

enter image description here