[PYTHON] Quantopian Tutorial LESSON 1, 2

Introduction

I decided to use Quantopian at A certain event, so I went to Tutorial. I tried to summarize the points that are likely to be points. Since it is a super-translation that has been chewed (welcome to Tsukkomi), please refer to the official document if you want to check it properly.

Features of Quantopian

What is Quantopian? If you refer to this article, the features that I found to be good are as follows. ..

How to use

Register as a user at www.quantopian.com and select Research --Algorithms --New Algorithm to display the IDE on your browser.

You can create an algorithm by editing the template code in various ways, and select Build Algorithm to run a simple test. Select Run Full Backtest for further verification.

Unique rules for writing code

Quantopian has its own namespace and function name rules. If you do not understand this, you will suddenly get stuck, so I will explain it as carefully as possible.

context object

A global instance is created without defining anything. The functions described below access the attributes of this instance to get the information you need. It seems to be a good idea to add arbitrary attributes to this context object without creating a global variable (object). This is my imagination, but if all the parameters passed to the function are used as arguments, it will be inefficient to share the same parameter with multiple functions, or the docstring will be huge and it will be difficult. So I think it was designed like this.

The following is an example of setting the context object. By writing like this, an Apple object is created.

context.aapl = symbol('AAPL')

data object

Like the context, it suddenly exists. The data object contains various APIs for pricing information.

In the example below, the latest stock price is obtained from the Apple object created above.

data.current(context.aapl, 'price')

LESSON 2 Core Functions Skip LESSON 1 and start with LESSON 2. As for the reason, I decided that this should be understood first, which is included in the above-mentioned original rule.

The Quantopian algorithm has three core functions. These must be defined by the user, not the built-in functions.

initialize() You will definitely need a function with this name. Called at the start of algorithm testing. Specify a context object as an argument.

handle_data() If you define a function with this name, it will be executed every minute (every minute of the test period). It is used to get price data and to set up a portfolio (buy, sell). Specify the context object and the data object as arguments. If you want to run it daily or weekly, use the schedule_function () function introduced in LESSON 7.

before_trading_start() Called daily before the start of trading. It is used when setting a stock to be traded on the day. Specify the context object and the data object as arguments.

Hello world Familiarize yourself with the IDE before getting into the tutorial.

Write the code below, select Save (if not Saved), and then select Build Algorithm.

def initialize(context):
    print('hello world')

image.png

You should see something like the above in the right pane. ʻThe initialize ()function is always called at the start of the algorithm, soprint ('hello world')is executed. The standard output is displayed in the Logs window. If you want to keep a log of the progress of the algorithm, it is recommended to writeprint in the handle_data () `function.

By default, SPY (SPDR S & P 500 ETF) is set as the benchmark. Since the above algorithm does not take any position, it is displayed as 0%.

Next, let's clone the algorithm from LESSON 2 in the tutorial.

https://www.quantopian.com/tutorials/getting-started#lesson2

image.png

Selecting Clone on the right side above will launch the IDE in a new tab and clone the algorithm.

Select Build Algorithm to test the algorithm.

image.png

You can see that it is output to Logs every minute, unlike the case of Hello world mentioned above. Select More to enlarge the Logs window.

LESSON 1 Introduction Let's clone the algorithm from LESSON 1 in the tutorial.

https://www.quantopian.com/tutorials/getting-started#lesson1

image.png

Select Build Algorithm to test the algorithm.

image.png

The above code is an algorithm for longing Apple stock. ʻThe second argument '1.00' of order_target_percent () means that all (100%) of the funds are allocated. The ʻorder_target_percent () function will be introduced in LESSON 4.

Select Run Full Backtest on the right to perform a detailed backtest of the algorithm.

image.png

-> LESSON 3

Recommended Posts

Quantopian Tutorial LESSON 10
Quantopian Tutorial LESSON 8
Quantopian Tutorial LESSON 1, 2
Quantopian Tutorial LESSON 6
Quantopian Tutorial LESSON 9
Quantopian Tutorial LESSON 5
Quantopian Tutorial LESSON 3
Quantopian Tutorial LESSON 7
Quantopian Tutorial LESSON 4
Quantopian Tutorial LESSON 11
PyODE Tutorial 2
Python tutorial
PyODE Tutorial 1
PyODE Tutorial 3
TensorFlow tutorial tutorial