Pinescript //@version=5, trying to combine 2 EMAs and PSAR and add buy/sell signals

1k views Asked by At

im really new at coding, so i know it doesnt run, but this what i got. I want to show buy signal when PSAR goes below the chart and the EMAs are below the chart(EMA200 below EMA20) I want to show sellsignal when PSAR goes above the chart and the EMAs are above the chart(EMA200 above EMA20) I find it really good automated startegy with R.R=2 . if you can help me run the code i reall appreciate your time and effort.

my effort is below - THANKS AGAIN :

strategy("PSAR 2EMAs Strategy",shorttitle="PSAR Buy/Sell" ,overlay=true )


indicator(title="Parabolic SAR", shorttitle="SAR", overlay=true, timeframe="", 
timeframe_gaps=true)
start = input(0.02)
increment = input(0.02)
maximum = input(0.2, "Max Value")
out3 = ta.sar(start, increment, maximum)
plot(out3, "ParabolicSAR", style=plot.style_cross, color=#2962FF)

indicator(title="Moving Average Exponential", shorttitle="EMA1", overlay=true, 
timeframe="", timeframe_gaps=true)
len = input.int(20, minval=1, title="Length")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.ema(src, len)
plot(out, title="EMA", color=color.yellow, offset=offset)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA 
(RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, 
group="Smoothing")

smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset, 
display=display.none)


indicator(title="Moving Average Exponential", shorttitle="EMA2", overlay=true, 
timeframe="", timeframe_gaps=true)
len2 = input.int(200, minval=1, title="Length")
src2 = input(close, title="Source")
offset2 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out2 = ta.ema(src, len)
plot(out2, title="EMA", color=color.blue, offset2=offset2)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA 
(RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, 
group="Smoothing")

smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset, 
display=display.none)

//the strategy goes like this: WHEN PSAR gets below the  chart and EMA200 is below EMA20 
and chart is above the 2 EMAs: "BUY"
//                             WHEN PSAR gets above the  chart and EMA200 is above EMA20 
and chart is below the 2 EMAs: "SELL"

if ta.change("PSAR Buy/Sell") < 0  and EMA1 below EMA2
    strategy.entry("My Long Entry Id", strategy.long)

if ta.change("PSAR Buy/Sell") > 0 and EMA1 above EMA2 
    strategy.entry("My Short Entry Id", strategy.short)
0

There are 0 answers