[PYTHON] Quantopian Tutorial LESSON 4

This is a continuation of Last time.

LESSON 4 Ordering Securities

ʻOrder_target_percent ()` function is used to lengthen or short the stock. The first argument is the intensity of the issue created by LESSON 3, and the second argument is the ratio when all assets are 1 (hereinafter portfolio). Specify.

In the example below, AAPL is 50% longer than the portfolio.

order_target_percent(sid(24), 0.50)

If you do not have a position before performing the above, 0.5 funds will be extended, but please note that at this point the amount invested will be different if other positions are included.

To short-circuit, specify a negative value for the second argument.

order_target_percent(sid(24), -0.50)

The code below lengths AAPL by 60% of the portfolio and shortens SPY by 40% of the portfolio. The code can be cloned from here (https://www.quantopian.com/tutorials/getting-started#lesson4). data.can_trade () will be discussed in LESSON 5.

def initialize(context):
    context.aapl = sid(24)
    context.spy = sid(8554)

def handle_data(context, data):
    # Note: data.can_trade() is explained in the next lesson
    if data.can_trade(context.aapl):
        order_target_percent(context.aapl, 0.60)
    if data.can_trade(context.spy):
        order_target_percent(context.spy, -0.40)

LESSON 3<-->LESSON5

Recommended Posts

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