r/pinescript • • Oct 11 '22

New to Pinescript? Looking for help/resources? START HERE

32 Upvotes

Asking for help

When asking for help, its best to structure your question in a way that avoids the XY Problem. When asking a question, you can talk about what you're trying to accomplish, before getting into the specifics of your implementation or attempt at a solution.

Examples

Hey, how do arrays work? I've tried x, y and z but that doesn't work because of a, b or c reason.

How do I write a script that triggers an alert during a SMA crossover?

How do I trigger a strategy to place an order at a specific date and time?

Pasting Code

Please try to use a site like pastebin or use code formatting on Reddit. Not doing so will probably result in less answers to your question. (as its hard to read unformatted code).

Pinescript Documentation

The documentation almost always has the answer you're looking for. However, reading documentation is an acquired skill that everyone might not have yet. That said, its recommended to at least do a quick search on the Docs page before asking

https://www.tradingview.com/pine-script-docs/en/v5/index.html

First Steps

https://www.tradingview.com/pine-script-docs/en/v5/primer/First_steps.html

If you're new to TradingView's Pinescript, the first steps section of the docs are a great place to start. Some however may find it difficult to follow documentation if they don't have programming/computer experience. In that case, its recommended to find some specific, beginner friendly tutorials.


r/pinescript • • Apr 01 '25

Please read these rules before posting

22 Upvotes

We always wanted this subreddit as a point for people helping each other when it comes to pinescript and a hub for discussing on code. Lately we are seeing increase on a lot of advertisement of invite only and protected scripts which we initially allowed but after a while it started becoming counterproductive and abusive so we felt the need the introduce rules below.

  • Please do not post with one liner titles like "Help". Instead try to explain your problem in one or two sentence in title and further details should be included in the post itself. Otherwise Your post might get deleted.
  • When you are asking for help, please use code tags properly and explain your question as clean as possible. Low effort posts might get deleted.
  • Sharing of invite only or code protected scripts are not allowed from this point on. All are free to share and talk about open source scripts.
  • Self advertising of any kind is not permitted. This place is not an advertisement hub for making money but rather helping each other when it comes to pinescript trading language.
  • Dishonest methods of communication to lead people to scammy methods may lead to your ban. Mod team has the right to decide which posts includes these based on experience. You are free to object via pm but final decision rights kept by mod team.

Thank you for reading.


r/pinescript • • 2m ago

Reproducing a closed-source TradingView indicator's 3 levels (high, mid, low) from a 4H window

1 Upvotes

I'm trying to reproduce the behavior of a closed-source TradingView indicator that draws 3 horizontal levels (high, mid, low) from a 4H window. I can read the levels from the chart for given days and timeframes, but I can't see the source. Fixed grids (Murrey Math) and standard pivot indicators did not match. Has anyone modeled this kind of rule (which bars define the window, how the mid is computed) or know open-source scripts that behave similarly? Any pointers welcome.


r/pinescript • • 5h ago

Feature Request: Expanding the 500 Future Bar Drawing Limit (xloc.bar_index) in Pine Script v6

Post image
1 Upvotes

Dear TradingView Support Team,

I am writing to submit a feature request regarding the current line and label drawing limitations in Pine Script, specifically concerning projectable drawing limits into future bars using xloc.bar_index.

Currently, Pine Script imposes a strict hard limit of 500 bars when drawing lines into the future beyond the current real-time bar (bar_index + 500).

While developing advanced long-term projection tools—such as 4-cycle Gann Boxes (e.g., 4 x 144 = 576 bars) and multi-timeframe geometric cycle models—we frequently run into this 500-bar ceiling. To prevent scripts from failing or throwing runtime rendering errors, developers currently have to implement complex custom math algorithms to dynamically calculate slopes and clip lines at bar_index + 490.

While these workarounds keep the scripts functional, they restrict full structural visualization for long-term strategic cycle analysis.

If technically feasible within your rendering pipeline and engine architecture, we kindly request the following enhancements in upcoming Pine Script updates:

  1. Increased Future Bar Projection Limit: Expanding the future bar drawing limit beyond 500 bars (e.g., to 1,000 bars or higher, especially for premium/pro tier users).
  2. Dynamic Limit Option: Providing a configurable parameter or native function in line.new() that allows drawing to project uninterruptedly across long timeframes without requiring manual line-clipping logic.

Thank you for your continuous dedication to improving Pine Script and supporting the developer community.


r/pinescript • • 13h ago

Whats this supposed to mean?

Post image
1 Upvotes

r/pinescript • • 1d ago

ta.* calls inside if blocks, ternaries and v6 "and"/"or": why the result drifts, and the CW10003 warning

2 Upvotes

If you've ever seen "The function ... should be called on each calculation for consistency" and ignored it, this is what it's actually warning about.

Local scopes don't get a history entry on every bar

Pine commits a value to a series' historical buffer only when the scope containing it is evaluated on the bar's closing tick. Global variables get a new entry on every bar, so x[1] always means "one bar ago". Code inside an if, a loop, a function body, or the unused branch of a ternary may not run on a given bar, and then nothing gets committed for that bar.

Per the docs (Execution model > Time series in scopes), [] on a local series therefore means "last committed value as of N bars back", not "value from N bars back". If the scope last ran 10 bars ago, localVar[1] through localVar[9] all return that 10-bar-old value.

Function parameters count as local too: each call site has its own buffer for its arguments, even when you pass a global like close.

Same thing applies to built-ins

Anything in ta.* that looks at past values internally (ta.sma, ta.rsi, ta.ema, ta.crossover, ...) keeps its own history of the source you pass. Call ta.sma(close, 20) only on bars where some condition is true and you get an average of the last 20 calls, not the last 20 bars. It compiles fine and looks plausible on the chart, which is why it's easy to miss.

The v6 trap: lazy and / or

In v5, and/or evaluated both operands every time. In v6 they short-circuit. The migration guide's own example: if close > open and ta.rsi(close, 14) > 50 gave correct RSI values in v5, but in v6 ta.rsi is skipped whenever close > open is false, so its internal history has gaps and the values are wrong. A script ported from v5 can change behavior with no compile error. The ternary ?: was already lazy in v5, so cond ? ta.sma(close, 20) : na has the same problem in both versions.

The fix is boring

Evaluate history-dependent calls once per bar in the global scope, store the result, and use the variable in your conditions:

  • rsi = ta.rsi(close, 14) then if close > open and rsi > 50
  • sma = ta.sma(close, 20) then cond ? sma : na
  • Same for your own functions that use [] on a parameter.

Lazy evaluation is still useful where you want to skip work, e.g. array.size(a) > 0 and array.first(a) > 0 no longer throws on an empty array in v6. The rule of thumb is just: guards can be lazy, anything with memory can't.

A couple of edge cases I'd like to hear how people handle: loops that call a history-dependent function per iteration (one buffer per call site, so iterations overwrite each other), and switch arms that each call a different ta.* function. Do you precompute every variant globally, or restructure some other way?


r/pinescript • • 1d ago

Go back!

Thumbnail reddit.com
1 Upvotes

I pay good money for Trading View. The recent updates to the Phone app are terrible. The timeframe and stock swipe left and right are a terrible idea. Also, the horizontal screen size got smaller. It wasn’t broken, are you just looking for reasons to screw it up? Go back or tell me how to get the older version back.


r/pinescript • • 1d ago

Sonar battery squeeze indicator freebie

Thumbnail
gallery
11 Upvotes

r/pinescript • • 1d ago

Go back!

Thumbnail reddit.com
1 Upvotes

r/pinescript • • 1d ago

An Open Letter to Management: Forcing Free Users Out is a Terrible Strategy

Thumbnail reddit.com
1 Upvotes

r/pinescript • • 2d ago

Hundreds of indicators were contributed by the community free of charge

Thumbnail reddit.com
3 Upvotes

r/pinescript • • 2d ago

So no one has a cracked version of Trading view 😕

0 Upvotes

r/pinescript • • 2d ago

Wow wtf. New Low for Tradingview. New restriction this morning

Post image
10 Upvotes

r/pinescript • • 2d ago

Tradingview has HORRIBLE CUSTOMER SERVICE communications.

Thumbnail reddit.com
1 Upvotes

r/pinescript • • 2d ago

Subject: Extremely Disappointed with the New 30 Drawing Limit on the Free Plan Dear TradingView Team, I am extremely disappointed with the new 30-drawing limit on the Free Plan. TradingView has been an essential platform for my daily market analysis. I use multiple drawings to mark important mark

0 Upvotes

r/pinescript • • 2d ago

The settings I check before I believe a Pine backtest (including the July strategy changes)

1 Upvotes

I used to trust the strategy report a lot more than I should have. [One line of your own here: the strategy or setting that fooled you.]

These days I go through the same short list before I get excited about any result. Posting it in case it saves someone a bad week, and because I'd like to know what I'm still missing.

Fees first. Commission and slippage are zero unless you set them, and for anything that trades often that's the whole result. I set both, then run it again at double the commission. If it only works at the cheap number, it doesn't work.

Then fills. By default your order fills at the next bar's open, which is honest. Filling on the same bar's close looks better and is a price you mostly couldn't have had. Since July this lives under "Order execution delay" in Properties; process_orders_on_close still sets the default.

History and live don't run the same way. On past bars your script runs once per bar; live, with calc_on_every_tick, it runs on every tick. That gap is where a lot of "it worked in the tester" comes from. The new calc_on_every_history_tick closes some of it if you're on Premium or Ultimate.

Watch request.security(). Higher-timeframe data with lookahead on and no offset hands your script tomorrow's close. If you use lookahead_on, offset by one bar:

htfClose = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)

Heikin Ashi will lie to you. Orders fill at prices that never traded. Test on a standard chart, or set fill_orders_on_standard_ohlc=true (now "Heikin Ashi mode").

And size. 100% of equity with compounding makes almost any curve look steep. I start with a small fixed fraction so I'm looking at the strategy and not the leverage.

Where I start from:

//@version=6

strategy("Honest defaults", overlay=true,

initial_capital=10000,

default_qty_type=strategy.percent_of_equity,

default_qty_value=10,

commission_type=strategy.commission.percent,

commission_value=0.05,

slippage=2,

process_orders_on_close=false,

calc_on_every_tick=false,

use_bar_magnifier=true,

fill_orders_on_standard_ohlc=true)

None of this proves a strategy is good. It just stops the settings from flattering it.

The one I still don't have a clean answer for is walk-forward testing, since Pine can't hold data back. How are you all handling that?


r/pinescript • • 2d ago

Why does a good 1D strategy fall apart on 1m or 5m?

1 Upvotes

A lot of the time the logic isn't the problem. The costs are.

A simple way to see it is to compare a typical move with what one trade costs you.

On my BTC chart, ATR(14) on the daily is about 3% of price. On the 1m it's about 0.08%. If a round trip costs around 0.1% in fees and slippage, that's a small part of a daily move, but more than an entire typical 1m move.

So the same rules that capture, say, a fifth of the typical move can be profitable on the daily and below zero on the 1m. That's also why so many low-timeframe backtests look great with zero costs and go flat or negative once you add them.

Before tuning anything on a low timeframe, I'd check how big the average move is compared to the costs. If costs eat a big part of it, more optimization won't fix it.


r/pinescript • • 3d ago

Session and time filters in Pine: exchange time zone vs chart time zone, and why "UTC-5" drifts twice a year

2 Upvotes

A common source of "my session filter is off by an hour" reports is mixing up the three time contexts Pine actually uses.

1. Timestamps are zone-agnostic. time, time_close and the values returned by time() are UNIX milliseconds since 1970-01-01 UTC. They don't carry a time zone; the zone only matters when you turn them into calendar fields or text.

2. Calendar variables use the exchange time zone. hour, dayofweek, month etc. are always expressed in syminfo.timezone, and every time function with a timezone parameter defaults to that same exchange zone.

3. The chart time zone is invisible to the script. The "Timezone" setting on the chart only changes how the axis and Pine Logs prefixes display time. Scripts can't read it. So if you switch the chart to your local zone and your hour == 9 filter "moves", it didn't move; it was never in your zone. If you want users to think in their own zone, expose a timezone input and pass it explicitly.

Session strings

time(timeframe.period, "0930-1600:23456", tz) returns a timestamp only for bars inside the session and na outside it, so not na(time(...)) is the usual in-session test. The timezone argument defines the zone of the session string itself (the returned timestamp is still plain UTC ms). Leave it out and the session is read in the exchange zone, which is easy to forget when you're copying a "London open" window onto a crypto or US symbol.

UTC offsets vs IANA names

"UTC-4" / "GMT+0100" are fixed offsets. "America/New_York" / "Europe/London" follow DST and historical rule changes. The docs show it directly: hour(time, "UTC-4") matches New York only during DST and is off by one hour the rest of the year, while hour(time, "America/New_York") is always right. For anything defined by a real market's local clock, IANA names are the safer default. Also note that two regions don't switch DST on the same date, so the offset between, say, New York and Sydney changes more than once a year.

Overnight sessions

For symbols whose session spans two calendar days (FX is the docs' example: the "Monday" EURUSD session starts Sunday 17:00 exchange time), dayofweek on early bars still says Sunday. time_tradingday gives the 00:00 UTC timestamp of the trading day the session belongs to, which is usually what you want for "one trade per day" or daily resets. For "new day / new month" detection, timeframe.change("1D") or ta.change(time("1D")) > 0 tends to be cleaner than comparing calendar fields.

Quick checklist when a time filter looks wrong:

  • Which zone is the session string in? (Explicit timezone, or exchange zone by default?)
  • Fixed offset or IANA name?
  • Is the chart zone fooling your eyes?
  • Does the session cross midnight?

Curious how people handle multi-market session windows (e.g. London + NY overlap) on 24/7 crypto symbols: one time() call per window with explicit IANA zones, or something else?


r/pinescript • • 2d ago

Highest profit in one day only from NSE Cash Market

1 Upvotes

My highest profit in one day only from NSE Cash Market. The actual broker gross profit is slightly (around ₹100 less).


r/pinescript • • 3d ago

Script to put broker account summary on bottom bar instead so it can be visible at all times

Thumbnail
reddit.com
1 Upvotes

r/pinescript • • 3d ago

Indicator I use as an alarm clock

Thumbnail
tradingview.com
1 Upvotes

r/pinescript • • 3d ago

Selling indicators?

Thumbnail reddit.com
2 Upvotes

r/pinescript • • 4d ago

tradingview在错误的道路上越走越远了

1 Upvotes

作为一个运行了这么多年的专业交易平台,不去思考如何让更多专业交易员选择平台,相反,却将更多潜在的优质客户推的更远——我这里指的不仅仅是Basic用户,更多的是各类型的付费用户。
数据的及时性、指标数量和运行速度及对开发者的限制、UI的稀烂设计......太多问题了。我都怀疑他们目前的内部运营是不是出了问题?为什么让不做交易或者不懂交易的人,负责这些决策?