Skip to content
QuantWright

Your own code

Bring a Pine Script strategy from TradingView, or run your own backtesting.py code, instead of describing the strategy in words.


Pasting Pine Script

Paste a Pine Script strategy into the chat, on its own or after /pine. If the script does not name the market, say it in the same message:

ExampleRun this on QQQ 15m. (then the script)

Scripts can be up to 12,000 characters. The AI translates the script, statement by statement, into a Python strategy, and that strategy runs on the same engine, data and costs as every other backtest here. Pine’s built-in indicators are computed the way Pine computes them. Nothing is added: no stop, target or time exit your script does not have. A card first shows what each strategy() setting becomes on this run, then the backtest runs. If you only want that reading, say “do not run it” in the same message; nothing is translated or run.

  • Commission and slippage come from the script’s strategy() settings. A percent commission is charged on the entry and on the exit, as TradingView charges it. A commission in cash per order or per contract is converted at the tested window’s median price, and the card says it is an approximation. Slippage in ticks is charged as a price on every fill. A cost you name in your message replaces the script’s, and a cost the script does not set is the platform default, named on the card.
  • The size of each order follows default_qty_type and default_qty_value (percent of equity, a fixed quantity or a cash amount), in whole shares and contracts. initial_capital is the run’s starting account; without it the run starts with the account size you stated, or $100,000.
  • pyramiding is applied: the run holds up to that many entries in one direction, as TradingView does. margin_long and margin_short become leverage, with one margin for both sides (the lower leverage of the sides your script trades) and at most what each market lends; on a future the engine’s own margin applies whatever the script sets. Settings this engine cannot apply are listed, never silently changed: process_orders_on_close (orders fill at the next bar’s open here) and the bar magnifier, and any margin that is not applied says so.
  • Pine takes the market and bar size from the chart. Name them in the same message; otherwise the run uses the chat’s last market, or SPY daily bars, and a note says which.
  • The pasted source is also checked for repainting, look-ahead through request.security() and deprecated calls, and anything found is listed with the result.
  • The translation is checked against your script, statement by statement, before it runs. A mismatch is corrected once and shown to you before anything runs; one that remains is named on the result.

When part of a script cannot be translated

When the translation cannot do part of your script faithfully, as with request.security() on another symbol or tick-level logic, it names that part before anything runs and waits for you. Send /params go to run it anyway: the numbers are shown for what the translation does, and the result is not scored, because it is not your script. Change that part of the script, or describe it in plain English, for a run that is.

The card also lists how this engine differs from TradingView’s: a stop or limit that price only touches does not fill here, and when a stop and a target are both inside one bar the stop comes first.

Exporting a strategy

/code exports the current strategy as Pine Script, NinjaScript or EasyLanguage. /code python returns the exact Python that ran the last backtest.

Running your own Python

In a chat that already has a strategy, open the Code tab, choose Edit, change the code or replace it with your own, and press Run edited code. It runs as you sent it, on the symbol and bar size of the chat’s last result, with the account size you stated (or $100,000), whole units and the same costs as any other run.

If the engine rejects the code while running it, a repair round may rewrite it. The result then says so plainly: the numbers belong to the repaired code shown in the Code tab, not to the text you sent.

Code pasted into a chat message is not run as it is: a message is read as a description, and the AI writes the code afresh. When that happens the result says so. To run your file exactly, use the Code tab.

What the sandbox accepts

  • Exactly one strategy class for backtesting.py: class MyStrategy(Strategy):.
  • Imports from numpy, pandas, math, backtesting, ta and qw_features only.
  • At the top level of the file: imports, plain helper functions, assignments and the one class. An if __name__ == "__main__": block is removed.
  • No eval, exec, compile, open, __import__, globals or locals, and no file or network access.
  • No attributes whose names start with an underscore: use self.entry_bar, not self._entry_bar.
  • Printed output is not shown. Put what you want to see in the strategy’s trades or parameters.

Code that plainly breaks one of these rules is stopped before it runs. The reply starts “Code failed the sandbox pre-check:” and names each problem, such as an import that is not on the list.

Why a translated or exported strategy can give different numbers on another platform: FAQ.

Not answered here? Write to us through the Support page.