You have a Pine Script strategy that backtests well on TradingView. The problem: TradingView charts, it doesn't trade. To actually automate it, you need the logic running somewhere that places orders — like HaasScript inside HaasOnline TradeServer. Here's what moves over cleanly, what needs reworking, and what you should leave on TradingView entirely.

Let's set expectations up front: converting Pine Script to HaasScript is a translation, not a copy-paste. Both are purpose-built languages for trading strategies, so the concepts line up — moving averages are moving averages, RSI is RSI. But they make different assumptions about when your code runs and how price history is stored, and those two differences are where naive line-by-line conversions quietly break.

This guide walks the real mechanics: the three mental-model shifts, a worked RSI example side by side, a function map you can keep open in another tab, and an honest breakdown of what doesn't convert. HaasScript is HaasOnline's Lua-based scripting language — case-sensitive, with 750+ built-in commands — and it's the engine behind every bot in HaasOnline TradeServer.

First: do you even need to convert?

Two paths get a TradingView strategy trading on HaasOnline, and one of them skips conversion entirely.

  1. Keep the Pine script on TradingView, pipe the signal in. Your indicator stays where it is. You attach a TradingView alert to it and route that alert into a HaasOnline signal bot, which does the actual order placement. Nothing gets rewritten. This is the right call when the strategy is heavily tuned to TradingView, uses features HaasScript doesn't have (more on those below), or you just don't want to maintain a second copy of the logic.
  2. Rebuild the logic natively in HaasScript. You translate the strategy into a HaasScript bot that runs entirely inside HaasOnline TradeServer. This is the right call when you want everything in one place — backtesting, execution, and risk management on the same platform — without a dependency on TradingView alerts firing on time.

If you only need the second path some of the time, that's normal. The rest of this guide is about doing the native rebuild well — but keep path one in your back pocket. Sometimes the honest answer is "don't convert this one."

The three shifts that break copy-paste

Almost every conversion bug traces back to one of these three differences. Internalize them and the rest is mostly vocabulary.

1. Series and indexing — history works differently

In Pine, every variable is implicitly a series. close is the current bar's close, close[1] is the previous bar, and that history is carried for you automatically, every bar, with no setup.

HaasScript hands you a HaasNumberCollection instead — an array of values, sorted newest first. You fetch it with a command, then read into it:

local prices = ClosePrices()      -- a collection, newest first
local currentClose  = prices.Value          -- current bar
local currentClose2 = ArrayIndex(prices, 0)  -- same thing, current bar
local previousClose = ArrayIndex(prices, 1)  -- one bar ago (Pine's close[1])

Here's the gotcha that trips up everyone coming from Pine: there are two indexing conventions in play. The ArrayIndex() accessor uses "bars ago" — index 0 is the current bar, 1 is one bar back, just like Pine's [1] but written differently. But any plain Lua table you build is 1-based, and reading index 0 on it throws an error. So ArrayIndex(prices, 0) is valid and means "now," while myTable[0] is a bug. Keep those straight and most indexing errors disappear.

One more: Pine's persistent var variables (values that survive from bar to bar) map to HaasScript's Save() and Load() — you explicitly stash a value and read it back on the next execution.

2. Execution — per-tick, not per-bar-close

This is the single biggest source of "why did my bot fire fifteen times?" confusion.

A Pine strategy is evaluated once per bar (and by default, signals act on the bar's close). A HaasScript bot, by default, runs on every price tick — potentially many times per minute. If you translate a crossover condition literally and wire it straight to DoLong(), an indicator that wobbles across your threshold intra-bar can fire repeatedly before the candle even closes.

Two fixes, both native:

  • Use a once-per-bar guard so an action can only happen once per candle — commands like OrderOncePerBar() / TradeOncePerBar() exist for exactly this.
  • Gate your entry logic on the candle interval so the comparison only evaluates on a closed bar, mirroring Pine's default behavior.

If you remember nothing else from this guide, remember this: Pine assumes the bar is closed; HaasScript assumes it isn't. Decide intra-bar vs. on-close behavior deliberately for every signal.

3. Orders — strategy.* becomes managed trading

Pine's strategy.entry / strategy.close / strategy.exit map to HaasScript's managed trading commands, which handle position tracking and order validation for you:

TradingView Pine HaasScript (managed)
strategy.entry("L", strategy.long) DoLong()
strategy.entry("S", strategy.short) DoShort()
strategy.close / strategy.exit DoExitPosition()
stop / take-profit params StopLoss(%) / TakeProfit(%) / TrailingStopLoss(%)

Managed trading covers the large majority of strategies. When you need fine-grained control — exact order prices, multi-leg routing, custom scaling ladders — HaasScript has an unmanaged mode where you place and track every order yourself. That's the equivalent of dropping into Pine's lower-level order plumbing, and you reach for it for the same reasons.

A worked example: RSI reversal, side by side

Here's a compact Pine v5 strategy — go long when RSI crosses up through oversold, close when it crosses down through overbought.

//@version=5
strategy("RSI Reversal", overlay=false)
length    = input.int(14, "RSI Length")
buyLevel  = input.int(30, "Oversold")
sellLevel = input.int(70, "Overbought")

r = ta.rsi(close, length)

if ta.crossover(r, buyLevel)
    strategy.entry("Long", strategy.long)
if ta.crossunder(r, sellLevel)
    strategy.close("Long")

And the same strategy in HaasScript, using managed trading and a once-per-bar guard to respect the execution-model difference:

-- RSI Reversal (HaasScript, managed)
local length    = Input("RSI Length", 14)
local buyLevel  = Input("Oversold", 30)
local sellLevel = Input("Overbought", 70)

local r     = RSI(ClosePrices(), length)
local rNow  = ArrayIndex(r, 0)   -- current bar
local rPrev = ArrayIndex(r, 1)   -- one bar ago

TradeOncePerBar()  -- don't re-fire intra-bar

if rPrev <= buyLevel and rNow > buyLevel then
    DoLong("RSI crossed up through oversold")
elseif rPrev >= sellLevel and rNow < sellLevel then
    DoExitPosition("RSI crossed down through overbought")
end

StopLoss(3.0)  -- a safety Pine's strategy() had to spell out manually

Notice what changed and why. input.int became Input. ta.rsi(close, length) became RSI(ClosePrices(), length). ta.crossover — which Pine gives you as a one-liner — became an explicit "previous bar was below, current bar is above" comparison, because spelling it out makes the bar-to-bar logic visible and keeps you honest about the per-tick execution model. (HaasScript also ships a built-in crossover signal helper if you'd rather not write the comparison by hand.) And the once-per-bar guard plus a stop-loss are doing the safety work that lives implicitly in Pine's strategy() engine.

That's the whole shape of a conversion: same intent, re-expressed against HaasScript's series model, execution model, and order commands.

The function map

The pieces that translate one-to-one. Keep this open while you work — exact command signatures evolve, so when in doubt, check the in-app command reference and let the compiler's error messages guide the fixes.

TradingView Pine HaasScript Notes
close / open / high / lowClosePrices() / OpenPrices() / HighPrices() / LowPrices()Collections, newest first
close[1]ArrayIndex(ClosePrices(), 1)0 = current bar
ta.sma / ta.emaSMA() / EMA()Direct
ta.rsiRSI().Value = current
ta.macdMACD().Value / .Signal / .Histogram
ta.bb / bollingerBBANDS().upper / .middle / .lower
ta.atr / ta.stoch / ta.cciATR() / STOCH() / CCI()Direct
ta.crossover / ta.crossunderprev-vs-now compare, or built-in cross signal helperMind per-tick firing
ta.highest / ta.lowestGetHigh() / GetLow()Use offset=1 to exclude current bar
input.*Input(name, default)Direct
var (persistent)Save() / Load()Explicit state
plot / plotshape / hlinePlot() / PlotShape() / PlotHorizontalLine()Charting
print debugLog()Not Lua's print()

What converts cleanly — and what won't

The honest breakdown. Based on mapping HaasScript's command surface against the way retail Pine strategies are actually built, strategies fall into three buckets.

Converts cleanly — often an afternoon, sometimes minutes. Single-timeframe indicator strategies: moving-average crossovers, RSI / MACD / Bollinger / stochastic reversals, Donchian and channel breakouts, candlestick-pattern triggers. If your script reads price and a handful of standard indicators and acts on simple conditions, it ports with little friction.

Needs rework — budget real time. Anything leaning on Pine convenience functions that HaasScript has no direct equivalent for: ta.valuewhen() and ta.barssince() (you reimplement these with a loop or a Save/Load counter), pyramiding and custom scaling ladders, and strategies that depend on exact bar-close timing. The logic is portable; it just isn't a one-liner anymore.

Better left on TradingView — pipe the signal in instead. Multi-timeframe strategies built on request.security() (HaasScript evaluates one interval per execution and has no inline equivalent), and anything heavy on arrays, matrices, maps, or machine-learning-style data manipulation. If a script lives or dies by a Pine feature with no HaasScript analog, don't fight it — keep it on TradingView and route its alert into a HaasOnline bot.

Roughly speaking, expect about half of typical retail strategies to convert with little fuss, a bit under half to need genuine reworking, and a slice to be better off staying on TradingView. Knowing which bucket you're in before you start saves the most time.

The shortcut — and its one rule

You don't have to do the translation by hand. HaasOnline's in-app assistant, Thomas, can take a Pine script and produce a first-pass HaasScript draft — useful for getting past the blank page, especially on the "converts cleanly" bucket. Treat its output as a draft, not a finished bot: read it, confirm the commands exist, and check the per-tick and indexing details this guide flagged.

Which leads to the one rule that matters more than any mapping table:

A converted strategy that compiles is not a converted strategy that works. Because the execution model and history handling differ, a script can translate without errors and still behave differently than it did on TradingView — firing at different moments, sizing differently, reading a slightly different value for "the previous bar." The only way to know is to backtest it on HaasOnline before you commit real capital, and compare the behavior against what you saw on TradingView. Convert, backtest, then go live.

Bottom line

Pine Script and HaasScript are closer cousins than they look — the indicators and the strategy concepts line up. The work is in three places: history and indexing, per-tick versus per-bar execution, and order commands. Get those right and most TradingView strategies move over. For the ones that don't, you've still got the alert-passthrough path, so nothing strands you on the wrong platform.

Past performance is not indicative of future results, and only deploy capital you can afford to have at risk — a strategy that backtested well on TradingView carries exactly the same caveat after it's running in HaasScript.

Want to see the full command set before you start? The HaasScript page covers the language and its three build modes. When you're ready to translate a strategy and backtest it end to end, you can start a free 7-day trial and run it against the rest of the HaasOnline platform.