HorizonAI App

Export a Python strategy

Take a finished Python strategy to the platform you actually trade on

Python mode is where you build and test; it isn't where you trade. When a strategy holds up, the Export button in the chat header rewrites it as Pine Script, MQL5 or NinjaScript and opens the result in a new chat. Your Python thread — its code, its chart and its backtest — stays exactly as it was.

Export is one-way. Python is a source, never a target: you can export out of Python to all three platforms, and you can convert between Pine, MQL5 and NinjaScript in any direction — but nothing converts into Python. Build in Python, leave once.

What it is

Export runs the same conversion engine as Convert Pine Script to MT5, pointed at Python as the source. The assistant reads your strategy — the pandas maths, the drawing calls, the set_long_entry / set_short_entry signals — and writes the equivalent in the target language, then puts the result through that language's checker.

Because it mints a new thread rather than replacing the current one, an export is disposable. If it comes out wrong, throw it away and export again; the Python original is never touched.

1) Finish the strategy in Python first

Export what you've already proven. Get the chart looking right and, if it trades, run it in the Backtest tab until you're happy. A conversion inherits whatever logic you hand it — exporting a half-finished idea just moves the half-finished idea somewhere slower to iterate.

2) Click Export in the chat header

The Export button sits in the chat header and only appears on Python threads. Click it and you get three targets:

  • Pine Script — TradingView
  • MQL5 — MetaTrader 5
  • NinjaScript — NinjaTrader 8

Pick one. A toast appears while it works; a full conversion can take a minute, so don't refresh.

3) You land in a new chat

When it finishes you're dropped into the converted script in a fresh thread, in the target language, with the Code panel already showing it. Everything that language supports is now available on that thread — the Pine and MQL5 backtesters, the Visual Flow diagram, and the ordinary edit loop.

You will now see two entries in My Scripts: the Python original and the export. That's intended — keep the Python one as the master copy and re-export whenever you change the logic, rather than editing the export and the original in parallel.

4) How far to trust the result

This is the honest bit, and it differs by target.

MQL5 and NinjaScript are compiled for real. Before an export comes back, the code is sent to an actual MetaTrader 5 compiler and an actual NinjaTrader compiler, and the assistant fixes what they reject. If it lands, it compiles.

Pine Script is not. There is no TradingView compiler we can call — TradingView does not offer one. A Pine export is checked by Horizon's own Pine parser, which catches syntax and a good deal of semantics, but it is not TradingView's compiler and it is not the last word.

Always verify a Pine Script export on TradingView before you trust it. Paste it into the Pine Editor, add it to a chart, and confirm it compiles and plots what you expect. Horizon's parser is a strong first pass, not a substitute for TradingView itself. See Run Strategies on TradingView.

5) Check the logic survived the trip

Compiling is not the same as being correct. The three languages differ in ways that don't always raise an error:

  • Bar indexing and lookback. Python's .shift(1) and Pine's [1] mean the same thing; a mistake here shifts every signal by a bar and shows up as different entry times, not as an error.
  • Warm-up. A 200-period average has no value on the first 199 bars. The platforms handle that gap differently.
  • Volume. If your Python ran on a forex pair, volume was empty — a volume-based rule may have been silently doing nothing before the export, and may start doing something after it.
  • Order handling. The Python engine is one-position-at-a-time at bar close with a flat 0.1% commission. Real MT5 and TradingView backtests apply their own fill rules, spreads and properties, so the numbers will not match. Compare shapes, not totals.

The quickest sanity check is to backtest the export on its own platform and see whether the trade count and the direction of the equity curve resemble the Python run.

The one thing Pine genuinely can't do

Python mode lets a single script draw on the price chart and in one or more named lower panes at the same time — an EMA over the candles with an RSI underneath, in one file. Pine Script cannot: a script is either an overlay on price or a single lower pane, not both.

So when a Python script uses both, the Pine export has to choose one and the other set of drawings can't come across as-is. If your Python chart has a price overlay plus an oscillator pane and you want both on TradingView, plan on two Pine scripts — export, then ask in the new thread for the other half as a separate script. MQL5 and NinjaScript don't have this restriction.

One more Pine-specific behaviour worth knowing: whether the export comes out as a TradingView indicator or strategy is decided by whether your Python declared entries. Drawing-only Python becomes an indicator(); anything with set_long_entry / set_short_entry becomes a strategy() that can be run in TradingView's Strategy Tester.

Limits, defaults & fine print

  • Free on every plan. Export isn't plan-gated. Like any other chat turn it's metered by tokens, so a heavy export draws on your allowance the same way a long conversation does.
  • Python threads only. The button doesn't appear on Pine, MQL5 or NinjaScript threads — those convert between each other from the Convert page instead.
  • Non-destructive. The Python thread's code, chart and backtest are never modified.
  • One target per export. Want all three? Export three times from the same Python thread.
  • Not a round trip. There is no import back into Python. If you need to change the strategy, change the Python original and export again.
  • It can take a minute. Conversion runs the full assistant loop including compilation, not a text substitution.

Troubleshooting

  • "There is no script to export yet" — the editor is empty. Ask the assistant to write the strategy first.
  • "That export didn't come back as Pine Script" (or MQL5/NinjaScript) — the conversion finished but the result wasn't recognisably the target language. Just export again; it's a retry, and nothing was lost.
  • "Export failed. Please try again." — the conversion run errored out. Retry; if it keeps failing, simplify the Python (very long scripts with unusual pandas gymnastics are the usual cause).
  • "Export timed out. Please try again." — the run never reported back. The Python original is untouched, so retrying is safe.
  • No Export button in the header — you're not on a Python thread. Check the language shown in the chat.
  • The Pine export compiles but plots nothing — usually a strategy-versus-indicator mismatch or a warm-up gap. Ask in the new thread: "this plots nothing on TradingView, fix it."