Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How would you program this algorithm?

I'm new to programming so I am still struggling on how to convert my algorithms from English to computer code.
So how would you program this simple algorithm?:

Tell the algorithm to sell everything if there are three consistent negative movements in the stock
Tell the algorithm to buy maximum amount of shares with given capital if there are three consistent positive movements in the stock

Also, does anyone have any suggested literature to read to make the learning process easier to program algorithms?

Thanks.

6 responses

anyone?

Use the history function and the order_target_percent function. For more details see: https://www.quantopian.com/help

Hello Gustav,

It's doable, but you'll need to refine your language a bit, since "three consistent negative movements" and "three consistent positive movements" are not yet requirements that could be translated directly into code. Daily or minutely bars or something else? Three consecutive bars? What do you mean by consistent?

As a tip, you might consider some type of smoothing of minutely bars, so that your algo is not just responding to noise (e.g. see http://pandas.pydata.org/pandas-docs/version/0.15.0/computation.html).

Regarding reading, I would start with everything Quantopian has available on their site (help docs, FAQs, etc.). For Python, you could try the free e-book, http://www.greenteapress.com/thinkpython/. Also, Pandas is very handy. See https://www.quantopian.com/posts/working-with-history-dataframes for starters.

Grant

Thanks everyone!

Grant is right, but here was my take on it. Be forewarned, I am very novice, so if there are any issues with the code please bring it up.

[Sorry, had to fix a few things...]

Here's the same code I posted, but using the 3 higher highs for entry (2 actually, one would need 4 prices to get 3 higher prices). And 3 (again 2 really) lower lows for exit. Curious results...
This code performs the test to get out 10 minutes before the EOD and the test to get in 5 minutes before EOD. By doing this one actually uses the current day's prices as part of the logic.

@Calvin W. You realize that your algo will be running every minute right? And that the last "1d" daily period it finds will be the last close price for that minute, just past. So, eventually, at some point in the day, when price rises enough (or fall enough), you'll trigger to get in (or out), and then trigger again, the next minute, and the next. Plotting cost_basis helps to see what's going on. And printing out when your logic gets in or out also helps.