Search code examples
pine-scriptpine-script-v5lookback

How can I scan last 5 days for a function what is the code for PineScript Tradingview?


Hello everyone I just can't fix a problem. I have signal code like that:

crossingrule = ta.crossover(line1,line2 ) or ta.crossunder (line1, line2) ? color.lime :color.orange

but when I check this on the screen. It gives me only the last bar's condition.

I want this code to scan last 5 days or like 2 days. how can I add a code what should I write and where)?

I found a code like barssince and it works but I don't want bars to be counted I want a specific time like "last 5 days". not "last 5 bars"


Solution

  • ta.crossover(Buy1,buy2) returns True if:

    • on the previous candle, the value of the Series Buy1 was lower than the value of the Series buy2
    • and on the current candle, the value of the Series Buy1 is higher than the value of the Series buy2

    This means the value of Buy1 has crossed over the value of buy2.

    Extending upon your comment I suggest a mechanical approach (especially if you are new to Pine). To check for crossover on the current day, you'd use ta.crossover(Buy1,buy2) as above. To check if a crossover happened a day ago (provided you are on the Daily chart), you can do either:

    - ta.crossover(Buy1[1],buy2[1]) OR
    - ta.crossover(Buy1,buy2)[1] // I suggest this one
    

    The crossover two days ago will be ta.crossover(Buy1,buy2)[2] and you get the idea for the rest. You can chain together a large condition or you can show the last 5 days on a row in your table.