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?]