Alex Rambarose
Alex Rambarose

Reputation: 1

Can someone let me know how would I implement this into my code (Im very new to this)

Hey hope you are doing great!

To summarize my script it basically looks for engulfing candles and once it finds that it checks if the 13ema crosses thru the bodies of both candles. If that is true then a signal shape immediately appears on the open of the next candle.

So what I'm trying to implement is something that makes the signal shape only appear if the 50ema is at least 10 pips away from the close of the candle right before the signal appear. I will do my best to give some examples!

https://www.tradingview.com/x/o9HnCVQ6/ (Sell Example)

https://www.tradingview.com/x/fCKcHdD4/ (Buy Example) Btw the shape would be facing up and green for buys


Lets say the 50ema happens to be:

-Above the 13 ema for the sell signal

-Below the 13ema for the buy signal

I want it to calculate the at least 10 pips using the close of the previous candle but instead of the 50ema it uses the 200ema.

https://www.tradingview.com/x/STD4mX4F/ (Sell Example)

https://www.tradingview.com/x/1M3qxoHw/ (Buy Example)


Lets say the 50ema and the 200ema happens to be:

-Above the 13 ema for the sell signal

-Below the 13ema for the buy signal

I want it to calculate the at least 10 pips using the close of the previous candle but instead of the 50ema or the 200 ema it uses the 800ema.

https://www.tradingview.com/x/zqQaKFPj/ (Sell Example)

https://www.tradingview.com/x/hTyBGsyn/ (Buy Example)


If on a buy signal all ema are below the buy it doesn't call it vice versa with a sell. I already did this so don't worry about this part!

I got a hint that I need to use syminfo.pointvalue to do this but I'm very new to this so I'm not sure how to implement it in the script.

I tried reading this but even this breakdown is a bit advanced for me to understand. https://kodify.net/tradingview/info/syminfo-pointvalue/

Thank you in advance!! :)

study("Test 1", overlay=true)

//Colors
_color5 = #ffff00
_color13 = #ff0000
_color50 = #00ffff
_color200 = #434651
_color800 = #0000ff

//EMA value
_ema5 = ema(close, 5)
_ema13 = ema(close, 13)
_ema50 = ema(close, 50)
_ema200 = ema(close, 200)
_ema800 = ema(close, 800)


//Plots EMAs
//plot(_ema5, color=#ffff00, transp=100 )
//plot(_ema13, color=#ff0000, transp=100)
//plot(_ema50, color=#00ffff, transp=100)
//plot(_ema200, color=#434651, transp=100)
//plot(_ema800, color=#0000ff, transp=100)

//_c13 and _c50 is true if the ema
_c13 = cross(close, _ema13)
_c50 = cross(close, _ema50)

//Line that the EMAs actually cross
plot(close, style=plot.style_line, color=color.black, transp=100)

//b2 is candle 1 and b1 is candle 2 (IN PINE YOU COUNT CANDLES BACKWARDS FORM THE CURRENT CANDLE)
//Gives us the size of the candles (Regardless of bull or bear) 
_b1size = max(open[1], close[1]) - min(open[1], close[1])
_b2size = max(open[2], close[2]) - min(open[2], close[2])

//Ratios of the candle size so you can filter out different sized candles
_szRatio = _b2size/_b1size
_szCondition = _szRatio >= 0.01 and _szRatio<=0.99

//Compares the MAX of candle 1 body against MAX of candle 2 body (same with min)
_engulfing = max(open[1], close[1]) >= max(open[2], close[2]) and min(open[1], close[1]) <= min(open[2], close[2])

//Check the conditions here is true for the EMA to cross candles
_crossed13 = _c13[1] and _c13[2]
_crossed50 = _c50[1] or _c50[2]

//if ema cuts either candle but is also above/below (bear/bull) the 13ema
_crossed50above13ema = _crossed50 and _ema50 > _ema13 //bearsignal
_crossed50below13ema = _crossed50 and _ema50 < _ema13 //bullsignal

//Shows the condidtions for the Signal to Show BULLISH
_emaCondbull =  (open<_ema200 or open<_ema800 or (open<_ema50 or _crossed50below13ema)) and not (open>_ema200 and open>_ema800 and open>_ema50)
_bullSignal = _engulfing  and _crossed13 and close[1] > max(open[2], close[2]) and _szCondition and _emaCondbull

//Plots the Signal if Conditions are true
plotshape(_bullSignal?close:na, location=location.belowbar, style=shape.triangleup, size=size.tiny, color=#00ff73)

//Shows the condidtions for the Signal to Show BEARISH
_emaCondbear =  (open>_ema200 or open>_ema800 or (open>_ema50 or _crossed50above13ema)) and not (open<_ema200 and open<_ema800 and open<_ema50)
_bearSignal =_engulfing and _crossed13 and close[1] < min(open[2], close[2]) and _szCondition and _emaCondbear

//Plots the Signal if Conditions are true
plotshape(_bearSignal?close:na, style=shape.triangledown, size=size.tiny, color=#ff0000)

Upvotes: 0

Views: 884

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6905

A pip is dependent on how you define it.
For EUR/CAD, a pip is defined as 0.0001 (4 digits after comma)

However, TradingView displays it as 0.00001 (5 digits after comma)
That is the value found in syminfo.mintick.

That means that 1 pip equals 10 ticks on TradingView.
See my addition to your code below.
You can set the number of ticks in 1 pip in the input variable ticks_per_pip.
You can also set a threshold, expressed in pips, in the input variable pip_threshold.

Is this what you were looking for?

//@version=4
study("SO 64839720", "SO", overlay=true)

//Colors
_color5 = #ffff00
_color13 = #ff0000
_color50 = #00ffff
_color200 = #434651
_color800 = #0000ff

//EMA value
_ema5 = ema(close, 5)
_ema13 = ema(close, 13)
_ema50 = ema(close, 50)
_ema200 = ema(close, 200)
_ema800 = ema(close, 800)


//Plots EMAs
//plot(_ema5, color=#ffff00, transp=100 )
//plot(_ema13, color=#ff0000, transp=100)
//plot(_ema50, color=#00ffff, transp=100)
//plot(_ema200, color=#434651, transp=100)
//plot(_ema800, color=#0000ff, transp=100)

//_c13 and _c50 is true if the ema
_c13 = cross(close, _ema13)
_c50 = cross(close, _ema50)

//Line that the EMAs actually cross
plot(close, style=plot.style_line, color=color.black, transp=100)

//b2 is candle 1 and b1 is candle 2 (IN PINE YOU COUNT CANDLES BACKWARDS FORM THE CURRENT CANDLE)
//Gives us the size of the candles (Regardless of bull or bear) 
_b1size = max(open[1], close[1]) - min(open[1], close[1])
_b2size = max(open[2], close[2]) - min(open[2], close[2])

//Ratios of the candle size so you can filter out different sized candles
_szRatio = _b2size/_b1size
_szCondition = _szRatio >= 0.01 and _szRatio<=0.99

//Compares the MAX of candle 1 body against MAX of candle 2 body (same with min)
_engulfing = max(open[1], close[1]) >= max(open[2], close[2]) and min(open[1], close[1]) <= min(open[2], close[2])

//Check the conditions here is true for the EMA to cross candles
_crossed13 = _c13[1] and _c13[2]
_crossed50 = _c50[1] or _c50[2]

//if ema cuts either candle but is also above/below (bear/bull) the 13ema
_crossed50above13ema = _crossed50 and _ema50 > _ema13 //bearsignal
_crossed50below13ema = _crossed50 and _ema50 < _ema13 //bullsignal

//Shows the condidtions for the Signal to Show BULLISH
_emaCondbull =  (open<_ema200 or open<_ema800 or (open<_ema50 or _crossed50below13ema)) and not (open>_ema200 and open>_ema800 and open>_ema50)
_bullSignal = _engulfing  and _crossed13 and close[1] > max(open[2], close[2]) and _szCondition and _emaCondbull


// ----------------------------------
// STACKOVERFLOW CODE ADDED - [START]
// ----------------------------------
var int     ticks_per_pip           = input(10, "1 pip = ... ticks (A tick is the smallest value increment on the chart = syminfo.mintick)",    input.integer)
var int     pip_threshold           = input(10, "ema is at least ... pips away from the close of the candle right before the signal appears",   input.integer)

var float   ema_to_compare          = na
var float   distance_in_ticks       = na
var float   distance_in_pips        = na
var bool    pip_threshold_broken    = na


// Extended version (to explain each calculation step)
if _ema50[1] < _ema13[1] and _ema200[1] < _ema13[1]
    ema_to_compare := _ema800
else if _ema50[1] < _ema13[1]
    ema_to_compare := _ema200
else
    ema_to_compare := _ema50

distance_in_ticks       := abs(ema_to_compare[1] - close[1])    // Distance measured on the chart
distance_in_pips        := distance_in_ticks / ticks_per_pip    // Converte that distance to pips
pip_threshold_broken    := distance_in_pips > pip_threshold     // Compare that pip-distance to the threshold distance


// Short version
pip_threshold_broken    := (abs((_ema50[1] < _ema13[1] and _ema200[1] < _ema13[1] ? _ema800[1] : _ema50[1] < _ema13[1] ? _ema200[1] : _ema50[1]) - close[1]) / ticks_per_pip) > pip_threshold

// --------------------------------
// STACKOVERFLOW CODE ADDED - [END]
// --------------------------------

Updated code 18 Nov 2020

//@version=4
study("SO 64839720", "SO", overlay=true)

//Colors
_color5 = #ffff00
_color13 = #ff0000
_color50 = #00ffff
_color200 = #434651
_color800 = #0000ff

//EMA value
_ema5 = ema(close, 5)
_ema13 = ema(close, 13)
_ema50 = ema(close, 50)
_ema200 = ema(close, 200)
_ema800 = ema(close, 800)


//Plots EMAs
//plot(_ema5, color=#ffff00, transp=100 )
//plot(_ema13, color=#ff0000, transp=100)
//plot(_ema50, color=#00ffff, transp=100)
//plot(_ema200, color=#434651, transp=100)
//plot(_ema800, color=#0000ff, transp=100)

//_c13 and _c50 is true if the ema
_c13 = cross(close, _ema13)
_c50 = cross(close, _ema50)

//Line that the EMAs actually cross
plot(close, style=plot.style_line, color=color.black, transp=100)

//b2 is candle 1 and b1 is candle 2 (IN PINE YOU COUNT CANDLES BACKWARDS FORM THE CURRENT CANDLE)
//Gives us the size of the candles (Regardless of bull or bear) 
_b1size = max(open[1], close[1]) - min(open[1], close[1])
_b2size = max(open[2], close[2]) - min(open[2], close[2])

//Ratios of the candle size so you can filter out different sized candles
_szRatio = _b2size/_b1size
_szCondition = _szRatio >= 0.01 and _szRatio<=0.99

//Compares the MAX of candle 1 body against MAX of candle 2 body (same with min)
_engulfing = max(open[1], close[1]) >= max(open[2], close[2]) and min(open[1], close[1]) <= min(open[2], close[2])

//Check the conditions here is true for the EMA to cross candles
_crossed13 = _c13[1] and _c13[2]
_crossed50 = _c50[1] or _c50[2]

//if ema cuts either candle but is also above/below (bear/bull) the 13ema
_crossed50above13ema = _crossed50 and _ema50 > _ema13 //bearsignal
_crossed50below13ema = _crossed50 and _ema50 < _ema13 //bullsignal



// ----------------------------------
// STACKOVERFLOW CODE ADDED - [START]
// ----------------------------------
var int     ticks_per_pip           = input(10, "1 pip = ... ticks (A tick is the smallest value increment on the chart = syminfo.mintick)",    input.integer)
var int     pip_threshold           = input(10, "ema is at least ... pips away from the close of the candle right before the signal appears",   input.integer)
var bool    use_pip_treshold        = input(true, "Use pip treshold",                                                                           input.bool)

var float   ema_to_compare          = na
var float   distance_in_ticks       = na
var float   distance_in_pips        = na
var bool    pip_threshold_broken    = na


// Extended version (to explain each calculation step)
if _ema50[1] < _ema13[1] and _ema200[1] < _ema13[1]
    ema_to_compare := _ema800
else if _ema50[1] < _ema13[1]
    ema_to_compare := _ema200
else
    ema_to_compare := _ema50

distance_in_ticks       := abs(ema_to_compare[1] - close[1]) / syminfo.mintick   // Distance measured on the chart
distance_in_pips        := distance_in_ticks / ticks_per_pip    // Converte that distance to pips
pip_threshold_broken    := distance_in_pips > pip_threshold     // Compare that pip-distance to the threshold distance


// Short version
// pip_threshold_broken    := (abs((_ema50[1] < _ema13[1] and _ema200[1] < _ema13[1] ? _ema800[1] : _ema50[1] < _ema13[1] ? _ema200[1] : _ema50[1]) - close[1]) / ticks_per_pip) > pip_threshold


plotshape(distance_in_ticks, "distance_in_ticks", "")
plotshape(distance_in_pips, "distance_in_pips", "")
plotshape(pip_threshold, "pip_threshold", "")
plotshape(pip_threshold_broken, "pip_threshold_broken", "")

plot(_ema13, "_ema13", color.red)
plot(_ema50, "_ema50", color.green)
plot(_ema200, "_ema200", color.blue)

// --------------------------------
// STACKOVERFLOW CODE ADDED - [END]
// --------------------------------

pip_threshold_broken := use_pip_treshold ? pip_threshold_broken : true

//Shows the condidtions for the Signal to Show BULLISH
_emaCondbull =  (open<_ema200 or open<_ema800 or (open<_ema50 or _crossed50below13ema)) and not (open>_ema200 and open>_ema800 and open>_ema50)
_bullSignal = pip_threshold_broken and _engulfing and _crossed13 and close[1] > max(open[2], close[2]) and _szCondition and _emaCondbull

//Plots the Signal if Conditions are true
plotshape(_bullSignal?close:na, style=shape.triangleup, size=size.tiny, color=#00ff00)

Upvotes: 0

Related Questions