Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trying to learn.... could use a hand :)

Hey everybody, I'm new to Quantopian. I love the idea and am pumped to dive in, just having a bit of a tough time for some reason.

I've got a bit of a computer programming background but have only really ever used Visual Basic and some C++. For some reason the logic with stocks has me a little stumped. I feel that while going through the tutorials it'd be a breeze but obviously I was wrong. Though I followed along and seemingly understood them (until I got to the pipeline where I fell off the track a little), I'm still having issues here. If somebody could help me make a nice simple strategy that I could build on it'd be much appreciated.

For something to build on I'd like a strategy that just trades 1 equity, say AAPL for now. If the previous days bar was green (close > open) then if the following days price exceeds yesterdays high then buy (buy the minute the price surpassed yesterdays high). If the price ever closes below the low of the green bar which signaled a buy then sell. Also, if the open price for any day is higher then the price at which the equity was purchased then sell.

I don't expect this to be a good strategy by any means, I just figured it'd incorporate a decent amount of different logic, time scales (daily with minute), opens and closes, etc etc.

If there are any questions or something maybe I've left out please let me know, thanks!

7 responses

Here's a quick and dirty algo to get you started, most of it should be pretty intuitive, you may question the following code in before_trading_start

if context.aapl not in context.portfolio.positions:  
        if context.close[-1] > context.open[-1]:  
            context.trade_day = True  
            context.low_signal = context.low[-1]  

This code is written this way to basically create a setup (context.trade_day) so once we get into handle_data we know if we should look for a trade today instead of checking yesterday's prices every minute. It also records the low on the day that the signal (close>open) occurred since if we just called context.low[-1] later on it would be yesterday's low not the low of the signal day like you specified. Hopefully this gets you started.

Awesome Dustin, thank you very much for that. Through the tutorial I wasn't seeing the ability to use [-1] to reference a previous price (maybe I overlooked it) so instead was playing with taking a range of days and then concatenating the result. Also, your explanation of why to add that piece of code in before_trading_start makes sense as well. Thanks again, I'll look at this closer and hopefully it'll set me on my way. Take care and happy holidays!

No problem Andy, be careful when referencing prior prices with [-1], [2] etc. I used them in before_trading_start so [-1] (the latest item in the list) was yesterday's close, if you used that during the trading session you would get info from that day. I just looked at your post again and realized your trade entry was if current price exceeded yesterday's high not yesterday's close, so here's an edited algo.

Yeah I caught that, no worries. It was more or less to get an idea of how a few things work so I have something to modify/reference. I took a stab at it a bit this morning and got a few lines punched out. I have a piece of code from thinkorswim that I'm trying to convert over to quantopian and there are a couple issues there. I can't reference them for ya right now but when I have a chance I'll show a few lines on this thread and maybe you'd be able to help me. Thanks again, that little bit of code you'be written has taken me leaps and bounds already.

Alright, so I've played with the code and got it working to something I'm somewhat happy with. I'd like it to scan through a list of stocks now that have decent volume and priced between say $2 and $10. If they meet the criteria for a buy then buy them whent he price exceeds the trigger. If anybody has some code to do this it'd be appreciated, thanks to all who took the take the time to lend a hand here.

You're getting into having a need for pipeline now, I don't have much time but you may have a look at some pipeline examples, search for Robinhood extreme vetting, I think the author's name is Charles and he puts a lot of comments in the code so you may be able to cut and paste bits of that algo to meet your needs.

I figured as much, thought maybe there was a way I could just loop through every equity in the Q500 without having to deal with pipeline as much but maybe that isn't possible.

I'll have a look at your suggestion and piece together what I can from it.

Thank you very much again!