# Turn your rules into a graph and backtest them automatically

> Build a trading graph, assign it to a strategy and run it over many markets and timeframes with the auto backtest.

Source: https://quantraider.com/docs/build-a-graph-and-backtest-it-automatically

To backtest your rules automatically, build them as a trading graph that ends in a trade entry, assign the graph to a strategy and run it from the **Auto backtest** tab over the markets and dates you choose. The app previews the trades first and saves them only when you choose **Generate trades**.

> Not financial advice. Trading involves risk of loss. Backtest results are hypothetical and do not guarantee future performance.

## Who this is for and what you get

This guide is for you if your idea can be written as precise conditions, for example "the close is above a moving average". You end up with a saved graph and a large set of saved trades over many markets and timeframes, ready for the **Analysis** tab. If you are not sure the idea is worth automating, start with [Test a trading idea by hand](/docs/test-a-trading-idea-by-hand).

## Before you start

- A strategy. See [Your first strategy in 10 minutes](/docs/first-strategy).
- A plan that includes **Automated backtesting**. Without it, the **Auto backtest** tab tells you so and links to the plans. See [Plans and billing](/docs/plans-and-billing).
- Your rule written as conditions that a computer can check on each closed candle.

## 1. Create a trading graph

1. Choose **Graph editor** in the sidebar, or open the **Graph** tab of your strategy.
2. Choose **Create graph**.
3. Set **Graph type** to **Trading graph**. Only trading graphs can contain a trade entry.
4. Enter a **Graph name** and choose **Create graph**.

![The graph library with the Create graph button, a search field and the lists of indicator and trading graphs](/img/docs/graph-library.png "The graph library on the Graph tab.")

1. **Create graph** starts a new graph.
2. Search your graphs by name or description.
3. **Indicator graphs** are reusable indicators. Execution nodes are not allowed in them.
4. **Trading graphs** hold trading logic. Execution nodes are allowed.
5. Open, edit, publish or delete a graph.

## 2. Build the logic

Drag nodes from the **Node library** onto the canvas and connect an output socket to an input socket. A simple graph has this shape:

1. A **Data** node gives you the candle: Open, Close, High, Low and more.
2. An indicator node such as **EMA** takes the Data node as input.
3. A **GreaterThan** node compares the Close with the EMA and outputs true or false. Combine several conditions with **And** and **Or**.
4. A **TradeEntry** node receives that boolean on **Trigger**. Set its **Direction** to **Buy** or **Sell**, connect an **Entry Price** and use **Add** to create a named stop loss pin and a named take profit pin, each with a price.

Use **Visualize** nodes to draw lines or arrows on the chart so you can see what the graph does. They do not change trades. The nodes are listed in the [Node reference](/docs/graph-nodes). If you would rather describe the rule in words, the [graph assistant](/docs/graph-assistant) proposes a graph for you to review, when your plan and server include it.

Choose **Save**. A dot on the button means there are unsaved changes.

![The graph editor with connected nodes on the canvas and the node library on the right](/img/docs/graph-editor.png "The graph editor with a trading graph open.")

1. The name and kind of the graph.
2. Undo and redo, **Clean up**, and the view and file buttons.
3. **Assistant** opens the graph assistant.
4. **Save** stores the graph.
5. The **Node library**: search it, or switch between **Nodes**, **My graphs** and **Published**.
6. The node categories. Drag a node onto the canvas, or click it to add it.
7. A node on the canvas. Lines connect the output of one node to the input of another.

## 3. Assign the graph to the strategy

1. Open the strategy and choose the **Edit** tab.
2. Under **Graphs**, choose **Assign graphs** and tick your graph.
3. Under **Name and components**, add the stop losses and take profits you want to compare. Next to each, choose **Map**, pick the graph and click the matching node in it.

After you change the graph later, choose **Sync graph items** next to it.

## 4. Check the graph on a few candles

Open the **Backtest** tab, choose **Logic graph** in the left toolbar and pick your graph. As you replay, the panel under the chart shows what the graph computes. **Next trade** advances until the graph triggers. This is the quickest way to catch a graph that does not do what you meant.

## 5. Preview an auto backtest

1. Open the **Auto backtest** tab.
2. Choose the **Graph**, one or more **Markets** and one or more **Timeframes**.
3. Set **From** and **To**. The start must be before the end.
4. Choose **Simulate**. The button counts the candles processed.

When it finishes, the app says how many trades it previewed. Nothing is saved yet. Choose a market and a timeframe under **Chart** to see the previewed trades on the chart.

![The controls of the Auto backtest tab: graph, markets, timeframes, date range, Simulate and Generate trades](/img/docs/auto-backtest-controls.png "The controls of the Auto backtest tab.")

1. **Graph**: the trading graph to run.
2. **Markets**: one or more markets.
3. **Timeframes**: one or more timeframes.
4. **From**: the start of the range.
5. **To**: the end of the range.
6. **Saved trades** and **Preview**: how many trades are stored, and how many the last simulation found.
7. **Simulate** runs a preview. Nothing is saved.
8. **Generate trades** saves the previewed trades.
9. The trash button deletes all saved trades of the strategy.

## 6. Generate and save the trades

1. Choose **Generate trades**. It is enabled only when there is a preview to save.
2. Watch the progress line: **Processed**, **Calculated**, **Persisted**, **Forwarded**, **Failed** and **Duplicates skipped**.
3. Choose **Failed trades** to see why any trade was rejected. Rejected trades are not saved and do not count in the statistics.

Duplicates of trades you already saved are skipped, so running the same range twice does not double the history. After you change a graph, choose **Delete all saved trades** (the trash button) and generate again, so old and new logic are not mixed. This cannot be undone.

## 7. Read the result

Open the **Analysis** tab. The equity curve, **Key metrics** and the trade table now describe many more trades than a manual session usually gives you. See [Analysis](/docs/analysis). A result from many trades is more reliable than one from a few, but it is still a result on past data.

## Common problems

- **Simulate is disabled** or **Please select a Node Graph**: choose a graph, at least one market and one timeframe. If the list says **No graphs found**, assign a graph on the **Edit** tab.
- **A warning about insufficient indicator history**: an indicator needs earlier candles before it has a value. Start the date range earlier or use a shorter period.
- **The preview finds no trades**: check that the **Trigger** can become true and that the date range and timeframes suit it. Use **Logic graph** in the **Backtest** tab to see the graph on a chart.
- **Indicator graphs cannot contain execution nodes**: you are in an indicator graph. Create a **Trading graph**.

## Next

- [Find the best stop loss and take profit](/docs/find-best-stop-loss-and-take-profit)
- [Find out when your strategy works and when it does not](/docs/find-when-a-strategy-works-and-when-it-does-not)
- Reference: [Build logic with the graph editor](/docs/graph-editor), [Node reference](/docs/graph-nodes), [Auto backtest](/docs/auto-backtest).
