1

Trying to write a pine script for showing the 10ema crossing above the 21ema within the past 3 days. If a script already exist for this or is close to this please provide links.

Below is the script I wrote which did not work.

//@version=5
indicator("EMA Cross", overlay=true)

length1 = 10
length2 = 21

ema1 = ta.ema(close, length1)
ema2 = ta.ema(close, length2)

crossedAbove = ta.crossover(ema1, ema2)

// Check if crossover occurred within the past 3 days
within3Days = ta.change(crossedAbove) < 3

if crossedAbove and within3Days
    label.new(x = bar_index, y = close, text = "EMA Cross", style = label.style_label_up, color = color.green)
James Z
  • 12,209
  • 10
  • 24
  • 44
GeorgeIII
  • 11
  • 1

1 Answers1

0

You need to switch your ta.change to ta.barssince

Assuming you want the label to only show if its within 3 days of the cross, you will want to create label variable and then use the label delete function if it's been more than 3 days.

//@version=5 
indicator("EMA Cross", overlay=true)

length1 = 10 
length2 = 21

ema1 = ta.ema(close, length1) 
ema2 = ta.ema(close, length2)

crossedAbove = ta.crossover(ema1, ema2)

within3Days = ta.barssince(crossedAbove) 

var label crossLabel = na
if crossedAbove and within3Days < 3
    crossLabel := label.new(x = bar_index, y = close, text = "EMA Cross", style = label.style_label_up, color = color.green)
else if within3Days > 3
    crossLabel.delete()
smashapalooza
  • 932
  • 2
  • 2
  • 12
  • The new script worked but the results were a bit off. When I ran it in the stock screener it gave results of the 10ema crossing the 21ema more than 3 days ago. Some up to 3 months. I just want it to show results if they crossed within the past 3 trading days. – GeorgeIII Jul 11 '23 at 02:37
  • Interesting I didn’t have any weird results – smashapalooza Jul 11 '23 at 03:48
  • When I copy the script into pine script and then click on screener I get 44 matches but only one stock actually meets the criteria. Can you copy the script into pine script adn run the screener and see how many matches you get and how many actually give the green ema cross on the chart. – GeorgeIII Jul 12 '23 at 02:24
  • you cant screen based on pine so I'm not sure what you're actually looking at – smashapalooza Jul 12 '23 at 14:05
  • I wanted to screen for stocks that meet the criteria for the pine script you helped me with. But, what I hear you saying is that you cant write your own screens? Is that correct? For example: When I subscribes to StockCharts.com I could write the script above in syntax and it would generate a list of stocks that met that criteria. – GeorgeIII Jul 13 '23 at 01:17
  • Correct you can not write your own screens based on that – smashapalooza Jul 13 '23 at 03:18