---
title: "Getting Started"
description: "Quants take a scientific approach to trading, applying concepts from mathematics, time series analysis, statistics, computer science, and machine learning."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/getting-started
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/getting-started.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/01%20Key%20Concepts/01%20Getting%20Started
modified: 2026-10-30
index: https://www.quantconnect.com/llms.txt
---

# Getting Started

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Key Concepts > Getting Started

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

### GET STARTED WITH WRITING ALGORITHMS

Guide through creating a project, running your first backtest, and live algo trading.

![Terminal Home Page](https://cdn.quantconnect.com/i/tu/cloud-platform-getting-started.gif)

---

### Follow these steps to write your first trading algorithm:

1. Create a new project.

The process to creating a new project depends on if you use the [Cloud Platform](https://www.quantconnect.com/docs/v2/cloud-platform/projects/getting-started.md#03-Create-Projects), [Local Platform](https://www.quantconnect.com/docs/v2/local-platform/projects/getting-started.md#03-Create-Projects), or [CLI](https://www.quantconnect.com/docs/v2/lean-cli/projects/project-management.md#02-Create-Projects).

Start every Python file in your project with `from AlgorithmImports import *`. Your algorithm class inherits only from `QCAlgorithm`. To share logic between algorithms, put it in helper classes and store instances of them as attributes, such as `self._risk = RiskHelper(self)`.
2. In the [initialize](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md) method, add a [universe](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/key-concepts.md) to the algorithm.

For example, you may want to trade the US Equities that make up the S&P 500.

```python
self._universe = self.universe.etf('SPY')
self.add_universe(self._universe)
```
3. In the `initialize` method, enable the following [settings](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#10-Set-Algorithm-Settings).

```python
self.settings.automatic_indicator_warm_up = True
self.settings.seed_initial_prices = True
```
4. In the `initialize` method, add a [Scheduled Event](https://www.quantconnect.com/docs/v2/writing-algorithms/scheduled-events.md) to rebalance the portfolio each week.

```python
self.schedule.on(self.date_rules.week_start('SPY'), self.time_rules.at(8, 0), self._rebalance)
```
5. Add an [on_securities_changed](https://www.quantconnect.com/docs/v2/writing-algorithms/universes/key-concepts.md#06-Security-Changed-Events) method that creates an [indicator](https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/key-concepts.md) for each stock in the universe.

For example, you may want to track the trailing 1-month returns of each stock.

```python
def on_securities_changed(self, changes):
    for security in changes.added_securities:
        security.roc = self.roc(security, 21, Resolution.DAILY)
```

[Duck typing](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/duck-typing.md) the indicator onto the `Security` object saves you from creating a new data structure.
6. Add your trading rules and [position sizing](https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/position-sizing.md) to the `_rebalance` method that runs each week.

```python
def _rebalance(self):
    # Get the stocks that are currently in the universe.
    securities = [self.securities[symbol] for symbol in self._universe.selected]
    # Select the 10 stocks with the greatest trailing returns.
    selected_assets = sorted([s for s in securities if s.roc.is_ready], key=lambda s: s.roc)[-10:]
    # Form an equal-weighted portfolio.
    targets = [PortfolioTarget(security.symbol, 1/len(selected_assets)) for security in selected_assets]
    self.set_holdings(targets, True)
```
7. Delete the [on_data](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md) method that comes with the template algorithm.

Congratulations! You just wrote your first trading algorithm.

```python
from AlgorithmImports import *

class BasicTemplateAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2020, 1, 1)
        self.set_end_date(2021, 1, 1)
        self.set_cash(100000)
        self._universe = self.universe.etf('SPY')
        self.add_universe(self._universe)
        self.settings.automatic_indicator_warm_up = True
        self.settings.seed_initial_prices = True
        self.schedule.on(self.date_rules.week_start('SPY'), self.time_rules.at(8, 0), self._rebalance)

    def on_securities_changed(self, changes):
        for security in changes.added_securities:
            security.roc = self.roc(security, 21, Resolution.DAILY)

    def _rebalance(self):
        # Get the stocks that are currently in the universe.
        securities = [self.securities[symbol] for symbol in self._universe.selected]
        # Select the 10 stocks with the greatest trailing returns.
        selected_assets = sorted([s for s in securities if s.roc.is_ready], key=lambda s: s.roc)[-10:]
        # Form an equal-weighted portfolio.
        targets = [PortfolioTarget(security.symbol, 1/len(selected_assets)) for security in selected_assets]
        self.set_holdings(targets, True)
```
