Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to buy stock at a specific time?

I am new to quantopian and want to make an algorithm that compares the price of a specific security at 10.10 to the price at 9.10 and then buys or sells the security depending on the trend. Does someone know how to do this, or if it is at all possible?

4 responses

Simplest way, without having to deal with datetimes in python (ugh!), is to use the schedule function and perform your tests, your entry and your exit in separate methods:

• UpdateTest peforms the period offset test for last period > first period. A flag is set MorningTrendIsUp if the condition is true.
• HandleEntry tests the flag set in UpdateTest to determine if a trade should be placed.
• HandleExit closes any open positions.

Note: commissions and slippage (as they stand in the Q) will eat this strategy alive. If you're trying to pickup nickels in front of the steam roller, then you'll have to set the slippage and commissions to something other than the default.

[Quantopian, so, here's an interesting affectation of this strategy -- no leverage ever shows up. It's in and out in a single day, no overnight positions. Curious no?]

A rather interesting surprise. It sells short rather than buys, and based on intraday moves, a specific intraday move. Oh, and it's leveraged 2x. But should be good for day traders.
No statistical significance of course. Backtested more than 3 months and I'm sure it falls down the stairs, bump, bump, bump.
Still...

It did not fare out well over 2 years when I tested it...
Thanks for making the algorithm and disproving my idea... ;)
However when I look at the transactions it buys or sells at 9.26 and then exits the position at 9.56. how would I change the function to buy at 10.10 and exit at 15.55?

@Lucas M,

So you see that the triggers that drive your algo's timing are setup inside these method calls:

schedule_function(func=UpdateTest,   date_rule=date_rules.every_day(),  time_rule=time_rules.market_close(minutes=135))  
schedule_function(func=HandleEntry,  date_rule=date_rules.every_day(),  time_rule=time_rules.market_close(minutes=35))  
schedule_function(func=HandleExit,   date_rule=date_rules.every_day(),  time_rule=time_rules.market_close(minutes=5))  

If you study the schedule_function you'll find that there are other ways to specify the time of day at which methods will be triggered.

Look inside the Help/API for that method "schedule_function" and you'll find your answer.