go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Quantopian -- The basics
 
Subject: Quantopian -- The basics
Author: WebSpider
Posted on: 04/29/2019 10:45:33 PM

The Algorithm Bare Bone:

# Import Algorithm API
import quantopian.algorithm as algo


def initialize(context):
    # Initialize algorithm parameters
    context.minute_count = 0
    context.day_count = 0
    context.minutely_message = "Minute {}."
    context.daily_message = "Day {}."
    context.weekly_message = "Time to place some trades!"

    # Schedule rebalance function
    algo.schedule_function(
        rebalance,
        date_rule=algo.date_rules.week_start(),
        time_rule=algo.time_rules.market_open()
    )

def handle_data(context, data):
    context.minute_count += 1
    if context.minute_count%60 == 0:
        log.info(context.minutely_message, context.minute_count)
    
def before_trading_start(context, data):
    # Execute any daily actions that need to happen
    # before the start of a trading session
    context.day_count += 1
    log.info(context.daily_message, context.day_count)


def rebalance(context, data):
    # Execute rebalance logic
    log.info(context.weekly_message)    


The output:
2019-01-02 05:45 before_trading_start:29 INFO Day 1.
2019-01-02 07:30 handle_data:23 INFO Minute 60.
2019-01-02 08:30 handle_data:23 INFO Minute 120.
2019-01-02 09:30 handle_data:23 INFO Minute 180.
2019-01-02 10:30 handle_data:23 INFO Minute 240.
2019-01-02 11:30 handle_data:23 INFO Minute 300.
2019-01-02 12:30 handle_data:23 INFO Minute 360.
2019-01-03 05:45 before_trading_start:29 INFO Day 2.
2019-01-03 07:00 handle_data:23 INFO Minute 420.
2019-01-03 08:00 handle_data:23 INFO Minute 480.
2019-01-03 09:00 handle_data:23 INFO Minute 540.
2019-01-03 10:00 handle_data:23 INFO Minute 600.
2019-01-03 11:00 handle_data:23 INFO Minute 660.
2019-01-03 12:00 handle_data:23 INFO Minute 720.
2019-01-03 13:00 handle_data:23 INFO Minute 780.
2019-01-04 05:45 before_trading_start:29 INFO Day 3.
2019-01-04 07:30 handle_data:23 INFO Minute 840.
2019-01-04 08:30 handle_data:23 INFO Minute 900.
2019-01-04 09:30 handle_data:23 INFO Minute 960.
2019-01-04 10:30 handle_data:23 INFO Minute 1020.
2019-01-04 11:30 handle_data:23 INFO Minute 1080.
2019-01-04 12:30 handle_data:23 INFO Minute 1140.
2019-01-07 05:45 before_trading_start:29 INFO Day 4.
2019-01-07 06:31 rebalance:34 INFO Time to place some trades!
2019-01-07 07:00 handle_data:23 INFO Minute 1200.
2019-01-07 08:00 handle_data:23 INFO Minute 1260.
2019-01-07 09:00 handle_data:23 INFO Minute 1320.
2019-01-07 10:00 handle_data:23 INFO Minute 1380.
2019-01-07 11:00 handle_data:23 INFO Minute 1440.
2019-01-07 12:00 handle_data:23 INFO Minute 1500.
2019-01-07 13:00 handle_data:23 INFO Minute 1560.
2019-01-08 05:45 before_trading_start:29 INFO Day 5.
2019-01-08 07:30 handle_data:23 INFO Minute 1620.
2019-01-08 08:30 handle_data:23 INFO Minute 1680.
2019-01-08 09:30 handle_data:23 INFO Minute 1740.
2019-01-08 10:30 handle_data:23 INFO Minute 1800.
2019-01-08 11:30 handle_data:23 INFO Minute 1860.
2019-01-08 12:30 handle_data:23 INFO Minute 1920.
2019-01-09 05:45 before_trading_start:29 INFO Day 6.
2019-01-09 07:00 handle_data:23 INFO Minute 1980.
2019-01-09 08:00 handle_data:23 INFO Minute 2040.
2019-01-09 09:00 handle_data:23 INFO Minute 2100.
2019-01-09 10:00 handle_data:23 INFO Minute 2160.
2019-01-09 11:00 handle_data:23 INFO Minute 2220.
2019-01-09 12:00 handle_data:23 INFO Minute 2280.
2019-01-09 13:00 handle_data:23 INFO Minute 2340.
2019-01-10 05:45 before_trading_start:29 INFO Day 7.
2019-01-10 07:30 handle_data:23 INFO Minute 2400.
2019-01-10 08:30 handle_data:23 INFO Minute 2460.
2019-01-10 09:30 handle_data:23 INFO Minute 2520.
2019-01-10 10:30 handle_data:23 INFO Minute 2580.
2019-01-10 11:30 handle_data:23 INFO Minute 2640.
2019-01-10 12:30 handle_data:23 INFO Minute 2700.
2019-01-11 05:45 before_trading_start:29 INFO Day 8.
2019-01-11 07:00 handle_data:23 INFO Minute 2760.
2019-01-11 08:00 handle_data:23 INFO Minute 2820.
2019-01-11 09:00 handle_data:23 INFO Minute 2880.
2019-01-11 10:00 handle_data:23 INFO Minute 2940.
2019-01-11 11:00 handle_data:23 INFO Minute 3000.
2019-01-11 12:00 handle_data:23 INFO Minute 3060.
2019-01-11 13:00 handle_data:23 INFO Minute 3120.
2019-01-14 05:45 before_trading_start:29 INFO Day 9.
2019-01-14 06:31 rebalance:34 INFO Time to place some trades!


What's going on here?
  • Function handle_data(context, data) (-- optional) has been triggered by system every minute;
  • Function before_trading_start(context, data) has been triggered by system (45 minutes) before trading start;
  • Function rebalance(context, data) has been triggered by Scheduler by its own specific rules.


    Replies:


    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.