Nick Syiek
Nick Syiek

Reputation: 55

Pinescript tradingview example needed

I am a novice at using TradingView's Pinescript and having a hard time finding an easy to understand example of a script. I am used to Java/C++ and Pinescript is very different. I am trying to build a script that will scan a stock chart and look for gaps of over 5%. Here is psuedocode for what I am trying to create:

if(difference between open of current day and previous day close > 5%) { plot a green circle or red circle, depending on if gap was up or down }

Thank you in advance!

Upvotes: 0

Views: 3722

Answers (2)

Marx Babu
Marx Babu

Reputation: 760

Pine scripting is easy to use; Initially it was bit hard to understand, Once started using it it becomes so useful to strategize the logic.

In your case you can use conditional operator as well to detect this.This will work in Version 2 .The version 3 is bit differrent

//version =2
study(title ="Experementing the code ",overlay =true ,shorttitle ="testing")  //overlay=false to get this down of the chart as seperate layout
plotchar( (close-close[1])/close[1] >0.05 ? 1:na ,char =' ',text ="plot\nTest",textcolor=red,size.huge)

Instead of if the condition you can use ?: operator to do this job.

Please make sure plotchar(.....) coming in the same line, not in separate line.

Pine has lot of cool features to use and helped me to derive my own strategy. The tutorial is really good.

Note if you don't put char='' above it will print STAR as the default character. And in the character even if you put char='testtest' it will print the only t .

Upvotes: 2

ixje
ixje

Reputation: 390

You're best bet would be to go through their tutorial

There's some odds choices in this language if you have any programming background so it's probably a good idea to read it all (it's not that much). E.g.

  • open is the current bars open price, but open[1] is the previous bar open price (so should be read as open[current_index-1])
  • you can't use the plot calls inside function bodies

as for you question (not tested, but should be close enough to give the right idea):

study(title='gap detector', overlay=true)

//plotshape(<condition>, <options>) // condition must be true to plot something

is_percentage_increase = if (close-close[1])/close[1] > 0.05
    true
plotshape(is_percentage_increase, style=shape.circle, color=green)

Upvotes: 2

Related Questions