Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with one-time trade, completely new to Quantopian programming..

Hi,

I have only spent a few hours toying with Quantopian and I am predictably stuck. I wanted to do a buy-and-hold test.
For example, buy a set of securities on March 15, 2002 and hold on until now.
Just one transaction per security for all time. I started from the trading example that rebalances an equally weighted
portfolio of 9 sector ETFs.

I either get an invalid syntax error or if it runs, no trade happens.

    if exchange_time.date() == datetime.date(2002,3,15):     <--- this results in no trades

   if exchange_time.date() == datetime.datetime(2002,3,15,0,0,tzinfo=):   <--- this results in invalid syntax.

    if exchange_time.date() == '2002-03-15':       <--- this results in no trades  
        # Do nothing if there are open orders:  
        if has_orders(context):  
            print('has open orders - doing nothing!')  
            return  
        rebalance(context, data, exchange_time)  

Clearly the date condition is not met. How can I trade only on a specific date? (or date/time for that matter)

Thank you

3 responses

Hello Georgios,

You're getting there.
Those functions are presenting you with objects, you want to compare to a string in your third example.

Try this:
a. Click on the margin under handle_data() somewhere on a line number.
b. Click 'Build Algorithm'
c. When stopped at that breakpoint, in the debugger console window, type:

get_datetime().date()  

d. Click the little right triangle next to the output (at its center or a little to the right or on the point of it), that will expand that object.
e. Find the input area again (maybe click the triangle again to collapse that structure).
f Hit up arrow to recall the last command, and this time wrap it in str(), like:

str(get_datetime().date())  

That will return the string for you.
Some sort of magic. :)

More:

# daily: 00:00:00+00:00 and minutely like 14:33:00-05:00  
date   = str(get_datetime().date()) # data date like 2014-01-03  
time   = str(get_datetime().time()) # data time like 00:00:00  
bar_dt = get_datetime().astimezone(timezone('US/Eastern'))  
context.date    = date   # for other functions  
exchange_time   = get_datetime().astimezone(timezone('US/Eastern'))  
exchange_minute = (exchange_time.hour * 60) + exchange_time.minute - 570  

You don't really need to check the date for your test. Just run it from a specific date.
I've attached an example.

Hi,
the str() cast was what I needed. Thank you Gary.