Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Time-based orders

Hi. I'm new to Quantopian, and Python. I've seen tons of posts on rules-based orders, but nothing yet on time-based orders. This is probably quite naive, but how would I code an order to buy every minute for 30 minutes from 930a to 10a? Or, is there a VWAP order type I could use for one order from 930a to 10a?

Related, how would I code an order to buy each day for the first 4 days of the month and then sell on the 5th?

Thx in advance!!
BC

7 responses

Brett,

I suggest seeing https://www.quantopian.com/help#ide-schedulefunction on the help page. Note also that handle_data is called every minute, if you include it in your algo (not required).

Regarding a VWAP order type for backtesting & Quantopian paper trading, I don't think it is supported. However, see https://www.quantopian.com/help#api-VWAPBestEffort .

Thanks, Grant! I see you name all over the boards - great job. I get it conceptually, but could use more examples to replicate the syntax. I'm still not sure how to code a loop that starts at time X and ends at time Y. Where can I find more examples to piece together what I need?

Perhaps the easiest way to do it is to use schedule_function to kick things off at a specific time every day, and then use a counter to execute your function for N times sequentially. No time now to write an example, but it is doable. Or, I think you can schedule the same function multiple times per day. It'd be kinda ugly, but you'd just end up scheduling the same function 30 times, for example.

I'm stuck after a couple days work. I'm using syntax off the website, but have two issues: 1. the program keeps buying after 90 minutes (it should stop), 2. the program buys twice every minute (it should buy once). Can anyone help get me over the finish line? I'd really appreciate it. :)

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

set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))  
set_commission(commission.PerShare(cost=0.0050, min_trade_cost=1))

total_minutes=59  
for i in range(1, total_minutes):  

    if i <= total_minutes:  
        schedule_function(handle_data,date_rules.every_day(),time_rules.market_open(minutes=i))  


schedule_function(close_positions, date_rules.every_day(), time_rules.market_close(minutes=1))  

def handle_data(context,data):

order_percent(context.SPY, 0.01)

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

Oops I was tinkering... just change the 59 back to 90 to get back to my original code.

The 'initialize' function gets automatically executed exactly once at the beginning of a backtest (or when an algo is first set up for live or paper trading). As the name implies it initializes things (once). Within the initialize function you may schedule other functions to run periodically using the 'schedule_function' method. The 'handle_data' function gets automatically executed every minute during trading hours - you do not need to schedule it.

Dan, thanks. This was just enough info to help me re-think things and get it right.