Quantopian Tutorial LESSON 7

This is a continuation of Last time.

LESSON 7 Scheduling Functions

Trading Calendars I will skip it, and I will explain when futures come out.

Scheduling Functions So far, we have used handle_data () as the timing to execute the algorithm. This is done every minute, but if you trade every minute you're likely to go bankrupt easily with a fee.

As a matter of course, you may want to process daily or monthly. The schedule_function () function can schedule the algorithm at a specified frequency.

position Keyword arguments value
1 func Function name to execute
2 date_rules Daily rules
3 time_rules Hourly rules

The code below runs the rebalance () function every day, one hour after the start of trading [^ 1].

schedule_function(func=rebalance,
                  date_rules=date_rules.every_day(),
                  time_rules=time_rules.market_open(hours=1))

Once again, the instances date_rules and time_rules suddenly popped up even though they didn't define anything ... this also seems to be Quantopian's own rules.

The date_rules and time_rules objects have the following methods.

date_rules

time_rules

The code below runs the weekly_trades () function 30 minutes before the end of each weekend.

schedule_function(weekly_trades, date_rules.week_end(), time_rules.market_close(minutes=30))

The code below extends SPY by 10% of the portfolio at the beginning of the week and closes positions 30 minutes before the end of the weekend trading. The code can be cloned from here (https://www.quantopian.com/tutorials/getting-started#lesson7).

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

    schedule_function(open_positions, date_rules.week_start(), time_rules.market_open())
    schedule_function(close_positions, date_rules.week_end(), time_rules.market_close(minutes=30))

def open_positions(context, data):
    order_target_percent(context.spy, 0.10)

def close_positions(context, data):
    order_target_percent(context.spy, 0)

schedule_function () is skipped if the market is closed. You can also skip half-day transactions by setting half_days = False.

When dealing with financial data, handling holidays is very troublesome, so I'm grateful for this area.

LESSON 6<-->LESSON8


[^ 1]: time_rules.market_open () normally returns 9:30 (ET: Eastern Standard Time).

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 7
Quantopian Tutorial LESSON 4
Quantopian Tutorial LESSON 11
PyODE Tutorial 2
Python tutorial
PyODE Tutorial 1
TensorFlow tutorial tutorial