Learning to Build Trading Algorithms on QuantConnect

Discover the power of QuantConnect for building trading algorithms! This blog explores the use of moving averages and RSI, providing real-life examples to showcase how these tools contribute to a successful strategy.

Learning to Build Trading Algorithms on QuantConnect
Photo by Jamie Street / Unsplash

As an aspiring quant trader, I am constantly exploring new tools and platforms to build and refine trading algorithms. Recently, I discovered QuantConnect, a platform specifically designed to facilitate algorithmic trading strategies. This post will provide an introduction to QuantConnect, discuss its key features, and use two specific algorithm examples to showcase how this platform can help develop trading strategies.

What is QuantConnect?

QuantConnect is a cloud-based algorithmic trading platform that allows traders to backtest, research, and deploy their own trading strategies. It offers access to a wide array of data sources, alternative datasets, and tools, making it ideal for those looking to dive deep into the financial markets and explore quantitative strategies. Whether you’re interested in equities, options, futures, or crypto, QuantConnect enables seamless integration of multi-asset portfolios.

Key features of QuantConnect include:

  • Cloud-based research terminals for analyzing vast datasets.
  • Backtesting capabilities to assess the effectiveness of strategies before live deployment.
  • Access to alternative data and machine learning tools for more advanced strategy development.
  • LEAN, an open-source algorithmic trading engine that supports on-premise and cloud trading.

QuantConnect provides everything a trader needs, from idealization and research to real-time execution, enabling the development of robust quantitative strategies.


The Basics: Moving Averages and RSI

Before diving into our algorithm examples, it's essential to understand the basic indicators used—Moving Averages (MA) and the Relative Strength Index (RSI).

Moving Averages (MA):

A Moving Average (MA) smooths out price data over a set period by creating a constantly updated average price. MAs are useful for identifying trends and potential buy/sell signals. A common trading strategy is to buy when the price crosses above the moving average and sell when it falls below.

Relative Strength Index (RSI):

The RSI is a momentum oscillator that measures the speed and change of price movements on a scale from 0 to 100. Traditionally, an RSI above 70 is considered overbought (potential sell signal), while an RSI below 30 is considered oversold (potential buy signal).

Pseudo-Code Overview

Both of the following examples use the QuantConnect platform, with slight variations in their implementation of Moving Averages and RSI.

Common Setup for Both Algorithms:

#Import QuantConnect libraries
from AlgorithmImports import *

#Define the Algorithm class
class TradingAlgorithm(QCAlgorithm):

  # Initialize settings
  def Initialize(self):
      SetStartDate and SetEndDate
      SetCash (e.g., 5000)
      AddEquity for a ticker (e.g., SPY)
      Add indicators: SMA and RSI
      Set Warmup period

In both examples, we define the start date, set the initial capital, and add equity and indicators such as SMA and RSI. The warmup period ensures that indicators have enough historical data to start making calculations.

Example 1: Moving Average Strategy

In this example, the algorithm buys when the price crosses above a 100-period SMA and sells when the price falls below 90% of the buy price. Here’s a summary of the pseudo-code:

If not Invested and Current Price > SMA:
    Buy (SetHoldings 100%)
If Invested and Current Price < 90% of Buy Price:
    Sell (Liquidate)

The strategy focuses on catching price trends based on the Simple Moving Average (SMA) and includes basic risk management by liquidating the position if the price drops below 90% of the entry point.

Results:

For a test period from 2006 to 2023, this strategy yielded:

$26,490.04 Equity

$17,692.21 Net Profit

1.544% PSR

429.80% Return

While these results appear impressive, it’s essential to remember that the strategy was applied over an extended bullish period. Hence, market conditions likely played a significant role in these positive outcomes.

Example 2: 30-Minute Consolidated Strategy

This second algorithm builds on the first by adding a 30-minute data consolidation. The SMA is calculated based on the 30-minute closing prices, and trades are executed based on the relationship between the closing price and the SMA.

Consolidate data to 30-minute intervals
If not Invested and Close Price > 30-min SMA:
    Buy (SetHoldings 100%)
If Invested and Close Price < 30-min SMA:
    Sell (Liquidate)

This algorithm aims to improve trading precision by using shorter, consolidated intervals rather than daily data.

Results:

For a shorter test period (June 6, 2023), the results were:

$5,005.13 Equity

$4,612.05 Holdings

0% Return

Although the return is minimal, the limited test period and market conditions could explain this outcome. Additionally, the strategy’s performance may improve with more fine-tuning and longer testing.


Moving Forward with QuantConnect

These examples demonstrate just a small part of what can be achieved using QuantConnect. Moving averages and RSI are fundamental indicators, but QuantConnect’s platform supports far more complex strategies using advanced statistical models and machine learning.

Conclusion

I’m excited to continue exploring QuantConnect, particularly its ability to backtest and optimize more advanced trading strategies. Stay tuned for next week’s post, where we’ll dive deeper into the platform’s capabilities and explore more complex strategies that incorporate cutting-edge quantitative methods.

QuantConnect offers a powerful platform for aspiring quant traders like myself, and I look forward to continuing this journey with more advanced strategies.


Join the Conversation

What are your thoughts on QuantConnect as a tool for learning algorithmic trading? Have you used QuantConnect before, or do you have ideas for new algorithms? Let me know your insights in the comments!

Thank you for taking the time to read this post! Your support means a lot to me. If you enjoyed this article and want to stay updated with future posts, please consider subscribing for free to receive notifications. You can also use [linktr.ee/sean.rocinante] to stay connected through LinkedIn and other social platforms. I look forward to sharing more valuable insights with you!