top of page
  • Nikhil Adithyan

Introduction to Algorithmic Trading with Quantopian

Updated: Oct 30, 2020

Learn to create simple Trading Algorithms in Quantopian




Algorithmic Trading


Before diving into Algorithmic Trading, it is essential to know about an Algorithm. Basically, an Algorithm is a set of instructions used to solve simple or complex problems. Due to the increasing trend of technology and programming, traders and investors transformed the system of stock trading to be aligned with technology, and that’s where Algorithmic Trading came into action. Algorithmic Trading is an automated process of trading stocks that executes orders when a given set of instructions are satisfied. This specific type of trading was developed mainly for speed and to overcome market emotions.


Quantopian for Algorithmic Trading


Quantopian is a US-based company where, people can collaborate, learn, develop, test, and share their trading algorithms and backtesting results. Quantopian provides its users with free education, various quant tools, and data so that anyone can pursue quantitative analysis and algorithmic trading. To get your hands-on Quantopian, you must have some basic knowledge of python as the code as it is the only programming language supported on the Quantopian console. Let’s code an algorithm in Quantopian and backtest it!


Creating an Algorithm in Quantopian


In this article, we are going to create a basic Simple Moving Average (SMA) trading strategy to trade the stocks of FAANG (Facebook, Amazon, Apple, Netflix, and Google). Along with that, we will also leverage our trades for better Sharpe ratio and returns


Step-1: Creating an Account in Quantopian


Before going to our coding section, remember that you must have an account in Quantopian for getting access to its coding console and notebooks. If you successfully created or own an account in Quantopian, you can find ‘Research’ in the navigation bar and in that, select ‘Algorithms’ under its options. After that, you will land on the ‘Algorithms’ page where you can find the ‘New Algorithm’ option. Select the option and you will land on a coding console page where we are going to code our trading algorithm.


Step-2: Initializing and Scheduling our Trading Function


Follow the code and its explanation mentioned below to initialize and schedule our trading function.


Explanation: Firstly, we are defining a function called ‘initialize’ which takes context (a Python dictionary which stores information) as a parameter and runs only once when our program starts running. Inside that, we store the information about the companies we are interested in using a function called ‘sid’. And don’t worry about the numbers inside it because, when you type ‘sid’ in the Quantopian console, a search bar pop-ups and you just have to enter the name of the company you’re interested in. After that, we call ‘schedule_function’ to run our trading algorithm which we will be creating in just a moment. 


Step-3: Creating a Trading Algorithm


Our step three of creating a Trading Algorithm is divided into three sections: Getting historical data, Creating SMA indicators, and finally creating the Trading Strategy.


a) Getting Historical Data:

As I’ve mentioned before, we are going to get the stock prices of FAANG and use it for building our strategy. Follow the code and explanation to extract the price data of FAANG. Before that, we have to define a function ‘ma_strategy’ which we have initialized in the previous code.


Explanation: As I said, we are starting our code by defining a function ‘ma_strategy’ where all our created algorithms are implemented. After defining our function, we are extracting the historical price data using a function called ‘history’. In that function, we are passing some specific parameters to get a company’s price data.


b) Creating SMA Indicators:


Under the function ‘ma_strategy’ we just created, we are going to calculate SMA 20 and SMA 50 of FAANG for building our trading strategy. Follow the code and explanation to create SMA indicators of FAANG.


Explanation: We are starting our code by creating SMA 20 indicators for FAANG followed by SMA 50 indicators using a simple function ‘mean’.


c) Creating Trading Strategy and Leveraging:


In this section we are going to create an SMA Crossover trading strategy and also we are going to create a variable that avoids execution of multiple orders for one single SMA crossover. By seeing the code and explanation, you will get a clear picture of what I’m saying and remember that, every code comes under the ‘ma_strategy’ function.


Explanation: Firstly, we are creating a variable called ‘open_orders’ by passing on a function ‘get_open_orders’ which returns only order objects with active status. After that, we are creating a ‘buy’ strategy which returns buy when SMA 20 of FAANG is greater than SMA 50. You can also observe that the orders are executed only when it is not listed in our variable ‘open_orders’ which helps us from multiple executions of orders for one single crossover. Followed by our ‘buy’ strategy, we are creating a ‘sell’ strategy which shorts the stock when SMA 50 of FAANG is greater than SMA 20. Throughout our strategy, we utilized the function ‘order_target_percent’ which helps us to execute orders for a specified company and a specified percentage. For instance, we have mentioned ‘0.20’ in all of our ‘buy’ strategies which means, we hold 20% of our portfolio in that stock. Finally, we are creating a custom variable using the ‘record’ function to create a variable ‘leverage’ that leverages all of our trades.


We are now set to backtest our trading algorithm. To do that, you can hit the ‘build algorithm’ option for a sample backtest or ‘Run full backtest’ for a full-fledged backtest report.


Backtested Results



We’ve got nice backtested results! Our created trading algorithm total returns are 60.02% which is comparatively higher than our benchmark (SPY). We also got a good Sharpe ratio of 2.65%. The above-represented results’ time frame was from 11 Sep 2019 to 10 Sep 2020 and my initial capital amount was 10 million. You can also view more details by hovering on other options like ‘Risk’, ‘Performance’, and much more. You can also view your trading transactions in the ‘Activity’ option. Note that, every piece of coding and explanations are strictly for educational purpose and should not be considered as a piece of Investment advice. And don't’ worry if you missed any coding sections as I’ve provided the full code for this Trading Algorithm. Apart from this algorithm, there is much more you can do with Quantopian. You can also make use of the notebook section in Quantopian for further analysis and tools like pipeline and so on. You can follow Quantopian documentation for more tools. So never stop learning and never stop coding!


Happy Algo Trading!


Full code:



 

2 comments
bottom of page