0

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

//@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.

These are the signals I am trying to replicate for a strategy, the code was used to create the indicator but I'm having a hard time translating it to a strategy. You see the initial signal is given when the 9ema crosses the 21ema and the larger signal is given when the 21ema crosses the 33 simple ma...

image

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
EMAguru
  • 1
  • 1
  • 1
  • 2

1 Answers1

0

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

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Thank you for sowing me the ways of this website, I am new as you can obviously tell hahaha but I really do appreciate your help! I have a couple more questions if you could possible help me regarding this script some more... It still isn't working as I'm expecting it to. It gives off sell signals as soon as some of the crosses happen. Is there anyway to make each condition independent, for example like making a "small entry" for the 9 and 21 cross to the updside , then making a "Large entry" for the 21 and 33 cross to the upside? then vice versa for the downside as well... – EMAguru Sep 29 '19 at 07:05
  • I posted a picture of what I'm looking for and trying to replicate in the answer's box above if you can see it – EMAguru Sep 29 '19 at 07:10
  • You can make each condition independent by making two conditions statements for a long, one on each crossover, and then two `if` statements for longs instead of one, then do the same for the shorts, so you'll end up with a total of four conditions. This should be a fairly simple task, as you now have working code. This is not a code-writing service, so I'll let you work on your code. If you try something and it doesn't work, then come back with your code and we'll see. Since you have a script that shows markers where you need entries/exits, could be useful to see how it achieves that. – PineCoders-LucF Sep 29 '19 at 16:25