Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie Question regarding some functions

I am no developer but would like to create the following logic:

Start of the day list top 10 gainers
Check EPS for those listed 10 gainers
IF EPS>=0 then
Buy "$X"

Check the increase/decrease value of the purchased stock
IF above 3% of purchase price sell
Else Hold

For stocks purchased and decline in value
Sell if it hits 1% drop

Looking at the functions and tutorials I am not able to figure out how to check the EPS values, I know this logic is destine to fail but I want to try it out just for fun to see what happens. :)

Thank you!

4 responses

This might get you started. It is pretty much section 1.

Make sure to Go to Backtest > Log Output

Note that I chose EPS > 0, rather than EPS >= 0. You should be able to tweak this easy enough via Clone Algorithm

Read more about the evolution of this algorithm here:

https://www.quantopian.com/posts/using-pipeline-to-select-the-top-30-stocks-based-on-their-returns

Let me know if you have any questions.

Hi Whimsbee , first of all thank you for sharing this, looking at the code ( and I am no coder) I am not sure where and what defines a sale? with this code all purchases are being sold at the end of each day? Also I don't understand why when I ran a backtest starting on March 2016, first day only 2 stocks with a $100 is bought and not more. Could not get the logic behind it. and lastly, how can I implement a sale of a stock if it goes up 3% that day or 1% down from the purchase price for the same day. If not sold, lets say up 2% would still like to initiate a sale instead of a hold.

Again thank you so much for responding.

When testing, I have only been doing a week because it does take some time to compute. I just did a 1 year backtest on your strategy and it is attached.

Did you make sure that your start and end dates are different when setting up your Backtest? Because it is possible not to have any stocks selected in your requirements per day and if you only did a short period, you might have only received the one hit.

Don't be afraid to Attach your BackTest so that I can see what you are talking about ;)

So, I have not attempted your second nor third sections - a lot of time well spent working on the first part. We will get there in due time.

Click on Source Code. The last section does the ordering. Right now the only strategy is to order all of the daily picks, 10 shares each.

def SimpleEPS_timeToTrade(context, data):  
    print "Time to Trade!"  
    for i in context.todays_entries:  
        print "Ordering: " + str(i.symbol)  
        order(i, 10)  

See more here: https://www.quantopian.com/help#api-order

I am still new here, so I cannot describe well the parameters of order(). But order(i,10) orders 10 shares of each of cotext.todays_entries.

context.todays_entries is set in before_trading_start() as you prescribed in the first section of your original post.

before_trading_start() is a key function for a quantopian algorithm and it is called each "day" before anything else.

initialize() is another key function that is called only once to setup everything and is essentially not called again. (How quantopian does this in the backend, I am gathering, is slightly different, but that is the gist.)

SimpleEPS_TimeToTrade() is called twice a day because I have set that up with schedule_function in the initialize function.

handle_data() is another key function that is called every minute. You likely don't need to concern yourself with this function.


To implement your second and third sections, you might put that logic in the SimpleEPS_TimeToTradefunction. :yoda:

Sorry, I did not know how to post the test results. This back test for the year 2016 starting at March provides a solid 20% gain. Since you are coding I am happy to share my ideas :)) This is just for fun for me... But it would be great to be able to make gains from something you designed.

So for the code ran above pretty much your code with the morning and evening trade times adjusted to the hr.

I still don't get why day 1 there is only 3 trades == $64. Also the trade quantities since random, not sure if I understand the logic. Eventually the start of the day should be fairly simple.

List all trades starting above 3% gain
List all those with EPS above "0"
Buy order those stocks, not sure how to distribute it here.
if gain from buy to day close or whichever comes first is above 3% sell otherwise hold
if the loss from the purchase price hits -1% sell.

My idea is very simple if there are stocks opening at or over 3% it is most likely (this me talking) will be gaining more that day. So for a short return you got on the train and get out on the first stop.

I hope this makes sense. Please let me know what you can do and we can go from there :)