1.年回報
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.filters.morningstar import Q1500US
def initialize(context):
"""
Called once at the start of the algorithm.
"""
set_benchmark(symbol('QQQ'))
context.firstBuy = False
context.firstYear = "%d"%(get_datetime("US/Eastern").date().year)
context.leverage = 1.0
# Rebalance every day, 1 hour after market open.
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1))
# Record tracking variables at the end of each day.
schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
def my_assign_weights(context, data):
"""
Assign weights to securities that we want to order.a
"""
pass
def my_rebalance(context,data):
"""
Execute orders according to our schedule_function() timing.
"""
currentYear = "%d"%(get_datetime("US/Eastern").date().year)
if currentYear != context.firstYear or context.firstBuy == False:
context.firstYear = currentYear
context.firstBuy = True
order_target_percent(symbol('QQQ'), 0.5 * context.leverage)
order_target_percent(symbol('TLT'), 0.5 * context.leverage)
pass
def my_record_vars(context, data):
"""
Plot variables at the end of each day.
"""
pass
def handle_data(context,data):
"""
Called every minute.
"""
pass