woodsyc
Pine Script Rookie
Pine Script Rookie
Posts: 5
Joined: April 2nd, 2023
Contact: TradingView Profile

Detecting Pip spread

I am looking to detect a Pip spread in ema2 and ema3.

//@version=5
indicator('Scalp strategy', overlay=true)

// Get indicator data
ema1 = ta.ema(close, 6)
ema2 = ta.ema(high, 32)
ema3 = ta.ema(low, 32)

// Detect Buy or Sell
buy = ema1 > ema2 and (ema2 - ema3) > 0.004
sell = ema1 < ema3 and (ema2 - ema3) > 0.004

// Eliminate repeated detects
longSignal = buy and not buy[1]
shortSignal = sell and not sell[1]

plotshape(longSignal, title= "Long signal", style=shape.triangleup, location=location.belowbar, color=color.green)
plotshape(shortSignal, title= "Short signal", style=shape.triangledown, location=location.abovebar, color=color.red)
plot(ema1, color=color.white, linewidth=2 ,title="EMA 1")
plot(ema2, color=color.green, linewidth=2, title="EMA 2")
plot(ema3, color=color.green, linewidth=2, title="EMA 3")

Basically I am trying to scalp where the ema1 crosses through ema2 and ema3 but I want to ensure there is a spread between ema2 and ema3 which will indicate that there is momentum. If the spread is low then it is likely consolidating. If the spread is good then it is a good opportunity to scalp.

So the 0.004 is the PIPs spread. So far this is not working. Also with the Yen, how do I make this work for that as well?

Any help is appreciated.

Thank you.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Detecting Pip spread

Hey woodsync,

Your logic seems sound and I have it working here. In what way is it not working? Do you have any errors . As far as the Yen is concerned, you will need to do a check for the Yen and adjust the decimal places such as:

// Check if Yen
yen = str.endswith(syminfo.currency,'JPY')

// Detect Buy or Sell
if yen
buy := ema1 > ema2 and (ema2 - ema3) > 0.04
sell := ema1 < ema3 and (ema2 - ema3) > 0.04
else
buy := ema1 > ema2 and (ema2 - ema3) > 0.004
sell := ema1 < ema3 and (ema2 - ema3) > 0.004

woodsyc
Pine Script Rookie
Pine Script Rookie
Posts: 5
Joined: April 2nd, 2023
Contact: TradingView Profile

Re: Detecting Pip spread

Looks like it is working most of the time. I have seen instances where the alert is going off when it should not be which is weird. At the moment I am just ignoring them.

Thank you for the help. I had a login issue so my response was delayed : )

Return to “Pine Script Q&A”